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
);
443 // // send the event to the parent grid, skipping the
444 // // event if nothing happens
446 // event.Skip( m_grid->ProcessEvent( event ) );
451 if (!m_grid
->ProcessEvent(event
))
452 m_editor
->HandleReturn(event
);
461 // ----------------------------------------------------------------------------
462 // wxGridCellRenderer
463 // ----------------------------------------------------------------------------
465 void wxGridCellRenderer::Draw(wxGrid
& grid
,
466 wxGridCellAttr
& attr
,
472 dc
.SetBackgroundMode( wxSOLID
);
476 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
480 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
483 dc
.SetPen( *wxTRANSPARENT_PEN
);
484 dc
.DrawRectangle(rect
);
487 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
488 wxGridCellAttr
& attr
,
490 const wxRect
& rectCell
,
494 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
496 // now we only have to draw the text
497 dc
.SetBackgroundMode( wxTRANSPARENT
);
501 dc
.SetTextBackground( grid
.GetSelectionBackground() );
502 dc
.SetTextForeground( grid
.GetSelectionForeground() );
506 dc
.SetTextBackground( attr
.GetBackgroundColour() );
507 dc
.SetTextForeground( attr
.GetTextColour() );
509 dc
.SetFont( attr
.GetFont() );
512 attr
.GetAlignment(&hAlign
, &vAlign
);
514 wxRect rect
= rectCell
;
520 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
521 rect
, hAlign
, vAlign
);
524 // ----------------------------------------------------------------------------
526 // ----------------------------------------------------------------------------
528 const wxColour
& wxGridCellAttr::GetTextColour() const
532 else if (m_defGridAttr
!= this)
533 return m_defGridAttr
->GetTextColour();
535 wxFAIL_MSG(wxT("Missing default cell attribute"));
541 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
543 if (HasBackgroundColour())
545 else if (m_defGridAttr
!= this)
546 return m_defGridAttr
->GetBackgroundColour();
548 wxFAIL_MSG(wxT("Missing default cell attribute"));
554 const wxFont
& wxGridCellAttr::GetFont() const
558 else if (m_defGridAttr
!= this)
559 return m_defGridAttr
->GetFont();
561 wxFAIL_MSG(wxT("Missing default cell attribute"));
567 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
569 if (HasAlignment()) {
570 if ( hAlign
) *hAlign
= m_hAlign
;
571 if ( vAlign
) *vAlign
= m_vAlign
;
573 else if (m_defGridAttr
!= this)
574 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
576 wxFAIL_MSG(wxT("Missing default cell attribute"));
581 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
585 else if (m_defGridAttr
!= this)
586 return m_defGridAttr
->GetRenderer();
588 wxFAIL_MSG(wxT("Missing default cell attribute"));
593 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
597 else if (m_defGridAttr
!= this)
598 return m_defGridAttr
->GetEditor();
600 wxFAIL_MSG(wxT("Missing default cell attribute"));
605 // ----------------------------------------------------------------------------
606 // wxGridCellAttrData
607 // ----------------------------------------------------------------------------
609 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
611 int n
= FindIndex(row
, col
);
612 if ( n
== wxNOT_FOUND
)
615 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
621 // change the attribute
622 m_attrs
[(size_t)n
].attr
= attr
;
626 // remove this attribute
627 m_attrs
.RemoveAt((size_t)n
);
632 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
634 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
636 int n
= FindIndex(row
, col
);
637 if ( n
!= wxNOT_FOUND
)
639 attr
= m_attrs
[(size_t)n
].attr
;
646 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
648 size_t count
= m_attrs
.GetCount();
649 for ( size_t n
= 0; n
< count
; n
++ )
651 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
652 wxCoord row
= coords
.GetRow();
653 if ((size_t)row
>= pos
)
657 // If rows inserted, include row counter where necessary
658 coords
.SetRow(row
+ numRows
);
660 else if (numRows
< 0)
662 // If rows deleted ...
663 if ((size_t)row
>= pos
- numRows
)
665 // ...either decrement row counter (if row still exists)...
666 coords
.SetRow(row
+ numRows
);
670 // ...or remove the attribute
671 m_attrs
.RemoveAt((size_t)n
);
679 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
681 size_t count
= m_attrs
.GetCount();
682 for ( size_t n
= 0; n
< count
; n
++ )
684 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
685 wxCoord col
= coords
.GetCol();
686 if ( (size_t)col
>= pos
)
690 // If rows inserted, include row counter where necessary
691 coords
.SetCol(col
+ numCols
);
693 else if (numCols
< 0)
695 // If rows deleted ...
696 if ((size_t)col
>= pos
- numCols
)
698 // ...either decrement row counter (if row still exists)...
699 coords
.SetCol(col
+ numCols
);
703 // ...or remove the attribute
704 m_attrs
.RemoveAt((size_t)n
);
712 int wxGridCellAttrData::FindIndex(int row
, int col
) const
714 size_t count
= m_attrs
.GetCount();
715 for ( size_t n
= 0; n
< count
; n
++ )
717 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
718 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
727 // ----------------------------------------------------------------------------
728 // wxGridRowOrColAttrData
729 // ----------------------------------------------------------------------------
731 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
733 size_t count
= m_attrs
.Count();
734 for ( size_t n
= 0; n
< count
; n
++ )
736 m_attrs
[n
]->DecRef();
740 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
742 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
744 int n
= m_rowsOrCols
.Index(rowOrCol
);
745 if ( n
!= wxNOT_FOUND
)
747 attr
= m_attrs
[(size_t)n
];
754 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
756 int n
= m_rowsOrCols
.Index(rowOrCol
);
757 if ( n
== wxNOT_FOUND
)
760 m_rowsOrCols
.Add(rowOrCol
);
767 // change the attribute
768 m_attrs
[(size_t)n
] = attr
;
772 // remove this attribute
773 m_attrs
[(size_t)n
]->DecRef();
774 m_rowsOrCols
.RemoveAt((size_t)n
);
775 m_attrs
.RemoveAt((size_t)n
);
780 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
782 size_t count
= m_attrs
.GetCount();
783 for ( size_t n
= 0; n
< count
; n
++ )
785 int & rowOrCol
= m_rowsOrCols
[n
];
786 if ( (size_t)rowOrCol
>= pos
)
788 if ( numRowsOrCols
> 0 )
790 // If rows inserted, include row counter where necessary
791 rowOrCol
+= numRowsOrCols
;
793 else if ( numRowsOrCols
< 0)
795 // If rows deleted, either decrement row counter (if row still exists)
796 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
797 rowOrCol
+= numRowsOrCols
;
800 m_rowsOrCols
.RemoveAt((size_t)n
);
801 m_attrs
.RemoveAt((size_t)n
);
809 // ----------------------------------------------------------------------------
810 // wxGridCellAttrProvider
811 // ----------------------------------------------------------------------------
813 wxGridCellAttrProvider::wxGridCellAttrProvider()
815 m_data
= (wxGridCellAttrProviderData
*)NULL
;
818 wxGridCellAttrProvider::~wxGridCellAttrProvider()
823 void wxGridCellAttrProvider::InitData()
825 m_data
= new wxGridCellAttrProviderData
;
828 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
830 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
833 // first look for the attribute of this specific cell
834 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
838 // then look for the col attr (col attributes are more common than
839 // the row ones, hence they have priority)
840 attr
= m_data
->m_colAttrs
.GetAttr(col
);
845 // finally try the row attributes
846 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
853 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
859 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
862 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
867 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
870 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
875 m_data
->m_colAttrs
.SetAttr(attr
, col
);
878 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
882 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
884 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
888 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
892 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
894 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
898 // ----------------------------------------------------------------------------
900 // ----------------------------------------------------------------------------
902 //////////////////////////////////////////////////////////////////////
904 // Abstract base class for grid data (the model)
906 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
909 wxGridTableBase::wxGridTableBase()
911 m_view
= (wxGrid
*) NULL
;
912 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
915 wxGridTableBase::~wxGridTableBase()
917 delete m_attrProvider
;
920 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
922 delete m_attrProvider
;
923 m_attrProvider
= attrProvider
;
926 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
928 if ( m_attrProvider
)
929 return m_attrProvider
->GetAttr(row
, col
);
931 return (wxGridCellAttr
*)NULL
;
934 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
936 if ( m_attrProvider
)
938 m_attrProvider
->SetAttr(attr
, row
, col
);
942 // as we take ownership of the pointer and don't store it, we must
948 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
950 if ( m_attrProvider
)
952 m_attrProvider
->SetRowAttr(attr
, row
);
956 // as we take ownership of the pointer and don't store it, we must
962 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
964 if ( m_attrProvider
)
966 m_attrProvider
->SetColAttr(attr
, col
);
970 // as we take ownership of the pointer and don't store it, we must
976 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
978 if ( m_attrProvider
)
980 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
984 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
986 if ( m_attrProvider
)
988 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
992 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
994 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
995 "but your derived table class does not override this function") );
1000 bool wxGridTableBase::AppendRows( size_t numRows
)
1002 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1003 "but your derived table class does not override this function"));
1008 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1010 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1011 "but your derived table class does not override this function"));
1016 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1018 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1019 "but your derived table class does not override this function"));
1024 bool wxGridTableBase::AppendCols( size_t numCols
)
1026 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1027 "but your derived table class does not override this function"));
1032 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1034 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1035 "but your derived table class does not override this function"));
1041 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1048 wxString
wxGridTableBase::GetColLabelValue( int col
)
1050 // default col labels are:
1051 // cols 0 to 25 : A-Z
1052 // cols 26 to 675 : AA-ZZ
1057 for ( n
= 1; ; n
++ )
1059 s
+= (_T('A') + (wxChar
)( col%26
));
1061 if ( col
< 0 ) break;
1064 // reverse the string...
1066 for ( i
= 0; i
< n
; i
++ )
1076 //////////////////////////////////////////////////////////////////////
1078 // Message class for the grid table to send requests and notifications
1082 wxGridTableMessage::wxGridTableMessage()
1084 m_table
= (wxGridTableBase
*) NULL
;
1090 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1091 int commandInt1
, int commandInt2
)
1095 m_comInt1
= commandInt1
;
1096 m_comInt2
= commandInt2
;
1101 //////////////////////////////////////////////////////////////////////
1103 // A basic grid table for string data. An object of this class will
1104 // created by wxGrid if you don't specify an alternative table class.
1107 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1109 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1111 wxGridStringTable::wxGridStringTable()
1116 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1121 m_data
.Alloc( numRows
);
1124 sa
.Alloc( numCols
);
1125 for ( col
= 0; col
< numCols
; col
++ )
1127 sa
.Add( wxEmptyString
);
1130 for ( row
= 0; row
< numRows
; row
++ )
1136 wxGridStringTable::~wxGridStringTable()
1140 long wxGridStringTable::GetNumberRows()
1142 return m_data
.GetCount();
1145 long wxGridStringTable::GetNumberCols()
1147 if ( m_data
.GetCount() > 0 )
1148 return m_data
[0].GetCount();
1153 wxString
wxGridStringTable::GetValue( int row
, int col
)
1155 // TODO: bounds checking
1157 return m_data
[row
][col
];
1160 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1162 // TODO: bounds checking
1164 m_data
[row
][col
] = s
;
1167 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1169 // TODO: bounds checking
1171 return (m_data
[row
][col
] == wxEmptyString
);
1175 void wxGridStringTable::Clear()
1178 int numRows
, numCols
;
1180 numRows
= m_data
.GetCount();
1183 numCols
= m_data
[0].GetCount();
1185 for ( row
= 0; row
< numRows
; row
++ )
1187 for ( col
= 0; col
< numCols
; col
++ )
1189 m_data
[row
][col
] = wxEmptyString
;
1196 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1200 size_t curNumRows
= m_data
.GetCount();
1201 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1203 if ( pos
>= curNumRows
)
1205 return AppendRows( numRows
);
1209 sa
.Alloc( curNumCols
);
1210 for ( col
= 0; col
< curNumCols
; col
++ )
1212 sa
.Add( wxEmptyString
);
1215 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1217 m_data
.Insert( sa
, row
);
1219 UpdateAttrRows( pos
, numRows
);
1222 wxGridTableMessage
msg( this,
1223 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1227 GetView()->ProcessTableMessage( msg
);
1233 bool wxGridStringTable::AppendRows( size_t numRows
)
1237 size_t curNumRows
= m_data
.GetCount();
1238 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1241 if ( curNumCols
> 0 )
1243 sa
.Alloc( curNumCols
);
1244 for ( col
= 0; col
< curNumCols
; col
++ )
1246 sa
.Add( wxEmptyString
);
1250 for ( row
= 0; row
< numRows
; row
++ )
1257 wxGridTableMessage
msg( this,
1258 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1261 GetView()->ProcessTableMessage( msg
);
1267 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1271 size_t curNumRows
= m_data
.GetCount();
1273 if ( pos
>= curNumRows
)
1276 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1277 "Pos value is invalid for present table with %d rows",
1278 pos
, numRows
, curNumRows
);
1279 wxFAIL_MSG( wxT(errmsg
) );
1283 if ( numRows
> curNumRows
- pos
)
1285 numRows
= curNumRows
- pos
;
1288 if ( numRows
>= curNumRows
)
1290 m_data
.Empty(); // don't release memory just yet
1294 for ( n
= 0; n
< numRows
; n
++ )
1296 m_data
.Remove( pos
);
1299 UpdateAttrRows( pos
, -numRows
);
1302 wxGridTableMessage
msg( this,
1303 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1307 GetView()->ProcessTableMessage( msg
);
1313 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1317 size_t curNumRows
= m_data
.GetCount();
1318 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1320 if ( pos
>= curNumCols
)
1322 return AppendCols( numCols
);
1325 for ( row
= 0; row
< curNumRows
; row
++ )
1327 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1329 m_data
[row
].Insert( wxEmptyString
, col
);
1332 UpdateAttrCols( pos
, numCols
);
1335 wxGridTableMessage
msg( this,
1336 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1340 GetView()->ProcessTableMessage( msg
);
1346 bool wxGridStringTable::AppendCols( size_t numCols
)
1350 size_t curNumRows
= m_data
.GetCount();
1353 // TODO: something better than this ?
1355 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1356 "Call AppendRows() first") );
1360 for ( row
= 0; row
< curNumRows
; row
++ )
1362 for ( n
= 0; n
< numCols
; n
++ )
1364 m_data
[row
].Add( wxEmptyString
);
1370 wxGridTableMessage
msg( this,
1371 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1374 GetView()->ProcessTableMessage( msg
);
1380 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1384 size_t curNumRows
= m_data
.GetCount();
1385 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1387 if ( pos
>= curNumCols
)
1390 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1391 "Pos value is invalid for present table with %d cols",
1392 pos
, numCols
, curNumCols
);
1393 wxFAIL_MSG( wxT( errmsg
) );
1397 if ( numCols
> curNumCols
- pos
)
1399 numCols
= curNumCols
- pos
;
1402 for ( row
= 0; row
< curNumRows
; row
++ )
1404 if ( numCols
>= curNumCols
)
1406 m_data
[row
].Clear();
1410 for ( n
= 0; n
< numCols
; n
++ )
1412 m_data
[row
].Remove( pos
);
1416 UpdateAttrCols( pos
, -numCols
);
1419 wxGridTableMessage
msg( this,
1420 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1424 GetView()->ProcessTableMessage( msg
);
1430 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1432 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1434 // using default label
1436 return wxGridTableBase::GetRowLabelValue( row
);
1440 return m_rowLabels
[ row
];
1444 wxString
wxGridStringTable::GetColLabelValue( int col
)
1446 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1448 // using default label
1450 return wxGridTableBase::GetColLabelValue( col
);
1454 return m_colLabels
[ col
];
1458 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1460 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1462 int n
= m_rowLabels
.GetCount();
1464 for ( i
= n
; i
<= row
; i
++ )
1466 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1470 m_rowLabels
[row
] = value
;
1473 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1475 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1477 int n
= m_colLabels
.GetCount();
1479 for ( i
= n
; i
<= col
; i
++ )
1481 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1485 m_colLabels
[col
] = value
;
1490 //////////////////////////////////////////////////////////////////////
1491 //////////////////////////////////////////////////////////////////////
1493 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1495 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1496 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1497 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1498 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1501 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1503 const wxPoint
&pos
, const wxSize
&size
)
1504 : wxWindow( parent
, id
, pos
, size
)
1509 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1513 // NO - don't do this because it will set both the x and y origin
1514 // coords to match the parent scrolled window and we just want to
1515 // set the y coord - MB
1517 // m_owner->PrepareDC( dc );
1520 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1521 dc
.SetDeviceOrigin( 0, -y
);
1523 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1524 m_owner
->DrawRowLabels( dc
);
1528 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1530 m_owner
->ProcessRowLabelMouseEvent( event
);
1534 // This seems to be required for wxMotif otherwise the mouse
1535 // cursor must be in the cell edit control to get key events
1537 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1539 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1544 //////////////////////////////////////////////////////////////////////
1546 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1548 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1549 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1550 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1551 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1554 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1556 const wxPoint
&pos
, const wxSize
&size
)
1557 : wxWindow( parent
, id
, pos
, size
)
1562 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1566 // NO - don't do this because it will set both the x and y origin
1567 // coords to match the parent scrolled window and we just want to
1568 // set the x coord - MB
1570 // m_owner->PrepareDC( dc );
1573 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1574 dc
.SetDeviceOrigin( -x
, 0 );
1576 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1577 m_owner
->DrawColLabels( dc
);
1581 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1583 m_owner
->ProcessColLabelMouseEvent( event
);
1587 // This seems to be required for wxMotif otherwise the mouse
1588 // cursor must be in the cell edit control to get key events
1590 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1592 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1597 //////////////////////////////////////////////////////////////////////
1599 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1601 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1602 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1603 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1604 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1607 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1609 const wxPoint
&pos
, const wxSize
&size
)
1610 : wxWindow( parent
, id
, pos
, size
)
1615 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1619 int client_height
= 0;
1620 int client_width
= 0;
1621 GetClientSize( &client_width
, &client_height
);
1623 dc
.SetPen( *wxBLACK_PEN
);
1624 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1625 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1627 dc
.SetPen( *wxWHITE_PEN
);
1628 dc
.DrawLine( 0, 0, client_width
, 0 );
1629 dc
.DrawLine( 0, 0, 0, client_height
);
1633 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1635 m_owner
->ProcessCornerLabelMouseEvent( event
);
1639 // This seems to be required for wxMotif otherwise the mouse
1640 // cursor must be in the cell edit control to get key events
1642 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1644 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1649 //////////////////////////////////////////////////////////////////////
1651 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1653 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1654 EVT_PAINT( wxGridWindow::OnPaint
)
1655 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1656 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1657 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1660 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1661 wxGridRowLabelWindow
*rowLblWin
,
1662 wxGridColLabelWindow
*colLblWin
,
1663 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1664 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1667 m_rowLabelWin
= rowLblWin
;
1668 m_colLabelWin
= colLblWin
;
1669 SetBackgroundColour( "WHITE" );
1673 wxGridWindow::~wxGridWindow()
1678 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1680 wxPaintDC
dc( this );
1681 m_owner
->PrepareDC( dc
);
1682 wxRegion reg
= GetUpdateRegion();
1683 m_owner
->CalcCellsExposed( reg
);
1684 m_owner
->DrawGridCellArea( dc
);
1685 #if WXGRID_DRAW_LINES
1686 m_owner
->DrawAllGridLines( dc
, reg
);
1691 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1693 wxPanel::ScrollWindow( dx
, dy
, rect
);
1694 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1695 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1699 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1701 m_owner
->ProcessGridCellMouseEvent( event
);
1705 // This seems to be required for wxMotif otherwise the mouse
1706 // cursor must be in the cell edit control to get key events
1708 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1710 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1713 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1719 //////////////////////////////////////////////////////////////////////
1722 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1724 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1725 EVT_PAINT( wxGrid::OnPaint
)
1726 EVT_SIZE( wxGrid::OnSize
)
1727 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1728 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1731 wxGrid::wxGrid( wxWindow
*parent
,
1736 const wxString
& name
)
1737 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1746 m_defaultCellAttr
->SafeDecRef();
1748 #ifdef DEBUG_ATTR_CACHE
1749 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1750 wxPrintf(_T("wxGrid attribute cache statistics: "
1751 "total: %u, hits: %u (%u%%)\n"),
1752 total
, gs_nAttrCacheHits
,
1753 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1762 // ----- internal init and update functions
1765 void wxGrid::Create()
1767 m_created
= FALSE
; // set to TRUE by CreateGrid
1768 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
1770 m_table
= (wxGridTableBase
*) NULL
;
1773 m_cellEditCtrlEnabled
= FALSE
;
1775 m_defaultCellAttr
= new wxGridCellAttr
;
1776 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1777 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1782 m_currentCellCoords
= wxGridNoCellCoords
;
1784 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1785 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1787 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1792 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1797 m_colLabelWin
= new wxGridColLabelWindow( this,
1802 m_gridWin
= new wxGridWindow( this,
1809 SetTargetWindow( m_gridWin
);
1813 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1817 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1822 m_numRows
= numRows
;
1823 m_numCols
= numCols
;
1825 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1826 m_table
->SetView( this );
1835 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1839 // RD: Actually, this should probably be allowed. I think it would be
1840 // nice to be able to switch multiple Tables in and out of a single
1841 // View at runtime. Is there anything in the implmentation that would
1844 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1849 m_numRows
= table
->GetNumberRows();
1850 m_numCols
= table
->GetNumberCols();
1853 m_table
->SetView( this );
1868 if ( m_numRows
<= 0 )
1869 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1871 if ( m_numCols
<= 0 )
1872 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1874 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1875 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1877 if ( m_rowLabelWin
)
1879 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1883 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1886 m_labelTextColour
= wxColour( _T("BLACK") );
1889 m_attrCache
.row
= -1;
1891 // TODO: something better than this ?
1893 m_labelFont
= this->GetFont();
1894 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1896 m_rowLabelHorizAlign
= wxLEFT
;
1897 m_rowLabelVertAlign
= wxCENTRE
;
1899 m_colLabelHorizAlign
= wxCENTRE
;
1900 m_colLabelVertAlign
= wxTOP
;
1902 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1903 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1905 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1906 m_defaultRowHeight
+= 8;
1908 m_defaultRowHeight
+= 4;
1911 m_rowHeights
.Alloc( m_numRows
);
1912 m_rowBottoms
.Alloc( m_numRows
);
1914 for ( i
= 0; i
< m_numRows
; i
++ )
1916 m_rowHeights
.Add( m_defaultRowHeight
);
1917 rowBottom
+= m_defaultRowHeight
;
1918 m_rowBottoms
.Add( rowBottom
);
1921 m_colWidths
.Alloc( m_numCols
);
1922 m_colRights
.Alloc( m_numCols
);
1924 for ( i
= 0; i
< m_numCols
; i
++ )
1926 m_colWidths
.Add( m_defaultColWidth
);
1927 colRight
+= m_defaultColWidth
;
1928 m_colRights
.Add( colRight
);
1931 // Set default cell attributes
1932 m_defaultCellAttr
->SetFont(GetFont());
1933 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1934 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1935 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1936 m_defaultCellAttr
->SetTextColour(
1937 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1938 m_defaultCellAttr
->SetBackgroundColour(
1939 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1942 m_gridLineColour
= wxColour( 128, 128, 255 );
1943 m_gridLinesEnabled
= TRUE
;
1945 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1946 m_winCapture
= (wxWindow
*)NULL
;
1948 m_dragRowOrCol
= -1;
1949 m_isDragging
= FALSE
;
1950 m_startDragPos
= wxDefaultPosition
;
1952 m_waitForSlowClick
= FALSE
;
1954 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1955 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1957 m_currentCellCoords
= wxGridNoCellCoords
;
1959 m_selectedTopLeft
= wxGridNoCellCoords
;
1960 m_selectedBottomRight
= wxGridNoCellCoords
;
1961 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
1962 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1964 m_editable
= TRUE
; // default for whole grid
1966 m_inOnKeyDown
= FALSE
;
1972 void wxGrid::CalcDimensions()
1975 GetClientSize( &cw
, &ch
);
1977 if ( m_numRows
> 0 && m_numCols
> 0 )
1979 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1980 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1982 // TODO: restore the scroll position that we had before sizing
1985 GetViewStart( &x
, &y
);
1986 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1987 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1993 void wxGrid::CalcWindowSizes()
1996 GetClientSize( &cw
, &ch
);
1998 if ( m_cornerLabelWin
->IsShown() )
1999 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2001 if ( m_colLabelWin
->IsShown() )
2002 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2004 if ( m_rowLabelWin
->IsShown() )
2005 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2007 if ( m_gridWin
->IsShown() )
2008 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2012 // this is called when the grid table sends a message to say that it
2013 // has been redimensioned
2015 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2019 switch ( msg
.GetId() )
2021 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2023 size_t pos
= msg
.GetCommandInt();
2024 int numRows
= msg
.GetCommandInt2();
2025 for ( i
= 0; i
< numRows
; i
++ )
2027 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2028 m_rowBottoms
.Insert( 0, pos
);
2030 m_numRows
+= numRows
;
2033 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2035 for ( i
= pos
; i
< m_numRows
; i
++ )
2037 bottom
+= m_rowHeights
[i
];
2038 m_rowBottoms
[i
] = bottom
;
2044 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2046 int numRows
= msg
.GetCommandInt();
2047 for ( i
= 0; i
< numRows
; i
++ )
2049 m_rowHeights
.Add( m_defaultRowHeight
);
2050 m_rowBottoms
.Add( 0 );
2053 int oldNumRows
= m_numRows
;
2054 m_numRows
+= numRows
;
2057 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2059 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2061 bottom
+= m_rowHeights
[i
];
2062 m_rowBottoms
[i
] = bottom
;
2068 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2070 size_t pos
= msg
.GetCommandInt();
2071 int numRows
= msg
.GetCommandInt2();
2072 for ( i
= 0; i
< numRows
; i
++ )
2074 m_rowHeights
.Remove( pos
);
2075 m_rowBottoms
.Remove( pos
);
2077 m_numRows
-= numRows
;
2082 m_colWidths
.Clear();
2083 m_colRights
.Clear();
2084 m_currentCellCoords
= wxGridNoCellCoords
;
2088 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2089 m_currentCellCoords
.Set( 0, 0 );
2092 for ( i
= 0; i
< m_numRows
; i
++ )
2094 h
+= m_rowHeights
[i
];
2095 m_rowBottoms
[i
] = h
;
2103 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2105 size_t pos
= msg
.GetCommandInt();
2106 int numCols
= msg
.GetCommandInt2();
2107 for ( i
= 0; i
< numCols
; i
++ )
2109 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2110 m_colRights
.Insert( 0, pos
);
2112 m_numCols
+= numCols
;
2115 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2117 for ( i
= pos
; i
< m_numCols
; i
++ )
2119 right
+= m_colWidths
[i
];
2120 m_colRights
[i
] = right
;
2126 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2128 int numCols
= msg
.GetCommandInt();
2129 for ( i
= 0; i
< numCols
; i
++ )
2131 m_colWidths
.Add( m_defaultColWidth
);
2132 m_colRights
.Add( 0 );
2135 int oldNumCols
= m_numCols
;
2136 m_numCols
+= numCols
;
2139 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2141 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2143 right
+= m_colWidths
[i
];
2144 m_colRights
[i
] = right
;
2150 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2152 size_t pos
= msg
.GetCommandInt();
2153 int numCols
= msg
.GetCommandInt2();
2154 for ( i
= 0; i
< numCols
; i
++ )
2156 m_colWidths
.Remove( pos
);
2157 m_colRights
.Remove( pos
);
2159 m_numCols
-= numCols
;
2163 #if 0 // leave the row alone here so that AppendCols will work subsequently
2165 m_rowHeights
.Clear();
2166 m_rowBottoms
.Clear();
2168 m_currentCellCoords
= wxGridNoCellCoords
;
2172 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2173 m_currentCellCoords
.Set( 0, 0 );
2176 for ( i
= 0; i
< m_numCols
; i
++ )
2178 w
+= m_colWidths
[i
];
2191 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2193 wxRegionIterator
iter( reg
);
2196 m_rowLabelsExposed
.Empty();
2203 // TODO: remove this when we can...
2204 // There is a bug in wxMotif that gives garbage update
2205 // rectangles if you jump-scroll a long way by clicking the
2206 // scrollbar with middle button. This is a work-around
2208 #if defined(__WXMOTIF__)
2210 m_gridWin
->GetClientSize( &cw
, &ch
);
2211 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2212 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2215 // logical bounds of update region
2218 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2219 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2221 // find the row labels within these bounds
2225 for ( row
= 0; row
< m_numRows
; row
++ )
2227 if ( m_rowBottoms
[row
] < top
) continue;
2229 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2230 if ( rowTop
> bottom
) break;
2232 m_rowLabelsExposed
.Add( row
);
2240 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2242 wxRegionIterator
iter( reg
);
2245 m_colLabelsExposed
.Empty();
2252 // TODO: remove this when we can...
2253 // There is a bug in wxMotif that gives garbage update
2254 // rectangles if you jump-scroll a long way by clicking the
2255 // scrollbar with middle button. This is a work-around
2257 #if defined(__WXMOTIF__)
2259 m_gridWin
->GetClientSize( &cw
, &ch
);
2260 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2261 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2264 // logical bounds of update region
2267 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2268 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2270 // find the cells within these bounds
2274 for ( col
= 0; col
< m_numCols
; col
++ )
2276 if ( m_colRights
[col
] < left
) continue;
2278 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2279 if ( colLeft
> right
) break;
2281 m_colLabelsExposed
.Add( col
);
2289 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2291 wxRegionIterator
iter( reg
);
2294 m_cellsExposed
.Empty();
2295 m_rowsExposed
.Empty();
2296 m_colsExposed
.Empty();
2298 int left
, top
, right
, bottom
;
2303 // TODO: remove this when we can...
2304 // There is a bug in wxMotif that gives garbage update
2305 // rectangles if you jump-scroll a long way by clicking the
2306 // scrollbar with middle button. This is a work-around
2308 #if defined(__WXMOTIF__)
2310 m_gridWin
->GetClientSize( &cw
, &ch
);
2311 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2312 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2313 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2314 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2317 // logical bounds of update region
2319 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2320 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2322 // find the cells within these bounds
2325 int colLeft
, rowTop
;
2326 for ( row
= 0; row
< m_numRows
; row
++ )
2328 if ( m_rowBottoms
[row
] <= top
) continue;
2330 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2331 if ( rowTop
> bottom
) break;
2333 m_rowsExposed
.Add( row
);
2335 for ( col
= 0; col
< m_numCols
; col
++ )
2337 if ( m_colRights
[col
] <= left
) continue;
2339 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2340 if ( colLeft
> right
) break;
2342 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2343 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2352 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2355 wxPoint
pos( event
.GetPosition() );
2356 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2358 if ( event
.Dragging() )
2360 m_isDragging
= TRUE
;
2362 if ( event
.LeftIsDown() )
2364 switch( m_cursorMode
)
2366 case WXGRID_CURSOR_RESIZE_ROW
:
2368 int cw
, ch
, left
, dummy
;
2369 m_gridWin
->GetClientSize( &cw
, &ch
);
2370 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2372 wxClientDC
dc( m_gridWin
);
2375 m_rowBottoms
[m_dragRowOrCol
] -
2376 m_rowHeights
[m_dragRowOrCol
] +
2377 WXGRID_MIN_ROW_HEIGHT
);
2378 dc
.SetLogicalFunction(wxINVERT
);
2379 if ( m_dragLastPos
>= 0 )
2381 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2383 dc
.DrawLine( left
, y
, left
+cw
, y
);
2388 case WXGRID_CURSOR_SELECT_ROW
:
2389 if ( (row
= YToRow( y
)) >= 0 &&
2390 !IsInSelection( row
, 0 ) )
2392 SelectRow( row
, TRUE
);
2395 // default label to suppress warnings about "enumeration value
2396 // 'xxx' not handled in switch
2404 m_isDragging
= FALSE
;
2407 // ------------ Entering or leaving the window
2409 if ( event
.Entering() || event
.Leaving() )
2411 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2415 // ------------ Left button pressed
2417 else if ( event
.LeftDown() )
2419 // don't send a label click event for a hit on the
2420 // edge of the row label - this is probably the user
2421 // wanting to resize the row
2423 if ( YToEdgeOfRow(y
) < 0 )
2427 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2429 SelectRow( row
, event
.ShiftDown() );
2430 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2435 // starting to drag-resize a row
2437 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2442 // ------------ Left double click
2444 else if (event
.LeftDClick() )
2446 if ( YToEdgeOfRow(y
) < 0 )
2449 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2454 // ------------ Left button released
2456 else if ( event
.LeftUp() )
2458 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2460 DoEndDragResizeRow();
2462 // Note: we are ending the event *after* doing
2463 // default processing in this case
2465 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2468 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2473 // ------------ Right button down
2475 else if ( event
.RightDown() )
2478 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2480 // no default action at the moment
2485 // ------------ Right double click
2487 else if ( event
.RightDClick() )
2490 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2492 // no default action at the moment
2497 // ------------ No buttons down and mouse moving
2499 else if ( event
.Moving() )
2501 m_dragRowOrCol
= YToEdgeOfRow( y
);
2502 if ( m_dragRowOrCol
>= 0 )
2504 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2506 // don't capture the mouse yet
2507 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2510 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2512 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2518 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2521 wxPoint
pos( event
.GetPosition() );
2522 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2524 if ( event
.Dragging() )
2526 m_isDragging
= TRUE
;
2528 if ( event
.LeftIsDown() )
2530 switch( m_cursorMode
)
2532 case WXGRID_CURSOR_RESIZE_COL
:
2534 int cw
, ch
, dummy
, top
;
2535 m_gridWin
->GetClientSize( &cw
, &ch
);
2536 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2538 wxClientDC
dc( m_gridWin
);
2541 m_colRights
[m_dragRowOrCol
] -
2542 m_colWidths
[m_dragRowOrCol
] +
2543 WXGRID_MIN_COL_WIDTH
);
2544 dc
.SetLogicalFunction(wxINVERT
);
2545 if ( m_dragLastPos
>= 0 )
2547 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2549 dc
.DrawLine( x
, top
, x
, top
+ch
);
2554 case WXGRID_CURSOR_SELECT_COL
:
2555 if ( (col
= XToCol( x
)) >= 0 &&
2556 !IsInSelection( 0, col
) )
2558 SelectCol( col
, TRUE
);
2561 // default label to suppress warnings about "enumeration value
2562 // 'xxx' not handled in switch
2570 m_isDragging
= FALSE
;
2573 // ------------ Entering or leaving the window
2575 if ( event
.Entering() || event
.Leaving() )
2577 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2581 // ------------ Left button pressed
2583 else if ( event
.LeftDown() )
2585 // don't send a label click event for a hit on the
2586 // edge of the col label - this is probably the user
2587 // wanting to resize the col
2589 if ( XToEdgeOfCol(x
) < 0 )
2593 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2595 SelectCol( col
, event
.ShiftDown() );
2596 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2601 // starting to drag-resize a col
2603 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2608 // ------------ Left double click
2610 if ( event
.LeftDClick() )
2612 if ( XToEdgeOfCol(x
) < 0 )
2615 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2620 // ------------ Left button released
2622 else if ( event
.LeftUp() )
2624 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2626 DoEndDragResizeCol();
2628 // Note: we are ending the event *after* doing
2629 // default processing in this case
2631 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2634 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2639 // ------------ Right button down
2641 else if ( event
.RightDown() )
2644 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2646 // no default action at the moment
2651 // ------------ Right double click
2653 else if ( event
.RightDClick() )
2656 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2658 // no default action at the moment
2663 // ------------ No buttons down and mouse moving
2665 else if ( event
.Moving() )
2667 m_dragRowOrCol
= XToEdgeOfCol( x
);
2668 if ( m_dragRowOrCol
>= 0 )
2670 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2672 // don't capture the cursor yet
2673 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2676 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2678 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2684 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2686 if ( event
.LeftDown() )
2688 // indicate corner label by having both row and
2691 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2697 else if ( event
.LeftDClick() )
2699 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2702 else if ( event
.RightDown() )
2704 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2706 // no default action at the moment
2710 else if ( event
.RightDClick() )
2712 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2714 // no default action at the moment
2719 void wxGrid::ChangeCursorMode(CursorMode mode
,
2724 static const wxChar
*cursorModes
[] =
2733 wxLogTrace(_T("grid"),
2734 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2735 win
== m_colLabelWin
? _T("colLabelWin")
2736 : win
? _T("rowLabelWin")
2738 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2739 #endif // __WXDEBUG__
2741 if ( mode
== m_cursorMode
)
2746 // by default use the grid itself
2752 m_winCapture
->ReleaseMouse();
2753 m_winCapture
= (wxWindow
*)NULL
;
2756 m_cursorMode
= mode
;
2758 switch ( m_cursorMode
)
2760 case WXGRID_CURSOR_RESIZE_ROW
:
2761 win
->SetCursor( m_rowResizeCursor
);
2764 case WXGRID_CURSOR_RESIZE_COL
:
2765 win
->SetCursor( m_colResizeCursor
);
2769 win
->SetCursor( *wxSTANDARD_CURSOR
);
2772 // we need to capture mouse when resizing
2773 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2774 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2776 if ( captureMouse
&& resize
)
2778 win
->CaptureMouse();
2783 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2786 wxPoint
pos( event
.GetPosition() );
2787 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2789 wxGridCellCoords coords
;
2790 XYToCell( x
, y
, coords
);
2792 if ( event
.Dragging() )
2794 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
2796 // Don't start doing anything until the mouse has been drug at
2797 // least 3 pixels in any direction...
2798 if (! m_isDragging
) {
2799 if (m_startDragPos
== wxDefaultPosition
) {
2800 m_startDragPos
= pos
;
2803 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
2807 m_isDragging
= TRUE
;
2808 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2810 // Hide the edit control, so it
2811 // won't interfer with drag-shrinking.
2812 if ( IsCellEditControlEnabled() )
2813 HideCellEditControl();
2815 // Have we captured the mouse yet?
2816 if (! m_winCapture
) {
2817 m_winCapture
= m_gridWin
;
2818 m_winCapture
->CaptureMouse();
2821 if ( coords
!= wxGridNoCellCoords
)
2823 if ( !IsSelection() )
2825 SelectBlock( coords
, coords
);
2829 SelectBlock( m_currentCellCoords
, coords
);
2832 if (! IsVisible(coords
)) {
2833 MakeCellVisible(coords
);
2834 // TODO: need to introduce a delay or something here. The
2835 // scrolling is way to fast, at least on MSW.
2839 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2841 int cw
, ch
, left
, dummy
;
2842 m_gridWin
->GetClientSize( &cw
, &ch
);
2843 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2845 wxClientDC
dc( m_gridWin
);
2848 m_rowBottoms
[m_dragRowOrCol
] -
2849 m_rowHeights
[m_dragRowOrCol
] +
2850 WXGRID_MIN_ROW_HEIGHT
);
2851 dc
.SetLogicalFunction(wxINVERT
);
2852 if ( m_dragLastPos
>= 0 )
2854 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2856 dc
.DrawLine( left
, y
, left
+cw
, y
);
2859 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2861 int cw
, ch
, dummy
, top
;
2862 m_gridWin
->GetClientSize( &cw
, &ch
);
2863 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2865 wxClientDC
dc( m_gridWin
);
2868 m_colRights
[m_dragRowOrCol
] -
2869 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
2870 dc
.SetLogicalFunction(wxINVERT
);
2871 if ( m_dragLastPos
>= 0 )
2873 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2875 dc
.DrawLine( x
, top
, x
, top
+ch
);
2882 m_isDragging
= FALSE
;
2883 m_startDragPos
= wxDefaultPosition
;
2886 if ( coords
!= wxGridNoCellCoords
)
2888 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2889 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2892 if ( event
.Entering() || event
.Leaving() )
2894 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2895 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2900 // ------------ Left button pressed
2902 if ( event
.LeftDown() )
2904 EnableCellEditControl( FALSE
);
2905 if ( event
.ShiftDown() )
2907 SelectBlock( m_currentCellCoords
, coords
);
2909 else if ( XToEdgeOfCol(x
) < 0 &&
2910 YToEdgeOfRow(y
) < 0 )
2912 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2917 MakeCellVisible( coords
);
2919 // if this is the second click on this cell then start
2921 if (m_waitForSlowClick
&& coords
== m_currentCellCoords
) {
2922 EnableCellEditControl(TRUE
);
2923 ShowCellEditControl();
2924 m_waitForSlowClick
= FALSE
;
2927 SetCurrentCell( coords
);
2928 m_waitForSlowClick
= TRUE
;
2935 // ------------ Left double click
2937 else if ( event
.LeftDClick() )
2939 EnableCellEditControl( FALSE
);
2940 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2942 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2950 // ------------ Left button released
2952 else if ( event
.LeftUp() )
2954 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2956 if ( IsSelection() )
2959 m_winCapture
->ReleaseMouse();
2960 m_winCapture
= NULL
;
2962 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2965 // Show the edit control, if it has
2966 // been hidden for drag-shrinking.
2967 if ( IsCellEditControlEnabled() )
2968 ShowCellEditControl();
2970 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2972 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2973 DoEndDragResizeRow();
2975 // Note: we are ending the event *after* doing
2976 // default processing in this case
2978 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2980 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2982 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2983 DoEndDragResizeCol();
2985 // Note: we are ending the event *after* doing
2986 // default processing in this case
2988 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2995 // ------------ Right button down
2997 else if ( event
.RightDown() )
2999 EnableCellEditControl( FALSE
);
3000 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
3005 // no default action at the moment
3010 // ------------ Right double click
3012 else if ( event
.RightDClick() )
3014 EnableCellEditControl( FALSE
);
3015 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
3020 // no default action at the moment
3024 // ------------ Moving and no button action
3026 else if ( event
.Moving() && !event
.IsButton() )
3028 int dragRow
= YToEdgeOfRow( y
);
3029 int dragCol
= XToEdgeOfCol( x
);
3031 // Dragging on the corner of a cell to resize in both
3032 // directions is not implemented yet...
3034 if ( dragRow
>= 0 && dragCol
>= 0 )
3036 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3042 m_dragRowOrCol
= dragRow
;
3044 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3046 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3054 m_dragRowOrCol
= dragCol
;
3056 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3058 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3064 // Neither on a row or col edge
3066 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3068 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3075 void wxGrid::DoEndDragResizeRow()
3077 if ( m_dragLastPos
>= 0 )
3079 // erase the last line and resize the row
3081 int cw
, ch
, left
, dummy
;
3082 m_gridWin
->GetClientSize( &cw
, &ch
);
3083 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3085 wxClientDC
dc( m_gridWin
);
3087 dc
.SetLogicalFunction( wxINVERT
);
3088 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3089 HideCellEditControl();
3091 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3092 SetRowSize( m_dragRowOrCol
,
3093 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3095 if ( !GetBatchCount() )
3097 // Only needed to get the correct rect.y:
3098 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3100 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3101 rect
.width
= m_rowLabelWidth
;
3102 rect
.height
= ch
- rect
.y
;
3103 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3105 m_gridWin
->Refresh( FALSE
, &rect
);
3108 ShowCellEditControl();
3113 void wxGrid::DoEndDragResizeCol()
3115 if ( m_dragLastPos
>= 0 )
3117 // erase the last line and resize the col
3119 int cw
, ch
, dummy
, top
;
3120 m_gridWin
->GetClientSize( &cw
, &ch
);
3121 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3123 wxClientDC
dc( m_gridWin
);
3125 dc
.SetLogicalFunction( wxINVERT
);
3126 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3127 HideCellEditControl();
3129 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3130 SetColSize( m_dragRowOrCol
,
3131 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3133 if ( !GetBatchCount() )
3135 // Only needed to get the correct rect.x:
3136 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3138 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3139 rect
.width
= cw
- rect
.x
;
3140 rect
.height
= m_colLabelHeight
;
3141 m_colLabelWin
->Refresh( TRUE
, &rect
);
3143 m_gridWin
->Refresh( FALSE
, &rect
);
3146 ShowCellEditControl();
3153 // ------ interaction with data model
3155 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3157 switch ( msg
.GetId() )
3159 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3160 return GetModelValues();
3162 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3163 return SetModelValues();
3165 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3166 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3167 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3168 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3169 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3170 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3171 return Redimension( msg
);
3180 // The behaviour of this function depends on the grid table class
3181 // Clear() function. For the default wxGridStringTable class the
3182 // behavious is to replace all cell contents with wxEmptyString but
3183 // not to change the number of rows or cols.
3185 void wxGrid::ClearGrid()
3190 SetEditControlValue();
3191 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3196 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3198 // TODO: something with updateLabels flag
3202 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3208 bool ok
= m_table
->InsertRows( pos
, numRows
);
3210 // the table will have sent the results of the insert row
3211 // operation to this view object as a grid table message
3215 if ( m_numCols
== 0 )
3217 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3219 // TODO: perhaps instead of appending the default number of cols
3220 // we should remember what the last non-zero number of cols was ?
3224 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3226 // if we have just inserted cols into an empty grid the current
3227 // cell will be undefined...
3229 SetCurrentCell( 0, 0 );
3233 if ( !GetBatchCount() ) Refresh();
3236 SetEditControlValue();
3246 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3248 // TODO: something with updateLabels flag
3252 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3256 if ( m_table
&& m_table
->AppendRows( numRows
) )
3258 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3260 // if we have just inserted cols into an empty grid the current
3261 // cell will be undefined...
3263 SetCurrentCell( 0, 0 );
3266 // the table will have sent the results of the append row
3267 // operation to this view object as a grid table message
3270 if ( !GetBatchCount() ) Refresh();
3280 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3282 // TODO: something with updateLabels flag
3286 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3290 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3292 // the table will have sent the results of the delete row
3293 // operation to this view object as a grid table message
3295 if ( m_numRows
> 0 )
3296 SetEditControlValue();
3298 HideCellEditControl();
3301 if ( !GetBatchCount() ) Refresh();
3311 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3313 // TODO: something with updateLabels flag
3317 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3323 HideCellEditControl();
3324 bool ok
= m_table
->InsertCols( pos
, numCols
);
3326 // the table will have sent the results of the insert col
3327 // operation to this view object as a grid table message
3331 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3333 // if we have just inserted cols into an empty grid the current
3334 // cell will be undefined...
3336 SetCurrentCell( 0, 0 );
3340 if ( !GetBatchCount() ) Refresh();
3343 SetEditControlValue();
3353 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3355 // TODO: something with updateLabels flag
3359 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3363 if ( m_table
&& m_table
->AppendCols( numCols
) )
3365 // the table will have sent the results of the append col
3366 // operation to this view object as a grid table message
3368 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3370 // if we have just inserted cols into an empty grid the current
3371 // cell will be undefined...
3373 SetCurrentCell( 0, 0 );
3377 if ( !GetBatchCount() ) Refresh();
3387 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3389 // TODO: something with updateLabels flag
3393 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3397 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3399 // the table will have sent the results of the delete col
3400 // operation to this view object as a grid table message
3402 if ( m_numCols
> 0 )
3403 SetEditControlValue();
3405 HideCellEditControl();
3408 if ( !GetBatchCount() ) Refresh();
3420 // ----- event handlers
3423 // Generate a grid event based on a mouse event and
3424 // return the result of ProcessEvent()
3426 bool wxGrid::SendEvent( const wxEventType type
,
3428 wxMouseEvent
& mouseEv
)
3430 if ( type
== EVT_GRID_ROW_SIZE
||
3431 type
== EVT_GRID_COL_SIZE
)
3433 int rowOrCol
= (row
== -1 ? col
: row
);
3435 wxGridSizeEvent
gridEvt( GetId(),
3439 mouseEv
.GetX(), mouseEv
.GetY(),
3440 mouseEv
.ControlDown(),
3441 mouseEv
.ShiftDown(),
3443 mouseEv
.MetaDown() );
3445 return GetEventHandler()->ProcessEvent(gridEvt
);
3447 else if ( type
== EVT_GRID_RANGE_SELECT
)
3449 wxGridRangeSelectEvent
gridEvt( GetId(),
3453 m_selectedBottomRight
,
3454 mouseEv
.ControlDown(),
3455 mouseEv
.ShiftDown(),
3457 mouseEv
.MetaDown() );
3459 return GetEventHandler()->ProcessEvent(gridEvt
);
3463 wxGridEvent
gridEvt( GetId(),
3467 mouseEv
.GetX(), mouseEv
.GetY(),
3468 mouseEv
.ControlDown(),
3469 mouseEv
.ShiftDown(),
3471 mouseEv
.MetaDown() );
3473 return GetEventHandler()->ProcessEvent(gridEvt
);
3478 // Generate a grid event of specified type and return the result
3479 // of ProcessEvent().
3481 bool wxGrid::SendEvent( const wxEventType type
,
3484 if ( type
== EVT_GRID_ROW_SIZE
||
3485 type
== EVT_GRID_COL_SIZE
)
3487 int rowOrCol
= (row
== -1 ? col
: row
);
3489 wxGridSizeEvent
gridEvt( GetId(),
3494 return GetEventHandler()->ProcessEvent(gridEvt
);
3498 wxGridEvent
gridEvt( GetId(),
3503 return GetEventHandler()->ProcessEvent(gridEvt
);
3508 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3510 wxPaintDC
dc( this );
3512 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3513 m_numRows
&& m_numCols
)
3515 m_currentCellCoords
.Set(0, 0);
3516 SetEditControlValue();
3517 ShowCellEditControl();
3524 // This is just here to make sure that CalcDimensions gets called when
3525 // the grid view is resized... then the size event is skipped to allow
3526 // the box sizers to handle everything
3528 void wxGrid::OnSize( wxSizeEvent
& event
)
3535 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3537 if ( m_inOnKeyDown
)
3539 // shouldn't be here - we are going round in circles...
3541 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3544 m_inOnKeyDown
= TRUE
;
3546 // propagate the event up and see if it gets processed
3548 wxWindow
*parent
= GetParent();
3549 wxKeyEvent
keyEvt( event
);
3550 keyEvt
.SetEventObject( parent
);
3552 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3554 // try local handlers
3556 switch ( event
.KeyCode() )
3559 if ( event
.ControlDown() )
3561 MoveCursorUpBlock();
3570 if ( event
.ControlDown() )
3572 MoveCursorDownBlock();
3581 if ( event
.ControlDown() )
3583 MoveCursorLeftBlock();
3592 if ( event
.ControlDown() )
3594 MoveCursorRightBlock();
3603 if ( event
.ControlDown() )
3605 event
.Skip(); // to let the edit control have the return
3614 if (event
.ShiftDown())
3621 if ( event
.ControlDown() )
3623 MakeCellVisible( 0, 0 );
3624 SetCurrentCell( 0, 0 );
3633 if ( event
.ControlDown() )
3635 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3636 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3652 // We don't want these keys to trigger the edit control, any others?
3661 if ( !IsEditable() )
3666 // Otherwise fall through to default
3669 // now try the cell edit control
3671 if ( !IsCellEditControlEnabled() )
3672 EnableCellEditControl( TRUE
);
3673 if (IsCellEditControlEnabled()) {
3674 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3675 attr
->GetEditor()->StartingKey(event
);
3682 m_inOnKeyDown
= FALSE
;
3686 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3692 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3694 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3696 // the event has been intercepted - do nothing
3701 m_currentCellCoords
!= wxGridNoCellCoords
)
3703 HideCellEditControl();
3704 SaveEditControlValue();
3705 EnableCellEditControl(FALSE
);
3707 // Clear the old current cell highlight
3708 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3709 m_currentCellCoords
= coords
; // Otherwise refresh redraws the highlight!
3710 m_gridWin
->Refresh( FALSE
, &r
);
3713 m_currentCellCoords
= coords
;
3715 SetEditControlValue();
3719 wxClientDC
dc(m_gridWin
);
3721 DrawCellHighlight(dc
);
3723 if ( IsSelection() )
3725 wxRect
r( SelectionToDeviceRect() );
3727 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3734 // ------ functions to get/send data (see also public functions)
3737 bool wxGrid::GetModelValues()
3741 // all we need to do is repaint the grid
3743 m_gridWin
->Refresh();
3751 bool wxGrid::SetModelValues()
3757 for ( row
= 0; row
< m_numRows
; row
++ )
3759 for ( col
= 0; col
< m_numCols
; col
++ )
3761 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3773 // Note - this function only draws cells that are in the list of
3774 // exposed cells (usually set from the update region by
3775 // CalcExposedCells)
3777 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3779 if ( !m_numRows
|| !m_numCols
) return;
3782 size_t numCells
= m_cellsExposed
.GetCount();
3784 for ( i
= 0; i
< numCells
; i
++ )
3786 DrawCell( dc
, m_cellsExposed
[i
] );
3791 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3793 int row
= coords
.GetRow();
3794 int col
= coords
.GetCol();
3796 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3799 // we draw the cell border ourselves
3800 #if !WXGRID_DRAW_LINES
3801 if ( m_gridLinesEnabled
)
3802 DrawCellBorder( dc
, coords
);
3805 // but all the rest is drawn by the cell renderer and hence may be
3808 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3809 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3810 rect
.width
= m_colWidths
[col
]-1;
3811 rect
.height
= m_rowHeights
[row
]-1;
3813 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3814 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3817 if (m_currentCellCoords
== coords
)
3818 DrawCellHighlight(dc
);
3822 void wxGrid::DrawCellHighlight( wxDC
& dc
)
3824 int row
= m_currentCellCoords
.GetRow();
3825 int col
= m_currentCellCoords
.GetCol();
3827 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3831 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3832 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3833 rect
.width
= m_colWidths
[col
] - 1;
3834 rect
.height
= m_rowHeights
[row
] - 1;
3836 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
3837 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3839 dc
.DrawRectangle(rect
);
3842 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3844 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3845 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3847 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3848 int row
= coords
.GetRow();
3849 int col
= coords
.GetCol();
3851 // right hand border
3853 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3854 m_colRights
[col
], m_rowBottoms
[row
] );
3858 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3859 m_colRights
[col
], m_rowBottoms
[row
] );
3863 // TODO: remove this ???
3864 // This is used to redraw all grid lines e.g. when the grid line colour
3867 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3869 if ( !m_gridLinesEnabled
||
3871 !m_numCols
) return;
3873 int top
, bottom
, left
, right
;
3877 m_gridWin
->GetClientSize(&cw
, &ch
);
3879 // virtual coords of visible area
3881 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3882 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3886 reg
.GetBox(x
, y
, w
, h
);
3887 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3888 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3891 // avoid drawing grid lines past the last row and col
3893 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3894 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3896 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3898 // horizontal grid lines
3901 for ( i
= 0; i
< m_numRows
; i
++ )
3903 if ( m_rowBottoms
[i
]-1 > bottom
)
3907 else if ( m_rowBottoms
[i
]-1 >= top
)
3909 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
3914 // vertical grid lines
3916 for ( i
= 0; i
< m_numCols
; i
++ )
3918 if ( m_colRights
[i
]-1 > right
)
3922 else if ( m_colRights
[i
]-1 >= left
)
3924 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
3930 void wxGrid::DrawRowLabels( wxDC
& dc
)
3932 if ( !m_numRows
|| !m_numCols
) return;
3935 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3937 for ( i
= 0; i
< numLabels
; i
++ )
3939 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3944 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3946 if ( m_rowHeights
[row
] <= 0 ) return;
3948 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3950 dc
.SetPen( *wxBLACK_PEN
);
3951 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3952 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3954 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3955 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3957 dc
.SetPen( *wxWHITE_PEN
);
3958 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3959 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3961 dc
.SetBackgroundMode( wxTRANSPARENT
);
3962 dc
.SetTextForeground( GetLabelTextColour() );
3963 dc
.SetFont( GetLabelFont() );
3966 GetRowLabelAlignment( &hAlign
, &vAlign
);
3970 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3971 rect
.SetWidth( m_rowLabelWidth
- 4 );
3972 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3973 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3977 void wxGrid::DrawColLabels( wxDC
& dc
)
3979 if ( !m_numRows
|| !m_numCols
) return;
3982 size_t numLabels
= m_colLabelsExposed
.GetCount();
3984 for ( i
= 0; i
< numLabels
; i
++ )
3986 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3991 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3993 if ( m_colWidths
[col
] <= 0 ) return;
3995 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3997 dc
.SetPen( *wxBLACK_PEN
);
3998 dc
.DrawLine( m_colRights
[col
]-1, 0,
3999 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4001 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4002 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4004 dc
.SetPen( *wxWHITE_PEN
);
4005 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4006 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4008 dc
.SetBackgroundMode( wxTRANSPARENT
);
4009 dc
.SetTextForeground( GetLabelTextColour() );
4010 dc
.SetFont( GetLabelFont() );
4012 dc
.SetBackgroundMode( wxTRANSPARENT
);
4013 dc
.SetTextForeground( GetLabelTextColour() );
4014 dc
.SetFont( GetLabelFont() );
4017 GetColLabelAlignment( &hAlign
, &vAlign
);
4020 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4022 rect
.SetWidth( m_colWidths
[col
] - 4 );
4023 rect
.SetHeight( m_colLabelHeight
- 4 );
4024 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4028 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4029 const wxString
& value
,
4034 long textWidth
, textHeight
;
4035 long lineWidth
, lineHeight
;
4036 wxArrayString lines
;
4038 dc
.SetClippingRegion( rect
);
4039 StringToLines( value
, lines
);
4040 if ( lines
.GetCount() )
4042 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4043 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4046 switch ( horizAlign
)
4049 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4053 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4062 switch ( vertAlign
)
4065 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4069 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4078 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4080 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4085 dc
.DestroyClippingRegion();
4089 // Split multi line text up into an array of strings. Any existing
4090 // contents of the string array are preserved.
4092 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4096 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4097 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4099 while ( startPos
< (int)tVal
.Length() )
4101 pos
= tVal
.Mid(startPos
).Find( eol
);
4106 else if ( pos
== 0 )
4108 lines
.Add( wxEmptyString
);
4112 lines
.Add( value
.Mid(startPos
, pos
) );
4116 if ( startPos
< (int)value
.Length() )
4118 lines
.Add( value
.Mid( startPos
) );
4123 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4124 wxArrayString
& lines
,
4125 long *width
, long *height
)
4132 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4134 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4135 w
= wxMax( w
, lineW
);
4145 // ------ Edit control functions
4149 void wxGrid::EnableEditing( bool edit
)
4151 // TODO: improve this ?
4153 if ( edit
!= m_editable
)
4157 EnableCellEditControl(m_editable
);
4162 void wxGrid::EnableCellEditControl( bool enable
)
4167 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4168 SetCurrentCell( 0, 0 );
4170 if ( enable
!= m_cellEditCtrlEnabled
)
4174 m_cellEditCtrlEnabled
= enable
;
4175 SetEditControlValue();
4176 ShowCellEditControl();
4180 HideCellEditControl();
4181 SaveEditControlValue();
4182 m_cellEditCtrlEnabled
= enable
;
4188 void wxGrid::ShowCellEditControl()
4190 if ( IsCellEditControlEnabled() )
4192 if ( !IsVisible( m_currentCellCoords
) )
4198 wxRect rect
= CellToRect( m_currentCellCoords
);
4199 int row
= m_currentCellCoords
.GetRow();
4200 int col
= m_currentCellCoords
.GetCol();
4202 // convert to scrolled coords
4204 int left
, top
, right
, bottom
;
4205 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4206 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4207 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4209 m_gridWin
->GetClientSize( &cw
, &ch
);
4211 // Make the edit control large enough to allow for internal margins
4212 // TODO: remove this if the text ctrl sizing is improved esp. for unix
4215 #if defined(__WXMOTIF__)
4216 if ( row
== 0 || col
== 0 )
4225 if ( row
== 0 || col
== 0 )
4235 #if defined(__WXGTK__)
4238 if (left
!= 0) left_diff
++;
4239 if (top
!= 0) top_diff
++;
4240 rect
.SetLeft( left
+ left_diff
);
4241 rect
.SetTop( top
+ top_diff
);
4242 rect
.SetRight( rect
.GetRight() - left_diff
);
4243 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4245 rect
.SetLeft( wxMax(0, left
- extra
) );
4246 rect
.SetTop( wxMax(0, top
- extra
) );
4247 rect
.SetRight( rect
.GetRight() + 2*extra
);
4248 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4251 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4252 wxGridCellEditor
* editor
= attr
->GetEditor();
4253 if (! editor
->IsCreated()) {
4254 editor
->Create(m_gridWin
, -1,
4255 new wxGridCellEditorEvtHandler(this, editor
));
4258 editor
->SetSize( rect
);
4259 editor
->Show( TRUE
);
4260 editor
->BeginEdit(row
, col
, this, attr
);
4267 void wxGrid::HideCellEditControl()
4269 if ( IsCellEditControlEnabled() )
4271 int row
= m_currentCellCoords
.GetRow();
4272 int col
= m_currentCellCoords
.GetCol();
4274 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4275 attr
->GetEditor()->Show( FALSE
);
4277 m_gridWin
->SetFocus();
4282 void wxGrid::SetEditControlValue( const wxString
& value
)
4284 // RD: The new Editors get the value from the table themselves now. This
4285 // method can probably be removed...
4289 void wxGrid::SaveEditControlValue()
4291 if (IsCellEditControlEnabled()) {
4292 int row
= m_currentCellCoords
.GetRow();
4293 int col
= m_currentCellCoords
.GetCol();
4295 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4296 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this, attr
);
4301 SendEvent( EVT_GRID_CELL_CHANGE
,
4302 m_currentCellCoords
.GetRow(),
4303 m_currentCellCoords
.GetCol() );
4310 // ------ Grid location functions
4311 // Note that all of these functions work with the logical coordinates of
4312 // grid cells and labels so you will need to convert from device
4313 // coordinates for mouse events etc.
4316 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4318 int row
= YToRow(y
);
4319 int col
= XToCol(x
);
4321 if ( row
== -1 || col
== -1 )
4323 coords
= wxGridNoCellCoords
;
4327 coords
.Set( row
, col
);
4332 int wxGrid::YToRow( int y
)
4336 for ( i
= 0; i
< m_numRows
; i
++ )
4338 if ( y
< m_rowBottoms
[i
] ) return i
;
4341 return m_numRows
; //-1;
4345 int wxGrid::XToCol( int x
)
4349 for ( i
= 0; i
< m_numCols
; i
++ )
4351 if ( x
< m_colRights
[i
] ) return i
;
4354 return m_numCols
; //-1;
4358 // return the row number that that the y coord is near the edge of, or
4359 // -1 if not near an edge
4361 int wxGrid::YToEdgeOfRow( int y
)
4365 for ( i
= 0; i
< m_numRows
; i
++ )
4367 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4369 d
= abs( y
- m_rowBottoms
[i
] );
4371 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4380 // return the col number that that the x coord is near the edge of, or
4381 // -1 if not near an edge
4383 int wxGrid::XToEdgeOfCol( int x
)
4387 for ( i
= 0; i
< m_numCols
; i
++ )
4389 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4391 d
= abs( x
- m_colRights
[i
] );
4393 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4402 wxRect
wxGrid::CellToRect( int row
, int col
)
4404 wxRect
rect( -1, -1, -1, -1 );
4406 if ( row
>= 0 && row
< m_numRows
&&
4407 col
>= 0 && col
< m_numCols
)
4409 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4410 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4411 rect
.width
= m_colWidths
[col
];
4412 rect
.height
= m_rowHeights
[ row
];
4419 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4421 // get the cell rectangle in logical coords
4423 wxRect
r( CellToRect( row
, col
) );
4425 // convert to device coords
4427 int left
, top
, right
, bottom
;
4428 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4429 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4431 // check against the client area of the grid window
4434 m_gridWin
->GetClientSize( &cw
, &ch
);
4436 if ( wholeCellVisible
)
4438 // is the cell wholly visible ?
4440 return ( left
>= 0 && right
<= cw
&&
4441 top
>= 0 && bottom
<= ch
);
4445 // is the cell partly visible ?
4447 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4448 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4453 // make the specified cell location visible by doing a minimal amount
4456 void wxGrid::MakeCellVisible( int row
, int col
)
4459 int xpos
= -1, ypos
= -1;
4461 if ( row
>= 0 && row
< m_numRows
&&
4462 col
>= 0 && col
< m_numCols
)
4464 // get the cell rectangle in logical coords
4466 wxRect
r( CellToRect( row
, col
) );
4468 // convert to device coords
4470 int left
, top
, right
, bottom
;
4471 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4472 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4475 m_gridWin
->GetClientSize( &cw
, &ch
);
4481 else if ( bottom
> ch
)
4483 int h
= r
.GetHeight();
4485 for ( i
= row
-1; i
>= 0; i
-- )
4487 if ( h
+ m_rowHeights
[i
] > ch
) break;
4489 h
+= m_rowHeights
[i
];
4490 ypos
-= m_rowHeights
[i
];
4493 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4494 // have rounding errors (this is important, because if we do, we
4495 // might not scroll at all and some cells won't be redrawn)
4496 ypos
+= GRID_SCROLL_LINE
/ 2;
4503 else if ( right
> cw
)
4505 int w
= r
.GetWidth();
4507 for ( i
= col
-1; i
>= 0; i
-- )
4509 if ( w
+ m_colWidths
[i
] > cw
) break;
4511 w
+= m_colWidths
[i
];
4512 xpos
-= m_colWidths
[i
];
4515 // see comment for ypos above
4516 xpos
+= GRID_SCROLL_LINE
/ 2;
4519 if ( xpos
!= -1 || ypos
!= -1 )
4521 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4522 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4523 Scroll( xpos
, ypos
);
4531 // ------ Grid cursor movement functions
4534 bool wxGrid::MoveCursorUp()
4536 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4537 m_currentCellCoords
.GetRow() > 0 )
4539 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4540 m_currentCellCoords
.GetCol() );
4542 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4543 m_currentCellCoords
.GetCol() );
4552 bool wxGrid::MoveCursorDown()
4554 // TODO: allow for scrolling
4556 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4557 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4559 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4560 m_currentCellCoords
.GetCol() );
4562 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4563 m_currentCellCoords
.GetCol() );
4572 bool wxGrid::MoveCursorLeft()
4574 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4575 m_currentCellCoords
.GetCol() > 0 )
4577 MakeCellVisible( m_currentCellCoords
.GetRow(),
4578 m_currentCellCoords
.GetCol() - 1 );
4580 SetCurrentCell( m_currentCellCoords
.GetRow(),
4581 m_currentCellCoords
.GetCol() - 1 );
4590 bool wxGrid::MoveCursorRight()
4592 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4593 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4595 MakeCellVisible( m_currentCellCoords
.GetRow(),
4596 m_currentCellCoords
.GetCol() + 1 );
4598 SetCurrentCell( m_currentCellCoords
.GetRow(),
4599 m_currentCellCoords
.GetCol() + 1 );
4608 bool wxGrid::MovePageUp()
4610 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4612 int row
= m_currentCellCoords
.GetRow();
4616 m_gridWin
->GetClientSize( &cw
, &ch
);
4618 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4619 int newRow
= YToRow( y
- ch
+ 1 );
4624 else if ( newRow
== row
)
4629 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4630 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4638 bool wxGrid::MovePageDown()
4640 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4642 int row
= m_currentCellCoords
.GetRow();
4643 if ( row
< m_numRows
)
4646 m_gridWin
->GetClientSize( &cw
, &ch
);
4648 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4649 int newRow
= YToRow( y
+ ch
);
4652 newRow
= m_numRows
- 1;
4654 else if ( newRow
== row
)
4659 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4660 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4668 bool wxGrid::MoveCursorUpBlock()
4671 m_currentCellCoords
!= wxGridNoCellCoords
&&
4672 m_currentCellCoords
.GetRow() > 0 )
4674 int row
= m_currentCellCoords
.GetRow();
4675 int col
= m_currentCellCoords
.GetCol();
4677 if ( m_table
->IsEmptyCell(row
, col
) )
4679 // starting in an empty cell: find the next block of
4685 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4688 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4690 // starting at the top of a block: find the next block
4696 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4701 // starting within a block: find the top of the block
4706 if ( m_table
->IsEmptyCell(row
, col
) )
4714 MakeCellVisible( row
, col
);
4715 SetCurrentCell( row
, col
);
4723 bool wxGrid::MoveCursorDownBlock()
4726 m_currentCellCoords
!= wxGridNoCellCoords
&&
4727 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4729 int row
= m_currentCellCoords
.GetRow();
4730 int col
= m_currentCellCoords
.GetCol();
4732 if ( m_table
->IsEmptyCell(row
, col
) )
4734 // starting in an empty cell: find the next block of
4737 while ( row
< m_numRows
-1 )
4740 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4743 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4745 // starting at the bottom of a block: find the next block
4748 while ( row
< m_numRows
-1 )
4751 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4756 // starting within a block: find the bottom of the block
4758 while ( row
< m_numRows
-1 )
4761 if ( m_table
->IsEmptyCell(row
, col
) )
4769 MakeCellVisible( row
, col
);
4770 SetCurrentCell( row
, col
);
4778 bool wxGrid::MoveCursorLeftBlock()
4781 m_currentCellCoords
!= wxGridNoCellCoords
&&
4782 m_currentCellCoords
.GetCol() > 0 )
4784 int row
= m_currentCellCoords
.GetRow();
4785 int col
= m_currentCellCoords
.GetCol();
4787 if ( m_table
->IsEmptyCell(row
, col
) )
4789 // starting in an empty cell: find the next block of
4795 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4798 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4800 // starting at the left of a block: find the next block
4806 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4811 // starting within a block: find the left of the block
4816 if ( m_table
->IsEmptyCell(row
, col
) )
4824 MakeCellVisible( row
, col
);
4825 SetCurrentCell( row
, col
);
4833 bool wxGrid::MoveCursorRightBlock()
4836 m_currentCellCoords
!= wxGridNoCellCoords
&&
4837 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4839 int row
= m_currentCellCoords
.GetRow();
4840 int col
= m_currentCellCoords
.GetCol();
4842 if ( m_table
->IsEmptyCell(row
, col
) )
4844 // starting in an empty cell: find the next block of
4847 while ( col
< m_numCols
-1 )
4850 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4853 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4855 // starting at the right of a block: find the next block
4858 while ( col
< m_numCols
-1 )
4861 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4866 // starting within a block: find the right of the block
4868 while ( col
< m_numCols
-1 )
4871 if ( m_table
->IsEmptyCell(row
, col
) )
4879 MakeCellVisible( row
, col
);
4880 SetCurrentCell( row
, col
);
4891 // ------ Label values and formatting
4894 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4896 *horiz
= m_rowLabelHorizAlign
;
4897 *vert
= m_rowLabelVertAlign
;
4900 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4902 *horiz
= m_colLabelHorizAlign
;
4903 *vert
= m_colLabelVertAlign
;
4906 wxString
wxGrid::GetRowLabelValue( int row
)
4910 return m_table
->GetRowLabelValue( row
);
4920 wxString
wxGrid::GetColLabelValue( int col
)
4924 return m_table
->GetColLabelValue( col
);
4935 void wxGrid::SetRowLabelSize( int width
)
4937 width
= wxMax( width
, 0 );
4938 if ( width
!= m_rowLabelWidth
)
4942 m_rowLabelWin
->Show( FALSE
);
4943 m_cornerLabelWin
->Show( FALSE
);
4945 else if ( m_rowLabelWidth
== 0 )
4947 m_rowLabelWin
->Show( TRUE
);
4948 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4951 m_rowLabelWidth
= width
;
4958 void wxGrid::SetColLabelSize( int height
)
4960 height
= wxMax( height
, 0 );
4961 if ( height
!= m_colLabelHeight
)
4965 m_colLabelWin
->Show( FALSE
);
4966 m_cornerLabelWin
->Show( FALSE
);
4968 else if ( m_colLabelHeight
== 0 )
4970 m_colLabelWin
->Show( TRUE
);
4971 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4974 m_colLabelHeight
= height
;
4981 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4983 if ( m_labelBackgroundColour
!= colour
)
4985 m_labelBackgroundColour
= colour
;
4986 m_rowLabelWin
->SetBackgroundColour( colour
);
4987 m_colLabelWin
->SetBackgroundColour( colour
);
4988 m_cornerLabelWin
->SetBackgroundColour( colour
);
4990 if ( !GetBatchCount() )
4992 m_rowLabelWin
->Refresh();
4993 m_colLabelWin
->Refresh();
4994 m_cornerLabelWin
->Refresh();
4999 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5001 if ( m_labelTextColour
!= colour
)
5003 m_labelTextColour
= colour
;
5004 if ( !GetBatchCount() )
5006 m_rowLabelWin
->Refresh();
5007 m_colLabelWin
->Refresh();
5012 void wxGrid::SetLabelFont( const wxFont
& font
)
5015 if ( !GetBatchCount() )
5017 m_rowLabelWin
->Refresh();
5018 m_colLabelWin
->Refresh();
5022 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5024 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5026 m_rowLabelHorizAlign
= horiz
;
5029 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5031 m_rowLabelVertAlign
= vert
;
5034 if ( !GetBatchCount() )
5036 m_rowLabelWin
->Refresh();
5040 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5042 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5044 m_colLabelHorizAlign
= horiz
;
5047 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5049 m_colLabelVertAlign
= vert
;
5052 if ( !GetBatchCount() )
5054 m_colLabelWin
->Refresh();
5058 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5062 m_table
->SetRowLabelValue( row
, s
);
5063 if ( !GetBatchCount() )
5065 wxRect rect
= CellToRect( row
, 0);
5066 if ( rect
.height
> 0 )
5068 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5070 rect
.width
= m_rowLabelWidth
;
5071 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5077 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5081 m_table
->SetColLabelValue( col
, s
);
5082 if ( !GetBatchCount() )
5084 wxRect rect
= CellToRect( 0, col
);
5085 if ( rect
.width
> 0 )
5087 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5089 rect
.height
= m_colLabelHeight
;
5090 m_colLabelWin
->Refresh( TRUE
, &rect
);
5096 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5098 if ( m_gridLineColour
!= colour
)
5100 m_gridLineColour
= colour
;
5102 wxClientDC
dc( m_gridWin
);
5104 DrawAllGridLines( dc
, wxRegion() );
5108 void wxGrid::EnableGridLines( bool enable
)
5110 if ( enable
!= m_gridLinesEnabled
)
5112 m_gridLinesEnabled
= enable
;
5114 if ( !GetBatchCount() )
5118 wxClientDC
dc( m_gridWin
);
5120 DrawAllGridLines( dc
, wxRegion() );
5124 m_gridWin
->Refresh();
5131 int wxGrid::GetDefaultRowSize()
5133 return m_defaultRowHeight
;
5136 int wxGrid::GetRowSize( int row
)
5138 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5140 return m_rowHeights
[row
];
5143 int wxGrid::GetDefaultColSize()
5145 return m_defaultColWidth
;
5148 int wxGrid::GetColSize( int col
)
5150 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5152 return m_colWidths
[col
];
5155 // ============================================================================
5156 // access to the grid attributes: each of them has a default value in the grid
5157 // itself and may be overidden on a per-cell basis
5158 // ============================================================================
5160 // ----------------------------------------------------------------------------
5161 // setting default attributes
5162 // ----------------------------------------------------------------------------
5164 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5166 m_defaultCellAttr
->SetBackgroundColour(col
);
5169 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5171 m_defaultCellAttr
->SetTextColour(col
);
5174 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5176 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5179 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5181 m_defaultCellAttr
->SetFont(font
);
5184 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5186 m_defaultCellAttr
->SetRenderer(renderer
);
5189 // ----------------------------------------------------------------------------
5190 // access to the default attrbiutes
5191 // ----------------------------------------------------------------------------
5193 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5195 return m_defaultCellAttr
->GetBackgroundColour();
5198 wxColour
wxGrid::GetDefaultCellTextColour()
5200 return m_defaultCellAttr
->GetTextColour();
5203 wxFont
wxGrid::GetDefaultCellFont()
5205 return m_defaultCellAttr
->GetFont();
5208 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5210 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5213 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5215 return m_defaultCellAttr
->GetRenderer();
5218 // ----------------------------------------------------------------------------
5219 // access to cell attributes
5220 // ----------------------------------------------------------------------------
5222 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5224 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5225 wxColour colour
= attr
->GetBackgroundColour();
5230 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5232 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5233 wxColour colour
= attr
->GetTextColour();
5238 wxFont
wxGrid::GetCellFont( int row
, int col
)
5240 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5241 wxFont font
= attr
->GetFont();
5246 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5248 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5249 attr
->GetAlignment(horiz
, vert
);
5253 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5255 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5256 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5261 // ----------------------------------------------------------------------------
5262 // attribute support: cache, automatic provider creation, ...
5263 // ----------------------------------------------------------------------------
5265 bool wxGrid::CanHaveAttributes()
5272 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5273 // table is providing the attributes itself??? In which case
5274 // I don't think the grid should create a Provider object for the
5275 // table but the table should be smart enough to do that on its own.
5276 if ( !m_table
->GetAttrProvider() )
5278 // use the default attr provider by default
5279 // (another choice would be to just return FALSE thus forcing the user
5281 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5287 void wxGrid::ClearAttrCache()
5289 if ( m_attrCache
.row
!= -1 )
5291 m_attrCache
.attr
->SafeDecRef();
5292 m_attrCache
.row
= -1;
5296 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5298 wxGrid
*self
= (wxGrid
*)this; // const_cast
5300 self
->ClearAttrCache();
5301 self
->m_attrCache
.row
= row
;
5302 self
->m_attrCache
.col
= col
;
5303 self
->m_attrCache
.attr
= attr
;
5307 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5309 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5311 *attr
= m_attrCache
.attr
;
5312 (*attr
)->SafeIncRef();
5314 #ifdef DEBUG_ATTR_CACHE
5315 gs_nAttrCacheHits
++;
5322 #ifdef DEBUG_ATTR_CACHE
5323 gs_nAttrCacheMisses
++;
5329 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5331 wxGridCellAttr
*attr
;
5332 if ( !LookupAttr(row
, col
, &attr
) )
5334 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5335 CacheAttr(row
, col
, attr
);
5338 attr
->SetDefAttr(m_defaultCellAttr
);
5340 attr
= m_defaultCellAttr
;
5347 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5349 wxGridCellAttr
*attr
;
5350 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5352 wxASSERT_MSG( m_table
,
5353 _T("we may only be called if CanHaveAttributes() "
5354 "returned TRUE and then m_table should be !NULL") );
5356 attr
= m_table
->GetAttr(row
, col
);
5359 attr
= new wxGridCellAttr
;
5361 // artificially inc the ref count to match DecRef() in caller
5364 m_table
->SetAttr(attr
, row
, col
);
5367 CacheAttr(row
, col
, attr
);
5369 attr
->SetDefAttr(m_defaultCellAttr
);
5373 // ----------------------------------------------------------------------------
5374 // setting cell attributes: this is forwarded to the table
5375 // ----------------------------------------------------------------------------
5377 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5379 if ( CanHaveAttributes() )
5381 m_table
->SetRowAttr(attr
, row
);
5389 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5391 if ( CanHaveAttributes() )
5393 m_table
->SetColAttr(attr
, col
);
5401 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5403 if ( CanHaveAttributes() )
5405 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5406 attr
->SetBackgroundColour(colour
);
5411 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5413 if ( CanHaveAttributes() )
5415 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5416 attr
->SetTextColour(colour
);
5421 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5423 if ( CanHaveAttributes() )
5425 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5426 attr
->SetFont(font
);
5431 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5433 if ( CanHaveAttributes() )
5435 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5436 attr
->SetAlignment(horiz
, vert
);
5441 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5443 if ( CanHaveAttributes() )
5445 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5446 attr
->SetRenderer(renderer
);
5451 // ----------------------------------------------------------------------------
5453 // ----------------------------------------------------------------------------
5455 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5457 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5459 if ( resizeExistingRows
)
5463 for ( row
= 0; row
< m_numRows
; row
++ )
5465 m_rowHeights
[row
] = m_defaultRowHeight
;
5466 bottom
+= m_defaultRowHeight
;
5467 m_rowBottoms
[row
] = bottom
;
5473 void wxGrid::SetRowSize( int row
, int height
)
5475 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5479 int h
= wxMax( 0, height
);
5480 int diff
= h
- m_rowHeights
[row
];
5482 m_rowHeights
[row
] = h
;
5483 for ( i
= row
; i
< m_numRows
; i
++ )
5485 m_rowBottoms
[i
] += diff
;
5490 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5492 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5494 if ( resizeExistingCols
)
5498 for ( col
= 0; col
< m_numCols
; col
++ )
5500 m_colWidths
[col
] = m_defaultColWidth
;
5501 right
+= m_defaultColWidth
;
5502 m_colRights
[col
] = right
;
5508 void wxGrid::SetColSize( int col
, int width
)
5510 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5514 int w
= wxMax( 0, width
);
5515 int diff
= w
- m_colWidths
[col
];
5516 m_colWidths
[col
] = w
;
5518 for ( i
= col
; i
< m_numCols
; i
++ )
5520 m_colRights
[i
] += diff
;
5527 // ------ cell value accessor functions
5530 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5534 m_table
->SetValue( row
, col
, s
.c_str() );
5535 if ( !GetBatchCount() )
5537 wxClientDC
dc( m_gridWin
);
5539 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5542 #if 0 // TODO: edit in place
5544 if ( m_currentCellCoords
.GetRow() == row
&&
5545 m_currentCellCoords
.GetCol() == col
)
5547 SetEditControlValue( s
);
5556 // ------ Block, row and col selection
5559 void wxGrid::SelectRow( int row
, bool addToSelected
)
5563 if ( IsSelection() && addToSelected
)
5566 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5569 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5570 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5571 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5572 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5576 need_refresh
[0] = TRUE
;
5577 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5578 wxGridCellCoords ( oldTop
- 1,
5580 m_selectedTopLeft
.SetRow( row
);
5585 need_refresh
[1] = TRUE
;
5586 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5587 wxGridCellCoords ( oldBottom
,
5590 m_selectedTopLeft
.SetCol( 0 );
5593 if ( oldBottom
< row
)
5595 need_refresh
[2] = TRUE
;
5596 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5597 wxGridCellCoords ( row
,
5599 m_selectedBottomRight
.SetRow( row
);
5602 if ( oldRight
< m_numCols
- 1 )
5604 need_refresh
[3] = TRUE
;
5605 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5607 wxGridCellCoords ( oldBottom
,
5609 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5612 for (i
= 0; i
< 4; i
++ )
5613 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5614 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5618 r
= SelectionToDeviceRect();
5620 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5622 m_selectedTopLeft
.Set( row
, 0 );
5623 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5624 r
= SelectionToDeviceRect();
5625 m_gridWin
->Refresh( FALSE
, &r
);
5628 wxGridRangeSelectEvent
gridEvt( GetId(),
5629 EVT_GRID_RANGE_SELECT
,
5632 m_selectedBottomRight
);
5634 GetEventHandler()->ProcessEvent(gridEvt
);
5638 void wxGrid::SelectCol( int col
, bool addToSelected
)
5640 if ( IsSelection() && addToSelected
)
5643 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5646 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5647 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5648 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5649 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5651 if ( oldLeft
> col
)
5653 need_refresh
[0] = TRUE
;
5654 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5655 wxGridCellCoords ( m_numRows
- 1,
5657 m_selectedTopLeft
.SetCol( col
);
5662 need_refresh
[1] = TRUE
;
5663 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5664 wxGridCellCoords ( oldTop
- 1,
5666 m_selectedTopLeft
.SetRow( 0 );
5669 if ( oldRight
< col
)
5671 need_refresh
[2] = TRUE
;
5672 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5673 wxGridCellCoords ( m_numRows
- 1,
5675 m_selectedBottomRight
.SetCol( col
);
5678 if ( oldBottom
< m_numRows
- 1 )
5680 need_refresh
[3] = TRUE
;
5681 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5683 wxGridCellCoords ( m_numRows
- 1,
5685 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5688 for (i
= 0; i
< 4; i
++ )
5689 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5690 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5696 r
= SelectionToDeviceRect();
5698 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5700 m_selectedTopLeft
.Set( 0, col
);
5701 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5702 r
= SelectionToDeviceRect();
5703 m_gridWin
->Refresh( FALSE
, &r
);
5706 wxGridRangeSelectEvent
gridEvt( GetId(),
5707 EVT_GRID_RANGE_SELECT
,
5710 m_selectedBottomRight
);
5712 GetEventHandler()->ProcessEvent(gridEvt
);
5716 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5719 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5721 if ( topRow
> bottomRow
)
5728 if ( leftCol
> rightCol
)
5735 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5736 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5738 if ( m_selectedTopLeft
!= updateTopLeft
||
5739 m_selectedBottomRight
!= updateBottomRight
)
5741 // Compute two optimal update rectangles:
5742 // Either one rectangle is a real subset of the
5743 // other, or they are (almost) disjoint!
5745 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5748 // Store intermediate values
5749 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5750 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5751 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5752 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5754 // Determine the outer/inner coordinates.
5755 if (oldLeft
> leftCol
)
5761 if (oldTop
> topRow
)
5767 if (oldRight
< rightCol
)
5770 oldRight
= rightCol
;
5773 if (oldBottom
< bottomRow
)
5776 oldBottom
= bottomRow
;
5780 // Now, either the stuff marked old is the outer
5781 // rectangle or we don't have a situation where one
5782 // is contained in the other.
5784 if ( oldLeft
< leftCol
)
5786 need_refresh
[0] = TRUE
;
5787 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5789 wxGridCellCoords ( oldBottom
,
5793 if ( oldTop
< topRow
)
5795 need_refresh
[1] = TRUE
;
5796 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5798 wxGridCellCoords ( topRow
- 1,
5802 if ( oldRight
> rightCol
)
5804 need_refresh
[2] = TRUE
;
5805 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5807 wxGridCellCoords ( oldBottom
,
5811 if ( oldBottom
> bottomRow
)
5813 need_refresh
[3] = TRUE
;
5814 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5816 wxGridCellCoords ( oldBottom
,
5822 m_selectedTopLeft
= updateTopLeft
;
5823 m_selectedBottomRight
= updateBottomRight
;
5825 // various Refresh() calls
5826 for (i
= 0; i
< 4; i
++ )
5827 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5828 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5831 // only generate an event if the block is not being selected by
5832 // dragging the mouse (in which case the event will be generated in
5833 // the mouse event handler)
5834 if ( !m_isDragging
)
5836 wxGridRangeSelectEvent
gridEvt( GetId(),
5837 EVT_GRID_RANGE_SELECT
,
5840 m_selectedBottomRight
);
5842 GetEventHandler()->ProcessEvent(gridEvt
);
5846 void wxGrid::SelectAll()
5848 m_selectedTopLeft
.Set( 0, 0 );
5849 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5851 m_gridWin
->Refresh();
5855 void wxGrid::ClearSelection()
5857 m_selectedTopLeft
= wxGridNoCellCoords
;
5858 m_selectedBottomRight
= wxGridNoCellCoords
;
5862 // This function returns the rectangle that encloses the given block
5863 // in device coords clipped to the client size of the grid window.
5865 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5866 const wxGridCellCoords
&bottomRight
)
5868 wxRect
rect( wxGridNoCellRect
);
5871 cellRect
= CellToRect( topLeft
);
5872 if ( cellRect
!= wxGridNoCellRect
)
5878 rect
= wxRect( 0, 0, 0, 0 );
5881 cellRect
= CellToRect( bottomRight
);
5882 if ( cellRect
!= wxGridNoCellRect
)
5888 return wxGridNoCellRect
;
5891 // convert to scrolled coords
5893 int left
, top
, right
, bottom
;
5894 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5895 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5898 m_gridWin
->GetClientSize( &cw
, &ch
);
5900 rect
.SetLeft( wxMax(0, left
) );
5901 rect
.SetTop( wxMax(0, top
) );
5902 rect
.SetRight( wxMin(cw
, right
) );
5903 rect
.SetBottom( wxMin(ch
, bottom
) );
5911 // ------ Grid event classes
5914 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5916 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5917 int row
, int col
, int x
, int y
,
5918 bool control
, bool shift
, bool alt
, bool meta
)
5919 : wxNotifyEvent( type
, id
)
5925 m_control
= control
;
5930 SetEventObject(obj
);
5934 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5936 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5937 int rowOrCol
, int x
, int y
,
5938 bool control
, bool shift
, bool alt
, bool meta
)
5939 : wxNotifyEvent( type
, id
)
5941 m_rowOrCol
= rowOrCol
;
5944 m_control
= control
;
5949 SetEventObject(obj
);
5953 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5955 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5956 const wxGridCellCoords
& topLeft
,
5957 const wxGridCellCoords
& bottomRight
,
5958 bool control
, bool shift
, bool alt
, bool meta
)
5959 : wxNotifyEvent( type
, id
)
5961 m_topLeft
= topLeft
;
5962 m_bottomRight
= bottomRight
;
5963 m_control
= control
;
5968 SetEventObject(obj
);
5972 #endif // ifndef wxUSE_NEW_GRID