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"
35 #include "wx/layout.h"
38 #include "wx/generic/grid.h"
40 #ifndef WXGRID_DRAW_LINES
41 #define WXGRID_DRAW_LINES 1
44 //////////////////////////////////////////////////////////////////////
46 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
47 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
49 // this is a magic incantation which must be done!
50 #include "wx/arrimpl.cpp"
52 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
55 // TODO: fixed so far - make configurable later (and also different for x/y)
56 static const size_t GRID_SCROLL_LINE
= 10;
58 //////////////////////////////////////////////////////////////////////
60 // Abstract base class for grid data (the model)
62 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
65 wxGridTableBase::wxGridTableBase()
68 m_view
= (wxGrid
*) NULL
;
71 wxGridTableBase::~wxGridTableBase()
76 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
78 wxLogWarning( wxT("Called grid table class function InsertRows(pos=%d, N=%d)\n"
79 "but your derived table class does not override this function"),
85 bool wxGridTableBase::AppendRows( size_t numRows
)
87 wxLogWarning( wxT("Called grid table class function AppendRows(N=%d)\n"
88 "but your derived table class does not override this function"),
94 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
96 wxLogWarning( wxT("Called grid table class function DeleteRows(pos=%d, N=%d)\n"
97 "but your derived table class does not override this function"),
103 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
105 wxLogWarning( wxT("Called grid table class function InsertCols(pos=%d, N=%d)\n"
106 "but your derived table class does not override this function"),
112 bool wxGridTableBase::AppendCols( size_t numCols
)
114 wxLogWarning( wxT("Called grid table class function AppendCols(N=%d)\n"
115 "but your derived table class does not override this function"),
121 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
123 wxLogWarning( wxT("Called grid table class function DeleteCols(pos=%d, N=%d)\n"
124 "but your derived table class does not override this function"),
131 wxString
wxGridTableBase::GetRowLabelValue( int row
)
138 wxString
wxGridTableBase::GetColLabelValue( int col
)
140 // default col labels are:
141 // cols 0 to 25 : A-Z
142 // cols 26 to 675 : AA-ZZ
149 s
+= (_T('A') + (wxChar
)( col%26
));
151 if ( col
< 0 ) break;
154 // reverse the string...
156 for ( i
= 0; i
< n
; i
++ )
166 //////////////////////////////////////////////////////////////////////
168 // Message class for the grid table to send requests and notifications
172 wxGridTableMessage::wxGridTableMessage()
174 m_table
= (wxGridTableBase
*) NULL
;
180 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
181 int commandInt1
, int commandInt2
)
185 m_comInt1
= commandInt1
;
186 m_comInt2
= commandInt2
;
191 //////////////////////////////////////////////////////////////////////
193 // A basic grid table for string data. An object of this class will
194 // created by wxGrid if you don't specify an alternative table class.
197 WX_DEFINE_OBJARRAY(wxGridStringArray
)
199 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
201 wxGridStringTable::wxGridStringTable()
206 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
211 m_data
.Alloc( numRows
);
215 for ( col
= 0; col
< numCols
; col
++ )
217 sa
.Add( wxEmptyString
);
220 for ( row
= 0; row
< numRows
; row
++ )
226 wxGridStringTable::~wxGridStringTable()
230 long wxGridStringTable::GetNumberRows()
232 return m_data
.GetCount();
235 long wxGridStringTable::GetNumberCols()
237 if ( m_data
.GetCount() > 0 )
238 return m_data
[0].GetCount();
243 wxString
wxGridStringTable::GetValue( int row
, int col
)
245 // TODO: bounds checking
247 return m_data
[row
][col
];
250 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
252 // TODO: bounds checking
254 m_data
[row
][col
] = s
;
257 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
259 // TODO: bounds checking
261 return (m_data
[row
][col
] == wxEmptyString
);
265 void wxGridStringTable::Clear()
268 int numRows
, numCols
;
270 numRows
= m_data
.GetCount();
273 numCols
= m_data
[0].GetCount();
275 for ( row
= 0; row
< numRows
; row
++ )
277 for ( col
= 0; col
< numCols
; col
++ )
279 m_data
[row
][col
] = wxEmptyString
;
286 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
290 size_t curNumRows
= m_data
.GetCount();
291 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
293 if ( pos
>= curNumRows
)
295 return AppendRows( numRows
);
299 sa
.Alloc( curNumCols
);
300 for ( col
= 0; col
< curNumCols
; col
++ )
302 sa
.Add( wxEmptyString
);
305 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
307 m_data
.Insert( sa
, row
);
312 wxGridTableMessage
msg( this,
313 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
317 GetView()->ProcessTableMessage( msg
);
323 bool wxGridStringTable::AppendRows( size_t numRows
)
327 size_t curNumRows
= m_data
.GetCount();
328 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
331 if ( curNumCols
> 0 )
333 sa
.Alloc( curNumCols
);
334 for ( col
= 0; col
< curNumCols
; col
++ )
336 sa
.Add( wxEmptyString
);
340 for ( row
= 0; row
< numRows
; row
++ )
347 wxGridTableMessage
msg( this,
348 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
351 GetView()->ProcessTableMessage( msg
);
357 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
361 size_t curNumRows
= m_data
.GetCount();
363 if ( pos
>= curNumRows
)
365 wxLogError( wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)...\n"
366 "Pos value is invalid for present table with %d rows"),
367 pos
, numRows
, curNumRows
);
371 if ( numRows
> curNumRows
- pos
)
373 numRows
= curNumRows
- pos
;
376 if ( numRows
>= curNumRows
)
378 m_data
.Empty(); // don't release memory just yet
382 for ( n
= 0; n
< numRows
; n
++ )
384 m_data
.Remove( pos
);
390 wxGridTableMessage
msg( this,
391 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
395 GetView()->ProcessTableMessage( msg
);
401 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
405 size_t curNumRows
= m_data
.GetCount();
406 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
408 if ( pos
>= curNumCols
)
410 return AppendCols( numCols
);
413 for ( row
= 0; row
< curNumRows
; row
++ )
415 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
417 m_data
[row
].Insert( wxEmptyString
, col
);
423 wxGridTableMessage
msg( this,
424 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
428 GetView()->ProcessTableMessage( msg
);
434 bool wxGridStringTable::AppendCols( size_t numCols
)
438 size_t curNumRows
= m_data
.GetCount();
441 // TODO: something better than this ?
443 wxLogError( wxT("Unable to append cols to a grid table with no rows.\n"
444 "Call AppendRows() first") );
448 for ( row
= 0; row
< curNumRows
; row
++ )
450 for ( n
= 0; n
< numCols
; n
++ )
452 m_data
[row
].Add( wxEmptyString
);
458 wxGridTableMessage
msg( this,
459 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
462 GetView()->ProcessTableMessage( msg
);
468 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
472 size_t curNumRows
= m_data
.GetCount();
473 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
475 if ( pos
>= curNumCols
)
477 wxLogError( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
478 "Pos value is invalid for present table with %d cols"),
479 pos
, numCols
, curNumCols
);
483 if ( numCols
> curNumCols
- pos
)
485 numCols
= curNumCols
- pos
;
488 for ( row
= 0; row
< curNumRows
; row
++ )
490 if ( numCols
>= curNumCols
)
496 for ( n
= 0; n
< numCols
; n
++ )
498 m_data
[row
].Remove( pos
);
505 wxGridTableMessage
msg( this,
506 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
510 GetView()->ProcessTableMessage( msg
);
516 wxString
wxGridStringTable::GetRowLabelValue( int row
)
518 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
520 // using default label
522 return wxGridTableBase::GetRowLabelValue( row
);
526 return m_rowLabels
[ row
];
530 wxString
wxGridStringTable::GetColLabelValue( int col
)
532 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
534 // using default label
536 return wxGridTableBase::GetColLabelValue( col
);
540 return m_colLabels
[ col
];
544 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
546 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
548 int n
= m_rowLabels
.GetCount();
550 for ( i
= n
; i
<= row
; i
++ )
552 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
556 m_rowLabels
[row
] = value
;
559 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
561 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
563 int n
= m_colLabels
.GetCount();
565 for ( i
= n
; i
<= col
; i
++ )
567 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
571 m_colLabels
[col
] = value
;
577 //////////////////////////////////////////////////////////////////////
579 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
581 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
582 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
586 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
590 const wxString
& value
,
594 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
597 m_isCellControl
= isCellControl
;
601 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
603 switch ( event
.KeyCode() )
606 m_grid
->SetEditControlValue( startValue
);
607 SetInsertionPointEnd();
617 if ( m_isCellControl
)
619 // send the event to the parent grid, skipping the
620 // event if nothing happens
622 event
.Skip( m_grid
->ProcessEvent( event
) );
626 // default text control response within the top edit
634 if ( m_isCellControl
)
636 if ( !m_grid
->ProcessEvent( event
) )
638 #if defined(__WXMOTIF__) || defined(__WXGTK__)
639 // wxMotif needs a little extra help...
641 int pos
= GetInsertionPoint();
642 wxString
s( GetValue() );
643 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
645 SetInsertionPoint( pos
);
647 // the other ports can handle a Return key press
657 if ( m_isCellControl
)
659 // send the event to the parent grid, skipping the
660 // event if nothing happens
662 event
.Skip( m_grid
->ProcessEvent( event
) );
666 // default text control response within the top edit
678 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
681 wxTextCtrl::SetValue(s
);
686 //////////////////////////////////////////////////////////////////////
688 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
690 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
691 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
692 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
693 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
696 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
698 const wxPoint
&pos
, const wxSize
&size
)
699 : wxWindow( parent
, id
, pos
, size
)
704 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
708 // NO - don't do this because it will set both the x and y origin
709 // coords to match the parent scrolled window and we just want to
710 // set the y coord - MB
712 // m_owner->PrepareDC( dc );
715 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
716 dc
.SetDeviceOrigin( 0, -y
);
718 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
719 m_owner
->DrawRowLabels( dc
);
723 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
725 m_owner
->ProcessRowLabelMouseEvent( event
);
729 // This seems to be required for wxMotif otherwise the mouse
730 // cursor must be in the cell edit control to get key events
732 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
734 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
739 //////////////////////////////////////////////////////////////////////
741 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
743 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
744 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
745 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
746 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
749 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
751 const wxPoint
&pos
, const wxSize
&size
)
752 : wxWindow( parent
, id
, pos
, size
)
757 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
761 // NO - don't do this because it will set both the x and y origin
762 // coords to match the parent scrolled window and we just want to
763 // set the x coord - MB
765 // m_owner->PrepareDC( dc );
768 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
769 dc
.SetDeviceOrigin( -x
, 0 );
771 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
772 m_owner
->DrawColLabels( dc
);
776 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
778 m_owner
->ProcessColLabelMouseEvent( event
);
782 // This seems to be required for wxMotif otherwise the mouse
783 // cursor must be in the cell edit control to get key events
785 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
787 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
792 //////////////////////////////////////////////////////////////////////
794 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
796 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
797 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
798 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
799 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
802 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
804 const wxPoint
&pos
, const wxSize
&size
)
805 : wxWindow( parent
, id
, pos
, size
)
810 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
814 int client_height
= 0;
815 int client_width
= 0;
816 GetClientSize( &client_width
, &client_height
);
818 dc
.SetPen( *wxBLACK_PEN
);
819 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
820 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
822 dc
.SetPen( *wxWHITE_PEN
);
823 dc
.DrawLine( 0, 0, client_width
, 0 );
824 dc
.DrawLine( 0, 0, 0, client_height
);
828 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
830 m_owner
->ProcessCornerLabelMouseEvent( event
);
834 // This seems to be required for wxMotif otherwise the mouse
835 // cursor must be in the cell edit control to get key events
837 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
839 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
844 //////////////////////////////////////////////////////////////////////
846 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
848 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
849 EVT_PAINT( wxGridWindow::OnPaint
)
850 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
851 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
854 wxGridWindow::wxGridWindow( wxGrid
*parent
,
855 wxGridRowLabelWindow
*rowLblWin
,
856 wxGridColLabelWindow
*colLblWin
,
857 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
858 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
861 m_rowLabelWin
= rowLblWin
;
862 m_colLabelWin
= colLblWin
;
864 SetBackgroundColour( "WHITE" );
868 wxGridWindow::~wxGridWindow()
873 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
875 wxPaintDC
dc( this );
876 m_owner
->PrepareDC( dc
);
877 wxRegion reg
= GetUpdateRegion();
878 m_owner
->CalcCellsExposed( reg
);
879 m_owner
->DrawGridCellArea( dc
);
880 #if WXGRID_DRAW_LINES
881 m_owner
->DrawAllGridLines( dc
, reg
);
886 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
888 wxPanel::ScrollWindow( dx
, dy
, rect
);
889 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
890 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
894 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
896 m_owner
->ProcessGridCellMouseEvent( event
);
900 // This seems to be required for wxMotif otherwise the mouse
901 // cursor must be in the cell edit control to get key events
903 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
905 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
910 //////////////////////////////////////////////////////////////////////
912 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
914 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
915 EVT_PAINT( wxGrid::OnPaint
)
916 EVT_SIZE( wxGrid::OnSize
)
917 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
920 wxGrid::wxGrid( wxWindow
*parent
,
925 const wxString
& name
)
926 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
939 // ----- internal init and update functions
942 void wxGrid::Create()
944 m_created
= FALSE
; // set to TRUE by CreateGrid
945 m_displayed
= FALSE
; // set to TRUE by OnPaint
947 m_table
= (wxGridTableBase
*) NULL
;
948 m_cellEditCtrl
= (wxWindow
*) NULL
;
952 m_currentCellCoords
= wxGridNoCellCoords
;
954 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
955 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
957 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
962 m_rowLabelWin
= new wxGridRowLabelWindow( this,
967 m_colLabelWin
= new wxGridColLabelWindow( this,
972 m_gridWin
= new wxGridWindow( this,
979 SetTargetWindow( m_gridWin
);
983 bool wxGrid::CreateGrid( int numRows
, int numCols
)
987 wxLogError( wxT("wxGrid::CreateGrid(numRows, numCols) called more than once") );
995 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
996 m_table
->SetView( this );
1009 if ( m_numRows
<= 0 )
1010 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1012 if ( m_numCols
<= 0 )
1013 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1015 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1016 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1018 if ( m_rowLabelWin
)
1020 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1024 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1027 m_labelTextColour
= wxColour( _T("BLACK") );
1029 // TODO: something better than this ?
1031 m_labelFont
= this->GetFont();
1032 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1034 m_rowLabelHorizAlign
= wxLEFT
;
1035 m_rowLabelVertAlign
= wxCENTRE
;
1037 m_colLabelHorizAlign
= wxCENTRE
;
1038 m_colLabelVertAlign
= wxTOP
;
1040 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1041 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1043 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1044 m_defaultRowHeight
+= 8;
1046 m_defaultRowHeight
+= 4;
1049 m_rowHeights
.Alloc( m_numRows
);
1050 m_rowBottoms
.Alloc( m_numRows
);
1052 for ( i
= 0; i
< m_numRows
; i
++ )
1054 m_rowHeights
.Add( m_defaultRowHeight
);
1055 rowBottom
+= m_defaultRowHeight
;
1056 m_rowBottoms
.Add( rowBottom
);
1059 m_colWidths
.Alloc( m_numCols
);
1060 m_colRights
.Alloc( m_numCols
);
1062 for ( i
= 0; i
< m_numCols
; i
++ )
1064 m_colWidths
.Add( m_defaultColWidth
);
1065 colRight
+= m_defaultColWidth
;
1066 m_colRights
.Add( colRight
);
1069 // TODO: improve this ?
1071 m_defaultCellFont
= this->GetFont();
1073 m_gridLineColour
= wxColour( 128, 128, 255 );
1074 m_gridLinesEnabled
= TRUE
;
1076 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1078 m_dragRowOrCol
= -1;
1079 m_isDragging
= FALSE
;
1081 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1082 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1084 m_currentCellCoords
= wxGridNoCellCoords
;
1086 m_selectedTopLeft
= wxGridNoCellCoords
;
1087 m_selectedBottomRight
= wxGridNoCellCoords
;
1089 m_editable
= TRUE
; // default for whole grid
1091 m_inOnKeyDown
= FALSE
;
1094 // TODO: extend this to other types of controls
1096 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1103 #if defined(__WXMSW__)
1104 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1108 m_cellEditCtrl
->Show( FALSE
);
1109 m_cellEditCtrlEnabled
= TRUE
;
1110 m_editCtrlType
= wxGRID_TEXTCTRL
;
1114 void wxGrid::CalcDimensions()
1117 GetClientSize( &cw
, &ch
);
1119 if ( m_numRows
> 0 && m_numCols
> 0 )
1121 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1122 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1124 // TODO: restore the scroll position that we had before sizing
1127 GetViewStart( &x
, &y
);
1128 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1129 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1135 void wxGrid::CalcWindowSizes()
1138 GetClientSize( &cw
, &ch
);
1140 if ( m_cornerLabelWin
->IsShown() )
1141 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1143 if ( m_colLabelWin
->IsShown() )
1144 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1146 if ( m_rowLabelWin
->IsShown() )
1147 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1149 if ( m_gridWin
->IsShown() )
1150 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1154 // this is called when the grid table sends a message to say that it
1155 // has been redimensioned
1157 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1161 switch ( msg
.GetId() )
1163 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1165 size_t pos
= msg
.GetCommandInt();
1166 int numRows
= msg
.GetCommandInt2();
1167 for ( i
= 0; i
< numRows
; i
++ )
1169 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1170 m_rowBottoms
.Insert( 0, pos
);
1172 m_numRows
+= numRows
;
1175 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1177 for ( i
= pos
; i
< m_numRows
; i
++ )
1179 bottom
+= m_rowHeights
[i
];
1180 m_rowBottoms
[i
] = bottom
;
1186 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1188 int numRows
= msg
.GetCommandInt();
1189 for ( i
= 0; i
< numRows
; i
++ )
1191 m_rowHeights
.Add( m_defaultRowHeight
);
1192 m_rowBottoms
.Add( 0 );
1195 int oldNumRows
= m_numRows
;
1196 m_numRows
+= numRows
;
1199 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1201 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1203 bottom
+= m_rowHeights
[i
];
1204 m_rowBottoms
[i
] = bottom
;
1210 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1212 size_t pos
= msg
.GetCommandInt();
1213 int numRows
= msg
.GetCommandInt2();
1214 for ( i
= 0; i
< numRows
; i
++ )
1216 m_rowHeights
.Remove( pos
);
1217 m_rowBottoms
.Remove( pos
);
1219 m_numRows
-= numRows
;
1224 m_colWidths
.Clear();
1225 m_colRights
.Clear();
1226 m_currentCellCoords
= wxGridNoCellCoords
;
1230 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1231 m_currentCellCoords
.Set( 0, 0 );
1234 for ( i
= 0; i
< m_numRows
; i
++ )
1236 h
+= m_rowHeights
[i
];
1237 m_rowBottoms
[i
] = h
;
1245 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1247 size_t pos
= msg
.GetCommandInt();
1248 int numCols
= msg
.GetCommandInt2();
1249 for ( i
= 0; i
< numCols
; i
++ )
1251 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1252 m_colRights
.Insert( 0, pos
);
1254 m_numCols
+= numCols
;
1257 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1259 for ( i
= pos
; i
< m_numCols
; i
++ )
1261 right
+= m_colWidths
[i
];
1262 m_colRights
[i
] = right
;
1268 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1270 int numCols
= msg
.GetCommandInt();
1271 for ( i
= 0; i
< numCols
; i
++ )
1273 m_colWidths
.Add( m_defaultColWidth
);
1274 m_colRights
.Add( 0 );
1277 int oldNumCols
= m_numCols
;
1278 m_numCols
+= numCols
;
1281 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1283 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1285 right
+= m_colWidths
[i
];
1286 m_colRights
[i
] = right
;
1292 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1294 size_t pos
= msg
.GetCommandInt();
1295 int numCols
= msg
.GetCommandInt2();
1296 for ( i
= 0; i
< numCols
; i
++ )
1298 m_colWidths
.Remove( pos
);
1299 m_colRights
.Remove( pos
);
1301 m_numCols
-= numCols
;
1305 #if 0 // leave the row alone here so that AppendCols will work subsequently
1307 m_rowHeights
.Clear();
1308 m_rowBottoms
.Clear();
1310 m_currentCellCoords
= wxGridNoCellCoords
;
1314 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1315 m_currentCellCoords
.Set( 0, 0 );
1318 for ( i
= 0; i
< m_numCols
; i
++ )
1320 w
+= m_colWidths
[i
];
1333 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1335 wxRegionIterator
iter( reg
);
1338 m_rowLabelsExposed
.Empty();
1345 // TODO: remove this when we can...
1346 // There is a bug in wxMotif that gives garbage update
1347 // rectangles if you jump-scroll a long way by clicking the
1348 // scrollbar with middle button. This is a work-around
1350 #if defined(__WXMOTIF__)
1352 m_gridWin
->GetClientSize( &cw
, &ch
);
1353 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1354 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1357 // logical bounds of update region
1360 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1361 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1363 // find the row labels within these bounds
1367 for ( row
= 0; row
< m_numRows
; row
++ )
1369 if ( m_rowBottoms
[row
] < top
) continue;
1371 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1372 if ( rowTop
> bottom
) break;
1374 m_rowLabelsExposed
.Add( row
);
1382 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1384 wxRegionIterator
iter( reg
);
1387 m_colLabelsExposed
.Empty();
1394 // TODO: remove this when we can...
1395 // There is a bug in wxMotif that gives garbage update
1396 // rectangles if you jump-scroll a long way by clicking the
1397 // scrollbar with middle button. This is a work-around
1399 #if defined(__WXMOTIF__)
1401 m_gridWin
->GetClientSize( &cw
, &ch
);
1402 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1403 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1406 // logical bounds of update region
1409 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1410 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1412 // find the cells within these bounds
1416 for ( col
= 0; col
< m_numCols
; col
++ )
1418 if ( m_colRights
[col
] < left
) continue;
1420 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1421 if ( colLeft
> right
) break;
1423 m_colLabelsExposed
.Add( col
);
1431 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1433 wxRegionIterator
iter( reg
);
1436 m_cellsExposed
.Empty();
1437 m_rowsExposed
.Empty();
1438 m_colsExposed
.Empty();
1440 int left
, top
, right
, bottom
;
1445 // TODO: remove this when we can...
1446 // There is a bug in wxMotif that gives garbage update
1447 // rectangles if you jump-scroll a long way by clicking the
1448 // scrollbar with middle button. This is a work-around
1450 #if defined(__WXMOTIF__)
1452 m_gridWin
->GetClientSize( &cw
, &ch
);
1453 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1454 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1455 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1456 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1459 // logical bounds of update region
1461 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1462 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1464 // find the cells within these bounds
1467 int colLeft
, rowTop
;
1468 for ( row
= 0; row
< m_numRows
; row
++ )
1470 if ( m_rowBottoms
[row
] < top
) continue;
1472 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1473 if ( rowTop
> bottom
) break;
1475 m_rowsExposed
.Add( row
);
1477 for ( col
= 0; col
< m_numCols
; col
++ )
1479 if ( m_colRights
[col
] < left
) continue;
1481 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1482 if ( colLeft
> right
) break;
1484 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1485 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1494 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1497 wxPoint
pos( event
.GetPosition() );
1498 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1500 if ( event
.Dragging() )
1502 m_isDragging
= TRUE
;
1504 if ( event
.LeftIsDown() )
1506 switch( m_cursorMode
)
1508 case WXGRID_CURSOR_RESIZE_ROW
:
1510 int cw
, ch
, left
, dummy
;
1511 m_gridWin
->GetClientSize( &cw
, &ch
);
1512 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1514 wxClientDC
dc( m_gridWin
);
1516 dc
.SetLogicalFunction(wxINVERT
);
1517 if ( m_dragLastPos
>= 0 )
1519 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1521 dc
.DrawLine( left
, y
, left
+cw
, y
);
1526 case WXGRID_CURSOR_SELECT_ROW
:
1528 if ( (row
= YToRow( y
)) >= 0 &&
1529 !IsInSelection( row
, 0 ) )
1531 SelectRow( row
, TRUE
);
1540 m_isDragging
= FALSE
;
1543 // ------------ Left button pressed
1545 if ( event
.LeftDown() )
1547 // don't send a label click event for a hit on the
1548 // edge of the row label - this is probably the user
1549 // wanting to resize the row
1551 if ( YToEdgeOfRow(y
) < 0 )
1555 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1557 SelectRow( row
, event
.ShiftDown() );
1558 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1563 // starting to drag-resize a row
1565 m_rowLabelWin
->CaptureMouse();
1570 // ------------ Left double click
1572 else if (event
.LeftDClick() )
1574 if ( YToEdgeOfRow(y
) < 0 )
1577 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1582 // ------------ Left button released
1584 else if ( event
.LeftUp() )
1586 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1588 m_rowLabelWin
->ReleaseMouse();
1590 if ( m_dragLastPos
>= 0 )
1592 // erase the last line and resize the row
1594 int cw
, ch
, left
, dummy
;
1595 m_gridWin
->GetClientSize( &cw
, &ch
);
1596 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1598 wxClientDC
dc( m_gridWin
);
1600 dc
.SetLogicalFunction( wxINVERT
);
1601 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1602 HideCellEditControl();
1604 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
1605 SetRowSize( m_dragRowOrCol
, wxMax( y
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
1606 if ( !GetBatchCount() )
1608 // Only needed to get the correct rect.y:
1609 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
1611 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
1612 rect
.width
= m_rowLabelWidth
;
1613 rect
.height
= ch
- rect
.y
;
1614 m_rowLabelWin
->Refresh( TRUE
, &rect
);
1616 m_gridWin
->Refresh( FALSE
, &rect
);
1619 ShowCellEditControl();
1621 // Note: we are ending the event *after* doing
1622 // default processing in this case
1624 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1628 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1633 // ------------ Right button down
1635 else if ( event
.RightDown() )
1638 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1640 // no default action at the moment
1645 // ------------ Right double click
1647 else if ( event
.RightDClick() )
1650 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1652 // no default action at the moment
1657 // ------------ No buttons down and mouse moving
1659 else if ( event
.Moving() )
1661 m_dragRowOrCol
= YToEdgeOfRow( y
);
1662 if ( m_dragRowOrCol
>= 0 )
1664 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1666 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1667 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1672 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1673 if ( m_rowLabelWin
->GetCursor() == m_rowResizeCursor
)
1674 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1680 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1683 wxPoint
pos( event
.GetPosition() );
1684 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1686 if ( event
.Dragging() )
1688 m_isDragging
= TRUE
;
1690 if ( event
.LeftIsDown() )
1692 switch( m_cursorMode
)
1694 case WXGRID_CURSOR_RESIZE_COL
:
1696 int cw
, ch
, dummy
, top
;
1697 m_gridWin
->GetClientSize( &cw
, &ch
);
1698 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1700 wxClientDC
dc( m_gridWin
);
1702 dc
.SetLogicalFunction(wxINVERT
);
1703 if ( m_dragLastPos
>= 0 )
1705 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1707 dc
.DrawLine( x
, top
, x
, top
+ch
);
1712 case WXGRID_CURSOR_SELECT_COL
:
1714 if ( (col
= XToCol( x
)) >= 0 &&
1715 !IsInSelection( 0, col
) )
1717 SelectCol( col
, TRUE
);
1726 m_isDragging
= FALSE
;
1729 // ------------ Left button pressed
1731 if ( event
.LeftDown() )
1733 // don't send a label click event for a hit on the
1734 // edge of the col label - this is probably the user
1735 // wanting to resize the col
1737 if ( XToEdgeOfCol(x
) < 0 )
1741 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1743 SelectCol( col
, event
.ShiftDown() );
1744 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1749 // starting to drag-resize a col
1751 m_colLabelWin
->CaptureMouse();
1756 // ------------ Left double click
1758 if ( event
.LeftDClick() )
1760 if ( XToEdgeOfCol(x
) < 0 )
1763 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
1768 // ------------ Left button released
1770 else if ( event
.LeftUp() )
1772 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
1774 m_colLabelWin
->ReleaseMouse();
1776 if ( m_dragLastPos
>= 0 )
1778 // erase the last line and resize the col
1780 int cw
, ch
, dummy
, top
;
1781 m_gridWin
->GetClientSize( &cw
, &ch
);
1782 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1784 wxClientDC
dc( m_gridWin
);
1786 dc
.SetLogicalFunction( wxINVERT
);
1787 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1788 HideCellEditControl();
1790 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
1791 SetColSize( m_dragRowOrCol
, wxMax( x
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
1793 if ( !GetBatchCount() )
1795 // Only needed to get the correct rect.x:
1796 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
1798 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
1799 rect
.width
= cw
- rect
.x
;
1800 rect
.height
= m_colLabelHeight
;
1801 m_colLabelWin
->Refresh( TRUE
, &rect
);
1803 m_gridWin
->Refresh( FALSE
, &rect
);
1806 ShowCellEditControl();
1808 // Note: we are ending the event *after* doing
1809 // default processing in this case
1811 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1815 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1820 // ------------ Right button down
1822 else if ( event
.RightDown() )
1825 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1827 // no default action at the moment
1832 // ------------ Right double click
1834 else if ( event
.RightDClick() )
1837 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1839 // no default action at the moment
1844 // ------------ No buttons down and mouse moving
1846 else if ( event
.Moving() )
1848 m_dragRowOrCol
= XToEdgeOfCol( x
);
1849 if ( m_dragRowOrCol
>= 0 )
1851 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1853 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1854 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1859 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1860 if ( m_colLabelWin
->GetCursor() == m_colResizeCursor
)
1861 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1867 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
1869 if ( event
.LeftDown() )
1871 // indicate corner label by having both row and
1874 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
1880 else if ( event
.LeftDClick() )
1882 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
1885 else if ( event
.RightDown() )
1887 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
1889 // no default action at the moment
1893 else if ( event
.RightDClick() )
1895 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
1897 // no default action at the moment
1903 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
1906 wxPoint
pos( event
.GetPosition() );
1907 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1909 wxGridCellCoords coords
;
1910 XYToCell( x
, y
, coords
);
1912 if ( event
.Dragging() )
1914 m_isDragging
= TRUE
;
1915 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1917 // Hide the edit control, so it
1918 // won't interfer with drag-shrinking.
1919 if ( IsCellEditControlEnabled() )
1920 HideCellEditControl();
1921 if ( coords
!= wxGridNoCellCoords
)
1923 if ( !IsSelection() )
1925 SelectBlock( coords
, coords
);
1929 SelectBlock( m_currentCellCoords
, coords
);
1937 m_isDragging
= FALSE
;
1939 if ( coords
!= wxGridNoCellCoords
)
1941 if ( event
.LeftDown() )
1943 if ( event
.ShiftDown() )
1945 SelectBlock( m_currentCellCoords
, coords
);
1949 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1954 MakeCellVisible( coords
);
1955 SetCurrentCell( coords
);
1961 // ------------ Left double click
1963 else if ( event
.LeftDClick() )
1965 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1972 // ------------ Left button released
1974 else if ( event
.LeftUp() )
1976 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1978 if ( IsSelection() )
1980 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
1984 // Show the edit control, if it has
1985 // been hidden for drag-shrinking.
1986 if ( IsCellEditControlEnabled() )
1987 ShowCellEditControl();
1993 // ------------ Right button down
1995 else if ( event
.RightDown() )
1997 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2002 // no default action at the moment
2007 // ------------ Right double click
2009 else if ( event
.RightDClick() )
2011 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2016 // no default action at the moment
2020 // ------------ Moving and no button action
2022 else if ( event
.Moving() && !event
.IsButton() )
2024 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2031 // ------ interaction with data model
2033 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2035 switch ( msg
.GetId() )
2037 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2038 return GetModelValues();
2040 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2041 return SetModelValues();
2043 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2044 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2045 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2046 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2047 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2048 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2049 return Redimension( msg
);
2058 // The behaviour of this function depends on the grid table class
2059 // Clear() function. For the default wxGridStringTable class the
2060 // behavious is to replace all cell contents with wxEmptyString but
2061 // not to change the number of rows or cols.
2063 void wxGrid::ClearGrid()
2068 SetEditControlValue();
2069 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2074 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2076 // TODO: something with updateLabels flag
2080 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2086 bool ok
= m_table
->InsertRows( pos
, numRows
);
2088 // the table will have sent the results of the insert row
2089 // operation to this view object as a grid table message
2093 if ( m_numCols
== 0 )
2095 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2097 // TODO: perhaps instead of appending the default number of cols
2098 // we should remember what the last non-zero number of cols was ?
2102 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2104 // if we have just inserted cols into an empty grid the current
2105 // cell will be undefined...
2107 SetCurrentCell( 0, 0 );
2111 if ( !GetBatchCount() ) Refresh();
2114 SetEditControlValue();
2124 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2126 // TODO: something with updateLabels flag
2130 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2134 if ( m_table
&& m_table
->AppendRows( numRows
) )
2136 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2138 // if we have just inserted cols into an empty grid the current
2139 // cell will be undefined...
2141 SetCurrentCell( 0, 0 );
2144 // the table will have sent the results of the append row
2145 // operation to this view object as a grid table message
2148 if ( !GetBatchCount() ) Refresh();
2158 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2160 // TODO: something with updateLabels flag
2164 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2168 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2170 // the table will have sent the results of the delete row
2171 // operation to this view object as a grid table message
2173 if ( m_numRows
> 0 )
2174 SetEditControlValue();
2176 HideCellEditControl();
2179 if ( !GetBatchCount() ) Refresh();
2189 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2191 // TODO: something with updateLabels flag
2195 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2201 HideCellEditControl();
2202 bool ok
= m_table
->InsertCols( pos
, numCols
);
2204 // the table will have sent the results of the insert col
2205 // operation to this view object as a grid table message
2209 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2211 // if we have just inserted cols into an empty grid the current
2212 // cell will be undefined...
2214 SetCurrentCell( 0, 0 );
2218 if ( !GetBatchCount() ) Refresh();
2221 SetEditControlValue();
2231 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2233 // TODO: something with updateLabels flag
2237 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2241 if ( m_table
&& m_table
->AppendCols( numCols
) )
2243 // the table will have sent the results of the append col
2244 // operation to this view object as a grid table message
2246 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2248 // if we have just inserted cols into an empty grid the current
2249 // cell will be undefined...
2251 SetCurrentCell( 0, 0 );
2255 if ( !GetBatchCount() ) Refresh();
2265 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2267 // TODO: something with updateLabels flag
2271 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2275 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2277 // the table will have sent the results of the delete col
2278 // operation to this view object as a grid table message
2280 if ( m_numCols
> 0 )
2281 SetEditControlValue();
2283 HideCellEditControl();
2286 if ( !GetBatchCount() ) Refresh();
2298 // ----- event handlers
2301 // Generate a grid event based on a mouse event and
2302 // return the result of ProcessEvent()
2304 bool wxGrid::SendEvent( const wxEventType type
,
2306 wxMouseEvent
& mouseEv
)
2308 if ( type
== EVT_GRID_ROW_SIZE
||
2309 type
== EVT_GRID_COL_SIZE
)
2311 int rowOrCol
= (row
== -1 ? col
: row
);
2313 wxGridSizeEvent
gridEvt( GetId(),
2317 mouseEv
.GetX(), mouseEv
.GetY(),
2318 mouseEv
.ControlDown(),
2319 mouseEv
.ShiftDown(),
2321 mouseEv
.MetaDown() );
2323 return GetEventHandler()->ProcessEvent(gridEvt
);
2325 else if ( type
== EVT_GRID_RANGE_SELECT
)
2327 wxGridRangeSelectEvent
gridEvt( GetId(),
2331 m_selectedBottomRight
,
2332 mouseEv
.ControlDown(),
2333 mouseEv
.ShiftDown(),
2335 mouseEv
.MetaDown() );
2337 return GetEventHandler()->ProcessEvent(gridEvt
);
2341 wxGridEvent
gridEvt( GetId(),
2345 mouseEv
.GetX(), mouseEv
.GetY(),
2346 mouseEv
.ControlDown(),
2347 mouseEv
.ShiftDown(),
2349 mouseEv
.MetaDown() );
2351 return GetEventHandler()->ProcessEvent(gridEvt
);
2356 // Generate a grid event of specified type and return the result
2357 // of ProcessEvent().
2359 bool wxGrid::SendEvent( const wxEventType type
,
2362 if ( type
== EVT_GRID_ROW_SIZE
||
2363 type
== EVT_GRID_COL_SIZE
)
2365 int rowOrCol
= (row
== -1 ? col
: row
);
2367 wxGridSizeEvent
gridEvt( GetId(),
2372 return GetEventHandler()->ProcessEvent(gridEvt
);
2376 wxGridEvent
gridEvt( GetId(),
2381 return GetEventHandler()->ProcessEvent(gridEvt
);
2386 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2388 wxPaintDC
dc( this );
2390 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2391 m_numRows
&& m_numCols
)
2393 m_currentCellCoords
.Set(0, 0);
2394 SetEditControlValue();
2395 ShowCellEditControl();
2402 // This is just here to make sure that CalcDimensions gets called when
2403 // the grid view is resized... then the size event is skipped to allow
2404 // the box sizers to handle everything
2406 void wxGrid::OnSize( wxSizeEvent
& event
)
2413 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2415 if ( m_inOnKeyDown
)
2417 // shouldn't be here - we are going round in circles...
2419 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2422 m_inOnKeyDown
= TRUE
;
2424 // propagate the event up and see if it gets processed
2426 wxWindow
*parent
= GetParent();
2427 wxKeyEvent
keyEvt( event
);
2428 keyEvt
.SetEventObject( parent
);
2430 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2432 // try local handlers
2434 switch ( event
.KeyCode() )
2437 if ( event
.ControlDown() )
2439 MoveCursorUpBlock();
2448 if ( event
.ControlDown() )
2450 MoveCursorDownBlock();
2459 if ( event
.ControlDown() )
2461 MoveCursorLeftBlock();
2470 if ( event
.ControlDown() )
2472 MoveCursorRightBlock();
2481 if ( !IsEditable() )
2492 if ( event
.ControlDown() )
2494 event
.Skip(); // to let the edit control have the return
2503 if ( event
.ControlDown() )
2505 MakeCellVisible( 0, 0 );
2506 SetCurrentCell( 0, 0 );
2515 if ( event
.ControlDown() )
2517 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2518 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2535 // now try the cell edit control
2537 if ( IsCellEditControlEnabled() )
2539 event
.SetEventObject( m_cellEditCtrl
);
2540 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2546 m_inOnKeyDown
= FALSE
;
2550 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2552 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2554 // the event has been intercepted - do nothing
2559 m_currentCellCoords
!= wxGridNoCellCoords
)
2561 HideCellEditControl();
2562 SaveEditControlValue();
2565 m_currentCellCoords
= coords
;
2567 SetEditControlValue();
2571 ShowCellEditControl();
2573 if ( IsSelection() )
2575 wxRect
r( SelectionToDeviceRect() );
2577 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
2584 // ------ functions to get/send data (see also public functions)
2587 bool wxGrid::GetModelValues()
2591 // all we need to do is repaint the grid
2593 m_gridWin
->Refresh();
2601 bool wxGrid::SetModelValues()
2607 for ( row
= 0; row
< m_numRows
; row
++ )
2609 for ( col
= 0; col
< m_numCols
; col
++ )
2611 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2623 // Note - this function only draws cells that are in the list of
2624 // exposed cells (usually set from the update region by
2625 // CalcExposedCells)
2627 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2629 if ( !m_numRows
|| !m_numCols
) return;
2632 size_t numCells
= m_cellsExposed
.GetCount();
2634 for ( i
= 0; i
< numCells
; i
++ )
2636 DrawCell( dc
, m_cellsExposed
[i
] );
2641 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2643 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2644 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2646 #if !WXGRID_DRAW_LINES
2647 if ( m_gridLinesEnabled
)
2648 DrawCellBorder( dc
, coords
);
2651 DrawCellBackground( dc
, coords
);
2653 // TODO: separate functions here for different kinds of cells ?
2656 DrawCellValue( dc
, coords
);
2660 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2662 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2663 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2665 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2666 int row
= coords
.GetRow();
2667 int col
= coords
.GetCol();
2669 // right hand border
2671 dc
.DrawLine( m_colRights
[col
]-1, m_rowBottoms
[row
] - m_rowHeights
[row
],
2672 m_colRights
[col
]-1, m_rowBottoms
[row
]-1 );
2676 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
]-1,
2677 m_colRights
[col
]-1, m_rowBottoms
[row
]-1 );
2681 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2683 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2684 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2686 int row
= coords
.GetRow();
2687 int col
= coords
.GetCol();
2689 dc
.SetBackgroundMode( wxSOLID
);
2691 if ( IsInSelection( coords
) )
2693 // TODO: improve this
2695 dc
.SetBrush( *wxBLACK_BRUSH
);
2699 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2702 dc
.SetPen( *wxTRANSPARENT_PEN
);
2704 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2705 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2707 m_rowHeights
[row
]-1 );
2711 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2713 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2714 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2716 int row
= coords
.GetRow();
2717 int col
= coords
.GetCol();
2719 dc
.SetBackgroundMode( wxTRANSPARENT
);
2721 if ( IsInSelection( row
, col
) )
2723 // TODO: improve this
2725 dc
.SetTextBackground( wxColour(0, 0, 0) );
2726 dc
.SetTextForeground( wxColour(255, 255, 255) );
2730 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2731 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2733 dc
.SetFont( GetCellFont(row
, col
) );
2736 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2739 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2740 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2741 rect
.SetWidth( m_colWidths
[col
] - 4 );
2742 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2744 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2749 // TODO: remove this ???
2750 // This is used to redraw all grid lines e.g. when the grid line colour
2753 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
2755 if ( !m_gridLinesEnabled
||
2757 !m_numCols
) return;
2759 int top
, bottom
, left
, right
;
2763 m_gridWin
->GetClientSize(&cw
, &ch
);
2765 // virtual coords of visible area
2767 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2768 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2772 reg
.GetBox(x
, y
, w
, h
);
2773 CalcUnscrolledPosition( x
, y
, &left
, &top
);
2774 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
2777 // avoid drawing grid lines past the last row and col
2779 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
2780 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
2782 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2784 // horizontal grid lines
2787 for ( i
= 0; i
< m_numRows
; i
++ )
2789 if ( m_rowBottoms
[i
]-1 > bottom
)
2793 else if ( m_rowBottoms
[i
]-1 >= top
)
2795 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
2800 // vertical grid lines
2802 for ( i
= 0; i
< m_numCols
; i
++ )
2804 if ( m_colRights
[i
]-1 > right
)
2808 else if ( m_colRights
[i
]-1 >= left
)
2810 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
2816 void wxGrid::DrawRowLabels( wxDC
& dc
)
2818 if ( !m_numRows
|| !m_numCols
) return;
2821 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2823 for ( i
= 0; i
< numLabels
; i
++ )
2825 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2830 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2832 if ( m_rowHeights
[row
] <= 0 ) return;
2834 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2836 dc
.SetPen( *wxBLACK_PEN
);
2837 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
2838 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
2840 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
2841 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
2843 dc
.SetPen( *wxWHITE_PEN
);
2844 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
2845 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
2847 dc
.SetBackgroundMode( wxTRANSPARENT
);
2848 dc
.SetTextForeground( GetLabelTextColour() );
2849 dc
.SetFont( GetLabelFont() );
2852 GetRowLabelAlignment( &hAlign
, &vAlign
);
2856 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2857 rect
.SetWidth( m_rowLabelWidth
- 4 );
2858 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2859 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2863 void wxGrid::DrawColLabels( wxDC
& dc
)
2865 if ( !m_numRows
|| !m_numCols
) return;
2868 size_t numLabels
= m_colLabelsExposed
.GetCount();
2870 for ( i
= 0; i
< numLabels
; i
++ )
2872 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2877 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2879 if ( m_colWidths
[col
] <= 0 ) return;
2881 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2883 dc
.SetPen( *wxBLACK_PEN
);
2884 dc
.DrawLine( m_colRights
[col
]-1, 0,
2885 m_colRights
[col
]-1, m_colLabelHeight
-1 );
2887 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
2888 m_colRights
[col
]-1, m_colLabelHeight
-1 );
2890 dc
.SetPen( *wxWHITE_PEN
);
2891 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
2892 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
2894 dc
.SetBackgroundMode( wxTRANSPARENT
);
2895 dc
.SetTextForeground( GetLabelTextColour() );
2896 dc
.SetFont( GetLabelFont() );
2898 dc
.SetBackgroundMode( wxTRANSPARENT
);
2899 dc
.SetTextForeground( GetLabelTextColour() );
2900 dc
.SetFont( GetLabelFont() );
2903 GetColLabelAlignment( &hAlign
, &vAlign
);
2906 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2908 rect
.SetWidth( m_colWidths
[col
] - 4 );
2909 rect
.SetHeight( m_colLabelHeight
- 4 );
2910 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2914 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2915 const wxString
& value
,
2920 long textWidth
, textHeight
;
2921 long lineWidth
, lineHeight
;
2922 wxArrayString lines
;
2924 dc
.SetClippingRegion( rect
);
2925 StringToLines( value
, lines
);
2926 if ( lines
.GetCount() )
2928 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2929 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2932 switch ( horizAlign
)
2935 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2939 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2948 switch ( vertAlign
)
2951 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2955 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2964 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2966 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2971 dc
.DestroyClippingRegion();
2975 // Split multi line text up into an array of strings. Any existing
2976 // contents of the string array are preserved.
2978 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2980 // TODO: this won't work for WXMAC ? (lines end with '\r')
2981 // => use wxTextFile functions then (VZ)
2984 while ( startPos
< (int)value
.Length() )
2986 pos
= value
.Mid(startPos
).Find( '\n' );
2991 else if ( pos
== 0 )
2993 lines
.Add( wxEmptyString
);
2997 if ( value
[startPos
+pos
-1] == '\r' )
2999 lines
.Add( value
.Mid(startPos
, pos
-1) );
3003 lines
.Add( value
.Mid(startPos
, pos
) );
3008 if ( startPos
< (int)value
.Length() )
3010 lines
.Add( value
.Mid( startPos
) );
3015 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3016 wxArrayString
& lines
,
3017 long *width
, long *height
)
3024 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3026 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3027 w
= wxMax( w
, lineW
);
3037 // ------ Edit control functions
3041 void wxGrid::EnableEditing( bool edit
)
3043 // TODO: improve this ?
3045 if ( edit
!= m_editable
)
3049 // TODO: extend this for other edit control types
3051 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3053 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3059 #if 0 // disabled for the moment - the cell control is always active
3060 void wxGrid::EnableCellEditControl( bool enable
)
3062 if ( m_cellEditCtrl
&&
3063 enable
!= m_cellEditCtrlEnabled
)
3065 m_cellEditCtrlEnabled
= enable
;
3067 if ( m_cellEditCtrlEnabled
)
3069 SetEditControlValue();
3070 ShowCellEditControl();
3074 HideCellEditControl();
3075 SaveEditControlValue();
3082 void wxGrid::ShowCellEditControl()
3086 if ( IsCellEditControlEnabled() )
3088 if ( !IsVisible( m_currentCellCoords
) )
3094 rect
= CellToRect( m_currentCellCoords
);
3096 // convert to scrolled coords
3098 int left
, top
, right
, bottom
;
3099 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3100 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3103 m_gridWin
->GetClientSize( &cw
, &ch
);
3105 // Make the edit control large enough to allow for internal margins
3106 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3109 #if defined(__WXMOTIF__)
3110 if ( m_currentCellCoords
.GetRow() == 0 ||
3111 m_currentCellCoords
.GetCol() == 0 )
3120 if ( m_currentCellCoords
.GetRow() == 0 ||
3121 m_currentCellCoords
.GetCol() == 0 )
3131 #if defined(__WXGTK__)
3134 if (left
!= 0) left_diff
++;
3135 if (top
!= 0) top_diff
++;
3136 rect
.SetLeft( left
+ left_diff
);
3137 rect
.SetTop( top
+ top_diff
);
3138 rect
.SetRight( rect
.GetRight() - left_diff
);
3139 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3141 rect
.SetLeft( wxMax(0, left
- extra
) );
3142 rect
.SetTop( wxMax(0, top
- extra
) );
3143 rect
.SetRight( rect
.GetRight() + 2*extra
);
3144 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3147 m_cellEditCtrl
->SetSize( rect
);
3148 m_cellEditCtrl
->Show( TRUE
);
3150 switch ( m_editCtrlType
)
3152 case wxGRID_TEXTCTRL
:
3153 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3156 case wxGRID_CHECKBOX
:
3157 // TODO: anything ???
3162 // TODO: anything ???
3166 case wxGRID_COMBOBOX
:
3167 // TODO: anything ???
3172 m_cellEditCtrl
->SetFocus();
3178 void wxGrid::HideCellEditControl()
3180 if ( IsCellEditControlEnabled() )
3182 m_cellEditCtrl
->Show( FALSE
);
3187 void wxGrid::SetEditControlValue( const wxString
& value
)
3193 s
= GetCellValue(m_currentCellCoords
);
3197 if ( IsCellEditControlEnabled() )
3199 switch ( m_editCtrlType
)
3201 case wxGRID_TEXTCTRL
:
3202 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3205 case wxGRID_CHECKBOX
:
3206 // TODO: implement this
3211 // TODO: implement this
3215 case wxGRID_COMBOBOX
:
3216 // TODO: implement this
3225 void wxGrid::SaveEditControlValue()
3229 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3231 if ( IsCellEditControlEnabled() )
3233 ctrl
= m_cellEditCtrl
;
3240 bool valueChanged
= FALSE
;
3242 switch ( m_editCtrlType
)
3244 case wxGRID_TEXTCTRL
:
3245 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3246 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3247 SetCellValue( m_currentCellCoords
,
3248 ((wxTextCtrl
*) ctrl
)->GetValue() );
3251 case wxGRID_CHECKBOX
:
3252 // TODO: implement this
3257 // TODO: implement this
3261 case wxGRID_COMBOBOX
:
3262 // TODO: implement this
3269 SendEvent( EVT_GRID_CELL_CHANGE
,
3270 m_currentCellCoords
.GetRow(),
3271 m_currentCellCoords
.GetCol() );
3278 // ------ Grid location functions
3279 // Note that all of these functions work with the logical coordinates of
3280 // grid cells and labels so you will need to convert from device
3281 // coordinates for mouse events etc.
3284 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3286 int row
= YToRow(y
);
3287 int col
= XToCol(x
);
3289 if ( row
== -1 || col
== -1 )
3291 coords
= wxGridNoCellCoords
;
3295 coords
.Set( row
, col
);
3300 int wxGrid::YToRow( int y
)
3304 for ( i
= 0; i
< m_numRows
; i
++ )
3306 if ( y
< m_rowBottoms
[i
] ) return i
;
3313 int wxGrid::XToCol( int x
)
3317 for ( i
= 0; i
< m_numCols
; i
++ )
3319 if ( x
< m_colRights
[i
] ) return i
;
3326 // return the row number that that the y coord is near the edge of, or
3327 // -1 if not near an edge
3329 int wxGrid::YToEdgeOfRow( int y
)
3333 for ( i
= 0; i
< m_numRows
; i
++ )
3335 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3337 d
= abs( y
- m_rowBottoms
[i
] );
3339 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3348 // return the col number that that the x coord is near the edge of, or
3349 // -1 if not near an edge
3351 int wxGrid::XToEdgeOfCol( int x
)
3355 for ( i
= 0; i
< m_numCols
; i
++ )
3357 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3359 d
= abs( x
- m_colRights
[i
] );
3361 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3370 wxRect
wxGrid::CellToRect( int row
, int col
)
3372 wxRect
rect( -1, -1, -1, -1 );
3374 if ( row
>= 0 && row
< m_numRows
&&
3375 col
>= 0 && col
< m_numCols
)
3377 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3378 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3379 rect
.width
= m_colWidths
[col
];
3380 rect
.height
= m_rowHeights
[ row
];
3387 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3389 // get the cell rectangle in logical coords
3391 wxRect
r( CellToRect( row
, col
) );
3393 // convert to device coords
3395 int left
, top
, right
, bottom
;
3396 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3397 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3399 // check against the client area of the grid window
3402 m_gridWin
->GetClientSize( &cw
, &ch
);
3404 if ( wholeCellVisible
)
3406 // is the cell wholly visible ?
3408 return ( left
>= 0 && right
<= cw
&&
3409 top
>= 0 && bottom
<= ch
);
3413 // is the cell partly visible ?
3415 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3416 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3421 // make the specified cell location visible by doing a minimal amount
3424 void wxGrid::MakeCellVisible( int row
, int col
)
3427 int xpos
= -1, ypos
= -1;
3429 if ( row
>= 0 && row
< m_numRows
&&
3430 col
>= 0 && col
< m_numCols
)
3432 // get the cell rectangle in logical coords
3434 wxRect
r( CellToRect( row
, col
) );
3436 // convert to device coords
3438 int left
, top
, right
, bottom
;
3439 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3440 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3443 m_gridWin
->GetClientSize( &cw
, &ch
);
3449 else if ( bottom
> ch
)
3451 int h
= r
.GetHeight();
3453 for ( i
= row
-1; i
>= 0; i
-- )
3455 if ( h
+ m_rowHeights
[i
] > ch
) break;
3457 h
+= m_rowHeights
[i
];
3458 ypos
-= m_rowHeights
[i
];
3461 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3462 // have rounding errors (this is important, because if we do, we
3463 // might not scroll at all and some cells won't be redrawn)
3464 ypos
+= GRID_SCROLL_LINE
/ 2;
3471 else if ( right
> cw
)
3473 int w
= r
.GetWidth();
3475 for ( i
= col
-1; i
>= 0; i
-- )
3477 if ( w
+ m_colWidths
[i
] > cw
) break;
3479 w
+= m_colWidths
[i
];
3480 xpos
-= m_colWidths
[i
];
3483 // see comment for ypos above
3484 xpos
+= GRID_SCROLL_LINE
/ 2;
3487 if ( xpos
!= -1 || ypos
!= -1 )
3489 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3490 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3491 Scroll( xpos
, ypos
);
3499 // ------ Grid cursor movement functions
3502 bool wxGrid::MoveCursorUp()
3504 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3505 m_currentCellCoords
.GetRow() > 0 )
3507 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3508 m_currentCellCoords
.GetCol() );
3510 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3511 m_currentCellCoords
.GetCol() );
3520 bool wxGrid::MoveCursorDown()
3522 // TODO: allow for scrolling
3524 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3525 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3527 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3528 m_currentCellCoords
.GetCol() );
3530 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3531 m_currentCellCoords
.GetCol() );
3540 bool wxGrid::MoveCursorLeft()
3542 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3543 m_currentCellCoords
.GetCol() > 0 )
3545 MakeCellVisible( m_currentCellCoords
.GetRow(),
3546 m_currentCellCoords
.GetCol() - 1 );
3548 SetCurrentCell( m_currentCellCoords
.GetRow(),
3549 m_currentCellCoords
.GetCol() - 1 );
3558 bool wxGrid::MoveCursorRight()
3560 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3561 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3563 MakeCellVisible( m_currentCellCoords
.GetRow(),
3564 m_currentCellCoords
.GetCol() + 1 );
3566 SetCurrentCell( m_currentCellCoords
.GetRow(),
3567 m_currentCellCoords
.GetCol() + 1 );
3576 bool wxGrid::MovePageUp()
3578 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3580 int row
= m_currentCellCoords
.GetRow();
3584 m_gridWin
->GetClientSize( &cw
, &ch
);
3586 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3587 int newRow
= YToRow( y
- ch
+ 1 );
3592 else if ( newRow
== row
)
3597 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3598 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3606 bool wxGrid::MovePageDown()
3608 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3610 int row
= m_currentCellCoords
.GetRow();
3611 if ( row
< m_numRows
)
3614 m_gridWin
->GetClientSize( &cw
, &ch
);
3616 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3617 int newRow
= YToRow( y
+ ch
);
3620 newRow
= m_numRows
- 1;
3622 else if ( newRow
== row
)
3627 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3628 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3636 bool wxGrid::MoveCursorUpBlock()
3639 m_currentCellCoords
!= wxGridNoCellCoords
&&
3640 m_currentCellCoords
.GetRow() > 0 )
3642 int row
= m_currentCellCoords
.GetRow();
3643 int col
= m_currentCellCoords
.GetCol();
3645 if ( m_table
->IsEmptyCell(row
, col
) )
3647 // starting in an empty cell: find the next block of
3653 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3656 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3658 // starting at the top of a block: find the next block
3664 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3669 // starting within a block: find the top of the block
3674 if ( m_table
->IsEmptyCell(row
, col
) )
3682 MakeCellVisible( row
, col
);
3683 SetCurrentCell( row
, col
);
3691 bool wxGrid::MoveCursorDownBlock()
3694 m_currentCellCoords
!= wxGridNoCellCoords
&&
3695 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3697 int row
= m_currentCellCoords
.GetRow();
3698 int col
= m_currentCellCoords
.GetCol();
3700 if ( m_table
->IsEmptyCell(row
, col
) )
3702 // starting in an empty cell: find the next block of
3705 while ( row
< m_numRows
-1 )
3708 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3711 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3713 // starting at the bottom of a block: find the next block
3716 while ( row
< m_numRows
-1 )
3719 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3724 // starting within a block: find the bottom of the block
3726 while ( row
< m_numRows
-1 )
3729 if ( m_table
->IsEmptyCell(row
, col
) )
3737 MakeCellVisible( row
, col
);
3738 SetCurrentCell( row
, col
);
3746 bool wxGrid::MoveCursorLeftBlock()
3749 m_currentCellCoords
!= wxGridNoCellCoords
&&
3750 m_currentCellCoords
.GetCol() > 0 )
3752 int row
= m_currentCellCoords
.GetRow();
3753 int col
= m_currentCellCoords
.GetCol();
3755 if ( m_table
->IsEmptyCell(row
, col
) )
3757 // starting in an empty cell: find the next block of
3763 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3766 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3768 // starting at the left of a block: find the next block
3774 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3779 // starting within a block: find the left of the block
3784 if ( m_table
->IsEmptyCell(row
, col
) )
3792 MakeCellVisible( row
, col
);
3793 SetCurrentCell( row
, col
);
3801 bool wxGrid::MoveCursorRightBlock()
3804 m_currentCellCoords
!= wxGridNoCellCoords
&&
3805 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3807 int row
= m_currentCellCoords
.GetRow();
3808 int col
= m_currentCellCoords
.GetCol();
3810 if ( m_table
->IsEmptyCell(row
, col
) )
3812 // starting in an empty cell: find the next block of
3815 while ( col
< m_numCols
-1 )
3818 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3821 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3823 // starting at the right of a block: find the next block
3826 while ( col
< m_numCols
-1 )
3829 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3834 // starting within a block: find the right of the block
3836 while ( col
< m_numCols
-1 )
3839 if ( m_table
->IsEmptyCell(row
, col
) )
3847 MakeCellVisible( row
, col
);
3848 SetCurrentCell( row
, col
);
3859 // ------ Label values and formatting
3862 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3864 *horiz
= m_rowLabelHorizAlign
;
3865 *vert
= m_rowLabelVertAlign
;
3868 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3870 *horiz
= m_colLabelHorizAlign
;
3871 *vert
= m_colLabelVertAlign
;
3874 wxString
wxGrid::GetRowLabelValue( int row
)
3878 return m_table
->GetRowLabelValue( row
);
3888 wxString
wxGrid::GetColLabelValue( int col
)
3892 return m_table
->GetColLabelValue( col
);
3903 void wxGrid::SetRowLabelSize( int width
)
3905 width
= wxMax( width
, 0 );
3906 if ( width
!= m_rowLabelWidth
)
3910 m_rowLabelWin
->Show( FALSE
);
3911 m_cornerLabelWin
->Show( FALSE
);
3913 else if ( m_rowLabelWidth
== 0 )
3915 m_rowLabelWin
->Show( TRUE
);
3916 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
3919 m_rowLabelWidth
= width
;
3926 void wxGrid::SetColLabelSize( int height
)
3928 height
= wxMax( height
, 0 );
3929 if ( height
!= m_colLabelHeight
)
3933 m_colLabelWin
->Show( FALSE
);
3934 m_cornerLabelWin
->Show( FALSE
);
3936 else if ( m_colLabelHeight
== 0 )
3938 m_colLabelWin
->Show( TRUE
);
3939 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
3942 m_colLabelHeight
= height
;
3949 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3951 if ( m_labelBackgroundColour
!= colour
)
3953 m_labelBackgroundColour
= colour
;
3954 m_rowLabelWin
->SetBackgroundColour( colour
);
3955 m_colLabelWin
->SetBackgroundColour( colour
);
3956 m_cornerLabelWin
->SetBackgroundColour( colour
);
3958 if ( !GetBatchCount() )
3960 m_rowLabelWin
->Refresh();
3961 m_colLabelWin
->Refresh();
3962 m_cornerLabelWin
->Refresh();
3967 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3969 if ( m_labelTextColour
!= colour
)
3971 m_labelTextColour
= colour
;
3972 if ( !GetBatchCount() )
3974 m_rowLabelWin
->Refresh();
3975 m_colLabelWin
->Refresh();
3980 void wxGrid::SetLabelFont( const wxFont
& font
)
3983 if ( !GetBatchCount() )
3985 m_rowLabelWin
->Refresh();
3986 m_colLabelWin
->Refresh();
3990 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3992 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3994 m_rowLabelHorizAlign
= horiz
;
3997 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3999 m_rowLabelVertAlign
= vert
;
4002 if ( !GetBatchCount() )
4004 m_rowLabelWin
->Refresh();
4008 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4010 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4012 m_colLabelHorizAlign
= horiz
;
4015 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4017 m_colLabelVertAlign
= vert
;
4020 if ( !GetBatchCount() )
4022 m_colLabelWin
->Refresh();
4026 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4030 m_table
->SetRowLabelValue( row
, s
);
4031 if ( !GetBatchCount() )
4033 wxRect rect
= CellToRect( row
, 0);
4034 if ( rect
.height
> 0 )
4036 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4038 rect
.width
= m_rowLabelWidth
;
4039 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4045 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4049 m_table
->SetColLabelValue( col
, s
);
4050 if ( !GetBatchCount() )
4052 wxRect rect
= CellToRect( 0, col
);
4053 if ( rect
.width
> 0 )
4055 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4057 rect
.height
= m_colLabelHeight
;
4058 m_colLabelWin
->Refresh( TRUE
, &rect
);
4064 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4066 if ( m_gridLineColour
!= colour
)
4068 m_gridLineColour
= colour
;
4070 wxClientDC
dc( m_gridWin
);
4072 DrawAllGridLines( dc
, wxRegion() );
4076 void wxGrid::EnableGridLines( bool enable
)
4078 if ( enable
!= m_gridLinesEnabled
)
4080 m_gridLinesEnabled
= enable
;
4082 if ( !GetBatchCount() )
4086 wxClientDC
dc( m_gridWin
);
4088 DrawAllGridLines( dc
, wxRegion() );
4092 m_gridWin
->Refresh();
4099 int wxGrid::GetDefaultRowSize()
4101 return m_defaultRowHeight
;
4104 int wxGrid::GetRowSize( int row
)
4106 if ( row
>= 0 && row
< m_numRows
)
4107 return m_rowHeights
[row
];
4109 return 0; // TODO: log an error here
4112 int wxGrid::GetDefaultColSize()
4114 return m_defaultColWidth
;
4117 int wxGrid::GetColSize( int col
)
4119 if ( col
>= 0 && col
< m_numCols
)
4120 return m_colWidths
[col
];
4122 return 0; // TODO: log an error here
4125 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4127 // TODO: replace this temp test code
4129 return wxColour( 255, 255, 255 );
4132 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4134 // TODO: replace this temp test code
4136 return wxColour( 255, 255, 255 );
4139 wxColour
wxGrid::GetDefaultCellTextColour()
4141 // TODO: replace this temp test code
4143 return wxColour( 0, 0, 0 );
4146 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4148 // TODO: replace this temp test code
4150 return wxColour( 0, 0, 0 );
4154 wxFont
wxGrid::GetDefaultCellFont()
4156 return m_defaultCellFont
;
4159 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
4161 // TODO: replace this temp test code
4163 return m_defaultCellFont
;
4166 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4168 // TODO: replace this temp test code
4174 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
4176 // TODO: replace this temp test code
4182 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4184 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4186 if ( resizeExistingRows
)
4190 for ( row
= 0; row
< m_numRows
; row
++ )
4192 m_rowHeights
[row
] = m_defaultRowHeight
;
4193 bottom
+= m_defaultRowHeight
;
4194 m_rowBottoms
[row
] = bottom
;
4200 void wxGrid::SetRowSize( int row
, int height
)
4204 if ( row
>= 0 && row
< m_numRows
)
4206 int h
= wxMax( 0, height
);
4207 int diff
= h
- m_rowHeights
[row
];
4209 m_rowHeights
[row
] = h
;
4210 for ( i
= row
; i
< m_numRows
; i
++ )
4212 m_rowBottoms
[i
] += diff
;
4216 // Note: we are ending the event *after* doing
4217 // default processing in this case
4219 SendEvent( EVT_GRID_ROW_SIZE
,
4224 // TODO: log an error here
4228 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4230 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4232 if ( resizeExistingCols
)
4236 for ( col
= 0; col
< m_numCols
; col
++ )
4238 m_colWidths
[col
] = m_defaultColWidth
;
4239 right
+= m_defaultColWidth
;
4240 m_colRights
[col
] = right
;
4246 void wxGrid::SetColSize( int col
, int width
)
4250 if ( col
>= 0 && col
< m_numCols
)
4252 int w
= wxMax( 0, width
);
4253 int diff
= w
- m_colWidths
[col
];
4254 m_colWidths
[col
] = w
;
4256 for ( i
= col
; i
< m_numCols
; i
++ )
4258 m_colRights
[i
] += diff
;
4262 // Note: we are ending the event *after* doing
4263 // default processing in this case
4265 SendEvent( EVT_GRID_COL_SIZE
,
4270 // TODO: log an error here
4274 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4276 // TODO: everything !!!
4280 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4282 // TODO: everything !!!
4286 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4288 // TODO: everything !!!
4292 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4294 // TODO: everything !!!
4298 void wxGrid::SetDefaultCellFont( const wxFont
& )
4300 // TODO: everything !!!
4304 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4306 // TODO: everything !!!
4310 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4312 // TODO: everything !!!
4316 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4318 // TODO: everything !!!
4325 // ------ cell value accessor functions
4328 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4332 m_table
->SetValue( row
, col
, s
.c_str() );
4333 if ( !GetBatchCount() )
4335 wxClientDC
dc( m_gridWin
);
4337 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4340 #if 0 // TODO: edit in place
4342 if ( m_currentCellCoords
.GetRow() == row
&&
4343 m_currentCellCoords
.GetCol() == col
)
4345 SetEditControlValue( s
);
4354 // ------ Block, row and col selection
4357 void wxGrid::SelectRow( int row
, bool addToSelected
)
4361 if ( IsSelection() && addToSelected
)
4364 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4367 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4368 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4369 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4370 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4374 need_refresh
[0] = TRUE
;
4375 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4376 wxGridCellCoords ( oldTop
- 1,
4378 m_selectedTopLeft
.SetRow( row
);
4383 need_refresh
[1] = TRUE
;
4384 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4385 wxGridCellCoords ( oldBottom
,
4388 m_selectedTopLeft
.SetCol( 0 );
4391 if ( oldBottom
< row
)
4393 need_refresh
[2] = TRUE
;
4394 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4395 wxGridCellCoords ( row
,
4397 m_selectedBottomRight
.SetRow( row
);
4400 if ( oldRight
< m_numCols
- 1 )
4402 need_refresh
[3] = TRUE
;
4403 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4405 wxGridCellCoords ( oldBottom
,
4407 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4410 for (i
= 0; i
< 4; i
++ )
4411 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4412 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4416 r
= SelectionToDeviceRect();
4418 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4420 m_selectedTopLeft
.Set( row
, 0 );
4421 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4422 r
= SelectionToDeviceRect();
4423 m_gridWin
->Refresh( FALSE
, &r
);
4426 wxGridRangeSelectEvent
gridEvt( GetId(),
4427 EVT_GRID_RANGE_SELECT
,
4430 m_selectedBottomRight
);
4432 GetEventHandler()->ProcessEvent(gridEvt
);
4436 void wxGrid::SelectCol( int col
, bool addToSelected
)
4438 if ( IsSelection() && addToSelected
)
4441 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4444 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4445 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4446 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4447 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4449 if ( oldLeft
> col
)
4451 need_refresh
[0] = TRUE
;
4452 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4453 wxGridCellCoords ( m_numRows
- 1,
4455 m_selectedTopLeft
.SetCol( col
);
4460 need_refresh
[1] = TRUE
;
4461 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4462 wxGridCellCoords ( oldTop
- 1,
4464 m_selectedTopLeft
.SetRow( 0 );
4467 if ( oldRight
< col
)
4469 need_refresh
[2] = TRUE
;
4470 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4471 wxGridCellCoords ( m_numRows
- 1,
4473 m_selectedBottomRight
.SetCol( col
);
4476 if ( oldBottom
< m_numRows
- 1 )
4478 need_refresh
[3] = TRUE
;
4479 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4481 wxGridCellCoords ( m_numRows
- 1,
4483 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4486 for (i
= 0; i
< 4; i
++ )
4487 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4488 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4494 r
= SelectionToDeviceRect();
4496 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4498 m_selectedTopLeft
.Set( 0, col
);
4499 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4500 r
= SelectionToDeviceRect();
4501 m_gridWin
->Refresh( FALSE
, &r
);
4504 wxGridRangeSelectEvent
gridEvt( GetId(),
4505 EVT_GRID_RANGE_SELECT
,
4508 m_selectedBottomRight
);
4510 GetEventHandler()->ProcessEvent(gridEvt
);
4514 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4517 wxGridCellCoords updateTopLeft
, updateBottomRight
;
4519 if ( topRow
> bottomRow
)
4526 if ( leftCol
> rightCol
)
4533 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
4534 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
4536 if ( m_selectedTopLeft
!= updateTopLeft
||
4537 m_selectedBottomRight
!= updateBottomRight
)
4539 // Compute two optimal update rectangles:
4540 // Either one rectangle is a real subset of the
4541 // other, or they are (almost) disjoint!
4543 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4546 // Store intermediate values
4547 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4548 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4549 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4550 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4552 // Determine the outer/inner coordinates.
4553 if (oldLeft
> leftCol
)
4559 if (oldTop
> topRow
)
4565 if (oldRight
< rightCol
)
4568 oldRight
= rightCol
;
4571 if (oldBottom
< bottomRow
)
4574 oldBottom
= bottomRow
;
4578 // Now, either the stuff marked old is the outer
4579 // rectangle or we don't have a situation where one
4580 // is contained in the other.
4582 if ( oldLeft
< leftCol
)
4584 need_refresh
[0] = TRUE
;
4585 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4587 wxGridCellCoords ( oldBottom
,
4591 if ( oldTop
< topRow
)
4593 need_refresh
[1] = TRUE
;
4594 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4596 wxGridCellCoords ( topRow
- 1,
4600 if ( oldRight
> rightCol
)
4602 need_refresh
[2] = TRUE
;
4603 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4605 wxGridCellCoords ( oldBottom
,
4609 if ( oldBottom
> bottomRow
)
4611 need_refresh
[3] = TRUE
;
4612 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
4614 wxGridCellCoords ( oldBottom
,
4620 m_selectedTopLeft
= updateTopLeft
;
4621 m_selectedBottomRight
= updateBottomRight
;
4623 // various Refresh() calls
4624 for (i
= 0; i
< 4; i
++ )
4625 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4626 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4629 // only generate an event if the block is not being selected by
4630 // dragging the mouse (in which case the event will be generated in
4631 // the mouse event handler)
4632 if ( !m_isDragging
)
4634 wxGridRangeSelectEvent
gridEvt( GetId(),
4635 EVT_GRID_RANGE_SELECT
,
4638 m_selectedBottomRight
);
4640 GetEventHandler()->ProcessEvent(gridEvt
);
4644 void wxGrid::SelectAll()
4646 m_selectedTopLeft
.Set( 0, 0 );
4647 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4649 m_gridWin
->Refresh();
4653 void wxGrid::ClearSelection()
4655 m_selectedTopLeft
= wxGridNoCellCoords
;
4656 m_selectedBottomRight
= wxGridNoCellCoords
;
4660 // This function returns the rectangle that encloses the given block
4661 // in device coords clipped to the client size of the grid window.
4663 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
4664 const wxGridCellCoords
&bottomRight
)
4666 wxRect
rect( wxGridNoCellRect
);
4669 cellRect
= CellToRect( topLeft
);
4670 if ( cellRect
!= wxGridNoCellRect
)
4676 rect
= wxRect( 0, 0, 0, 0 );
4679 cellRect
= CellToRect( bottomRight
);
4680 if ( cellRect
!= wxGridNoCellRect
)
4686 return wxGridNoCellRect
;
4689 // convert to scrolled coords
4691 int left
, top
, right
, bottom
;
4692 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4693 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4696 m_gridWin
->GetClientSize( &cw
, &ch
);
4698 rect
.SetLeft( wxMax(0, left
) );
4699 rect
.SetTop( wxMax(0, top
) );
4700 rect
.SetRight( wxMin(cw
, right
) );
4701 rect
.SetBottom( wxMin(ch
, bottom
) );
4709 // ------ Grid event classes
4712 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4714 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4715 int row
, int col
, int x
, int y
,
4716 bool control
, bool shift
, bool alt
, bool meta
)
4717 : wxNotifyEvent( type
, id
)
4723 m_control
= control
;
4728 SetEventObject(obj
);
4732 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4734 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4735 int rowOrCol
, int x
, int y
,
4736 bool control
, bool shift
, bool alt
, bool meta
)
4737 : wxNotifyEvent( type
, id
)
4739 m_rowOrCol
= rowOrCol
;
4742 m_control
= control
;
4747 SetEventObject(obj
);
4751 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4753 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4754 const wxGridCellCoords
& topLeft
,
4755 const wxGridCellCoords
& bottomRight
,
4756 bool control
, bool shift
, bool alt
, bool meta
)
4757 : wxNotifyEvent( type
, id
)
4759 m_topLeft
= topLeft
;
4760 m_bottomRight
= bottomRight
;
4761 m_control
= control
;
4766 SetEventObject(obj
);
4770 #endif // ifndef wxUSE_NEW_GRID