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"
36 #include "wx/generic/grid.h"
39 //////////////////////////////////////////////////////////////////////
41 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
42 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
46 //////////////////////////////////////////////////////////////////////
48 // Abstract base class for grid data (the model)
50 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
53 wxGridTableBase::wxGridTableBase()
56 m_view
= (wxGrid
*) NULL
;
59 wxGridTableBase::~wxGridTableBase()
64 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
66 wxLogWarning( "Called grid table class function InsertRows(pos=%d, N=%d)\n"
67 "but your derived table class does not override this function",
73 bool wxGridTableBase::AppendRows( size_t numRows
)
75 wxLogWarning( "Called grid table class function AppendRows(N=%d)\n"
76 "but your derived table class does not override this function",
82 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
84 wxLogWarning( "Called grid table class function DeleteRows(pos=%d, N=%d)\n"
85 "but your derived table class does not override this function",
91 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
93 wxLogWarning( "Called grid table class function InsertCols(pos=%d, N=%d)\n"
94 "but your derived table class does not override this function",
100 bool wxGridTableBase::AppendCols( size_t numCols
)
102 wxLogWarning( "Called grid table class function AppendCols(N=%d)\n"
103 "but your derived table class does not override this function",
109 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
111 wxLogWarning( "Called grid table class function DeleteCols(pos=%d, N=%d)\n"
112 "but your derived table class does not override this function",
119 wxString
wxGridTableBase::GetRowLabelValue( int row
)
126 wxString
wxGridTableBase::GetColLabelValue( int col
)
128 // default col labels are:
129 // cols 0 to 25 : A-Z
130 // cols 26 to 675 : AA-ZZ
137 s
+= ('A' + (char)( col%26
));
139 if ( col
< 0 ) break;
142 // reverse the string...
144 for ( i
= 0; i
< n
; i
++ )
154 //////////////////////////////////////////////////////////////////////
156 // Message class for the grid table to send requests and notifications
160 wxGridTableMessage::wxGridTableMessage()
162 m_table
= (wxGridTableBase
*) NULL
;
168 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
169 int commandInt1
, int commandInt2
)
173 m_comInt1
= commandInt1
;
174 m_comInt2
= commandInt2
;
179 //////////////////////////////////////////////////////////////////////
181 // A basic grid table for string data. An object of this class will
182 // created by wxGrid if you don't specify an alternative table class.
186 // this is a magic incantation which must be done!
187 #include <wx/arrimpl.cpp>
189 WX_DEFINE_OBJARRAY(wxGridStringArray
)
191 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
193 wxGridStringTable::wxGridStringTable()
198 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
203 m_data
.Alloc( numRows
);
207 for ( col
= 0; col
< numCols
; col
++ )
209 sa
.Add( wxEmptyString
);
212 for ( row
= 0; row
< numRows
; row
++ )
218 wxGridStringTable::~wxGridStringTable()
222 long wxGridStringTable::GetNumberRows()
224 return m_data
.GetCount();
227 long wxGridStringTable::GetNumberCols()
229 if ( m_data
.GetCount() > 0 )
230 return m_data
[0].GetCount();
235 wxString
wxGridStringTable::GetValue( int row
, int col
)
237 // TODO: bounds checking
239 return m_data
[row
][col
];
242 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
244 // TODO: bounds checking
246 m_data
[row
][col
] = s
;
249 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
251 // TODO: bounds checking
253 return (m_data
[row
][col
] == wxEmptyString
);
257 void wxGridStringTable::Clear()
260 int numRows
, numCols
;
262 numRows
= m_data
.GetCount();
265 numCols
= m_data
[0].GetCount();
267 for ( row
= 0; row
< numRows
; row
++ )
269 for ( col
= 0; col
< numCols
; col
++ )
271 m_data
[row
][col
] = wxEmptyString
;
278 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
282 size_t curNumRows
= m_data
.GetCount();
283 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
285 if ( pos
>= curNumRows
)
287 return AppendRows( numRows
);
291 sa
.Alloc( curNumCols
);
292 for ( col
= 0; col
< curNumCols
; col
++ )
294 sa
.Add( wxEmptyString
);
297 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
299 m_data
.Insert( sa
, row
);
304 wxGridTableMessage
msg( this,
305 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
309 GetView()->ProcessTableMessage( msg
);
315 bool wxGridStringTable::AppendRows( size_t numRows
)
319 size_t curNumRows
= m_data
.GetCount();
320 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
323 if ( curNumCols
> 0 )
325 sa
.Alloc( curNumCols
);
326 for ( col
= 0; col
< curNumCols
; col
++ )
328 sa
.Add( wxEmptyString
);
332 for ( row
= 0; row
< numRows
; row
++ )
339 wxGridTableMessage
msg( this,
340 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
343 GetView()->ProcessTableMessage( msg
);
349 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
353 size_t curNumRows
= m_data
.GetCount();
355 if ( pos
>= curNumRows
)
357 wxLogError( "Called wxGridStringTable::DeleteRows(pos=%d, N=%d)...\n"
358 "Pos value is invalid for present table with %d rows",
359 pos
, numRows
, curNumRows
);
363 if ( numRows
> curNumRows
- pos
)
365 numRows
= curNumRows
- pos
;
368 if ( numRows
>= curNumRows
)
370 m_data
.Empty(); // don't release memory just yet
374 for ( n
= 0; n
< numRows
; n
++ )
376 m_data
.Remove( pos
);
382 wxGridTableMessage
msg( this,
383 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
387 GetView()->ProcessTableMessage( msg
);
393 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
397 size_t curNumRows
= m_data
.GetCount();
398 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
400 if ( pos
>= curNumCols
)
402 return AppendCols( numCols
);
405 for ( row
= 0; row
< curNumRows
; row
++ )
407 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
409 m_data
[row
].Insert( wxEmptyString
, col
);
415 wxGridTableMessage
msg( this,
416 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
420 GetView()->ProcessTableMessage( msg
);
426 bool wxGridStringTable::AppendCols( size_t numCols
)
430 size_t curNumRows
= m_data
.GetCount();
433 // TODO: something better than this ?
435 wxLogError( "Unable to append cols to a grid table with no rows.\n"
436 "Call AppendRows() first" );
440 for ( row
= 0; row
< curNumRows
; row
++ )
442 for ( n
= 0; n
< numCols
; n
++ )
444 m_data
[row
].Add( wxEmptyString
);
450 wxGridTableMessage
msg( this,
451 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
454 GetView()->ProcessTableMessage( msg
);
460 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
464 size_t curNumRows
= m_data
.GetCount();
465 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
467 if ( pos
>= curNumCols
)
469 wxLogError( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
470 "Pos value is invalid for present table with %d cols",
471 pos
, numCols
, curNumCols
);
475 if ( numCols
> curNumCols
- pos
)
477 numCols
= curNumCols
- pos
;
480 for ( row
= 0; row
< curNumRows
; row
++ )
482 if ( numCols
>= curNumCols
)
488 for ( n
= 0; n
< numCols
; n
++ )
490 m_data
[row
].Remove( pos
);
497 wxGridTableMessage
msg( this,
498 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
502 GetView()->ProcessTableMessage( msg
);
508 wxString
wxGridStringTable::GetRowLabelValue( int row
)
510 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
512 // using default label
514 return wxGridTableBase::GetRowLabelValue( row
);
518 return m_rowLabels
[ row
];
522 wxString
wxGridStringTable::GetColLabelValue( int col
)
524 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
526 // using default label
528 return wxGridTableBase::GetColLabelValue( col
);
532 return m_colLabels
[ col
];
536 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
538 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
540 int n
= m_rowLabels
.GetCount();
542 for ( i
= n
; i
<= row
; i
++ )
544 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
548 m_rowLabels
[row
] = value
;
551 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
553 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
555 int n
= m_colLabels
.GetCount();
557 for ( i
= n
; i
<= col
; i
++ )
559 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
563 m_colLabels
[col
] = value
;
569 //////////////////////////////////////////////////////////////////////
571 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
573 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
574 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
578 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
581 const wxString
& value
,
585 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
587 m_isCellControl
= isCellControl
;
591 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& ev
)
593 switch ( ev
.KeyCode() )
596 ((wxGrid
*)GetParent())->SetEditControlValue( startValue
);
597 SetInsertionPointEnd();
607 if ( m_isCellControl
)
609 // send the event to the parent grid, skipping the
610 // event if nothing happens
612 ev
.Skip( !GetParent()->ProcessEvent( ev
) );
616 // default text control response within the top edit
625 if ( m_isCellControl
)
627 // send the event to the parent grid, skipping the
628 // event if nothing happens
630 ev
.Skip( !GetParent()->ProcessEvent( ev
) );
634 // default text control response within the top edit
646 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
649 wxTextCtrl::SetValue( s
.c_str() );
653 //////////////////////////////////////////////////////////////////////
655 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxPanel
)
658 BEGIN_EVENT_TABLE( wxGrid
, wxPanel
)
659 EVT_PAINT( wxGrid::OnPaint
)
660 EVT_SIZE( wxGrid::OnSize
)
661 EVT_MOUSE_EVENTS( wxGrid::OnMouse
)
662 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
663 EVT_TEXT( wxGRID_CELLCTRL
, wxGrid::OnText
)
664 EVT_TEXT( wxGRID_TOPCTRL
, wxGrid::OnText
)
665 EVT_COMMAND_SCROLL( wxGRID_HORIZSCROLL
, wxGrid::OnGridScroll
)
666 EVT_COMMAND_SCROLL( wxGRID_VERTSCROLL
, wxGrid::OnGridScroll
)
677 // ----- internal init and update functions
680 void wxGrid::Create()
682 m_table
= (wxGridTableBase
*) NULL
;
683 m_topEditCtrl
= (wxWindow
*) NULL
;
684 m_cellEditCtrl
= (wxWindow
*) NULL
;
685 m_horizScrollBar
= (wxScrollBar
*) NULL
;
686 m_vertScrollBar
= (wxScrollBar
*) NULL
;
700 // TODO: perhaps have a style flag for control panel
702 m_topEditCtrlEnabled
= FALSE
;
703 m_topEditCtrl
= new wxGridTextCtrl( this,
708 wxSize(WXGRID_DEFAULT_TOPEDIT_WIDTH
,
709 WXGRID_DEFAULT_TOPEDIT_HEIGHT
),
711 m_topEditCtrl
->Show( FALSE
);
713 if ( m_numRows
<= 0 )
714 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
716 if ( m_numCols
<= 0 )
717 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
719 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
720 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
722 // default labels are pale grey with black text
724 m_labelBackgroundColour
= wxColour( 192, 192, 192 );
725 m_labelTextColour
= wxColour( 0, 0, 0 );
727 // TODO: something better than this ?
729 m_labelFont
= this->GetFont();
730 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
732 m_rowLabelHorizAlign
= wxLEFT
;
733 m_rowLabelVertAlign
= wxCENTRE
;
735 m_colLabelHorizAlign
= wxCENTRE
;
736 m_colLabelVertAlign
= wxTOP
;
738 m_defaultRowHeight
= WXGRID_DEFAULT_ROW_HEIGHT
;
739 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
741 m_rowHeights
.Alloc( m_numRows
);
742 m_rowBottoms
.Alloc( m_numRows
);
743 for ( i
= 0; i
< m_numRows
; i
++ )
745 m_rowHeights
.Add( m_defaultRowHeight
);
746 m_rowBottoms
.Add( 0 ); // set by CalcDimensions()
749 m_colWidths
.Alloc( m_numCols
);
750 m_colRights
.Alloc( m_numRows
);
751 for ( i
= 0; i
< m_numCols
; i
++ )
753 m_colWidths
.Add( m_defaultColWidth
);
754 m_colRights
.Add( 0 ); // set by CalcDimensions()
757 // TODO: improve this ?
759 m_defaultCellFont
= this->GetFont();
761 m_gridLineColour
= wxColour( 0, 0, 255 );
762 m_gridLinesEnabled
= TRUE
;
764 m_scrollBarWidth
= WXGRID_DEFAULT_SCROLLBAR_WIDTH
;
766 m_horizScrollBar
= new wxScrollBar( this,
772 m_vertScrollBar
= new wxScrollBar( this,
779 m_wholeColsVisible
= 0;
780 m_wholeRowsVisible
= 0;
783 m_inOnKeyDown
= FALSE
;
786 m_cursorMode
= WXGRID_CURSOR_DEFAULT
;
789 m_isDragging
= FALSE
;
791 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
792 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
794 m_currentCellCoords
= wxGridNoCellCoords
;
795 m_currentCellHighlighted
= FALSE
;
797 m_selectedTopLeft
= wxGridNoCellCoords
;
798 m_selectedBottomRight
= wxGridNoCellCoords
;
800 m_editable
= TRUE
; // default for whole grid
802 // TODO: extend this to other types of controls
804 m_cellEditCtrl
= new wxGridTextCtrl( this,
811 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
815 m_cellEditCtrl
->Show( FALSE
);
816 m_cellEditCtrlEnabled
= TRUE
;
817 m_editCtrlType
= wxGRID_TEXTCTRL
;
819 // Not really needed here, it gets called by OnSize()
825 void wxGrid::CalcDimensions()
829 if ( IsTopEditControlEnabled() )
832 m_topEditCtrl
->GetSize( &ctrlW
, &ctrlH
);
841 int bottom
= m_top
+ m_colLabelHeight
;
842 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
844 bottom
+= m_rowHeights
[i
];
845 m_rowBottoms
[i
] = bottom
;
848 int right
= m_left
+ m_rowLabelWidth
;
849 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
851 right
+= m_colWidths
[i
];
852 m_colRights
[i
] = right
;
855 // adjust the scroll bars
859 GetClientSize(&cw
, &ch
);
861 // begin by assuming that we don't need either scroll bar
863 int vertScrollBarWidth
= 0;
864 int horizScrollBarHeight
= 0;
866 // Each scroll bar needs to eventually know if the other one is
867 // required in deciding whether or not it is also required - hence
868 // this loop. A bit inelegant but simple and it works.
871 for ( check
= 0; check
< 2; check
++ )
873 if ( m_numRows
> 0 &&
874 m_rowBottoms
[m_numRows
-1] + horizScrollBarHeight
> ch
)
876 vertScrollBarWidth
= m_scrollBarWidth
;
878 m_wholeRowsVisible
= 0;
879 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
881 // A partial row doesn't count, we still have to scroll to
882 // see the rest of it
883 if ( m_rowBottoms
[i
] + horizScrollBarHeight
> ch
) break;
885 m_wholeRowsVisible
++ ;
890 m_wholeRowsVisible
= m_numRows
- m_scrollPosY
;
893 vertScrollBarWidth
= m_scrollBarWidth
;
899 m_colRights
[m_numCols
-1] + vertScrollBarWidth
> cw
)
901 horizScrollBarHeight
= m_scrollBarWidth
;
903 m_wholeColsVisible
= 0;
904 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
906 // A partial col doesn't count, we still have to scroll to
907 // see the rest of it
908 if ( m_colRights
[i
] + vertScrollBarWidth
> cw
) break;
910 m_wholeColsVisible
++ ;
915 // we can see the right-most column
917 m_wholeColsVisible
= m_numCols
- m_scrollPosX
;
920 horizScrollBarHeight
= m_scrollBarWidth
;
926 if ( m_vertScrollBar
)
928 if ( !vertScrollBarWidth
)
930 m_vertScrollBar
->Show(FALSE
);
934 m_vertScrollBar
->Show(TRUE
);
935 m_vertScrollBar
->SetScrollbar(
937 wxMax(m_wholeRowsVisible
, 1),
938 (m_wholeRowsVisible
== 0 ? 1 : m_numRows
),
939 wxMax(m_wholeRowsVisible
, 1) );
941 m_vertScrollBar
->SetSize( cw
- m_scrollBarWidth
,
944 ch
- m_top
- horizScrollBarHeight
);
948 if ( m_horizScrollBar
)
950 if ( !horizScrollBarHeight
)
952 m_horizScrollBar
->Show(FALSE
);
956 m_horizScrollBar
->Show(TRUE
);
958 m_horizScrollBar
->SetScrollbar(
960 wxMax(m_wholeColsVisible
, 1),
961 (m_wholeColsVisible
== 0) ? 1 : m_numCols
,
962 wxMax(m_wholeColsVisible
, 1) );
964 m_horizScrollBar
->SetSize( m_left
,
965 ch
- m_scrollBarWidth
,
966 cw
- m_left
- vertScrollBarWidth
,
971 m_bottom
= m_right
= 0;
974 m_bottom
= wxMin( m_rowBottoms
[m_numRows
-1],
975 ch
- horizScrollBarHeight
);
979 m_right
= wxMin( m_colRights
[m_numCols
-1],
980 cw
- vertScrollBarWidth
);
985 bool wxGrid::IsOnScreen()
988 GetClientSize( &cw
, &ch
);
993 // this is called when the grid table sends a message to say that it
994 // has been redimensioned
996 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1000 switch ( msg
.GetId() )
1002 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1004 size_t pos
= msg
.GetCommandInt();
1005 int numRows
= msg
.GetCommandInt2();
1006 for ( i
= 0; i
< numRows
; i
++ )
1008 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1009 m_rowBottoms
.Insert( 0, pos
);
1011 m_numRows
+= numRows
;
1016 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1018 int numRows
= msg
.GetCommandInt();
1019 for ( i
= 0; i
< numRows
; i
++ )
1021 m_rowHeights
.Add( m_defaultRowHeight
);
1022 m_rowBottoms
.Add( 0 );
1024 m_numRows
+= numRows
;
1029 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1031 size_t pos
= msg
.GetCommandInt();
1032 int numRows
= msg
.GetCommandInt2();
1033 for ( i
= 0; i
< numRows
; i
++ )
1035 m_rowHeights
.Remove( pos
);
1036 m_rowBottoms
.Remove( pos
);
1038 m_numRows
-= numRows
;
1040 // TODO: improve these adjustments...
1042 if ( m_scrollPosY
>= m_numRows
)
1048 m_colWidths
.Clear();
1049 m_colRights
.Clear();
1050 m_currentCellCoords
= wxGridNoCellCoords
;
1052 else if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1054 m_currentCellCoords
.Set( 0, 0 );
1060 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1062 size_t pos
= msg
.GetCommandInt();
1063 int numCols
= msg
.GetCommandInt2();
1064 for ( i
= 0; i
< numCols
; i
++ )
1066 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1067 m_colRights
.Insert( 0, pos
);
1069 m_numCols
+= numCols
;
1074 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1076 int numCols
= msg
.GetCommandInt();
1077 for ( i
= 0; i
< numCols
; i
++ )
1079 m_colWidths
.Add( m_defaultColWidth
);
1080 m_colRights
.Add( 0 );
1082 m_numCols
+= numCols
;
1087 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1089 size_t pos
= msg
.GetCommandInt();
1090 int numCols
= msg
.GetCommandInt2();
1091 for ( i
= 0; i
< numCols
; i
++ )
1093 m_colWidths
.Remove( pos
);
1094 m_colRights
.Remove( pos
);
1096 m_numCols
-= numCols
;
1098 // TODO: improve these adjustments...
1100 if ( m_scrollPosX
>= m_numCols
)
1105 #if 0 // leave the row alone here so that AppendCols will work subsequently
1107 m_rowHeights
.Clear();
1108 m_rowBottoms
.Clear();
1110 m_currentCellCoords
= wxGridNoCellCoords
;
1112 else if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1114 m_currentCellCoords
.Set( 0, 0 );
1126 // ----- event handlers
1129 // Generate a grid event based on a mouse event and
1130 // return the result of ProcessEvent()
1132 bool wxGrid::SendEvent( const wxEventType type
,
1134 wxMouseEvent
& mouseEv
)
1136 if ( type
== EVT_GRID_ROW_SIZE
||
1137 type
== EVT_GRID_COL_SIZE
)
1139 int rowOrCol
= (row
== -1 ? col
: row
);
1141 wxGridSizeEvent
gridEvt( GetId(),
1145 mouseEv
.GetX(), mouseEv
.GetY(),
1146 mouseEv
.ControlDown(),
1147 mouseEv
.ShiftDown(),
1149 mouseEv
.MetaDown() );
1151 return GetEventHandler()->ProcessEvent(gridEvt
);
1153 else if ( type
== EVT_GRID_RANGE_SELECT
)
1155 wxGridRangeSelectEvent
gridEvt( GetId(),
1159 m_selectedBottomRight
,
1160 mouseEv
.ControlDown(),
1161 mouseEv
.ShiftDown(),
1163 mouseEv
.MetaDown() );
1165 return GetEventHandler()->ProcessEvent(gridEvt
);
1169 wxGridEvent
gridEvt( GetId(),
1173 mouseEv
.GetX(), mouseEv
.GetY(),
1174 mouseEv
.ControlDown(),
1175 mouseEv
.ShiftDown(),
1177 mouseEv
.MetaDown() );
1179 return GetEventHandler()->ProcessEvent(gridEvt
);
1184 // Generate a grid event of specified type and return the result
1185 // of ProcessEvent().
1187 bool wxGrid::SendEvent( const wxEventType type
,
1190 if ( type
== EVT_GRID_ROW_SIZE
||
1191 type
== EVT_GRID_COL_SIZE
)
1193 int rowOrCol
= (row
== -1 ? col
: row
);
1195 wxGridSizeEvent
gridEvt( GetId(),
1200 return GetEventHandler()->ProcessEvent(gridEvt
);
1204 wxGridEvent
gridEvt( GetId(),
1209 return GetEventHandler()->ProcessEvent(gridEvt
);
1214 void wxGrid::OnPaint( wxPaintEvent
& ev
)
1216 wxPaintDC
dc( this );
1218 if ( !m_batchCount
)
1220 // define a clipping region to avoid painting over the scroll bars
1223 if ( m_vertScrollBar
&& m_vertScrollBar
->IsShown() )
1224 vs
= m_scrollBarWidth
;
1227 if ( m_horizScrollBar
&& m_horizScrollBar
->IsShown() )
1228 hs
= m_scrollBarWidth
;
1231 GetClientSize( &cw
, &ch
);
1232 dc
.SetClippingRegion( 0, 0, cw
- vs
, ch
- hs
);
1234 HideCurrentCellHighlight( dc
);
1236 DrawLabelAreas( dc
);
1237 DrawColLabels( dc
);
1238 DrawRowLabels( dc
);
1240 DrawGridLines( dc
);
1243 // TODO: something more elegant than this...
1247 if ( m_currentCellCoords
== wxGridNoCellCoords
)
1248 m_currentCellCoords
.Set(0, 0);
1250 SetEditControlValue();
1251 ShowCellEditControl();
1252 m_firstPaint
= FALSE
;
1255 ShowCurrentCellHighlight( dc
);
1257 dc
.DestroyClippingRegion();
1262 void wxGrid::OnSize( wxSizeEvent
& ev
)
1268 void wxGrid::OnMouse( wxMouseEvent
& ev
)
1274 // ------------------------------------------------------------
1278 if ( ev
.Dragging() )
1280 m_isDragging
= TRUE
;
1282 if ( ev
.LeftIsDown() )
1284 switch( m_cursorMode
)
1286 case WXGRID_CURSOR_SELECT_CELL
:
1288 wxGridCellCoords cellCoords
;
1289 XYToCell( x
, y
, cellCoords
);
1290 if ( cellCoords
!= wxGridNoCellCoords
)
1292 if ( !IsSelection() )
1294 SelectBlock( cellCoords
, cellCoords
);
1296 else if ( !IsInSelection( cellCoords
) )
1298 SelectBlock( m_currentCellCoords
, cellCoords
);
1304 case WXGRID_CURSOR_RESIZE_ROW
:
1306 wxClientDC
dc(this);
1307 dc
.SetLogicalFunction(wxXOR
);
1308 if ( m_dragLastPos
>= 0 )
1310 dc
.DrawLine( m_left
, m_dragLastPos
,
1311 m_right
, m_dragLastPos
);
1313 dc
.DrawLine( m_left
, ev
.GetY(),
1314 m_right
, ev
.GetY());
1316 m_dragLastPos
= ev
.GetY();
1320 case WXGRID_CURSOR_RESIZE_COL
:
1322 wxClientDC
dc(this);
1323 dc
.SetLogicalFunction(wxINVERT
);
1324 if ( m_dragLastPos
>= 0 )
1326 dc
.DrawLine( m_dragLastPos
, m_top
,
1327 m_dragLastPos
, m_bottom
);
1329 dc
.DrawLine( ev
.GetX(), m_top
,
1330 ev
.GetX(), m_bottom
);
1332 m_dragLastPos
= ev
.GetX();
1336 case WXGRID_CURSOR_SELECT_ROW
:
1338 if ( (row
= YToRow( y
)) >= 0 &&
1339 !IsInSelection( row
, 0 ) )
1341 SelectRow( row
, TRUE
);
1346 case WXGRID_CURSOR_SELECT_COL
:
1348 if ( (col
= XToCol( x
)) >= 0 &&
1349 !IsInSelection( 0, col
) )
1351 SelectCol( col
, TRUE
);
1360 m_isDragging
= FALSE
;
1362 // ------------------------------------------------------------
1364 // Left mouse button down
1366 if ( ev
.LeftDown() )
1370 wxGridCellCoords cellCoords
;
1372 switch( XYToArea( x
, y
) )
1374 case WXGRID_ROWLABEL
:
1376 // don't send a label click event for a hit on the
1377 // edge of the row label - this is probably the user
1378 // wanting to resize the row
1380 if ( YToEdgeOfRow(y
) < 0 )
1383 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, col
, ev
) )
1385 SelectRow( row
, ev
.ShiftDown() );
1386 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1392 case WXGRID_COLLABEL
:
1394 // don't send a label click event for a hit on the
1395 // edge of the col label - this is probably the user
1396 // wanting to resize the col
1398 if ( XToEdgeOfCol(x
) < 0 )
1401 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, col
, ev
) )
1403 SelectCol( col
, ev
.ShiftDown() );
1404 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1410 case WXGRID_CORNERLABEL
:
1412 // leave both row and col as -1
1414 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, col
, ev
) )
1423 XYToCell( x
, y
, cellCoords
);
1424 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1425 cellCoords
.GetRow(),
1426 cellCoords
.GetCol(),
1429 MakeCellVisible( cellCoords
);
1430 SelectCell( cellCoords
);
1437 wxLogMessage( "outside grid area" );
1442 // ------------------------------------------------------------
1444 // Left mouse button double click
1446 else if ( ev
.LeftDClick() )
1450 wxGridCellCoords cellCoords
;
1452 switch( XYToArea( x
, y
) )
1454 case WXGRID_ROWLABEL
:
1456 // don't send a label click event for a hit on the
1457 // edge of the row label - this is probably the user
1458 // wanting to resize the row
1460 if ( YToEdgeOfRow(y
) < 0 )
1463 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, col
, ev
);
1468 case WXGRID_COLLABEL
:
1470 // don't send a label click event for a hit on the
1471 // edge of the col label - this is probably the user
1472 // wanting to resize the col
1474 if ( XToEdgeOfCol(x
) < 0 )
1477 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, col
, ev
);
1482 case WXGRID_CORNERLABEL
:
1484 // leave both row and col as -1
1486 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, col
, ev
);
1492 XYToCell( x
, y
, cellCoords
);
1493 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1494 cellCoords
.GetRow(),
1495 cellCoords
.GetCol(),
1502 wxLogMessage( "outside grid area" );
1507 // ------------------------------------------------------------
1509 // Left mouse button released
1511 else if ( ev
.LeftUp() )
1513 switch ( m_cursorMode
)
1515 case WXGRID_CURSOR_RESIZE_ROW
:
1517 if ( m_dragLastPos
>= 0 )
1519 // erase the last line and resize the row
1521 wxClientDC
dc( this );
1522 dc
.SetLogicalFunction( wxINVERT
);
1523 dc
.DrawLine( m_left
, m_dragLastPos
,
1524 m_right
, m_dragLastPos
);
1525 HideCellEditControl();
1526 int top
= m_top
+ m_colLabelHeight
;
1527 if ( m_dragRowOrCol
> 0 )
1528 top
= m_rowBottoms
[m_dragRowOrCol
-1];
1529 m_rowHeights
[m_dragRowOrCol
] = wxMax( ev
.GetY() - top
,
1530 WXGRID_MIN_ROW_HEIGHT
);
1532 ShowCellEditControl();
1535 // Note: we are ending the event *after* doing
1536 // default processing in this case
1538 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, ev
);
1543 case WXGRID_CURSOR_RESIZE_COL
:
1545 if ( m_dragLastPos
>= 0 )
1547 // erase the last line and resize the col
1549 wxClientDC
dc( this );
1550 dc
.SetLogicalFunction( wxINVERT
);
1551 dc
.DrawLine( m_left
, m_dragLastPos
,
1552 m_right
, m_dragLastPos
);
1553 HideCellEditControl();
1554 int left
= m_left
+ m_rowLabelWidth
;
1555 if ( m_dragRowOrCol
> 0 )
1556 left
= m_colRights
[m_dragRowOrCol
-1];
1557 m_colWidths
[m_dragRowOrCol
] = wxMax( ev
.GetX() - left
,
1558 WXGRID_MIN_COL_WIDTH
);
1560 ShowCellEditControl();
1563 // Note: we are ending the event *after* doing
1564 // default processing in this case
1566 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, ev
);
1571 case WXGRID_CURSOR_SELECT_CELL
:
1573 if ( IsSelection() )
1575 // Note: we are ending the event *after* doing
1576 // default processing in this case
1578 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, ev
);
1586 // ------------------------------------------------------------
1588 // Right mouse button down
1590 else if ( ev
.RightDown() )
1594 wxGridCellCoords cellCoords
;
1596 switch( XYToArea( x
, y
) )
1599 case WXGRID_ROWLABEL
:
1602 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, col
, ev
) )
1604 // TODO: default processing ?
1609 case WXGRID_COLLABEL
:
1612 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, col
, ev
) )
1614 // TODO: default processing ?
1619 case WXGRID_CORNERLABEL
:
1621 // leave both row and col as -1
1623 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, col
, ev
) )
1625 // TODO: default processing ?
1632 XYToCell( x
, y
, cellCoords
);
1633 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
1634 cellCoords
.GetRow(),
1635 cellCoords
.GetCol(),
1638 // TODO: default processing ?
1645 wxLogMessage( "outside grid area" );
1650 // ------------------------------------------------------------
1652 // Right mouse button double click
1654 else if ( ev
.RightDClick() )
1658 wxGridCellCoords cellCoords
;
1660 switch( XYToArea( x
, y
) )
1663 case WXGRID_ROWLABEL
:
1666 SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, col
, ev
);
1670 case WXGRID_COLLABEL
:
1673 SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, col
, ev
);
1677 case WXGRID_CORNERLABEL
:
1679 // leave both row and col as -1
1681 SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, col
, ev
);
1687 XYToCell( x
, y
, cellCoords
);
1688 SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
1689 cellCoords
.GetRow(),
1690 cellCoords
.GetCol(),
1697 wxLogMessage( "outside grid area" );
1702 // ------------------------------------------------------------
1704 // No buttons down and mouse moving
1706 else if ( ev
.Moving() )
1708 switch( XYToArea( x
, y
) )
1710 case WXGRID_ROWLABEL
:
1712 m_dragRowOrCol
= YToEdgeOfRow( y
);
1713 if ( m_dragRowOrCol
>= 0 )
1715 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1717 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1718 SetCursor( m_rowResizeCursor
);
1723 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1725 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1726 SetCursor( *wxSTANDARD_CURSOR
);
1732 case WXGRID_COLLABEL
:
1734 m_dragRowOrCol
= XToEdgeOfCol( x
);
1735 if ( m_dragRowOrCol
>= 0 )
1737 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1739 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1740 SetCursor( m_colResizeCursor
);
1745 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1747 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1748 SetCursor( *wxSTANDARD_CURSOR
);
1756 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1758 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1759 SetCursor( *wxSTANDARD_CURSOR
);
1768 void wxGrid::OnKeyDown( wxKeyEvent
& ev
)
1770 if ( m_inOnKeyDown
)
1772 // shouldn't be here - we are going round in circles...
1774 wxLogFatalError( "wxGrid::OnKeyDown called while alread active" );
1777 m_inOnKeyDown
= TRUE
;
1779 // propagate the event up and see if it gets processed
1781 wxWindow
*parent
= GetParent();
1782 wxKeyEvent
keyEvt( ev
);
1783 keyEvt
.SetEventObject( parent
);
1785 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
1787 // try local handlers
1789 switch ( ev
.KeyCode() )
1792 if ( ev
.ControlDown() )
1794 MoveCursorUpBlock();
1803 if ( ev
.ControlDown() )
1805 MoveCursorDownBlock();
1814 if ( ev
.ControlDown() )
1816 MoveCursorLeftBlock();
1825 if ( ev
.ControlDown() )
1827 MoveCursorRightBlock();
1840 if ( ev
.ControlDown() )
1842 MakeCellVisible( 0, 0 );
1852 if ( ev
.ControlDown() )
1854 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
1855 SelectCell( m_numRows
-1, m_numCols
-1 );
1872 // now try the cell edit control
1874 if ( IsCellEditControlEnabled() )
1876 ev
.SetEventObject( m_cellEditCtrl
);
1877 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( ev
);
1883 m_inOnKeyDown
= FALSE
;
1887 // Text updated in an edit control - either a text control or a
1890 void wxGrid::OnText( wxKeyEvent
& ev
)
1895 wxWindow
*ctrl
= (wxWindow
*)ev
.GetEventObject();
1897 if ( ctrl
== m_cellEditCtrl
&&
1898 IsTopEditControlEnabled() )
1900 // set the value of the top edit control
1902 switch ( m_editCtrlType
)
1904 case wxGRID_TEXTCTRL
:
1905 ((wxTextCtrl
*)m_topEditCtrl
)->
1906 SetValue(((wxTextCtrl
*)ctrl
)->GetValue());
1909 case wxGRID_COMBOBOX
:
1910 ((wxComboBox
*)m_topEditCtrl
)->
1911 SetValue(((wxComboBox
*)ctrl
)->GetValue());
1915 else if ( ctrl
== m_topEditCtrl
&&
1916 IsCellEditControlEnabled() )
1918 switch ( m_editCtrlType
)
1920 case wxGRID_TEXTCTRL
:
1921 ((wxTextCtrl
*)m_cellEditCtrl
)->
1922 SetValue(((wxTextCtrl
*)ctrl
)->GetValue());
1925 case wxGRID_COMBOBOX
:
1926 ((wxComboBox
*)m_cellEditCtrl
)->
1927 SetValue(((wxComboBox
*)ctrl
)->GetValue());
1936 void wxGrid::OnGridScroll( wxScrollEvent
& ev
)
1938 // propagate the event up and see if it gets processed
1940 wxWindow
*parent
= GetParent();
1941 wxScrollEvent
scrollEvt( ev
);
1942 if (parent
->GetEventHandler()->ProcessEvent( scrollEvt
)) return;
1944 HideCellEditControl();
1946 if ( ev
.GetEventObject() == m_horizScrollBar
)
1948 if ( ev
.GetPosition() != m_scrollPosX
)
1950 SetHorizontalScrollPos( ev
.GetPosition() );
1955 if ( ev
.GetPosition() != m_scrollPosY
)
1957 SetVerticalScrollPos( ev
.GetPosition() );
1961 ShowCellEditControl();
1965 void wxGrid::SelectCell( const wxGridCellCoords
& coords
)
1967 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
1969 // the event has been intercepted - do nothing
1973 wxClientDC
dc( this );
1975 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
1977 HideCurrentCellHighlight( dc
);
1978 HideCellEditControl();
1979 SaveEditControlValue();
1982 m_currentCellCoords
= coords
;
1984 SetEditControlValue();
1987 ShowCellEditControl();
1988 ShowCurrentCellHighlight( dc
);
1991 if ( IsSelection() )
1994 if ( !GetBatchCount() ) Refresh();
1999 void wxGrid::ShowCellEditControl()
2003 if ( IsCellEditControlEnabled() )
2005 if ( !IsVisible( m_currentCellCoords
) )
2011 rect
= CellToRect( m_currentCellCoords
);
2013 m_cellEditCtrl
->SetSize( rect
);
2014 m_cellEditCtrl
->Show( TRUE
);
2016 switch ( m_editCtrlType
)
2018 case wxGRID_TEXTCTRL
:
2019 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
2022 case wxGRID_CHECKBOX
:
2023 // TODO: anything ???
2028 // TODO: anything ???
2032 case wxGRID_COMBOBOX
:
2033 // TODO: anything ???
2038 m_cellEditCtrl
->SetFocus();
2044 void wxGrid::HideCellEditControl()
2046 if ( IsCellEditControlEnabled() )
2048 m_cellEditCtrl
->Show( FALSE
);
2052 void wxGrid::SetEditControlValue( const wxString
& value
)
2058 s
= GetCellValue(m_currentCellCoords
);
2062 if ( IsTopEditControlEnabled() )
2064 switch ( m_editCtrlType
)
2066 case wxGRID_TEXTCTRL
:
2067 ((wxGridTextCtrl
*)m_topEditCtrl
)->SetStartValue(s
);
2070 case wxGRID_CHECKBOX
:
2071 // TODO: implement this
2076 // TODO: implement this
2080 case wxGRID_COMBOBOX
:
2081 // TODO: implement this
2087 if ( IsCellEditControlEnabled() )
2089 switch ( m_editCtrlType
)
2091 case wxGRID_TEXTCTRL
:
2092 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
2095 case wxGRID_CHECKBOX
:
2096 // TODO: implement this
2101 // TODO: implement this
2105 case wxGRID_COMBOBOX
:
2106 // TODO: implement this
2114 void wxGrid::SaveEditControlValue()
2118 wxWindow
*ctrl
= (wxWindow
*)NULL
;
2120 if ( IsCellEditControlEnabled() )
2122 ctrl
= m_cellEditCtrl
;
2124 else if ( IsTopEditControlEnabled() )
2126 ctrl
= m_topEditCtrl
;
2133 bool valueChanged
= FALSE
;
2135 switch ( m_editCtrlType
)
2137 case wxGRID_TEXTCTRL
:
2138 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
2139 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
2140 SetCellValue( m_currentCellCoords
,
2141 ((wxTextCtrl
*) ctrl
)->GetValue() );
2144 case wxGRID_CHECKBOX
:
2145 // TODO: implement this
2150 // TODO: implement this
2154 case wxGRID_COMBOBOX
:
2155 // TODO: implement this
2162 SendEvent( EVT_GRID_CELL_CHANGE
,
2163 m_currentCellCoords
.GetRow(),
2164 m_currentCellCoords
.GetCol() );
2170 int wxGrid::XYToArea( int x
, int y
)
2172 if ( x
> m_left
&& x
< m_right
&&
2173 y
> m_top
&& y
< m_bottom
)
2175 if ( y
< m_top
+ m_colLabelHeight
)
2177 if ( x
> m_left
+ m_rowLabelWidth
)
2179 return WXGRID_COLLABEL
;
2183 return WXGRID_CORNERLABEL
;
2186 else if ( x
<= m_left
+ m_rowLabelWidth
)
2188 return WXGRID_ROWLABEL
;
2196 return WXGRID_NOAREA
;
2200 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
2202 coords
.SetRow( YToRow(y
) );
2203 coords
.SetCol( XToCol(x
) );
2207 int wxGrid::YToRow( int y
)
2211 if ( y
> m_top
+ m_colLabelHeight
)
2213 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
2215 if ( y
< m_rowBottoms
[i
] )
2226 int wxGrid::XToCol( int x
)
2230 if ( x
> m_left
+ m_rowLabelWidth
)
2232 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
2234 if ( x
< m_colRights
[i
] )
2245 // return the row number that that the y coord is near the edge of, or
2246 // -1 if not near an edge
2248 int wxGrid::YToEdgeOfRow( int y
)
2252 if ( y
> m_top
+ m_colLabelHeight
)
2254 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
2256 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
2258 d
= abs( y
- m_rowBottoms
[i
] );
2260 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
2271 // return the col number that that the x coord is near the edge of, or
2272 // -1 if not near an edge
2274 int wxGrid::XToEdgeOfCol( int x
)
2278 if ( x
> m_left
+ m_rowLabelWidth
)
2280 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
2282 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
2284 d
= abs( x
- m_colRights
[i
] );
2286 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
2297 wxRect
wxGrid::CellToRect( int row
, int col
)
2299 wxRect
rect( -1, -1, -1, -1 );
2301 if ( row
>= m_scrollPosY
&& col
>= m_scrollPosX
)
2303 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
2304 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2305 rect
.width
= m_colWidths
[col
];
2306 rect
.height
= m_rowHeights
[ row
];
2313 bool wxGrid::MoveCursorUp()
2315 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2316 m_currentCellCoords
.GetRow() > 0 )
2318 SelectCell( m_currentCellCoords
.GetRow() - 1,
2319 m_currentCellCoords
.GetCol() );
2321 if ( !IsVisible( m_currentCellCoords
) )
2322 MakeCellVisible( m_currentCellCoords
);
2330 bool wxGrid::MoveCursorDown()
2332 // TODO: allow for scrolling
2334 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2335 m_currentCellCoords
.GetRow() < m_numRows
-1 )
2337 SelectCell( m_currentCellCoords
.GetRow() + 1,
2338 m_currentCellCoords
.GetCol() );
2340 if ( !IsVisible( m_currentCellCoords
) )
2341 MakeCellVisible( m_currentCellCoords
);
2349 bool wxGrid::MoveCursorLeft()
2351 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2352 m_currentCellCoords
.GetCol() > 0 )
2354 SelectCell( m_currentCellCoords
.GetRow(),
2355 m_currentCellCoords
.GetCol() - 1 );
2357 if ( !IsVisible( m_currentCellCoords
) )
2358 MakeCellVisible( m_currentCellCoords
);
2366 bool wxGrid::MoveCursorRight()
2368 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2369 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
2371 SelectCell( m_currentCellCoords
.GetRow(),
2372 m_currentCellCoords
.GetCol() + 1 );
2374 if ( !IsVisible( m_currentCellCoords
) )
2375 MakeCellVisible( m_currentCellCoords
);
2383 bool wxGrid::MovePageUp()
2385 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2388 int row
= m_currentCellCoords
.GetRow();
2389 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
2392 if ( y
+ m_rowHeights
[row
-1] > m_bottom
) break;
2393 y
+= m_rowHeights
[ --row
];
2395 SetVerticalScrollPos( row
);
2397 SelectCell( row
, m_currentCellCoords
.GetCol() );
2404 bool wxGrid::MovePageDown()
2406 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2407 m_scrollPosY
+ m_wholeRowsVisible
< m_numRows
)
2409 if ( m_wholeRowsVisible
> 0 )
2411 SetVerticalScrollPos( m_scrollPosY
+ m_wholeRowsVisible
);
2413 else if ( m_scrollPosY
< m_numRows
- 1 )
2415 SetVerticalScrollPos( m_scrollPosY
+ 1 );
2422 // m_scrollPosY will have been updated
2424 SelectCell( m_scrollPosY
, m_currentCellCoords
.GetCol() );
2431 bool wxGrid::MoveCursorUpBlock()
2434 m_currentCellCoords
!= wxGridNoCellCoords
&&
2435 m_currentCellCoords
.GetRow() > 0 )
2437 int row
= m_currentCellCoords
.GetRow();
2438 int col
= m_currentCellCoords
.GetCol();
2440 if ( m_table
->IsEmptyCell(row
, col
) )
2442 // starting in an empty cell: find the next block of
2448 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2451 else if ( m_table
->IsEmptyCell(row
-1, col
) )
2453 // starting at the top of a block: find the next block
2459 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2464 // starting within a block: find the top of the block
2469 if ( m_table
->IsEmptyCell(row
, col
) )
2477 SelectCell( row
, col
);
2479 if ( !IsVisible( m_currentCellCoords
) )
2480 MakeCellVisible( m_currentCellCoords
);
2488 bool wxGrid::MoveCursorDownBlock()
2491 m_currentCellCoords
!= wxGridNoCellCoords
&&
2492 m_currentCellCoords
.GetRow() < m_numRows
-1 )
2494 int row
= m_currentCellCoords
.GetRow();
2495 int col
= m_currentCellCoords
.GetCol();
2497 if ( m_table
->IsEmptyCell(row
, col
) )
2499 // starting in an empty cell: find the next block of
2502 while ( row
< m_numRows
-1 )
2505 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2508 else if ( m_table
->IsEmptyCell(row
+1, col
) )
2510 // starting at the bottom of a block: find the next block
2513 while ( row
< m_numRows
-1 )
2516 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2521 // starting within a block: find the bottom of the block
2523 while ( row
< m_numRows
-1 )
2526 if ( m_table
->IsEmptyCell(row
, col
) )
2534 SelectCell( row
, col
);
2536 if ( !IsVisible( m_currentCellCoords
) )
2537 MakeCellVisible( m_currentCellCoords
);
2545 bool wxGrid::MoveCursorLeftBlock()
2548 m_currentCellCoords
!= wxGridNoCellCoords
&&
2549 m_currentCellCoords
.GetCol() > 0 )
2551 int row
= m_currentCellCoords
.GetRow();
2552 int col
= m_currentCellCoords
.GetCol();
2554 if ( m_table
->IsEmptyCell(row
, col
) )
2556 // starting in an empty cell: find the next block of
2562 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2565 else if ( m_table
->IsEmptyCell(row
, col
-1) )
2567 // starting at the left of a block: find the next block
2573 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2578 // starting within a block: find the left of the block
2583 if ( m_table
->IsEmptyCell(row
, col
) )
2591 SelectCell( row
, col
);
2593 if ( !IsVisible( m_currentCellCoords
) )
2594 MakeCellVisible( m_currentCellCoords
);
2602 bool wxGrid::MoveCursorRightBlock()
2605 m_currentCellCoords
!= wxGridNoCellCoords
&&
2606 m_currentCellCoords
.GetCol() < m_numCols
-1 )
2608 int row
= m_currentCellCoords
.GetRow();
2609 int col
= m_currentCellCoords
.GetCol();
2611 if ( m_table
->IsEmptyCell(row
, col
) )
2613 // starting in an empty cell: find the next block of
2616 while ( col
< m_numCols
-1 )
2619 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2622 else if ( m_table
->IsEmptyCell(row
, col
+1) )
2624 // starting at the right of a block: find the next block
2627 while ( col
< m_numCols
-1 )
2630 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2635 // starting within a block: find the right of the block
2637 while ( col
< m_numCols
-1 )
2640 if ( m_table
->IsEmptyCell(row
, col
) )
2648 SelectCell( row
, col
);
2650 if ( !IsVisible( m_currentCellCoords
) )
2651 MakeCellVisible( m_currentCellCoords
);
2662 // ----- grid drawing functions
2665 void wxGrid::DrawLabelAreas( wxDC
& dc
)
2668 GetClientSize(&cw
, &ch
);
2670 dc
.SetPen(*wxTRANSPARENT_PEN
);
2671 dc
.SetBrush( wxBrush(GetLabelBackgroundColour(), wxSOLID
) );
2673 dc
.DrawRectangle( m_left
, m_top
,
2674 cw
- m_left
, m_colLabelHeight
);
2676 dc
.DrawRectangle( m_left
, m_top
,
2677 m_rowLabelWidth
, ch
- m_top
);
2681 void wxGrid::DrawColLabels( wxDC
& dc
)
2684 GetClientSize(&cw
, &ch
);
2686 if (m_colLabelHeight
== 0) return;
2688 DrawColLabelBorders( dc
);
2692 rect
.height
= m_colLabelHeight
- 1;
2694 int labelLeft
= m_left
+ m_rowLabelWidth
;
2697 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
2699 if ( labelLeft
> cw
) break;
2701 rect
.x
= 1 + labelLeft
;
2702 rect
.width
= m_colWidths
[i
];
2703 DrawColLabel( dc
, rect
, i
);
2705 labelLeft
+= m_colWidths
[i
];
2710 void wxGrid::DrawColLabelBorders( wxDC
& dc
)
2712 if ( m_colLabelHeight
<= 0 ) return;
2716 GetClientSize( &cw
, &ch
);
2718 dc
.SetPen( *wxBLACK_PEN
);
2722 dc
.DrawLine( m_left
, m_top
, cw
, m_top
);
2724 dc
.DrawLine( m_left
, m_top
+ m_colLabelHeight
,
2725 cw
, m_top
+ m_colLabelHeight
);
2729 int colLeft
= m_left
+ m_rowLabelWidth
;
2730 for ( i
= m_scrollPosX
; i
<= m_numCols
; i
++ )
2732 if (colLeft
> cw
) break;
2734 dc
.DrawLine( colLeft
, m_top
,
2735 colLeft
, m_top
+ m_colLabelHeight
);
2737 if ( i
< m_numCols
) colLeft
+= m_colWidths
[i
];
2740 // Draw white highlights for a 3d effect
2742 dc
.SetPen( *wxWHITE_PEN
);
2744 colLeft
= m_left
+ m_rowLabelWidth
;
2745 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
2747 if (colLeft
> cw
) break;
2749 dc
.DrawLine(colLeft
+ 1, m_top
+ 1,
2750 colLeft
+ m_colWidths
[i
], m_top
+ 1);
2752 dc
.DrawLine(colLeft
+ 1, m_top
+ 1,
2753 colLeft
+ 1, m_top
+ m_colLabelHeight
);
2755 colLeft
+= m_colWidths
[i
];
2760 void wxGrid::DrawColLabel( wxDC
& dc
, const wxRect
& rect
, int col
)
2769 dc
.SetBackgroundMode( wxTRANSPARENT
);
2770 dc
.SetTextBackground( GetLabelBackgroundColour() );
2771 dc
.SetTextForeground( GetLabelTextColour() );
2772 dc
.SetFont( GetLabelFont() );
2775 GetColLabelAlignment( &hAlign
, &vAlign
);
2776 DrawTextRectangle( dc
, GetColLabelValue( col
), rect2
, hAlign
, vAlign
);
2780 void wxGrid::DrawRowLabels( wxDC
& dc
)
2783 GetClientSize(&cw
, &ch
);
2785 if (m_rowLabelWidth
== 0) return;
2787 DrawRowLabelBorders( dc
);
2790 rect
.x
= m_left
+ 1;
2791 rect
.width
= m_rowLabelWidth
- 1;
2793 int labelTop
= m_top
+ m_colLabelHeight
;
2796 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
2798 if ( labelTop
> ch
) break;
2800 rect
.y
= 1 + labelTop
;
2801 rect
.height
= m_rowHeights
[i
];
2802 DrawRowLabel( dc
, rect
, i
);
2804 labelTop
+= m_rowHeights
[i
];
2809 void wxGrid::DrawRowLabelBorders( wxDC
& dc
)
2811 if ( m_rowLabelWidth
<= 0 ) return;
2815 GetClientSize( &cw
, &ch
);
2817 dc
.SetPen( *wxBLACK_PEN
);
2821 dc
.DrawLine( m_left
, m_top
, m_left
, ch
);
2823 dc
.DrawLine( m_left
+ m_rowLabelWidth
, m_top
,
2824 m_left
+ m_rowLabelWidth
, ch
);
2828 int rowTop
= m_top
+ m_colLabelHeight
;
2829 for ( i
= m_scrollPosY
; i
<= m_numRows
; i
++ )
2831 if ( rowTop
> ch
) break;
2833 dc
.DrawLine( m_left
, rowTop
,
2834 m_left
+ m_rowLabelWidth
, rowTop
);
2836 if ( i
< m_numRows
) rowTop
+= m_rowHeights
[i
];
2839 // Draw white highlights for a 3d effect
2841 dc
.SetPen( *wxWHITE_PEN
);
2843 rowTop
= m_top
+ m_colLabelHeight
;
2844 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
2846 if ( rowTop
> ch
) break;
2848 dc
.DrawLine( m_left
+ 1, rowTop
+ 1,
2849 m_left
+ m_rowLabelWidth
, rowTop
+ 1 );
2851 dc
.DrawLine( m_left
+ 1, rowTop
+ 1,
2852 m_left
+ 1, rowTop
+ m_rowHeights
[i
] );
2854 rowTop
+= m_rowHeights
[i
];
2859 void wxGrid::DrawRowLabel( wxDC
& dc
, const wxRect
& rect
, int row
)
2868 dc
.SetBackgroundMode( wxTRANSPARENT
);
2869 dc
.SetTextBackground( GetLabelBackgroundColour() );
2870 dc
.SetTextForeground( GetLabelTextColour() );
2871 dc
.SetFont( GetLabelFont() );
2874 GetRowLabelAlignment( &hAlign
, &vAlign
);
2875 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect2
, hAlign
, vAlign
);
2879 void wxGrid::DrawCellArea( wxDC
& dc
)
2882 GetClientSize(&cw
, &ch
);
2884 dc
.SetPen( *wxTRANSPARENT_PEN
);
2885 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
2887 int left
= m_left
+ m_rowLabelWidth
+ 1;
2888 int top
= m_top
+ m_colLabelHeight
+ 1;
2890 dc
.DrawRectangle( left
, top
, cw
- left
, ch
- top
);
2894 void wxGrid::DrawGridLines( wxDC
& dc
)
2896 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
) return;
2900 GetClientSize(&cw
, &ch
);
2902 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2904 // horizontal grid lines
2906 int rowTop
= m_top
+ m_colLabelHeight
+ m_rowHeights
[m_scrollPosY
];
2907 for ( i
= m_scrollPosY
+ 1; i
<= m_numRows
; i
++ )
2909 if ( rowTop
> ch
) break;
2911 dc
.DrawLine( m_left
+ m_rowLabelWidth
+ 1, rowTop
,
2914 if ( i
< m_numRows
) rowTop
+= m_rowHeights
[i
];
2918 // vertical grid lines
2920 int colLeft
= m_left
+ m_rowLabelWidth
+ m_colWidths
[m_scrollPosX
];
2921 for ( i
= m_scrollPosX
+ 1; i
<= m_numCols
; i
++ )
2923 if ( colLeft
> cw
) break;
2925 dc
.DrawLine( colLeft
, m_top
+ m_colLabelHeight
+ 1,
2926 colLeft
, m_bottom
);
2928 if ( i
< m_numCols
) colLeft
+= m_colWidths
[i
];
2933 void wxGrid::DrawCells( wxDC
& dc
)
2935 if ( !m_numRows
|| !m_numCols
) return;
2940 GetClientSize( &cw
, &ch
);
2946 rect
.y
= m_top
+ m_colLabelHeight
;
2947 for ( row
= m_scrollPosY
; row
< m_numRows
; row
++ )
2949 if ( rect
.y
> ch
) break;
2951 rect
.height
= m_rowHeights
[ row
];
2952 rect
.x
= m_left
+ m_rowLabelWidth
;
2954 for ( col
= m_scrollPosX
; col
< m_numCols
; col
++ )
2956 if ( rect
.x
> cw
) break;
2958 rect
.width
= m_colWidths
[col
];
2959 DrawCellBackground( dc
, rect
, row
, col
);
2960 DrawCellValue( dc
, rect
, row
, col
);
2961 rect
.x
+= rect
.width
;
2963 rect
.y
+= rect
.height
;
2969 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxRect
& rect
, int row
, int col
)
2978 dc
.SetBackgroundMode( wxSOLID
);
2980 if ( IsInSelection( row
, col
) )
2982 // TODO: improve this
2984 dc
.SetBrush( *wxBLACK_BRUSH
);
2988 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2990 dc
.SetPen( *wxTRANSPARENT_PEN
);
2991 dc
.DrawRectangle( rect2
);
2995 void wxGrid::DrawCellValue( wxDC
& dc
, const wxRect
& rect
, int row
, int col
)
3004 dc
.SetBackgroundMode( wxTRANSPARENT
);
3006 if ( IsInSelection( row
, col
) )
3008 // TODO: improve this
3010 dc
.SetTextBackground( wxColour(0, 0, 0) );
3011 dc
.SetTextForeground( wxColour(255, 255, 255) );
3015 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
3016 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
3018 dc
.SetFont( GetCellFont(row
, col
) );
3021 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
3022 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect2
, hAlign
, vAlign
);
3026 void wxGrid::DrawCellHighlight( wxDC
& dc
, int row
, int col
)
3028 // TODO: bounds checking on row, col ?
3031 if ( row
>= m_scrollPosY
&& col
>= m_scrollPosX
)
3036 GetClientSize( &cw
, &ch
);
3038 x
= m_colRights
[col
] - m_colWidths
[col
];
3039 if ( x
>= cw
) return;
3041 y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3042 if ( y
>= ch
) return;
3044 dc
.SetLogicalFunction( wxINVERT
);
3045 dc
.SetPen( wxPen(GetCellHighlightColour(), 2, wxSOLID
) );
3046 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
3048 dc
.DrawRectangle( x
-2, y
-2,
3049 m_colWidths
[col
] + 6,
3050 m_rowHeights
[row
] + 6 );
3052 dc
.SetLogicalFunction( wxCOPY
);
3057 // This function is handy when you just want to update one or a few
3058 // cells. For example, it is used by SetCellValue()
3060 void wxGrid::DrawCell( int row
, int col
)
3062 if ( !GetBatchCount() )
3064 if ( !IsVisible( wxGridCellCoords(row
, col
) ) ) return;
3067 GetClientSize( &cw
, &ch
);
3069 wxRect
rect( CellToRect( row
, col
) );
3073 wxClientDC
dc( this );
3074 DrawCellBackground( dc
, rect
, row
, col
);
3075 DrawCellValue( dc
, rect
, row
, col
);
3081 // this is just to make other code more obvious
3083 void wxGrid::HideCurrentCellHighlight( wxDC
& dc
)
3085 if ( !m_cellEditCtrlEnabled
&&
3086 m_currentCellHighlighted
&&
3087 m_currentCellCoords
!= wxGridNoCellCoords
)
3089 DrawCellHighlight( dc
, m_currentCellCoords
);
3090 m_currentCellHighlighted
= FALSE
;
3095 // this is just to make other code more obvious
3097 void wxGrid::ShowCurrentCellHighlight( wxDC
& dc
)
3099 if ( !m_cellEditCtrlEnabled
&&
3100 !m_currentCellHighlighted
&&
3101 m_currentCellCoords
!= wxGridNoCellCoords
)
3103 DrawCellHighlight( dc
, m_currentCellCoords
);
3104 m_currentCellHighlighted
= TRUE
;
3109 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3110 const wxString
& value
,
3115 long textWidth
, textHeight
;
3116 long lineWidth
, lineHeight
;
3117 wxArrayString lines
;
3119 // see if we are already clipping
3122 dc
.GetClippingBox( clipRect
);
3124 bool alreadyClipping
= TRUE
;
3125 wxRect intersectRect
;
3127 if ( clipRect
.x
== 0 && clipRect
.y
== 0 &&
3128 clipRect
.width
== 0 && clipRect
.height
== 0)
3130 alreadyClipping
= FALSE
;
3131 intersectRect
= rect
;
3135 // Find the intersection of the clipping rectangle and our
3138 wxRegion
region( rect
);
3139 region
.Intersect( clipRect
);
3140 if ( region
.IsEmpty() )
3146 intersectRect
= region
.GetBox();
3149 if ( alreadyClipping
) dc
.DestroyClippingRegion();
3151 dc
.SetClippingRegion( intersectRect
);
3153 StringToLines( value
, lines
);
3154 if ( lines
.GetCount() )
3156 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3157 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3160 switch ( horizAlign
)
3163 x
= rect
.x
+ (rect
.width
- textWidth
- 1.0);
3167 x
= rect
.x
+ ((rect
.width
- textWidth
)/2.0);
3176 switch ( vertAlign
)
3179 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3183 y
= rect
.y
+ ((rect
.height
- textHeight
)/2.0);
3192 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3194 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3199 dc
.DestroyClippingRegion();
3200 if (alreadyClipping
) dc
.SetClippingRegion( clipRect
);
3204 // Split multi line text up into an array of strings. Any existing
3205 // contents of the string array are preserved.
3207 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3209 // TODO: this won't work for WXMAC ? (lines end with '\r')
3210 // => use wxTextFile functions then (VZ)
3213 while ( startPos
< (int)value
.Length() )
3215 pos
= value
.Mid(startPos
).Find( '\n' );
3220 else if ( pos
== 0 )
3222 lines
.Add( wxEmptyString
);
3226 if ( value
[startPos
+pos
-1] == '\r' )
3228 lines
.Add( value
.Mid(startPos
, pos
-1) );
3232 lines
.Add( value
.Mid(startPos
, pos
) );
3237 if ( startPos
< (int)value
.Length() )
3239 lines
.Add( value
.Mid( startPos
) );
3244 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3245 wxArrayString
& lines
,
3246 long *width
, long *height
)
3253 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3255 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3256 w
= wxMax( w
, lineW
);
3266 // ------ functions to get/send data (see also public functions)
3269 bool wxGrid::GetModelValues()
3273 // all we need to do is repaint the grid
3283 bool wxGrid::SetModelValues()
3289 for ( row
= m_scrollPosY
; row
< m_numRows
; row
++ )
3291 for ( col
= m_scrollPosX
; col
< m_numCols
; col
++ )
3293 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3305 // ------ public functions
3308 bool wxGrid::CreateGrid( int numRows
, int numCols
)
3312 wxLogError( "wxGrid::CreateGrid(numRows, numCols) called more than once" );
3317 m_numRows
= numRows
;
3318 m_numCols
= numCols
;
3320 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3321 m_table
->SetView( this );
3330 // The behaviour of this function depends on the grid table class
3331 // Clear() function. For the default wxGridStringTable class the
3332 // behavious is to replace all cell contents with wxEmptyString but
3333 // not to change the number of rows or cols.
3335 void wxGrid::ClearGrid()
3340 SetEditControlValue();
3341 if ( !GetBatchCount() ) Refresh();
3346 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3348 // TODO: something with updateLabels flag
3352 wxLogError( "Called wxGrid::InsertRows() before calling CreateGrid()" );
3358 bool ok
= m_table
->InsertRows( pos
, numRows
);
3360 // the table will have sent the results of the insert row
3361 // operation to this view object as a grid table message
3365 if ( m_numCols
== 0 )
3367 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3369 // TODO: perhaps instead of appending the default number of cols
3370 // we should remember what the last non-zero number of cols was ?
3374 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3376 // if we have just inserted cols into an empty grid the current
3377 // cell will be undefined...
3382 if ( !GetBatchCount() ) Refresh();
3385 SetEditControlValue();
3394 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3396 // TODO: something with updateLabels flag
3400 wxLogError( "Called wxGrid::AppendRows() before calling CreateGrid()" );
3404 if ( m_table
&& m_table
->AppendRows( numRows
) )
3406 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3408 // if we have just inserted cols into an empty grid the current
3409 // cell will be undefined...
3414 // the table will have sent the results of the append row
3415 // operation to this view object as a grid table message
3417 if ( !GetBatchCount() ) Refresh();
3426 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3428 // TODO: something with updateLabels flag
3432 wxLogError( "Called wxGrid::DeleteRows() before calling CreateGrid()" );
3436 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3438 // the table will have sent the results of the delete row
3439 // operation to this view object as a grid table message
3441 if ( m_numRows
> 0 )
3442 SetEditControlValue();
3444 HideCellEditControl();
3446 if ( !GetBatchCount() ) Refresh();
3455 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3457 // TODO: something with updateLabels flag
3461 wxLogError( "Called wxGrid::InsertCols() before calling CreateGrid()" );
3467 HideCellEditControl();
3468 bool ok
= m_table
->InsertCols( pos
, numCols
);
3470 // the table will have sent the results of the insert col
3471 // operation to this view object as a grid table message
3475 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3477 // if we have just inserted cols into an empty grid the current
3478 // cell will be undefined...
3483 if ( !GetBatchCount() ) Refresh();
3486 SetEditControlValue();
3495 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3497 // TODO: something with updateLabels flag
3501 wxLogError( "Called wxGrid::AppendCols() before calling CreateGrid()" );
3505 if ( m_table
&& m_table
->AppendCols( numCols
) )
3507 // the table will have sent the results of the append col
3508 // operation to this view object as a grid table message
3510 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3512 // if we have just inserted cols into an empty grid the current
3513 // cell will be undefined...
3517 if ( !GetBatchCount() ) Refresh();
3526 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3528 // TODO: something with updateLabels flag
3532 wxLogError( "Called wxGrid::DeleteCols() before calling CreateGrid()" );
3536 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3538 // the table will have sent the results of the delete col
3539 // operation to this view object as a grid table message
3541 if ( m_numCols
> 0 )
3542 SetEditControlValue();
3544 HideCellEditControl();
3546 if ( !GetBatchCount() ) Refresh();
3558 // ------ control panel and cell edit control
3561 void wxGrid::EnableEditing( bool edit
)
3563 // TODO: improve this ?
3565 if ( edit
!= m_editable
)
3568 if ( !m_editable
) HideCellEditControl();
3569 m_topEditCtrlEnabled
= m_editable
;
3570 m_cellEditCtrlEnabled
= m_editable
;
3571 if ( !m_editable
) ShowCellEditControl();
3576 void wxGrid::EnableTopEditControl( bool enable
)
3578 if ( enable
!= m_topEditCtrlEnabled
)
3580 HideCellEditControl();
3581 m_topEditCtrlEnabled
= enable
;
3583 m_topEditCtrl
->Show( enable
);
3585 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
3586 SetEditControlValue();
3588 ShowCellEditControl();
3589 if ( !GetBatchCount() ) Refresh();
3593 void wxGrid::EnableCellEditControl( bool enable
)
3595 if ( m_cellEditCtrl
&&
3596 enable
!= m_cellEditCtrlEnabled
)
3598 wxClientDC
dc( this );
3600 HideCurrentCellHighlight( dc
);
3601 HideCellEditControl();
3602 SaveEditControlValue();
3604 m_cellEditCtrlEnabled
= enable
;
3606 SetEditControlValue();
3607 ShowCellEditControl();
3608 ShowCurrentCellHighlight( dc
);
3614 // ------ grid formatting functions
3617 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3619 *horiz
= m_rowLabelHorizAlign
;
3620 *vert
= m_rowLabelVertAlign
;
3623 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3625 *horiz
= m_colLabelHorizAlign
;
3626 *vert
= m_colLabelVertAlign
;
3629 wxString
wxGrid::GetRowLabelValue( int row
)
3633 return m_table
->GetRowLabelValue( row
);
3643 wxString
wxGrid::GetColLabelValue( int col
)
3647 return m_table
->GetColLabelValue( col
);
3657 void wxGrid::SetRowLabelSize( int width
)
3659 m_rowLabelWidth
= wxMax( 0, width
);
3661 ShowCellEditControl();
3662 if ( !GetBatchCount() ) Refresh();
3665 void wxGrid::SetColLabelSize( int height
)
3667 m_colLabelHeight
= wxMax( 0, height
);
3669 ShowCellEditControl();
3670 if ( !GetBatchCount() ) Refresh();
3673 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3675 m_labelBackgroundColour
= colour
;
3676 if ( !GetBatchCount() ) Refresh();
3679 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3681 m_labelTextColour
= colour
;
3682 if ( !GetBatchCount() ) Refresh();
3685 void wxGrid::SetLabelFont( const wxFont
& font
)
3688 if ( !GetBatchCount() ) Refresh();
3691 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3693 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3695 m_rowLabelHorizAlign
= horiz
;
3698 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3700 m_rowLabelVertAlign
= vert
;
3703 if ( !GetBatchCount() ) Refresh();
3706 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
3708 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3710 m_colLabelHorizAlign
= horiz
;
3713 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3715 m_colLabelVertAlign
= vert
;
3718 if ( !GetBatchCount() ) Refresh();
3721 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
3725 m_table
->SetRowLabelValue( row
, s
);
3726 if ( !GetBatchCount() ) Refresh();
3730 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
3734 m_table
->SetColLabelValue( col
, s
);
3735 if ( !GetBatchCount() ) Refresh();
3739 void wxGrid::SetGridLineColour( const wxColour
& colour
)
3741 m_gridLineColour
= colour
;
3743 wxClientDC
dc( this );
3744 DrawGridLines( dc
);
3747 void wxGrid::EnableGridLines( bool enable
)
3749 if ( enable
!= m_gridLinesEnabled
)
3751 m_gridLinesEnabled
= enable
;
3752 if ( !GetBatchCount() ) Refresh();
3757 int wxGrid::GetDefaultRowSize()
3759 return m_defaultRowHeight
;
3762 int wxGrid::GetRowSize( int row
)
3764 if ( row
>= 0 && row
< m_numRows
)
3765 return m_rowHeights
[row
];
3767 return 0; // TODO: log an error here
3770 int wxGrid::GetDefaultColSize()
3772 return m_defaultColWidth
;
3775 int wxGrid::GetColSize( int col
)
3777 if ( col
>= 0 && col
< m_numCols
)
3778 return m_colWidths
[col
];
3780 return 0; // TODO: log an error here
3783 wxColour
wxGrid::GetDefaultCellBackgroundColour()
3785 // TODO: replace this temp test code
3787 return wxColour( 255, 255, 255 );
3790 wxColour
wxGrid::GetCellBackgroundColour( int row
, int col
)
3792 // TODO: replace this temp test code
3794 return wxColour( 255, 255, 255 );
3797 wxColour
wxGrid::GetDefaultCellTextColour()
3799 // TODO: replace this temp test code
3801 return wxColour( 0, 0, 0 );
3804 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
3806 // TODO: replace this temp test code
3808 return wxColour( 0, 0, 0 );
3812 wxColour
wxGrid::GetCellHighlightColour()
3814 // TODO: replace this temp test code
3816 return wxColour( 0, 0, 0 );
3820 wxFont
wxGrid::GetDefaultCellFont()
3822 return m_defaultCellFont
;
3825 wxFont
wxGrid::GetCellFont( int row
, int col
)
3827 // TODO: replace this temp test code
3829 return m_defaultCellFont
;
3832 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
3834 // TODO: replace this temp test code
3840 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
3842 // TODO: replace this temp test code
3848 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
3850 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
3852 if ( resizeExistingRows
)
3854 // TODO: what do we do about events here ?
3855 // Generate an event for each resize ?
3858 for ( row
= 0; row
< m_numRows
; row
++ )
3860 m_rowHeights
[row
] = m_defaultRowHeight
;
3863 if ( !GetBatchCount() ) Refresh();
3867 void wxGrid::SetRowSize( int row
, int height
)
3869 if ( row
>= 0 && row
< m_numRows
)
3871 m_rowHeights
[row
] = wxMax( 0, height
);
3873 if ( !GetBatchCount() ) Refresh();
3875 // Note: we are ending the event *after* doing
3876 // default processing in this case
3878 SendEvent( EVT_GRID_ROW_SIZE
,
3883 // TODO: log an error here
3887 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
3889 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
3891 if ( resizeExistingCols
)
3893 // TODO: what do we do about events here ?
3894 // Generate an event for each resize ?
3897 for ( col
= 0; col
< m_numCols
; col
++ )
3899 m_colWidths
[col
] = m_defaultColWidth
;
3902 if ( !GetBatchCount() ) Refresh();
3906 void wxGrid::SetColSize( int col
, int width
)
3908 if ( col
>= 0 && col
< m_numCols
)
3910 m_colWidths
[col
] = wxMax( 0, width
);
3912 if ( !GetBatchCount() ) Refresh();
3914 // Note: we are ending the event *after* doing
3915 // default processing in this case
3917 SendEvent( EVT_GRID_COL_SIZE
,
3922 // TODO: log an error here
3926 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
3928 // TODO: everything !!!
3932 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& )
3934 // TODO: everything !!!
3938 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
3940 // TODO: everything !!!
3944 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& )
3946 // TODO: everything !!!
3950 void wxGrid::SetCellHighlightColour( const wxColour
& )
3952 // TODO: everything !!!
3956 void wxGrid::SetDefaultCellFont( const wxFont
& )
3958 // TODO: everything !!!
3962 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& )
3964 // TODO: everything !!!
3968 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
3970 // TODO: everything !!!
3974 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
3976 // TODO: everything !!!
3982 // ------ cell value accessor functions
3985 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
3989 m_table
->SetValue( row
, col
, s
.c_str() );
3990 DrawCell( row
, col
);
3991 if ( m_currentCellCoords
.GetRow() == row
&&
3992 m_currentCellCoords
.GetCol() == col
)
3994 SetEditControlValue( s
);
4002 // ------ interaction with data model
4004 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4006 switch ( msg
.GetId() )
4008 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4009 return GetModelValues();
4011 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4012 return SetModelValues();
4014 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4015 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4016 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4017 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4018 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4019 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4020 return Redimension( msg
);
4029 // ------ Grid location functions
4031 // (see also inline functions in grid.h)
4034 // check to see if a cell location is wholly visible
4036 bool wxGrid::IsVisible( const wxGridCellCoords
& coords
)
4038 return ( coords
.GetRow() >= m_scrollPosY
&&
4039 coords
.GetRow() < m_scrollPosY
+ m_wholeRowsVisible
&&
4040 coords
.GetCol() >= m_scrollPosX
&&
4041 coords
.GetCol() < m_scrollPosX
+ m_wholeColsVisible
);
4045 // make the specified cell location visible by doing a minimal amount
4048 void wxGrid::MakeCellVisible( int row
, int col
)
4050 int lastX
= m_scrollPosX
;
4051 int lastY
= m_scrollPosY
;
4053 if ( row
>= 0 && row
< m_numRows
&&
4054 col
>= 0 && col
< m_numCols
)
4056 if ( row
< m_scrollPosY
)
4058 SetVerticalScrollPos( row
);
4060 else if ( row
>= m_scrollPosY
+ m_wholeRowsVisible
)
4063 int h
= m_rowBottoms
[row
];
4064 for ( i
= m_scrollPosY
; i
< m_numRows
&& h
> m_bottom
; i
++ )
4066 h
-= m_rowHeights
[i
];
4068 SetVerticalScrollPos( i
);
4071 if ( col
< m_scrollPosX
)
4073 SetHorizontalScrollPos( col
);
4075 else if ( col
>= m_scrollPosX
+ m_wholeColsVisible
)
4078 int w
= m_colRights
[col
];
4079 for ( i
= m_scrollPosX
; i
< m_numCols
&& w
> m_right
; i
++ )
4081 w
-= m_colWidths
[i
];
4083 SetHorizontalScrollPos( i
);
4086 if ( m_scrollPosX
!= lastX
|| m_scrollPosY
!= lastY
)
4088 // The cell was not visible before but not it is
4090 ShowCellEditControl();
4095 // TODO: log an error
4100 void wxGrid::SetVerticalScrollPos( int topMostRow
)
4102 if ( m_vertScrollBar
&& topMostRow
!= m_scrollPosY
)
4104 m_scrollPosY
= topMostRow
;
4112 void wxGrid::SetHorizontalScrollPos( int leftMostCol
)
4114 if ( m_horizScrollBar
&& leftMostCol
!= m_scrollPosX
)
4116 m_scrollPosX
= leftMostCol
;
4125 // ------ block, row and col selection
4128 void wxGrid::SelectRow( int row
, bool addToSelected
)
4130 if ( IsSelection() && addToSelected
)
4132 if ( m_selectedTopLeft
.GetRow() > row
)
4133 m_selectedTopLeft
.SetRow( row
);
4135 m_selectedTopLeft
.SetCol( 0 );
4137 if ( m_selectedBottomRight
.GetRow() < row
)
4138 m_selectedBottomRight
.SetRow( row
);
4140 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4145 m_selectedTopLeft
.Set( row
, 0 );
4146 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4149 if ( !GetBatchCount() )
4151 wxRect
rect( SelectionToRect() );
4152 if ( rect
!= wxGridNoCellRect
) Refresh( TRUE
, &rect
);
4155 wxGridRangeSelectEvent
gridEvt( GetId(),
4156 EVT_GRID_RANGE_SELECT
,
4159 m_selectedBottomRight
);
4161 GetEventHandler()->ProcessEvent(gridEvt
);
4165 void wxGrid::SelectCol( int col
, bool addToSelected
)
4167 if ( addToSelected
&& m_selectedTopLeft
!= wxGridNoCellCoords
)
4169 if ( m_selectedTopLeft
.GetCol() > col
)
4170 m_selectedTopLeft
.SetCol( col
);
4172 m_selectedTopLeft
.SetRow( 0 );
4174 if ( m_selectedBottomRight
.GetCol() < col
)
4175 m_selectedBottomRight
.SetCol( col
);
4177 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4182 m_selectedTopLeft
.Set( 0, col
);
4183 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4186 if ( !GetBatchCount() )
4188 wxRect
rect( SelectionToRect() );
4189 if ( rect
!= wxGridNoCellRect
) Refresh( TRUE
, &rect
);
4192 wxGridRangeSelectEvent
gridEvt( GetId(),
4193 EVT_GRID_RANGE_SELECT
,
4196 m_selectedBottomRight
);
4198 GetEventHandler()->ProcessEvent(gridEvt
);
4202 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4206 if ( topRow
> bottomRow
)
4213 if ( leftCol
> rightCol
)
4220 m_selectedTopLeft
.Set( topRow
, leftCol
);
4221 m_selectedBottomRight
.Set( bottomRow
, rightCol
);
4223 if ( !GetBatchCount() )
4225 wxRect
rect( SelectionToRect() );
4226 if ( rect
!= wxGridNoCellRect
) Refresh( TRUE
, &rect
);
4229 // only generate an event if the block is not being selected by
4230 // dragging the mouse (in which case the event will be generated in
4232 if ( !m_isDragging
)
4234 wxGridRangeSelectEvent
gridEvt( GetId(),
4235 EVT_GRID_RANGE_SELECT
,
4238 m_selectedBottomRight
);
4240 GetEventHandler()->ProcessEvent(gridEvt
);
4244 void wxGrid::SelectAll()
4246 m_selectedTopLeft
.Set( 0, 0 );
4247 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4249 if ( !GetBatchCount() ) Refresh();
4253 void wxGrid::ClearSelection()
4255 if ( IsSelection() )
4257 wxRect
rect( SelectionToRect() );
4258 if ( rect
!= wxGridNoCellRect
)
4260 Refresh( TRUE
, &rect
);
4263 m_selectedTopLeft
= wxGridNoCellCoords
;
4264 m_selectedBottomRight
= wxGridNoCellCoords
;
4269 wxRect
wxGrid::SelectionToRect()
4274 if ( IsSelection() )
4276 cellRect
= CellToRect( m_selectedTopLeft
);
4277 if ( cellRect
!= wxGridNoCellRect
)
4283 rect
= wxRect( m_left
, m_top
, 0, 0 );
4286 cellRect
= CellToRect( m_selectedBottomRight
);
4287 if ( cellRect
!= wxGridNoCellRect
)
4293 return wxGridNoCellRect
;
4304 // ------ Grid event classes
4307 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4309 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4310 int row
, int col
, int x
, int y
,
4311 bool control
, bool shift
, bool alt
, bool meta
)
4312 : wxNotifyEvent( type
, id
)
4318 m_control
= control
;
4323 SetEventObject(obj
);
4327 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4329 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4330 int rowOrCol
, int x
, int y
,
4331 bool control
, bool shift
, bool alt
, bool meta
)
4332 : wxNotifyEvent( type
, id
)
4334 m_rowOrCol
= rowOrCol
;
4337 m_control
= control
;
4342 SetEventObject(obj
);
4346 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4348 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4349 const wxGridCellCoords
& topLeft
,
4350 const wxGridCellCoords
& bottomRight
,
4351 bool control
, bool shift
, bool alt
, bool meta
)
4352 : wxNotifyEvent( type
, id
)
4354 m_topLeft
= topLeft
;
4355 m_bottomRight
= bottomRight
;
4356 m_control
= control
;
4361 SetEventObject(obj
);
4365 #endif // ifndef wxUSE_NEW_GRID