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 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
44 // this include needs to be outside precomp for BCC
45 #include "wx/textfile.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
56 struct wxGridCellWithAttr
58 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
59 : coords(row
, col
), attr(attr_
)
68 wxGridCellCoords coords
;
72 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
74 #include "wx/arrimpl.cpp"
76 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
77 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
86 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
87 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
88 const wxPoint
&pos
, const wxSize
&size
);
93 void OnPaint( wxPaintEvent
& event
);
94 void OnMouseEvent( wxMouseEvent
& event
);
95 void OnKeyDown( wxKeyEvent
& event
);
97 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
102 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
105 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
106 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
107 const wxPoint
&pos
, const wxSize
&size
);
112 void OnPaint( wxPaintEvent
&event
);
113 void OnMouseEvent( wxMouseEvent
& event
);
114 void OnKeyDown( wxKeyEvent
& event
);
116 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
117 DECLARE_EVENT_TABLE()
121 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
124 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
125 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
126 const wxPoint
&pos
, const wxSize
&size
);
131 void OnMouseEvent( wxMouseEvent
& event
);
132 void OnKeyDown( wxKeyEvent
& event
);
133 void OnPaint( wxPaintEvent
& event
);
135 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
136 DECLARE_EVENT_TABLE()
139 class WXDLLEXPORT wxGridWindow
: public wxPanel
144 m_owner
= (wxGrid
*)NULL
;
145 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
146 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
149 wxGridWindow( wxGrid
*parent
,
150 wxGridRowLabelWindow
*rowLblWin
,
151 wxGridColLabelWindow
*colLblWin
,
152 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
155 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
159 wxGridRowLabelWindow
*m_rowLabelWin
;
160 wxGridColLabelWindow
*m_colLabelWin
;
162 void OnPaint( wxPaintEvent
&event
);
163 void OnMouseEvent( wxMouseEvent
& event
);
164 void OnKeyDown( wxKeyEvent
& );
165 void OnEraseBackground( wxEraseEvent
& );
168 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
169 DECLARE_EVENT_TABLE()
174 class wxGridCellEditorEvtHandler
: public wxEvtHandler
177 wxGridCellEditorEvtHandler()
178 : m_grid(0), m_editor(0)
180 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
181 : m_grid(grid
), m_editor(editor
)
184 void OnKeyDown(wxKeyEvent
& event
);
188 wxGridCellEditor
* m_editor
;
189 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
190 DECLARE_EVENT_TABLE()
194 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
195 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
196 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
201 // ----------------------------------------------------------------------------
202 // the internal data representation used by wxGridCellAttrProvider
203 // ----------------------------------------------------------------------------
205 // this class stores attributes set for cells
206 class WXDLLEXPORT wxGridCellAttrData
209 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
210 wxGridCellAttr
*GetAttr(int row
, int col
) const;
211 void UpdateAttrRows( size_t pos
, int numRows
);
212 void UpdateAttrCols( size_t pos
, int numCols
);
215 // searches for the attr for given cell, returns wxNOT_FOUND if not found
216 int FindIndex(int row
, int col
) const;
218 wxGridCellWithAttrArray m_attrs
;
221 // this class stores attributes set for rows or columns
222 class WXDLLEXPORT wxGridRowOrColAttrData
225 ~wxGridRowOrColAttrData();
227 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
228 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
229 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
232 wxArrayInt m_rowsOrCols
;
233 wxArrayAttrs m_attrs
;
236 // NB: this is just a wrapper around 3 objects: one which stores cell
237 // attributes, and 2 others for row/col ones
238 class WXDLLEXPORT wxGridCellAttrProviderData
241 wxGridCellAttrData m_cellAttrs
;
242 wxGridRowOrColAttrData m_rowAttrs
,
246 // ----------------------------------------------------------------------------
247 // conditional compilation
248 // ----------------------------------------------------------------------------
250 #ifndef WXGRID_DRAW_LINES
251 #define WXGRID_DRAW_LINES 1
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 //#define DEBUG_ATTR_CACHE
259 #ifdef DEBUG_ATTR_CACHE
260 static size_t gs_nAttrCacheHits
= 0;
261 static size_t gs_nAttrCacheMisses
= 0;
262 #endif // DEBUG_ATTR_CACHE
264 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
265 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
268 // TODO: fixed so far - make configurable later (and also different for x/y)
269 static const size_t GRID_SCROLL_LINE
= 10;
271 // ============================================================================
273 // ============================================================================
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 wxGridCellEditor::wxGridCellEditor()
286 wxGridCellEditor::~wxGridCellEditor()
292 void wxGridCellEditor::Destroy()
295 m_control
->Destroy();
300 void wxGridCellEditor::Show(bool show
)
302 wxASSERT_MSG(m_control
,
303 wxT("The wxGridCellEditor must be Created first!"));
304 m_control
->Show(show
);
307 void wxGridCellEditor::SetSize(const wxRect
& rect
)
309 wxASSERT_MSG(m_control
,
310 wxT("The wxGridCellEditor must be Created first!"));
311 m_control
->SetSize(rect
);
314 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
320 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
326 wxGridCellTextEditor::wxGridCellTextEditor()
330 void wxGridCellTextEditor::Create(wxWindow
* parent
,
332 wxEvtHandler
* evtHandler
)
334 m_control
= new wxTextCtrl(parent
, -1, "",
335 wxDefaultPosition
, wxDefaultSize
336 #if defined(__WXMSW__)
337 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
342 m_control
->PushEventHandler(evtHandler
);
346 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
,
347 wxGridCellAttr
* attr
)
349 wxASSERT_MSG(m_control
,
350 wxT("The wxGridCellEditor must be Created first!"));
352 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
353 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
354 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
355 ((wxTextCtrl
*)m_control
)->SetFocus();
357 // ??? Should we use attr and try to set colours and font?
362 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
363 wxGrid
* grid
, wxGridCellAttr
* attr
)
365 wxASSERT_MSG(m_control
,
366 wxT("The wxGridCellEditor must be Created first!"));
368 bool changed
= FALSE
;
369 wxString value
= ((wxTextCtrl
*)m_control
)->GetValue();
370 if (value
!= m_startValue
)
374 grid
->GetTable()->SetValue(row
, col
, value
);
377 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
383 void wxGridCellTextEditor::Reset()
385 wxASSERT_MSG(m_control
,
386 wxT("The wxGridCellEditor must be Created first!"));
388 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
389 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
393 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
395 wxASSERT_MSG(m_control
,
396 wxT("The wxGridCellEditor must be Created first!"));
398 int code
= event
.KeyCode();
399 if (code
>= 32 && code
< 255) {
400 wxString
st((char)code
);
401 if (! event
.ShiftDown())
403 ((wxTextCtrl
*)m_control
)->AppendText(st
);
408 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
410 #if defined(__WXMOTIF__) || defined(__WXGTK__)
411 // wxMotif needs a little extra help...
412 int pos
= ((wxTextCtrl
*)m_control
)->GetInsertionPoint();
413 wxString
s( ((wxTextCtrl
*)m_control
)->GetValue() );
414 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
415 ((wxTextCtrl
*)m_control
)->SetValue(s
);
416 ((wxTextCtrl
*)m_control
)->SetInsertionPoint( pos
);
418 // the other ports can handle a Return key press
425 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
427 switch ( event
.KeyCode() )
431 m_grid
->EnableCellEditControl(FALSE
);
435 event
.Skip( m_grid
->ProcessEvent( event
) );
439 if (!m_grid
->ProcessEvent(event
))
440 m_editor
->HandleReturn(event
);
449 // ----------------------------------------------------------------------------
450 // wxGridCellRenderer
451 // ----------------------------------------------------------------------------
453 void wxGridCellRenderer::Draw(wxGrid
& grid
,
454 wxGridCellAttr
& attr
,
460 dc
.SetBackgroundMode( wxSOLID
);
464 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
468 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
471 dc
.SetPen( *wxTRANSPARENT_PEN
);
472 dc
.DrawRectangle(rect
);
475 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
476 wxGridCellAttr
& attr
,
478 const wxRect
& rectCell
,
482 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
484 // now we only have to draw the text
485 dc
.SetBackgroundMode( wxTRANSPARENT
);
489 dc
.SetTextBackground( grid
.GetSelectionBackground() );
490 dc
.SetTextForeground( grid
.GetSelectionForeground() );
494 dc
.SetTextBackground( attr
.GetBackgroundColour() );
495 dc
.SetTextForeground( attr
.GetTextColour() );
497 dc
.SetFont( attr
.GetFont() );
500 attr
.GetAlignment(&hAlign
, &vAlign
);
502 wxRect rect
= rectCell
;
508 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
509 rect
, hAlign
, vAlign
);
512 // ----------------------------------------------------------------------------
514 // ----------------------------------------------------------------------------
516 const wxColour
& wxGridCellAttr::GetTextColour() const
520 else if (m_defGridAttr
!= this)
521 return m_defGridAttr
->GetTextColour();
523 wxFAIL_MSG(wxT("Missing default cell attribute"));
529 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
531 if (HasBackgroundColour())
533 else if (m_defGridAttr
!= this)
534 return m_defGridAttr
->GetBackgroundColour();
536 wxFAIL_MSG(wxT("Missing default cell attribute"));
542 const wxFont
& wxGridCellAttr::GetFont() const
546 else if (m_defGridAttr
!= this)
547 return m_defGridAttr
->GetFont();
549 wxFAIL_MSG(wxT("Missing default cell attribute"));
555 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
557 if (HasAlignment()) {
558 if ( hAlign
) *hAlign
= m_hAlign
;
559 if ( vAlign
) *vAlign
= m_vAlign
;
561 else if (m_defGridAttr
!= this)
562 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
564 wxFAIL_MSG(wxT("Missing default cell attribute"));
569 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
573 else if (m_defGridAttr
!= this)
574 return m_defGridAttr
->GetRenderer();
576 wxFAIL_MSG(wxT("Missing default cell attribute"));
581 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
585 else if (m_defGridAttr
!= this)
586 return m_defGridAttr
->GetEditor();
588 wxFAIL_MSG(wxT("Missing default cell attribute"));
593 // ----------------------------------------------------------------------------
594 // wxGridCellAttrData
595 // ----------------------------------------------------------------------------
597 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
599 int n
= FindIndex(row
, col
);
600 if ( n
== wxNOT_FOUND
)
603 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
609 // change the attribute
610 m_attrs
[(size_t)n
].attr
= attr
;
614 // remove this attribute
615 m_attrs
.RemoveAt((size_t)n
);
620 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
622 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
624 int n
= FindIndex(row
, col
);
625 if ( n
!= wxNOT_FOUND
)
627 attr
= m_attrs
[(size_t)n
].attr
;
634 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
636 size_t count
= m_attrs
.GetCount();
637 for ( size_t n
= 0; n
< count
; n
++ )
639 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
640 wxCoord row
= coords
.GetRow();
641 if ((size_t)row
>= pos
)
645 // If rows inserted, include row counter where necessary
646 coords
.SetRow(row
+ numRows
);
648 else if (numRows
< 0)
650 // If rows deleted ...
651 if ((size_t)row
>= pos
- numRows
)
653 // ...either decrement row counter (if row still exists)...
654 coords
.SetRow(row
+ numRows
);
658 // ...or remove the attribute
659 m_attrs
.RemoveAt((size_t)n
);
667 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
669 size_t count
= m_attrs
.GetCount();
670 for ( size_t n
= 0; n
< count
; n
++ )
672 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
673 wxCoord col
= coords
.GetCol();
674 if ( (size_t)col
>= pos
)
678 // If rows inserted, include row counter where necessary
679 coords
.SetCol(col
+ numCols
);
681 else if (numCols
< 0)
683 // If rows deleted ...
684 if ((size_t)col
>= pos
- numCols
)
686 // ...either decrement row counter (if row still exists)...
687 coords
.SetCol(col
+ numCols
);
691 // ...or remove the attribute
692 m_attrs
.RemoveAt((size_t)n
);
700 int wxGridCellAttrData::FindIndex(int row
, int col
) const
702 size_t count
= m_attrs
.GetCount();
703 for ( size_t n
= 0; n
< count
; n
++ )
705 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
706 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
715 // ----------------------------------------------------------------------------
716 // wxGridRowOrColAttrData
717 // ----------------------------------------------------------------------------
719 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
721 size_t count
= m_attrs
.Count();
722 for ( size_t n
= 0; n
< count
; n
++ )
724 m_attrs
[n
]->DecRef();
728 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
730 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
732 int n
= m_rowsOrCols
.Index(rowOrCol
);
733 if ( n
!= wxNOT_FOUND
)
735 attr
= m_attrs
[(size_t)n
];
742 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
744 int n
= m_rowsOrCols
.Index(rowOrCol
);
745 if ( n
== wxNOT_FOUND
)
748 m_rowsOrCols
.Add(rowOrCol
);
755 // change the attribute
756 m_attrs
[(size_t)n
] = attr
;
760 // remove this attribute
761 m_attrs
[(size_t)n
]->DecRef();
762 m_rowsOrCols
.RemoveAt((size_t)n
);
763 m_attrs
.RemoveAt((size_t)n
);
768 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
770 size_t count
= m_attrs
.GetCount();
771 for ( size_t n
= 0; n
< count
; n
++ )
773 int & rowOrCol
= m_rowsOrCols
[n
];
774 if ( (size_t)rowOrCol
>= pos
)
776 if ( numRowsOrCols
> 0 )
778 // If rows inserted, include row counter where necessary
779 rowOrCol
+= numRowsOrCols
;
781 else if ( numRowsOrCols
< 0)
783 // If rows deleted, either decrement row counter (if row still exists)
784 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
785 rowOrCol
+= numRowsOrCols
;
788 m_rowsOrCols
.RemoveAt((size_t)n
);
789 m_attrs
.RemoveAt((size_t)n
);
797 // ----------------------------------------------------------------------------
798 // wxGridCellAttrProvider
799 // ----------------------------------------------------------------------------
801 wxGridCellAttrProvider::wxGridCellAttrProvider()
803 m_data
= (wxGridCellAttrProviderData
*)NULL
;
806 wxGridCellAttrProvider::~wxGridCellAttrProvider()
811 void wxGridCellAttrProvider::InitData()
813 m_data
= new wxGridCellAttrProviderData
;
816 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
818 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
821 // first look for the attribute of this specific cell
822 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
826 // then look for the col attr (col attributes are more common than
827 // the row ones, hence they have priority)
828 attr
= m_data
->m_colAttrs
.GetAttr(col
);
833 // finally try the row attributes
834 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
841 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
847 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
850 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
855 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
858 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
863 m_data
->m_colAttrs
.SetAttr(attr
, col
);
866 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
870 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
872 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
876 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
880 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
882 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
886 // ----------------------------------------------------------------------------
888 // ----------------------------------------------------------------------------
890 //////////////////////////////////////////////////////////////////////
892 // Abstract base class for grid data (the model)
894 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
897 wxGridTableBase::wxGridTableBase()
899 m_view
= (wxGrid
*) NULL
;
900 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
903 wxGridTableBase::~wxGridTableBase()
905 delete m_attrProvider
;
908 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
910 delete m_attrProvider
;
911 m_attrProvider
= attrProvider
;
914 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
916 if ( m_attrProvider
)
917 return m_attrProvider
->GetAttr(row
, col
);
919 return (wxGridCellAttr
*)NULL
;
922 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
924 if ( m_attrProvider
)
926 m_attrProvider
->SetAttr(attr
, row
, col
);
930 // as we take ownership of the pointer and don't store it, we must
936 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
938 if ( m_attrProvider
)
940 m_attrProvider
->SetRowAttr(attr
, row
);
944 // as we take ownership of the pointer and don't store it, we must
950 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
952 if ( m_attrProvider
)
954 m_attrProvider
->SetColAttr(attr
, col
);
958 // as we take ownership of the pointer and don't store it, we must
964 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
966 if ( m_attrProvider
)
968 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
972 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
974 if ( m_attrProvider
)
976 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
980 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
982 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
983 "but your derived table class does not override this function") );
988 bool wxGridTableBase::AppendRows( size_t numRows
)
990 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
991 "but your derived table class does not override this function"));
996 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
998 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
999 "but your derived table class does not override this function"));
1004 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1006 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1007 "but your derived table class does not override this function"));
1012 bool wxGridTableBase::AppendCols( size_t numCols
)
1014 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1015 "but your derived table class does not override this function"));
1020 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1022 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1023 "but your derived table class does not override this function"));
1029 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1036 wxString
wxGridTableBase::GetColLabelValue( int col
)
1038 // default col labels are:
1039 // cols 0 to 25 : A-Z
1040 // cols 26 to 675 : AA-ZZ
1045 for ( n
= 1; ; n
++ )
1047 s
+= (_T('A') + (wxChar
)( col%26
));
1049 if ( col
< 0 ) break;
1052 // reverse the string...
1054 for ( i
= 0; i
< n
; i
++ )
1064 //////////////////////////////////////////////////////////////////////
1066 // Message class for the grid table to send requests and notifications
1070 wxGridTableMessage::wxGridTableMessage()
1072 m_table
= (wxGridTableBase
*) NULL
;
1078 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1079 int commandInt1
, int commandInt2
)
1083 m_comInt1
= commandInt1
;
1084 m_comInt2
= commandInt2
;
1089 //////////////////////////////////////////////////////////////////////
1091 // A basic grid table for string data. An object of this class will
1092 // created by wxGrid if you don't specify an alternative table class.
1095 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1097 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1099 wxGridStringTable::wxGridStringTable()
1104 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1109 m_data
.Alloc( numRows
);
1112 sa
.Alloc( numCols
);
1113 for ( col
= 0; col
< numCols
; col
++ )
1115 sa
.Add( wxEmptyString
);
1118 for ( row
= 0; row
< numRows
; row
++ )
1124 wxGridStringTable::~wxGridStringTable()
1128 long wxGridStringTable::GetNumberRows()
1130 return m_data
.GetCount();
1133 long wxGridStringTable::GetNumberCols()
1135 if ( m_data
.GetCount() > 0 )
1136 return m_data
[0].GetCount();
1141 wxString
wxGridStringTable::GetValue( int row
, int col
)
1143 // TODO: bounds checking
1145 return m_data
[row
][col
];
1148 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1150 // TODO: bounds checking
1152 m_data
[row
][col
] = s
;
1155 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1157 // TODO: bounds checking
1159 return (m_data
[row
][col
] == wxEmptyString
);
1163 void wxGridStringTable::Clear()
1166 int numRows
, numCols
;
1168 numRows
= m_data
.GetCount();
1171 numCols
= m_data
[0].GetCount();
1173 for ( row
= 0; row
< numRows
; row
++ )
1175 for ( col
= 0; col
< numCols
; col
++ )
1177 m_data
[row
][col
] = wxEmptyString
;
1184 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1188 size_t curNumRows
= m_data
.GetCount();
1189 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1191 if ( pos
>= curNumRows
)
1193 return AppendRows( numRows
);
1197 sa
.Alloc( curNumCols
);
1198 for ( col
= 0; col
< curNumCols
; col
++ )
1200 sa
.Add( wxEmptyString
);
1203 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1205 m_data
.Insert( sa
, row
);
1207 UpdateAttrRows( pos
, numRows
);
1210 wxGridTableMessage
msg( this,
1211 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1215 GetView()->ProcessTableMessage( msg
);
1221 bool wxGridStringTable::AppendRows( size_t numRows
)
1225 size_t curNumRows
= m_data
.GetCount();
1226 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1229 if ( curNumCols
> 0 )
1231 sa
.Alloc( curNumCols
);
1232 for ( col
= 0; col
< curNumCols
; col
++ )
1234 sa
.Add( wxEmptyString
);
1238 for ( row
= 0; row
< numRows
; row
++ )
1245 wxGridTableMessage
msg( this,
1246 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1249 GetView()->ProcessTableMessage( msg
);
1255 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1259 size_t curNumRows
= m_data
.GetCount();
1261 if ( pos
>= curNumRows
)
1264 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1265 "Pos value is invalid for present table with %d rows",
1266 pos
, numRows
, curNumRows
);
1267 wxFAIL_MSG( wxT(errmsg
) );
1271 if ( numRows
> curNumRows
- pos
)
1273 numRows
= curNumRows
- pos
;
1276 if ( numRows
>= curNumRows
)
1278 m_data
.Empty(); // don't release memory just yet
1282 for ( n
= 0; n
< numRows
; n
++ )
1284 m_data
.Remove( pos
);
1287 UpdateAttrRows( pos
, -((int)numRows
) );
1290 wxGridTableMessage
msg( this,
1291 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1295 GetView()->ProcessTableMessage( msg
);
1301 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1305 size_t curNumRows
= m_data
.GetCount();
1306 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1308 if ( pos
>= curNumCols
)
1310 return AppendCols( numCols
);
1313 for ( row
= 0; row
< curNumRows
; row
++ )
1315 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1317 m_data
[row
].Insert( wxEmptyString
, col
);
1320 UpdateAttrCols( pos
, numCols
);
1323 wxGridTableMessage
msg( this,
1324 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1328 GetView()->ProcessTableMessage( msg
);
1334 bool wxGridStringTable::AppendCols( size_t numCols
)
1338 size_t curNumRows
= m_data
.GetCount();
1341 // TODO: something better than this ?
1343 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1344 "Call AppendRows() first") );
1348 for ( row
= 0; row
< curNumRows
; row
++ )
1350 for ( n
= 0; n
< numCols
; n
++ )
1352 m_data
[row
].Add( wxEmptyString
);
1358 wxGridTableMessage
msg( this,
1359 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1362 GetView()->ProcessTableMessage( msg
);
1368 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1372 size_t curNumRows
= m_data
.GetCount();
1373 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1375 if ( pos
>= curNumCols
)
1378 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1379 "Pos value is invalid for present table with %d cols",
1380 pos
, numCols
, curNumCols
);
1381 wxFAIL_MSG( wxT( errmsg
) );
1385 if ( numCols
> curNumCols
- pos
)
1387 numCols
= curNumCols
- pos
;
1390 for ( row
= 0; row
< curNumRows
; row
++ )
1392 if ( numCols
>= curNumCols
)
1394 m_data
[row
].Clear();
1398 for ( n
= 0; n
< numCols
; n
++ )
1400 m_data
[row
].Remove( pos
);
1404 UpdateAttrCols( pos
, -((int)numCols
) );
1407 wxGridTableMessage
msg( this,
1408 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1412 GetView()->ProcessTableMessage( msg
);
1418 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1420 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1422 // using default label
1424 return wxGridTableBase::GetRowLabelValue( row
);
1428 return m_rowLabels
[ row
];
1432 wxString
wxGridStringTable::GetColLabelValue( int col
)
1434 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1436 // using default label
1438 return wxGridTableBase::GetColLabelValue( col
);
1442 return m_colLabels
[ col
];
1446 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1448 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1450 int n
= m_rowLabels
.GetCount();
1452 for ( i
= n
; i
<= row
; i
++ )
1454 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1458 m_rowLabels
[row
] = value
;
1461 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1463 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1465 int n
= m_colLabels
.GetCount();
1467 for ( i
= n
; i
<= col
; i
++ )
1469 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1473 m_colLabels
[col
] = value
;
1478 //////////////////////////////////////////////////////////////////////
1479 //////////////////////////////////////////////////////////////////////
1481 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1483 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1484 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1485 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1486 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1489 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1491 const wxPoint
&pos
, const wxSize
&size
)
1492 : wxWindow( parent
, id
, pos
, size
)
1497 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1501 // NO - don't do this because it will set both the x and y origin
1502 // coords to match the parent scrolled window and we just want to
1503 // set the y coord - MB
1505 // m_owner->PrepareDC( dc );
1508 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1509 dc
.SetDeviceOrigin( 0, -y
);
1511 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1512 m_owner
->DrawRowLabels( dc
);
1516 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1518 m_owner
->ProcessRowLabelMouseEvent( event
);
1522 // This seems to be required for wxMotif otherwise the mouse
1523 // cursor must be in the cell edit control to get key events
1525 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1527 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1532 //////////////////////////////////////////////////////////////////////
1534 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1536 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1537 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1538 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1539 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1542 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1544 const wxPoint
&pos
, const wxSize
&size
)
1545 : wxWindow( parent
, id
, pos
, size
)
1550 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1554 // NO - don't do this because it will set both the x and y origin
1555 // coords to match the parent scrolled window and we just want to
1556 // set the x coord - MB
1558 // m_owner->PrepareDC( dc );
1561 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1562 dc
.SetDeviceOrigin( -x
, 0 );
1564 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1565 m_owner
->DrawColLabels( dc
);
1569 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1571 m_owner
->ProcessColLabelMouseEvent( event
);
1575 // This seems to be required for wxMotif otherwise the mouse
1576 // cursor must be in the cell edit control to get key events
1578 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1580 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1585 //////////////////////////////////////////////////////////////////////
1587 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1589 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1590 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1591 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1592 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1595 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1597 const wxPoint
&pos
, const wxSize
&size
)
1598 : wxWindow( parent
, id
, pos
, size
)
1603 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1607 int client_height
= 0;
1608 int client_width
= 0;
1609 GetClientSize( &client_width
, &client_height
);
1611 dc
.SetPen( *wxBLACK_PEN
);
1612 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1613 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1615 dc
.SetPen( *wxWHITE_PEN
);
1616 dc
.DrawLine( 0, 0, client_width
, 0 );
1617 dc
.DrawLine( 0, 0, 0, client_height
);
1621 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1623 m_owner
->ProcessCornerLabelMouseEvent( event
);
1627 // This seems to be required for wxMotif otherwise the mouse
1628 // cursor must be in the cell edit control to get key events
1630 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1632 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1637 //////////////////////////////////////////////////////////////////////
1639 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1641 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1642 EVT_PAINT( wxGridWindow::OnPaint
)
1643 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1644 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1645 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1648 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1649 wxGridRowLabelWindow
*rowLblWin
,
1650 wxGridColLabelWindow
*colLblWin
,
1651 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1652 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1655 m_rowLabelWin
= rowLblWin
;
1656 m_colLabelWin
= colLblWin
;
1657 SetBackgroundColour( "WHITE" );
1661 wxGridWindow::~wxGridWindow()
1666 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1668 wxPaintDC
dc( this );
1669 m_owner
->PrepareDC( dc
);
1670 wxRegion reg
= GetUpdateRegion();
1671 m_owner
->CalcCellsExposed( reg
);
1672 m_owner
->DrawGridCellArea( dc
);
1673 #if WXGRID_DRAW_LINES
1674 m_owner
->DrawAllGridLines( dc
, reg
);
1679 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1681 wxPanel::ScrollWindow( dx
, dy
, rect
);
1682 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1683 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1687 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1689 m_owner
->ProcessGridCellMouseEvent( event
);
1693 // This seems to be required for wxMotif otherwise the mouse
1694 // cursor must be in the cell edit control to get key events
1696 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1698 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1701 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1707 //////////////////////////////////////////////////////////////////////
1710 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1712 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1713 EVT_PAINT( wxGrid::OnPaint
)
1714 EVT_SIZE( wxGrid::OnSize
)
1715 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1716 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1719 wxGrid::wxGrid( wxWindow
*parent
,
1724 const wxString
& name
)
1725 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1734 m_defaultCellAttr
->SafeDecRef();
1736 #ifdef DEBUG_ATTR_CACHE
1737 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1738 wxPrintf(_T("wxGrid attribute cache statistics: "
1739 "total: %u, hits: %u (%u%%)\n"),
1740 total
, gs_nAttrCacheHits
,
1741 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1750 // ----- internal init and update functions
1753 void wxGrid::Create()
1755 m_created
= FALSE
; // set to TRUE by CreateGrid
1756 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
1758 m_table
= (wxGridTableBase
*) NULL
;
1761 m_cellEditCtrlEnabled
= FALSE
;
1763 m_defaultCellAttr
= new wxGridCellAttr
;
1764 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1765 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1770 m_currentCellCoords
= wxGridNoCellCoords
;
1772 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1773 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1775 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1780 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1785 m_colLabelWin
= new wxGridColLabelWindow( this,
1790 m_gridWin
= new wxGridWindow( this,
1797 SetTargetWindow( m_gridWin
);
1801 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1805 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1810 m_numRows
= numRows
;
1811 m_numCols
= numCols
;
1813 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1814 m_table
->SetView( this );
1823 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1827 // RD: Actually, this should probably be allowed. I think it would be
1828 // nice to be able to switch multiple Tables in and out of a single
1829 // View at runtime. Is there anything in the implmentation that would
1832 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1837 m_numRows
= table
->GetNumberRows();
1838 m_numCols
= table
->GetNumberCols();
1841 m_table
->SetView( this );
1856 if ( m_numRows
<= 0 )
1857 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1859 if ( m_numCols
<= 0 )
1860 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1862 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1863 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1865 if ( m_rowLabelWin
)
1867 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1871 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1874 m_labelTextColour
= wxColour( _T("BLACK") );
1877 m_attrCache
.row
= -1;
1879 // TODO: something better than this ?
1881 m_labelFont
= this->GetFont();
1882 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1884 m_rowLabelHorizAlign
= wxLEFT
;
1885 m_rowLabelVertAlign
= wxCENTRE
;
1887 m_colLabelHorizAlign
= wxCENTRE
;
1888 m_colLabelVertAlign
= wxTOP
;
1890 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1891 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1893 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1894 m_defaultRowHeight
+= 8;
1896 m_defaultRowHeight
+= 4;
1899 m_rowHeights
.Alloc( m_numRows
);
1900 m_rowBottoms
.Alloc( m_numRows
);
1902 for ( i
= 0; i
< m_numRows
; i
++ )
1904 m_rowHeights
.Add( m_defaultRowHeight
);
1905 rowBottom
+= m_defaultRowHeight
;
1906 m_rowBottoms
.Add( rowBottom
);
1909 m_colWidths
.Alloc( m_numCols
);
1910 m_colRights
.Alloc( m_numCols
);
1912 for ( i
= 0; i
< m_numCols
; i
++ )
1914 m_colWidths
.Add( m_defaultColWidth
);
1915 colRight
+= m_defaultColWidth
;
1916 m_colRights
.Add( colRight
);
1919 // Set default cell attributes
1920 m_defaultCellAttr
->SetFont(GetFont());
1921 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1922 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1923 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1924 m_defaultCellAttr
->SetTextColour(
1925 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1926 m_defaultCellAttr
->SetBackgroundColour(
1927 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1930 m_gridLineColour
= wxColour( 128, 128, 255 );
1931 m_gridLinesEnabled
= TRUE
;
1933 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1934 m_winCapture
= (wxWindow
*)NULL
;
1936 m_dragRowOrCol
= -1;
1937 m_isDragging
= FALSE
;
1938 m_startDragPos
= wxDefaultPosition
;
1940 m_waitForSlowClick
= FALSE
;
1942 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1943 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1945 m_currentCellCoords
= wxGridNoCellCoords
;
1947 m_selectedTopLeft
= wxGridNoCellCoords
;
1948 m_selectedBottomRight
= wxGridNoCellCoords
;
1949 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
1950 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1952 m_editable
= TRUE
; // default for whole grid
1954 m_inOnKeyDown
= FALSE
;
1960 void wxGrid::CalcDimensions()
1963 GetClientSize( &cw
, &ch
);
1965 if ( m_numRows
> 0 && m_numCols
> 0 )
1967 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1968 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1970 // TODO: restore the scroll position that we had before sizing
1973 GetViewStart( &x
, &y
);
1974 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1975 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1981 void wxGrid::CalcWindowSizes()
1984 GetClientSize( &cw
, &ch
);
1986 if ( m_cornerLabelWin
->IsShown() )
1987 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1989 if ( m_colLabelWin
->IsShown() )
1990 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1992 if ( m_rowLabelWin
->IsShown() )
1993 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1995 if ( m_gridWin
->IsShown() )
1996 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2000 // this is called when the grid table sends a message to say that it
2001 // has been redimensioned
2003 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2007 switch ( msg
.GetId() )
2009 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2011 size_t pos
= msg
.GetCommandInt();
2012 int numRows
= msg
.GetCommandInt2();
2013 for ( i
= 0; i
< numRows
; i
++ )
2015 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2016 m_rowBottoms
.Insert( 0, pos
);
2018 m_numRows
+= numRows
;
2021 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2023 for ( i
= pos
; i
< m_numRows
; i
++ )
2025 bottom
+= m_rowHeights
[i
];
2026 m_rowBottoms
[i
] = bottom
;
2032 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2034 int numRows
= msg
.GetCommandInt();
2035 for ( i
= 0; i
< numRows
; i
++ )
2037 m_rowHeights
.Add( m_defaultRowHeight
);
2038 m_rowBottoms
.Add( 0 );
2041 int oldNumRows
= m_numRows
;
2042 m_numRows
+= numRows
;
2045 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2047 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2049 bottom
+= m_rowHeights
[i
];
2050 m_rowBottoms
[i
] = bottom
;
2056 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2058 size_t pos
= msg
.GetCommandInt();
2059 int numRows
= msg
.GetCommandInt2();
2060 for ( i
= 0; i
< numRows
; i
++ )
2062 m_rowHeights
.Remove( pos
);
2063 m_rowBottoms
.Remove( pos
);
2065 m_numRows
-= numRows
;
2070 m_colWidths
.Clear();
2071 m_colRights
.Clear();
2072 m_currentCellCoords
= wxGridNoCellCoords
;
2076 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2077 m_currentCellCoords
.Set( 0, 0 );
2080 for ( i
= 0; i
< m_numRows
; i
++ )
2082 h
+= m_rowHeights
[i
];
2083 m_rowBottoms
[i
] = h
;
2091 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2093 size_t pos
= msg
.GetCommandInt();
2094 int numCols
= msg
.GetCommandInt2();
2095 for ( i
= 0; i
< numCols
; i
++ )
2097 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2098 m_colRights
.Insert( 0, pos
);
2100 m_numCols
+= numCols
;
2103 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2105 for ( i
= pos
; i
< m_numCols
; i
++ )
2107 right
+= m_colWidths
[i
];
2108 m_colRights
[i
] = right
;
2114 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2116 int numCols
= msg
.GetCommandInt();
2117 for ( i
= 0; i
< numCols
; i
++ )
2119 m_colWidths
.Add( m_defaultColWidth
);
2120 m_colRights
.Add( 0 );
2123 int oldNumCols
= m_numCols
;
2124 m_numCols
+= numCols
;
2127 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2129 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2131 right
+= m_colWidths
[i
];
2132 m_colRights
[i
] = right
;
2138 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2140 size_t pos
= msg
.GetCommandInt();
2141 int numCols
= msg
.GetCommandInt2();
2142 for ( i
= 0; i
< numCols
; i
++ )
2144 m_colWidths
.Remove( pos
);
2145 m_colRights
.Remove( pos
);
2147 m_numCols
-= numCols
;
2151 #if 0 // leave the row alone here so that AppendCols will work subsequently
2153 m_rowHeights
.Clear();
2154 m_rowBottoms
.Clear();
2156 m_currentCellCoords
= wxGridNoCellCoords
;
2160 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2161 m_currentCellCoords
.Set( 0, 0 );
2164 for ( i
= 0; i
< m_numCols
; i
++ )
2166 w
+= m_colWidths
[i
];
2179 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2181 wxRegionIterator
iter( reg
);
2184 m_rowLabelsExposed
.Empty();
2191 // TODO: remove this when we can...
2192 // There is a bug in wxMotif that gives garbage update
2193 // rectangles if you jump-scroll a long way by clicking the
2194 // scrollbar with middle button. This is a work-around
2196 #if defined(__WXMOTIF__)
2198 m_gridWin
->GetClientSize( &cw
, &ch
);
2199 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2200 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2203 // logical bounds of update region
2206 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2207 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2209 // find the row labels within these bounds
2213 for ( row
= 0; row
< m_numRows
; row
++ )
2215 if ( m_rowBottoms
[row
] < top
) continue;
2217 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2218 if ( rowTop
> bottom
) break;
2220 m_rowLabelsExposed
.Add( row
);
2228 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2230 wxRegionIterator
iter( reg
);
2233 m_colLabelsExposed
.Empty();
2240 // TODO: remove this when we can...
2241 // There is a bug in wxMotif that gives garbage update
2242 // rectangles if you jump-scroll a long way by clicking the
2243 // scrollbar with middle button. This is a work-around
2245 #if defined(__WXMOTIF__)
2247 m_gridWin
->GetClientSize( &cw
, &ch
);
2248 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2249 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2252 // logical bounds of update region
2255 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2256 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2258 // find the cells within these bounds
2262 for ( col
= 0; col
< m_numCols
; col
++ )
2264 if ( m_colRights
[col
] < left
) continue;
2266 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2267 if ( colLeft
> right
) break;
2269 m_colLabelsExposed
.Add( col
);
2277 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2279 wxRegionIterator
iter( reg
);
2282 m_cellsExposed
.Empty();
2283 m_rowsExposed
.Empty();
2284 m_colsExposed
.Empty();
2286 int left
, top
, right
, bottom
;
2291 // TODO: remove this when we can...
2292 // There is a bug in wxMotif that gives garbage update
2293 // rectangles if you jump-scroll a long way by clicking the
2294 // scrollbar with middle button. This is a work-around
2296 #if defined(__WXMOTIF__)
2298 m_gridWin
->GetClientSize( &cw
, &ch
);
2299 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2300 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2301 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2302 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2305 // logical bounds of update region
2307 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2308 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2310 // find the cells within these bounds
2313 int colLeft
, rowTop
;
2314 for ( row
= 0; row
< m_numRows
; row
++ )
2316 if ( m_rowBottoms
[row
] <= top
) continue;
2318 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2319 if ( rowTop
> bottom
) break;
2321 m_rowsExposed
.Add( row
);
2323 for ( col
= 0; col
< m_numCols
; col
++ )
2325 if ( m_colRights
[col
] <= left
) continue;
2327 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2328 if ( colLeft
> right
) break;
2330 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2331 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2340 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2343 wxPoint
pos( event
.GetPosition() );
2344 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2346 if ( event
.Dragging() )
2348 m_isDragging
= TRUE
;
2350 if ( event
.LeftIsDown() )
2352 switch( m_cursorMode
)
2354 case WXGRID_CURSOR_RESIZE_ROW
:
2356 int cw
, ch
, left
, dummy
;
2357 m_gridWin
->GetClientSize( &cw
, &ch
);
2358 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2360 wxClientDC
dc( m_gridWin
);
2363 m_rowBottoms
[m_dragRowOrCol
] -
2364 m_rowHeights
[m_dragRowOrCol
] +
2365 WXGRID_MIN_ROW_HEIGHT
);
2366 dc
.SetLogicalFunction(wxINVERT
);
2367 if ( m_dragLastPos
>= 0 )
2369 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2371 dc
.DrawLine( left
, y
, left
+cw
, y
);
2376 case WXGRID_CURSOR_SELECT_ROW
:
2377 if ( (row
= YToRow( y
)) >= 0 &&
2378 !IsInSelection( row
, 0 ) )
2380 SelectRow( row
, TRUE
);
2383 // default label to suppress warnings about "enumeration value
2384 // 'xxx' not handled in switch
2392 m_isDragging
= FALSE
;
2395 // ------------ Entering or leaving the window
2397 if ( event
.Entering() || event
.Leaving() )
2399 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2403 // ------------ Left button pressed
2405 else if ( event
.LeftDown() )
2407 // don't send a label click event for a hit on the
2408 // edge of the row label - this is probably the user
2409 // wanting to resize the row
2411 if ( YToEdgeOfRow(y
) < 0 )
2415 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2417 SelectRow( row
, event
.ShiftDown() );
2418 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2423 // starting to drag-resize a row
2425 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2430 // ------------ Left double click
2432 else if (event
.LeftDClick() )
2434 if ( YToEdgeOfRow(y
) < 0 )
2437 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2442 // ------------ Left button released
2444 else if ( event
.LeftUp() )
2446 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2448 DoEndDragResizeRow();
2450 // Note: we are ending the event *after* doing
2451 // default processing in this case
2453 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2456 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2461 // ------------ Right button down
2463 else if ( event
.RightDown() )
2466 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2468 // no default action at the moment
2473 // ------------ Right double click
2475 else if ( event
.RightDClick() )
2478 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2480 // no default action at the moment
2485 // ------------ No buttons down and mouse moving
2487 else if ( event
.Moving() )
2489 m_dragRowOrCol
= YToEdgeOfRow( y
);
2490 if ( m_dragRowOrCol
>= 0 )
2492 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2494 // don't capture the mouse yet
2495 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2498 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2500 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2506 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2509 wxPoint
pos( event
.GetPosition() );
2510 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2512 if ( event
.Dragging() )
2514 m_isDragging
= TRUE
;
2516 if ( event
.LeftIsDown() )
2518 switch( m_cursorMode
)
2520 case WXGRID_CURSOR_RESIZE_COL
:
2522 int cw
, ch
, dummy
, top
;
2523 m_gridWin
->GetClientSize( &cw
, &ch
);
2524 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2526 wxClientDC
dc( m_gridWin
);
2529 m_colRights
[m_dragRowOrCol
] -
2530 m_colWidths
[m_dragRowOrCol
] +
2531 WXGRID_MIN_COL_WIDTH
);
2532 dc
.SetLogicalFunction(wxINVERT
);
2533 if ( m_dragLastPos
>= 0 )
2535 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2537 dc
.DrawLine( x
, top
, x
, top
+ch
);
2542 case WXGRID_CURSOR_SELECT_COL
:
2543 if ( (col
= XToCol( x
)) >= 0 &&
2544 !IsInSelection( 0, col
) )
2546 SelectCol( col
, TRUE
);
2549 // default label to suppress warnings about "enumeration value
2550 // 'xxx' not handled in switch
2558 m_isDragging
= FALSE
;
2561 // ------------ Entering or leaving the window
2563 if ( event
.Entering() || event
.Leaving() )
2565 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2569 // ------------ Left button pressed
2571 else if ( event
.LeftDown() )
2573 // don't send a label click event for a hit on the
2574 // edge of the col label - this is probably the user
2575 // wanting to resize the col
2577 if ( XToEdgeOfCol(x
) < 0 )
2581 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2583 SelectCol( col
, event
.ShiftDown() );
2584 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2589 // starting to drag-resize a col
2591 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2596 // ------------ Left double click
2598 if ( event
.LeftDClick() )
2600 if ( XToEdgeOfCol(x
) < 0 )
2603 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2608 // ------------ Left button released
2610 else if ( event
.LeftUp() )
2612 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2614 DoEndDragResizeCol();
2616 // Note: we are ending the event *after* doing
2617 // default processing in this case
2619 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2622 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2627 // ------------ Right button down
2629 else if ( event
.RightDown() )
2632 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2634 // no default action at the moment
2639 // ------------ Right double click
2641 else if ( event
.RightDClick() )
2644 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2646 // no default action at the moment
2651 // ------------ No buttons down and mouse moving
2653 else if ( event
.Moving() )
2655 m_dragRowOrCol
= XToEdgeOfCol( x
);
2656 if ( m_dragRowOrCol
>= 0 )
2658 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2660 // don't capture the cursor yet
2661 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2664 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2666 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2672 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2674 if ( event
.LeftDown() )
2676 // indicate corner label by having both row and
2679 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2685 else if ( event
.LeftDClick() )
2687 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2690 else if ( event
.RightDown() )
2692 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2694 // no default action at the moment
2698 else if ( event
.RightDClick() )
2700 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2702 // no default action at the moment
2707 void wxGrid::ChangeCursorMode(CursorMode mode
,
2712 static const wxChar
*cursorModes
[] =
2721 wxLogTrace(_T("grid"),
2722 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2723 win
== m_colLabelWin
? _T("colLabelWin")
2724 : win
? _T("rowLabelWin")
2726 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2727 #endif // __WXDEBUG__
2729 if ( mode
== m_cursorMode
)
2734 // by default use the grid itself
2740 m_winCapture
->ReleaseMouse();
2741 m_winCapture
= (wxWindow
*)NULL
;
2744 m_cursorMode
= mode
;
2746 switch ( m_cursorMode
)
2748 case WXGRID_CURSOR_RESIZE_ROW
:
2749 win
->SetCursor( m_rowResizeCursor
);
2752 case WXGRID_CURSOR_RESIZE_COL
:
2753 win
->SetCursor( m_colResizeCursor
);
2757 win
->SetCursor( *wxSTANDARD_CURSOR
);
2760 // we need to capture mouse when resizing
2761 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2762 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2764 if ( captureMouse
&& resize
)
2766 win
->CaptureMouse();
2771 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2774 wxPoint
pos( event
.GetPosition() );
2775 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2777 wxGridCellCoords coords
;
2778 XYToCell( x
, y
, coords
);
2780 if ( event
.Dragging() )
2782 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
2784 // Don't start doing anything until the mouse has been drug at
2785 // least 3 pixels in any direction...
2786 if (! m_isDragging
) {
2787 if (m_startDragPos
== wxDefaultPosition
) {
2788 m_startDragPos
= pos
;
2791 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
2795 m_isDragging
= TRUE
;
2796 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2798 // Hide the edit control, so it
2799 // won't interfer with drag-shrinking.
2800 if ( IsCellEditControlEnabled() )
2801 HideCellEditControl();
2803 // Have we captured the mouse yet?
2804 if (! m_winCapture
) {
2805 m_winCapture
= m_gridWin
;
2806 m_winCapture
->CaptureMouse();
2809 if ( coords
!= wxGridNoCellCoords
)
2811 if ( !IsSelection() )
2813 SelectBlock( coords
, coords
);
2817 SelectBlock( m_currentCellCoords
, coords
);
2820 if (! IsVisible(coords
)) {
2821 MakeCellVisible(coords
);
2822 // TODO: need to introduce a delay or something here. The
2823 // scrolling is way to fast, at least on MSW.
2827 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2829 int cw
, ch
, left
, dummy
;
2830 m_gridWin
->GetClientSize( &cw
, &ch
);
2831 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2833 wxClientDC
dc( m_gridWin
);
2836 m_rowBottoms
[m_dragRowOrCol
] -
2837 m_rowHeights
[m_dragRowOrCol
] +
2838 WXGRID_MIN_ROW_HEIGHT
);
2839 dc
.SetLogicalFunction(wxINVERT
);
2840 if ( m_dragLastPos
>= 0 )
2842 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2844 dc
.DrawLine( left
, y
, left
+cw
, y
);
2847 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2849 int cw
, ch
, dummy
, top
;
2850 m_gridWin
->GetClientSize( &cw
, &ch
);
2851 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2853 wxClientDC
dc( m_gridWin
);
2856 m_colRights
[m_dragRowOrCol
] -
2857 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
2858 dc
.SetLogicalFunction(wxINVERT
);
2859 if ( m_dragLastPos
>= 0 )
2861 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2863 dc
.DrawLine( x
, top
, x
, top
+ch
);
2870 m_isDragging
= FALSE
;
2871 m_startDragPos
= wxDefaultPosition
;
2874 if ( coords
!= wxGridNoCellCoords
)
2876 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2877 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2880 if ( event
.Entering() || event
.Leaving() )
2882 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2883 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2888 // ------------ Left button pressed
2890 if ( event
.LeftDown() )
2892 EnableCellEditControl( FALSE
);
2893 if ( event
.ShiftDown() )
2895 SelectBlock( m_currentCellCoords
, coords
);
2897 else if ( XToEdgeOfCol(x
) < 0 &&
2898 YToEdgeOfRow(y
) < 0 )
2900 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2905 MakeCellVisible( coords
);
2907 // if this is the second click on this cell then start
2909 if (m_waitForSlowClick
&& coords
== m_currentCellCoords
) {
2910 EnableCellEditControl(TRUE
);
2911 ShowCellEditControl();
2912 m_waitForSlowClick
= FALSE
;
2915 SetCurrentCell( coords
);
2916 m_waitForSlowClick
= TRUE
;
2923 // ------------ Left double click
2925 else if ( event
.LeftDClick() )
2927 EnableCellEditControl( FALSE
);
2928 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2930 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2938 // ------------ Left button released
2940 else if ( event
.LeftUp() )
2942 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2944 if ( IsSelection() )
2947 m_winCapture
->ReleaseMouse();
2948 m_winCapture
= NULL
;
2950 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2953 // Show the edit control, if it has
2954 // been hidden for drag-shrinking.
2955 if ( IsCellEditControlEnabled() )
2956 ShowCellEditControl();
2958 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2960 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2961 DoEndDragResizeRow();
2963 // Note: we are ending the event *after* doing
2964 // default processing in this case
2966 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2968 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2970 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2971 DoEndDragResizeCol();
2973 // Note: we are ending the event *after* doing
2974 // default processing in this case
2976 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2983 // ------------ Right button down
2985 else if ( event
.RightDown() )
2987 EnableCellEditControl( FALSE
);
2988 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2993 // no default action at the moment
2998 // ------------ Right double click
3000 else if ( event
.RightDClick() )
3002 EnableCellEditControl( FALSE
);
3003 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
3008 // no default action at the moment
3012 // ------------ Moving and no button action
3014 else if ( event
.Moving() && !event
.IsButton() )
3016 int dragRow
= YToEdgeOfRow( y
);
3017 int dragCol
= XToEdgeOfCol( x
);
3019 // Dragging on the corner of a cell to resize in both
3020 // directions is not implemented yet...
3022 if ( dragRow
>= 0 && dragCol
>= 0 )
3024 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3030 m_dragRowOrCol
= dragRow
;
3032 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3034 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3042 m_dragRowOrCol
= dragCol
;
3044 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3046 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3052 // Neither on a row or col edge
3054 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3056 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3063 void wxGrid::DoEndDragResizeRow()
3065 if ( m_dragLastPos
>= 0 )
3067 // erase the last line and resize the row
3069 int cw
, ch
, left
, dummy
;
3070 m_gridWin
->GetClientSize( &cw
, &ch
);
3071 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3073 wxClientDC
dc( m_gridWin
);
3075 dc
.SetLogicalFunction( wxINVERT
);
3076 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3077 HideCellEditControl();
3079 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3080 SetRowSize( m_dragRowOrCol
,
3081 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3083 if ( !GetBatchCount() )
3085 // Only needed to get the correct rect.y:
3086 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3088 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3089 rect
.width
= m_rowLabelWidth
;
3090 rect
.height
= ch
- rect
.y
;
3091 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3093 m_gridWin
->Refresh( FALSE
, &rect
);
3096 ShowCellEditControl();
3101 void wxGrid::DoEndDragResizeCol()
3103 if ( m_dragLastPos
>= 0 )
3105 // erase the last line and resize the col
3107 int cw
, ch
, dummy
, top
;
3108 m_gridWin
->GetClientSize( &cw
, &ch
);
3109 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3111 wxClientDC
dc( m_gridWin
);
3113 dc
.SetLogicalFunction( wxINVERT
);
3114 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3115 HideCellEditControl();
3117 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3118 SetColSize( m_dragRowOrCol
,
3119 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3121 if ( !GetBatchCount() )
3123 // Only needed to get the correct rect.x:
3124 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3126 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3127 rect
.width
= cw
- rect
.x
;
3128 rect
.height
= m_colLabelHeight
;
3129 m_colLabelWin
->Refresh( TRUE
, &rect
);
3131 m_gridWin
->Refresh( FALSE
, &rect
);
3134 ShowCellEditControl();
3141 // ------ interaction with data model
3143 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3145 switch ( msg
.GetId() )
3147 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3148 return GetModelValues();
3150 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3151 return SetModelValues();
3153 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3154 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3155 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3156 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3157 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3158 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3159 return Redimension( msg
);
3168 // The behaviour of this function depends on the grid table class
3169 // Clear() function. For the default wxGridStringTable class the
3170 // behavious is to replace all cell contents with wxEmptyString but
3171 // not to change the number of rows or cols.
3173 void wxGrid::ClearGrid()
3178 SetEditControlValue();
3179 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3184 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3186 // TODO: something with updateLabels flag
3190 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3196 bool ok
= m_table
->InsertRows( pos
, numRows
);
3198 // the table will have sent the results of the insert row
3199 // operation to this view object as a grid table message
3203 if ( m_numCols
== 0 )
3205 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3207 // TODO: perhaps instead of appending the default number of cols
3208 // we should remember what the last non-zero number of cols was ?
3212 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3214 // if we have just inserted cols into an empty grid the current
3215 // cell will be undefined...
3217 SetCurrentCell( 0, 0 );
3221 if ( !GetBatchCount() ) Refresh();
3224 SetEditControlValue();
3234 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3236 // TODO: something with updateLabels flag
3240 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3244 if ( m_table
&& m_table
->AppendRows( numRows
) )
3246 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3248 // if we have just inserted cols into an empty grid the current
3249 // cell will be undefined...
3251 SetCurrentCell( 0, 0 );
3254 // the table will have sent the results of the append row
3255 // operation to this view object as a grid table message
3258 if ( !GetBatchCount() ) Refresh();
3268 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3270 // TODO: something with updateLabels flag
3274 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3278 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3280 // the table will have sent the results of the delete row
3281 // operation to this view object as a grid table message
3283 if ( m_numRows
> 0 )
3284 SetEditControlValue();
3286 HideCellEditControl();
3289 if ( !GetBatchCount() ) Refresh();
3299 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3301 // TODO: something with updateLabels flag
3305 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3311 HideCellEditControl();
3312 bool ok
= m_table
->InsertCols( pos
, numCols
);
3314 // the table will have sent the results of the insert col
3315 // operation to this view object as a grid table message
3319 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3321 // if we have just inserted cols into an empty grid the current
3322 // cell will be undefined...
3324 SetCurrentCell( 0, 0 );
3328 if ( !GetBatchCount() ) Refresh();
3331 SetEditControlValue();
3341 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3343 // TODO: something with updateLabels flag
3347 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3351 if ( m_table
&& m_table
->AppendCols( numCols
) )
3353 // the table will have sent the results of the append col
3354 // operation to this view object as a grid table message
3356 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3358 // if we have just inserted cols into an empty grid the current
3359 // cell will be undefined...
3361 SetCurrentCell( 0, 0 );
3365 if ( !GetBatchCount() ) Refresh();
3375 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3377 // TODO: something with updateLabels flag
3381 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3385 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3387 // the table will have sent the results of the delete col
3388 // operation to this view object as a grid table message
3390 if ( m_numCols
> 0 )
3391 SetEditControlValue();
3393 HideCellEditControl();
3396 if ( !GetBatchCount() ) Refresh();
3408 // ----- event handlers
3411 // Generate a grid event based on a mouse event and
3412 // return the result of ProcessEvent()
3414 bool wxGrid::SendEvent( const wxEventType type
,
3416 wxMouseEvent
& mouseEv
)
3418 if ( type
== EVT_GRID_ROW_SIZE
||
3419 type
== EVT_GRID_COL_SIZE
)
3421 int rowOrCol
= (row
== -1 ? col
: row
);
3423 wxGridSizeEvent
gridEvt( GetId(),
3427 mouseEv
.GetX(), mouseEv
.GetY(),
3428 mouseEv
.ControlDown(),
3429 mouseEv
.ShiftDown(),
3431 mouseEv
.MetaDown() );
3433 return GetEventHandler()->ProcessEvent(gridEvt
);
3435 else if ( type
== EVT_GRID_RANGE_SELECT
)
3437 wxGridRangeSelectEvent
gridEvt( GetId(),
3441 m_selectedBottomRight
,
3442 mouseEv
.ControlDown(),
3443 mouseEv
.ShiftDown(),
3445 mouseEv
.MetaDown() );
3447 return GetEventHandler()->ProcessEvent(gridEvt
);
3451 wxGridEvent
gridEvt( GetId(),
3455 mouseEv
.GetX(), mouseEv
.GetY(),
3456 mouseEv
.ControlDown(),
3457 mouseEv
.ShiftDown(),
3459 mouseEv
.MetaDown() );
3461 return GetEventHandler()->ProcessEvent(gridEvt
);
3466 // Generate a grid event of specified type and return the result
3467 // of ProcessEvent().
3469 bool wxGrid::SendEvent( const wxEventType type
,
3472 if ( type
== EVT_GRID_ROW_SIZE
||
3473 type
== EVT_GRID_COL_SIZE
)
3475 int rowOrCol
= (row
== -1 ? col
: row
);
3477 wxGridSizeEvent
gridEvt( GetId(),
3482 return GetEventHandler()->ProcessEvent(gridEvt
);
3486 wxGridEvent
gridEvt( GetId(),
3491 return GetEventHandler()->ProcessEvent(gridEvt
);
3496 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3498 wxPaintDC
dc( this );
3500 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3501 m_numRows
&& m_numCols
)
3503 m_currentCellCoords
.Set(0, 0);
3504 SetEditControlValue();
3505 ShowCellEditControl();
3512 // This is just here to make sure that CalcDimensions gets called when
3513 // the grid view is resized... then the size event is skipped to allow
3514 // the box sizers to handle everything
3516 void wxGrid::OnSize( wxSizeEvent
& event
)
3523 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3525 if ( m_inOnKeyDown
)
3527 // shouldn't be here - we are going round in circles...
3529 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3532 m_inOnKeyDown
= TRUE
;
3534 // propagate the event up and see if it gets processed
3536 wxWindow
*parent
= GetParent();
3537 wxKeyEvent
keyEvt( event
);
3538 keyEvt
.SetEventObject( parent
);
3540 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3543 // TODO: Should also support Shift-cursor keys for
3544 // extending the selection. Maybe add a flag to
3545 // MoveCursorXXX() and MoveCursorXXXBlock() and
3546 // just send event.ShiftDown().
3548 // try local handlers
3550 switch ( event
.KeyCode() )
3553 if ( event
.ControlDown() )
3555 MoveCursorUpBlock();
3564 if ( event
.ControlDown() )
3566 MoveCursorDownBlock();
3575 if ( event
.ControlDown() )
3577 MoveCursorLeftBlock();
3586 if ( event
.ControlDown() )
3588 MoveCursorRightBlock();
3597 if ( event
.ControlDown() )
3599 event
.Skip(); // to let the edit control have the return
3608 if (event
.ShiftDown())
3615 if ( event
.ControlDown() )
3617 MakeCellVisible( 0, 0 );
3618 SetCurrentCell( 0, 0 );
3627 if ( event
.ControlDown() )
3629 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3630 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3646 // We don't want these keys to trigger the edit control, any others?
3655 if ( !IsEditable() )
3660 // Otherwise fall through to default
3663 // now try the cell edit control
3665 if ( !IsCellEditControlEnabled() )
3666 EnableCellEditControl( TRUE
);
3667 if (IsCellEditControlEnabled()) {
3668 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3669 attr
->GetEditor()->StartingKey(event
);
3676 m_inOnKeyDown
= FALSE
;
3680 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3686 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3688 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3690 // the event has been intercepted - do nothing
3695 m_currentCellCoords
!= wxGridNoCellCoords
)
3697 HideCellEditControl();
3698 SaveEditControlValue();
3699 EnableCellEditControl(FALSE
);
3701 // Clear the old current cell highlight
3702 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3703 m_currentCellCoords
= coords
; // Otherwise refresh redraws the highlight!
3704 m_gridWin
->Refresh( FALSE
, &r
);
3707 m_currentCellCoords
= coords
;
3709 SetEditControlValue();
3713 wxClientDC
dc(m_gridWin
);
3715 DrawCellHighlight(dc
);
3717 if ( IsSelection() )
3719 wxRect
r( SelectionToDeviceRect() );
3721 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3728 // ------ functions to get/send data (see also public functions)
3731 bool wxGrid::GetModelValues()
3735 // all we need to do is repaint the grid
3737 m_gridWin
->Refresh();
3745 bool wxGrid::SetModelValues()
3751 for ( row
= 0; row
< m_numRows
; row
++ )
3753 for ( col
= 0; col
< m_numCols
; col
++ )
3755 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3767 // Note - this function only draws cells that are in the list of
3768 // exposed cells (usually set from the update region by
3769 // CalcExposedCells)
3771 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3773 if ( !m_numRows
|| !m_numCols
) return;
3776 size_t numCells
= m_cellsExposed
.GetCount();
3778 for ( i
= 0; i
< numCells
; i
++ )
3780 DrawCell( dc
, m_cellsExposed
[i
] );
3785 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3787 int row
= coords
.GetRow();
3788 int col
= coords
.GetCol();
3790 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3793 // we draw the cell border ourselves
3794 #if !WXGRID_DRAW_LINES
3795 if ( m_gridLinesEnabled
)
3796 DrawCellBorder( dc
, coords
);
3799 // but all the rest is drawn by the cell renderer and hence may be
3802 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3803 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3804 rect
.width
= m_colWidths
[col
]-1;
3805 rect
.height
= m_rowHeights
[row
]-1;
3807 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3808 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3811 if (m_currentCellCoords
== coords
)
3812 DrawCellHighlight(dc
);
3816 void wxGrid::DrawCellHighlight( wxDC
& dc
)
3818 int row
= m_currentCellCoords
.GetRow();
3819 int col
= m_currentCellCoords
.GetCol();
3821 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3825 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3826 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3827 rect
.width
= m_colWidths
[col
] - 1;
3828 rect
.height
= m_rowHeights
[row
] - 1;
3830 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
3831 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3833 dc
.DrawRectangle(rect
);
3836 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3838 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3839 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3841 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3842 int row
= coords
.GetRow();
3843 int col
= coords
.GetCol();
3845 // right hand border
3847 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3848 m_colRights
[col
], m_rowBottoms
[row
] );
3852 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3853 m_colRights
[col
], m_rowBottoms
[row
] );
3857 // TODO: remove this ???
3858 // This is used to redraw all grid lines e.g. when the grid line colour
3861 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3863 if ( !m_gridLinesEnabled
||
3865 !m_numCols
) return;
3867 int top
, bottom
, left
, right
;
3871 m_gridWin
->GetClientSize(&cw
, &ch
);
3873 // virtual coords of visible area
3875 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3876 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3880 reg
.GetBox(x
, y
, w
, h
);
3881 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3882 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3885 // avoid drawing grid lines past the last row and col
3887 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3888 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3890 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3892 // horizontal grid lines
3895 for ( i
= 0; i
< m_numRows
; i
++ )
3897 if ( m_rowBottoms
[i
]-1 > bottom
)
3901 else if ( m_rowBottoms
[i
]-1 >= top
)
3903 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
3908 // vertical grid lines
3910 for ( i
= 0; i
< m_numCols
; i
++ )
3912 if ( m_colRights
[i
]-1 > right
)
3916 else if ( m_colRights
[i
]-1 >= left
)
3918 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
3924 void wxGrid::DrawRowLabels( wxDC
& dc
)
3926 if ( !m_numRows
|| !m_numCols
) return;
3929 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3931 for ( i
= 0; i
< numLabels
; i
++ )
3933 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3938 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3940 if ( m_rowHeights
[row
] <= 0 ) return;
3942 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3944 dc
.SetPen( *wxBLACK_PEN
);
3945 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3946 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3948 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3949 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3951 dc
.SetPen( *wxWHITE_PEN
);
3952 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3953 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3955 dc
.SetBackgroundMode( wxTRANSPARENT
);
3956 dc
.SetTextForeground( GetLabelTextColour() );
3957 dc
.SetFont( GetLabelFont() );
3960 GetRowLabelAlignment( &hAlign
, &vAlign
);
3964 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3965 rect
.SetWidth( m_rowLabelWidth
- 4 );
3966 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3967 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3971 void wxGrid::DrawColLabels( wxDC
& dc
)
3973 if ( !m_numRows
|| !m_numCols
) return;
3976 size_t numLabels
= m_colLabelsExposed
.GetCount();
3978 for ( i
= 0; i
< numLabels
; i
++ )
3980 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3985 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3987 if ( m_colWidths
[col
] <= 0 ) return;
3989 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3991 dc
.SetPen( *wxBLACK_PEN
);
3992 dc
.DrawLine( m_colRights
[col
]-1, 0,
3993 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3995 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3996 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3998 dc
.SetPen( *wxWHITE_PEN
);
3999 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4000 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4002 dc
.SetBackgroundMode( wxTRANSPARENT
);
4003 dc
.SetTextForeground( GetLabelTextColour() );
4004 dc
.SetFont( GetLabelFont() );
4006 dc
.SetBackgroundMode( wxTRANSPARENT
);
4007 dc
.SetTextForeground( GetLabelTextColour() );
4008 dc
.SetFont( GetLabelFont() );
4011 GetColLabelAlignment( &hAlign
, &vAlign
);
4014 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4016 rect
.SetWidth( m_colWidths
[col
] - 4 );
4017 rect
.SetHeight( m_colLabelHeight
- 4 );
4018 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4022 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4023 const wxString
& value
,
4028 long textWidth
, textHeight
;
4029 long lineWidth
, lineHeight
;
4030 wxArrayString lines
;
4032 dc
.SetClippingRegion( rect
);
4033 StringToLines( value
, lines
);
4034 if ( lines
.GetCount() )
4036 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4037 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4040 switch ( horizAlign
)
4043 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4047 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4056 switch ( vertAlign
)
4059 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4063 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4072 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4074 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4079 dc
.DestroyClippingRegion();
4083 // Split multi line text up into an array of strings. Any existing
4084 // contents of the string array are preserved.
4086 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4090 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4091 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4093 while ( startPos
< (int)tVal
.Length() )
4095 pos
= tVal
.Mid(startPos
).Find( eol
);
4100 else if ( pos
== 0 )
4102 lines
.Add( wxEmptyString
);
4106 lines
.Add( value
.Mid(startPos
, pos
) );
4110 if ( startPos
< (int)value
.Length() )
4112 lines
.Add( value
.Mid( startPos
) );
4117 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4118 wxArrayString
& lines
,
4119 long *width
, long *height
)
4126 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4128 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4129 w
= wxMax( w
, lineW
);
4139 // ------ Edit control functions
4143 void wxGrid::EnableEditing( bool edit
)
4145 // TODO: improve this ?
4147 if ( edit
!= m_editable
)
4151 EnableCellEditControl(m_editable
);
4156 void wxGrid::EnableCellEditControl( bool enable
)
4161 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4162 SetCurrentCell( 0, 0 );
4164 if ( enable
!= m_cellEditCtrlEnabled
)
4168 m_cellEditCtrlEnabled
= enable
;
4169 SetEditControlValue();
4170 ShowCellEditControl();
4174 HideCellEditControl();
4175 SaveEditControlValue();
4176 m_cellEditCtrlEnabled
= enable
;
4182 void wxGrid::ShowCellEditControl()
4184 if ( IsCellEditControlEnabled() )
4186 if ( !IsVisible( m_currentCellCoords
) )
4192 wxRect rect
= CellToRect( m_currentCellCoords
);
4193 int row
= m_currentCellCoords
.GetRow();
4194 int col
= m_currentCellCoords
.GetCol();
4196 // convert to scrolled coords
4198 int left
, top
, right
, bottom
;
4199 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4200 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4201 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4203 m_gridWin
->GetClientSize( &cw
, &ch
);
4205 // Make the edit control large enough to allow for internal margins
4206 // TODO: remove this if the text ctrl sizing is improved esp. for unix
4209 #if defined(__WXMOTIF__)
4210 if ( row
== 0 || col
== 0 )
4219 if ( row
== 0 || col
== 0 )
4229 #if defined(__WXGTK__)
4232 if (left
!= 0) left_diff
++;
4233 if (top
!= 0) top_diff
++;
4234 rect
.SetLeft( left
+ left_diff
);
4235 rect
.SetTop( top
+ top_diff
);
4236 rect
.SetRight( rect
.GetRight() - left_diff
);
4237 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4239 rect
.SetLeft( wxMax(0, left
- extra
) );
4240 rect
.SetTop( wxMax(0, top
- extra
) );
4241 rect
.SetRight( rect
.GetRight() + 2*extra
);
4242 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4245 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4246 wxGridCellEditor
* editor
= attr
->GetEditor();
4247 if (! editor
->IsCreated()) {
4248 editor
->Create(m_gridWin
, -1,
4249 new wxGridCellEditorEvtHandler(this, editor
));
4252 editor
->SetSize( rect
);
4253 editor
->Show( TRUE
);
4254 editor
->BeginEdit(row
, col
, this, attr
);
4261 void wxGrid::HideCellEditControl()
4263 if ( IsCellEditControlEnabled() )
4265 int row
= m_currentCellCoords
.GetRow();
4266 int col
= m_currentCellCoords
.GetCol();
4268 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4269 attr
->GetEditor()->Show( FALSE
);
4271 m_gridWin
->SetFocus();
4276 void wxGrid::SetEditControlValue( const wxString
& value
)
4278 // RD: The new Editors get the value from the table themselves now. This
4279 // method can probably be removed...
4283 void wxGrid::SaveEditControlValue()
4285 if (IsCellEditControlEnabled()) {
4286 int row
= m_currentCellCoords
.GetRow();
4287 int col
= m_currentCellCoords
.GetCol();
4289 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4290 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this, attr
);
4295 SendEvent( EVT_GRID_CELL_CHANGE
,
4296 m_currentCellCoords
.GetRow(),
4297 m_currentCellCoords
.GetCol() );
4304 // ------ Grid location functions
4305 // Note that all of these functions work with the logical coordinates of
4306 // grid cells and labels so you will need to convert from device
4307 // coordinates for mouse events etc.
4310 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4312 int row
= YToRow(y
);
4313 int col
= XToCol(x
);
4315 if ( row
== -1 || col
== -1 )
4317 coords
= wxGridNoCellCoords
;
4321 coords
.Set( row
, col
);
4326 int wxGrid::YToRow( int y
)
4330 for ( i
= 0; i
< m_numRows
; i
++ )
4332 if ( y
< m_rowBottoms
[i
] ) return i
;
4335 return m_numRows
; //-1;
4339 int wxGrid::XToCol( int x
)
4343 for ( i
= 0; i
< m_numCols
; i
++ )
4345 if ( x
< m_colRights
[i
] ) return i
;
4348 return m_numCols
; //-1;
4352 // return the row number that that the y coord is near the edge of, or
4353 // -1 if not near an edge
4355 int wxGrid::YToEdgeOfRow( int y
)
4359 for ( i
= 0; i
< m_numRows
; i
++ )
4361 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4363 d
= abs( y
- m_rowBottoms
[i
] );
4365 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4374 // return the col number that that the x coord is near the edge of, or
4375 // -1 if not near an edge
4377 int wxGrid::XToEdgeOfCol( int x
)
4381 for ( i
= 0; i
< m_numCols
; i
++ )
4383 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4385 d
= abs( x
- m_colRights
[i
] );
4387 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4396 wxRect
wxGrid::CellToRect( int row
, int col
)
4398 wxRect
rect( -1, -1, -1, -1 );
4400 if ( row
>= 0 && row
< m_numRows
&&
4401 col
>= 0 && col
< m_numCols
)
4403 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4404 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4405 rect
.width
= m_colWidths
[col
];
4406 rect
.height
= m_rowHeights
[ row
];
4413 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4415 // get the cell rectangle in logical coords
4417 wxRect
r( CellToRect( row
, col
) );
4419 // convert to device coords
4421 int left
, top
, right
, bottom
;
4422 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4423 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4425 // check against the client area of the grid window
4428 m_gridWin
->GetClientSize( &cw
, &ch
);
4430 if ( wholeCellVisible
)
4432 // is the cell wholly visible ?
4434 return ( left
>= 0 && right
<= cw
&&
4435 top
>= 0 && bottom
<= ch
);
4439 // is the cell partly visible ?
4441 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4442 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4447 // make the specified cell location visible by doing a minimal amount
4450 void wxGrid::MakeCellVisible( int row
, int col
)
4453 int xpos
= -1, ypos
= -1;
4455 if ( row
>= 0 && row
< m_numRows
&&
4456 col
>= 0 && col
< m_numCols
)
4458 // get the cell rectangle in logical coords
4460 wxRect
r( CellToRect( row
, col
) );
4462 // convert to device coords
4464 int left
, top
, right
, bottom
;
4465 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4466 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4469 m_gridWin
->GetClientSize( &cw
, &ch
);
4475 else if ( bottom
> ch
)
4477 int h
= r
.GetHeight();
4479 for ( i
= row
-1; i
>= 0; i
-- )
4481 if ( h
+ m_rowHeights
[i
] > ch
) break;
4483 h
+= m_rowHeights
[i
];
4484 ypos
-= m_rowHeights
[i
];
4487 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4488 // have rounding errors (this is important, because if we do, we
4489 // might not scroll at all and some cells won't be redrawn)
4490 ypos
+= GRID_SCROLL_LINE
/ 2;
4497 else if ( right
> cw
)
4499 int w
= r
.GetWidth();
4501 for ( i
= col
-1; i
>= 0; i
-- )
4503 if ( w
+ m_colWidths
[i
] > cw
) break;
4505 w
+= m_colWidths
[i
];
4506 xpos
-= m_colWidths
[i
];
4509 // see comment for ypos above
4510 xpos
+= GRID_SCROLL_LINE
/ 2;
4513 if ( xpos
!= -1 || ypos
!= -1 )
4515 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4516 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4517 Scroll( xpos
, ypos
);
4525 // ------ Grid cursor movement functions
4528 bool wxGrid::MoveCursorUp()
4530 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4531 m_currentCellCoords
.GetRow() > 0 )
4533 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4534 m_currentCellCoords
.GetCol() );
4536 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4537 m_currentCellCoords
.GetCol() );
4546 bool wxGrid::MoveCursorDown()
4548 // TODO: allow for scrolling
4550 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4551 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4553 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4554 m_currentCellCoords
.GetCol() );
4556 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4557 m_currentCellCoords
.GetCol() );
4566 bool wxGrid::MoveCursorLeft()
4568 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4569 m_currentCellCoords
.GetCol() > 0 )
4571 MakeCellVisible( m_currentCellCoords
.GetRow(),
4572 m_currentCellCoords
.GetCol() - 1 );
4574 SetCurrentCell( m_currentCellCoords
.GetRow(),
4575 m_currentCellCoords
.GetCol() - 1 );
4584 bool wxGrid::MoveCursorRight()
4586 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4587 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4589 MakeCellVisible( m_currentCellCoords
.GetRow(),
4590 m_currentCellCoords
.GetCol() + 1 );
4592 SetCurrentCell( m_currentCellCoords
.GetRow(),
4593 m_currentCellCoords
.GetCol() + 1 );
4602 bool wxGrid::MovePageUp()
4604 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4606 int row
= m_currentCellCoords
.GetRow();
4610 m_gridWin
->GetClientSize( &cw
, &ch
);
4612 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4613 int newRow
= YToRow( y
- ch
+ 1 );
4618 else if ( newRow
== row
)
4623 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4624 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4632 bool wxGrid::MovePageDown()
4634 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4636 int row
= m_currentCellCoords
.GetRow();
4637 if ( row
< m_numRows
)
4640 m_gridWin
->GetClientSize( &cw
, &ch
);
4642 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4643 int newRow
= YToRow( y
+ ch
);
4646 newRow
= m_numRows
- 1;
4648 else if ( newRow
== row
)
4653 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4654 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4662 bool wxGrid::MoveCursorUpBlock()
4665 m_currentCellCoords
!= wxGridNoCellCoords
&&
4666 m_currentCellCoords
.GetRow() > 0 )
4668 int row
= m_currentCellCoords
.GetRow();
4669 int col
= m_currentCellCoords
.GetCol();
4671 if ( m_table
->IsEmptyCell(row
, col
) )
4673 // starting in an empty cell: find the next block of
4679 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4682 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4684 // starting at the top of a block: find the next block
4690 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4695 // starting within a block: find the top of the block
4700 if ( m_table
->IsEmptyCell(row
, col
) )
4708 MakeCellVisible( row
, col
);
4709 SetCurrentCell( row
, col
);
4717 bool wxGrid::MoveCursorDownBlock()
4720 m_currentCellCoords
!= wxGridNoCellCoords
&&
4721 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4723 int row
= m_currentCellCoords
.GetRow();
4724 int col
= m_currentCellCoords
.GetCol();
4726 if ( m_table
->IsEmptyCell(row
, col
) )
4728 // starting in an empty cell: find the next block of
4731 while ( row
< m_numRows
-1 )
4734 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4737 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4739 // starting at the bottom of a block: find the next block
4742 while ( row
< m_numRows
-1 )
4745 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4750 // starting within a block: find the bottom of the block
4752 while ( row
< m_numRows
-1 )
4755 if ( m_table
->IsEmptyCell(row
, col
) )
4763 MakeCellVisible( row
, col
);
4764 SetCurrentCell( row
, col
);
4772 bool wxGrid::MoveCursorLeftBlock()
4775 m_currentCellCoords
!= wxGridNoCellCoords
&&
4776 m_currentCellCoords
.GetCol() > 0 )
4778 int row
= m_currentCellCoords
.GetRow();
4779 int col
= m_currentCellCoords
.GetCol();
4781 if ( m_table
->IsEmptyCell(row
, col
) )
4783 // starting in an empty cell: find the next block of
4789 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4792 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4794 // starting at the left of a block: find the next block
4800 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4805 // starting within a block: find the left of the block
4810 if ( m_table
->IsEmptyCell(row
, col
) )
4818 MakeCellVisible( row
, col
);
4819 SetCurrentCell( row
, col
);
4827 bool wxGrid::MoveCursorRightBlock()
4830 m_currentCellCoords
!= wxGridNoCellCoords
&&
4831 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4833 int row
= m_currentCellCoords
.GetRow();
4834 int col
= m_currentCellCoords
.GetCol();
4836 if ( m_table
->IsEmptyCell(row
, col
) )
4838 // starting in an empty cell: find the next block of
4841 while ( col
< m_numCols
-1 )
4844 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4847 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4849 // starting at the right of a block: find the next block
4852 while ( col
< m_numCols
-1 )
4855 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4860 // starting within a block: find the right of the block
4862 while ( col
< m_numCols
-1 )
4865 if ( m_table
->IsEmptyCell(row
, col
) )
4873 MakeCellVisible( row
, col
);
4874 SetCurrentCell( row
, col
);
4885 // ------ Label values and formatting
4888 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4890 *horiz
= m_rowLabelHorizAlign
;
4891 *vert
= m_rowLabelVertAlign
;
4894 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4896 *horiz
= m_colLabelHorizAlign
;
4897 *vert
= m_colLabelVertAlign
;
4900 wxString
wxGrid::GetRowLabelValue( int row
)
4904 return m_table
->GetRowLabelValue( row
);
4914 wxString
wxGrid::GetColLabelValue( int col
)
4918 return m_table
->GetColLabelValue( col
);
4929 void wxGrid::SetRowLabelSize( int width
)
4931 width
= wxMax( width
, 0 );
4932 if ( width
!= m_rowLabelWidth
)
4936 m_rowLabelWin
->Show( FALSE
);
4937 m_cornerLabelWin
->Show( FALSE
);
4939 else if ( m_rowLabelWidth
== 0 )
4941 m_rowLabelWin
->Show( TRUE
);
4942 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4945 m_rowLabelWidth
= width
;
4952 void wxGrid::SetColLabelSize( int height
)
4954 height
= wxMax( height
, 0 );
4955 if ( height
!= m_colLabelHeight
)
4959 m_colLabelWin
->Show( FALSE
);
4960 m_cornerLabelWin
->Show( FALSE
);
4962 else if ( m_colLabelHeight
== 0 )
4964 m_colLabelWin
->Show( TRUE
);
4965 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4968 m_colLabelHeight
= height
;
4975 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4977 if ( m_labelBackgroundColour
!= colour
)
4979 m_labelBackgroundColour
= colour
;
4980 m_rowLabelWin
->SetBackgroundColour( colour
);
4981 m_colLabelWin
->SetBackgroundColour( colour
);
4982 m_cornerLabelWin
->SetBackgroundColour( colour
);
4984 if ( !GetBatchCount() )
4986 m_rowLabelWin
->Refresh();
4987 m_colLabelWin
->Refresh();
4988 m_cornerLabelWin
->Refresh();
4993 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4995 if ( m_labelTextColour
!= colour
)
4997 m_labelTextColour
= colour
;
4998 if ( !GetBatchCount() )
5000 m_rowLabelWin
->Refresh();
5001 m_colLabelWin
->Refresh();
5006 void wxGrid::SetLabelFont( const wxFont
& font
)
5009 if ( !GetBatchCount() )
5011 m_rowLabelWin
->Refresh();
5012 m_colLabelWin
->Refresh();
5016 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5018 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5020 m_rowLabelHorizAlign
= horiz
;
5023 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5025 m_rowLabelVertAlign
= vert
;
5028 if ( !GetBatchCount() )
5030 m_rowLabelWin
->Refresh();
5034 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5036 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5038 m_colLabelHorizAlign
= horiz
;
5041 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5043 m_colLabelVertAlign
= vert
;
5046 if ( !GetBatchCount() )
5048 m_colLabelWin
->Refresh();
5052 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5056 m_table
->SetRowLabelValue( row
, s
);
5057 if ( !GetBatchCount() )
5059 wxRect rect
= CellToRect( row
, 0);
5060 if ( rect
.height
> 0 )
5062 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5064 rect
.width
= m_rowLabelWidth
;
5065 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5071 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5075 m_table
->SetColLabelValue( col
, s
);
5076 if ( !GetBatchCount() )
5078 wxRect rect
= CellToRect( 0, col
);
5079 if ( rect
.width
> 0 )
5081 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5083 rect
.height
= m_colLabelHeight
;
5084 m_colLabelWin
->Refresh( TRUE
, &rect
);
5090 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5092 if ( m_gridLineColour
!= colour
)
5094 m_gridLineColour
= colour
;
5096 wxClientDC
dc( m_gridWin
);
5098 DrawAllGridLines( dc
, wxRegion() );
5102 void wxGrid::EnableGridLines( bool enable
)
5104 if ( enable
!= m_gridLinesEnabled
)
5106 m_gridLinesEnabled
= enable
;
5108 if ( !GetBatchCount() )
5112 wxClientDC
dc( m_gridWin
);
5114 DrawAllGridLines( dc
, wxRegion() );
5118 m_gridWin
->Refresh();
5125 int wxGrid::GetDefaultRowSize()
5127 return m_defaultRowHeight
;
5130 int wxGrid::GetRowSize( int row
)
5132 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5134 return m_rowHeights
[row
];
5137 int wxGrid::GetDefaultColSize()
5139 return m_defaultColWidth
;
5142 int wxGrid::GetColSize( int col
)
5144 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5146 return m_colWidths
[col
];
5149 // ============================================================================
5150 // access to the grid attributes: each of them has a default value in the grid
5151 // itself and may be overidden on a per-cell basis
5152 // ============================================================================
5154 // ----------------------------------------------------------------------------
5155 // setting default attributes
5156 // ----------------------------------------------------------------------------
5158 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5160 m_defaultCellAttr
->SetBackgroundColour(col
);
5163 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5165 m_defaultCellAttr
->SetTextColour(col
);
5168 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5170 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5173 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5175 m_defaultCellAttr
->SetFont(font
);
5178 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5180 m_defaultCellAttr
->SetRenderer(renderer
);
5183 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5185 m_defaultCellAttr
->SetEditor(editor
);
5188 // ----------------------------------------------------------------------------
5189 // access to the default attrbiutes
5190 // ----------------------------------------------------------------------------
5192 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5194 return m_defaultCellAttr
->GetBackgroundColour();
5197 wxColour
wxGrid::GetDefaultCellTextColour()
5199 return m_defaultCellAttr
->GetTextColour();
5202 wxFont
wxGrid::GetDefaultCellFont()
5204 return m_defaultCellAttr
->GetFont();
5207 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5209 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5212 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5214 return m_defaultCellAttr
->GetRenderer();
5217 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5219 return m_defaultCellAttr
->GetEditor();
5222 // ----------------------------------------------------------------------------
5223 // access to cell attributes
5224 // ----------------------------------------------------------------------------
5226 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5228 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5229 wxColour colour
= attr
->GetBackgroundColour();
5234 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5236 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5237 wxColour colour
= attr
->GetTextColour();
5242 wxFont
wxGrid::GetCellFont( int row
, int col
)
5244 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5245 wxFont font
= attr
->GetFont();
5250 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5252 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5253 attr
->GetAlignment(horiz
, vert
);
5257 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5259 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5260 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5265 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5267 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5268 wxGridCellEditor
* editor
= attr
->GetEditor();
5273 // ----------------------------------------------------------------------------
5274 // attribute support: cache, automatic provider creation, ...
5275 // ----------------------------------------------------------------------------
5277 bool wxGrid::CanHaveAttributes()
5284 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5285 // table is providing the attributes itself??? In which case
5286 // I don't think the grid should create a Provider object for the
5287 // table but the table should be smart enough to do that on its own.
5288 if ( !m_table
->GetAttrProvider() )
5290 // use the default attr provider by default
5291 // (another choice would be to just return FALSE thus forcing the user
5293 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5299 void wxGrid::ClearAttrCache()
5301 if ( m_attrCache
.row
!= -1 )
5303 m_attrCache
.attr
->SafeDecRef();
5304 m_attrCache
.row
= -1;
5308 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5310 wxGrid
*self
= (wxGrid
*)this; // const_cast
5312 self
->ClearAttrCache();
5313 self
->m_attrCache
.row
= row
;
5314 self
->m_attrCache
.col
= col
;
5315 self
->m_attrCache
.attr
= attr
;
5319 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5321 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5323 *attr
= m_attrCache
.attr
;
5324 (*attr
)->SafeIncRef();
5326 #ifdef DEBUG_ATTR_CACHE
5327 gs_nAttrCacheHits
++;
5334 #ifdef DEBUG_ATTR_CACHE
5335 gs_nAttrCacheMisses
++;
5341 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5343 wxGridCellAttr
*attr
;
5344 if ( !LookupAttr(row
, col
, &attr
) )
5346 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5347 CacheAttr(row
, col
, attr
);
5350 attr
->SetDefAttr(m_defaultCellAttr
);
5352 attr
= m_defaultCellAttr
;
5359 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5361 wxGridCellAttr
*attr
;
5362 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5364 wxASSERT_MSG( m_table
,
5365 _T("we may only be called if CanHaveAttributes() "
5366 "returned TRUE and then m_table should be !NULL") );
5368 attr
= m_table
->GetAttr(row
, col
);
5371 attr
= new wxGridCellAttr
;
5373 // artificially inc the ref count to match DecRef() in caller
5376 m_table
->SetAttr(attr
, row
, col
);
5379 CacheAttr(row
, col
, attr
);
5381 attr
->SetDefAttr(m_defaultCellAttr
);
5385 // ----------------------------------------------------------------------------
5386 // setting cell attributes: this is forwarded to the table
5387 // ----------------------------------------------------------------------------
5389 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5391 if ( CanHaveAttributes() )
5393 m_table
->SetRowAttr(attr
, row
);
5401 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5403 if ( CanHaveAttributes() )
5405 m_table
->SetColAttr(attr
, col
);
5413 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5415 if ( CanHaveAttributes() )
5417 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5418 attr
->SetBackgroundColour(colour
);
5423 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5425 if ( CanHaveAttributes() )
5427 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5428 attr
->SetTextColour(colour
);
5433 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5435 if ( CanHaveAttributes() )
5437 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5438 attr
->SetFont(font
);
5443 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5445 if ( CanHaveAttributes() )
5447 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5448 attr
->SetAlignment(horiz
, vert
);
5453 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5455 if ( CanHaveAttributes() )
5457 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5458 attr
->SetRenderer(renderer
);
5463 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5465 if ( CanHaveAttributes() )
5467 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5468 attr
->SetEditor(editor
);
5473 // ----------------------------------------------------------------------------
5475 // ----------------------------------------------------------------------------
5477 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5479 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5481 if ( resizeExistingRows
)
5485 for ( row
= 0; row
< m_numRows
; row
++ )
5487 m_rowHeights
[row
] = m_defaultRowHeight
;
5488 bottom
+= m_defaultRowHeight
;
5489 m_rowBottoms
[row
] = bottom
;
5495 void wxGrid::SetRowSize( int row
, int height
)
5497 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5501 int h
= wxMax( 0, height
);
5502 int diff
= h
- m_rowHeights
[row
];
5504 m_rowHeights
[row
] = h
;
5505 for ( i
= row
; i
< m_numRows
; i
++ )
5507 m_rowBottoms
[i
] += diff
;
5512 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5514 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5516 if ( resizeExistingCols
)
5520 for ( col
= 0; col
< m_numCols
; col
++ )
5522 m_colWidths
[col
] = m_defaultColWidth
;
5523 right
+= m_defaultColWidth
;
5524 m_colRights
[col
] = right
;
5530 void wxGrid::SetColSize( int col
, int width
)
5532 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5536 int w
= wxMax( 0, width
);
5537 int diff
= w
- m_colWidths
[col
];
5538 m_colWidths
[col
] = w
;
5540 for ( i
= col
; i
< m_numCols
; i
++ )
5542 m_colRights
[i
] += diff
;
5549 // ------ cell value accessor functions
5552 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5556 m_table
->SetValue( row
, col
, s
.c_str() );
5557 if ( !GetBatchCount() )
5559 wxClientDC
dc( m_gridWin
);
5561 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5564 #if 0 // TODO: edit in place
5566 if ( m_currentCellCoords
.GetRow() == row
&&
5567 m_currentCellCoords
.GetCol() == col
)
5569 SetEditControlValue( s
);
5578 // ------ Block, row and col selection
5581 void wxGrid::SelectRow( int row
, bool addToSelected
)
5585 if ( IsSelection() && addToSelected
)
5588 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5591 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5592 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5593 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5594 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5598 need_refresh
[0] = TRUE
;
5599 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5600 wxGridCellCoords ( oldTop
- 1,
5602 m_selectedTopLeft
.SetRow( row
);
5607 need_refresh
[1] = TRUE
;
5608 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5609 wxGridCellCoords ( oldBottom
,
5612 m_selectedTopLeft
.SetCol( 0 );
5615 if ( oldBottom
< row
)
5617 need_refresh
[2] = TRUE
;
5618 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5619 wxGridCellCoords ( row
,
5621 m_selectedBottomRight
.SetRow( row
);
5624 if ( oldRight
< m_numCols
- 1 )
5626 need_refresh
[3] = TRUE
;
5627 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5629 wxGridCellCoords ( oldBottom
,
5631 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5634 for (i
= 0; i
< 4; i
++ )
5635 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5636 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5640 r
= SelectionToDeviceRect();
5642 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5644 m_selectedTopLeft
.Set( row
, 0 );
5645 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5646 r
= SelectionToDeviceRect();
5647 m_gridWin
->Refresh( FALSE
, &r
);
5650 wxGridRangeSelectEvent
gridEvt( GetId(),
5651 EVT_GRID_RANGE_SELECT
,
5654 m_selectedBottomRight
);
5656 GetEventHandler()->ProcessEvent(gridEvt
);
5660 void wxGrid::SelectCol( int col
, bool addToSelected
)
5662 if ( IsSelection() && addToSelected
)
5665 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5668 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5669 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5670 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5671 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5673 if ( oldLeft
> col
)
5675 need_refresh
[0] = TRUE
;
5676 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5677 wxGridCellCoords ( m_numRows
- 1,
5679 m_selectedTopLeft
.SetCol( col
);
5684 need_refresh
[1] = TRUE
;
5685 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5686 wxGridCellCoords ( oldTop
- 1,
5688 m_selectedTopLeft
.SetRow( 0 );
5691 if ( oldRight
< col
)
5693 need_refresh
[2] = TRUE
;
5694 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5695 wxGridCellCoords ( m_numRows
- 1,
5697 m_selectedBottomRight
.SetCol( col
);
5700 if ( oldBottom
< m_numRows
- 1 )
5702 need_refresh
[3] = TRUE
;
5703 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5705 wxGridCellCoords ( m_numRows
- 1,
5707 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5710 for (i
= 0; i
< 4; i
++ )
5711 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5712 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5718 r
= SelectionToDeviceRect();
5720 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5722 m_selectedTopLeft
.Set( 0, col
);
5723 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5724 r
= SelectionToDeviceRect();
5725 m_gridWin
->Refresh( FALSE
, &r
);
5728 wxGridRangeSelectEvent
gridEvt( GetId(),
5729 EVT_GRID_RANGE_SELECT
,
5732 m_selectedBottomRight
);
5734 GetEventHandler()->ProcessEvent(gridEvt
);
5738 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5741 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5743 if ( topRow
> bottomRow
)
5750 if ( leftCol
> rightCol
)
5757 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5758 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5760 if ( m_selectedTopLeft
!= updateTopLeft
||
5761 m_selectedBottomRight
!= updateBottomRight
)
5763 // Compute two optimal update rectangles:
5764 // Either one rectangle is a real subset of the
5765 // other, or they are (almost) disjoint!
5767 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5770 // Store intermediate values
5771 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5772 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5773 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5774 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5776 // Determine the outer/inner coordinates.
5777 if (oldLeft
> leftCol
)
5783 if (oldTop
> topRow
)
5789 if (oldRight
< rightCol
)
5792 oldRight
= rightCol
;
5795 if (oldBottom
< bottomRow
)
5798 oldBottom
= bottomRow
;
5802 // Now, either the stuff marked old is the outer
5803 // rectangle or we don't have a situation where one
5804 // is contained in the other.
5806 if ( oldLeft
< leftCol
)
5808 need_refresh
[0] = TRUE
;
5809 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5811 wxGridCellCoords ( oldBottom
,
5815 if ( oldTop
< topRow
)
5817 need_refresh
[1] = TRUE
;
5818 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5820 wxGridCellCoords ( topRow
- 1,
5824 if ( oldRight
> rightCol
)
5826 need_refresh
[2] = TRUE
;
5827 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5829 wxGridCellCoords ( oldBottom
,
5833 if ( oldBottom
> bottomRow
)
5835 need_refresh
[3] = TRUE
;
5836 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5838 wxGridCellCoords ( oldBottom
,
5844 m_selectedTopLeft
= updateTopLeft
;
5845 m_selectedBottomRight
= updateBottomRight
;
5847 // various Refresh() calls
5848 for (i
= 0; i
< 4; i
++ )
5849 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5850 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5853 // only generate an event if the block is not being selected by
5854 // dragging the mouse (in which case the event will be generated in
5855 // the mouse event handler)
5856 if ( !m_isDragging
)
5858 wxGridRangeSelectEvent
gridEvt( GetId(),
5859 EVT_GRID_RANGE_SELECT
,
5862 m_selectedBottomRight
);
5864 GetEventHandler()->ProcessEvent(gridEvt
);
5868 void wxGrid::SelectAll()
5870 m_selectedTopLeft
.Set( 0, 0 );
5871 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5873 m_gridWin
->Refresh();
5877 void wxGrid::ClearSelection()
5879 m_selectedTopLeft
= wxGridNoCellCoords
;
5880 m_selectedBottomRight
= wxGridNoCellCoords
;
5884 // This function returns the rectangle that encloses the given block
5885 // in device coords clipped to the client size of the grid window.
5887 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5888 const wxGridCellCoords
&bottomRight
)
5890 wxRect
rect( wxGridNoCellRect
);
5893 cellRect
= CellToRect( topLeft
);
5894 if ( cellRect
!= wxGridNoCellRect
)
5900 rect
= wxRect( 0, 0, 0, 0 );
5903 cellRect
= CellToRect( bottomRight
);
5904 if ( cellRect
!= wxGridNoCellRect
)
5910 return wxGridNoCellRect
;
5913 // convert to scrolled coords
5915 int left
, top
, right
, bottom
;
5916 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5917 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5920 m_gridWin
->GetClientSize( &cw
, &ch
);
5922 rect
.SetLeft( wxMax(0, left
) );
5923 rect
.SetTop( wxMax(0, top
) );
5924 rect
.SetRight( wxMin(cw
, right
) );
5925 rect
.SetBottom( wxMin(ch
, bottom
) );
5933 // ------ Grid event classes
5936 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5938 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5939 int row
, int col
, int x
, int y
,
5940 bool control
, bool shift
, bool alt
, bool meta
)
5941 : wxNotifyEvent( type
, id
)
5947 m_control
= control
;
5952 SetEventObject(obj
);
5956 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5958 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5959 int rowOrCol
, int x
, int y
,
5960 bool control
, bool shift
, bool alt
, bool meta
)
5961 : wxNotifyEvent( type
, id
)
5963 m_rowOrCol
= rowOrCol
;
5966 m_control
= control
;
5971 SetEventObject(obj
);
5975 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5977 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5978 const wxGridCellCoords
& topLeft
,
5979 const wxGridCellCoords
& bottomRight
,
5980 bool control
, bool shift
, bool alt
, bool meta
)
5981 : wxNotifyEvent( type
, id
)
5983 m_topLeft
= topLeft
;
5984 m_bottomRight
= bottomRight
;
5985 m_control
= control
;
5990 SetEventObject(obj
);
5994 #endif // ifndef wxUSE_NEW_GRID