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"
34 #include "wx/layout.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
, 0, "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 m_created
= FALSE
; // set to TRUE by CreateGrid
944 m_displayed
= FALSE
; // set to TRUE by OnPaint
946 m_table
= (wxGridTableBase
*) NULL
;
947 m_cellEditCtrl
= (wxWindow
*) NULL
;
951 m_currentCellCoords
= wxGridNoCellCoords
;
953 int colLblH
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
954 int rowLblW
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
956 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
959 wxSize(rowLblW
, colLblH
) );
961 m_rowLabelWin
= new wxGridRowLabelWindow( this,
964 wxSize(rowLblW
, -1) );
966 m_colLabelWin
= new wxGridColLabelWindow( this,
969 wxSize(-1, colLblH
) );
971 m_gridWin
= new wxGridWindow( this,
975 wxPoint(rowLblW
, colLblH
),
978 SetTargetWindow( m_gridWin
);
982 bool wxGrid::CreateGrid( int numRows
, int numCols
)
986 wxLogError( wxT("wxGrid::CreateGrid(numRows, numCols) called more than once") );
994 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
995 m_table
->SetView( this );
1008 if ( m_numRows
<= 0 )
1009 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1011 if ( m_numCols
<= 0 )
1012 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1014 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1015 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1017 if ( m_rowLabelWin
)
1019 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1023 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1026 m_labelTextColour
= wxColour( _T("BLACK") );
1028 // TODO: something better than this ?
1030 m_labelFont
= this->GetFont();
1031 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1033 m_rowLabelHorizAlign
= wxLEFT
;
1034 m_rowLabelVertAlign
= wxCENTRE
;
1036 m_colLabelHorizAlign
= wxCENTRE
;
1037 m_colLabelVertAlign
= wxTOP
;
1039 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1040 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1042 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1043 m_defaultRowHeight
+= 8;
1045 m_defaultRowHeight
+= 4;
1048 m_rowHeights
.Alloc( m_numRows
);
1049 m_rowBottoms
.Alloc( m_numRows
);
1051 for ( i
= 0; i
< m_numRows
; i
++ )
1053 m_rowHeights
.Add( m_defaultRowHeight
);
1054 rowBottom
+= m_defaultRowHeight
;
1055 m_rowBottoms
.Add( rowBottom
);
1058 m_colWidths
.Alloc( m_numCols
);
1059 m_colRights
.Alloc( m_numCols
);
1061 for ( i
= 0; i
< m_numCols
; i
++ )
1063 m_colWidths
.Add( m_defaultColWidth
);
1064 colRight
+= m_defaultColWidth
;
1065 m_colRights
.Add( colRight
);
1068 // TODO: improve this ?
1070 m_defaultCellFont
= this->GetFont();
1072 m_gridLineColour
= wxColour( 128, 128, 255 );
1073 m_gridLinesEnabled
= TRUE
;
1075 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1077 m_dragRowOrCol
= -1;
1078 m_isDragging
= FALSE
;
1080 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1081 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1083 m_currentCellCoords
= wxGridNoCellCoords
;
1085 m_selectedTopLeft
= wxGridNoCellCoords
;
1086 m_selectedBottomRight
= wxGridNoCellCoords
;
1088 m_editable
= TRUE
; // default for whole grid
1090 m_inOnKeyDown
= FALSE
;
1093 // TODO: extend this to other types of controls
1095 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1102 #if defined(__WXMSW__)
1103 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1107 m_cellEditCtrl
->Show( FALSE
);
1108 m_cellEditCtrlEnabled
= TRUE
;
1109 m_editCtrlType
= wxGRID_TEXTCTRL
;
1113 void wxGrid::CalcDimensions()
1116 GetClientSize( &cw
, &ch
);
1118 if ( m_numRows
> 0 && m_numCols
> 0 )
1120 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1121 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1123 // TODO: restore the scroll position that we had before sizing
1126 GetViewStart( &x
, &y
);
1127 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1128 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1134 void wxGrid::CalcWindowSizes()
1137 GetClientSize( &cw
, &ch
);
1139 if ( m_cornerLabelWin
->IsShown() )
1140 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1142 if ( m_colLabelWin
->IsShown() )
1143 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1145 if ( m_rowLabelWin
->IsShown() )
1146 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1148 if ( m_gridWin
->IsShown() )
1149 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1153 // this is called when the grid table sends a message to say that it
1154 // has been redimensioned
1156 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1160 switch ( msg
.GetId() )
1162 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1164 size_t pos
= msg
.GetCommandInt();
1165 int numRows
= msg
.GetCommandInt2();
1166 for ( i
= 0; i
< numRows
; i
++ )
1168 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1169 m_rowBottoms
.Insert( 0, pos
);
1171 m_numRows
+= numRows
;
1174 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1176 for ( i
= pos
; i
< m_numRows
; i
++ )
1178 bottom
+= m_rowHeights
[i
];
1179 m_rowBottoms
[i
] = bottom
;
1185 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1187 int numRows
= msg
.GetCommandInt();
1188 for ( i
= 0; i
< numRows
; i
++ )
1190 m_rowHeights
.Add( m_defaultRowHeight
);
1191 m_rowBottoms
.Add( 0 );
1194 int oldNumRows
= m_numRows
;
1195 m_numRows
+= numRows
;
1198 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1200 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1202 bottom
+= m_rowHeights
[i
];
1203 m_rowBottoms
[i
] = bottom
;
1209 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1211 size_t pos
= msg
.GetCommandInt();
1212 int numRows
= msg
.GetCommandInt2();
1213 for ( i
= 0; i
< numRows
; i
++ )
1215 m_rowHeights
.Remove( pos
);
1216 m_rowBottoms
.Remove( pos
);
1218 m_numRows
-= numRows
;
1223 m_colWidths
.Clear();
1224 m_colRights
.Clear();
1225 m_currentCellCoords
= wxGridNoCellCoords
;
1229 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1230 m_currentCellCoords
.Set( 0, 0 );
1233 for ( i
= 0; i
< m_numRows
; i
++ )
1235 h
+= m_rowHeights
[i
];
1236 m_rowBottoms
[i
] = h
;
1244 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1246 size_t pos
= msg
.GetCommandInt();
1247 int numCols
= msg
.GetCommandInt2();
1248 for ( i
= 0; i
< numCols
; i
++ )
1250 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1251 m_colRights
.Insert( 0, pos
);
1253 m_numCols
+= numCols
;
1256 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1258 for ( i
= pos
; i
< m_numCols
; i
++ )
1260 right
+= m_colWidths
[i
];
1261 m_colRights
[i
] = right
;
1267 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1269 int numCols
= msg
.GetCommandInt();
1270 for ( i
= 0; i
< numCols
; i
++ )
1272 m_colWidths
.Add( m_defaultColWidth
);
1273 m_colRights
.Add( 0 );
1276 int oldNumCols
= m_numCols
;
1277 m_numCols
+= numCols
;
1280 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1282 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1284 right
+= m_colWidths
[i
];
1285 m_colRights
[i
] = right
;
1291 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1293 size_t pos
= msg
.GetCommandInt();
1294 int numCols
= msg
.GetCommandInt2();
1295 for ( i
= 0; i
< numCols
; i
++ )
1297 m_colWidths
.Remove( pos
);
1298 m_colRights
.Remove( pos
);
1300 m_numCols
-= numCols
;
1304 #if 0 // leave the row alone here so that AppendCols will work subsequently
1306 m_rowHeights
.Clear();
1307 m_rowBottoms
.Clear();
1309 m_currentCellCoords
= wxGridNoCellCoords
;
1313 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1314 m_currentCellCoords
.Set( 0, 0 );
1317 for ( i
= 0; i
< m_numCols
; i
++ )
1319 w
+= m_colWidths
[i
];
1332 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1334 wxRegionIterator
iter( reg
);
1337 m_rowLabelsExposed
.Empty();
1344 // TODO: remove this when we can...
1345 // There is a bug in wxMotif that gives garbage update
1346 // rectangles if you jump-scroll a long way by clicking the
1347 // scrollbar with middle button. This is a work-around
1349 #if defined(__WXMOTIF__)
1351 m_gridWin
->GetClientSize( &cw
, &ch
);
1352 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1353 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1356 // logical bounds of update region
1359 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1360 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1362 // find the row labels within these bounds
1366 for ( row
= 0; row
< m_numRows
; row
++ )
1368 if ( m_rowBottoms
[row
] < top
) continue;
1370 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1371 if ( rowTop
> bottom
) break;
1373 m_rowLabelsExposed
.Add( row
);
1381 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1383 wxRegionIterator
iter( reg
);
1386 m_colLabelsExposed
.Empty();
1393 // TODO: remove this when we can...
1394 // There is a bug in wxMotif that gives garbage update
1395 // rectangles if you jump-scroll a long way by clicking the
1396 // scrollbar with middle button. This is a work-around
1398 #if defined(__WXMOTIF__)
1400 m_gridWin
->GetClientSize( &cw
, &ch
);
1401 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1402 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1405 // logical bounds of update region
1408 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1409 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1411 // find the cells within these bounds
1415 for ( col
= 0; col
< m_numCols
; col
++ )
1417 if ( m_colRights
[col
] < left
) continue;
1419 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1420 if ( colLeft
> right
) break;
1422 m_colLabelsExposed
.Add( col
);
1430 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1432 wxRegionIterator
iter( reg
);
1435 m_cellsExposed
.Empty();
1436 m_rowsExposed
.Empty();
1437 m_colsExposed
.Empty();
1439 int left
, top
, right
, bottom
;
1444 // TODO: remove this when we can...
1445 // There is a bug in wxMotif that gives garbage update
1446 // rectangles if you jump-scroll a long way by clicking the
1447 // scrollbar with middle button. This is a work-around
1449 #if defined(__WXMOTIF__)
1451 m_gridWin
->GetClientSize( &cw
, &ch
);
1452 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1453 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1454 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1455 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1458 // logical bounds of update region
1460 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1461 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1463 // find the cells within these bounds
1466 int colLeft
, rowTop
;
1467 for ( row
= 0; row
< m_numRows
; row
++ )
1469 if ( m_rowBottoms
[row
] < top
) continue;
1471 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1472 if ( rowTop
> bottom
) break;
1474 m_rowsExposed
.Add( row
);
1476 for ( col
= 0; col
< m_numCols
; col
++ )
1478 if ( m_colRights
[col
] < left
) continue;
1480 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1481 if ( colLeft
> right
) break;
1483 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1484 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1493 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1496 wxPoint
pos( event
.GetPosition() );
1497 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1499 if ( event
.Dragging() )
1501 m_isDragging
= TRUE
;
1503 if ( event
.LeftIsDown() )
1505 switch( m_cursorMode
)
1507 case WXGRID_CURSOR_RESIZE_ROW
:
1509 int cw
, ch
, left
, dummy
;
1510 m_gridWin
->GetClientSize( &cw
, &ch
);
1511 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1513 wxClientDC
dc( m_gridWin
);
1515 dc
.SetLogicalFunction(wxINVERT
);
1516 if ( m_dragLastPos
>= 0 )
1518 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1520 dc
.DrawLine( left
, y
, left
+cw
, y
);
1525 case WXGRID_CURSOR_SELECT_ROW
:
1527 if ( (row
= YToRow( y
)) >= 0 &&
1528 !IsInSelection( row
, 0 ) )
1530 SelectRow( row
, TRUE
);
1539 m_isDragging
= FALSE
;
1542 // ------------ Left button pressed
1544 if ( event
.LeftDown() )
1546 // don't send a label click event for a hit on the
1547 // edge of the row label - this is probably the user
1548 // wanting to resize the row
1550 if ( YToEdgeOfRow(y
) < 0 )
1554 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1556 SelectRow( row
, event
.ShiftDown() );
1557 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1562 // starting to drag-resize a row
1564 m_rowLabelWin
->CaptureMouse();
1569 // ------------ Left double click
1571 else if (event
.LeftDClick() )
1573 if ( YToEdgeOfRow(y
) < 0 )
1576 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1581 // ------------ Left button released
1583 else if ( event
.LeftUp() )
1585 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1587 m_rowLabelWin
->ReleaseMouse();
1589 if ( m_dragLastPos
>= 0 )
1591 // erase the last line and resize the row
1593 int cw
, ch
, left
, dummy
;
1594 m_gridWin
->GetClientSize( &cw
, &ch
);
1595 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1597 wxClientDC
dc( m_gridWin
);
1599 dc
.SetLogicalFunction( wxINVERT
);
1600 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1601 HideCellEditControl();
1603 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
1604 SetRowSize( m_dragRowOrCol
, wxMax( y
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
1605 if ( !GetBatchCount() )
1607 // Only needed to get the correct rect.y:
1608 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
1610 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
1611 rect
.width
= m_rowLabelWidth
;
1612 rect
.height
= ch
- rect
.y
;
1613 m_rowLabelWin
->Refresh( TRUE
, &rect
);
1615 m_gridWin
->Refresh( FALSE
, &rect
);
1618 ShowCellEditControl();
1620 // Note: we are ending the event *after* doing
1621 // default processing in this case
1623 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1627 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1632 // ------------ Right button down
1634 else if ( event
.RightDown() )
1637 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1639 // no default action at the moment
1644 // ------------ Right double click
1646 else if ( event
.RightDClick() )
1649 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1651 // no default action at the moment
1656 // ------------ No buttons down and mouse moving
1658 else if ( event
.Moving() )
1660 m_dragRowOrCol
= YToEdgeOfRow( y
);
1661 if ( m_dragRowOrCol
>= 0 )
1663 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1665 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1666 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1671 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1672 if ( m_rowLabelWin
->GetCursor() == m_rowResizeCursor
)
1673 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1679 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1682 wxPoint
pos( event
.GetPosition() );
1683 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1685 if ( event
.Dragging() )
1687 m_isDragging
= TRUE
;
1689 if ( event
.LeftIsDown() )
1691 switch( m_cursorMode
)
1693 case WXGRID_CURSOR_RESIZE_COL
:
1695 int cw
, ch
, dummy
, top
;
1696 m_gridWin
->GetClientSize( &cw
, &ch
);
1697 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1699 wxClientDC
dc( m_gridWin
);
1701 dc
.SetLogicalFunction(wxINVERT
);
1702 if ( m_dragLastPos
>= 0 )
1704 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1706 dc
.DrawLine( x
, top
, x
, top
+ch
);
1711 case WXGRID_CURSOR_SELECT_COL
:
1713 if ( (col
= XToCol( x
)) >= 0 &&
1714 !IsInSelection( 0, col
) )
1716 SelectCol( col
, TRUE
);
1725 m_isDragging
= FALSE
;
1728 // ------------ Left button pressed
1730 if ( event
.LeftDown() )
1732 // don't send a label click event for a hit on the
1733 // edge of the col label - this is probably the user
1734 // wanting to resize the col
1736 if ( XToEdgeOfCol(x
) < 0 )
1740 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1742 SelectCol( col
, event
.ShiftDown() );
1743 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1748 // starting to drag-resize a col
1750 m_colLabelWin
->CaptureMouse();
1755 // ------------ Left double click
1757 if ( event
.LeftDClick() )
1759 if ( XToEdgeOfCol(x
) < 0 )
1762 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
1767 // ------------ Left button released
1769 else if ( event
.LeftUp() )
1771 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
1773 m_colLabelWin
->ReleaseMouse();
1775 if ( m_dragLastPos
>= 0 )
1777 // erase the last line and resize the col
1779 int cw
, ch
, dummy
, top
;
1780 m_gridWin
->GetClientSize( &cw
, &ch
);
1781 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1783 wxClientDC
dc( m_gridWin
);
1785 dc
.SetLogicalFunction( wxINVERT
);
1786 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1787 HideCellEditControl();
1789 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
1790 SetColSize( m_dragRowOrCol
, wxMax( x
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
1792 if ( !GetBatchCount() )
1794 // Only needed to get the correct rect.x:
1795 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
1797 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
1798 rect
.width
= cw
- rect
.x
;
1799 rect
.height
= m_colLabelHeight
;
1800 m_colLabelWin
->Refresh( TRUE
, &rect
);
1802 m_gridWin
->Refresh( FALSE
, &rect
);
1805 ShowCellEditControl();
1807 // Note: we are ending the event *after* doing
1808 // default processing in this case
1810 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1814 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1819 // ------------ Right button down
1821 else if ( event
.RightDown() )
1824 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1826 // no default action at the moment
1831 // ------------ Right double click
1833 else if ( event
.RightDClick() )
1836 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1838 // no default action at the moment
1843 // ------------ No buttons down and mouse moving
1845 else if ( event
.Moving() )
1847 m_dragRowOrCol
= XToEdgeOfCol( x
);
1848 if ( m_dragRowOrCol
>= 0 )
1850 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1852 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1853 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1858 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1859 if ( m_colLabelWin
->GetCursor() == m_colResizeCursor
)
1860 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1866 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
1868 if ( event
.LeftDown() )
1870 // indicate corner label by having both row and
1873 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
1879 else if ( event
.LeftDClick() )
1881 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
1884 else if ( event
.RightDown() )
1886 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
1888 // no default action at the moment
1892 else if ( event
.RightDClick() )
1894 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
1896 // no default action at the moment
1902 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
1905 wxPoint
pos( event
.GetPosition() );
1906 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1908 wxGridCellCoords coords
;
1909 XYToCell( x
, y
, coords
);
1911 if ( event
.Dragging() )
1913 m_isDragging
= TRUE
;
1914 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1916 // Hide the edit control, so it
1917 // won't interfer with drag-shrinking.
1918 if ( IsCellEditControlEnabled() )
1919 HideCellEditControl();
1920 if ( coords
!= wxGridNoCellCoords
)
1922 if ( !IsSelection() )
1924 SelectBlock( coords
, coords
);
1928 SelectBlock( m_currentCellCoords
, coords
);
1936 m_isDragging
= FALSE
;
1938 if ( coords
!= wxGridNoCellCoords
)
1940 if ( event
.LeftDown() )
1942 if ( event
.ShiftDown() )
1944 SelectBlock( m_currentCellCoords
, coords
);
1948 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1953 MakeCellVisible( coords
);
1954 SetCurrentCell( coords
);
1960 // ------------ Left double click
1962 else if ( event
.LeftDClick() )
1964 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1971 // ------------ Left button released
1973 else if ( event
.LeftUp() )
1975 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1977 if ( IsSelection() )
1979 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
1983 // Show the edit control, if it has
1984 // been hidden for drag-shrinking.
1985 if ( IsCellEditControlEnabled() )
1986 ShowCellEditControl();
1992 // ------------ Right button down
1994 else if ( event
.RightDown() )
1996 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2001 // no default action at the moment
2006 // ------------ Right double click
2008 else if ( event
.RightDClick() )
2010 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2015 // no default action at the moment
2019 // ------------ Moving and no button action
2021 else if ( event
.Moving() && !event
.IsButton() )
2023 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2030 // ------ interaction with data model
2032 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2034 switch ( msg
.GetId() )
2036 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2037 return GetModelValues();
2039 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2040 return SetModelValues();
2042 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2043 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2044 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2045 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2046 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2047 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2048 return Redimension( msg
);
2057 // The behaviour of this function depends on the grid table class
2058 // Clear() function. For the default wxGridStringTable class the
2059 // behavious is to replace all cell contents with wxEmptyString but
2060 // not to change the number of rows or cols.
2062 void wxGrid::ClearGrid()
2067 SetEditControlValue();
2068 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2073 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2075 // TODO: something with updateLabels flag
2079 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2085 bool ok
= m_table
->InsertRows( pos
, numRows
);
2087 // the table will have sent the results of the insert row
2088 // operation to this view object as a grid table message
2092 if ( m_numCols
== 0 )
2094 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2096 // TODO: perhaps instead of appending the default number of cols
2097 // we should remember what the last non-zero number of cols was ?
2101 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2103 // if we have just inserted cols into an empty grid the current
2104 // cell will be undefined...
2106 SetCurrentCell( 0, 0 );
2110 if ( !GetBatchCount() ) Refresh();
2113 SetEditControlValue();
2123 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2125 // TODO: something with updateLabels flag
2129 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2133 if ( m_table
&& m_table
->AppendRows( numRows
) )
2135 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2137 // if we have just inserted cols into an empty grid the current
2138 // cell will be undefined...
2140 SetCurrentCell( 0, 0 );
2143 // the table will have sent the results of the append row
2144 // operation to this view object as a grid table message
2147 if ( !GetBatchCount() ) Refresh();
2157 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2159 // TODO: something with updateLabels flag
2163 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2167 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2169 // the table will have sent the results of the delete row
2170 // operation to this view object as a grid table message
2172 if ( m_numRows
> 0 )
2173 SetEditControlValue();
2175 HideCellEditControl();
2178 if ( !GetBatchCount() ) Refresh();
2188 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2190 // TODO: something with updateLabels flag
2194 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2200 HideCellEditControl();
2201 bool ok
= m_table
->InsertCols( pos
, numCols
);
2203 // the table will have sent the results of the insert col
2204 // operation to this view object as a grid table message
2208 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2210 // if we have just inserted cols into an empty grid the current
2211 // cell will be undefined...
2213 SetCurrentCell( 0, 0 );
2217 if ( !GetBatchCount() ) Refresh();
2220 SetEditControlValue();
2230 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2232 // TODO: something with updateLabels flag
2236 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2240 if ( m_table
&& m_table
->AppendCols( numCols
) )
2242 // the table will have sent the results of the append col
2243 // operation to this view object as a grid table message
2245 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2247 // if we have just inserted cols into an empty grid the current
2248 // cell will be undefined...
2250 SetCurrentCell( 0, 0 );
2254 if ( !GetBatchCount() ) Refresh();
2264 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2266 // TODO: something with updateLabels flag
2270 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2274 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2276 // the table will have sent the results of the delete col
2277 // operation to this view object as a grid table message
2279 if ( m_numCols
> 0 )
2280 SetEditControlValue();
2282 HideCellEditControl();
2285 if ( !GetBatchCount() ) Refresh();
2297 // ----- event handlers
2300 // Generate a grid event based on a mouse event and
2301 // return the result of ProcessEvent()
2303 bool wxGrid::SendEvent( const wxEventType type
,
2305 wxMouseEvent
& mouseEv
)
2307 if ( type
== EVT_GRID_ROW_SIZE
||
2308 type
== EVT_GRID_COL_SIZE
)
2310 int rowOrCol
= (row
== -1 ? col
: row
);
2312 wxGridSizeEvent
gridEvt( GetId(),
2316 mouseEv
.GetX(), mouseEv
.GetY(),
2317 mouseEv
.ControlDown(),
2318 mouseEv
.ShiftDown(),
2320 mouseEv
.MetaDown() );
2322 return GetEventHandler()->ProcessEvent(gridEvt
);
2324 else if ( type
== EVT_GRID_RANGE_SELECT
)
2326 wxGridRangeSelectEvent
gridEvt( GetId(),
2330 m_selectedBottomRight
,
2331 mouseEv
.ControlDown(),
2332 mouseEv
.ShiftDown(),
2334 mouseEv
.MetaDown() );
2336 return GetEventHandler()->ProcessEvent(gridEvt
);
2340 wxGridEvent
gridEvt( GetId(),
2344 mouseEv
.GetX(), mouseEv
.GetY(),
2345 mouseEv
.ControlDown(),
2346 mouseEv
.ShiftDown(),
2348 mouseEv
.MetaDown() );
2350 return GetEventHandler()->ProcessEvent(gridEvt
);
2355 // Generate a grid event of specified type and return the result
2356 // of ProcessEvent().
2358 bool wxGrid::SendEvent( const wxEventType type
,
2361 if ( type
== EVT_GRID_ROW_SIZE
||
2362 type
== EVT_GRID_COL_SIZE
)
2364 int rowOrCol
= (row
== -1 ? col
: row
);
2366 wxGridSizeEvent
gridEvt( GetId(),
2371 return GetEventHandler()->ProcessEvent(gridEvt
);
2375 wxGridEvent
gridEvt( GetId(),
2380 return GetEventHandler()->ProcessEvent(gridEvt
);
2385 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2387 wxPaintDC
dc( this );
2389 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2390 m_numRows
&& m_numCols
)
2392 m_currentCellCoords
.Set(0, 0);
2393 SetEditControlValue();
2394 ShowCellEditControl();
2401 void wxGrid::OnSize( wxSizeEvent
& event
)
2408 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2410 if ( m_inOnKeyDown
)
2412 // shouldn't be here - we are going round in circles...
2414 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2417 m_inOnKeyDown
= TRUE
;
2419 // propagate the event up and see if it gets processed
2421 wxWindow
*parent
= GetParent();
2422 wxKeyEvent
keyEvt( event
);
2423 keyEvt
.SetEventObject( parent
);
2425 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2427 // try local handlers
2429 switch ( event
.KeyCode() )
2432 if ( event
.ControlDown() )
2434 MoveCursorUpBlock();
2443 if ( event
.ControlDown() )
2445 MoveCursorDownBlock();
2454 if ( event
.ControlDown() )
2456 MoveCursorLeftBlock();
2465 if ( event
.ControlDown() )
2467 MoveCursorRightBlock();
2476 if ( !IsEditable() )
2487 if ( event
.ControlDown() )
2489 event
.Skip(); // to let the edit control have the return
2498 if ( event
.ControlDown() )
2500 MakeCellVisible( 0, 0 );
2501 SetCurrentCell( 0, 0 );
2510 if ( event
.ControlDown() )
2512 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2513 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2530 // now try the cell edit control
2532 if ( IsCellEditControlEnabled() )
2534 event
.SetEventObject( m_cellEditCtrl
);
2535 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2541 m_inOnKeyDown
= FALSE
;
2545 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2547 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2549 // the event has been intercepted - do nothing
2554 m_currentCellCoords
!= wxGridNoCellCoords
)
2556 HideCellEditControl();
2557 SaveEditControlValue();
2560 m_currentCellCoords
= coords
;
2562 SetEditControlValue();
2566 ShowCellEditControl();
2568 if ( IsSelection() )
2570 wxRect
r( SelectionToDeviceRect() );
2572 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
2579 // ------ functions to get/send data (see also public functions)
2582 bool wxGrid::GetModelValues()
2586 // all we need to do is repaint the grid
2588 m_gridWin
->Refresh();
2596 bool wxGrid::SetModelValues()
2602 for ( row
= 0; row
< m_numRows
; row
++ )
2604 for ( col
= 0; col
< m_numCols
; col
++ )
2606 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2618 // Note - this function only draws cells that are in the list of
2619 // exposed cells (usually set from the update region by
2620 // CalcExposedCells)
2622 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2624 if ( !m_numRows
|| !m_numCols
) return;
2627 size_t numCells
= m_cellsExposed
.GetCount();
2629 for ( i
= 0; i
< numCells
; i
++ )
2631 DrawCell( dc
, m_cellsExposed
[i
] );
2636 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2638 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2639 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2641 #if !WXGRID_DRAW_LINES
2642 if ( m_gridLinesEnabled
)
2643 DrawCellBorder( dc
, coords
);
2646 DrawCellBackground( dc
, coords
);
2648 // TODO: separate functions here for different kinds of cells ?
2651 DrawCellValue( dc
, coords
);
2655 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2657 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2658 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2660 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2661 int row
= coords
.GetRow();
2662 int col
= coords
.GetCol();
2664 // right hand border
2666 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
2667 m_colRights
[col
], m_rowBottoms
[row
] );
2671 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
2672 m_colRights
[col
], m_rowBottoms
[row
] );
2676 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2678 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2679 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2681 int row
= coords
.GetRow();
2682 int col
= coords
.GetCol();
2684 dc
.SetBackgroundMode( wxSOLID
);
2686 if ( IsInSelection( coords
) )
2688 // TODO: improve this
2690 dc
.SetBrush( *wxBLACK_BRUSH
);
2694 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2697 dc
.SetPen( *wxTRANSPARENT_PEN
);
2699 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2700 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2702 m_rowHeights
[row
]-1 );
2706 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2708 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2709 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2711 int row
= coords
.GetRow();
2712 int col
= coords
.GetCol();
2714 dc
.SetBackgroundMode( wxTRANSPARENT
);
2716 if ( IsInSelection( row
, col
) )
2718 // TODO: improve this
2720 dc
.SetTextBackground( wxColour(0, 0, 0) );
2721 dc
.SetTextForeground( wxColour(255, 255, 255) );
2725 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2726 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2728 dc
.SetFont( GetCellFont(row
, col
) );
2731 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2734 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2735 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2736 rect
.SetWidth( m_colWidths
[col
] - 4 );
2737 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2739 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2744 // TODO: remove this ???
2745 // This is used to redraw all grid lines e.g. when the grid line colour
2748 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
2750 if ( !m_gridLinesEnabled
||
2752 !m_numCols
) return;
2754 int top
, bottom
, left
, right
;
2758 m_gridWin
->GetClientSize(&cw
, &ch
);
2760 // virtual coords of visible area
2762 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2763 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2767 reg
.GetBox(x
, y
, w
, h
);
2768 CalcUnscrolledPosition( x
, y
, &left
, &top
);
2769 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
2772 // avoid drawing grid lines past the last row and col
2774 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
2775 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
2777 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2779 // horizontal grid lines
2782 for ( i
= 0; i
< m_numRows
; i
++ )
2784 if ( m_rowBottoms
[i
] > bottom
)
2788 else if ( m_rowBottoms
[i
] >= top
)
2790 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
2795 // vertical grid lines
2797 for ( i
= 0; i
< m_numCols
; i
++ )
2799 if ( m_colRights
[i
] > right
)
2803 else if ( m_colRights
[i
] >= left
)
2805 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
2811 void wxGrid::DrawRowLabels( wxDC
& dc
)
2813 if ( !m_numRows
|| !m_numCols
) return;
2816 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2818 for ( i
= 0; i
< numLabels
; i
++ )
2820 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2825 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2827 if ( m_rowHeights
[row
] <= 0 ) return;
2829 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2831 dc
.SetPen( *wxBLACK_PEN
);
2832 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
2833 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
2835 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
2836 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
2838 dc
.SetPen( *wxWHITE_PEN
);
2839 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
2840 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
2842 dc
.SetBackgroundMode( wxTRANSPARENT
);
2843 dc
.SetTextForeground( GetLabelTextColour() );
2844 dc
.SetFont( GetLabelFont() );
2847 GetRowLabelAlignment( &hAlign
, &vAlign
);
2851 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2852 rect
.SetWidth( m_rowLabelWidth
- 4 );
2853 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2854 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2858 void wxGrid::DrawColLabels( wxDC
& dc
)
2860 if ( !m_numRows
|| !m_numCols
) return;
2863 size_t numLabels
= m_colLabelsExposed
.GetCount();
2865 for ( i
= 0; i
< numLabels
; i
++ )
2867 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2872 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2874 if ( m_colWidths
[col
] <= 0 ) return;
2876 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2878 dc
.SetPen( *wxBLACK_PEN
);
2879 dc
.DrawLine( m_colRights
[col
]-1, 0,
2880 m_colRights
[col
]-1, m_colLabelHeight
-1 );
2882 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
2883 m_colRights
[col
]-1, m_colLabelHeight
-1 );
2885 dc
.SetPen( *wxWHITE_PEN
);
2886 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
2887 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
2889 dc
.SetBackgroundMode( wxTRANSPARENT
);
2890 dc
.SetTextForeground( GetLabelTextColour() );
2891 dc
.SetFont( GetLabelFont() );
2893 dc
.SetBackgroundMode( wxTRANSPARENT
);
2894 dc
.SetTextForeground( GetLabelTextColour() );
2895 dc
.SetFont( GetLabelFont() );
2898 GetColLabelAlignment( &hAlign
, &vAlign
);
2901 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2903 rect
.SetWidth( m_colWidths
[col
] - 4 );
2904 rect
.SetHeight( m_colLabelHeight
- 4 );
2905 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2909 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2910 const wxString
& value
,
2915 long textWidth
, textHeight
;
2916 long lineWidth
, lineHeight
;
2917 wxArrayString lines
;
2919 dc
.SetClippingRegion( rect
);
2920 StringToLines( value
, lines
);
2921 if ( lines
.GetCount() )
2923 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2924 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2927 switch ( horizAlign
)
2930 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2934 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2943 switch ( vertAlign
)
2946 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2950 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2959 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2961 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2966 dc
.DestroyClippingRegion();
2970 // Split multi line text up into an array of strings. Any existing
2971 // contents of the string array are preserved.
2973 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2975 // TODO: this won't work for WXMAC ? (lines end with '\r')
2976 // => use wxTextFile functions then (VZ)
2979 while ( startPos
< (int)value
.Length() )
2981 pos
= value
.Mid(startPos
).Find( '\n' );
2986 else if ( pos
== 0 )
2988 lines
.Add( wxEmptyString
);
2992 if ( value
[startPos
+pos
-1] == '\r' )
2994 lines
.Add( value
.Mid(startPos
, pos
-1) );
2998 lines
.Add( value
.Mid(startPos
, pos
) );
3003 if ( startPos
< (int)value
.Length() )
3005 lines
.Add( value
.Mid( startPos
) );
3010 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3011 wxArrayString
& lines
,
3012 long *width
, long *height
)
3019 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3021 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3022 w
= wxMax( w
, lineW
);
3032 // ------ Edit control functions
3036 void wxGrid::EnableEditing( bool edit
)
3038 // TODO: improve this ?
3040 if ( edit
!= m_editable
)
3044 // TODO: extend this for other edit control types
3046 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3048 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3054 #if 0 // disabled for the moment - the cell control is always active
3055 void wxGrid::EnableCellEditControl( bool enable
)
3057 if ( m_cellEditCtrl
&&
3058 enable
!= m_cellEditCtrlEnabled
)
3060 m_cellEditCtrlEnabled
= enable
;
3062 if ( m_cellEditCtrlEnabled
)
3064 SetEditControlValue();
3065 ShowCellEditControl();
3069 HideCellEditControl();
3070 SaveEditControlValue();
3077 void wxGrid::ShowCellEditControl()
3081 if ( IsCellEditControlEnabled() )
3083 if ( !IsVisible( m_currentCellCoords
) )
3089 rect
= CellToRect( m_currentCellCoords
);
3091 // convert to scrolled coords
3093 int left
, top
, right
, bottom
;
3094 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3095 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3098 m_gridWin
->GetClientSize( &cw
, &ch
);
3100 // Make the edit control large enough to allow for internal margins
3101 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3104 #if defined(__WXMOTIF__)
3105 if ( m_currentCellCoords
.GetRow() == 0 ||
3106 m_currentCellCoords
.GetCol() == 0 )
3115 if ( m_currentCellCoords
.GetRow() == 0 ||
3116 m_currentCellCoords
.GetCol() == 0 )
3126 #if defined(__WXGTK__)
3129 if (left
!= 0) left_diff
++;
3130 if (top
!= 0) top_diff
++;
3131 rect
.SetLeft( left
+ left_diff
);
3132 rect
.SetTop( top
+ top_diff
);
3133 rect
.SetRight( rect
.GetRight() - left_diff
);
3134 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3136 rect
.SetLeft( wxMax(0, left
- extra
) );
3137 rect
.SetTop( wxMax(0, top
- extra
) );
3138 rect
.SetRight( rect
.GetRight() + 2*extra
);
3139 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3142 m_cellEditCtrl
->SetSize( rect
);
3143 m_cellEditCtrl
->Show( TRUE
);
3145 switch ( m_editCtrlType
)
3147 case wxGRID_TEXTCTRL
:
3148 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3151 case wxGRID_CHECKBOX
:
3152 // TODO: anything ???
3157 // TODO: anything ???
3161 case wxGRID_COMBOBOX
:
3162 // TODO: anything ???
3167 m_cellEditCtrl
->SetFocus();
3173 void wxGrid::HideCellEditControl()
3175 if ( IsCellEditControlEnabled() )
3177 m_cellEditCtrl
->Show( FALSE
);
3182 void wxGrid::SetEditControlValue( const wxString
& value
)
3188 s
= GetCellValue(m_currentCellCoords
);
3192 if ( IsCellEditControlEnabled() )
3194 switch ( m_editCtrlType
)
3196 case wxGRID_TEXTCTRL
:
3197 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3200 case wxGRID_CHECKBOX
:
3201 // TODO: implement this
3206 // TODO: implement this
3210 case wxGRID_COMBOBOX
:
3211 // TODO: implement this
3220 void wxGrid::SaveEditControlValue()
3224 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3226 if ( IsCellEditControlEnabled() )
3228 ctrl
= m_cellEditCtrl
;
3235 bool valueChanged
= FALSE
;
3237 switch ( m_editCtrlType
)
3239 case wxGRID_TEXTCTRL
:
3240 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3241 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3242 SetCellValue( m_currentCellCoords
,
3243 ((wxTextCtrl
*) ctrl
)->GetValue() );
3246 case wxGRID_CHECKBOX
:
3247 // TODO: implement this
3252 // TODO: implement this
3256 case wxGRID_COMBOBOX
:
3257 // TODO: implement this
3264 SendEvent( EVT_GRID_CELL_CHANGE
,
3265 m_currentCellCoords
.GetRow(),
3266 m_currentCellCoords
.GetCol() );
3273 // ------ Grid location functions
3274 // Note that all of these functions work with the logical coordinates of
3275 // grid cells and labels so you will need to convert from device
3276 // coordinates for mouse events etc.
3279 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3281 int row
= YToRow(y
);
3282 int col
= XToCol(x
);
3284 if ( row
== -1 || col
== -1 )
3286 coords
= wxGridNoCellCoords
;
3290 coords
.Set( row
, col
);
3295 int wxGrid::YToRow( int y
)
3299 for ( i
= 0; i
< m_numRows
; i
++ )
3301 if ( y
< m_rowBottoms
[i
] ) return i
;
3308 int wxGrid::XToCol( int x
)
3312 for ( i
= 0; i
< m_numCols
; i
++ )
3314 if ( x
< m_colRights
[i
] ) return i
;
3321 // return the row number that that the y coord is near the edge of, or
3322 // -1 if not near an edge
3324 int wxGrid::YToEdgeOfRow( int y
)
3328 for ( i
= 0; i
< m_numRows
; i
++ )
3330 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3332 d
= abs( y
- m_rowBottoms
[i
] );
3334 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3343 // return the col number that that the x coord is near the edge of, or
3344 // -1 if not near an edge
3346 int wxGrid::XToEdgeOfCol( int x
)
3350 for ( i
= 0; i
< m_numCols
; i
++ )
3352 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3354 d
= abs( x
- m_colRights
[i
] );
3356 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3365 wxRect
wxGrid::CellToRect( int row
, int col
)
3367 wxRect
rect( -1, -1, -1, -1 );
3369 if ( row
>= 0 && row
< m_numRows
&&
3370 col
>= 0 && col
< m_numCols
)
3372 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3373 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3374 rect
.width
= m_colWidths
[col
];
3375 rect
.height
= m_rowHeights
[ row
];
3382 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3384 // get the cell rectangle in logical coords
3386 wxRect
r( CellToRect( row
, col
) );
3388 // convert to device coords
3390 int left
, top
, right
, bottom
;
3391 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3392 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3394 // check against the client area of the grid window
3397 m_gridWin
->GetClientSize( &cw
, &ch
);
3399 if ( wholeCellVisible
)
3401 // is the cell wholly visible ?
3403 return ( left
>= 0 && right
<= cw
&&
3404 top
>= 0 && bottom
<= ch
);
3408 // is the cell partly visible ?
3410 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3411 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3416 // make the specified cell location visible by doing a minimal amount
3419 void wxGrid::MakeCellVisible( int row
, int col
)
3422 int xpos
= -1, ypos
= -1;
3424 if ( row
>= 0 && row
< m_numRows
&&
3425 col
>= 0 && col
< m_numCols
)
3427 // get the cell rectangle in logical coords
3429 wxRect
r( CellToRect( row
, col
) );
3431 // convert to device coords
3433 int left
, top
, right
, bottom
;
3434 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3435 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3438 m_gridWin
->GetClientSize( &cw
, &ch
);
3444 else if ( bottom
> ch
)
3446 int h
= r
.GetHeight();
3448 for ( i
= row
-1; i
>= 0; i
-- )
3450 if ( h
+ m_rowHeights
[i
] > ch
) break;
3452 h
+= m_rowHeights
[i
];
3453 ypos
-= m_rowHeights
[i
];
3456 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3457 // have rounding errors (this is important, because if we do, we
3458 // might not scroll at all and some cells won't be redrawn)
3459 ypos
+= GRID_SCROLL_LINE
/ 2;
3466 else if ( right
> cw
)
3468 int w
= r
.GetWidth();
3470 for ( i
= col
-1; i
>= 0; i
-- )
3472 if ( w
+ m_colWidths
[i
] > cw
) break;
3474 w
+= m_colWidths
[i
];
3475 xpos
-= m_colWidths
[i
];
3478 // see comment for ypos above
3479 xpos
+= GRID_SCROLL_LINE
/ 2;
3482 if ( xpos
!= -1 || ypos
!= -1 )
3484 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3485 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3486 Scroll( xpos
, ypos
);
3494 // ------ Grid cursor movement functions
3497 bool wxGrid::MoveCursorUp()
3499 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3500 m_currentCellCoords
.GetRow() > 0 )
3502 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3503 m_currentCellCoords
.GetCol() );
3505 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3506 m_currentCellCoords
.GetCol() );
3515 bool wxGrid::MoveCursorDown()
3517 // TODO: allow for scrolling
3519 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3520 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3522 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3523 m_currentCellCoords
.GetCol() );
3525 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3526 m_currentCellCoords
.GetCol() );
3535 bool wxGrid::MoveCursorLeft()
3537 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3538 m_currentCellCoords
.GetCol() > 0 )
3540 MakeCellVisible( m_currentCellCoords
.GetRow(),
3541 m_currentCellCoords
.GetCol() - 1 );
3543 SetCurrentCell( m_currentCellCoords
.GetRow(),
3544 m_currentCellCoords
.GetCol() - 1 );
3553 bool wxGrid::MoveCursorRight()
3555 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3556 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3558 MakeCellVisible( m_currentCellCoords
.GetRow(),
3559 m_currentCellCoords
.GetCol() + 1 );
3561 SetCurrentCell( m_currentCellCoords
.GetRow(),
3562 m_currentCellCoords
.GetCol() + 1 );
3571 bool wxGrid::MovePageUp()
3573 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3575 int row
= m_currentCellCoords
.GetRow();
3579 m_gridWin
->GetClientSize( &cw
, &ch
);
3581 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3582 int newRow
= YToRow( y
- ch
+ 1 );
3587 else if ( newRow
== row
)
3592 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3593 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3601 bool wxGrid::MovePageDown()
3603 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3605 int row
= m_currentCellCoords
.GetRow();
3606 if ( row
< m_numRows
)
3609 m_gridWin
->GetClientSize( &cw
, &ch
);
3611 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3612 int newRow
= YToRow( y
+ ch
);
3615 newRow
= m_numRows
- 1;
3617 else if ( newRow
== row
)
3622 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3623 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3631 bool wxGrid::MoveCursorUpBlock()
3634 m_currentCellCoords
!= wxGridNoCellCoords
&&
3635 m_currentCellCoords
.GetRow() > 0 )
3637 int row
= m_currentCellCoords
.GetRow();
3638 int col
= m_currentCellCoords
.GetCol();
3640 if ( m_table
->IsEmptyCell(row
, col
) )
3642 // starting in an empty cell: find the next block of
3648 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3651 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3653 // starting at the top of a block: find the next block
3659 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3664 // starting within a block: find the top of the block
3669 if ( m_table
->IsEmptyCell(row
, col
) )
3677 MakeCellVisible( row
, col
);
3678 SetCurrentCell( row
, col
);
3686 bool wxGrid::MoveCursorDownBlock()
3689 m_currentCellCoords
!= wxGridNoCellCoords
&&
3690 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3692 int row
= m_currentCellCoords
.GetRow();
3693 int col
= m_currentCellCoords
.GetCol();
3695 if ( m_table
->IsEmptyCell(row
, col
) )
3697 // starting in an empty cell: find the next block of
3700 while ( row
< m_numRows
-1 )
3703 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3706 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3708 // starting at the bottom of a block: find the next block
3711 while ( row
< m_numRows
-1 )
3714 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3719 // starting within a block: find the bottom of the block
3721 while ( row
< m_numRows
-1 )
3724 if ( m_table
->IsEmptyCell(row
, col
) )
3732 MakeCellVisible( row
, col
);
3733 SetCurrentCell( row
, col
);
3741 bool wxGrid::MoveCursorLeftBlock()
3744 m_currentCellCoords
!= wxGridNoCellCoords
&&
3745 m_currentCellCoords
.GetCol() > 0 )
3747 int row
= m_currentCellCoords
.GetRow();
3748 int col
= m_currentCellCoords
.GetCol();
3750 if ( m_table
->IsEmptyCell(row
, col
) )
3752 // starting in an empty cell: find the next block of
3758 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3761 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3763 // starting at the left of a block: find the next block
3769 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3774 // starting within a block: find the left of the block
3779 if ( m_table
->IsEmptyCell(row
, col
) )
3787 MakeCellVisible( row
, col
);
3788 SetCurrentCell( row
, col
);
3796 bool wxGrid::MoveCursorRightBlock()
3799 m_currentCellCoords
!= wxGridNoCellCoords
&&
3800 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3802 int row
= m_currentCellCoords
.GetRow();
3803 int col
= m_currentCellCoords
.GetCol();
3805 if ( m_table
->IsEmptyCell(row
, col
) )
3807 // starting in an empty cell: find the next block of
3810 while ( col
< m_numCols
-1 )
3813 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3816 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3818 // starting at the right of a block: find the next block
3821 while ( col
< m_numCols
-1 )
3824 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3829 // starting within a block: find the right of the block
3831 while ( col
< m_numCols
-1 )
3834 if ( m_table
->IsEmptyCell(row
, col
) )
3842 MakeCellVisible( row
, col
);
3843 SetCurrentCell( row
, col
);
3854 // ------ Label values and formatting
3857 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3859 *horiz
= m_rowLabelHorizAlign
;
3860 *vert
= m_rowLabelVertAlign
;
3863 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3865 *horiz
= m_colLabelHorizAlign
;
3866 *vert
= m_colLabelVertAlign
;
3869 wxString
wxGrid::GetRowLabelValue( int row
)
3873 return m_table
->GetRowLabelValue( row
);
3883 wxString
wxGrid::GetColLabelValue( int col
)
3887 return m_table
->GetColLabelValue( col
);
3898 void wxGrid::SetRowLabelSize( int width
)
3900 width
= wxMax( width
, 0 );
3901 if ( width
!= m_rowLabelWidth
)
3905 m_rowLabelWin
->Show( FALSE
);
3906 m_cornerLabelWin
->Show( FALSE
);
3908 else if ( m_rowLabelWidth
== 0 )
3910 m_rowLabelWin
->Show( TRUE
);
3911 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
3914 m_rowLabelWidth
= width
;
3921 void wxGrid::SetColLabelSize( int height
)
3923 height
= wxMax( height
, 0 );
3924 if ( height
!= m_colLabelHeight
)
3928 m_colLabelWin
->Show( FALSE
);
3929 m_cornerLabelWin
->Show( FALSE
);
3931 else if ( m_colLabelHeight
== 0 )
3933 m_colLabelWin
->Show( TRUE
);
3934 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
3937 m_colLabelHeight
= height
;
3944 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3946 if ( m_labelBackgroundColour
!= colour
)
3948 m_labelBackgroundColour
= colour
;
3949 m_rowLabelWin
->SetBackgroundColour( colour
);
3950 m_colLabelWin
->SetBackgroundColour( colour
);
3951 m_cornerLabelWin
->SetBackgroundColour( colour
);
3953 if ( !GetBatchCount() )
3955 m_rowLabelWin
->Refresh();
3956 m_colLabelWin
->Refresh();
3957 m_cornerLabelWin
->Refresh();
3962 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3964 if ( m_labelTextColour
!= colour
)
3966 m_labelTextColour
= colour
;
3967 if ( !GetBatchCount() )
3969 m_rowLabelWin
->Refresh();
3970 m_colLabelWin
->Refresh();
3975 void wxGrid::SetLabelFont( const wxFont
& font
)
3978 if ( !GetBatchCount() )
3980 m_rowLabelWin
->Refresh();
3981 m_colLabelWin
->Refresh();
3985 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3987 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3989 m_rowLabelHorizAlign
= horiz
;
3992 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3994 m_rowLabelVertAlign
= vert
;
3997 if ( !GetBatchCount() )
3999 m_rowLabelWin
->Refresh();
4003 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4005 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4007 m_colLabelHorizAlign
= horiz
;
4010 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4012 m_colLabelVertAlign
= vert
;
4015 if ( !GetBatchCount() )
4017 m_colLabelWin
->Refresh();
4021 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4025 m_table
->SetRowLabelValue( row
, s
);
4026 if ( !GetBatchCount() )
4028 wxRect rect
= CellToRect( row
, 0);
4029 if ( rect
.height
> 0 )
4031 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4033 rect
.width
= m_rowLabelWidth
;
4034 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4040 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4044 m_table
->SetColLabelValue( col
, s
);
4045 if ( !GetBatchCount() )
4047 wxRect rect
= CellToRect( 0, col
);
4048 if ( rect
.width
> 0 )
4050 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4052 rect
.height
= m_colLabelHeight
;
4053 m_colLabelWin
->Refresh( TRUE
, &rect
);
4059 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4061 if ( m_gridLineColour
!= colour
)
4063 m_gridLineColour
= colour
;
4065 wxClientDC
dc( m_gridWin
);
4067 DrawAllGridLines( dc
, wxRegion() );
4071 void wxGrid::EnableGridLines( bool enable
)
4073 if ( enable
!= m_gridLinesEnabled
)
4075 m_gridLinesEnabled
= enable
;
4077 if ( !GetBatchCount() )
4081 wxClientDC
dc( m_gridWin
);
4083 DrawAllGridLines( dc
, wxRegion() );
4087 m_gridWin
->Refresh();
4094 int wxGrid::GetDefaultRowSize()
4096 return m_defaultRowHeight
;
4099 int wxGrid::GetRowSize( int row
)
4101 if ( row
>= 0 && row
< m_numRows
)
4102 return m_rowHeights
[row
];
4104 return 0; // TODO: log an error here
4107 int wxGrid::GetDefaultColSize()
4109 return m_defaultColWidth
;
4112 int wxGrid::GetColSize( int col
)
4114 if ( col
>= 0 && col
< m_numCols
)
4115 return m_colWidths
[col
];
4117 return 0; // TODO: log an error here
4120 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4122 // TODO: replace this temp test code
4124 return wxColour( 255, 255, 255 );
4127 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4129 // TODO: replace this temp test code
4131 return wxColour( 255, 255, 255 );
4134 wxColour
wxGrid::GetDefaultCellTextColour()
4136 // TODO: replace this temp test code
4138 return wxColour( 0, 0, 0 );
4141 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4143 // TODO: replace this temp test code
4145 return wxColour( 0, 0, 0 );
4149 wxFont
wxGrid::GetDefaultCellFont()
4151 return m_defaultCellFont
;
4154 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
4156 // TODO: replace this temp test code
4158 return m_defaultCellFont
;
4161 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4163 // TODO: replace this temp test code
4169 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
4171 // TODO: replace this temp test code
4177 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4179 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4181 if ( resizeExistingRows
)
4185 for ( row
= 0; row
< m_numRows
; row
++ )
4187 m_rowHeights
[row
] = m_defaultRowHeight
;
4188 bottom
+= m_defaultRowHeight
;
4189 m_rowBottoms
[row
] = bottom
;
4195 void wxGrid::SetRowSize( int row
, int height
)
4199 if ( row
>= 0 && row
< m_numRows
)
4201 int h
= wxMax( 0, height
);
4202 int diff
= h
- m_rowHeights
[row
];
4204 m_rowHeights
[row
] = h
;
4205 for ( i
= row
; i
< m_numRows
; i
++ )
4207 m_rowBottoms
[i
] += diff
;
4211 // Note: we are ending the event *after* doing
4212 // default processing in this case
4214 SendEvent( EVT_GRID_ROW_SIZE
,
4219 // TODO: log an error here
4223 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4225 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4227 if ( resizeExistingCols
)
4231 for ( col
= 0; col
< m_numCols
; col
++ )
4233 m_colWidths
[col
] = m_defaultColWidth
;
4234 right
+= m_defaultColWidth
;
4235 m_colRights
[col
] = right
;
4241 void wxGrid::SetColSize( int col
, int width
)
4245 if ( col
>= 0 && col
< m_numCols
)
4247 int w
= wxMax( 0, width
);
4248 int diff
= w
- m_colWidths
[col
];
4249 m_colWidths
[col
] = w
;
4251 for ( i
= col
; i
< m_numCols
; i
++ )
4253 m_colRights
[i
] += diff
;
4257 // Note: we are ending the event *after* doing
4258 // default processing in this case
4260 SendEvent( EVT_GRID_COL_SIZE
,
4265 // TODO: log an error here
4269 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4271 // TODO: everything !!!
4275 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4277 // TODO: everything !!!
4281 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4283 // TODO: everything !!!
4287 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4289 // TODO: everything !!!
4293 void wxGrid::SetDefaultCellFont( const wxFont
& )
4295 // TODO: everything !!!
4299 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4301 // TODO: everything !!!
4305 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4307 // TODO: everything !!!
4311 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4313 // TODO: everything !!!
4320 // ------ cell value accessor functions
4323 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4327 m_table
->SetValue( row
, col
, s
.c_str() );
4328 if ( !GetBatchCount() )
4330 wxClientDC
dc( m_gridWin
);
4332 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4335 #if 0 // TODO: edit in place
4337 if ( m_currentCellCoords
.GetRow() == row
&&
4338 m_currentCellCoords
.GetCol() == col
)
4340 SetEditControlValue( s
);
4349 // ------ Block, row and col selection
4352 void wxGrid::SelectRow( int row
, bool addToSelected
)
4356 if ( IsSelection() && addToSelected
)
4359 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4362 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4363 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4364 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4365 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4369 need_refresh
[0] = TRUE
;
4370 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4371 wxGridCellCoords ( oldTop
- 1,
4373 m_selectedTopLeft
.SetRow( row
);
4378 need_refresh
[1] = TRUE
;
4379 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4380 wxGridCellCoords ( oldBottom
,
4383 m_selectedTopLeft
.SetCol( 0 );
4386 if ( oldBottom
< row
)
4388 need_refresh
[2] = TRUE
;
4389 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4390 wxGridCellCoords ( row
,
4392 m_selectedBottomRight
.SetRow( row
);
4395 if ( oldRight
< m_numCols
- 1 )
4397 need_refresh
[3] = TRUE
;
4398 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4400 wxGridCellCoords ( oldBottom
,
4402 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4405 for (i
= 0; i
< 4; i
++ )
4406 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4407 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4411 r
= SelectionToDeviceRect();
4413 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4415 m_selectedTopLeft
.Set( row
, 0 );
4416 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4417 r
= SelectionToDeviceRect();
4418 m_gridWin
->Refresh( FALSE
, &r
);
4421 wxGridRangeSelectEvent
gridEvt( GetId(),
4422 EVT_GRID_RANGE_SELECT
,
4425 m_selectedBottomRight
);
4427 GetEventHandler()->ProcessEvent(gridEvt
);
4431 void wxGrid::SelectCol( int col
, bool addToSelected
)
4433 if ( IsSelection() && addToSelected
)
4436 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4439 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4440 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4441 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4442 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4444 if ( oldLeft
> col
)
4446 need_refresh
[0] = TRUE
;
4447 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4448 wxGridCellCoords ( m_numRows
- 1,
4450 m_selectedTopLeft
.SetCol( col
);
4455 need_refresh
[1] = TRUE
;
4456 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4457 wxGridCellCoords ( oldTop
- 1,
4459 m_selectedTopLeft
.SetRow( 0 );
4462 if ( oldRight
< col
)
4464 need_refresh
[2] = TRUE
;
4465 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4466 wxGridCellCoords ( m_numRows
- 1,
4468 m_selectedBottomRight
.SetCol( col
);
4471 if ( oldBottom
< m_numRows
- 1 )
4473 need_refresh
[3] = TRUE
;
4474 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4476 wxGridCellCoords ( m_numRows
- 1,
4478 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4481 for (i
= 0; i
< 4; i
++ )
4482 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4483 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4489 r
= SelectionToDeviceRect();
4491 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4493 m_selectedTopLeft
.Set( 0, col
);
4494 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4495 r
= SelectionToDeviceRect();
4496 m_gridWin
->Refresh( FALSE
, &r
);
4499 wxGridRangeSelectEvent
gridEvt( GetId(),
4500 EVT_GRID_RANGE_SELECT
,
4503 m_selectedBottomRight
);
4505 GetEventHandler()->ProcessEvent(gridEvt
);
4509 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4512 wxGridCellCoords updateTopLeft
, updateBottomRight
;
4514 if ( topRow
> bottomRow
)
4521 if ( leftCol
> rightCol
)
4528 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
4529 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
4531 if ( m_selectedTopLeft
!= updateTopLeft
||
4532 m_selectedBottomRight
!= updateBottomRight
)
4534 // Compute two optimal update rectangles:
4535 // Either one rectangle is a real subset of the
4536 // other, or they are (almost) disjoint!
4538 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4541 // Store intermediate values
4542 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4543 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4544 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4545 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4547 // Determine the outer/inner coordinates.
4548 if (oldLeft
> leftCol
)
4554 if (oldTop
> topRow
)
4560 if (oldRight
< rightCol
)
4563 oldRight
= rightCol
;
4566 if (oldBottom
< bottomRow
)
4569 oldBottom
= bottomRow
;
4573 // Now, either the stuff marked old is the outer
4574 // rectangle or we don't have a situation where one
4575 // is contained in the other.
4577 if ( oldLeft
< leftCol
)
4579 need_refresh
[0] = TRUE
;
4580 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4582 wxGridCellCoords ( oldBottom
,
4586 if ( oldTop
< topRow
)
4588 need_refresh
[1] = TRUE
;
4589 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4591 wxGridCellCoords ( topRow
- 1,
4595 if ( oldRight
> rightCol
)
4597 need_refresh
[2] = TRUE
;
4598 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4600 wxGridCellCoords ( oldBottom
,
4604 if ( oldBottom
> bottomRow
)
4606 need_refresh
[3] = TRUE
;
4607 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
4609 wxGridCellCoords ( oldBottom
,
4615 m_selectedTopLeft
= updateTopLeft
;
4616 m_selectedBottomRight
= updateBottomRight
;
4618 // various Refresh() calls
4619 for (i
= 0; i
< 4; i
++ )
4620 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4621 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4624 // only generate an event if the block is not being selected by
4625 // dragging the mouse (in which case the event will be generated in
4626 // the mouse event handler)
4627 if ( !m_isDragging
)
4629 wxGridRangeSelectEvent
gridEvt( GetId(),
4630 EVT_GRID_RANGE_SELECT
,
4633 m_selectedBottomRight
);
4635 GetEventHandler()->ProcessEvent(gridEvt
);
4639 void wxGrid::SelectAll()
4641 m_selectedTopLeft
.Set( 0, 0 );
4642 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4644 m_gridWin
->Refresh();
4648 void wxGrid::ClearSelection()
4650 m_selectedTopLeft
= wxGridNoCellCoords
;
4651 m_selectedBottomRight
= wxGridNoCellCoords
;
4655 // This function returns the rectangle that encloses the given block
4656 // in device coords clipped to the client size of the grid window.
4658 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
4659 const wxGridCellCoords
&bottomRight
)
4661 wxRect
rect( wxGridNoCellRect
);
4664 cellRect
= CellToRect( topLeft
);
4665 if ( cellRect
!= wxGridNoCellRect
)
4671 rect
= wxRect( 0, 0, 0, 0 );
4674 cellRect
= CellToRect( bottomRight
);
4675 if ( cellRect
!= wxGridNoCellRect
)
4681 return wxGridNoCellRect
;
4684 // convert to scrolled coords
4686 int left
, top
, right
, bottom
;
4687 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4688 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4691 m_gridWin
->GetClientSize( &cw
, &ch
);
4693 rect
.SetLeft( wxMax(0, left
) );
4694 rect
.SetTop( wxMax(0, top
) );
4695 rect
.SetRight( wxMin(cw
, right
) );
4696 rect
.SetBottom( wxMin(ch
, bottom
) );
4704 // ------ Grid event classes
4707 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4709 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4710 int row
, int col
, int x
, int y
,
4711 bool control
, bool shift
, bool alt
, bool meta
)
4712 : wxNotifyEvent( type
, id
)
4718 m_control
= control
;
4723 SetEventObject(obj
);
4727 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4729 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4730 int rowOrCol
, int x
, int y
,
4731 bool control
, bool shift
, bool alt
, bool meta
)
4732 : wxNotifyEvent( type
, id
)
4734 m_rowOrCol
= rowOrCol
;
4737 m_control
= control
;
4742 SetEventObject(obj
);
4746 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4748 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4749 const wxGridCellCoords
& topLeft
,
4750 const wxGridCellCoords
& bottomRight
,
4751 bool control
, bool shift
, bool alt
, bool meta
)
4752 : wxNotifyEvent( type
, id
)
4754 m_topLeft
= topLeft
;
4755 m_bottomRight
= bottomRight
;
4756 m_control
= control
;
4761 SetEventObject(obj
);
4765 #endif // ifndef wxUSE_NEW_GRID