1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "grid.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
25 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
37 #include "wx/generic/grid.h"
39 #ifndef WXGRID_DRAW_LINES
40 #define WXGRID_DRAW_LINES 1
43 //////////////////////////////////////////////////////////////////////
45 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
46 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
48 // this is a magic incantation which must be done!
49 #include "wx/arrimpl.cpp"
51 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
54 // TODO: fixed so far - make configurable later (and also different for x/y)
55 static const size_t GRID_SCROLL_LINE
= 10;
57 //////////////////////////////////////////////////////////////////////
59 // Abstract base class for grid data (the model)
61 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
64 wxGridTableBase::wxGridTableBase()
67 m_view
= (wxGrid
*) NULL
;
70 wxGridTableBase::~wxGridTableBase()
75 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
77 wxLogWarning( wxT("Called grid table class function InsertRows(pos=%d, N=%d)\n"
78 "but your derived table class does not override this function"),
84 bool wxGridTableBase::AppendRows( size_t numRows
)
86 wxLogWarning( wxT("Called grid table class function AppendRows(N=%d)\n"
87 "but your derived table class does not override this function"),
93 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
95 wxLogWarning( wxT("Called grid table class function DeleteRows(pos=%d, N=%d)\n"
96 "but your derived table class does not override this function"),
102 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
104 wxLogWarning( wxT("Called grid table class function InsertCols(pos=%d, N=%d)\n"
105 "but your derived table class does not override this function"),
111 bool wxGridTableBase::AppendCols( size_t numCols
)
113 wxLogWarning( wxT("Called grid table class function AppendCols(N=%d)\n"
114 "but your derived table class does not override this function"),
120 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
122 wxLogWarning( wxT("Called grid table class function DeleteCols(pos=%d, N=%d)\n"
123 "but your derived table class does not override this function"),
130 wxString
wxGridTableBase::GetRowLabelValue( int row
)
137 wxString
wxGridTableBase::GetColLabelValue( int col
)
139 // default col labels are:
140 // cols 0 to 25 : A-Z
141 // cols 26 to 675 : AA-ZZ
148 s
+= (_T('A') + (wxChar
)( col%26
));
150 if ( col
< 0 ) break;
153 // reverse the string...
155 for ( i
= 0; i
< n
; i
++ )
165 //////////////////////////////////////////////////////////////////////
167 // Message class for the grid table to send requests and notifications
171 wxGridTableMessage::wxGridTableMessage()
173 m_table
= (wxGridTableBase
*) NULL
;
179 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
180 int commandInt1
, int commandInt2
)
184 m_comInt1
= commandInt1
;
185 m_comInt2
= commandInt2
;
190 //////////////////////////////////////////////////////////////////////
192 // A basic grid table for string data. An object of this class will
193 // created by wxGrid if you don't specify an alternative table class.
196 WX_DEFINE_OBJARRAY(wxGridStringArray
)
198 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
200 wxGridStringTable::wxGridStringTable()
205 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
210 m_data
.Alloc( numRows
);
214 for ( col
= 0; col
< numCols
; col
++ )
216 sa
.Add( wxEmptyString
);
219 for ( row
= 0; row
< numRows
; row
++ )
225 wxGridStringTable::~wxGridStringTable()
229 long wxGridStringTable::GetNumberRows()
231 return m_data
.GetCount();
234 long wxGridStringTable::GetNumberCols()
236 if ( m_data
.GetCount() > 0 )
237 return m_data
[0].GetCount();
242 wxString
wxGridStringTable::GetValue( int row
, int col
)
244 // TODO: bounds checking
246 return m_data
[row
][col
];
249 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
251 // TODO: bounds checking
253 m_data
[row
][col
] = s
;
256 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
258 // TODO: bounds checking
260 return (m_data
[row
][col
] == wxEmptyString
);
264 void wxGridStringTable::Clear()
267 int numRows
, numCols
;
269 numRows
= m_data
.GetCount();
272 numCols
= m_data
[0].GetCount();
274 for ( row
= 0; row
< numRows
; row
++ )
276 for ( col
= 0; col
< numCols
; col
++ )
278 m_data
[row
][col
] = wxEmptyString
;
285 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
289 size_t curNumRows
= m_data
.GetCount();
290 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
292 if ( pos
>= curNumRows
)
294 return AppendRows( numRows
);
298 sa
.Alloc( curNumCols
);
299 for ( col
= 0; col
< curNumCols
; col
++ )
301 sa
.Add( wxEmptyString
);
304 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
306 m_data
.Insert( sa
, row
);
311 wxGridTableMessage
msg( this,
312 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
316 GetView()->ProcessTableMessage( msg
);
322 bool wxGridStringTable::AppendRows( size_t numRows
)
326 size_t curNumRows
= m_data
.GetCount();
327 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
330 if ( curNumCols
> 0 )
332 sa
.Alloc( curNumCols
);
333 for ( col
= 0; col
< curNumCols
; col
++ )
335 sa
.Add( wxEmptyString
);
339 for ( row
= 0; row
< numRows
; row
++ )
346 wxGridTableMessage
msg( this,
347 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
350 GetView()->ProcessTableMessage( msg
);
356 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
360 size_t curNumRows
= m_data
.GetCount();
362 if ( pos
>= curNumRows
)
364 wxLogError( wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)...\n"
365 "Pos value is invalid for present table with %d rows"),
366 pos
, numRows
, curNumRows
);
370 if ( numRows
> curNumRows
- pos
)
372 numRows
= curNumRows
- pos
;
375 if ( numRows
>= curNumRows
)
377 m_data
.Empty(); // don't release memory just yet
381 for ( n
= 0; n
< numRows
; n
++ )
383 m_data
.Remove( pos
);
389 wxGridTableMessage
msg( this,
390 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
394 GetView()->ProcessTableMessage( msg
);
400 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
404 size_t curNumRows
= m_data
.GetCount();
405 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
407 if ( pos
>= curNumCols
)
409 return AppendCols( numCols
);
412 for ( row
= 0; row
< curNumRows
; row
++ )
414 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
416 m_data
[row
].Insert( wxEmptyString
, col
);
422 wxGridTableMessage
msg( this,
423 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
427 GetView()->ProcessTableMessage( msg
);
433 bool wxGridStringTable::AppendCols( size_t numCols
)
437 size_t curNumRows
= m_data
.GetCount();
440 // TODO: something better than this ?
442 wxLogError( wxT("Unable to append cols to a grid table with no rows.\n"
443 "Call AppendRows() first") );
447 for ( row
= 0; row
< curNumRows
; row
++ )
449 for ( n
= 0; n
< numCols
; n
++ )
451 m_data
[row
].Add( wxEmptyString
);
457 wxGridTableMessage
msg( this,
458 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
461 GetView()->ProcessTableMessage( msg
);
467 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
471 size_t curNumRows
= m_data
.GetCount();
472 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
474 if ( pos
>= curNumCols
)
476 wxLogError( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
477 "Pos value is invalid for present table with %d cols"),
478 pos
, numCols
, curNumCols
);
482 if ( numCols
> curNumCols
- pos
)
484 numCols
= curNumCols
- pos
;
487 for ( row
= 0; row
< curNumRows
; row
++ )
489 if ( numCols
>= curNumCols
)
495 for ( n
= 0; n
< numCols
; n
++ )
497 m_data
[row
].Remove( pos
);
504 wxGridTableMessage
msg( this,
505 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
509 GetView()->ProcessTableMessage( msg
);
515 wxString
wxGridStringTable::GetRowLabelValue( int row
)
517 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
519 // using default label
521 return wxGridTableBase::GetRowLabelValue( row
);
525 return m_rowLabels
[ row
];
529 wxString
wxGridStringTable::GetColLabelValue( int col
)
531 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
533 // using default label
535 return wxGridTableBase::GetColLabelValue( col
);
539 return m_colLabels
[ col
];
543 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
545 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
547 int n
= m_rowLabels
.GetCount();
549 for ( i
= n
; i
<= row
; i
++ )
551 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
555 m_rowLabels
[row
] = value
;
558 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
560 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
562 int n
= m_colLabels
.GetCount();
564 for ( i
= n
; i
<= col
; i
++ )
566 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
570 m_colLabels
[col
] = value
;
576 //////////////////////////////////////////////////////////////////////
578 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
580 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
581 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
585 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
589 const wxString
& value
,
593 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
596 m_isCellControl
= isCellControl
;
600 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
602 switch ( event
.KeyCode() )
605 m_grid
->SetEditControlValue( startValue
);
606 SetInsertionPointEnd();
616 if ( m_isCellControl
)
618 // send the event to the parent grid, skipping the
619 // event if nothing happens
621 event
.Skip( m_grid
->ProcessEvent( event
) );
625 // default text control response within the top edit
633 if ( m_isCellControl
)
635 if ( !m_grid
->ProcessEvent( event
) )
637 #if defined(__WXMOTIF__) || defined(__WXGTK__)
638 // wxMotif needs a little extra help...
640 int pos
= GetInsertionPoint();
641 wxString
s( GetValue() );
642 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
644 SetInsertionPoint( pos
);
646 // the other ports can handle a Return key press
656 if ( m_isCellControl
)
658 // send the event to the parent grid, skipping the
659 // event if nothing happens
661 event
.Skip( m_grid
->ProcessEvent( event
) );
665 // default text control response within the top edit
677 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
680 wxTextCtrl::SetValue(s
);
685 //////////////////////////////////////////////////////////////////////
687 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
689 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
690 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
691 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
692 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
695 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
697 const wxPoint
&pos
, const wxSize
&size
)
698 : wxWindow( parent
, id
, pos
, size
)
703 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
707 // NO - don't do this because it will set both the x and y origin
708 // coords to match the parent scrolled window and we just want to
709 // set the y coord - MB
711 // m_owner->PrepareDC( dc );
714 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
715 dc
.SetDeviceOrigin( 0, -y
);
717 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
718 m_owner
->DrawRowLabels( dc
);
722 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
724 m_owner
->ProcessRowLabelMouseEvent( event
);
728 // This seems to be required for wxMotif otherwise the mouse
729 // cursor must be in the cell edit control to get key events
731 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
733 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
738 //////////////////////////////////////////////////////////////////////
740 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
742 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
743 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
744 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
745 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
748 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
750 const wxPoint
&pos
, const wxSize
&size
)
751 : wxWindow( parent
, id
, pos
, size
)
756 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
760 // NO - don't do this because it will set both the x and y origin
761 // coords to match the parent scrolled window and we just want to
762 // set the x coord - MB
764 // m_owner->PrepareDC( dc );
767 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
768 dc
.SetDeviceOrigin( -x
, 0 );
770 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
771 m_owner
->DrawColLabels( dc
);
775 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
777 m_owner
->ProcessColLabelMouseEvent( event
);
781 // This seems to be required for wxMotif otherwise the mouse
782 // cursor must be in the cell edit control to get key events
784 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
786 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
791 //////////////////////////////////////////////////////////////////////
793 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
795 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
796 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
797 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
798 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
801 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
803 const wxPoint
&pos
, const wxSize
&size
)
804 : wxWindow( parent
, id
, pos
, size
)
809 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
813 int client_height
= 0;
814 int client_width
= 0;
815 GetClientSize( &client_width
, &client_height
);
817 dc
.SetPen( *wxBLACK_PEN
);
818 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
819 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
821 dc
.SetPen( *wxWHITE_PEN
);
822 dc
.DrawLine( 0, 0, client_width
, 0 );
823 dc
.DrawLine( 0, 0, 0, client_height
);
827 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
829 m_owner
->ProcessCornerLabelMouseEvent( event
);
833 // This seems to be required for wxMotif otherwise the mouse
834 // cursor must be in the cell edit control to get key events
836 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
838 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
843 //////////////////////////////////////////////////////////////////////
845 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
847 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
848 EVT_PAINT( wxGridWindow::OnPaint
)
849 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
850 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
853 wxGridWindow::wxGridWindow( wxGrid
*parent
,
854 wxGridRowLabelWindow
*rowLblWin
,
855 wxGridColLabelWindow
*colLblWin
,
856 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
857 : wxPanel( parent
, id
, pos
, size
, 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_rowLabelWin
= new wxGridRowLabelWindow( this,
959 wxSize(rowLblW
,-1) );
961 m_colLabelWin
= new wxGridColLabelWindow( this,
964 wxSize(-1, colLblH
) );
966 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
969 wxSize(rowLblW
, colLblH
) );
971 m_gridWin
= new wxGridWindow( this,
978 SetTargetWindow( m_gridWin
);
980 m_mainSizer
= new wxBoxSizer( wxVERTICAL
);
982 m_topSizer
= new wxBoxSizer( wxHORIZONTAL
);
983 m_topSizer
->Add( m_cornerLabelWin
, 0 );
984 m_topSizer
->Add( m_colLabelWin
, 1 );
986 m_mainSizer
->Add( m_topSizer
, 0, wxEXPAND
);
988 m_middleSizer
= new wxBoxSizer( wxHORIZONTAL
);
989 m_middleSizer
->Add( m_rowLabelWin
, 0, wxEXPAND
);
990 m_middleSizer
->Add( m_gridWin
, 1, wxEXPAND
);
992 m_mainSizer
->Add( m_middleSizer
, 1, wxEXPAND
);
994 SetAutoLayout( TRUE
);
995 SetSizer( m_mainSizer
);
999 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1003 wxLogError( wxT("wxGrid::CreateGrid(numRows, numCols) called more than once") );
1008 m_numRows
= numRows
;
1009 m_numCols
= numCols
;
1011 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1012 m_table
->SetView( this );
1025 if ( m_numRows
<= 0 )
1026 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1028 if ( m_numCols
<= 0 )
1029 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1031 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1032 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1034 if ( m_rowLabelWin
)
1036 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1040 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1043 m_labelTextColour
= wxColour( _T("BLACK") );
1045 // TODO: something better than this ?
1047 m_labelFont
= this->GetFont();
1048 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1050 m_rowLabelHorizAlign
= wxLEFT
;
1051 m_rowLabelVertAlign
= wxCENTRE
;
1053 m_colLabelHorizAlign
= wxCENTRE
;
1054 m_colLabelVertAlign
= wxTOP
;
1056 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1057 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1059 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1060 m_defaultRowHeight
+= 8;
1062 m_defaultRowHeight
+= 4;
1065 m_rowHeights
.Alloc( m_numRows
);
1066 m_rowBottoms
.Alloc( m_numRows
);
1068 for ( i
= 0; i
< m_numRows
; i
++ )
1070 m_rowHeights
.Add( m_defaultRowHeight
);
1071 rowBottom
+= m_defaultRowHeight
;
1072 m_rowBottoms
.Add( rowBottom
);
1075 m_colWidths
.Alloc( m_numCols
);
1076 m_colRights
.Alloc( m_numCols
);
1078 for ( i
= 0; i
< m_numCols
; i
++ )
1080 m_colWidths
.Add( m_defaultColWidth
);
1081 colRight
+= m_defaultColWidth
;
1082 m_colRights
.Add( colRight
);
1085 // TODO: improve this ?
1087 m_defaultCellFont
= this->GetFont();
1089 m_gridLineColour
= wxColour( 128, 128, 255 );
1090 m_gridLinesEnabled
= TRUE
;
1092 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1094 m_dragRowOrCol
= -1;
1095 m_isDragging
= FALSE
;
1097 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1098 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1100 m_currentCellCoords
= wxGridNoCellCoords
;
1102 m_selectedTopLeft
= wxGridNoCellCoords
;
1103 m_selectedBottomRight
= wxGridNoCellCoords
;
1105 m_editable
= TRUE
; // default for whole grid
1107 m_inOnKeyDown
= FALSE
;
1110 // TODO: extend this to other types of controls
1112 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1119 #if defined(__WXMSW__)
1120 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1124 m_cellEditCtrl
->Show( FALSE
);
1125 m_cellEditCtrlEnabled
= TRUE
;
1126 m_editCtrlType
= wxGRID_TEXTCTRL
;
1130 void wxGrid::CalcDimensions()
1132 // This avoids a crash in SetScrollbars
1134 if ( !m_displayed
) return;
1137 GetClientSize( &cw
, &ch
);
1139 if ( m_numRows
> 0 && m_numCols
> 0 )
1141 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1142 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1144 // TODO: restore the scroll position that we had before sizing
1147 GetViewStart( &x
, &y
);
1148 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1149 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1155 // this is called when the grid table sends a message to say that it
1156 // has been redimensioned
1158 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1162 switch ( msg
.GetId() )
1164 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1166 size_t pos
= msg
.GetCommandInt();
1167 int numRows
= msg
.GetCommandInt2();
1168 for ( i
= 0; i
< numRows
; i
++ )
1170 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1171 m_rowBottoms
.Insert( 0, pos
);
1173 m_numRows
+= numRows
;
1176 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1178 for ( i
= pos
; i
< m_numRows
; i
++ )
1180 bottom
+= m_rowHeights
[i
];
1181 m_rowBottoms
[i
] = bottom
;
1187 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1189 int numRows
= msg
.GetCommandInt();
1190 for ( i
= 0; i
< numRows
; i
++ )
1192 m_rowHeights
.Add( m_defaultRowHeight
);
1193 m_rowBottoms
.Add( 0 );
1196 int oldNumRows
= m_numRows
;
1197 m_numRows
+= numRows
;
1200 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1202 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1204 bottom
+= m_rowHeights
[i
];
1205 m_rowBottoms
[i
] = bottom
;
1211 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1213 size_t pos
= msg
.GetCommandInt();
1214 int numRows
= msg
.GetCommandInt2();
1215 for ( i
= 0; i
< numRows
; i
++ )
1217 m_rowHeights
.Remove( pos
);
1218 m_rowBottoms
.Remove( pos
);
1220 m_numRows
-= numRows
;
1225 m_colWidths
.Clear();
1226 m_colRights
.Clear();
1227 m_currentCellCoords
= wxGridNoCellCoords
;
1231 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1232 m_currentCellCoords
.Set( 0, 0 );
1235 for ( i
= 0; i
< m_numRows
; i
++ )
1237 h
+= m_rowHeights
[i
];
1238 m_rowBottoms
[i
] = h
;
1246 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1248 size_t pos
= msg
.GetCommandInt();
1249 int numCols
= msg
.GetCommandInt2();
1250 for ( i
= 0; i
< numCols
; i
++ )
1252 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1253 m_colRights
.Insert( 0, pos
);
1255 m_numCols
+= numCols
;
1258 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1260 for ( i
= pos
; i
< m_numCols
; i
++ )
1262 right
+= m_colWidths
[i
];
1263 m_colRights
[i
] = right
;
1269 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1271 int numCols
= msg
.GetCommandInt();
1272 for ( i
= 0; i
< numCols
; i
++ )
1274 m_colWidths
.Add( m_defaultColWidth
);
1275 m_colRights
.Add( 0 );
1278 int oldNumCols
= m_numCols
;
1279 m_numCols
+= numCols
;
1282 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1284 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1286 right
+= m_colWidths
[i
];
1287 m_colRights
[i
] = right
;
1293 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1295 size_t pos
= msg
.GetCommandInt();
1296 int numCols
= msg
.GetCommandInt2();
1297 for ( i
= 0; i
< numCols
; i
++ )
1299 m_colWidths
.Remove( pos
);
1300 m_colRights
.Remove( pos
);
1302 m_numCols
-= numCols
;
1306 #if 0 // leave the row alone here so that AppendCols will work subsequently
1308 m_rowHeights
.Clear();
1309 m_rowBottoms
.Clear();
1311 m_currentCellCoords
= wxGridNoCellCoords
;
1315 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1316 m_currentCellCoords
.Set( 0, 0 );
1319 for ( i
= 0; i
< m_numCols
; i
++ )
1321 w
+= m_colWidths
[i
];
1334 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1336 wxRegionIterator
iter( reg
);
1339 m_rowLabelsExposed
.Empty();
1346 // TODO: remove this when we can...
1347 // There is a bug in wxMotif that gives garbage update
1348 // rectangles if you jump-scroll a long way by clicking the
1349 // scrollbar with middle button. This is a work-around
1351 #if defined(__WXMOTIF__)
1353 m_gridWin
->GetClientSize( &cw
, &ch
);
1354 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1355 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1358 // logical bounds of update region
1361 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1362 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1364 // find the row labels within these bounds
1368 for ( row
= 0; row
< m_numRows
; row
++ )
1370 if ( m_rowBottoms
[row
] < top
) continue;
1372 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1373 if ( rowTop
> bottom
) break;
1375 m_rowLabelsExposed
.Add( row
);
1383 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1385 wxRegionIterator
iter( reg
);
1388 m_colLabelsExposed
.Empty();
1395 // TODO: remove this when we can...
1396 // There is a bug in wxMotif that gives garbage update
1397 // rectangles if you jump-scroll a long way by clicking the
1398 // scrollbar with middle button. This is a work-around
1400 #if defined(__WXMOTIF__)
1402 m_gridWin
->GetClientSize( &cw
, &ch
);
1403 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1404 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1407 // logical bounds of update region
1410 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1411 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1413 // find the cells within these bounds
1417 for ( col
= 0; col
< m_numCols
; col
++ )
1419 if ( m_colRights
[col
] < left
) continue;
1421 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1422 if ( colLeft
> right
) break;
1424 m_colLabelsExposed
.Add( col
);
1432 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1434 wxRegionIterator
iter( reg
);
1437 m_cellsExposed
.Empty();
1438 m_rowsExposed
.Empty();
1439 m_colsExposed
.Empty();
1441 int left
, top
, right
, bottom
;
1446 // TODO: remove this when we can...
1447 // There is a bug in wxMotif that gives garbage update
1448 // rectangles if you jump-scroll a long way by clicking the
1449 // scrollbar with middle button. This is a work-around
1451 #if defined(__WXMOTIF__)
1453 m_gridWin
->GetClientSize( &cw
, &ch
);
1454 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1455 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1456 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1457 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1460 // logical bounds of update region
1462 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1463 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1465 // find the cells within these bounds
1468 int colLeft
, rowTop
;
1469 for ( row
= 0; row
< m_numRows
; row
++ )
1471 if ( m_rowBottoms
[row
] < top
) continue;
1473 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1474 if ( rowTop
> bottom
) break;
1476 m_rowsExposed
.Add( row
);
1478 for ( col
= 0; col
< m_numCols
; col
++ )
1480 if ( m_colRights
[col
] < left
) continue;
1482 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1483 if ( colLeft
> right
) break;
1485 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1486 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1495 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1498 wxPoint
pos( event
.GetPosition() );
1499 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1501 if ( event
.Dragging() )
1503 m_isDragging
= TRUE
;
1505 if ( event
.LeftIsDown() )
1507 switch( m_cursorMode
)
1509 case WXGRID_CURSOR_RESIZE_ROW
:
1511 int cw
, ch
, left
, dummy
;
1512 m_gridWin
->GetClientSize( &cw
, &ch
);
1513 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1515 wxClientDC
dc( m_gridWin
);
1517 dc
.SetLogicalFunction(wxINVERT
);
1518 if ( m_dragLastPos
>= 0 )
1520 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1522 dc
.DrawLine( left
, y
, left
+cw
, y
);
1527 case WXGRID_CURSOR_SELECT_ROW
:
1529 if ( (row
= YToRow( y
)) >= 0 &&
1530 !IsInSelection( row
, 0 ) )
1532 SelectRow( row
, TRUE
);
1541 m_isDragging
= FALSE
;
1544 // ------------ Left button pressed
1546 if ( event
.LeftDown() )
1548 // don't send a label click event for a hit on the
1549 // edge of the row label - this is probably the user
1550 // wanting to resize the row
1552 if ( YToEdgeOfRow(y
) < 0 )
1556 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1558 SelectRow( row
, event
.ShiftDown() );
1559 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1564 // starting to drag-resize a row
1566 m_rowLabelWin
->CaptureMouse();
1571 // ------------ Left double click
1573 else if (event
.LeftDClick() )
1575 if ( YToEdgeOfRow(y
) < 0 )
1578 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1583 // ------------ Left button released
1585 else if ( event
.LeftUp() )
1587 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1589 m_rowLabelWin
->ReleaseMouse();
1591 if ( m_dragLastPos
>= 0 )
1593 // erase the last line and resize the row
1595 int cw
, ch
, left
, dummy
;
1596 m_gridWin
->GetClientSize( &cw
, &ch
);
1597 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1599 wxClientDC
dc( m_gridWin
);
1601 dc
.SetLogicalFunction( wxINVERT
);
1602 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1603 HideCellEditControl();
1605 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
1606 SetRowSize( m_dragRowOrCol
, wxMax( y
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
1607 if ( !GetBatchCount() )
1609 // Only needed to get the correct rect.y:
1610 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
1612 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
1613 rect
.width
= m_rowLabelWidth
;
1614 rect
.height
= ch
- rect
.y
;
1615 m_rowLabelWin
->Refresh( TRUE
, &rect
);
1617 m_gridWin
->Refresh( FALSE
, &rect
);
1620 ShowCellEditControl();
1622 // Note: we are ending the event *after* doing
1623 // default processing in this case
1625 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1629 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1634 // ------------ Right button down
1636 else if ( event
.RightDown() )
1639 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1641 // no default action at the moment
1646 // ------------ Right double click
1648 else if ( event
.RightDClick() )
1651 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1653 // no default action at the moment
1658 // ------------ No buttons down and mouse moving
1660 else if ( event
.Moving() )
1662 m_dragRowOrCol
= YToEdgeOfRow( y
);
1663 if ( m_dragRowOrCol
>= 0 )
1665 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1667 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1668 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1673 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1674 if ( m_rowLabelWin
->GetCursor() == m_rowResizeCursor
)
1675 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1681 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1684 wxPoint
pos( event
.GetPosition() );
1685 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1687 if ( event
.Dragging() )
1689 m_isDragging
= TRUE
;
1691 if ( event
.LeftIsDown() )
1693 switch( m_cursorMode
)
1695 case WXGRID_CURSOR_RESIZE_COL
:
1697 int cw
, ch
, dummy
, top
;
1698 m_gridWin
->GetClientSize( &cw
, &ch
);
1699 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1701 wxClientDC
dc( m_gridWin
);
1703 dc
.SetLogicalFunction(wxINVERT
);
1704 if ( m_dragLastPos
>= 0 )
1706 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1708 dc
.DrawLine( x
, top
, x
, top
+ch
);
1713 case WXGRID_CURSOR_SELECT_COL
:
1715 if ( (col
= XToCol( x
)) >= 0 &&
1716 !IsInSelection( 0, col
) )
1718 SelectCol( col
, TRUE
);
1727 m_isDragging
= FALSE
;
1730 // ------------ Left button pressed
1732 if ( event
.LeftDown() )
1734 // don't send a label click event for a hit on the
1735 // edge of the col label - this is probably the user
1736 // wanting to resize the col
1738 if ( XToEdgeOfCol(x
) < 0 )
1742 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1744 SelectCol( col
, event
.ShiftDown() );
1745 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1750 // starting to drag-resize a col
1752 m_colLabelWin
->CaptureMouse();
1757 // ------------ Left double click
1759 if ( event
.LeftDClick() )
1761 if ( XToEdgeOfCol(x
) < 0 )
1764 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
1769 // ------------ Left button released
1771 else if ( event
.LeftUp() )
1773 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
1775 m_colLabelWin
->ReleaseMouse();
1777 if ( m_dragLastPos
>= 0 )
1779 // erase the last line and resize the col
1781 int cw
, ch
, dummy
, top
;
1782 m_gridWin
->GetClientSize( &cw
, &ch
);
1783 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1785 wxClientDC
dc( m_gridWin
);
1787 dc
.SetLogicalFunction( wxINVERT
);
1788 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1789 HideCellEditControl();
1791 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
1792 SetColSize( m_dragRowOrCol
, wxMax( x
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
1794 if ( !GetBatchCount() )
1796 // Only needed to get the correct rect.x:
1797 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
1799 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
1800 rect
.width
= cw
- rect
.x
;
1801 rect
.height
= m_colLabelHeight
;
1802 m_colLabelWin
->Refresh( TRUE
, &rect
);
1804 m_gridWin
->Refresh( FALSE
, &rect
);
1807 ShowCellEditControl();
1809 // Note: we are ending the event *after* doing
1810 // default processing in this case
1812 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1816 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1821 // ------------ Right button down
1823 else if ( event
.RightDown() )
1826 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1828 // no default action at the moment
1833 // ------------ Right double click
1835 else if ( event
.RightDClick() )
1838 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1840 // no default action at the moment
1845 // ------------ No buttons down and mouse moving
1847 else if ( event
.Moving() )
1849 m_dragRowOrCol
= XToEdgeOfCol( x
);
1850 if ( m_dragRowOrCol
>= 0 )
1852 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1854 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1855 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1860 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1861 if ( m_colLabelWin
->GetCursor() == m_colResizeCursor
)
1862 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1868 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
1870 if ( event
.LeftDown() )
1872 // indicate corner label by having both row and
1875 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
1881 else if ( event
.LeftDClick() )
1883 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
1886 else if ( event
.RightDown() )
1888 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
1890 // no default action at the moment
1894 else if ( event
.RightDClick() )
1896 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
1898 // no default action at the moment
1904 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
1907 wxPoint
pos( event
.GetPosition() );
1908 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1910 wxGridCellCoords coords
;
1911 XYToCell( x
, y
, coords
);
1913 if ( event
.Dragging() )
1915 m_isDragging
= TRUE
;
1916 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1918 // Hide the edit control, so it
1919 // won't interfer with drag-shrinking.
1920 if ( IsCellEditControlEnabled() )
1921 HideCellEditControl();
1922 if ( coords
!= wxGridNoCellCoords
)
1924 if ( !IsSelection() )
1926 SelectBlock( coords
, coords
);
1930 SelectBlock( m_currentCellCoords
, coords
);
1938 m_isDragging
= FALSE
;
1940 if ( coords
!= wxGridNoCellCoords
)
1942 if ( event
.LeftDown() )
1944 if ( event
.ShiftDown() )
1946 SelectBlock( m_currentCellCoords
, coords
);
1950 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1955 MakeCellVisible( coords
);
1956 SetCurrentCell( coords
);
1962 // ------------ Left double click
1964 else if ( event
.LeftDClick() )
1966 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1973 // ------------ Left button released
1975 else if ( event
.LeftUp() )
1977 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1979 if ( IsSelection() )
1981 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
1985 // Show the edit control, if it has
1986 // been hidden for drag-shrinking.
1987 if ( IsCellEditControlEnabled() )
1988 ShowCellEditControl();
1994 // ------------ Right button down
1996 else if ( event
.RightDown() )
1998 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2003 // no default action at the moment
2008 // ------------ Right double click
2010 else if ( event
.RightDClick() )
2012 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2017 // no default action at the moment
2021 // ------------ Moving and no button action
2023 else if ( event
.Moving() && !event
.IsButton() )
2025 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2032 // ------ interaction with data model
2034 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2036 switch ( msg
.GetId() )
2038 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2039 return GetModelValues();
2041 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2042 return SetModelValues();
2044 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2045 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2046 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2047 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2048 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2049 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2050 return Redimension( msg
);
2059 // The behaviour of this function depends on the grid table class
2060 // Clear() function. For the default wxGridStringTable class the
2061 // behavious is to replace all cell contents with wxEmptyString but
2062 // not to change the number of rows or cols.
2064 void wxGrid::ClearGrid()
2069 SetEditControlValue();
2070 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2075 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2077 // TODO: something with updateLabels flag
2081 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2087 bool ok
= m_table
->InsertRows( pos
, numRows
);
2089 // the table will have sent the results of the insert row
2090 // operation to this view object as a grid table message
2094 if ( m_numCols
== 0 )
2096 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2098 // TODO: perhaps instead of appending the default number of cols
2099 // we should remember what the last non-zero number of cols was ?
2103 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2105 // if we have just inserted cols into an empty grid the current
2106 // cell will be undefined...
2108 SetCurrentCell( 0, 0 );
2112 if ( !GetBatchCount() ) Refresh();
2115 SetEditControlValue();
2125 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2127 // TODO: something with updateLabels flag
2131 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2135 if ( m_table
&& m_table
->AppendRows( numRows
) )
2137 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2139 // if we have just inserted cols into an empty grid the current
2140 // cell will be undefined...
2142 SetCurrentCell( 0, 0 );
2145 // the table will have sent the results of the append row
2146 // operation to this view object as a grid table message
2149 if ( !GetBatchCount() ) Refresh();
2159 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2161 // TODO: something with updateLabels flag
2165 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2169 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2171 // the table will have sent the results of the delete row
2172 // operation to this view object as a grid table message
2174 if ( m_numRows
> 0 )
2175 SetEditControlValue();
2177 HideCellEditControl();
2180 if ( !GetBatchCount() ) Refresh();
2190 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2192 // TODO: something with updateLabels flag
2196 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2202 HideCellEditControl();
2203 bool ok
= m_table
->InsertCols( pos
, numCols
);
2205 // the table will have sent the results of the insert col
2206 // operation to this view object as a grid table message
2210 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2212 // if we have just inserted cols into an empty grid the current
2213 // cell will be undefined...
2215 SetCurrentCell( 0, 0 );
2219 if ( !GetBatchCount() ) Refresh();
2222 SetEditControlValue();
2232 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2234 // TODO: something with updateLabels flag
2238 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2242 if ( m_table
&& m_table
->AppendCols( numCols
) )
2244 // the table will have sent the results of the append col
2245 // operation to this view object as a grid table message
2247 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2249 // if we have just inserted cols into an empty grid the current
2250 // cell will be undefined...
2252 SetCurrentCell( 0, 0 );
2256 if ( !GetBatchCount() ) Refresh();
2266 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2268 // TODO: something with updateLabels flag
2272 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2276 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2278 // the table will have sent the results of the delete col
2279 // operation to this view object as a grid table message
2281 if ( m_numCols
> 0 )
2282 SetEditControlValue();
2284 HideCellEditControl();
2287 if ( !GetBatchCount() ) Refresh();
2299 // ----- event handlers
2302 // Generate a grid event based on a mouse event and
2303 // return the result of ProcessEvent()
2305 bool wxGrid::SendEvent( const wxEventType type
,
2307 wxMouseEvent
& mouseEv
)
2309 if ( type
== EVT_GRID_ROW_SIZE
||
2310 type
== EVT_GRID_COL_SIZE
)
2312 int rowOrCol
= (row
== -1 ? col
: row
);
2314 wxGridSizeEvent
gridEvt( GetId(),
2318 mouseEv
.GetX(), mouseEv
.GetY(),
2319 mouseEv
.ControlDown(),
2320 mouseEv
.ShiftDown(),
2322 mouseEv
.MetaDown() );
2324 return GetEventHandler()->ProcessEvent(gridEvt
);
2326 else if ( type
== EVT_GRID_RANGE_SELECT
)
2328 wxGridRangeSelectEvent
gridEvt( GetId(),
2332 m_selectedBottomRight
,
2333 mouseEv
.ControlDown(),
2334 mouseEv
.ShiftDown(),
2336 mouseEv
.MetaDown() );
2338 return GetEventHandler()->ProcessEvent(gridEvt
);
2342 wxGridEvent
gridEvt( GetId(),
2346 mouseEv
.GetX(), mouseEv
.GetY(),
2347 mouseEv
.ControlDown(),
2348 mouseEv
.ShiftDown(),
2350 mouseEv
.MetaDown() );
2352 return GetEventHandler()->ProcessEvent(gridEvt
);
2357 // Generate a grid event of specified type and return the result
2358 // of ProcessEvent().
2360 bool wxGrid::SendEvent( const wxEventType type
,
2363 if ( type
== EVT_GRID_ROW_SIZE
||
2364 type
== EVT_GRID_COL_SIZE
)
2366 int rowOrCol
= (row
== -1 ? col
: row
);
2368 wxGridSizeEvent
gridEvt( GetId(),
2373 return GetEventHandler()->ProcessEvent(gridEvt
);
2377 wxGridEvent
gridEvt( GetId(),
2382 return GetEventHandler()->ProcessEvent(gridEvt
);
2387 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2389 wxPaintDC
dc( this );
2391 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2392 m_numRows
&& m_numCols
)
2394 m_currentCellCoords
.Set(0, 0);
2395 SetEditControlValue();
2396 ShowCellEditControl();
2403 // This is just here to make sure that CalcDimensions gets called when
2404 // the grid view is resized... then the size event is skipped to allow
2405 // the box sizers to handle everything
2407 void wxGrid::OnSize( wxSizeEvent
& event
)
2414 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2416 if ( m_inOnKeyDown
)
2418 // shouldn't be here - we are going round in circles...
2420 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2423 m_inOnKeyDown
= TRUE
;
2425 // propagate the event up and see if it gets processed
2427 wxWindow
*parent
= GetParent();
2428 wxKeyEvent
keyEvt( event
);
2429 keyEvt
.SetEventObject( parent
);
2431 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2433 // try local handlers
2435 switch ( event
.KeyCode() )
2438 if ( event
.ControlDown() )
2440 MoveCursorUpBlock();
2449 if ( event
.ControlDown() )
2451 MoveCursorDownBlock();
2460 if ( event
.ControlDown() )
2462 MoveCursorLeftBlock();
2471 if ( event
.ControlDown() )
2473 MoveCursorRightBlock();
2482 if ( !IsEditable() )
2493 if ( event
.ControlDown() )
2495 event
.Skip(); // to let the edit control have the return
2504 if ( event
.ControlDown() )
2506 MakeCellVisible( 0, 0 );
2507 SetCurrentCell( 0, 0 );
2516 if ( event
.ControlDown() )
2518 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2519 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2536 // now try the cell edit control
2538 if ( IsCellEditControlEnabled() )
2540 event
.SetEventObject( m_cellEditCtrl
);
2541 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2547 m_inOnKeyDown
= FALSE
;
2551 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2553 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2555 // the event has been intercepted - do nothing
2560 m_currentCellCoords
!= wxGridNoCellCoords
)
2562 HideCellEditControl();
2563 SaveEditControlValue();
2566 m_currentCellCoords
= coords
;
2568 SetEditControlValue();
2572 ShowCellEditControl();
2574 if ( IsSelection() )
2576 wxRect
r( SelectionToDeviceRect() );
2578 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
2585 // ------ functions to get/send data (see also public functions)
2588 bool wxGrid::GetModelValues()
2592 // all we need to do is repaint the grid
2594 m_gridWin
->Refresh();
2602 bool wxGrid::SetModelValues()
2608 for ( row
= 0; row
< m_numRows
; row
++ )
2610 for ( col
= 0; col
< m_numCols
; col
++ )
2612 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2624 // Note - this function only draws cells that are in the list of
2625 // exposed cells (usually set from the update region by
2626 // CalcExposedCells)
2628 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2630 if ( !m_numRows
|| !m_numCols
) return;
2633 size_t numCells
= m_cellsExposed
.GetCount();
2635 for ( i
= 0; i
< numCells
; i
++ )
2637 DrawCell( dc
, m_cellsExposed
[i
] );
2642 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2644 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2645 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2647 #if !WXGRID_DRAW_LINES
2648 if ( m_gridLinesEnabled
)
2649 DrawCellBorder( dc
, coords
);
2652 DrawCellBackground( dc
, coords
);
2654 // TODO: separate functions here for different kinds of cells ?
2657 DrawCellValue( dc
, coords
);
2661 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2663 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2664 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2666 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2667 int row
= coords
.GetRow();
2668 int col
= coords
.GetCol();
2670 // right hand border
2672 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
2673 m_colRights
[col
], m_rowBottoms
[row
] );
2677 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
2678 m_colRights
[col
], m_rowBottoms
[row
] );
2682 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2684 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2685 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2687 int row
= coords
.GetRow();
2688 int col
= coords
.GetCol();
2690 dc
.SetBackgroundMode( wxSOLID
);
2692 if ( IsInSelection( coords
) )
2694 // TODO: improve this
2696 dc
.SetBrush( *wxBLACK_BRUSH
);
2700 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2703 dc
.SetPen( *wxTRANSPARENT_PEN
);
2705 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2706 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2708 m_rowHeights
[row
]-1 );
2712 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2714 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2715 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2717 int row
= coords
.GetRow();
2718 int col
= coords
.GetCol();
2720 dc
.SetBackgroundMode( wxTRANSPARENT
);
2722 if ( IsInSelection( row
, col
) )
2724 // TODO: improve this
2726 dc
.SetTextBackground( wxColour(0, 0, 0) );
2727 dc
.SetTextForeground( wxColour(255, 255, 255) );
2731 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2732 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2734 dc
.SetFont( GetCellFont(row
, col
) );
2737 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2740 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2741 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2742 rect
.SetWidth( m_colWidths
[col
] - 4 );
2743 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2745 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2750 // TODO: remove this ???
2751 // This is used to redraw all grid lines e.g. when the grid line colour
2754 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
2756 if ( !m_gridLinesEnabled
||
2758 !m_numCols
) return;
2760 int top
, bottom
, left
, right
;
2764 m_gridWin
->GetClientSize(&cw
, &ch
);
2766 // virtual coords of visible area
2768 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2769 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2773 reg
.GetBox(x
, y
, w
, h
);
2774 CalcUnscrolledPosition( x
, y
, &left
, &top
);
2775 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
2778 // avoid drawing grid lines past the last row and col
2780 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
2781 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
2783 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2785 // horizontal grid lines
2788 for ( i
= 0; i
< m_numRows
; i
++ )
2790 if ( m_rowBottoms
[i
] > bottom
)
2794 else if ( m_rowBottoms
[i
] >= top
)
2796 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
2801 // vertical grid lines
2803 for ( i
= 0; i
< m_numCols
; i
++ )
2805 if ( m_colRights
[i
] > right
)
2809 else if ( m_colRights
[i
] >= left
)
2811 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
2817 void wxGrid::DrawRowLabels( wxDC
& dc
)
2819 if ( !m_numRows
|| !m_numCols
) return;
2822 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2824 for ( i
= 0; i
< numLabels
; i
++ )
2826 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2831 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2833 if ( m_rowHeights
[row
] <= 0 ) return;
2835 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2837 dc
.SetPen( *wxBLACK_PEN
);
2838 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
2839 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
2841 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
2842 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
2844 dc
.SetPen( *wxWHITE_PEN
);
2845 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
2846 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
2848 dc
.SetBackgroundMode( wxTRANSPARENT
);
2849 dc
.SetTextForeground( GetLabelTextColour() );
2850 dc
.SetFont( GetLabelFont() );
2853 GetRowLabelAlignment( &hAlign
, &vAlign
);
2857 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2858 rect
.SetWidth( m_rowLabelWidth
- 4 );
2859 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2860 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2864 void wxGrid::DrawColLabels( wxDC
& dc
)
2866 if ( !m_numRows
|| !m_numCols
) return;
2869 size_t numLabels
= m_colLabelsExposed
.GetCount();
2871 for ( i
= 0; i
< numLabels
; i
++ )
2873 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2878 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2880 if ( m_colWidths
[col
] <= 0 ) return;
2882 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2884 dc
.SetPen( *wxBLACK_PEN
);
2885 dc
.DrawLine( m_colRights
[col
]-1, 0,
2886 m_colRights
[col
]-1, m_colLabelHeight
-1 );
2888 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
2889 m_colRights
[col
]-1, m_colLabelHeight
-1 );
2891 dc
.SetPen( *wxWHITE_PEN
);
2892 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
2893 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
2895 dc
.SetBackgroundMode( wxTRANSPARENT
);
2896 dc
.SetTextForeground( GetLabelTextColour() );
2897 dc
.SetFont( GetLabelFont() );
2899 dc
.SetBackgroundMode( wxTRANSPARENT
);
2900 dc
.SetTextForeground( GetLabelTextColour() );
2901 dc
.SetFont( GetLabelFont() );
2904 GetColLabelAlignment( &hAlign
, &vAlign
);
2907 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2909 rect
.SetWidth( m_colWidths
[col
] - 4 );
2910 rect
.SetHeight( m_colLabelHeight
- 4 );
2911 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2915 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2916 const wxString
& value
,
2921 long textWidth
, textHeight
;
2922 long lineWidth
, lineHeight
;
2923 wxArrayString lines
;
2925 dc
.SetClippingRegion( rect
);
2926 StringToLines( value
, lines
);
2927 if ( lines
.GetCount() )
2929 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2930 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2933 switch ( horizAlign
)
2936 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2940 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2949 switch ( vertAlign
)
2952 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2956 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2965 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2967 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2972 dc
.DestroyClippingRegion();
2976 // Split multi line text up into an array of strings. Any existing
2977 // contents of the string array are preserved.
2979 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2981 // TODO: this won't work for WXMAC ? (lines end with '\r')
2982 // => use wxTextFile functions then (VZ)
2985 while ( startPos
< (int)value
.Length() )
2987 pos
= value
.Mid(startPos
).Find( '\n' );
2992 else if ( pos
== 0 )
2994 lines
.Add( wxEmptyString
);
2998 if ( value
[startPos
+pos
-1] == '\r' )
3000 lines
.Add( value
.Mid(startPos
, pos
-1) );
3004 lines
.Add( value
.Mid(startPos
, pos
) );
3009 if ( startPos
< (int)value
.Length() )
3011 lines
.Add( value
.Mid( startPos
) );
3016 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3017 wxArrayString
& lines
,
3018 long *width
, long *height
)
3025 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3027 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3028 w
= wxMax( w
, lineW
);
3038 // ------ Edit control functions
3042 void wxGrid::EnableEditing( bool edit
)
3044 // TODO: improve this ?
3046 if ( edit
!= m_editable
)
3050 // TODO: extend this for other edit control types
3052 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3054 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3060 #if 0 // disabled for the moment - the cell control is always active
3061 void wxGrid::EnableCellEditControl( bool enable
)
3063 if ( m_cellEditCtrl
&&
3064 enable
!= m_cellEditCtrlEnabled
)
3066 m_cellEditCtrlEnabled
= enable
;
3068 if ( m_cellEditCtrlEnabled
)
3070 SetEditControlValue();
3071 ShowCellEditControl();
3075 HideCellEditControl();
3076 SaveEditControlValue();
3083 void wxGrid::ShowCellEditControl()
3087 if ( IsCellEditControlEnabled() )
3089 if ( !IsVisible( m_currentCellCoords
) )
3095 rect
= CellToRect( m_currentCellCoords
);
3097 // convert to scrolled coords
3099 int left
, top
, right
, bottom
;
3100 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3101 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3104 m_gridWin
->GetClientSize( &cw
, &ch
);
3106 // Make the edit control large enough to allow for internal margins
3107 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3110 #if defined(__WXMOTIF__)
3111 if ( m_currentCellCoords
.GetRow() == 0 ||
3112 m_currentCellCoords
.GetCol() == 0 )
3121 if ( m_currentCellCoords
.GetRow() == 0 ||
3122 m_currentCellCoords
.GetCol() == 0 )
3132 #if defined(__WXGTK__)
3135 if (left
!= 0) left_diff
++;
3136 if (top
!= 0) top_diff
++;
3137 rect
.SetLeft( left
+ left_diff
);
3138 rect
.SetTop( top
+ top_diff
);
3139 rect
.SetRight( rect
.GetRight() - left_diff
);
3140 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3142 rect
.SetLeft( wxMax(0, left
- extra
) );
3143 rect
.SetTop( wxMax(0, top
- extra
) );
3144 rect
.SetRight( rect
.GetRight() + 2*extra
);
3145 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3148 m_cellEditCtrl
->SetSize( rect
);
3149 m_cellEditCtrl
->Show( TRUE
);
3151 switch ( m_editCtrlType
)
3153 case wxGRID_TEXTCTRL
:
3154 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3157 case wxGRID_CHECKBOX
:
3158 // TODO: anything ???
3163 // TODO: anything ???
3167 case wxGRID_COMBOBOX
:
3168 // TODO: anything ???
3173 m_cellEditCtrl
->SetFocus();
3179 void wxGrid::HideCellEditControl()
3181 if ( IsCellEditControlEnabled() )
3183 m_cellEditCtrl
->Show( FALSE
);
3188 void wxGrid::SetEditControlValue( const wxString
& value
)
3194 s
= GetCellValue(m_currentCellCoords
);
3198 if ( IsCellEditControlEnabled() )
3200 switch ( m_editCtrlType
)
3202 case wxGRID_TEXTCTRL
:
3203 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3206 case wxGRID_CHECKBOX
:
3207 // TODO: implement this
3212 // TODO: implement this
3216 case wxGRID_COMBOBOX
:
3217 // TODO: implement this
3226 void wxGrid::SaveEditControlValue()
3230 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3232 if ( IsCellEditControlEnabled() )
3234 ctrl
= m_cellEditCtrl
;
3241 bool valueChanged
= FALSE
;
3243 switch ( m_editCtrlType
)
3245 case wxGRID_TEXTCTRL
:
3246 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3247 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3248 SetCellValue( m_currentCellCoords
,
3249 ((wxTextCtrl
*) ctrl
)->GetValue() );
3252 case wxGRID_CHECKBOX
:
3253 // TODO: implement this
3258 // TODO: implement this
3262 case wxGRID_COMBOBOX
:
3263 // TODO: implement this
3270 SendEvent( EVT_GRID_CELL_CHANGE
,
3271 m_currentCellCoords
.GetRow(),
3272 m_currentCellCoords
.GetCol() );
3279 // ------ Grid location functions
3280 // Note that all of these functions work with the logical coordinates of
3281 // grid cells and labels so you will need to convert from device
3282 // coordinates for mouse events etc.
3285 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3287 int row
= YToRow(y
);
3288 int col
= XToCol(x
);
3290 if ( row
== -1 || col
== -1 )
3292 coords
= wxGridNoCellCoords
;
3296 coords
.Set( row
, col
);
3301 int wxGrid::YToRow( int y
)
3305 for ( i
= 0; i
< m_numRows
; i
++ )
3307 if ( y
< m_rowBottoms
[i
] ) return i
;
3314 int wxGrid::XToCol( int x
)
3318 for ( i
= 0; i
< m_numCols
; i
++ )
3320 if ( x
< m_colRights
[i
] ) return i
;
3327 // return the row number that that the y coord is near the edge of, or
3328 // -1 if not near an edge
3330 int wxGrid::YToEdgeOfRow( int y
)
3334 for ( i
= 0; i
< m_numRows
; i
++ )
3336 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3338 d
= abs( y
- m_rowBottoms
[i
] );
3340 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3349 // return the col number that that the x coord is near the edge of, or
3350 // -1 if not near an edge
3352 int wxGrid::XToEdgeOfCol( int x
)
3356 for ( i
= 0; i
< m_numCols
; i
++ )
3358 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3360 d
= abs( x
- m_colRights
[i
] );
3362 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3371 wxRect
wxGrid::CellToRect( int row
, int col
)
3373 wxRect
rect( -1, -1, -1, -1 );
3375 if ( row
>= 0 && row
< m_numRows
&&
3376 col
>= 0 && col
< m_numCols
)
3378 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3379 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3380 rect
.width
= m_colWidths
[col
];
3381 rect
.height
= m_rowHeights
[ row
];
3388 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3390 // get the cell rectangle in logical coords
3392 wxRect
r( CellToRect( row
, col
) );
3394 // convert to device coords
3396 int left
, top
, right
, bottom
;
3397 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3398 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3400 // check against the client area of the grid window
3403 m_gridWin
->GetClientSize( &cw
, &ch
);
3405 if ( wholeCellVisible
)
3407 // is the cell wholly visible ?
3409 return ( left
>= 0 && right
<= cw
&&
3410 top
>= 0 && bottom
<= ch
);
3414 // is the cell partly visible ?
3416 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3417 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3422 // make the specified cell location visible by doing a minimal amount
3425 void wxGrid::MakeCellVisible( int row
, int col
)
3428 int xpos
= -1, ypos
= -1;
3430 if ( row
>= 0 && row
< m_numRows
&&
3431 col
>= 0 && col
< m_numCols
)
3433 // get the cell rectangle in logical coords
3435 wxRect
r( CellToRect( row
, col
) );
3437 // convert to device coords
3439 int left
, top
, right
, bottom
;
3440 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3441 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3444 m_gridWin
->GetClientSize( &cw
, &ch
);
3450 else if ( bottom
> ch
)
3452 int h
= r
.GetHeight();
3454 for ( i
= row
-1; i
>= 0; i
-- )
3456 if ( h
+ m_rowHeights
[i
] > ch
) break;
3458 h
+= m_rowHeights
[i
];
3459 ypos
-= m_rowHeights
[i
];
3462 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3463 // have rounding errors (this is important, because if we do, we
3464 // might not scroll at all and some cells won't be redrawn)
3465 ypos
+= GRID_SCROLL_LINE
/ 2;
3472 else if ( right
> cw
)
3474 int w
= r
.GetWidth();
3476 for ( i
= col
-1; i
>= 0; i
-- )
3478 if ( w
+ m_colWidths
[i
] > cw
) break;
3480 w
+= m_colWidths
[i
];
3481 xpos
-= m_colWidths
[i
];
3484 // see comment for ypos above
3485 xpos
+= GRID_SCROLL_LINE
/ 2;
3488 if ( xpos
!= -1 || ypos
!= -1 )
3490 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3491 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3492 Scroll( xpos
, ypos
);
3500 // ------ Grid cursor movement functions
3503 bool wxGrid::MoveCursorUp()
3505 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3506 m_currentCellCoords
.GetRow() > 0 )
3508 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3509 m_currentCellCoords
.GetCol() );
3511 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3512 m_currentCellCoords
.GetCol() );
3521 bool wxGrid::MoveCursorDown()
3523 // TODO: allow for scrolling
3525 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3526 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3528 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3529 m_currentCellCoords
.GetCol() );
3531 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3532 m_currentCellCoords
.GetCol() );
3541 bool wxGrid::MoveCursorLeft()
3543 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3544 m_currentCellCoords
.GetCol() > 0 )
3546 MakeCellVisible( m_currentCellCoords
.GetRow(),
3547 m_currentCellCoords
.GetCol() - 1 );
3549 SetCurrentCell( m_currentCellCoords
.GetRow(),
3550 m_currentCellCoords
.GetCol() - 1 );
3559 bool wxGrid::MoveCursorRight()
3561 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3562 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3564 MakeCellVisible( m_currentCellCoords
.GetRow(),
3565 m_currentCellCoords
.GetCol() + 1 );
3567 SetCurrentCell( m_currentCellCoords
.GetRow(),
3568 m_currentCellCoords
.GetCol() + 1 );
3577 bool wxGrid::MovePageUp()
3579 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3581 int row
= m_currentCellCoords
.GetRow();
3585 m_gridWin
->GetClientSize( &cw
, &ch
);
3587 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3588 int newRow
= YToRow( y
- ch
+ 1 );
3593 else if ( newRow
== row
)
3598 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3599 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3607 bool wxGrid::MovePageDown()
3609 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3611 int row
= m_currentCellCoords
.GetRow();
3612 if ( row
< m_numRows
)
3615 m_gridWin
->GetClientSize( &cw
, &ch
);
3617 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3618 int newRow
= YToRow( y
+ ch
);
3621 newRow
= m_numRows
- 1;
3623 else if ( newRow
== row
)
3628 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3629 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3637 bool wxGrid::MoveCursorUpBlock()
3640 m_currentCellCoords
!= wxGridNoCellCoords
&&
3641 m_currentCellCoords
.GetRow() > 0 )
3643 int row
= m_currentCellCoords
.GetRow();
3644 int col
= m_currentCellCoords
.GetCol();
3646 if ( m_table
->IsEmptyCell(row
, col
) )
3648 // starting in an empty cell: find the next block of
3654 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3657 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3659 // starting at the top of a block: find the next block
3665 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3670 // starting within a block: find the top of the block
3675 if ( m_table
->IsEmptyCell(row
, col
) )
3683 MakeCellVisible( row
, col
);
3684 SetCurrentCell( row
, col
);
3692 bool wxGrid::MoveCursorDownBlock()
3695 m_currentCellCoords
!= wxGridNoCellCoords
&&
3696 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3698 int row
= m_currentCellCoords
.GetRow();
3699 int col
= m_currentCellCoords
.GetCol();
3701 if ( m_table
->IsEmptyCell(row
, col
) )
3703 // starting in an empty cell: find the next block of
3706 while ( row
< m_numRows
-1 )
3709 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3712 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3714 // starting at the bottom of a block: find the next block
3717 while ( row
< m_numRows
-1 )
3720 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3725 // starting within a block: find the bottom of the block
3727 while ( row
< m_numRows
-1 )
3730 if ( m_table
->IsEmptyCell(row
, col
) )
3738 MakeCellVisible( row
, col
);
3739 SetCurrentCell( row
, col
);
3747 bool wxGrid::MoveCursorLeftBlock()
3750 m_currentCellCoords
!= wxGridNoCellCoords
&&
3751 m_currentCellCoords
.GetCol() > 0 )
3753 int row
= m_currentCellCoords
.GetRow();
3754 int col
= m_currentCellCoords
.GetCol();
3756 if ( m_table
->IsEmptyCell(row
, col
) )
3758 // starting in an empty cell: find the next block of
3764 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3767 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3769 // starting at the left of a block: find the next block
3775 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3780 // starting within a block: find the left of the block
3785 if ( m_table
->IsEmptyCell(row
, col
) )
3793 MakeCellVisible( row
, col
);
3794 SetCurrentCell( row
, col
);
3802 bool wxGrid::MoveCursorRightBlock()
3805 m_currentCellCoords
!= wxGridNoCellCoords
&&
3806 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3808 int row
= m_currentCellCoords
.GetRow();
3809 int col
= m_currentCellCoords
.GetCol();
3811 if ( m_table
->IsEmptyCell(row
, col
) )
3813 // starting in an empty cell: find the next block of
3816 while ( col
< m_numCols
-1 )
3819 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3822 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3824 // starting at the right of a block: find the next block
3827 while ( col
< m_numCols
-1 )
3830 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3835 // starting within a block: find the right of the block
3837 while ( col
< m_numCols
-1 )
3840 if ( m_table
->IsEmptyCell(row
, col
) )
3848 MakeCellVisible( row
, col
);
3849 SetCurrentCell( row
, col
);
3860 // ------ Label values and formatting
3863 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3865 *horiz
= m_rowLabelHorizAlign
;
3866 *vert
= m_rowLabelVertAlign
;
3869 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3871 *horiz
= m_colLabelHorizAlign
;
3872 *vert
= m_colLabelVertAlign
;
3875 wxString
wxGrid::GetRowLabelValue( int row
)
3879 return m_table
->GetRowLabelValue( row
);
3889 wxString
wxGrid::GetColLabelValue( int col
)
3893 return m_table
->GetColLabelValue( col
);
3904 void wxGrid::SetRowLabelSize( int width
)
3908 width
= wxMax( width
, 0 );
3909 if ( width
!= m_rowLabelWidth
)
3911 // Hiding the row labels (and possible the corner label)
3915 m_rowLabelWin
->Show( FALSE
);
3917 // If the col labels are on display we need to hide the
3918 // corner label and remove it from the top sizer
3920 if ( m_colLabelHeight
> 0 )
3922 m_cornerLabelWin
->Show( FALSE
);
3923 m_topSizer
->Remove( m_cornerLabelWin
);
3926 m_middleSizer
->Remove( m_rowLabelWin
);
3930 // Displaying the row labels (and possibly the corner
3931 // label) after being hidden
3933 if ( m_rowLabelWidth
== 0 )
3935 m_rowLabelWin
->Show( TRUE
);
3937 if ( m_colLabelHeight
> 0 )
3939 m_cornerLabelWin
->Show( TRUE
);
3940 m_topSizer
->Prepend( m_cornerLabelWin
, 0 );
3943 m_middleSizer
->Prepend( m_rowLabelWin
, 0, wxEXPAND
);
3947 // set the width of the corner label if it is on display
3949 if ( m_colLabelHeight
> 0 )
3951 wxList
& childList
= m_topSizer
->GetChildren();
3952 wxNode
*node
= childList
.First();
3955 wxSizerItem
*item
= (wxSizerItem
*)node
->Data();
3956 if ( item
->GetWindow() == m_cornerLabelWin
)
3958 item
->SetInitSize( width
, m_colLabelHeight
);
3961 node
= node
->Next();
3965 // set the width of the row labels
3967 wxList
& childList
= m_middleSizer
->GetChildren();
3968 wxNode
*node
= childList
.First();
3971 wxSizerItem
*item
= (wxSizerItem
*)node
->Data();
3972 if ( item
->GetWindow() == m_rowLabelWin
)
3974 sz
= item
->GetWindow()->GetSize();
3975 item
->SetInitSize( width
, sz
.GetHeight() );
3978 node
= node
->Next();
3982 m_rowLabelWidth
= width
;
3983 m_mainSizer
->Layout();
3988 void wxGrid::SetColLabelSize( int height
)
3992 if ( height
< 0 ) height
= 0;
3993 if ( height
!= m_colLabelHeight
)
3995 // hiding the column labels
3999 m_cornerLabelWin
->Show( FALSE
);
4000 m_colLabelWin
->Show( FALSE
);
4002 // Note: this call will actually delete the sizer
4004 m_mainSizer
->Remove( m_topSizer
);
4005 m_topSizer
= (wxBoxSizer
*)NULL
;
4009 // column labels to be displayed after being hidden
4011 if ( m_colLabelHeight
== 0 )
4013 // recreate the top sizer
4015 m_topSizer
= new wxBoxSizer( wxHORIZONTAL
);
4017 if ( m_rowLabelWidth
> 0 )
4018 m_topSizer
->Add( m_cornerLabelWin
, 0 );
4020 m_topSizer
->Add( m_colLabelWin
, 1 );
4021 m_mainSizer
->Prepend( m_topSizer
, 0, wxEXPAND
);
4023 // only show the corner label if the row labels are
4026 if ( m_rowLabelWidth
> 0 )
4027 m_cornerLabelWin
->Show( TRUE
);
4029 m_colLabelWin
->Show( TRUE
);
4032 wxList
& childList
= m_topSizer
->GetChildren();
4033 wxNode
*node
= childList
.First();
4036 wxSizerItem
*item
= (wxSizerItem
*)node
->Data();
4038 if ( (item
->GetWindow() == m_cornerLabelWin
&& m_rowLabelWidth
> 0) ||
4039 item
->GetWindow() == m_colLabelWin
)
4041 sz
= item
->GetWindow()->GetSize();
4042 item
->SetInitSize( sz
.GetWidth(), height
);
4044 node
= node
->Next();
4048 m_colLabelHeight
= height
;
4049 m_mainSizer
->Layout();
4054 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4056 if ( m_labelBackgroundColour
!= colour
)
4058 m_labelBackgroundColour
= colour
;
4059 m_rowLabelWin
->SetBackgroundColour( colour
);
4060 m_colLabelWin
->SetBackgroundColour( colour
);
4061 m_cornerLabelWin
->SetBackgroundColour( colour
);
4063 if ( !GetBatchCount() )
4065 m_rowLabelWin
->Refresh();
4066 m_colLabelWin
->Refresh();
4067 m_cornerLabelWin
->Refresh();
4072 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4074 if ( m_labelTextColour
!= colour
)
4076 m_labelTextColour
= colour
;
4077 if ( !GetBatchCount() )
4079 m_rowLabelWin
->Refresh();
4080 m_colLabelWin
->Refresh();
4085 void wxGrid::SetLabelFont( const wxFont
& font
)
4088 if ( !GetBatchCount() )
4090 m_rowLabelWin
->Refresh();
4091 m_colLabelWin
->Refresh();
4095 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4097 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4099 m_rowLabelHorizAlign
= horiz
;
4102 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4104 m_rowLabelVertAlign
= vert
;
4107 if ( !GetBatchCount() )
4109 m_rowLabelWin
->Refresh();
4113 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4115 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4117 m_colLabelHorizAlign
= horiz
;
4120 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4122 m_colLabelVertAlign
= vert
;
4125 if ( !GetBatchCount() )
4127 m_colLabelWin
->Refresh();
4131 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4135 m_table
->SetRowLabelValue( row
, s
);
4136 if ( !GetBatchCount() )
4138 wxRect rect
= CellToRect( row
, 0);
4139 if ( rect
.height
> 0 )
4141 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4143 rect
.width
= m_rowLabelWidth
;
4144 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4150 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4154 m_table
->SetColLabelValue( col
, s
);
4155 if ( !GetBatchCount() )
4157 wxRect rect
= CellToRect( 0, col
);
4158 if ( rect
.width
> 0 )
4160 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4162 rect
.height
= m_colLabelHeight
;
4163 m_colLabelWin
->Refresh( TRUE
, &rect
);
4169 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4171 if ( m_gridLineColour
!= colour
)
4173 m_gridLineColour
= colour
;
4175 wxClientDC
dc( m_gridWin
);
4177 DrawAllGridLines( dc
, wxRegion() );
4181 void wxGrid::EnableGridLines( bool enable
)
4183 if ( enable
!= m_gridLinesEnabled
)
4185 m_gridLinesEnabled
= enable
;
4187 if ( !GetBatchCount() )
4191 wxClientDC
dc( m_gridWin
);
4193 DrawAllGridLines( dc
, wxRegion() );
4197 m_gridWin
->Refresh();
4204 int wxGrid::GetDefaultRowSize()
4206 return m_defaultRowHeight
;
4209 int wxGrid::GetRowSize( int row
)
4211 if ( row
>= 0 && row
< m_numRows
)
4212 return m_rowHeights
[row
];
4214 return 0; // TODO: log an error here
4217 int wxGrid::GetDefaultColSize()
4219 return m_defaultColWidth
;
4222 int wxGrid::GetColSize( int col
)
4224 if ( col
>= 0 && col
< m_numCols
)
4225 return m_colWidths
[col
];
4227 return 0; // TODO: log an error here
4230 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4232 // TODO: replace this temp test code
4234 return wxColour( 255, 255, 255 );
4237 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4239 // TODO: replace this temp test code
4241 return wxColour( 255, 255, 255 );
4244 wxColour
wxGrid::GetDefaultCellTextColour()
4246 // TODO: replace this temp test code
4248 return wxColour( 0, 0, 0 );
4251 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4253 // TODO: replace this temp test code
4255 return wxColour( 0, 0, 0 );
4259 wxFont
wxGrid::GetDefaultCellFont()
4261 return m_defaultCellFont
;
4264 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
4266 // TODO: replace this temp test code
4268 return m_defaultCellFont
;
4271 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4273 // TODO: replace this temp test code
4279 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
4281 // TODO: replace this temp test code
4287 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4289 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4291 if ( resizeExistingRows
)
4295 for ( row
= 0; row
< m_numRows
; row
++ )
4297 m_rowHeights
[row
] = m_defaultRowHeight
;
4298 bottom
+= m_defaultRowHeight
;
4299 m_rowBottoms
[row
] = bottom
;
4305 void wxGrid::SetRowSize( int row
, int height
)
4309 if ( row
>= 0 && row
< m_numRows
)
4311 int h
= wxMax( 0, height
);
4312 int diff
= h
- m_rowHeights
[row
];
4314 m_rowHeights
[row
] = h
;
4315 for ( i
= row
; i
< m_numRows
; i
++ )
4317 m_rowBottoms
[i
] += diff
;
4321 // Note: we are ending the event *after* doing
4322 // default processing in this case
4324 SendEvent( EVT_GRID_ROW_SIZE
,
4329 // TODO: log an error here
4333 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4335 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4337 if ( resizeExistingCols
)
4341 for ( col
= 0; col
< m_numCols
; col
++ )
4343 m_colWidths
[col
] = m_defaultColWidth
;
4344 right
+= m_defaultColWidth
;
4345 m_colRights
[col
] = right
;
4351 void wxGrid::SetColSize( int col
, int width
)
4355 if ( col
>= 0 && col
< m_numCols
)
4357 int w
= wxMax( 0, width
);
4358 int diff
= w
- m_colWidths
[col
];
4359 m_colWidths
[col
] = w
;
4361 for ( i
= col
; i
< m_numCols
; i
++ )
4363 m_colRights
[i
] += diff
;
4367 // Note: we are ending the event *after* doing
4368 // default processing in this case
4370 SendEvent( EVT_GRID_COL_SIZE
,
4375 // TODO: log an error here
4379 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4381 // TODO: everything !!!
4385 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4387 // TODO: everything !!!
4391 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4393 // TODO: everything !!!
4397 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4399 // TODO: everything !!!
4403 void wxGrid::SetDefaultCellFont( const wxFont
& )
4405 // TODO: everything !!!
4409 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4411 // TODO: everything !!!
4415 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4417 // TODO: everything !!!
4421 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4423 // TODO: everything !!!
4430 // ------ cell value accessor functions
4433 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4437 m_table
->SetValue( row
, col
, s
.c_str() );
4438 if ( !GetBatchCount() )
4440 wxClientDC
dc( m_gridWin
);
4442 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4445 #if 0 // TODO: edit in place
4447 if ( m_currentCellCoords
.GetRow() == row
&&
4448 m_currentCellCoords
.GetCol() == col
)
4450 SetEditControlValue( s
);
4459 // ------ Block, row and col selection
4462 void wxGrid::SelectRow( int row
, bool addToSelected
)
4466 if ( IsSelection() && addToSelected
)
4469 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4472 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4473 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4474 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4475 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4479 need_refresh
[0] = TRUE
;
4480 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4481 wxGridCellCoords ( oldTop
- 1,
4483 m_selectedTopLeft
.SetRow( row
);
4488 need_refresh
[1] = TRUE
;
4489 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4490 wxGridCellCoords ( oldBottom
,
4493 m_selectedTopLeft
.SetCol( 0 );
4496 if ( oldBottom
< row
)
4498 need_refresh
[2] = TRUE
;
4499 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4500 wxGridCellCoords ( row
,
4502 m_selectedBottomRight
.SetRow( row
);
4505 if ( oldRight
< m_numCols
- 1 )
4507 need_refresh
[3] = TRUE
;
4508 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4510 wxGridCellCoords ( oldBottom
,
4512 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4515 for (i
= 0; i
< 4; i
++ )
4516 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4517 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4521 r
= SelectionToDeviceRect();
4523 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4525 m_selectedTopLeft
.Set( row
, 0 );
4526 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4527 r
= SelectionToDeviceRect();
4528 m_gridWin
->Refresh( FALSE
, &r
);
4531 wxGridRangeSelectEvent
gridEvt( GetId(),
4532 EVT_GRID_RANGE_SELECT
,
4535 m_selectedBottomRight
);
4537 GetEventHandler()->ProcessEvent(gridEvt
);
4541 void wxGrid::SelectCol( int col
, bool addToSelected
)
4543 if ( IsSelection() && addToSelected
)
4546 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4549 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4550 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4551 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4552 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4554 if ( oldLeft
> col
)
4556 need_refresh
[0] = TRUE
;
4557 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4558 wxGridCellCoords ( m_numRows
- 1,
4560 m_selectedTopLeft
.SetCol( col
);
4565 need_refresh
[1] = TRUE
;
4566 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4567 wxGridCellCoords ( oldTop
- 1,
4569 m_selectedTopLeft
.SetRow( 0 );
4572 if ( oldRight
< col
)
4574 need_refresh
[2] = TRUE
;
4575 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4576 wxGridCellCoords ( m_numRows
- 1,
4578 m_selectedBottomRight
.SetCol( col
);
4581 if ( oldBottom
< m_numRows
- 1 )
4583 need_refresh
[3] = TRUE
;
4584 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4586 wxGridCellCoords ( m_numRows
- 1,
4588 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4591 for (i
= 0; i
< 4; i
++ )
4592 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4593 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4599 r
= SelectionToDeviceRect();
4601 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4603 m_selectedTopLeft
.Set( 0, col
);
4604 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4605 r
= SelectionToDeviceRect();
4606 m_gridWin
->Refresh( FALSE
, &r
);
4609 wxGridRangeSelectEvent
gridEvt( GetId(),
4610 EVT_GRID_RANGE_SELECT
,
4613 m_selectedBottomRight
);
4615 GetEventHandler()->ProcessEvent(gridEvt
);
4619 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4622 wxGridCellCoords updateTopLeft
, updateBottomRight
;
4624 if ( topRow
> bottomRow
)
4631 if ( leftCol
> rightCol
)
4638 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
4639 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
4641 if ( m_selectedTopLeft
!= updateTopLeft
||
4642 m_selectedBottomRight
!= updateBottomRight
)
4644 // Compute two optimal update rectangles:
4645 // Either one rectangle is a real subset of the
4646 // other, or they are (almost) disjoint!
4648 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4651 // Store intermediate values
4652 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4653 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4654 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4655 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4657 // Determine the outer/inner coordinates.
4658 if (oldLeft
> leftCol
)
4664 if (oldTop
> topRow
)
4670 if (oldRight
< rightCol
)
4673 oldRight
= rightCol
;
4676 if (oldBottom
< bottomRow
)
4679 oldBottom
= bottomRow
;
4683 // Now, either the stuff marked old is the outer
4684 // rectangle or we don't have a situation where one
4685 // is contained in the other.
4687 if ( oldLeft
< leftCol
)
4689 need_refresh
[0] = TRUE
;
4690 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4692 wxGridCellCoords ( oldBottom
,
4696 if ( oldTop
< topRow
)
4698 need_refresh
[1] = TRUE
;
4699 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4701 wxGridCellCoords ( topRow
- 1,
4705 if ( oldRight
> rightCol
)
4707 need_refresh
[2] = TRUE
;
4708 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4710 wxGridCellCoords ( oldBottom
,
4714 if ( oldBottom
> bottomRow
)
4716 need_refresh
[3] = TRUE
;
4717 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
4719 wxGridCellCoords ( oldBottom
,
4725 m_selectedTopLeft
= updateTopLeft
;
4726 m_selectedBottomRight
= updateBottomRight
;
4728 // various Refresh() calls
4729 for (i
= 0; i
< 4; i
++ )
4730 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4731 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4734 // only generate an event if the block is not being selected by
4735 // dragging the mouse (in which case the event will be generated in
4736 // the mouse event handler)
4737 if ( !m_isDragging
)
4739 wxGridRangeSelectEvent
gridEvt( GetId(),
4740 EVT_GRID_RANGE_SELECT
,
4743 m_selectedBottomRight
);
4745 GetEventHandler()->ProcessEvent(gridEvt
);
4749 void wxGrid::SelectAll()
4751 m_selectedTopLeft
.Set( 0, 0 );
4752 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4754 m_gridWin
->Refresh();
4758 void wxGrid::ClearSelection()
4760 m_selectedTopLeft
= wxGridNoCellCoords
;
4761 m_selectedBottomRight
= wxGridNoCellCoords
;
4765 // This function returns the rectangle that encloses the given block
4766 // in device coords clipped to the client size of the grid window.
4768 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
4769 const wxGridCellCoords
&bottomRight
)
4771 wxRect
rect( wxGridNoCellRect
);
4774 cellRect
= CellToRect( topLeft
);
4775 if ( cellRect
!= wxGridNoCellRect
)
4781 rect
= wxRect( 0, 0, 0, 0 );
4784 cellRect
= CellToRect( bottomRight
);
4785 if ( cellRect
!= wxGridNoCellRect
)
4791 return wxGridNoCellRect
;
4794 // convert to scrolled coords
4796 int left
, top
, right
, bottom
;
4797 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4798 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4801 m_gridWin
->GetClientSize( &cw
, &ch
);
4803 rect
.SetLeft( wxMax(0, left
) );
4804 rect
.SetTop( wxMax(0, top
) );
4805 rect
.SetRight( wxMin(cw
, right
) );
4806 rect
.SetBottom( wxMin(ch
, bottom
) );
4814 // ------ Grid event classes
4817 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4819 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4820 int row
, int col
, int x
, int y
,
4821 bool control
, bool shift
, bool alt
, bool meta
)
4822 : wxNotifyEvent( type
, id
)
4828 m_control
= control
;
4833 SetEventObject(obj
);
4837 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4839 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4840 int rowOrCol
, int x
, int y
,
4841 bool control
, bool shift
, bool alt
, bool meta
)
4842 : wxNotifyEvent( type
, id
)
4844 m_rowOrCol
= rowOrCol
;
4847 m_control
= control
;
4852 SetEventObject(obj
);
4856 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4858 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4859 const wxGridCellCoords
& topLeft
,
4860 const wxGridCellCoords
& bottomRight
,
4861 bool control
, bool shift
, bool alt
, bool meta
)
4862 : wxNotifyEvent( type
, id
)
4864 m_topLeft
= topLeft
;
4865 m_bottomRight
= bottomRight
;
4866 m_control
= control
;
4871 SetEventObject(obj
);
4875 #endif // ifndef wxUSE_NEW_GRID