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 // This is here in case OnSize does not get called when the grid is
826 void wxGrid::CalcDimensions()
830 if ( IsTopEditControlEnabled() )
833 m_topEditCtrl
->GetSize( &ctrlW
, &ctrlH
);
842 int bottom
= m_top
+ m_colLabelHeight
;
843 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
845 bottom
+= m_rowHeights
[i
];
846 m_rowBottoms
[i
] = bottom
;
849 int right
= m_left
+ m_rowLabelWidth
;
850 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
852 right
+= m_colWidths
[i
];
853 m_colRights
[i
] = right
;
856 // adjust the scroll bars
860 GetClientSize(&cw
, &ch
);
862 // begin by assuming that we don't need either scroll bar
864 int vertScrollBarWidth
= 0;
865 int horizScrollBarHeight
= 0;
867 // Each scroll bar needs to eventually know if the other one is
868 // required in deciding whether or not it is also required - hence
869 // this loop. A bit inelegant but simple and it works.
872 for ( check
= 0; check
< 2; check
++ )
874 if ( m_numRows
> 0 &&
875 m_rowBottoms
[m_numRows
-1] + horizScrollBarHeight
> ch
)
877 vertScrollBarWidth
= m_scrollBarWidth
;
879 m_wholeRowsVisible
= 0;
880 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
882 // A partial row doesn't count, we still have to scroll to
883 // see the rest of it
884 if ( m_rowBottoms
[i
] + horizScrollBarHeight
> ch
) break;
886 m_wholeRowsVisible
++ ;
891 m_wholeRowsVisible
= m_numRows
- m_scrollPosY
;
894 vertScrollBarWidth
= m_scrollBarWidth
;
900 m_colRights
[m_numCols
-1] + vertScrollBarWidth
> cw
)
902 horizScrollBarHeight
= m_scrollBarWidth
;
904 m_wholeColsVisible
= 0;
905 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
907 // A partial col doesn't count, we still have to scroll to
908 // see the rest of it
909 if ( m_colRights
[i
] + vertScrollBarWidth
> cw
) break;
911 m_wholeColsVisible
++ ;
916 // we can see the right-most column
918 m_wholeColsVisible
= m_numCols
- m_scrollPosX
;
921 horizScrollBarHeight
= m_scrollBarWidth
;
927 if ( m_vertScrollBar
)
929 if ( !vertScrollBarWidth
)
931 m_vertScrollBar
->Show(FALSE
);
935 m_vertScrollBar
->Show(TRUE
);
936 m_vertScrollBar
->SetScrollbar(
938 wxMax(m_wholeRowsVisible
, 1),
939 (m_wholeRowsVisible
== 0 ? 1 : m_numRows
),
940 wxMax(m_wholeRowsVisible
, 1) );
942 m_vertScrollBar
->SetSize( cw
- m_scrollBarWidth
,
945 ch
- m_top
- horizScrollBarHeight
);
949 if ( m_horizScrollBar
)
951 if ( !horizScrollBarHeight
)
953 m_horizScrollBar
->Show(FALSE
);
957 m_horizScrollBar
->Show(TRUE
);
959 m_horizScrollBar
->SetScrollbar(
961 wxMax(m_wholeColsVisible
, 1),
962 (m_wholeColsVisible
== 0) ? 1 : m_numCols
,
963 wxMax(m_wholeColsVisible
, 1) );
965 m_horizScrollBar
->SetSize( m_left
,
966 ch
- m_scrollBarWidth
,
967 cw
- m_left
- vertScrollBarWidth
,
972 m_bottom
= m_right
= 0;
975 m_bottom
= wxMin( m_rowBottoms
[m_numRows
-1],
976 ch
- horizScrollBarHeight
);
980 m_right
= wxMin( m_colRights
[m_numCols
-1],
981 cw
- vertScrollBarWidth
);
986 bool wxGrid::IsOnScreen()
989 GetClientSize( &cw
, &ch
);
994 // this is called when the grid table sends a message to say that it
995 // has been redimensioned
997 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1001 switch ( msg
.GetId() )
1003 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1005 size_t pos
= msg
.GetCommandInt();
1006 int numRows
= msg
.GetCommandInt2();
1007 for ( i
= 0; i
< numRows
; i
++ )
1009 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1010 m_rowBottoms
.Insert( 0, pos
);
1012 m_numRows
+= numRows
;
1017 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1019 int numRows
= msg
.GetCommandInt();
1020 for ( i
= 0; i
< numRows
; i
++ )
1022 m_rowHeights
.Add( m_defaultRowHeight
);
1023 m_rowBottoms
.Add( 0 );
1025 m_numRows
+= numRows
;
1030 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1032 size_t pos
= msg
.GetCommandInt();
1033 int numRows
= msg
.GetCommandInt2();
1034 for ( i
= 0; i
< numRows
; i
++ )
1036 m_rowHeights
.Remove( pos
);
1037 m_rowBottoms
.Remove( pos
);
1039 m_numRows
-= numRows
;
1041 // TODO: improve these adjustments...
1043 if ( m_scrollPosY
>= m_numRows
)
1049 m_colWidths
.Clear();
1050 m_colRights
.Clear();
1051 m_currentCellCoords
= wxGridNoCellCoords
;
1053 else if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1055 m_currentCellCoords
.Set( 0, 0 );
1061 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1063 size_t pos
= msg
.GetCommandInt();
1064 int numCols
= msg
.GetCommandInt2();
1065 for ( i
= 0; i
< numCols
; i
++ )
1067 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1068 m_colRights
.Insert( 0, pos
);
1070 m_numCols
+= numCols
;
1075 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1077 int numCols
= msg
.GetCommandInt();
1078 for ( i
= 0; i
< numCols
; i
++ )
1080 m_colWidths
.Add( m_defaultColWidth
);
1081 m_colRights
.Add( 0 );
1083 m_numCols
+= numCols
;
1088 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1090 size_t pos
= msg
.GetCommandInt();
1091 int numCols
= msg
.GetCommandInt2();
1092 for ( i
= 0; i
< numCols
; i
++ )
1094 m_colWidths
.Remove( pos
);
1095 m_colRights
.Remove( pos
);
1097 m_numCols
-= numCols
;
1099 // TODO: improve these adjustments...
1101 if ( m_scrollPosX
>= m_numCols
)
1106 #if 0 // leave the row alone here so that AppendCols will work subsequently
1108 m_rowHeights
.Clear();
1109 m_rowBottoms
.Clear();
1111 m_currentCellCoords
= wxGridNoCellCoords
;
1113 else if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1115 m_currentCellCoords
.Set( 0, 0 );
1127 // ----- event handlers
1130 // Generate a grid event based on a mouse event and
1131 // return the result of ProcessEvent()
1133 bool wxGrid::SendEvent( const wxEventType type
,
1135 wxMouseEvent
& mouseEv
)
1137 if ( type
== EVT_GRID_ROW_SIZE
||
1138 type
== EVT_GRID_COL_SIZE
)
1140 int rowOrCol
= (row
== -1 ? col
: row
);
1142 wxGridSizeEvent
gridEvt( GetId(),
1146 mouseEv
.GetX(), mouseEv
.GetY(),
1147 mouseEv
.ControlDown(),
1148 mouseEv
.ShiftDown(),
1150 mouseEv
.MetaDown() );
1152 return GetEventHandler()->ProcessEvent(gridEvt
);
1154 else if ( type
== EVT_GRID_RANGE_SELECT
)
1156 wxGridRangeSelectEvent
gridEvt( GetId(),
1160 m_selectedBottomRight
,
1161 mouseEv
.ControlDown(),
1162 mouseEv
.ShiftDown(),
1164 mouseEv
.MetaDown() );
1166 return GetEventHandler()->ProcessEvent(gridEvt
);
1170 wxGridEvent
gridEvt( GetId(),
1174 mouseEv
.GetX(), mouseEv
.GetY(),
1175 mouseEv
.ControlDown(),
1176 mouseEv
.ShiftDown(),
1178 mouseEv
.MetaDown() );
1180 return GetEventHandler()->ProcessEvent(gridEvt
);
1185 // Generate a grid event of specified type and return the result
1186 // of ProcessEvent().
1188 bool wxGrid::SendEvent( const wxEventType type
,
1191 if ( type
== EVT_GRID_ROW_SIZE
||
1192 type
== EVT_GRID_COL_SIZE
)
1194 int rowOrCol
= (row
== -1 ? col
: row
);
1196 wxGridSizeEvent
gridEvt( GetId(),
1201 return GetEventHandler()->ProcessEvent(gridEvt
);
1205 wxGridEvent
gridEvt( GetId(),
1210 return GetEventHandler()->ProcessEvent(gridEvt
);
1215 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(ev
) )
1217 wxPaintDC
dc( this );
1219 if ( !m_batchCount
)
1221 // define a clipping region to avoid painting over the scroll bars
1224 if ( m_vertScrollBar
&& m_vertScrollBar
->IsShown() )
1225 vs
= m_scrollBarWidth
;
1228 if ( m_horizScrollBar
&& m_horizScrollBar
->IsShown() )
1229 hs
= m_scrollBarWidth
;
1232 GetClientSize( &cw
, &ch
);
1233 dc
.SetClippingRegion( 0, 0, cw
- vs
, ch
- hs
);
1235 HideCurrentCellHighlight( dc
);
1237 DrawLabelAreas( dc
);
1238 DrawColLabels( dc
);
1239 DrawRowLabels( dc
);
1241 DrawGridLines( dc
);
1244 // TODO: something more elegant than this...
1248 if ( m_currentCellCoords
== wxGridNoCellCoords
)
1249 m_currentCellCoords
.Set(0, 0);
1251 SetEditControlValue();
1252 ShowCellEditControl();
1253 m_firstPaint
= FALSE
;
1256 ShowCurrentCellHighlight( dc
);
1258 dc
.DestroyClippingRegion();
1263 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(ev
) )
1265 if ( m_created
) CalcDimensions();
1269 void wxGrid::OnMouse( wxMouseEvent
& ev
)
1275 // ------------------------------------------------------------
1279 if ( ev
.Dragging() )
1281 m_isDragging
= TRUE
;
1283 if ( ev
.LeftIsDown() )
1285 switch( m_cursorMode
)
1287 case WXGRID_CURSOR_SELECT_CELL
:
1289 wxGridCellCoords cellCoords
;
1290 XYToCell( x
, y
, cellCoords
);
1291 if ( cellCoords
!= wxGridNoCellCoords
)
1293 if ( !IsSelection() )
1295 SelectBlock( cellCoords
, cellCoords
);
1297 else if ( !IsInSelection( cellCoords
) )
1299 SelectBlock( m_currentCellCoords
, cellCoords
);
1305 case WXGRID_CURSOR_RESIZE_ROW
:
1307 wxClientDC
dc(this);
1308 dc
.SetLogicalFunction(wxXOR
);
1309 if ( m_dragLastPos
>= 0 )
1311 dc
.DrawLine( m_left
, m_dragLastPos
,
1312 m_right
, m_dragLastPos
);
1314 dc
.DrawLine( m_left
, ev
.GetY(),
1315 m_right
, ev
.GetY());
1317 m_dragLastPos
= ev
.GetY();
1321 case WXGRID_CURSOR_RESIZE_COL
:
1323 wxClientDC
dc(this);
1324 dc
.SetLogicalFunction(wxINVERT
);
1325 if ( m_dragLastPos
>= 0 )
1327 dc
.DrawLine( m_dragLastPos
, m_top
,
1328 m_dragLastPos
, m_bottom
);
1330 dc
.DrawLine( ev
.GetX(), m_top
,
1331 ev
.GetX(), m_bottom
);
1333 m_dragLastPos
= ev
.GetX();
1337 case WXGRID_CURSOR_SELECT_ROW
:
1339 if ( (row
= YToRow( y
)) >= 0 &&
1340 !IsInSelection( row
, 0 ) )
1342 SelectRow( row
, TRUE
);
1347 case WXGRID_CURSOR_SELECT_COL
:
1349 if ( (col
= XToCol( x
)) >= 0 &&
1350 !IsInSelection( 0, col
) )
1352 SelectCol( col
, TRUE
);
1361 m_isDragging
= FALSE
;
1363 // ------------------------------------------------------------
1365 // Left mouse button down
1367 if ( ev
.LeftDown() )
1371 wxGridCellCoords cellCoords
;
1373 switch( XYToArea( x
, y
) )
1375 case WXGRID_ROWLABEL
:
1377 // don't send a label click event for a hit on the
1378 // edge of the row label - this is probably the user
1379 // wanting to resize the row
1381 if ( YToEdgeOfRow(y
) < 0 )
1384 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, col
, ev
) )
1386 SelectRow( row
, ev
.ShiftDown() );
1387 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1393 case WXGRID_COLLABEL
:
1395 // don't send a label click event for a hit on the
1396 // edge of the col label - this is probably the user
1397 // wanting to resize the col
1399 if ( XToEdgeOfCol(x
) < 0 )
1402 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, col
, ev
) )
1404 SelectCol( col
, ev
.ShiftDown() );
1405 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1411 case WXGRID_CORNERLABEL
:
1413 // leave both row and col as -1
1415 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, col
, ev
) )
1424 XYToCell( x
, y
, cellCoords
);
1425 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1426 cellCoords
.GetRow(),
1427 cellCoords
.GetCol(),
1430 MakeCellVisible( cellCoords
);
1431 SelectCell( cellCoords
);
1438 wxLogMessage( "outside grid area" );
1443 // ------------------------------------------------------------
1445 // Left mouse button double click
1447 else if ( ev
.LeftDClick() )
1451 wxGridCellCoords cellCoords
;
1453 switch( XYToArea( x
, y
) )
1455 case WXGRID_ROWLABEL
:
1457 // don't send a label click event for a hit on the
1458 // edge of the row label - this is probably the user
1459 // wanting to resize the row
1461 if ( YToEdgeOfRow(y
) < 0 )
1464 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, col
, ev
);
1469 case WXGRID_COLLABEL
:
1471 // don't send a label click event for a hit on the
1472 // edge of the col label - this is probably the user
1473 // wanting to resize the col
1475 if ( XToEdgeOfCol(x
) < 0 )
1478 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, col
, ev
);
1483 case WXGRID_CORNERLABEL
:
1485 // leave both row and col as -1
1487 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, col
, ev
);
1493 XYToCell( x
, y
, cellCoords
);
1494 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1495 cellCoords
.GetRow(),
1496 cellCoords
.GetCol(),
1503 wxLogMessage( "outside grid area" );
1508 // ------------------------------------------------------------
1510 // Left mouse button released
1512 else if ( ev
.LeftUp() )
1514 switch ( m_cursorMode
)
1516 case WXGRID_CURSOR_RESIZE_ROW
:
1518 if ( m_dragLastPos
>= 0 )
1520 // erase the last line and resize the row
1522 wxClientDC
dc( this );
1523 dc
.SetLogicalFunction( wxINVERT
);
1524 dc
.DrawLine( m_left
, m_dragLastPos
,
1525 m_right
, m_dragLastPos
);
1526 HideCellEditControl();
1527 int top
= m_top
+ m_colLabelHeight
;
1528 if ( m_dragRowOrCol
> 0 )
1529 top
= m_rowBottoms
[m_dragRowOrCol
-1];
1530 m_rowHeights
[m_dragRowOrCol
] = wxMax( ev
.GetY() - top
,
1531 WXGRID_MIN_ROW_HEIGHT
);
1533 ShowCellEditControl();
1536 // Note: we are ending the event *after* doing
1537 // default processing in this case
1539 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, ev
);
1544 case WXGRID_CURSOR_RESIZE_COL
:
1546 if ( m_dragLastPos
>= 0 )
1548 // erase the last line and resize the col
1550 wxClientDC
dc( this );
1551 dc
.SetLogicalFunction( wxINVERT
);
1552 dc
.DrawLine( m_left
, m_dragLastPos
,
1553 m_right
, m_dragLastPos
);
1554 HideCellEditControl();
1555 int left
= m_left
+ m_rowLabelWidth
;
1556 if ( m_dragRowOrCol
> 0 )
1557 left
= m_colRights
[m_dragRowOrCol
-1];
1558 m_colWidths
[m_dragRowOrCol
] = wxMax( ev
.GetX() - left
,
1559 WXGRID_MIN_COL_WIDTH
);
1561 ShowCellEditControl();
1564 // Note: we are ending the event *after* doing
1565 // default processing in this case
1567 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, ev
);
1572 case WXGRID_CURSOR_SELECT_CELL
:
1574 if ( IsSelection() )
1576 // Note: we are ending the event *after* doing
1577 // default processing in this case
1579 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, ev
);
1587 // ------------------------------------------------------------
1589 // Right mouse button down
1591 else if ( ev
.RightDown() )
1595 wxGridCellCoords cellCoords
;
1597 switch( XYToArea( x
, y
) )
1600 case WXGRID_ROWLABEL
:
1603 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, col
, ev
) )
1605 // TODO: default processing ?
1610 case WXGRID_COLLABEL
:
1613 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, col
, ev
) )
1615 // TODO: default processing ?
1620 case WXGRID_CORNERLABEL
:
1622 // leave both row and col as -1
1624 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, col
, ev
) )
1626 // TODO: default processing ?
1633 XYToCell( x
, y
, cellCoords
);
1634 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
1635 cellCoords
.GetRow(),
1636 cellCoords
.GetCol(),
1639 // TODO: default processing ?
1646 wxLogMessage( "outside grid area" );
1651 // ------------------------------------------------------------
1653 // Right mouse button double click
1655 else if ( ev
.RightDClick() )
1659 wxGridCellCoords cellCoords
;
1661 switch( XYToArea( x
, y
) )
1664 case WXGRID_ROWLABEL
:
1667 SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, col
, ev
);
1671 case WXGRID_COLLABEL
:
1674 SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, col
, ev
);
1678 case WXGRID_CORNERLABEL
:
1680 // leave both row and col as -1
1682 SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, col
, ev
);
1688 XYToCell( x
, y
, cellCoords
);
1689 SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
1690 cellCoords
.GetRow(),
1691 cellCoords
.GetCol(),
1698 wxLogMessage( "outside grid area" );
1703 // ------------------------------------------------------------
1705 // No buttons down and mouse moving
1707 else if ( ev
.Moving() )
1709 switch( XYToArea( x
, y
) )
1711 case WXGRID_ROWLABEL
:
1713 m_dragRowOrCol
= YToEdgeOfRow( y
);
1714 if ( m_dragRowOrCol
>= 0 )
1716 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1718 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1719 SetCursor( m_rowResizeCursor
);
1724 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1726 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1727 SetCursor( *wxSTANDARD_CURSOR
);
1733 case WXGRID_COLLABEL
:
1735 m_dragRowOrCol
= XToEdgeOfCol( x
);
1736 if ( m_dragRowOrCol
>= 0 )
1738 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1740 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1741 SetCursor( m_colResizeCursor
);
1746 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1748 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1749 SetCursor( *wxSTANDARD_CURSOR
);
1757 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1759 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1760 SetCursor( *wxSTANDARD_CURSOR
);
1769 void wxGrid::OnKeyDown( wxKeyEvent
& ev
)
1771 if ( m_inOnKeyDown
)
1773 // shouldn't be here - we are going round in circles...
1775 wxLogFatalError( "wxGrid::OnKeyDown called while alread active" );
1778 m_inOnKeyDown
= TRUE
;
1780 // propagate the event up and see if it gets processed
1782 wxWindow
*parent
= GetParent();
1783 wxKeyEvent
keyEvt( ev
);
1784 keyEvt
.SetEventObject( parent
);
1786 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
1788 // try local handlers
1790 switch ( ev
.KeyCode() )
1793 if ( ev
.ControlDown() )
1795 MoveCursorUpBlock();
1804 if ( ev
.ControlDown() )
1806 MoveCursorDownBlock();
1815 if ( ev
.ControlDown() )
1817 MoveCursorLeftBlock();
1826 if ( ev
.ControlDown() )
1828 MoveCursorRightBlock();
1841 if ( ev
.ControlDown() )
1843 MakeCellVisible( 0, 0 );
1853 if ( ev
.ControlDown() )
1855 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
1856 SelectCell( m_numRows
-1, m_numCols
-1 );
1873 // now try the cell edit control
1875 if ( IsCellEditControlEnabled() )
1877 ev
.SetEventObject( m_cellEditCtrl
);
1878 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( ev
);
1884 m_inOnKeyDown
= FALSE
;
1888 // Text updated in an edit control - either a text control or a
1891 void wxGrid::OnText( wxKeyEvent
& ev
)
1896 wxWindow
*ctrl
= (wxWindow
*)ev
.GetEventObject();
1898 if ( ctrl
== m_cellEditCtrl
&&
1899 IsTopEditControlEnabled() )
1901 // set the value of the top edit control
1903 switch ( m_editCtrlType
)
1905 case wxGRID_TEXTCTRL
:
1906 ((wxTextCtrl
*)m_topEditCtrl
)->
1907 SetValue(((wxTextCtrl
*)ctrl
)->GetValue());
1910 case wxGRID_COMBOBOX
:
1911 ((wxComboBox
*)m_topEditCtrl
)->
1912 SetValue(((wxComboBox
*)ctrl
)->GetValue());
1916 else if ( ctrl
== m_topEditCtrl
&&
1917 IsCellEditControlEnabled() )
1919 switch ( m_editCtrlType
)
1921 case wxGRID_TEXTCTRL
:
1922 ((wxTextCtrl
*)m_cellEditCtrl
)->
1923 SetValue(((wxTextCtrl
*)ctrl
)->GetValue());
1926 case wxGRID_COMBOBOX
:
1927 ((wxComboBox
*)m_cellEditCtrl
)->
1928 SetValue(((wxComboBox
*)ctrl
)->GetValue());
1937 void wxGrid::OnGridScroll( wxScrollEvent
& ev
)
1939 // propagate the event up and see if it gets processed
1941 wxWindow
*parent
= GetParent();
1942 wxScrollEvent
scrollEvt( ev
);
1943 if (parent
->GetEventHandler()->ProcessEvent( scrollEvt
)) return;
1945 HideCellEditControl();
1947 if ( ev
.GetEventObject() == m_horizScrollBar
)
1949 if ( ev
.GetPosition() != m_scrollPosX
)
1951 SetHorizontalScrollPos( ev
.GetPosition() );
1956 if ( ev
.GetPosition() != m_scrollPosY
)
1958 SetVerticalScrollPos( ev
.GetPosition() );
1962 ShowCellEditControl();
1966 void wxGrid::SelectCell( const wxGridCellCoords
& coords
)
1968 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
1970 // the event has been intercepted - do nothing
1974 wxClientDC
dc( this );
1976 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
1978 HideCurrentCellHighlight( dc
);
1979 HideCellEditControl();
1980 SaveEditControlValue();
1983 m_currentCellCoords
= coords
;
1985 SetEditControlValue();
1988 ShowCellEditControl();
1989 ShowCurrentCellHighlight( dc
);
1992 if ( IsSelection() )
1995 if ( !GetBatchCount() ) Refresh();
2000 void wxGrid::ShowCellEditControl()
2004 if ( IsCellEditControlEnabled() )
2006 if ( !IsVisible( m_currentCellCoords
) )
2012 rect
= CellToRect( m_currentCellCoords
);
2014 m_cellEditCtrl
->SetSize( rect
);
2015 m_cellEditCtrl
->Show( TRUE
);
2017 switch ( m_editCtrlType
)
2019 case wxGRID_TEXTCTRL
:
2020 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
2023 case wxGRID_CHECKBOX
:
2024 // TODO: anything ???
2029 // TODO: anything ???
2033 case wxGRID_COMBOBOX
:
2034 // TODO: anything ???
2039 m_cellEditCtrl
->SetFocus();
2045 void wxGrid::HideCellEditControl()
2047 if ( IsCellEditControlEnabled() )
2049 m_cellEditCtrl
->Show( FALSE
);
2053 void wxGrid::SetEditControlValue( const wxString
& value
)
2059 s
= GetCellValue(m_currentCellCoords
);
2063 if ( IsTopEditControlEnabled() )
2065 switch ( m_editCtrlType
)
2067 case wxGRID_TEXTCTRL
:
2068 ((wxGridTextCtrl
*)m_topEditCtrl
)->SetStartValue(s
);
2071 case wxGRID_CHECKBOX
:
2072 // TODO: implement this
2077 // TODO: implement this
2081 case wxGRID_COMBOBOX
:
2082 // TODO: implement this
2088 if ( IsCellEditControlEnabled() )
2090 switch ( m_editCtrlType
)
2092 case wxGRID_TEXTCTRL
:
2093 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
2096 case wxGRID_CHECKBOX
:
2097 // TODO: implement this
2102 // TODO: implement this
2106 case wxGRID_COMBOBOX
:
2107 // TODO: implement this
2115 void wxGrid::SaveEditControlValue()
2119 wxWindow
*ctrl
= (wxWindow
*)NULL
;
2121 if ( IsCellEditControlEnabled() )
2123 ctrl
= m_cellEditCtrl
;
2125 else if ( IsTopEditControlEnabled() )
2127 ctrl
= m_topEditCtrl
;
2134 bool valueChanged
= FALSE
;
2136 switch ( m_editCtrlType
)
2138 case wxGRID_TEXTCTRL
:
2139 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
2140 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
2141 SetCellValue( m_currentCellCoords
,
2142 ((wxTextCtrl
*) ctrl
)->GetValue() );
2145 case wxGRID_CHECKBOX
:
2146 // TODO: implement this
2151 // TODO: implement this
2155 case wxGRID_COMBOBOX
:
2156 // TODO: implement this
2163 SendEvent( EVT_GRID_CELL_CHANGE
,
2164 m_currentCellCoords
.GetRow(),
2165 m_currentCellCoords
.GetCol() );
2171 int wxGrid::XYToArea( int x
, int y
)
2173 if ( x
> m_left
&& x
< m_right
&&
2174 y
> m_top
&& y
< m_bottom
)
2176 if ( y
< m_top
+ m_colLabelHeight
)
2178 if ( x
> m_left
+ m_rowLabelWidth
)
2180 return WXGRID_COLLABEL
;
2184 return WXGRID_CORNERLABEL
;
2187 else if ( x
<= m_left
+ m_rowLabelWidth
)
2189 return WXGRID_ROWLABEL
;
2197 return WXGRID_NOAREA
;
2201 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
2203 coords
.SetRow( YToRow(y
) );
2204 coords
.SetCol( XToCol(x
) );
2208 int wxGrid::YToRow( int y
)
2212 if ( y
> m_top
+ m_colLabelHeight
)
2214 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
2216 if ( y
< m_rowBottoms
[i
] )
2227 int wxGrid::XToCol( int x
)
2231 if ( x
> m_left
+ m_rowLabelWidth
)
2233 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
2235 if ( x
< m_colRights
[i
] )
2246 // return the row number that that the y coord is near the edge of, or
2247 // -1 if not near an edge
2249 int wxGrid::YToEdgeOfRow( int y
)
2253 if ( y
> m_top
+ m_colLabelHeight
)
2255 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
2257 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
2259 d
= abs( y
- m_rowBottoms
[i
] );
2261 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
2272 // return the col number that that the x coord is near the edge of, or
2273 // -1 if not near an edge
2275 int wxGrid::XToEdgeOfCol( int x
)
2279 if ( x
> m_left
+ m_rowLabelWidth
)
2281 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
2283 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
2285 d
= abs( x
- m_colRights
[i
] );
2287 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
2298 wxRect
wxGrid::CellToRect( int row
, int col
)
2300 wxRect
rect( -1, -1, -1, -1 );
2302 if ( row
>= m_scrollPosY
&& col
>= m_scrollPosX
)
2304 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
2305 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2306 rect
.width
= m_colWidths
[col
];
2307 rect
.height
= m_rowHeights
[ row
];
2314 bool wxGrid::MoveCursorUp()
2316 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2317 m_currentCellCoords
.GetRow() > 0 )
2319 SelectCell( m_currentCellCoords
.GetRow() - 1,
2320 m_currentCellCoords
.GetCol() );
2322 if ( !IsVisible( m_currentCellCoords
) )
2323 MakeCellVisible( m_currentCellCoords
);
2331 bool wxGrid::MoveCursorDown()
2333 // TODO: allow for scrolling
2335 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2336 m_currentCellCoords
.GetRow() < m_numRows
-1 )
2338 SelectCell( m_currentCellCoords
.GetRow() + 1,
2339 m_currentCellCoords
.GetCol() );
2341 if ( !IsVisible( m_currentCellCoords
) )
2342 MakeCellVisible( m_currentCellCoords
);
2350 bool wxGrid::MoveCursorLeft()
2352 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2353 m_currentCellCoords
.GetCol() > 0 )
2355 SelectCell( m_currentCellCoords
.GetRow(),
2356 m_currentCellCoords
.GetCol() - 1 );
2358 if ( !IsVisible( m_currentCellCoords
) )
2359 MakeCellVisible( m_currentCellCoords
);
2367 bool wxGrid::MoveCursorRight()
2369 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2370 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
2372 SelectCell( m_currentCellCoords
.GetRow(),
2373 m_currentCellCoords
.GetCol() + 1 );
2375 if ( !IsVisible( m_currentCellCoords
) )
2376 MakeCellVisible( m_currentCellCoords
);
2384 bool wxGrid::MovePageUp()
2386 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2389 int row
= m_currentCellCoords
.GetRow();
2390 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
2393 if ( y
+ m_rowHeights
[row
-1] > m_bottom
) break;
2394 y
+= m_rowHeights
[ --row
];
2396 SetVerticalScrollPos( row
);
2398 SelectCell( row
, m_currentCellCoords
.GetCol() );
2405 bool wxGrid::MovePageDown()
2407 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
2408 m_scrollPosY
+ m_wholeRowsVisible
< m_numRows
)
2410 if ( m_wholeRowsVisible
> 0 )
2412 SetVerticalScrollPos( m_scrollPosY
+ m_wholeRowsVisible
);
2414 else if ( m_scrollPosY
< m_numRows
- 1 )
2416 SetVerticalScrollPos( m_scrollPosY
+ 1 );
2423 // m_scrollPosY will have been updated
2425 SelectCell( m_scrollPosY
, m_currentCellCoords
.GetCol() );
2432 bool wxGrid::MoveCursorUpBlock()
2435 m_currentCellCoords
!= wxGridNoCellCoords
&&
2436 m_currentCellCoords
.GetRow() > 0 )
2438 int row
= m_currentCellCoords
.GetRow();
2439 int col
= m_currentCellCoords
.GetCol();
2441 if ( m_table
->IsEmptyCell(row
, col
) )
2443 // starting in an empty cell: find the next block of
2449 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2452 else if ( m_table
->IsEmptyCell(row
-1, col
) )
2454 // starting at the top of a block: find the next block
2460 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2465 // starting within a block: find the top of the block
2470 if ( m_table
->IsEmptyCell(row
, col
) )
2478 SelectCell( row
, col
);
2480 if ( !IsVisible( m_currentCellCoords
) )
2481 MakeCellVisible( m_currentCellCoords
);
2489 bool wxGrid::MoveCursorDownBlock()
2492 m_currentCellCoords
!= wxGridNoCellCoords
&&
2493 m_currentCellCoords
.GetRow() < m_numRows
-1 )
2495 int row
= m_currentCellCoords
.GetRow();
2496 int col
= m_currentCellCoords
.GetCol();
2498 if ( m_table
->IsEmptyCell(row
, col
) )
2500 // starting in an empty cell: find the next block of
2503 while ( row
< m_numRows
-1 )
2506 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2509 else if ( m_table
->IsEmptyCell(row
+1, col
) )
2511 // starting at the bottom of a block: find the next block
2514 while ( row
< m_numRows
-1 )
2517 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2522 // starting within a block: find the bottom of the block
2524 while ( row
< m_numRows
-1 )
2527 if ( m_table
->IsEmptyCell(row
, col
) )
2535 SelectCell( row
, col
);
2537 if ( !IsVisible( m_currentCellCoords
) )
2538 MakeCellVisible( m_currentCellCoords
);
2546 bool wxGrid::MoveCursorLeftBlock()
2549 m_currentCellCoords
!= wxGridNoCellCoords
&&
2550 m_currentCellCoords
.GetCol() > 0 )
2552 int row
= m_currentCellCoords
.GetRow();
2553 int col
= m_currentCellCoords
.GetCol();
2555 if ( m_table
->IsEmptyCell(row
, col
) )
2557 // starting in an empty cell: find the next block of
2563 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2566 else if ( m_table
->IsEmptyCell(row
, col
-1) )
2568 // starting at the left of a block: find the next block
2574 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2579 // starting within a block: find the left of the block
2584 if ( m_table
->IsEmptyCell(row
, col
) )
2592 SelectCell( row
, col
);
2594 if ( !IsVisible( m_currentCellCoords
) )
2595 MakeCellVisible( m_currentCellCoords
);
2603 bool wxGrid::MoveCursorRightBlock()
2606 m_currentCellCoords
!= wxGridNoCellCoords
&&
2607 m_currentCellCoords
.GetCol() < m_numCols
-1 )
2609 int row
= m_currentCellCoords
.GetRow();
2610 int col
= m_currentCellCoords
.GetCol();
2612 if ( m_table
->IsEmptyCell(row
, col
) )
2614 // starting in an empty cell: find the next block of
2617 while ( col
< m_numCols
-1 )
2620 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2623 else if ( m_table
->IsEmptyCell(row
, col
+1) )
2625 // starting at the right of a block: find the next block
2628 while ( col
< m_numCols
-1 )
2631 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
2636 // starting within a block: find the right of the block
2638 while ( col
< m_numCols
-1 )
2641 if ( m_table
->IsEmptyCell(row
, col
) )
2649 SelectCell( row
, col
);
2651 if ( !IsVisible( m_currentCellCoords
) )
2652 MakeCellVisible( m_currentCellCoords
);
2663 // ----- grid drawing functions
2666 void wxGrid::DrawLabelAreas( wxDC
& dc
)
2669 GetClientSize(&cw
, &ch
);
2671 dc
.SetPen(*wxTRANSPARENT_PEN
);
2672 dc
.SetBrush( wxBrush(GetLabelBackgroundColour(), wxSOLID
) );
2674 dc
.DrawRectangle( m_left
, m_top
,
2675 cw
- m_left
, m_colLabelHeight
);
2677 dc
.DrawRectangle( m_left
, m_top
,
2678 m_rowLabelWidth
, ch
- m_top
);
2682 void wxGrid::DrawColLabels( wxDC
& dc
)
2685 GetClientSize(&cw
, &ch
);
2687 if (m_colLabelHeight
== 0) return;
2689 DrawColLabelBorders( dc
);
2693 rect
.height
= m_colLabelHeight
- 1;
2695 int labelLeft
= m_left
+ m_rowLabelWidth
;
2698 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
2700 if ( labelLeft
> cw
) break;
2702 rect
.x
= 1 + labelLeft
;
2703 rect
.width
= m_colWidths
[i
];
2704 DrawColLabel( dc
, rect
, i
);
2706 labelLeft
+= m_colWidths
[i
];
2711 void wxGrid::DrawColLabelBorders( wxDC
& dc
)
2713 if ( m_colLabelHeight
<= 0 ) return;
2717 GetClientSize( &cw
, &ch
);
2719 dc
.SetPen( *wxBLACK_PEN
);
2723 dc
.DrawLine( m_left
, m_top
, cw
, m_top
);
2725 dc
.DrawLine( m_left
, m_top
+ m_colLabelHeight
,
2726 cw
, m_top
+ m_colLabelHeight
);
2730 int colLeft
= m_left
+ m_rowLabelWidth
;
2731 for ( i
= m_scrollPosX
; i
<= m_numCols
; i
++ )
2733 if (colLeft
> cw
) break;
2735 dc
.DrawLine( colLeft
, m_top
,
2736 colLeft
, m_top
+ m_colLabelHeight
);
2738 if ( i
< m_numCols
) colLeft
+= m_colWidths
[i
];
2741 // Draw white highlights for a 3d effect
2743 dc
.SetPen( *wxWHITE_PEN
);
2745 colLeft
= m_left
+ m_rowLabelWidth
;
2746 for ( i
= m_scrollPosX
; i
< m_numCols
; i
++ )
2748 if (colLeft
> cw
) break;
2750 dc
.DrawLine(colLeft
+ 1, m_top
+ 1,
2751 colLeft
+ m_colWidths
[i
], m_top
+ 1);
2753 dc
.DrawLine(colLeft
+ 1, m_top
+ 1,
2754 colLeft
+ 1, m_top
+ m_colLabelHeight
);
2756 colLeft
+= m_colWidths
[i
];
2761 void wxGrid::DrawColLabel( wxDC
& dc
, const wxRect
& rect
, int col
)
2770 dc
.SetBackgroundMode( wxTRANSPARENT
);
2771 dc
.SetTextBackground( GetLabelBackgroundColour() );
2772 dc
.SetTextForeground( GetLabelTextColour() );
2773 dc
.SetFont( GetLabelFont() );
2776 GetColLabelAlignment( &hAlign
, &vAlign
);
2777 DrawTextRectangle( dc
, GetColLabelValue( col
), rect2
, hAlign
, vAlign
);
2781 void wxGrid::DrawRowLabels( wxDC
& dc
)
2784 GetClientSize(&cw
, &ch
);
2786 if (m_rowLabelWidth
== 0) return;
2788 DrawRowLabelBorders( dc
);
2791 rect
.x
= m_left
+ 1;
2792 rect
.width
= m_rowLabelWidth
- 1;
2794 int labelTop
= m_top
+ m_colLabelHeight
;
2797 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
2799 if ( labelTop
> ch
) break;
2801 rect
.y
= 1 + labelTop
;
2802 rect
.height
= m_rowHeights
[i
];
2803 DrawRowLabel( dc
, rect
, i
);
2805 labelTop
+= m_rowHeights
[i
];
2810 void wxGrid::DrawRowLabelBorders( wxDC
& dc
)
2812 if ( m_rowLabelWidth
<= 0 ) return;
2816 GetClientSize( &cw
, &ch
);
2818 dc
.SetPen( *wxBLACK_PEN
);
2822 dc
.DrawLine( m_left
, m_top
, m_left
, ch
);
2824 dc
.DrawLine( m_left
+ m_rowLabelWidth
, m_top
,
2825 m_left
+ m_rowLabelWidth
, ch
);
2829 int rowTop
= m_top
+ m_colLabelHeight
;
2830 for ( i
= m_scrollPosY
; i
<= m_numRows
; i
++ )
2832 if ( rowTop
> ch
) break;
2834 dc
.DrawLine( m_left
, rowTop
,
2835 m_left
+ m_rowLabelWidth
, rowTop
);
2837 if ( i
< m_numRows
) rowTop
+= m_rowHeights
[i
];
2840 // Draw white highlights for a 3d effect
2842 dc
.SetPen( *wxWHITE_PEN
);
2844 rowTop
= m_top
+ m_colLabelHeight
;
2845 for ( i
= m_scrollPosY
; i
< m_numRows
; i
++ )
2847 if ( rowTop
> ch
) break;
2849 dc
.DrawLine( m_left
+ 1, rowTop
+ 1,
2850 m_left
+ m_rowLabelWidth
, rowTop
+ 1 );
2852 dc
.DrawLine( m_left
+ 1, rowTop
+ 1,
2853 m_left
+ 1, rowTop
+ m_rowHeights
[i
] );
2855 rowTop
+= m_rowHeights
[i
];
2860 void wxGrid::DrawRowLabel( wxDC
& dc
, const wxRect
& rect
, int row
)
2869 dc
.SetBackgroundMode( wxTRANSPARENT
);
2870 dc
.SetTextBackground( GetLabelBackgroundColour() );
2871 dc
.SetTextForeground( GetLabelTextColour() );
2872 dc
.SetFont( GetLabelFont() );
2875 GetRowLabelAlignment( &hAlign
, &vAlign
);
2876 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect2
, hAlign
, vAlign
);
2880 void wxGrid::DrawCellArea( wxDC
& dc
)
2883 GetClientSize(&cw
, &ch
);
2885 dc
.SetPen( *wxTRANSPARENT_PEN
);
2886 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
2888 int left
= m_left
+ m_rowLabelWidth
+ 1;
2889 int top
= m_top
+ m_colLabelHeight
+ 1;
2891 dc
.DrawRectangle( left
, top
, cw
- left
, ch
- top
);
2895 void wxGrid::DrawGridLines( wxDC
& dc
)
2897 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
) return;
2901 GetClientSize(&cw
, &ch
);
2903 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2905 // horizontal grid lines
2907 int rowTop
= m_top
+ m_colLabelHeight
+ m_rowHeights
[m_scrollPosY
];
2908 for ( i
= m_scrollPosY
+ 1; i
<= m_numRows
; i
++ )
2910 if ( rowTop
> ch
) break;
2912 dc
.DrawLine( m_left
+ m_rowLabelWidth
+ 1, rowTop
,
2915 if ( i
< m_numRows
) rowTop
+= m_rowHeights
[i
];
2919 // vertical grid lines
2921 int colLeft
= m_left
+ m_rowLabelWidth
+ m_colWidths
[m_scrollPosX
];
2922 for ( i
= m_scrollPosX
+ 1; i
<= m_numCols
; i
++ )
2924 if ( colLeft
> cw
) break;
2926 dc
.DrawLine( colLeft
, m_top
+ m_colLabelHeight
+ 1,
2927 colLeft
, m_bottom
);
2929 if ( i
< m_numCols
) colLeft
+= m_colWidths
[i
];
2934 void wxGrid::DrawCells( wxDC
& dc
)
2936 if ( !m_numRows
|| !m_numCols
) return;
2941 GetClientSize( &cw
, &ch
);
2947 rect
.y
= m_top
+ m_colLabelHeight
;
2948 for ( row
= m_scrollPosY
; row
< m_numRows
; row
++ )
2950 if ( rect
.y
> ch
) break;
2952 rect
.height
= m_rowHeights
[ row
];
2953 rect
.x
= m_left
+ m_rowLabelWidth
;
2955 for ( col
= m_scrollPosX
; col
< m_numCols
; col
++ )
2957 if ( rect
.x
> cw
) break;
2959 rect
.width
= m_colWidths
[col
];
2960 DrawCellBackground( dc
, rect
, row
, col
);
2961 DrawCellValue( dc
, rect
, row
, col
);
2962 rect
.x
+= rect
.width
;
2964 rect
.y
+= rect
.height
;
2970 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxRect
& rect
, int row
, int col
)
2979 dc
.SetBackgroundMode( wxSOLID
);
2981 if ( IsInSelection( row
, col
) )
2983 // TODO: improve this
2985 dc
.SetBrush( *wxBLACK_BRUSH
);
2989 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2991 dc
.SetPen( *wxTRANSPARENT_PEN
);
2992 dc
.DrawRectangle( rect2
);
2996 void wxGrid::DrawCellValue( wxDC
& dc
, const wxRect
& rect
, int row
, int col
)
3005 dc
.SetBackgroundMode( wxTRANSPARENT
);
3007 if ( IsInSelection( row
, col
) )
3009 // TODO: improve this
3011 dc
.SetTextBackground( wxColour(0, 0, 0) );
3012 dc
.SetTextForeground( wxColour(255, 255, 255) );
3016 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
3017 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
3019 dc
.SetFont( GetCellFont(row
, col
) );
3022 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
3023 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect2
, hAlign
, vAlign
);
3027 void wxGrid::DrawCellHighlight( wxDC
& dc
, int row
, int col
)
3029 // TODO: bounds checking on row, col ?
3032 if ( row
>= m_scrollPosY
&& col
>= m_scrollPosX
)
3037 GetClientSize( &cw
, &ch
);
3039 x
= m_colRights
[col
] - m_colWidths
[col
];
3040 if ( x
>= cw
) return;
3042 y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3043 if ( y
>= ch
) return;
3045 dc
.SetLogicalFunction( wxINVERT
);
3046 dc
.SetPen( wxPen(GetCellHighlightColour(), 2, wxSOLID
) );
3047 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
3049 dc
.DrawRectangle( x
-2, y
-2,
3050 m_colWidths
[col
] + 6,
3051 m_rowHeights
[row
] + 6 );
3053 dc
.SetLogicalFunction( wxCOPY
);
3058 // This function is handy when you just want to update one or a few
3059 // cells. For example, it is used by SetCellValue()
3061 void wxGrid::DrawCell( int row
, int col
)
3063 if ( !GetBatchCount() )
3065 if ( !IsVisible( wxGridCellCoords(row
, col
) ) ) return;
3068 GetClientSize( &cw
, &ch
);
3070 wxRect
rect( CellToRect( row
, col
) );
3074 wxClientDC
dc( this );
3075 DrawCellBackground( dc
, rect
, row
, col
);
3076 DrawCellValue( dc
, rect
, row
, col
);
3082 // this is just to make other code more obvious
3084 void wxGrid::HideCurrentCellHighlight( wxDC
& dc
)
3086 if ( !m_cellEditCtrlEnabled
&&
3087 m_currentCellHighlighted
&&
3088 m_currentCellCoords
!= wxGridNoCellCoords
)
3090 DrawCellHighlight( dc
, m_currentCellCoords
);
3091 m_currentCellHighlighted
= FALSE
;
3096 // this is just to make other code more obvious
3098 void wxGrid::ShowCurrentCellHighlight( wxDC
& dc
)
3100 if ( !m_cellEditCtrlEnabled
&&
3101 !m_currentCellHighlighted
&&
3102 m_currentCellCoords
!= wxGridNoCellCoords
)
3104 DrawCellHighlight( dc
, m_currentCellCoords
);
3105 m_currentCellHighlighted
= TRUE
;
3110 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3111 const wxString
& value
,
3116 long textWidth
, textHeight
;
3117 long lineWidth
, lineHeight
;
3118 wxArrayString lines
;
3120 // see if we are already clipping
3123 dc
.GetClippingBox( clipRect
);
3125 bool alreadyClipping
= TRUE
;
3126 wxRect intersectRect
;
3128 if ( clipRect
.x
== 0 && clipRect
.y
== 0 &&
3129 clipRect
.width
== 0 && clipRect
.height
== 0)
3131 alreadyClipping
= FALSE
;
3132 intersectRect
= rect
;
3136 // Find the intersection of the clipping rectangle and our
3139 wxRegion
region( rect
);
3140 region
.Intersect( clipRect
);
3141 if ( region
.IsEmpty() )
3147 intersectRect
= region
.GetBox();
3150 if ( alreadyClipping
) dc
.DestroyClippingRegion();
3152 dc
.SetClippingRegion( intersectRect
);
3154 StringToLines( value
, lines
);
3155 if ( lines
.GetCount() )
3157 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3158 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3161 switch ( horizAlign
)
3164 x
= rect
.x
+ (rect
.width
- textWidth
- 1.0);
3168 x
= rect
.x
+ ((rect
.width
- textWidth
)/2.0);
3177 switch ( vertAlign
)
3180 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3184 y
= rect
.y
+ ((rect
.height
- textHeight
)/2.0);
3193 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3195 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3200 dc
.DestroyClippingRegion();
3201 if (alreadyClipping
) dc
.SetClippingRegion( clipRect
);
3205 // Split multi line text up into an array of strings. Any existing
3206 // contents of the string array are preserved.
3208 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3210 // TODO: this won't work for WXMAC ? (lines end with '\r')
3211 // => use wxTextFile functions then (VZ)
3214 while ( startPos
< (int)value
.Length() )
3216 pos
= value
.Mid(startPos
).Find( '\n' );
3221 else if ( pos
== 0 )
3223 lines
.Add( wxEmptyString
);
3227 if ( value
[startPos
+pos
-1] == '\r' )
3229 lines
.Add( value
.Mid(startPos
, pos
-1) );
3233 lines
.Add( value
.Mid(startPos
, pos
) );
3238 if ( startPos
< (int)value
.Length() )
3240 lines
.Add( value
.Mid( startPos
) );
3245 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3246 wxArrayString
& lines
,
3247 long *width
, long *height
)
3254 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3256 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3257 w
= wxMax( w
, lineW
);
3267 // ------ functions to get/send data (see also public functions)
3270 bool wxGrid::GetModelValues()
3274 // all we need to do is repaint the grid
3284 bool wxGrid::SetModelValues()
3290 for ( row
= m_scrollPosY
; row
< m_numRows
; row
++ )
3292 for ( col
= m_scrollPosX
; col
< m_numCols
; col
++ )
3294 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3306 // ------ public functions
3309 bool wxGrid::CreateGrid( int numRows
, int numCols
)
3313 wxLogError( "wxGrid::CreateGrid(numRows, numCols) called more than once" );
3318 m_numRows
= numRows
;
3319 m_numCols
= numCols
;
3321 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3322 m_table
->SetView( this );
3331 // The behaviour of this function depends on the grid table class
3332 // Clear() function. For the default wxGridStringTable class the
3333 // behavious is to replace all cell contents with wxEmptyString but
3334 // not to change the number of rows or cols.
3336 void wxGrid::ClearGrid()
3341 SetEditControlValue();
3342 if ( !GetBatchCount() ) Refresh();
3347 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3349 // TODO: something with updateLabels flag
3353 wxLogError( "Called wxGrid::InsertRows() before calling CreateGrid()" );
3359 bool ok
= m_table
->InsertRows( pos
, numRows
);
3361 // the table will have sent the results of the insert row
3362 // operation to this view object as a grid table message
3366 if ( m_numCols
== 0 )
3368 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3370 // TODO: perhaps instead of appending the default number of cols
3371 // we should remember what the last non-zero number of cols was ?
3375 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3377 // if we have just inserted cols into an empty grid the current
3378 // cell will be undefined...
3383 if ( !GetBatchCount() ) Refresh();
3386 SetEditControlValue();
3395 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3397 // TODO: something with updateLabels flag
3401 wxLogError( "Called wxGrid::AppendRows() before calling CreateGrid()" );
3405 if ( m_table
&& m_table
->AppendRows( numRows
) )
3407 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3409 // if we have just inserted cols into an empty grid the current
3410 // cell will be undefined...
3415 // the table will have sent the results of the append row
3416 // operation to this view object as a grid table message
3418 if ( !GetBatchCount() ) Refresh();
3427 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3429 // TODO: something with updateLabels flag
3433 wxLogError( "Called wxGrid::DeleteRows() before calling CreateGrid()" );
3437 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3439 // the table will have sent the results of the delete row
3440 // operation to this view object as a grid table message
3442 if ( m_numRows
> 0 )
3443 SetEditControlValue();
3445 HideCellEditControl();
3447 if ( !GetBatchCount() ) Refresh();
3456 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3458 // TODO: something with updateLabels flag
3462 wxLogError( "Called wxGrid::InsertCols() before calling CreateGrid()" );
3468 HideCellEditControl();
3469 bool ok
= m_table
->InsertCols( pos
, numCols
);
3471 // the table will have sent the results of the insert col
3472 // operation to this view object as a grid table message
3476 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3478 // if we have just inserted cols into an empty grid the current
3479 // cell will be undefined...
3484 if ( !GetBatchCount() ) Refresh();
3487 SetEditControlValue();
3496 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3498 // TODO: something with updateLabels flag
3502 wxLogError( "Called wxGrid::AppendCols() before calling CreateGrid()" );
3506 if ( m_table
&& m_table
->AppendCols( numCols
) )
3508 // the table will have sent the results of the append col
3509 // operation to this view object as a grid table message
3511 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3513 // if we have just inserted cols into an empty grid the current
3514 // cell will be undefined...
3518 if ( !GetBatchCount() ) Refresh();
3527 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3529 // TODO: something with updateLabels flag
3533 wxLogError( "Called wxGrid::DeleteCols() before calling CreateGrid()" );
3537 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3539 // the table will have sent the results of the delete col
3540 // operation to this view object as a grid table message
3542 if ( m_numCols
> 0 )
3543 SetEditControlValue();
3545 HideCellEditControl();
3547 if ( !GetBatchCount() ) Refresh();
3559 // ------ control panel and cell edit control
3562 void wxGrid::EnableEditing( bool edit
)
3564 // TODO: improve this ?
3566 if ( edit
!= m_editable
)
3569 if ( !m_editable
) HideCellEditControl();
3570 m_topEditCtrlEnabled
= m_editable
;
3571 m_cellEditCtrlEnabled
= m_editable
;
3572 if ( !m_editable
) ShowCellEditControl();
3577 void wxGrid::EnableTopEditControl( bool enable
)
3579 if ( enable
!= m_topEditCtrlEnabled
)
3581 HideCellEditControl();
3582 m_topEditCtrlEnabled
= enable
;
3584 m_topEditCtrl
->Show( enable
);
3586 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
3587 SetEditControlValue();
3589 ShowCellEditControl();
3590 if ( !GetBatchCount() ) Refresh();
3594 void wxGrid::EnableCellEditControl( bool enable
)
3596 if ( m_cellEditCtrl
&&
3597 enable
!= m_cellEditCtrlEnabled
)
3599 wxClientDC
dc( this );
3601 HideCurrentCellHighlight( dc
);
3602 HideCellEditControl();
3603 SaveEditControlValue();
3605 m_cellEditCtrlEnabled
= enable
;
3607 SetEditControlValue();
3608 ShowCellEditControl();
3609 ShowCurrentCellHighlight( dc
);
3615 // ------ grid formatting functions
3618 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3620 *horiz
= m_rowLabelHorizAlign
;
3621 *vert
= m_rowLabelVertAlign
;
3624 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3626 *horiz
= m_colLabelHorizAlign
;
3627 *vert
= m_colLabelVertAlign
;
3630 wxString
wxGrid::GetRowLabelValue( int row
)
3634 return m_table
->GetRowLabelValue( row
);
3644 wxString
wxGrid::GetColLabelValue( int col
)
3648 return m_table
->GetColLabelValue( col
);
3658 void wxGrid::SetRowLabelSize( int width
)
3660 m_rowLabelWidth
= wxMax( 0, width
);
3662 ShowCellEditControl();
3663 if ( !GetBatchCount() ) Refresh();
3666 void wxGrid::SetColLabelSize( int height
)
3668 m_colLabelHeight
= wxMax( 0, height
);
3670 ShowCellEditControl();
3671 if ( !GetBatchCount() ) Refresh();
3674 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3676 m_labelBackgroundColour
= colour
;
3677 if ( !GetBatchCount() ) Refresh();
3680 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3682 m_labelTextColour
= colour
;
3683 if ( !GetBatchCount() ) Refresh();
3686 void wxGrid::SetLabelFont( const wxFont
& font
)
3689 if ( !GetBatchCount() ) Refresh();
3692 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3694 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3696 m_rowLabelHorizAlign
= horiz
;
3699 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3701 m_rowLabelVertAlign
= vert
;
3704 if ( !GetBatchCount() ) Refresh();
3707 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
3709 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3711 m_colLabelHorizAlign
= horiz
;
3714 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3716 m_colLabelVertAlign
= vert
;
3719 if ( !GetBatchCount() ) Refresh();
3722 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
3726 m_table
->SetRowLabelValue( row
, s
);
3727 if ( !GetBatchCount() ) Refresh();
3731 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
3735 m_table
->SetColLabelValue( col
, s
);
3736 if ( !GetBatchCount() ) Refresh();
3740 void wxGrid::SetGridLineColour( const wxColour
& colour
)
3742 m_gridLineColour
= colour
;
3744 wxClientDC
dc( this );
3745 DrawGridLines( dc
);
3748 void wxGrid::EnableGridLines( bool enable
)
3750 if ( enable
!= m_gridLinesEnabled
)
3752 m_gridLinesEnabled
= enable
;
3753 if ( !GetBatchCount() ) Refresh();
3758 int wxGrid::GetDefaultRowSize()
3760 return m_defaultRowHeight
;
3763 int wxGrid::GetRowSize( int row
)
3765 if ( row
>= 0 && row
< m_numRows
)
3766 return m_rowHeights
[row
];
3768 return 0; // TODO: log an error here
3771 int wxGrid::GetDefaultColSize()
3773 return m_defaultColWidth
;
3776 int wxGrid::GetColSize( int col
)
3778 if ( col
>= 0 && col
< m_numCols
)
3779 return m_colWidths
[col
];
3781 return 0; // TODO: log an error here
3784 wxColour
wxGrid::GetDefaultCellBackgroundColour()
3786 // TODO: replace this temp test code
3788 return wxColour( 255, 255, 255 );
3791 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
3793 // TODO: replace this temp test code
3795 return wxColour( 255, 255, 255 );
3798 wxColour
wxGrid::GetDefaultCellTextColour()
3800 // TODO: replace this temp test code
3802 return wxColour( 0, 0, 0 );
3805 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
3807 // TODO: replace this temp test code
3809 return wxColour( 0, 0, 0 );
3813 wxColour
wxGrid::GetCellHighlightColour()
3815 // TODO: replace this temp test code
3817 return wxColour( 0, 0, 0 );
3821 wxFont
wxGrid::GetDefaultCellFont()
3823 return m_defaultCellFont
;
3826 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
3828 // TODO: replace this temp test code
3830 return m_defaultCellFont
;
3833 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
3835 // TODO: replace this temp test code
3841 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
3843 // TODO: replace this temp test code
3849 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
3851 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
3853 if ( resizeExistingRows
)
3855 // TODO: what do we do about events here ?
3856 // Generate an event for each resize ?
3859 for ( row
= 0; row
< m_numRows
; row
++ )
3861 m_rowHeights
[row
] = m_defaultRowHeight
;
3864 if ( !GetBatchCount() ) Refresh();
3868 void wxGrid::SetRowSize( int row
, int height
)
3870 if ( row
>= 0 && row
< m_numRows
)
3872 m_rowHeights
[row
] = wxMax( 0, height
);
3874 if ( !GetBatchCount() ) Refresh();
3876 // Note: we are ending the event *after* doing
3877 // default processing in this case
3879 SendEvent( EVT_GRID_ROW_SIZE
,
3884 // TODO: log an error here
3888 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
3890 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
3892 if ( resizeExistingCols
)
3894 // TODO: what do we do about events here ?
3895 // Generate an event for each resize ?
3898 for ( col
= 0; col
< m_numCols
; col
++ )
3900 m_colWidths
[col
] = m_defaultColWidth
;
3903 if ( !GetBatchCount() ) Refresh();
3907 void wxGrid::SetColSize( int col
, int width
)
3909 if ( col
>= 0 && col
< m_numCols
)
3911 m_colWidths
[col
] = wxMax( 0, width
);
3913 if ( !GetBatchCount() ) Refresh();
3915 // Note: we are ending the event *after* doing
3916 // default processing in this case
3918 SendEvent( EVT_GRID_COL_SIZE
,
3923 // TODO: log an error here
3927 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
3929 // TODO: everything !!!
3933 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
3935 // TODO: everything !!!
3939 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
3941 // TODO: everything !!!
3945 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
3947 // TODO: everything !!!
3951 void wxGrid::SetCellHighlightColour( const wxColour
& )
3953 // TODO: everything !!!
3957 void wxGrid::SetDefaultCellFont( const wxFont
& )
3959 // TODO: everything !!!
3963 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
3965 // TODO: everything !!!
3969 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
3971 // TODO: everything !!!
3975 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
3977 // TODO: everything !!!
3983 // ------ cell value accessor functions
3986 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
3990 m_table
->SetValue( row
, col
, s
.c_str() );
3991 DrawCell( row
, col
);
3992 if ( m_currentCellCoords
.GetRow() == row
&&
3993 m_currentCellCoords
.GetCol() == col
)
3995 SetEditControlValue( s
);
4003 // ------ interaction with data model
4005 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4007 switch ( msg
.GetId() )
4009 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4010 return GetModelValues();
4012 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4013 return SetModelValues();
4015 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4016 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4017 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4018 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4019 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4020 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4021 return Redimension( msg
);
4030 // ------ Grid location functions
4032 // (see also inline functions in grid.h)
4035 // check to see if a cell location is wholly visible
4037 bool wxGrid::IsVisible( const wxGridCellCoords
& coords
)
4039 return ( coords
.GetRow() >= m_scrollPosY
&&
4040 coords
.GetRow() < m_scrollPosY
+ m_wholeRowsVisible
&&
4041 coords
.GetCol() >= m_scrollPosX
&&
4042 coords
.GetCol() < m_scrollPosX
+ m_wholeColsVisible
);
4046 // make the specified cell location visible by doing a minimal amount
4049 void wxGrid::MakeCellVisible( int row
, int col
)
4051 int lastX
= m_scrollPosX
;
4052 int lastY
= m_scrollPosY
;
4054 if ( row
>= 0 && row
< m_numRows
&&
4055 col
>= 0 && col
< m_numCols
)
4057 if ( row
< m_scrollPosY
)
4059 SetVerticalScrollPos( row
);
4061 else if ( row
>= m_scrollPosY
+ m_wholeRowsVisible
)
4064 int h
= m_rowBottoms
[row
];
4065 for ( i
= m_scrollPosY
; i
< m_numRows
&& h
> m_bottom
; i
++ )
4067 h
-= m_rowHeights
[i
];
4069 SetVerticalScrollPos( i
);
4072 if ( col
< m_scrollPosX
)
4074 SetHorizontalScrollPos( col
);
4076 else if ( col
>= m_scrollPosX
+ m_wholeColsVisible
)
4079 int w
= m_colRights
[col
];
4080 for ( i
= m_scrollPosX
; i
< m_numCols
&& w
> m_right
; i
++ )
4082 w
-= m_colWidths
[i
];
4084 SetHorizontalScrollPos( i
);
4087 if ( m_scrollPosX
!= lastX
|| m_scrollPosY
!= lastY
)
4089 // The cell was not visible before but not it is
4091 ShowCellEditControl();
4096 // TODO: log an error
4101 void wxGrid::SetVerticalScrollPos( int topMostRow
)
4103 if ( m_vertScrollBar
&& topMostRow
!= m_scrollPosY
)
4105 m_scrollPosY
= topMostRow
;
4113 void wxGrid::SetHorizontalScrollPos( int leftMostCol
)
4115 if ( m_horizScrollBar
&& leftMostCol
!= m_scrollPosX
)
4117 m_scrollPosX
= leftMostCol
;
4126 // ------ block, row and col selection
4129 void wxGrid::SelectRow( int row
, bool addToSelected
)
4131 if ( IsSelection() && addToSelected
)
4133 if ( m_selectedTopLeft
.GetRow() > row
)
4134 m_selectedTopLeft
.SetRow( row
);
4136 m_selectedTopLeft
.SetCol( 0 );
4138 if ( m_selectedBottomRight
.GetRow() < row
)
4139 m_selectedBottomRight
.SetRow( row
);
4141 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4146 m_selectedTopLeft
.Set( row
, 0 );
4147 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4150 if ( !GetBatchCount() )
4152 wxRect
rect( SelectionToRect() );
4153 if ( rect
!= wxGridNoCellRect
) Refresh( TRUE
, &rect
);
4156 wxGridRangeSelectEvent
gridEvt( GetId(),
4157 EVT_GRID_RANGE_SELECT
,
4160 m_selectedBottomRight
);
4162 GetEventHandler()->ProcessEvent(gridEvt
);
4166 void wxGrid::SelectCol( int col
, bool addToSelected
)
4168 if ( addToSelected
&& m_selectedTopLeft
!= wxGridNoCellCoords
)
4170 if ( m_selectedTopLeft
.GetCol() > col
)
4171 m_selectedTopLeft
.SetCol( col
);
4173 m_selectedTopLeft
.SetRow( 0 );
4175 if ( m_selectedBottomRight
.GetCol() < col
)
4176 m_selectedBottomRight
.SetCol( col
);
4178 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4183 m_selectedTopLeft
.Set( 0, col
);
4184 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4187 if ( !GetBatchCount() )
4189 wxRect
rect( SelectionToRect() );
4190 if ( rect
!= wxGridNoCellRect
) Refresh( TRUE
, &rect
);
4193 wxGridRangeSelectEvent
gridEvt( GetId(),
4194 EVT_GRID_RANGE_SELECT
,
4197 m_selectedBottomRight
);
4199 GetEventHandler()->ProcessEvent(gridEvt
);
4203 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4207 if ( topRow
> bottomRow
)
4214 if ( leftCol
> rightCol
)
4221 m_selectedTopLeft
.Set( topRow
, leftCol
);
4222 m_selectedBottomRight
.Set( bottomRow
, rightCol
);
4224 if ( !GetBatchCount() )
4226 wxRect
rect( SelectionToRect() );
4227 if ( rect
!= wxGridNoCellRect
) Refresh( TRUE
, &rect
);
4230 // only generate an event if the block is not being selected by
4231 // dragging the mouse (in which case the event will be generated in
4233 if ( !m_isDragging
)
4235 wxGridRangeSelectEvent
gridEvt( GetId(),
4236 EVT_GRID_RANGE_SELECT
,
4239 m_selectedBottomRight
);
4241 GetEventHandler()->ProcessEvent(gridEvt
);
4245 void wxGrid::SelectAll()
4247 m_selectedTopLeft
.Set( 0, 0 );
4248 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4250 if ( !GetBatchCount() ) Refresh();
4254 void wxGrid::ClearSelection()
4256 if ( IsSelection() )
4258 wxRect
rect( SelectionToRect() );
4259 if ( rect
!= wxGridNoCellRect
)
4261 Refresh( TRUE
, &rect
);
4264 m_selectedTopLeft
= wxGridNoCellCoords
;
4265 m_selectedBottomRight
= wxGridNoCellCoords
;
4270 wxRect
wxGrid::SelectionToRect()
4275 if ( IsSelection() )
4277 cellRect
= CellToRect( m_selectedTopLeft
);
4278 if ( cellRect
!= wxGridNoCellRect
)
4284 rect
= wxRect( m_left
, m_top
, 0, 0 );
4287 cellRect
= CellToRect( m_selectedBottomRight
);
4288 if ( cellRect
!= wxGridNoCellRect
)
4294 return wxGridNoCellRect
;
4305 // ------ Grid event classes
4308 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4310 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4311 int row
, int col
, int x
, int y
,
4312 bool control
, bool shift
, bool alt
, bool meta
)
4313 : wxNotifyEvent( type
, id
)
4319 m_control
= control
;
4324 SetEventObject(obj
);
4328 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4330 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4331 int rowOrCol
, int x
, int y
,
4332 bool control
, bool shift
, bool alt
, bool meta
)
4333 : wxNotifyEvent( type
, id
)
4335 m_rowOrCol
= rowOrCol
;
4338 m_control
= control
;
4343 SetEventObject(obj
);
4347 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4349 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4350 const wxGridCellCoords
& topLeft
,
4351 const wxGridCellCoords
& bottomRight
,
4352 bool control
, bool shift
, bool alt
, bool meta
)
4353 : wxNotifyEvent( type
, id
)
4355 m_topLeft
= topLeft
;
4356 m_bottomRight
= bottomRight
;
4357 m_control
= control
;
4362 SetEventObject(obj
);
4366 #endif // ifndef wxUSE_NEW_GRID