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()
1133 GetClientSize( &cw
, &ch
);
1135 if ( m_numRows
> 0 && m_numCols
> 0 )
1137 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1138 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1140 // TODO: restore the scroll position that we had before sizing
1143 GetViewStart( &x
, &y
);
1144 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1145 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1151 // this is called when the grid table sends a message to say that it
1152 // has been redimensioned
1154 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1158 switch ( msg
.GetId() )
1160 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1162 size_t pos
= msg
.GetCommandInt();
1163 int numRows
= msg
.GetCommandInt2();
1164 for ( i
= 0; i
< numRows
; i
++ )
1166 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1167 m_rowBottoms
.Insert( 0, pos
);
1169 m_numRows
+= numRows
;
1172 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1174 for ( i
= pos
; i
< m_numRows
; i
++ )
1176 bottom
+= m_rowHeights
[i
];
1177 m_rowBottoms
[i
] = bottom
;
1183 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1185 int numRows
= msg
.GetCommandInt();
1186 for ( i
= 0; i
< numRows
; i
++ )
1188 m_rowHeights
.Add( m_defaultRowHeight
);
1189 m_rowBottoms
.Add( 0 );
1192 int oldNumRows
= m_numRows
;
1193 m_numRows
+= numRows
;
1196 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1198 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1200 bottom
+= m_rowHeights
[i
];
1201 m_rowBottoms
[i
] = bottom
;
1207 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1209 size_t pos
= msg
.GetCommandInt();
1210 int numRows
= msg
.GetCommandInt2();
1211 for ( i
= 0; i
< numRows
; i
++ )
1213 m_rowHeights
.Remove( pos
);
1214 m_rowBottoms
.Remove( pos
);
1216 m_numRows
-= numRows
;
1221 m_colWidths
.Clear();
1222 m_colRights
.Clear();
1223 m_currentCellCoords
= wxGridNoCellCoords
;
1227 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1228 m_currentCellCoords
.Set( 0, 0 );
1231 for ( i
= 0; i
< m_numRows
; i
++ )
1233 h
+= m_rowHeights
[i
];
1234 m_rowBottoms
[i
] = h
;
1242 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1244 size_t pos
= msg
.GetCommandInt();
1245 int numCols
= msg
.GetCommandInt2();
1246 for ( i
= 0; i
< numCols
; i
++ )
1248 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1249 m_colRights
.Insert( 0, pos
);
1251 m_numCols
+= numCols
;
1254 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1256 for ( i
= pos
; i
< m_numCols
; i
++ )
1258 right
+= m_colWidths
[i
];
1259 m_colRights
[i
] = right
;
1265 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1267 int numCols
= msg
.GetCommandInt();
1268 for ( i
= 0; i
< numCols
; i
++ )
1270 m_colWidths
.Add( m_defaultColWidth
);
1271 m_colRights
.Add( 0 );
1274 int oldNumCols
= m_numCols
;
1275 m_numCols
+= numCols
;
1278 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1280 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1282 right
+= m_colWidths
[i
];
1283 m_colRights
[i
] = right
;
1289 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1291 size_t pos
= msg
.GetCommandInt();
1292 int numCols
= msg
.GetCommandInt2();
1293 for ( i
= 0; i
< numCols
; i
++ )
1295 m_colWidths
.Remove( pos
);
1296 m_colRights
.Remove( pos
);
1298 m_numCols
-= numCols
;
1302 #if 0 // leave the row alone here so that AppendCols will work subsequently
1304 m_rowHeights
.Clear();
1305 m_rowBottoms
.Clear();
1307 m_currentCellCoords
= wxGridNoCellCoords
;
1311 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1312 m_currentCellCoords
.Set( 0, 0 );
1315 for ( i
= 0; i
< m_numCols
; i
++ )
1317 w
+= m_colWidths
[i
];
1330 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1332 wxRegionIterator
iter( reg
);
1335 m_rowLabelsExposed
.Empty();
1342 // TODO: remove this when we can...
1343 // There is a bug in wxMotif that gives garbage update
1344 // rectangles if you jump-scroll a long way by clicking the
1345 // scrollbar with middle button. This is a work-around
1347 #if defined(__WXMOTIF__)
1349 m_gridWin
->GetClientSize( &cw
, &ch
);
1350 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1351 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1354 // logical bounds of update region
1357 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1358 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1360 // find the row labels within these bounds
1364 for ( row
= 0; row
< m_numRows
; row
++ )
1366 if ( m_rowBottoms
[row
] < top
) continue;
1368 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1369 if ( rowTop
> bottom
) break;
1371 m_rowLabelsExposed
.Add( row
);
1379 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1381 wxRegionIterator
iter( reg
);
1384 m_colLabelsExposed
.Empty();
1391 // TODO: remove this when we can...
1392 // There is a bug in wxMotif that gives garbage update
1393 // rectangles if you jump-scroll a long way by clicking the
1394 // scrollbar with middle button. This is a work-around
1396 #if defined(__WXMOTIF__)
1398 m_gridWin
->GetClientSize( &cw
, &ch
);
1399 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1400 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1403 // logical bounds of update region
1406 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1407 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1409 // find the cells within these bounds
1413 for ( col
= 0; col
< m_numCols
; col
++ )
1415 if ( m_colRights
[col
] < left
) continue;
1417 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1418 if ( colLeft
> right
) break;
1420 m_colLabelsExposed
.Add( col
);
1428 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1430 wxRegionIterator
iter( reg
);
1433 m_cellsExposed
.Empty();
1434 m_rowsExposed
.Empty();
1435 m_colsExposed
.Empty();
1437 int left
, top
, right
, bottom
;
1442 // TODO: remove this when we can...
1443 // There is a bug in wxMotif that gives garbage update
1444 // rectangles if you jump-scroll a long way by clicking the
1445 // scrollbar with middle button. This is a work-around
1447 #if defined(__WXMOTIF__)
1449 m_gridWin
->GetClientSize( &cw
, &ch
);
1450 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1451 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1452 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1453 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1456 // logical bounds of update region
1458 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1459 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1461 // find the cells within these bounds
1464 int colLeft
, rowTop
;
1465 for ( row
= 0; row
< m_numRows
; row
++ )
1467 if ( m_rowBottoms
[row
] < top
) continue;
1469 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1470 if ( rowTop
> bottom
) break;
1472 m_rowsExposed
.Add( row
);
1474 for ( col
= 0; col
< m_numCols
; col
++ )
1476 if ( m_colRights
[col
] < left
) continue;
1478 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1479 if ( colLeft
> right
) break;
1481 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1482 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1491 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1494 wxPoint
pos( event
.GetPosition() );
1495 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1497 if ( event
.Dragging() )
1499 m_isDragging
= TRUE
;
1501 if ( event
.LeftIsDown() )
1503 switch( m_cursorMode
)
1505 case WXGRID_CURSOR_RESIZE_ROW
:
1507 int cw
, ch
, left
, dummy
;
1508 m_gridWin
->GetClientSize( &cw
, &ch
);
1509 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1511 wxClientDC
dc( m_gridWin
);
1513 dc
.SetLogicalFunction(wxINVERT
);
1514 if ( m_dragLastPos
>= 0 )
1516 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1518 dc
.DrawLine( left
, y
, left
+cw
, y
);
1523 case WXGRID_CURSOR_SELECT_ROW
:
1525 if ( (row
= YToRow( y
)) >= 0 &&
1526 !IsInSelection( row
, 0 ) )
1528 SelectRow( row
, TRUE
);
1537 m_isDragging
= FALSE
;
1540 // ------------ Left button pressed
1542 if ( event
.LeftDown() )
1544 // don't send a label click event for a hit on the
1545 // edge of the row label - this is probably the user
1546 // wanting to resize the row
1548 if ( YToEdgeOfRow(y
) < 0 )
1552 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1554 SelectRow( row
, event
.ShiftDown() );
1555 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1560 // starting to drag-resize a row
1562 m_rowLabelWin
->CaptureMouse();
1567 // ------------ Left double click
1569 else if (event
.LeftDClick() )
1571 if ( YToEdgeOfRow(y
) < 0 )
1574 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1579 // ------------ Left button released
1581 else if ( event
.LeftUp() )
1583 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1585 m_rowLabelWin
->ReleaseMouse();
1587 if ( m_dragLastPos
>= 0 )
1589 // erase the last line and resize the row
1591 int cw
, ch
, left
, dummy
;
1592 m_gridWin
->GetClientSize( &cw
, &ch
);
1593 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1595 wxClientDC
dc( m_gridWin
);
1597 dc
.SetLogicalFunction( wxINVERT
);
1598 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1599 HideCellEditControl();
1601 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
1602 SetRowSize( m_dragRowOrCol
, wxMax( y
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
1603 if ( !GetBatchCount() )
1605 // Only needed to get the correct rect.y:
1606 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
1608 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
1609 rect
.width
= m_rowLabelWidth
;
1610 rect
.height
= ch
- rect
.y
;
1611 m_rowLabelWin
->Refresh( TRUE
, &rect
);
1613 m_gridWin
->Refresh( FALSE
, &rect
);
1616 ShowCellEditControl();
1618 // Note: we are ending the event *after* doing
1619 // default processing in this case
1621 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1625 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1630 // ------------ Right button down
1632 else if ( event
.RightDown() )
1635 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1637 // no default action at the moment
1642 // ------------ Right double click
1644 else if ( event
.RightDClick() )
1647 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1649 // no default action at the moment
1654 // ------------ No buttons down and mouse moving
1656 else if ( event
.Moving() )
1658 m_dragRowOrCol
= YToEdgeOfRow( y
);
1659 if ( m_dragRowOrCol
>= 0 )
1661 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1663 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1664 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1669 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1670 if ( m_rowLabelWin
->GetCursor() == m_rowResizeCursor
)
1671 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1677 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1680 wxPoint
pos( event
.GetPosition() );
1681 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1683 if ( event
.Dragging() )
1685 m_isDragging
= TRUE
;
1687 if ( event
.LeftIsDown() )
1689 switch( m_cursorMode
)
1691 case WXGRID_CURSOR_RESIZE_COL
:
1693 int cw
, ch
, dummy
, top
;
1694 m_gridWin
->GetClientSize( &cw
, &ch
);
1695 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1697 wxClientDC
dc( m_gridWin
);
1699 dc
.SetLogicalFunction(wxINVERT
);
1700 if ( m_dragLastPos
>= 0 )
1702 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1704 dc
.DrawLine( x
, top
, x
, top
+ch
);
1709 case WXGRID_CURSOR_SELECT_COL
:
1711 if ( (col
= XToCol( x
)) >= 0 &&
1712 !IsInSelection( 0, col
) )
1714 SelectCol( col
, TRUE
);
1723 m_isDragging
= FALSE
;
1726 // ------------ Left button pressed
1728 if ( event
.LeftDown() )
1730 // don't send a label click event for a hit on the
1731 // edge of the col label - this is probably the user
1732 // wanting to resize the col
1734 if ( XToEdgeOfCol(x
) < 0 )
1738 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1740 SelectCol( col
, event
.ShiftDown() );
1741 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1746 // starting to drag-resize a col
1748 m_colLabelWin
->CaptureMouse();
1753 // ------------ Left double click
1755 if ( event
.LeftDClick() )
1757 if ( XToEdgeOfCol(x
) < 0 )
1760 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
1765 // ------------ Left button released
1767 else if ( event
.LeftUp() )
1769 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
1771 m_colLabelWin
->ReleaseMouse();
1773 if ( m_dragLastPos
>= 0 )
1775 // erase the last line and resize the col
1777 int cw
, ch
, dummy
, top
;
1778 m_gridWin
->GetClientSize( &cw
, &ch
);
1779 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1781 wxClientDC
dc( m_gridWin
);
1783 dc
.SetLogicalFunction( wxINVERT
);
1784 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1785 HideCellEditControl();
1787 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
1788 SetColSize( m_dragRowOrCol
, wxMax( x
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
1790 if ( !GetBatchCount() )
1792 // Only needed to get the correct rect.x:
1793 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
1795 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
1796 rect
.width
= cw
- rect
.x
;
1797 rect
.height
= m_colLabelHeight
;
1798 m_colLabelWin
->Refresh( TRUE
, &rect
);
1800 m_gridWin
->Refresh( FALSE
, &rect
);
1803 ShowCellEditControl();
1805 // Note: we are ending the event *after* doing
1806 // default processing in this case
1808 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1812 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1817 // ------------ Right button down
1819 else if ( event
.RightDown() )
1822 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1824 // no default action at the moment
1829 // ------------ Right double click
1831 else if ( event
.RightDClick() )
1834 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1836 // no default action at the moment
1841 // ------------ No buttons down and mouse moving
1843 else if ( event
.Moving() )
1845 m_dragRowOrCol
= XToEdgeOfCol( x
);
1846 if ( m_dragRowOrCol
>= 0 )
1848 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1850 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1851 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1856 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1857 if ( m_colLabelWin
->GetCursor() == m_colResizeCursor
)
1858 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1864 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
1866 if ( event
.LeftDown() )
1868 // indicate corner label by having both row and
1871 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
1877 else if ( event
.LeftDClick() )
1879 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
1882 else if ( event
.RightDown() )
1884 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
1886 // no default action at the moment
1890 else if ( event
.RightDClick() )
1892 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
1894 // no default action at the moment
1900 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
1903 wxPoint
pos( event
.GetPosition() );
1904 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1906 wxGridCellCoords coords
;
1907 XYToCell( x
, y
, coords
);
1909 if ( event
.Dragging() )
1911 m_isDragging
= TRUE
;
1912 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1914 // Hide the edit control, so it
1915 // won't interfer with drag-shrinking.
1916 if ( IsCellEditControlEnabled() )
1917 HideCellEditControl();
1918 if ( coords
!= wxGridNoCellCoords
)
1920 if ( !IsSelection() )
1922 SelectBlock( coords
, coords
);
1926 SelectBlock( m_currentCellCoords
, coords
);
1934 m_isDragging
= FALSE
;
1936 if ( coords
!= wxGridNoCellCoords
)
1938 if ( event
.LeftDown() )
1940 if ( event
.ShiftDown() )
1942 SelectBlock( m_currentCellCoords
, coords
);
1946 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1951 MakeCellVisible( coords
);
1952 SetCurrentCell( coords
);
1958 // ------------ Left double click
1960 else if ( event
.LeftDClick() )
1962 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1969 // ------------ Left button released
1971 else if ( event
.LeftUp() )
1973 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1975 if ( IsSelection() )
1977 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
1981 // Show the edit control, if it has
1982 // been hidden for drag-shrinking.
1983 if ( IsCellEditControlEnabled() )
1984 ShowCellEditControl();
1990 // ------------ Right button down
1992 else if ( event
.RightDown() )
1994 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
1999 // no default action at the moment
2004 // ------------ Right double click
2006 else if ( event
.RightDClick() )
2008 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2013 // no default action at the moment
2017 // ------------ Moving and no button action
2019 else if ( event
.Moving() && !event
.IsButton() )
2021 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2028 // ------ interaction with data model
2030 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2032 switch ( msg
.GetId() )
2034 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2035 return GetModelValues();
2037 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2038 return SetModelValues();
2040 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2041 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2042 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2043 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2044 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2045 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2046 return Redimension( msg
);
2055 // The behaviour of this function depends on the grid table class
2056 // Clear() function. For the default wxGridStringTable class the
2057 // behavious is to replace all cell contents with wxEmptyString but
2058 // not to change the number of rows or cols.
2060 void wxGrid::ClearGrid()
2065 SetEditControlValue();
2066 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2071 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2073 // TODO: something with updateLabels flag
2077 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2083 bool ok
= m_table
->InsertRows( pos
, numRows
);
2085 // the table will have sent the results of the insert row
2086 // operation to this view object as a grid table message
2090 if ( m_numCols
== 0 )
2092 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2094 // TODO: perhaps instead of appending the default number of cols
2095 // we should remember what the last non-zero number of cols was ?
2099 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2101 // if we have just inserted cols into an empty grid the current
2102 // cell will be undefined...
2104 SetCurrentCell( 0, 0 );
2108 if ( !GetBatchCount() ) Refresh();
2111 SetEditControlValue();
2121 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2123 // TODO: something with updateLabels flag
2127 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2131 if ( m_table
&& m_table
->AppendRows( numRows
) )
2133 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2135 // if we have just inserted cols into an empty grid the current
2136 // cell will be undefined...
2138 SetCurrentCell( 0, 0 );
2141 // the table will have sent the results of the append row
2142 // operation to this view object as a grid table message
2145 if ( !GetBatchCount() ) Refresh();
2155 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2157 // TODO: something with updateLabels flag
2161 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2165 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2167 // the table will have sent the results of the delete row
2168 // operation to this view object as a grid table message
2170 if ( m_numRows
> 0 )
2171 SetEditControlValue();
2173 HideCellEditControl();
2176 if ( !GetBatchCount() ) Refresh();
2186 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2188 // TODO: something with updateLabels flag
2192 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2198 HideCellEditControl();
2199 bool ok
= m_table
->InsertCols( pos
, numCols
);
2201 // the table will have sent the results of the insert col
2202 // operation to this view object as a grid table message
2206 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2208 // if we have just inserted cols into an empty grid the current
2209 // cell will be undefined...
2211 SetCurrentCell( 0, 0 );
2215 if ( !GetBatchCount() ) Refresh();
2218 SetEditControlValue();
2228 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2230 // TODO: something with updateLabels flag
2234 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2238 if ( m_table
&& m_table
->AppendCols( numCols
) )
2240 // the table will have sent the results of the append col
2241 // operation to this view object as a grid table message
2243 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2245 // if we have just inserted cols into an empty grid the current
2246 // cell will be undefined...
2248 SetCurrentCell( 0, 0 );
2252 if ( !GetBatchCount() ) Refresh();
2262 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2264 // TODO: something with updateLabels flag
2268 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2272 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2274 // the table will have sent the results of the delete col
2275 // operation to this view object as a grid table message
2277 if ( m_numCols
> 0 )
2278 SetEditControlValue();
2280 HideCellEditControl();
2283 if ( !GetBatchCount() ) Refresh();
2295 // ----- event handlers
2298 // Generate a grid event based on a mouse event and
2299 // return the result of ProcessEvent()
2301 bool wxGrid::SendEvent( const wxEventType type
,
2303 wxMouseEvent
& mouseEv
)
2305 if ( type
== EVT_GRID_ROW_SIZE
||
2306 type
== EVT_GRID_COL_SIZE
)
2308 int rowOrCol
= (row
== -1 ? col
: row
);
2310 wxGridSizeEvent
gridEvt( GetId(),
2314 mouseEv
.GetX(), mouseEv
.GetY(),
2315 mouseEv
.ControlDown(),
2316 mouseEv
.ShiftDown(),
2318 mouseEv
.MetaDown() );
2320 return GetEventHandler()->ProcessEvent(gridEvt
);
2322 else if ( type
== EVT_GRID_RANGE_SELECT
)
2324 wxGridRangeSelectEvent
gridEvt( GetId(),
2328 m_selectedBottomRight
,
2329 mouseEv
.ControlDown(),
2330 mouseEv
.ShiftDown(),
2332 mouseEv
.MetaDown() );
2334 return GetEventHandler()->ProcessEvent(gridEvt
);
2338 wxGridEvent
gridEvt( GetId(),
2342 mouseEv
.GetX(), mouseEv
.GetY(),
2343 mouseEv
.ControlDown(),
2344 mouseEv
.ShiftDown(),
2346 mouseEv
.MetaDown() );
2348 return GetEventHandler()->ProcessEvent(gridEvt
);
2353 // Generate a grid event of specified type and return the result
2354 // of ProcessEvent().
2356 bool wxGrid::SendEvent( const wxEventType type
,
2359 if ( type
== EVT_GRID_ROW_SIZE
||
2360 type
== EVT_GRID_COL_SIZE
)
2362 int rowOrCol
= (row
== -1 ? col
: row
);
2364 wxGridSizeEvent
gridEvt( GetId(),
2369 return GetEventHandler()->ProcessEvent(gridEvt
);
2373 wxGridEvent
gridEvt( GetId(),
2378 return GetEventHandler()->ProcessEvent(gridEvt
);
2383 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2385 wxPaintDC
dc( this );
2387 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2388 m_numRows
&& m_numCols
)
2390 m_currentCellCoords
.Set(0, 0);
2391 SetEditControlValue();
2392 ShowCellEditControl();
2399 // This is just here to make sure that CalcDimensions gets called when
2400 // the grid view is resized... then the size event is skipped to allow
2401 // the box sizers to handle everything
2403 void wxGrid::OnSize( wxSizeEvent
& event
)
2410 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2412 if ( m_inOnKeyDown
)
2414 // shouldn't be here - we are going round in circles...
2416 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2419 m_inOnKeyDown
= TRUE
;
2421 // propagate the event up and see if it gets processed
2423 wxWindow
*parent
= GetParent();
2424 wxKeyEvent
keyEvt( event
);
2425 keyEvt
.SetEventObject( parent
);
2427 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2429 // try local handlers
2431 switch ( event
.KeyCode() )
2434 if ( event
.ControlDown() )
2436 MoveCursorUpBlock();
2445 if ( event
.ControlDown() )
2447 MoveCursorDownBlock();
2456 if ( event
.ControlDown() )
2458 MoveCursorLeftBlock();
2467 if ( event
.ControlDown() )
2469 MoveCursorRightBlock();
2478 if ( !IsEditable() )
2489 if ( event
.ControlDown() )
2491 event
.Skip(); // to let the edit control have the return
2500 if ( event
.ControlDown() )
2502 MakeCellVisible( 0, 0 );
2503 SetCurrentCell( 0, 0 );
2512 if ( event
.ControlDown() )
2514 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2515 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2532 // now try the cell edit control
2534 if ( IsCellEditControlEnabled() )
2536 event
.SetEventObject( m_cellEditCtrl
);
2537 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2543 m_inOnKeyDown
= FALSE
;
2547 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2549 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2551 // the event has been intercepted - do nothing
2556 m_currentCellCoords
!= wxGridNoCellCoords
)
2558 HideCellEditControl();
2559 SaveEditControlValue();
2562 m_currentCellCoords
= coords
;
2564 SetEditControlValue();
2568 ShowCellEditControl();
2570 if ( IsSelection() )
2572 wxRect
r( SelectionToDeviceRect() );
2574 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
2581 // ------ functions to get/send data (see also public functions)
2584 bool wxGrid::GetModelValues()
2588 // all we need to do is repaint the grid
2590 m_gridWin
->Refresh();
2598 bool wxGrid::SetModelValues()
2604 for ( row
= 0; row
< m_numRows
; row
++ )
2606 for ( col
= 0; col
< m_numCols
; col
++ )
2608 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2620 // Note - this function only draws cells that are in the list of
2621 // exposed cells (usually set from the update region by
2622 // CalcExposedCells)
2624 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2626 if ( !m_numRows
|| !m_numCols
) return;
2629 size_t numCells
= m_cellsExposed
.GetCount();
2631 for ( i
= 0; i
< numCells
; i
++ )
2633 DrawCell( dc
, m_cellsExposed
[i
] );
2638 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2640 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2641 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2643 #if !WXGRID_DRAW_LINES
2644 if ( m_gridLinesEnabled
)
2645 DrawCellBorder( dc
, coords
);
2648 DrawCellBackground( dc
, coords
);
2650 // TODO: separate functions here for different kinds of cells ?
2653 DrawCellValue( dc
, coords
);
2657 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2659 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2660 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2662 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2663 int row
= coords
.GetRow();
2664 int col
= coords
.GetCol();
2666 // right hand border
2668 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
2669 m_colRights
[col
], m_rowBottoms
[row
] );
2673 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
2674 m_colRights
[col
], m_rowBottoms
[row
] );
2678 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2680 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2681 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2683 int row
= coords
.GetRow();
2684 int col
= coords
.GetCol();
2686 dc
.SetBackgroundMode( wxSOLID
);
2688 if ( IsInSelection( coords
) )
2690 // TODO: improve this
2692 dc
.SetBrush( *wxBLACK_BRUSH
);
2696 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2699 dc
.SetPen( *wxTRANSPARENT_PEN
);
2701 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2702 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2704 m_rowHeights
[row
]-1 );
2708 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2710 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2711 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2713 int row
= coords
.GetRow();
2714 int col
= coords
.GetCol();
2716 dc
.SetBackgroundMode( wxTRANSPARENT
);
2718 if ( IsInSelection( row
, col
) )
2720 // TODO: improve this
2722 dc
.SetTextBackground( wxColour(0, 0, 0) );
2723 dc
.SetTextForeground( wxColour(255, 255, 255) );
2727 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2728 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2730 dc
.SetFont( GetCellFont(row
, col
) );
2733 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2736 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2737 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2738 rect
.SetWidth( m_colWidths
[col
] - 4 );
2739 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2741 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2746 // TODO: remove this ???
2747 // This is used to redraw all grid lines e.g. when the grid line colour
2750 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
2752 if ( !m_gridLinesEnabled
||
2754 !m_numCols
) return;
2756 int top
, bottom
, left
, right
;
2760 m_gridWin
->GetClientSize(&cw
, &ch
);
2762 // virtual coords of visible area
2764 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2765 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2769 reg
.GetBox(x
, y
, w
, h
);
2770 CalcUnscrolledPosition( x
, y
, &left
, &top
);
2771 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
2774 // avoid drawing grid lines past the last row and col
2776 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
2777 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
2779 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2781 // horizontal grid lines
2784 for ( i
= 0; i
< m_numRows
; i
++ )
2786 if ( m_rowBottoms
[i
] > bottom
)
2790 else if ( m_rowBottoms
[i
] >= top
)
2792 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
2797 // vertical grid lines
2799 for ( i
= 0; i
< m_numCols
; i
++ )
2801 if ( m_colRights
[i
] > right
)
2805 else if ( m_colRights
[i
] >= left
)
2807 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
2813 void wxGrid::DrawRowLabels( wxDC
& dc
)
2815 if ( !m_numRows
|| !m_numCols
) return;
2818 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2820 for ( i
= 0; i
< numLabels
; i
++ )
2822 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2827 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2829 if ( m_rowHeights
[row
] <= 0 ) return;
2831 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2833 dc
.SetPen( *wxBLACK_PEN
);
2834 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
2835 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
2837 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
2838 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
2840 dc
.SetPen( *wxWHITE_PEN
);
2841 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
2842 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
2844 dc
.SetBackgroundMode( wxTRANSPARENT
);
2845 dc
.SetTextForeground( GetLabelTextColour() );
2846 dc
.SetFont( GetLabelFont() );
2849 GetRowLabelAlignment( &hAlign
, &vAlign
);
2853 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2854 rect
.SetWidth( m_rowLabelWidth
- 4 );
2855 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2856 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2860 void wxGrid::DrawColLabels( wxDC
& dc
)
2862 if ( !m_numRows
|| !m_numCols
) return;
2865 size_t numLabels
= m_colLabelsExposed
.GetCount();
2867 for ( i
= 0; i
< numLabels
; i
++ )
2869 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2874 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2876 if ( m_colWidths
[col
] <= 0 ) return;
2878 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2880 dc
.SetPen( *wxBLACK_PEN
);
2881 dc
.DrawLine( m_colRights
[col
]-1, 0,
2882 m_colRights
[col
]-1, m_colLabelHeight
-1 );
2884 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
2885 m_colRights
[col
]-1, m_colLabelHeight
-1 );
2887 dc
.SetPen( *wxWHITE_PEN
);
2888 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
2889 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
2891 dc
.SetBackgroundMode( wxTRANSPARENT
);
2892 dc
.SetTextForeground( GetLabelTextColour() );
2893 dc
.SetFont( GetLabelFont() );
2895 dc
.SetBackgroundMode( wxTRANSPARENT
);
2896 dc
.SetTextForeground( GetLabelTextColour() );
2897 dc
.SetFont( GetLabelFont() );
2900 GetColLabelAlignment( &hAlign
, &vAlign
);
2903 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2905 rect
.SetWidth( m_colWidths
[col
] - 4 );
2906 rect
.SetHeight( m_colLabelHeight
- 4 );
2907 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2911 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2912 const wxString
& value
,
2917 long textWidth
, textHeight
;
2918 long lineWidth
, lineHeight
;
2919 wxArrayString lines
;
2921 dc
.SetClippingRegion( rect
);
2922 StringToLines( value
, lines
);
2923 if ( lines
.GetCount() )
2925 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2926 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2929 switch ( horizAlign
)
2932 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2936 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2945 switch ( vertAlign
)
2948 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2952 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2961 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2963 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2968 dc
.DestroyClippingRegion();
2972 // Split multi line text up into an array of strings. Any existing
2973 // contents of the string array are preserved.
2975 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2977 // TODO: this won't work for WXMAC ? (lines end with '\r')
2978 // => use wxTextFile functions then (VZ)
2981 while ( startPos
< (int)value
.Length() )
2983 pos
= value
.Mid(startPos
).Find( '\n' );
2988 else if ( pos
== 0 )
2990 lines
.Add( wxEmptyString
);
2994 if ( value
[startPos
+pos
-1] == '\r' )
2996 lines
.Add( value
.Mid(startPos
, pos
-1) );
3000 lines
.Add( value
.Mid(startPos
, pos
) );
3005 if ( startPos
< (int)value
.Length() )
3007 lines
.Add( value
.Mid( startPos
) );
3012 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3013 wxArrayString
& lines
,
3014 long *width
, long *height
)
3021 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3023 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3024 w
= wxMax( w
, lineW
);
3034 // ------ Edit control functions
3038 void wxGrid::EnableEditing( bool edit
)
3040 // TODO: improve this ?
3042 if ( edit
!= m_editable
)
3046 // TODO: extend this for other edit control types
3048 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3050 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3056 #if 0 // disabled for the moment - the cell control is always active
3057 void wxGrid::EnableCellEditControl( bool enable
)
3059 if ( m_cellEditCtrl
&&
3060 enable
!= m_cellEditCtrlEnabled
)
3062 m_cellEditCtrlEnabled
= enable
;
3064 if ( m_cellEditCtrlEnabled
)
3066 SetEditControlValue();
3067 ShowCellEditControl();
3071 HideCellEditControl();
3072 SaveEditControlValue();
3079 void wxGrid::ShowCellEditControl()
3083 if ( IsCellEditControlEnabled() )
3085 if ( !IsVisible( m_currentCellCoords
) )
3091 rect
= CellToRect( m_currentCellCoords
);
3093 // convert to scrolled coords
3095 int left
, top
, right
, bottom
;
3096 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3097 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3100 m_gridWin
->GetClientSize( &cw
, &ch
);
3102 // Make the edit control large enough to allow for internal margins
3103 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3106 #if defined(__WXMOTIF__)
3107 if ( m_currentCellCoords
.GetRow() == 0 ||
3108 m_currentCellCoords
.GetCol() == 0 )
3117 if ( m_currentCellCoords
.GetRow() == 0 ||
3118 m_currentCellCoords
.GetCol() == 0 )
3128 #if defined(__WXGTK__)
3131 if (left
!= 0) left_diff
++;
3132 if (top
!= 0) top_diff
++;
3133 rect
.SetLeft( left
+ left_diff
);
3134 rect
.SetTop( top
+ top_diff
);
3135 rect
.SetRight( rect
.GetRight() - left_diff
);
3136 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3138 rect
.SetLeft( wxMax(0, left
- extra
) );
3139 rect
.SetTop( wxMax(0, top
- extra
) );
3140 rect
.SetRight( rect
.GetRight() + 2*extra
);
3141 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3144 m_cellEditCtrl
->SetSize( rect
);
3145 m_cellEditCtrl
->Show( TRUE
);
3147 switch ( m_editCtrlType
)
3149 case wxGRID_TEXTCTRL
:
3150 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3153 case wxGRID_CHECKBOX
:
3154 // TODO: anything ???
3159 // TODO: anything ???
3163 case wxGRID_COMBOBOX
:
3164 // TODO: anything ???
3169 m_cellEditCtrl
->SetFocus();
3175 void wxGrid::HideCellEditControl()
3177 if ( IsCellEditControlEnabled() )
3179 m_cellEditCtrl
->Show( FALSE
);
3184 void wxGrid::SetEditControlValue( const wxString
& value
)
3190 s
= GetCellValue(m_currentCellCoords
);
3194 if ( IsCellEditControlEnabled() )
3196 switch ( m_editCtrlType
)
3198 case wxGRID_TEXTCTRL
:
3199 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3202 case wxGRID_CHECKBOX
:
3203 // TODO: implement this
3208 // TODO: implement this
3212 case wxGRID_COMBOBOX
:
3213 // TODO: implement this
3222 void wxGrid::SaveEditControlValue()
3226 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3228 if ( IsCellEditControlEnabled() )
3230 ctrl
= m_cellEditCtrl
;
3237 bool valueChanged
= FALSE
;
3239 switch ( m_editCtrlType
)
3241 case wxGRID_TEXTCTRL
:
3242 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3243 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3244 SetCellValue( m_currentCellCoords
,
3245 ((wxTextCtrl
*) ctrl
)->GetValue() );
3248 case wxGRID_CHECKBOX
:
3249 // TODO: implement this
3254 // TODO: implement this
3258 case wxGRID_COMBOBOX
:
3259 // TODO: implement this
3266 SendEvent( EVT_GRID_CELL_CHANGE
,
3267 m_currentCellCoords
.GetRow(),
3268 m_currentCellCoords
.GetCol() );
3275 // ------ Grid location functions
3276 // Note that all of these functions work with the logical coordinates of
3277 // grid cells and labels so you will need to convert from device
3278 // coordinates for mouse events etc.
3281 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3283 int row
= YToRow(y
);
3284 int col
= XToCol(x
);
3286 if ( row
== -1 || col
== -1 )
3288 coords
= wxGridNoCellCoords
;
3292 coords
.Set( row
, col
);
3297 int wxGrid::YToRow( int y
)
3301 for ( i
= 0; i
< m_numRows
; i
++ )
3303 if ( y
< m_rowBottoms
[i
] ) return i
;
3310 int wxGrid::XToCol( int x
)
3314 for ( i
= 0; i
< m_numCols
; i
++ )
3316 if ( x
< m_colRights
[i
] ) return i
;
3323 // return the row number that that the y coord is near the edge of, or
3324 // -1 if not near an edge
3326 int wxGrid::YToEdgeOfRow( int y
)
3330 for ( i
= 0; i
< m_numRows
; i
++ )
3332 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3334 d
= abs( y
- m_rowBottoms
[i
] );
3336 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3345 // return the col number that that the x coord is near the edge of, or
3346 // -1 if not near an edge
3348 int wxGrid::XToEdgeOfCol( int x
)
3352 for ( i
= 0; i
< m_numCols
; i
++ )
3354 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3356 d
= abs( x
- m_colRights
[i
] );
3358 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3367 wxRect
wxGrid::CellToRect( int row
, int col
)
3369 wxRect
rect( -1, -1, -1, -1 );
3371 if ( row
>= 0 && row
< m_numRows
&&
3372 col
>= 0 && col
< m_numCols
)
3374 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3375 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3376 rect
.width
= m_colWidths
[col
];
3377 rect
.height
= m_rowHeights
[ row
];
3384 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3386 // get the cell rectangle in logical coords
3388 wxRect
r( CellToRect( row
, col
) );
3390 // convert to device coords
3392 int left
, top
, right
, bottom
;
3393 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3394 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3396 // check against the client area of the grid window
3399 m_gridWin
->GetClientSize( &cw
, &ch
);
3401 if ( wholeCellVisible
)
3403 // is the cell wholly visible ?
3405 return ( left
>= 0 && right
<= cw
&&
3406 top
>= 0 && bottom
<= ch
);
3410 // is the cell partly visible ?
3412 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3413 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3418 // make the specified cell location visible by doing a minimal amount
3421 void wxGrid::MakeCellVisible( int row
, int col
)
3424 int xpos
= -1, ypos
= -1;
3426 if ( row
>= 0 && row
< m_numRows
&&
3427 col
>= 0 && col
< m_numCols
)
3429 // get the cell rectangle in logical coords
3431 wxRect
r( CellToRect( row
, col
) );
3433 // convert to device coords
3435 int left
, top
, right
, bottom
;
3436 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3437 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3440 m_gridWin
->GetClientSize( &cw
, &ch
);
3446 else if ( bottom
> ch
)
3448 int h
= r
.GetHeight();
3450 for ( i
= row
-1; i
>= 0; i
-- )
3452 if ( h
+ m_rowHeights
[i
] > ch
) break;
3454 h
+= m_rowHeights
[i
];
3455 ypos
-= m_rowHeights
[i
];
3458 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3459 // have rounding errors (this is important, because if we do, we
3460 // might not scroll at all and some cells won't be redrawn)
3461 ypos
+= GRID_SCROLL_LINE
/ 2;
3468 else if ( right
> cw
)
3470 int w
= r
.GetWidth();
3472 for ( i
= col
-1; i
>= 0; i
-- )
3474 if ( w
+ m_colWidths
[i
] > cw
) break;
3476 w
+= m_colWidths
[i
];
3477 xpos
-= m_colWidths
[i
];
3480 // see comment for ypos above
3481 xpos
+= GRID_SCROLL_LINE
/ 2;
3484 if ( xpos
!= -1 || ypos
!= -1 )
3486 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3487 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3488 Scroll( xpos
, ypos
);
3496 // ------ Grid cursor movement functions
3499 bool wxGrid::MoveCursorUp()
3501 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3502 m_currentCellCoords
.GetRow() > 0 )
3504 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3505 m_currentCellCoords
.GetCol() );
3507 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3508 m_currentCellCoords
.GetCol() );
3517 bool wxGrid::MoveCursorDown()
3519 // TODO: allow for scrolling
3521 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3522 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3524 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3525 m_currentCellCoords
.GetCol() );
3527 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3528 m_currentCellCoords
.GetCol() );
3537 bool wxGrid::MoveCursorLeft()
3539 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3540 m_currentCellCoords
.GetCol() > 0 )
3542 MakeCellVisible( m_currentCellCoords
.GetRow(),
3543 m_currentCellCoords
.GetCol() - 1 );
3545 SetCurrentCell( m_currentCellCoords
.GetRow(),
3546 m_currentCellCoords
.GetCol() - 1 );
3555 bool wxGrid::MoveCursorRight()
3557 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3558 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3560 MakeCellVisible( m_currentCellCoords
.GetRow(),
3561 m_currentCellCoords
.GetCol() + 1 );
3563 SetCurrentCell( m_currentCellCoords
.GetRow(),
3564 m_currentCellCoords
.GetCol() + 1 );
3573 bool wxGrid::MovePageUp()
3575 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3577 int row
= m_currentCellCoords
.GetRow();
3581 m_gridWin
->GetClientSize( &cw
, &ch
);
3583 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3584 int newRow
= YToRow( y
- ch
+ 1 );
3589 else if ( newRow
== row
)
3594 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3595 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3603 bool wxGrid::MovePageDown()
3605 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3607 int row
= m_currentCellCoords
.GetRow();
3608 if ( row
< m_numRows
)
3611 m_gridWin
->GetClientSize( &cw
, &ch
);
3613 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3614 int newRow
= YToRow( y
+ ch
);
3617 newRow
= m_numRows
- 1;
3619 else if ( newRow
== row
)
3624 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3625 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3633 bool wxGrid::MoveCursorUpBlock()
3636 m_currentCellCoords
!= wxGridNoCellCoords
&&
3637 m_currentCellCoords
.GetRow() > 0 )
3639 int row
= m_currentCellCoords
.GetRow();
3640 int col
= m_currentCellCoords
.GetCol();
3642 if ( m_table
->IsEmptyCell(row
, col
) )
3644 // starting in an empty cell: find the next block of
3650 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3653 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3655 // starting at the top of a block: find the next block
3661 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3666 // starting within a block: find the top of the block
3671 if ( m_table
->IsEmptyCell(row
, col
) )
3679 MakeCellVisible( row
, col
);
3680 SetCurrentCell( row
, col
);
3688 bool wxGrid::MoveCursorDownBlock()
3691 m_currentCellCoords
!= wxGridNoCellCoords
&&
3692 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3694 int row
= m_currentCellCoords
.GetRow();
3695 int col
= m_currentCellCoords
.GetCol();
3697 if ( m_table
->IsEmptyCell(row
, col
) )
3699 // starting in an empty cell: find the next block of
3702 while ( row
< m_numRows
-1 )
3705 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3708 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3710 // starting at the bottom of a block: find the next block
3713 while ( row
< m_numRows
-1 )
3716 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3721 // starting within a block: find the bottom of the block
3723 while ( row
< m_numRows
-1 )
3726 if ( m_table
->IsEmptyCell(row
, col
) )
3734 MakeCellVisible( row
, col
);
3735 SetCurrentCell( row
, col
);
3743 bool wxGrid::MoveCursorLeftBlock()
3746 m_currentCellCoords
!= wxGridNoCellCoords
&&
3747 m_currentCellCoords
.GetCol() > 0 )
3749 int row
= m_currentCellCoords
.GetRow();
3750 int col
= m_currentCellCoords
.GetCol();
3752 if ( m_table
->IsEmptyCell(row
, col
) )
3754 // starting in an empty cell: find the next block of
3760 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3763 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3765 // starting at the left of a block: find the next block
3771 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3776 // starting within a block: find the left of the block
3781 if ( m_table
->IsEmptyCell(row
, col
) )
3789 MakeCellVisible( row
, col
);
3790 SetCurrentCell( row
, col
);
3798 bool wxGrid::MoveCursorRightBlock()
3801 m_currentCellCoords
!= wxGridNoCellCoords
&&
3802 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3804 int row
= m_currentCellCoords
.GetRow();
3805 int col
= m_currentCellCoords
.GetCol();
3807 if ( m_table
->IsEmptyCell(row
, col
) )
3809 // starting in an empty cell: find the next block of
3812 while ( col
< m_numCols
-1 )
3815 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3818 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3820 // starting at the right of a block: find the next block
3823 while ( col
< m_numCols
-1 )
3826 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3831 // starting within a block: find the right of the block
3833 while ( col
< m_numCols
-1 )
3836 if ( m_table
->IsEmptyCell(row
, col
) )
3844 MakeCellVisible( row
, col
);
3845 SetCurrentCell( row
, col
);
3856 // ------ Label values and formatting
3859 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3861 *horiz
= m_rowLabelHorizAlign
;
3862 *vert
= m_rowLabelVertAlign
;
3865 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3867 *horiz
= m_colLabelHorizAlign
;
3868 *vert
= m_colLabelVertAlign
;
3871 wxString
wxGrid::GetRowLabelValue( int row
)
3875 return m_table
->GetRowLabelValue( row
);
3885 wxString
wxGrid::GetColLabelValue( int col
)
3889 return m_table
->GetColLabelValue( col
);
3900 void wxGrid::SetRowLabelSize( int width
)
3904 width
= wxMax( width
, 0 );
3905 if ( width
!= m_rowLabelWidth
)
3907 // Hiding the row labels (and possible the corner label)
3911 m_rowLabelWin
->Show( FALSE
);
3913 // If the col labels are on display we need to hide the
3914 // corner label and remove it from the top sizer
3916 if ( m_colLabelHeight
> 0 )
3918 m_cornerLabelWin
->Show( FALSE
);
3919 m_topSizer
->Remove( m_cornerLabelWin
);
3922 m_middleSizer
->Remove( m_rowLabelWin
);
3926 // Displaying the row labels (and possibly the corner
3927 // label) after being hidden
3929 if ( m_rowLabelWidth
== 0 )
3931 m_rowLabelWin
->Show( TRUE
);
3933 if ( m_colLabelHeight
> 0 )
3935 m_cornerLabelWin
->Show( TRUE
);
3936 m_topSizer
->Prepend( m_cornerLabelWin
, 0 );
3939 m_middleSizer
->Prepend( m_rowLabelWin
, 0, wxEXPAND
);
3943 // set the width of the corner label if it is on display
3945 if ( m_colLabelHeight
> 0 )
3947 wxList
& childList
= m_topSizer
->GetChildren();
3948 wxNode
*node
= childList
.First();
3951 wxSizerItem
*item
= (wxSizerItem
*)node
->Data();
3952 if ( item
->GetWindow() == m_cornerLabelWin
)
3954 item
->SetInitSize( width
, m_colLabelHeight
);
3957 node
= node
->Next();
3961 // set the width of the row labels
3963 wxList
& childList
= m_middleSizer
->GetChildren();
3964 wxNode
*node
= childList
.First();
3967 wxSizerItem
*item
= (wxSizerItem
*)node
->Data();
3968 if ( item
->GetWindow() == m_rowLabelWin
)
3970 sz
= item
->GetWindow()->GetSize();
3971 item
->SetInitSize( width
, sz
.GetHeight() );
3974 node
= node
->Next();
3978 m_rowLabelWidth
= width
;
3979 m_mainSizer
->Layout();
3984 void wxGrid::SetColLabelSize( int height
)
3988 if ( height
< 0 ) height
= 0;
3989 if ( height
!= m_colLabelHeight
)
3991 // hiding the column labels
3995 m_cornerLabelWin
->Show( FALSE
);
3996 m_colLabelWin
->Show( FALSE
);
3998 // Note: this call will actually delete the sizer
4000 m_mainSizer
->Remove( m_topSizer
);
4001 m_topSizer
= (wxBoxSizer
*)NULL
;
4005 // column labels to be displayed after being hidden
4007 if ( m_colLabelHeight
== 0 )
4009 // recreate the top sizer
4011 m_topSizer
= new wxBoxSizer( wxHORIZONTAL
);
4013 if ( m_rowLabelWidth
> 0 )
4014 m_topSizer
->Add( m_cornerLabelWin
, 0 );
4016 m_topSizer
->Add( m_colLabelWin
, 1 );
4017 m_mainSizer
->Prepend( m_topSizer
, 0, wxEXPAND
);
4019 // only show the corner label if the row labels are
4022 if ( m_rowLabelWidth
> 0 )
4023 m_cornerLabelWin
->Show( TRUE
);
4025 m_colLabelWin
->Show( TRUE
);
4028 wxList
& childList
= m_topSizer
->GetChildren();
4029 wxNode
*node
= childList
.First();
4032 wxSizerItem
*item
= (wxSizerItem
*)node
->Data();
4034 if ( (item
->GetWindow() == m_cornerLabelWin
&& m_rowLabelWidth
> 0) ||
4035 item
->GetWindow() == m_colLabelWin
)
4037 sz
= item
->GetWindow()->GetSize();
4038 item
->SetInitSize( sz
.GetWidth(), height
);
4040 node
= node
->Next();
4044 m_colLabelHeight
= height
;
4045 m_mainSizer
->Layout();
4050 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4052 if ( m_labelBackgroundColour
!= colour
)
4054 m_labelBackgroundColour
= colour
;
4055 m_rowLabelWin
->SetBackgroundColour( colour
);
4056 m_colLabelWin
->SetBackgroundColour( colour
);
4057 m_cornerLabelWin
->SetBackgroundColour( colour
);
4059 if ( !GetBatchCount() )
4061 m_rowLabelWin
->Refresh();
4062 m_colLabelWin
->Refresh();
4063 m_cornerLabelWin
->Refresh();
4068 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4070 if ( m_labelTextColour
!= colour
)
4072 m_labelTextColour
= colour
;
4073 if ( !GetBatchCount() )
4075 m_rowLabelWin
->Refresh();
4076 m_colLabelWin
->Refresh();
4081 void wxGrid::SetLabelFont( const wxFont
& font
)
4084 if ( !GetBatchCount() )
4086 m_rowLabelWin
->Refresh();
4087 m_colLabelWin
->Refresh();
4091 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4093 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4095 m_rowLabelHorizAlign
= horiz
;
4098 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4100 m_rowLabelVertAlign
= vert
;
4103 if ( !GetBatchCount() )
4105 m_rowLabelWin
->Refresh();
4109 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4111 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4113 m_colLabelHorizAlign
= horiz
;
4116 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4118 m_colLabelVertAlign
= vert
;
4121 if ( !GetBatchCount() )
4123 m_colLabelWin
->Refresh();
4127 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4131 m_table
->SetRowLabelValue( row
, s
);
4132 if ( !GetBatchCount() )
4134 wxRect rect
= CellToRect( row
, 0);
4135 if ( rect
.height
> 0 )
4137 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4139 rect
.width
= m_rowLabelWidth
;
4140 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4146 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4150 m_table
->SetColLabelValue( col
, s
);
4151 if ( !GetBatchCount() )
4153 wxRect rect
= CellToRect( 0, col
);
4154 if ( rect
.width
> 0 )
4156 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4158 rect
.height
= m_colLabelHeight
;
4159 m_colLabelWin
->Refresh( TRUE
, &rect
);
4165 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4167 if ( m_gridLineColour
!= colour
)
4169 m_gridLineColour
= colour
;
4171 wxClientDC
dc( m_gridWin
);
4173 DrawAllGridLines( dc
, wxRegion() );
4177 void wxGrid::EnableGridLines( bool enable
)
4179 if ( enable
!= m_gridLinesEnabled
)
4181 m_gridLinesEnabled
= enable
;
4183 if ( !GetBatchCount() )
4187 wxClientDC
dc( m_gridWin
);
4189 DrawAllGridLines( dc
, wxRegion() );
4193 m_gridWin
->Refresh();
4200 int wxGrid::GetDefaultRowSize()
4202 return m_defaultRowHeight
;
4205 int wxGrid::GetRowSize( int row
)
4207 if ( row
>= 0 && row
< m_numRows
)
4208 return m_rowHeights
[row
];
4210 return 0; // TODO: log an error here
4213 int wxGrid::GetDefaultColSize()
4215 return m_defaultColWidth
;
4218 int wxGrid::GetColSize( int col
)
4220 if ( col
>= 0 && col
< m_numCols
)
4221 return m_colWidths
[col
];
4223 return 0; // TODO: log an error here
4226 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4228 // TODO: replace this temp test code
4230 return wxColour( 255, 255, 255 );
4233 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4235 // TODO: replace this temp test code
4237 return wxColour( 255, 255, 255 );
4240 wxColour
wxGrid::GetDefaultCellTextColour()
4242 // TODO: replace this temp test code
4244 return wxColour( 0, 0, 0 );
4247 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4249 // TODO: replace this temp test code
4251 return wxColour( 0, 0, 0 );
4255 wxFont
wxGrid::GetDefaultCellFont()
4257 return m_defaultCellFont
;
4260 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
4262 // TODO: replace this temp test code
4264 return m_defaultCellFont
;
4267 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4269 // TODO: replace this temp test code
4275 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
4277 // TODO: replace this temp test code
4283 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4285 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4287 if ( resizeExistingRows
)
4291 for ( row
= 0; row
< m_numRows
; row
++ )
4293 m_rowHeights
[row
] = m_defaultRowHeight
;
4294 bottom
+= m_defaultRowHeight
;
4295 m_rowBottoms
[row
] = bottom
;
4301 void wxGrid::SetRowSize( int row
, int height
)
4305 if ( row
>= 0 && row
< m_numRows
)
4307 int h
= wxMax( 0, height
);
4308 int diff
= h
- m_rowHeights
[row
];
4310 m_rowHeights
[row
] = h
;
4311 for ( i
= row
; i
< m_numRows
; i
++ )
4313 m_rowBottoms
[i
] += diff
;
4317 // Note: we are ending the event *after* doing
4318 // default processing in this case
4320 SendEvent( EVT_GRID_ROW_SIZE
,
4325 // TODO: log an error here
4329 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4331 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4333 if ( resizeExistingCols
)
4337 for ( col
= 0; col
< m_numCols
; col
++ )
4339 m_colWidths
[col
] = m_defaultColWidth
;
4340 right
+= m_defaultColWidth
;
4341 m_colRights
[col
] = right
;
4347 void wxGrid::SetColSize( int col
, int width
)
4351 if ( col
>= 0 && col
< m_numCols
)
4353 int w
= wxMax( 0, width
);
4354 int diff
= w
- m_colWidths
[col
];
4355 m_colWidths
[col
] = w
;
4357 for ( i
= col
; i
< m_numCols
; i
++ )
4359 m_colRights
[i
] += diff
;
4363 // Note: we are ending the event *after* doing
4364 // default processing in this case
4366 SendEvent( EVT_GRID_COL_SIZE
,
4371 // TODO: log an error here
4375 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4377 // TODO: everything !!!
4381 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4383 // TODO: everything !!!
4387 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4389 // TODO: everything !!!
4393 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4395 // TODO: everything !!!
4399 void wxGrid::SetDefaultCellFont( const wxFont
& )
4401 // TODO: everything !!!
4405 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4407 // TODO: everything !!!
4411 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4413 // TODO: everything !!!
4417 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4419 // TODO: everything !!!
4426 // ------ cell value accessor functions
4429 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4433 m_table
->SetValue( row
, col
, s
.c_str() );
4434 if ( !GetBatchCount() )
4436 wxClientDC
dc( m_gridWin
);
4438 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4441 #if 0 // TODO: edit in place
4443 if ( m_currentCellCoords
.GetRow() == row
&&
4444 m_currentCellCoords
.GetCol() == col
)
4446 SetEditControlValue( s
);
4455 // ------ Block, row and col selection
4458 void wxGrid::SelectRow( int row
, bool addToSelected
)
4462 if ( IsSelection() && addToSelected
)
4465 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4468 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4469 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4470 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4471 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4475 need_refresh
[0] = TRUE
;
4476 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4477 wxGridCellCoords ( oldTop
- 1,
4479 m_selectedTopLeft
.SetRow( row
);
4484 need_refresh
[1] = TRUE
;
4485 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4486 wxGridCellCoords ( oldBottom
,
4489 m_selectedTopLeft
.SetCol( 0 );
4492 if ( oldBottom
< row
)
4494 need_refresh
[2] = TRUE
;
4495 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4496 wxGridCellCoords ( row
,
4498 m_selectedBottomRight
.SetRow( row
);
4501 if ( oldRight
< m_numCols
- 1 )
4503 need_refresh
[3] = TRUE
;
4504 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4506 wxGridCellCoords ( oldBottom
,
4508 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4511 for (i
= 0; i
< 4; i
++ )
4512 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4513 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4517 r
= SelectionToDeviceRect();
4519 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4521 m_selectedTopLeft
.Set( row
, 0 );
4522 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4523 r
= SelectionToDeviceRect();
4524 m_gridWin
->Refresh( FALSE
, &r
);
4527 wxGridRangeSelectEvent
gridEvt( GetId(),
4528 EVT_GRID_RANGE_SELECT
,
4531 m_selectedBottomRight
);
4533 GetEventHandler()->ProcessEvent(gridEvt
);
4537 void wxGrid::SelectCol( int col
, bool addToSelected
)
4539 if ( IsSelection() && addToSelected
)
4542 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4545 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4546 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4547 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4548 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4550 if ( oldLeft
> col
)
4552 need_refresh
[0] = TRUE
;
4553 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4554 wxGridCellCoords ( m_numRows
- 1,
4556 m_selectedTopLeft
.SetCol( col
);
4561 need_refresh
[1] = TRUE
;
4562 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4563 wxGridCellCoords ( oldTop
- 1,
4565 m_selectedTopLeft
.SetRow( 0 );
4568 if ( oldRight
< col
)
4570 need_refresh
[2] = TRUE
;
4571 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4572 wxGridCellCoords ( m_numRows
- 1,
4574 m_selectedBottomRight
.SetCol( col
);
4577 if ( oldBottom
< m_numRows
- 1 )
4579 need_refresh
[3] = TRUE
;
4580 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4582 wxGridCellCoords ( m_numRows
- 1,
4584 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4587 for (i
= 0; i
< 4; i
++ )
4588 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4589 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4595 r
= SelectionToDeviceRect();
4597 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4599 m_selectedTopLeft
.Set( 0, col
);
4600 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4601 r
= SelectionToDeviceRect();
4602 m_gridWin
->Refresh( FALSE
, &r
);
4605 wxGridRangeSelectEvent
gridEvt( GetId(),
4606 EVT_GRID_RANGE_SELECT
,
4609 m_selectedBottomRight
);
4611 GetEventHandler()->ProcessEvent(gridEvt
);
4615 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4618 wxGridCellCoords updateTopLeft
, updateBottomRight
;
4620 if ( topRow
> bottomRow
)
4627 if ( leftCol
> rightCol
)
4634 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
4635 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
4637 if ( m_selectedTopLeft
!= updateTopLeft
||
4638 m_selectedBottomRight
!= updateBottomRight
)
4640 // Compute two optimal update rectangles:
4641 // Either one rectangle is a real subset of the
4642 // other, or they are (almost) disjoint!
4644 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4647 // Store intermediate values
4648 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4649 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4650 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4651 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4653 // Determine the outer/inner coordinates.
4654 if (oldLeft
> leftCol
)
4660 if (oldTop
> topRow
)
4666 if (oldRight
< rightCol
)
4669 oldRight
= rightCol
;
4672 if (oldBottom
< bottomRow
)
4675 oldBottom
= bottomRow
;
4679 // Now, either the stuff marked old is the outer
4680 // rectangle or we don't have a situation where one
4681 // is contained in the other.
4683 if ( oldLeft
< leftCol
)
4685 need_refresh
[0] = TRUE
;
4686 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4688 wxGridCellCoords ( oldBottom
,
4692 if ( oldTop
< topRow
)
4694 need_refresh
[1] = TRUE
;
4695 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4697 wxGridCellCoords ( topRow
- 1,
4701 if ( oldRight
> rightCol
)
4703 need_refresh
[2] = TRUE
;
4704 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4706 wxGridCellCoords ( oldBottom
,
4710 if ( oldBottom
> bottomRow
)
4712 need_refresh
[3] = TRUE
;
4713 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
4715 wxGridCellCoords ( oldBottom
,
4721 m_selectedTopLeft
= updateTopLeft
;
4722 m_selectedBottomRight
= updateBottomRight
;
4724 // various Refresh() calls
4725 for (i
= 0; i
< 4; i
++ )
4726 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4727 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4730 // only generate an event if the block is not being selected by
4731 // dragging the mouse (in which case the event will be generated in
4732 // the mouse event handler)
4733 if ( !m_isDragging
)
4735 wxGridRangeSelectEvent
gridEvt( GetId(),
4736 EVT_GRID_RANGE_SELECT
,
4739 m_selectedBottomRight
);
4741 GetEventHandler()->ProcessEvent(gridEvt
);
4745 void wxGrid::SelectAll()
4747 m_selectedTopLeft
.Set( 0, 0 );
4748 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4750 m_gridWin
->Refresh();
4754 void wxGrid::ClearSelection()
4756 m_selectedTopLeft
= wxGridNoCellCoords
;
4757 m_selectedBottomRight
= wxGridNoCellCoords
;
4761 // This function returns the rectangle that encloses the given block
4762 // in device coords clipped to the client size of the grid window.
4764 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
4765 const wxGridCellCoords
&bottomRight
)
4767 wxRect
rect( wxGridNoCellRect
);
4770 cellRect
= CellToRect( topLeft
);
4771 if ( cellRect
!= wxGridNoCellRect
)
4777 rect
= wxRect( 0, 0, 0, 0 );
4780 cellRect
= CellToRect( bottomRight
);
4781 if ( cellRect
!= wxGridNoCellRect
)
4787 return wxGridNoCellRect
;
4790 // convert to scrolled coords
4792 int left
, top
, right
, bottom
;
4793 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4794 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4797 m_gridWin
->GetClientSize( &cw
, &ch
);
4799 rect
.SetLeft( wxMax(0, left
) );
4800 rect
.SetTop( wxMax(0, top
) );
4801 rect
.SetRight( wxMin(cw
, right
) );
4802 rect
.SetBottom( wxMin(ch
, bottom
) );
4810 // ------ Grid event classes
4813 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4815 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4816 int row
, int col
, int x
, int y
,
4817 bool control
, bool shift
, bool alt
, bool meta
)
4818 : wxNotifyEvent( type
, id
)
4824 m_control
= control
;
4829 SetEventObject(obj
);
4833 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4835 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4836 int rowOrCol
, int x
, int y
,
4837 bool control
, bool shift
, bool alt
, bool meta
)
4838 : wxNotifyEvent( type
, id
)
4840 m_rowOrCol
= rowOrCol
;
4843 m_control
= control
;
4848 SetEventObject(obj
);
4852 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4854 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4855 const wxGridCellCoords
& topLeft
,
4856 const wxGridCellCoords
& bottomRight
,
4857 bool control
, bool shift
, bool alt
, bool meta
)
4858 : wxNotifyEvent( type
, id
)
4860 m_topLeft
= topLeft
;
4861 m_bottomRight
= bottomRight
;
4862 m_control
= control
;
4867 SetEventObject(obj
);
4871 #endif // ifndef wxUSE_NEW_GRID