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
);
185 void OnChar(wxKeyEvent
& event
);
189 wxGridCellEditor
* m_editor
;
190 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
191 DECLARE_EVENT_TABLE()
195 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
196 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
197 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
198 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
203 // ----------------------------------------------------------------------------
204 // the internal data representation used by wxGridCellAttrProvider
205 // ----------------------------------------------------------------------------
207 // this class stores attributes set for cells
208 class WXDLLEXPORT wxGridCellAttrData
211 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
212 wxGridCellAttr
*GetAttr(int row
, int col
) const;
213 void UpdateAttrRows( size_t pos
, int numRows
);
214 void UpdateAttrCols( size_t pos
, int numCols
);
217 // searches for the attr for given cell, returns wxNOT_FOUND if not found
218 int FindIndex(int row
, int col
) const;
220 wxGridCellWithAttrArray m_attrs
;
223 // this class stores attributes set for rows or columns
224 class WXDLLEXPORT wxGridRowOrColAttrData
227 ~wxGridRowOrColAttrData();
229 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
230 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
231 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
234 wxArrayInt m_rowsOrCols
;
235 wxArrayAttrs m_attrs
;
238 // NB: this is just a wrapper around 3 objects: one which stores cell
239 // attributes, and 2 others for row/col ones
240 class WXDLLEXPORT wxGridCellAttrProviderData
243 wxGridCellAttrData m_cellAttrs
;
244 wxGridRowOrColAttrData m_rowAttrs
,
248 // ----------------------------------------------------------------------------
249 // conditional compilation
250 // ----------------------------------------------------------------------------
252 #ifndef WXGRID_DRAW_LINES
253 #define WXGRID_DRAW_LINES 1
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 //#define DEBUG_ATTR_CACHE
261 #ifdef DEBUG_ATTR_CACHE
262 static size_t gs_nAttrCacheHits
= 0;
263 static size_t gs_nAttrCacheMisses
= 0;
264 #endif // DEBUG_ATTR_CACHE
266 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
267 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
270 // TODO: fixed so far - make configurable later (and also different for x/y)
271 static const size_t GRID_SCROLL_LINE
= 10;
273 // ============================================================================
275 // ============================================================================
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 wxGridCellEditor::wxGridCellEditor()
287 wxGridCellEditor::~wxGridCellEditor()
293 void wxGridCellEditor::Destroy()
296 m_control
->Destroy();
301 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
303 wxASSERT_MSG(m_control
,
304 wxT("The wxGridCellEditor must be Created first!"));
305 m_control
->Show(show
);
309 // set the colours/fonts if we have any
312 if ( attr
->HasTextColour() )
314 m_colFgOld
= m_control
->GetForegroundColour();
315 m_control
->SetForegroundColour(attr
->GetTextColour());
318 if ( attr
->HasBackgroundColour() )
320 m_colBgOld
= m_control
->GetBackgroundColour();
321 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
324 if ( attr
->HasFont() )
326 m_fontOld
= m_control
->GetFont();
327 m_control
->SetFont(attr
->GetFont());
330 // can't do anything more in the base class version, the other
331 // attributes may only be used by the derived classes
336 // restore the standard colours fonts
337 if ( m_colFgOld
.Ok() )
339 m_control
->SetForegroundColour(m_colFgOld
);
340 m_colFgOld
= wxNullColour
;
343 if ( m_colBgOld
.Ok() )
345 m_control
->SetBackgroundColour(m_colBgOld
);
346 m_colBgOld
= wxNullColour
;
349 if ( m_fontOld
.Ok() )
351 m_control
->SetFont(m_fontOld
);
352 m_fontOld
= wxNullFont
;
357 void wxGridCellEditor::SetSize(const wxRect
& rect
)
359 wxASSERT_MSG(m_control
,
360 wxT("The wxGridCellEditor must be Created first!"));
361 m_control
->SetSize(rect
);
364 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
370 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
372 wxASSERT_MSG(m_control
,
373 wxT("The wxGridCellEditor must be Created first!"));
375 // pass the event to the control
376 m_control
->GetEventHandler()->ProcessEvent(event
);
379 // ----------------------------------------------------------------------------
380 // wxGridCellTextEditor
381 // ----------------------------------------------------------------------------
383 wxGridCellTextEditor::wxGridCellTextEditor()
387 void wxGridCellTextEditor::Create(wxWindow
* parent
,
389 wxEvtHandler
* evtHandler
)
391 m_control
= new wxTextCtrl(parent
, -1, "",
392 wxDefaultPosition
, wxDefaultSize
393 #if defined(__WXMSW__)
394 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
399 m_control
->PushEventHandler(evtHandler
);
403 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
405 wxASSERT_MSG(m_control
,
406 wxT("The wxGridCellEditor must be Created first!"));
408 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
409 Text()->SetValue(m_startValue
);
410 Text()->SetInsertionPointEnd();
416 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
419 wxASSERT_MSG(m_control
,
420 wxT("The wxGridCellEditor must be Created first!"));
422 bool changed
= FALSE
;
423 wxString value
= Text()->GetValue();
424 if (value
!= m_startValue
)
428 grid
->GetTable()->SetValue(row
, col
, value
);
430 m_startValue
= wxEmptyString
;
431 Text()->SetValue(m_startValue
);
437 void wxGridCellTextEditor::Reset()
439 wxASSERT_MSG(m_control
,
440 wxT("The wxGridCellEditor must be Created first!"));
442 Text()->SetValue(m_startValue
);
443 Text()->SetInsertionPointEnd();
446 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
448 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
450 // insert the key in the control
451 long keycode
= event
.KeyCode();
452 if ( isprint(keycode
) )
454 // FIXME this is not going to work for non letters...
455 if ( !event
.ShiftDown() )
457 keycode
= tolower(keycode
);
460 Text()->AppendText((wxChar
)keycode
);
470 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
472 #if defined(__WXMOTIF__) || defined(__WXGTK__)
473 // wxMotif needs a little extra help...
474 int pos
= Text()->GetInsertionPoint();
475 wxString
s( Text()->GetValue() );
476 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
478 Text()->SetInsertionPoint( pos
);
480 // the other ports can handle a Return key press
487 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
489 switch ( event
.KeyCode() )
493 m_grid
->DisableCellEditControl();
497 event
.Skip( m_grid
->ProcessEvent( event
) );
501 if (!m_grid
->ProcessEvent(event
))
502 m_editor
->HandleReturn(event
);
511 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
513 switch ( event
.KeyCode() )
525 // ----------------------------------------------------------------------------
526 // wxGridCellRenderer
527 // ----------------------------------------------------------------------------
529 void wxGridCellRenderer::Draw(wxGrid
& grid
,
530 wxGridCellAttr
& attr
,
536 dc
.SetBackgroundMode( wxSOLID
);
540 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
544 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
547 dc
.SetPen( *wxTRANSPARENT_PEN
);
548 dc
.DrawRectangle(rect
);
551 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
552 wxGridCellAttr
& attr
,
554 const wxRect
& rectCell
,
558 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
560 // now we only have to draw the text
561 dc
.SetBackgroundMode( wxTRANSPARENT
);
563 // TODO some special colours for attr.IsReadOnly() case?
567 dc
.SetTextBackground( grid
.GetSelectionBackground() );
568 dc
.SetTextForeground( grid
.GetSelectionForeground() );
572 dc
.SetTextBackground( attr
.GetBackgroundColour() );
573 dc
.SetTextForeground( attr
.GetTextColour() );
575 dc
.SetFont( attr
.GetFont() );
578 attr
.GetAlignment(&hAlign
, &vAlign
);
580 wxRect rect
= rectCell
;
586 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
587 rect
, hAlign
, vAlign
);
590 // ----------------------------------------------------------------------------
592 // ----------------------------------------------------------------------------
594 const wxColour
& wxGridCellAttr::GetTextColour() const
598 else if (m_defGridAttr
!= this)
599 return m_defGridAttr
->GetTextColour();
601 wxFAIL_MSG(wxT("Missing default cell attribute"));
607 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
609 if (HasBackgroundColour())
611 else if (m_defGridAttr
!= this)
612 return m_defGridAttr
->GetBackgroundColour();
614 wxFAIL_MSG(wxT("Missing default cell attribute"));
620 const wxFont
& wxGridCellAttr::GetFont() const
624 else if (m_defGridAttr
!= this)
625 return m_defGridAttr
->GetFont();
627 wxFAIL_MSG(wxT("Missing default cell attribute"));
633 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
635 if (HasAlignment()) {
636 if ( hAlign
) *hAlign
= m_hAlign
;
637 if ( vAlign
) *vAlign
= m_vAlign
;
639 else if (m_defGridAttr
!= this)
640 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
642 wxFAIL_MSG(wxT("Missing default cell attribute"));
647 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
651 else if (m_defGridAttr
!= this)
652 return m_defGridAttr
->GetRenderer();
654 wxFAIL_MSG(wxT("Missing default cell attribute"));
659 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
663 else if (m_defGridAttr
!= this)
664 return m_defGridAttr
->GetEditor();
666 wxFAIL_MSG(wxT("Missing default cell attribute"));
671 // ----------------------------------------------------------------------------
672 // wxGridCellAttrData
673 // ----------------------------------------------------------------------------
675 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
677 int n
= FindIndex(row
, col
);
678 if ( n
== wxNOT_FOUND
)
681 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
687 // change the attribute
688 m_attrs
[(size_t)n
].attr
= attr
;
692 // remove this attribute
693 m_attrs
.RemoveAt((size_t)n
);
698 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
700 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
702 int n
= FindIndex(row
, col
);
703 if ( n
!= wxNOT_FOUND
)
705 attr
= m_attrs
[(size_t)n
].attr
;
712 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
714 size_t count
= m_attrs
.GetCount();
715 for ( size_t n
= 0; n
< count
; n
++ )
717 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
718 wxCoord row
= coords
.GetRow();
719 if ((size_t)row
>= pos
)
723 // If rows inserted, include row counter where necessary
724 coords
.SetRow(row
+ numRows
);
726 else if (numRows
< 0)
728 // If rows deleted ...
729 if ((size_t)row
>= pos
- numRows
)
731 // ...either decrement row counter (if row still exists)...
732 coords
.SetRow(row
+ numRows
);
736 // ...or remove the attribute
737 m_attrs
.RemoveAt((size_t)n
);
745 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
747 size_t count
= m_attrs
.GetCount();
748 for ( size_t n
= 0; n
< count
; n
++ )
750 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
751 wxCoord col
= coords
.GetCol();
752 if ( (size_t)col
>= pos
)
756 // If rows inserted, include row counter where necessary
757 coords
.SetCol(col
+ numCols
);
759 else if (numCols
< 0)
761 // If rows deleted ...
762 if ((size_t)col
>= pos
- numCols
)
764 // ...either decrement row counter (if row still exists)...
765 coords
.SetCol(col
+ numCols
);
769 // ...or remove the attribute
770 m_attrs
.RemoveAt((size_t)n
);
778 int wxGridCellAttrData::FindIndex(int row
, int col
) const
780 size_t count
= m_attrs
.GetCount();
781 for ( size_t n
= 0; n
< count
; n
++ )
783 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
784 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
793 // ----------------------------------------------------------------------------
794 // wxGridRowOrColAttrData
795 // ----------------------------------------------------------------------------
797 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
799 size_t count
= m_attrs
.Count();
800 for ( size_t n
= 0; n
< count
; n
++ )
802 m_attrs
[n
]->DecRef();
806 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
808 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
810 int n
= m_rowsOrCols
.Index(rowOrCol
);
811 if ( n
!= wxNOT_FOUND
)
813 attr
= m_attrs
[(size_t)n
];
820 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
822 int n
= m_rowsOrCols
.Index(rowOrCol
);
823 if ( n
== wxNOT_FOUND
)
826 m_rowsOrCols
.Add(rowOrCol
);
833 // change the attribute
834 m_attrs
[(size_t)n
] = attr
;
838 // remove this attribute
839 m_attrs
[(size_t)n
]->DecRef();
840 m_rowsOrCols
.RemoveAt((size_t)n
);
841 m_attrs
.RemoveAt((size_t)n
);
846 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
848 size_t count
= m_attrs
.GetCount();
849 for ( size_t n
= 0; n
< count
; n
++ )
851 int & rowOrCol
= m_rowsOrCols
[n
];
852 if ( (size_t)rowOrCol
>= pos
)
854 if ( numRowsOrCols
> 0 )
856 // If rows inserted, include row counter where necessary
857 rowOrCol
+= numRowsOrCols
;
859 else if ( numRowsOrCols
< 0)
861 // If rows deleted, either decrement row counter (if row still exists)
862 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
863 rowOrCol
+= numRowsOrCols
;
866 m_rowsOrCols
.RemoveAt((size_t)n
);
867 m_attrs
.RemoveAt((size_t)n
);
875 // ----------------------------------------------------------------------------
876 // wxGridCellAttrProvider
877 // ----------------------------------------------------------------------------
879 wxGridCellAttrProvider::wxGridCellAttrProvider()
881 m_data
= (wxGridCellAttrProviderData
*)NULL
;
884 wxGridCellAttrProvider::~wxGridCellAttrProvider()
889 void wxGridCellAttrProvider::InitData()
891 m_data
= new wxGridCellAttrProviderData
;
894 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
896 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
899 // first look for the attribute of this specific cell
900 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
904 // then look for the col attr (col attributes are more common than
905 // the row ones, hence they have priority)
906 attr
= m_data
->m_colAttrs
.GetAttr(col
);
911 // finally try the row attributes
912 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
919 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
925 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
928 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
933 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
936 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
941 m_data
->m_colAttrs
.SetAttr(attr
, col
);
944 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
948 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
950 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
954 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
958 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
960 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
964 // ----------------------------------------------------------------------------
966 // ----------------------------------------------------------------------------
968 //////////////////////////////////////////////////////////////////////
970 // Abstract base class for grid data (the model)
972 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
975 wxGridTableBase::wxGridTableBase()
977 m_view
= (wxGrid
*) NULL
;
978 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
981 wxGridTableBase::~wxGridTableBase()
983 delete m_attrProvider
;
986 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
988 delete m_attrProvider
;
989 m_attrProvider
= attrProvider
;
992 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
994 if ( m_attrProvider
)
995 return m_attrProvider
->GetAttr(row
, col
);
997 return (wxGridCellAttr
*)NULL
;
1000 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1002 if ( m_attrProvider
)
1004 m_attrProvider
->SetAttr(attr
, row
, col
);
1008 // as we take ownership of the pointer and don't store it, we must
1014 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1016 if ( m_attrProvider
)
1018 m_attrProvider
->SetRowAttr(attr
, row
);
1022 // as we take ownership of the pointer and don't store it, we must
1028 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1030 if ( m_attrProvider
)
1032 m_attrProvider
->SetColAttr(attr
, col
);
1036 // as we take ownership of the pointer and don't store it, we must
1042 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1044 if ( m_attrProvider
)
1046 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1050 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1052 if ( m_attrProvider
)
1054 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1058 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1060 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1061 "but your derived table class does not override this function") );
1066 bool wxGridTableBase::AppendRows( size_t numRows
)
1068 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1069 "but your derived table class does not override this function"));
1074 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1076 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1077 "but your derived table class does not override this function"));
1082 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1084 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1085 "but your derived table class does not override this function"));
1090 bool wxGridTableBase::AppendCols( size_t numCols
)
1092 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1093 "but your derived table class does not override this function"));
1098 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1100 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1101 "but your derived table class does not override this function"));
1107 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1114 wxString
wxGridTableBase::GetColLabelValue( int col
)
1116 // default col labels are:
1117 // cols 0 to 25 : A-Z
1118 // cols 26 to 675 : AA-ZZ
1123 for ( n
= 1; ; n
++ )
1125 s
+= (_T('A') + (wxChar
)( col%26
));
1127 if ( col
< 0 ) break;
1130 // reverse the string...
1132 for ( i
= 0; i
< n
; i
++ )
1142 //////////////////////////////////////////////////////////////////////
1144 // Message class for the grid table to send requests and notifications
1148 wxGridTableMessage::wxGridTableMessage()
1150 m_table
= (wxGridTableBase
*) NULL
;
1156 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1157 int commandInt1
, int commandInt2
)
1161 m_comInt1
= commandInt1
;
1162 m_comInt2
= commandInt2
;
1167 //////////////////////////////////////////////////////////////////////
1169 // A basic grid table for string data. An object of this class will
1170 // created by wxGrid if you don't specify an alternative table class.
1173 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1175 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1177 wxGridStringTable::wxGridStringTable()
1182 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1187 m_data
.Alloc( numRows
);
1190 sa
.Alloc( numCols
);
1191 for ( col
= 0; col
< numCols
; col
++ )
1193 sa
.Add( wxEmptyString
);
1196 for ( row
= 0; row
< numRows
; row
++ )
1202 wxGridStringTable::~wxGridStringTable()
1206 long wxGridStringTable::GetNumberRows()
1208 return m_data
.GetCount();
1211 long wxGridStringTable::GetNumberCols()
1213 if ( m_data
.GetCount() > 0 )
1214 return m_data
[0].GetCount();
1219 wxString
wxGridStringTable::GetValue( int row
, int col
)
1221 // TODO: bounds checking
1223 return m_data
[row
][col
];
1226 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1228 // TODO: bounds checking
1230 m_data
[row
][col
] = s
;
1233 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1235 // TODO: bounds checking
1237 return (m_data
[row
][col
] == wxEmptyString
);
1241 void wxGridStringTable::Clear()
1244 int numRows
, numCols
;
1246 numRows
= m_data
.GetCount();
1249 numCols
= m_data
[0].GetCount();
1251 for ( row
= 0; row
< numRows
; row
++ )
1253 for ( col
= 0; col
< numCols
; col
++ )
1255 m_data
[row
][col
] = wxEmptyString
;
1262 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1266 size_t curNumRows
= m_data
.GetCount();
1267 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1269 if ( pos
>= curNumRows
)
1271 return AppendRows( numRows
);
1275 sa
.Alloc( curNumCols
);
1276 for ( col
= 0; col
< curNumCols
; col
++ )
1278 sa
.Add( wxEmptyString
);
1281 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1283 m_data
.Insert( sa
, row
);
1285 UpdateAttrRows( pos
, numRows
);
1288 wxGridTableMessage
msg( this,
1289 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1293 GetView()->ProcessTableMessage( msg
);
1299 bool wxGridStringTable::AppendRows( size_t numRows
)
1303 size_t curNumRows
= m_data
.GetCount();
1304 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1307 if ( curNumCols
> 0 )
1309 sa
.Alloc( curNumCols
);
1310 for ( col
= 0; col
< curNumCols
; col
++ )
1312 sa
.Add( wxEmptyString
);
1316 for ( row
= 0; row
< numRows
; row
++ )
1323 wxGridTableMessage
msg( this,
1324 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1327 GetView()->ProcessTableMessage( msg
);
1333 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1337 size_t curNumRows
= m_data
.GetCount();
1339 if ( pos
>= curNumRows
)
1342 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1343 "Pos value is invalid for present table with %d rows",
1344 pos
, numRows
, curNumRows
);
1345 wxFAIL_MSG( wxT(errmsg
) );
1349 if ( numRows
> curNumRows
- pos
)
1351 numRows
= curNumRows
- pos
;
1354 if ( numRows
>= curNumRows
)
1356 m_data
.Empty(); // don't release memory just yet
1360 for ( n
= 0; n
< numRows
; n
++ )
1362 m_data
.Remove( pos
);
1365 UpdateAttrRows( pos
, -((int)numRows
) );
1368 wxGridTableMessage
msg( this,
1369 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1373 GetView()->ProcessTableMessage( msg
);
1379 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1383 size_t curNumRows
= m_data
.GetCount();
1384 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1386 if ( pos
>= curNumCols
)
1388 return AppendCols( numCols
);
1391 for ( row
= 0; row
< curNumRows
; row
++ )
1393 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1395 m_data
[row
].Insert( wxEmptyString
, col
);
1398 UpdateAttrCols( pos
, numCols
);
1401 wxGridTableMessage
msg( this,
1402 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1406 GetView()->ProcessTableMessage( msg
);
1412 bool wxGridStringTable::AppendCols( size_t numCols
)
1416 size_t curNumRows
= m_data
.GetCount();
1419 // TODO: something better than this ?
1421 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1422 "Call AppendRows() first") );
1426 for ( row
= 0; row
< curNumRows
; row
++ )
1428 for ( n
= 0; n
< numCols
; n
++ )
1430 m_data
[row
].Add( wxEmptyString
);
1436 wxGridTableMessage
msg( this,
1437 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1440 GetView()->ProcessTableMessage( msg
);
1446 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1450 size_t curNumRows
= m_data
.GetCount();
1451 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1453 if ( pos
>= curNumCols
)
1456 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1457 "Pos value is invalid for present table with %d cols",
1458 pos
, numCols
, curNumCols
);
1459 wxFAIL_MSG( wxT( errmsg
) );
1463 if ( numCols
> curNumCols
- pos
)
1465 numCols
= curNumCols
- pos
;
1468 for ( row
= 0; row
< curNumRows
; row
++ )
1470 if ( numCols
>= curNumCols
)
1472 m_data
[row
].Clear();
1476 for ( n
= 0; n
< numCols
; n
++ )
1478 m_data
[row
].Remove( pos
);
1482 UpdateAttrCols( pos
, -((int)numCols
) );
1485 wxGridTableMessage
msg( this,
1486 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1490 GetView()->ProcessTableMessage( msg
);
1496 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1498 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1500 // using default label
1502 return wxGridTableBase::GetRowLabelValue( row
);
1506 return m_rowLabels
[ row
];
1510 wxString
wxGridStringTable::GetColLabelValue( int col
)
1512 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1514 // using default label
1516 return wxGridTableBase::GetColLabelValue( col
);
1520 return m_colLabels
[ col
];
1524 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1526 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1528 int n
= m_rowLabels
.GetCount();
1530 for ( i
= n
; i
<= row
; i
++ )
1532 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1536 m_rowLabels
[row
] = value
;
1539 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1541 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1543 int n
= m_colLabels
.GetCount();
1545 for ( i
= n
; i
<= col
; i
++ )
1547 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1551 m_colLabels
[col
] = value
;
1556 //////////////////////////////////////////////////////////////////////
1557 //////////////////////////////////////////////////////////////////////
1559 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1561 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1562 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1563 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1564 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1567 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1569 const wxPoint
&pos
, const wxSize
&size
)
1570 : wxWindow( parent
, id
, pos
, size
)
1575 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1579 // NO - don't do this because it will set both the x and y origin
1580 // coords to match the parent scrolled window and we just want to
1581 // set the y coord - MB
1583 // m_owner->PrepareDC( dc );
1586 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1587 dc
.SetDeviceOrigin( 0, -y
);
1589 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1590 m_owner
->DrawRowLabels( dc
);
1594 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1596 m_owner
->ProcessRowLabelMouseEvent( event
);
1600 // This seems to be required for wxMotif otherwise the mouse
1601 // cursor must be in the cell edit control to get key events
1603 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1605 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1610 //////////////////////////////////////////////////////////////////////
1612 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1614 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1615 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1616 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1617 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1620 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1622 const wxPoint
&pos
, const wxSize
&size
)
1623 : wxWindow( parent
, id
, pos
, size
)
1628 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1632 // NO - don't do this because it will set both the x and y origin
1633 // coords to match the parent scrolled window and we just want to
1634 // set the x coord - MB
1636 // m_owner->PrepareDC( dc );
1639 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1640 dc
.SetDeviceOrigin( -x
, 0 );
1642 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1643 m_owner
->DrawColLabels( dc
);
1647 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1649 m_owner
->ProcessColLabelMouseEvent( event
);
1653 // This seems to be required for wxMotif otherwise the mouse
1654 // cursor must be in the cell edit control to get key events
1656 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1658 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1663 //////////////////////////////////////////////////////////////////////
1665 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1667 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1668 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1669 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1670 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1673 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1675 const wxPoint
&pos
, const wxSize
&size
)
1676 : wxWindow( parent
, id
, pos
, size
)
1681 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1685 int client_height
= 0;
1686 int client_width
= 0;
1687 GetClientSize( &client_width
, &client_height
);
1689 dc
.SetPen( *wxBLACK_PEN
);
1690 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1691 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1693 dc
.SetPen( *wxWHITE_PEN
);
1694 dc
.DrawLine( 0, 0, client_width
, 0 );
1695 dc
.DrawLine( 0, 0, 0, client_height
);
1699 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1701 m_owner
->ProcessCornerLabelMouseEvent( 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 wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1710 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1715 //////////////////////////////////////////////////////////////////////
1717 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1719 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1720 EVT_PAINT( wxGridWindow::OnPaint
)
1721 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1722 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1723 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1726 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1727 wxGridRowLabelWindow
*rowLblWin
,
1728 wxGridColLabelWindow
*colLblWin
,
1729 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1730 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1733 m_rowLabelWin
= rowLblWin
;
1734 m_colLabelWin
= colLblWin
;
1735 SetBackgroundColour( "WHITE" );
1739 wxGridWindow::~wxGridWindow()
1744 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1746 wxPaintDC
dc( this );
1747 m_owner
->PrepareDC( dc
);
1748 wxRegion reg
= GetUpdateRegion();
1749 m_owner
->CalcCellsExposed( reg
);
1750 m_owner
->DrawGridCellArea( dc
);
1751 #if WXGRID_DRAW_LINES
1752 m_owner
->DrawAllGridLines( dc
, reg
);
1757 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1759 wxPanel::ScrollWindow( dx
, dy
, rect
);
1760 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1761 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1765 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1767 m_owner
->ProcessGridCellMouseEvent( event
);
1771 // This seems to be required for wxMotif otherwise the mouse
1772 // cursor must be in the cell edit control to get key events
1774 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1776 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1779 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1785 //////////////////////////////////////////////////////////////////////
1788 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1790 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1791 EVT_PAINT( wxGrid::OnPaint
)
1792 EVT_SIZE( wxGrid::OnSize
)
1793 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1794 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1797 wxGrid::wxGrid( wxWindow
*parent
,
1802 const wxString
& name
)
1803 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1812 m_defaultCellAttr
->SafeDecRef();
1814 #ifdef DEBUG_ATTR_CACHE
1815 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1816 wxPrintf(_T("wxGrid attribute cache statistics: "
1817 "total: %u, hits: %u (%u%%)\n"),
1818 total
, gs_nAttrCacheHits
,
1819 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1828 // ----- internal init and update functions
1831 void wxGrid::Create()
1833 m_created
= FALSE
; // set to TRUE by CreateGrid
1834 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
1836 m_table
= (wxGridTableBase
*) NULL
;
1839 m_cellEditCtrlEnabled
= FALSE
;
1841 m_defaultCellAttr
= new wxGridCellAttr
;
1842 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1843 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1848 m_currentCellCoords
= wxGridNoCellCoords
;
1850 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1851 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1853 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1858 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1863 m_colLabelWin
= new wxGridColLabelWindow( this,
1868 m_gridWin
= new wxGridWindow( this,
1875 SetTargetWindow( m_gridWin
);
1879 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1883 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1888 m_numRows
= numRows
;
1889 m_numCols
= numCols
;
1891 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1892 m_table
->SetView( this );
1901 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1905 // RD: Actually, this should probably be allowed. I think it would be
1906 // nice to be able to switch multiple Tables in and out of a single
1907 // View at runtime. Is there anything in the implmentation that would
1910 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1915 m_numRows
= table
->GetNumberRows();
1916 m_numCols
= table
->GetNumberCols();
1919 m_table
->SetView( this );
1934 if ( m_numRows
<= 0 )
1935 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1937 if ( m_numCols
<= 0 )
1938 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1940 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1941 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1943 if ( m_rowLabelWin
)
1945 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1949 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1952 m_labelTextColour
= wxColour( _T("BLACK") );
1955 m_attrCache
.row
= -1;
1957 // TODO: something better than this ?
1959 m_labelFont
= this->GetFont();
1960 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1962 m_rowLabelHorizAlign
= wxLEFT
;
1963 m_rowLabelVertAlign
= wxCENTRE
;
1965 m_colLabelHorizAlign
= wxCENTRE
;
1966 m_colLabelVertAlign
= wxTOP
;
1968 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1969 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1971 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1972 m_defaultRowHeight
+= 8;
1974 m_defaultRowHeight
+= 4;
1977 m_rowHeights
.Alloc( m_numRows
);
1978 m_rowBottoms
.Alloc( m_numRows
);
1980 for ( i
= 0; i
< m_numRows
; i
++ )
1982 m_rowHeights
.Add( m_defaultRowHeight
);
1983 rowBottom
+= m_defaultRowHeight
;
1984 m_rowBottoms
.Add( rowBottom
);
1987 m_colWidths
.Alloc( m_numCols
);
1988 m_colRights
.Alloc( m_numCols
);
1990 for ( i
= 0; i
< m_numCols
; i
++ )
1992 m_colWidths
.Add( m_defaultColWidth
);
1993 colRight
+= m_defaultColWidth
;
1994 m_colRights
.Add( colRight
);
1997 // Set default cell attributes
1998 m_defaultCellAttr
->SetFont(GetFont());
1999 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2000 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2001 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2002 m_defaultCellAttr
->SetTextColour(
2003 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2004 m_defaultCellAttr
->SetBackgroundColour(
2005 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2008 m_gridLineColour
= wxColour( 128, 128, 255 );
2009 m_gridLinesEnabled
= TRUE
;
2011 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2012 m_winCapture
= (wxWindow
*)NULL
;
2014 m_dragRowOrCol
= -1;
2015 m_isDragging
= FALSE
;
2016 m_startDragPos
= wxDefaultPosition
;
2018 m_waitForSlowClick
= FALSE
;
2020 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2021 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2023 m_currentCellCoords
= wxGridNoCellCoords
;
2025 m_selectedTopLeft
= wxGridNoCellCoords
;
2026 m_selectedBottomRight
= wxGridNoCellCoords
;
2027 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2028 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2030 m_editable
= TRUE
; // default for whole grid
2032 m_inOnKeyDown
= FALSE
;
2038 void wxGrid::CalcDimensions()
2041 GetClientSize( &cw
, &ch
);
2043 if ( m_numRows
> 0 && m_numCols
> 0 )
2045 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2046 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2048 // TODO: restore the scroll position that we had before sizing
2051 GetViewStart( &x
, &y
);
2052 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2053 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2059 void wxGrid::CalcWindowSizes()
2062 GetClientSize( &cw
, &ch
);
2064 if ( m_cornerLabelWin
->IsShown() )
2065 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2067 if ( m_colLabelWin
->IsShown() )
2068 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2070 if ( m_rowLabelWin
->IsShown() )
2071 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2073 if ( m_gridWin
->IsShown() )
2074 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2078 // this is called when the grid table sends a message to say that it
2079 // has been redimensioned
2081 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2085 switch ( msg
.GetId() )
2087 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2089 size_t pos
= msg
.GetCommandInt();
2090 int numRows
= msg
.GetCommandInt2();
2091 for ( i
= 0; i
< numRows
; i
++ )
2093 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2094 m_rowBottoms
.Insert( 0, pos
);
2096 m_numRows
+= numRows
;
2099 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2101 for ( i
= pos
; i
< m_numRows
; i
++ )
2103 bottom
+= m_rowHeights
[i
];
2104 m_rowBottoms
[i
] = bottom
;
2110 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2112 int numRows
= msg
.GetCommandInt();
2113 for ( i
= 0; i
< numRows
; i
++ )
2115 m_rowHeights
.Add( m_defaultRowHeight
);
2116 m_rowBottoms
.Add( 0 );
2119 int oldNumRows
= m_numRows
;
2120 m_numRows
+= numRows
;
2123 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2125 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2127 bottom
+= m_rowHeights
[i
];
2128 m_rowBottoms
[i
] = bottom
;
2134 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2136 size_t pos
= msg
.GetCommandInt();
2137 int numRows
= msg
.GetCommandInt2();
2138 for ( i
= 0; i
< numRows
; i
++ )
2140 m_rowHeights
.Remove( pos
);
2141 m_rowBottoms
.Remove( pos
);
2143 m_numRows
-= numRows
;
2148 m_colWidths
.Clear();
2149 m_colRights
.Clear();
2150 m_currentCellCoords
= wxGridNoCellCoords
;
2154 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2155 m_currentCellCoords
.Set( 0, 0 );
2158 for ( i
= 0; i
< m_numRows
; i
++ )
2160 h
+= m_rowHeights
[i
];
2161 m_rowBottoms
[i
] = h
;
2169 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2171 size_t pos
= msg
.GetCommandInt();
2172 int numCols
= msg
.GetCommandInt2();
2173 for ( i
= 0; i
< numCols
; i
++ )
2175 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2176 m_colRights
.Insert( 0, pos
);
2178 m_numCols
+= numCols
;
2181 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2183 for ( i
= pos
; i
< m_numCols
; i
++ )
2185 right
+= m_colWidths
[i
];
2186 m_colRights
[i
] = right
;
2192 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2194 int numCols
= msg
.GetCommandInt();
2195 for ( i
= 0; i
< numCols
; i
++ )
2197 m_colWidths
.Add( m_defaultColWidth
);
2198 m_colRights
.Add( 0 );
2201 int oldNumCols
= m_numCols
;
2202 m_numCols
+= numCols
;
2205 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2207 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2209 right
+= m_colWidths
[i
];
2210 m_colRights
[i
] = right
;
2216 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2218 size_t pos
= msg
.GetCommandInt();
2219 int numCols
= msg
.GetCommandInt2();
2220 for ( i
= 0; i
< numCols
; i
++ )
2222 m_colWidths
.Remove( pos
);
2223 m_colRights
.Remove( pos
);
2225 m_numCols
-= numCols
;
2229 #if 0 // leave the row alone here so that AppendCols will work subsequently
2231 m_rowHeights
.Clear();
2232 m_rowBottoms
.Clear();
2234 m_currentCellCoords
= wxGridNoCellCoords
;
2238 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2239 m_currentCellCoords
.Set( 0, 0 );
2242 for ( i
= 0; i
< m_numCols
; i
++ )
2244 w
+= m_colWidths
[i
];
2257 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2259 wxRegionIterator
iter( reg
);
2262 m_rowLabelsExposed
.Empty();
2269 // TODO: remove this when we can...
2270 // There is a bug in wxMotif that gives garbage update
2271 // rectangles if you jump-scroll a long way by clicking the
2272 // scrollbar with middle button. This is a work-around
2274 #if defined(__WXMOTIF__)
2276 m_gridWin
->GetClientSize( &cw
, &ch
);
2277 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2278 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2281 // logical bounds of update region
2284 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2285 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2287 // find the row labels within these bounds
2291 for ( row
= 0; row
< m_numRows
; row
++ )
2293 if ( m_rowBottoms
[row
] < top
) continue;
2295 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2296 if ( rowTop
> bottom
) break;
2298 m_rowLabelsExposed
.Add( row
);
2306 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2308 wxRegionIterator
iter( reg
);
2311 m_colLabelsExposed
.Empty();
2318 // TODO: remove this when we can...
2319 // There is a bug in wxMotif that gives garbage update
2320 // rectangles if you jump-scroll a long way by clicking the
2321 // scrollbar with middle button. This is a work-around
2323 #if defined(__WXMOTIF__)
2325 m_gridWin
->GetClientSize( &cw
, &ch
);
2326 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2327 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2330 // logical bounds of update region
2333 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2334 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2336 // find the cells within these bounds
2340 for ( col
= 0; col
< m_numCols
; col
++ )
2342 if ( m_colRights
[col
] < left
) continue;
2344 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2345 if ( colLeft
> right
) break;
2347 m_colLabelsExposed
.Add( col
);
2355 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2357 wxRegionIterator
iter( reg
);
2360 m_cellsExposed
.Empty();
2361 m_rowsExposed
.Empty();
2362 m_colsExposed
.Empty();
2364 int left
, top
, right
, bottom
;
2369 // TODO: remove this when we can...
2370 // There is a bug in wxMotif that gives garbage update
2371 // rectangles if you jump-scroll a long way by clicking the
2372 // scrollbar with middle button. This is a work-around
2374 #if defined(__WXMOTIF__)
2376 m_gridWin
->GetClientSize( &cw
, &ch
);
2377 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2378 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2379 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2380 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2383 // logical bounds of update region
2385 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2386 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2388 // find the cells within these bounds
2391 int colLeft
, rowTop
;
2392 for ( row
= 0; row
< m_numRows
; row
++ )
2394 if ( m_rowBottoms
[row
] <= top
) continue;
2396 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2397 if ( rowTop
> bottom
) break;
2399 m_rowsExposed
.Add( row
);
2401 for ( col
= 0; col
< m_numCols
; col
++ )
2403 if ( m_colRights
[col
] <= left
) continue;
2405 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2406 if ( colLeft
> right
) break;
2408 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2409 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2418 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2421 wxPoint
pos( event
.GetPosition() );
2422 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2424 if ( event
.Dragging() )
2426 m_isDragging
= TRUE
;
2428 if ( event
.LeftIsDown() )
2430 switch( m_cursorMode
)
2432 case WXGRID_CURSOR_RESIZE_ROW
:
2434 int cw
, ch
, left
, dummy
;
2435 m_gridWin
->GetClientSize( &cw
, &ch
);
2436 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2438 wxClientDC
dc( m_gridWin
);
2441 m_rowBottoms
[m_dragRowOrCol
] -
2442 m_rowHeights
[m_dragRowOrCol
] +
2443 WXGRID_MIN_ROW_HEIGHT
);
2444 dc
.SetLogicalFunction(wxINVERT
);
2445 if ( m_dragLastPos
>= 0 )
2447 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2449 dc
.DrawLine( left
, y
, left
+cw
, y
);
2454 case WXGRID_CURSOR_SELECT_ROW
:
2455 if ( (row
= YToRow( y
)) >= 0 &&
2456 !IsInSelection( row
, 0 ) )
2458 SelectRow( row
, TRUE
);
2461 // default label to suppress warnings about "enumeration value
2462 // 'xxx' not handled in switch
2470 m_isDragging
= FALSE
;
2473 // ------------ Entering or leaving the window
2475 if ( event
.Entering() || event
.Leaving() )
2477 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2481 // ------------ Left button pressed
2483 else if ( event
.LeftDown() )
2485 // don't send a label click event for a hit on the
2486 // edge of the row label - this is probably the user
2487 // wanting to resize the row
2489 if ( YToEdgeOfRow(y
) < 0 )
2493 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2495 SelectRow( row
, event
.ShiftDown() );
2496 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2501 // starting to drag-resize a row
2503 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2508 // ------------ Left double click
2510 else if (event
.LeftDClick() )
2512 if ( YToEdgeOfRow(y
) < 0 )
2515 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2520 // ------------ Left button released
2522 else if ( event
.LeftUp() )
2524 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2526 DoEndDragResizeRow();
2528 // Note: we are ending the event *after* doing
2529 // default processing in this case
2531 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2534 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2539 // ------------ Right button down
2541 else if ( event
.RightDown() )
2544 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2546 // no default action at the moment
2551 // ------------ Right double click
2553 else if ( event
.RightDClick() )
2556 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2558 // no default action at the moment
2563 // ------------ No buttons down and mouse moving
2565 else if ( event
.Moving() )
2567 m_dragRowOrCol
= YToEdgeOfRow( y
);
2568 if ( m_dragRowOrCol
>= 0 )
2570 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2572 // don't capture the mouse yet
2573 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2576 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2578 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2584 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2587 wxPoint
pos( event
.GetPosition() );
2588 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2590 if ( event
.Dragging() )
2592 m_isDragging
= TRUE
;
2594 if ( event
.LeftIsDown() )
2596 switch( m_cursorMode
)
2598 case WXGRID_CURSOR_RESIZE_COL
:
2600 int cw
, ch
, dummy
, top
;
2601 m_gridWin
->GetClientSize( &cw
, &ch
);
2602 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2604 wxClientDC
dc( m_gridWin
);
2607 m_colRights
[m_dragRowOrCol
] -
2608 m_colWidths
[m_dragRowOrCol
] +
2609 WXGRID_MIN_COL_WIDTH
);
2610 dc
.SetLogicalFunction(wxINVERT
);
2611 if ( m_dragLastPos
>= 0 )
2613 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2615 dc
.DrawLine( x
, top
, x
, top
+ch
);
2620 case WXGRID_CURSOR_SELECT_COL
:
2621 if ( (col
= XToCol( x
)) >= 0 &&
2622 !IsInSelection( 0, col
) )
2624 SelectCol( col
, TRUE
);
2627 // default label to suppress warnings about "enumeration value
2628 // 'xxx' not handled in switch
2636 m_isDragging
= FALSE
;
2639 // ------------ Entering or leaving the window
2641 if ( event
.Entering() || event
.Leaving() )
2643 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2647 // ------------ Left button pressed
2649 else if ( event
.LeftDown() )
2651 // don't send a label click event for a hit on the
2652 // edge of the col label - this is probably the user
2653 // wanting to resize the col
2655 if ( XToEdgeOfCol(x
) < 0 )
2659 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2661 SelectCol( col
, event
.ShiftDown() );
2662 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2667 // starting to drag-resize a col
2669 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2674 // ------------ Left double click
2676 if ( event
.LeftDClick() )
2678 if ( XToEdgeOfCol(x
) < 0 )
2681 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2686 // ------------ Left button released
2688 else if ( event
.LeftUp() )
2690 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2692 DoEndDragResizeCol();
2694 // Note: we are ending the event *after* doing
2695 // default processing in this case
2697 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2700 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2705 // ------------ Right button down
2707 else if ( event
.RightDown() )
2710 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2712 // no default action at the moment
2717 // ------------ Right double click
2719 else if ( event
.RightDClick() )
2722 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2724 // no default action at the moment
2729 // ------------ No buttons down and mouse moving
2731 else if ( event
.Moving() )
2733 m_dragRowOrCol
= XToEdgeOfCol( x
);
2734 if ( m_dragRowOrCol
>= 0 )
2736 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2738 // don't capture the cursor yet
2739 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2742 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2744 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2750 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2752 if ( event
.LeftDown() )
2754 // indicate corner label by having both row and
2757 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2763 else if ( event
.LeftDClick() )
2765 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2768 else if ( event
.RightDown() )
2770 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2772 // no default action at the moment
2776 else if ( event
.RightDClick() )
2778 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2780 // no default action at the moment
2785 void wxGrid::ChangeCursorMode(CursorMode mode
,
2790 static const wxChar
*cursorModes
[] =
2799 wxLogTrace(_T("grid"),
2800 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2801 win
== m_colLabelWin
? _T("colLabelWin")
2802 : win
? _T("rowLabelWin")
2804 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2805 #endif // __WXDEBUG__
2807 if ( mode
== m_cursorMode
)
2812 // by default use the grid itself
2818 m_winCapture
->ReleaseMouse();
2819 m_winCapture
= (wxWindow
*)NULL
;
2822 m_cursorMode
= mode
;
2824 switch ( m_cursorMode
)
2826 case WXGRID_CURSOR_RESIZE_ROW
:
2827 win
->SetCursor( m_rowResizeCursor
);
2830 case WXGRID_CURSOR_RESIZE_COL
:
2831 win
->SetCursor( m_colResizeCursor
);
2835 win
->SetCursor( *wxSTANDARD_CURSOR
);
2838 // we need to capture mouse when resizing
2839 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2840 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2842 if ( captureMouse
&& resize
)
2844 win
->CaptureMouse();
2849 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2852 wxPoint
pos( event
.GetPosition() );
2853 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2855 wxGridCellCoords coords
;
2856 XYToCell( x
, y
, coords
);
2858 if ( event
.Dragging() )
2860 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
2862 // Don't start doing anything until the mouse has been drug at
2863 // least 3 pixels in any direction...
2864 if (! m_isDragging
) {
2865 if (m_startDragPos
== wxDefaultPosition
) {
2866 m_startDragPos
= pos
;
2869 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
2873 m_isDragging
= TRUE
;
2874 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2876 // Hide the edit control, so it
2877 // won't interfer with drag-shrinking.
2878 if ( IsCellEditControlEnabled() )
2879 HideCellEditControl();
2881 // Have we captured the mouse yet?
2882 if (! m_winCapture
) {
2883 m_winCapture
= m_gridWin
;
2884 m_winCapture
->CaptureMouse();
2887 if ( coords
!= wxGridNoCellCoords
)
2889 if ( !IsSelection() )
2891 SelectBlock( coords
, coords
);
2895 SelectBlock( m_currentCellCoords
, coords
);
2898 if (! IsVisible(coords
)) {
2899 MakeCellVisible(coords
);
2900 // TODO: need to introduce a delay or something here. The
2901 // scrolling is way to fast, at least on MSW.
2905 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2907 int cw
, ch
, left
, dummy
;
2908 m_gridWin
->GetClientSize( &cw
, &ch
);
2909 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2911 wxClientDC
dc( m_gridWin
);
2914 m_rowBottoms
[m_dragRowOrCol
] -
2915 m_rowHeights
[m_dragRowOrCol
] +
2916 WXGRID_MIN_ROW_HEIGHT
);
2917 dc
.SetLogicalFunction(wxINVERT
);
2918 if ( m_dragLastPos
>= 0 )
2920 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2922 dc
.DrawLine( left
, y
, left
+cw
, y
);
2925 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2927 int cw
, ch
, dummy
, top
;
2928 m_gridWin
->GetClientSize( &cw
, &ch
);
2929 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2931 wxClientDC
dc( m_gridWin
);
2934 m_colRights
[m_dragRowOrCol
] -
2935 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
2936 dc
.SetLogicalFunction(wxINVERT
);
2937 if ( m_dragLastPos
>= 0 )
2939 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2941 dc
.DrawLine( x
, top
, x
, top
+ch
);
2948 m_isDragging
= FALSE
;
2949 m_startDragPos
= wxDefaultPosition
;
2952 if ( coords
!= wxGridNoCellCoords
)
2954 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2955 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2958 if ( event
.Entering() || event
.Leaving() )
2960 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2961 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2966 // ------------ Left button pressed
2968 if ( event
.LeftDown() )
2970 DisableCellEditControl();
2971 if ( event
.ShiftDown() )
2973 SelectBlock( m_currentCellCoords
, coords
);
2975 else if ( XToEdgeOfCol(x
) < 0 &&
2976 YToEdgeOfRow(y
) < 0 )
2978 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
2983 MakeCellVisible( coords
);
2985 // if this is the second click on this cell then start
2987 if ( m_waitForSlowClick
&&
2988 (coords
== m_currentCellCoords
) &&
2989 CanEnableCellControl())
2991 EnableCellEditControl();
2992 m_waitForSlowClick
= FALSE
;
2996 SetCurrentCell( coords
);
2997 m_waitForSlowClick
= TRUE
;
3004 // ------------ Left double click
3006 else if ( event
.LeftDClick() )
3008 DisableCellEditControl();
3009 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3011 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3019 // ------------ Left button released
3021 else if ( event
.LeftUp() )
3023 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3025 if ( IsSelection() )
3028 m_winCapture
->ReleaseMouse();
3029 m_winCapture
= NULL
;
3031 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3034 // Show the edit control, if it has been hidden for
3036 ShowCellEditControl();
3038 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3040 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3041 DoEndDragResizeRow();
3043 // Note: we are ending the event *after* doing
3044 // default processing in this case
3046 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3048 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3050 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3051 DoEndDragResizeCol();
3053 // Note: we are ending the event *after* doing
3054 // default processing in this case
3056 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3063 // ------------ Right button down
3065 else if ( event
.RightDown() )
3067 DisableCellEditControl();
3068 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3073 // no default action at the moment
3078 // ------------ Right double click
3080 else if ( event
.RightDClick() )
3082 DisableCellEditControl();
3083 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3088 // no default action at the moment
3092 // ------------ Moving and no button action
3094 else if ( event
.Moving() && !event
.IsButton() )
3096 int dragRow
= YToEdgeOfRow( y
);
3097 int dragCol
= XToEdgeOfCol( x
);
3099 // Dragging on the corner of a cell to resize in both
3100 // directions is not implemented yet...
3102 if ( dragRow
>= 0 && dragCol
>= 0 )
3104 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3110 m_dragRowOrCol
= dragRow
;
3112 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3114 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3122 m_dragRowOrCol
= dragCol
;
3124 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3126 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3132 // Neither on a row or col edge
3134 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3136 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3143 void wxGrid::DoEndDragResizeRow()
3145 if ( m_dragLastPos
>= 0 )
3147 // erase the last line and resize the row
3149 int cw
, ch
, left
, dummy
;
3150 m_gridWin
->GetClientSize( &cw
, &ch
);
3151 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3153 wxClientDC
dc( m_gridWin
);
3155 dc
.SetLogicalFunction( wxINVERT
);
3156 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3157 HideCellEditControl();
3159 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3160 SetRowSize( m_dragRowOrCol
,
3161 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3163 if ( !GetBatchCount() )
3165 // Only needed to get the correct rect.y:
3166 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3168 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3169 rect
.width
= m_rowLabelWidth
;
3170 rect
.height
= ch
- rect
.y
;
3171 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3173 m_gridWin
->Refresh( FALSE
, &rect
);
3176 ShowCellEditControl();
3181 void wxGrid::DoEndDragResizeCol()
3183 if ( m_dragLastPos
>= 0 )
3185 // erase the last line and resize the col
3187 int cw
, ch
, dummy
, top
;
3188 m_gridWin
->GetClientSize( &cw
, &ch
);
3189 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3191 wxClientDC
dc( m_gridWin
);
3193 dc
.SetLogicalFunction( wxINVERT
);
3194 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3195 HideCellEditControl();
3197 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3198 SetColSize( m_dragRowOrCol
,
3199 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3201 if ( !GetBatchCount() )
3203 // Only needed to get the correct rect.x:
3204 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3206 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3207 rect
.width
= cw
- rect
.x
;
3208 rect
.height
= m_colLabelHeight
;
3209 m_colLabelWin
->Refresh( TRUE
, &rect
);
3211 m_gridWin
->Refresh( FALSE
, &rect
);
3214 ShowCellEditControl();
3221 // ------ interaction with data model
3223 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3225 switch ( msg
.GetId() )
3227 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3228 return GetModelValues();
3230 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3231 return SetModelValues();
3233 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3234 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3235 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3236 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3237 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3238 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3239 return Redimension( msg
);
3248 // The behaviour of this function depends on the grid table class
3249 // Clear() function. For the default wxGridStringTable class the
3250 // behavious is to replace all cell contents with wxEmptyString but
3251 // not to change the number of rows or cols.
3253 void wxGrid::ClearGrid()
3258 SetEditControlValue();
3259 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3264 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3266 // TODO: something with updateLabels flag
3270 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3276 if (IsCellEditControlEnabled())
3277 DisableCellEditControl();
3279 bool ok
= m_table
->InsertRows( pos
, numRows
);
3281 // the table will have sent the results of the insert row
3282 // operation to this view object as a grid table message
3286 if ( m_numCols
== 0 )
3288 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3290 // TODO: perhaps instead of appending the default number of cols
3291 // we should remember what the last non-zero number of cols was ?
3295 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3297 // if we have just inserted cols into an empty grid the current
3298 // cell will be undefined...
3300 SetCurrentCell( 0, 0 );
3304 if ( !GetBatchCount() ) Refresh();
3307 SetEditControlValue();
3317 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3319 // TODO: something with updateLabels flag
3323 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3327 if ( m_table
&& m_table
->AppendRows( numRows
) )
3329 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3331 // if we have just inserted cols into an empty grid the current
3332 // cell will be undefined...
3334 SetCurrentCell( 0, 0 );
3337 // the table will have sent the results of the append row
3338 // operation to this view object as a grid table message
3341 if ( !GetBatchCount() ) Refresh();
3351 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3353 // TODO: something with updateLabels flag
3357 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3363 if (IsCellEditControlEnabled())
3364 DisableCellEditControl();
3366 if (m_table
->DeleteRows( pos
, numRows
))
3369 // the table will have sent the results of the delete row
3370 // operation to this view object as a grid table message
3373 if ( !GetBatchCount() ) Refresh();
3381 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3383 // TODO: something with updateLabels flag
3387 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3393 if (IsCellEditControlEnabled())
3394 DisableCellEditControl();
3396 bool ok
= m_table
->InsertCols( pos
, numCols
);
3398 // the table will have sent the results of the insert col
3399 // operation to this view object as a grid table message
3403 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3405 // if we have just inserted cols into an empty grid the current
3406 // cell will be undefined...
3408 SetCurrentCell( 0, 0 );
3412 if ( !GetBatchCount() ) Refresh();
3415 SetEditControlValue();
3425 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3427 // TODO: something with updateLabels flag
3431 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3435 if ( m_table
&& m_table
->AppendCols( numCols
) )
3437 // the table will have sent the results of the append col
3438 // operation to this view object as a grid table message
3440 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3442 // if we have just inserted cols into an empty grid the current
3443 // cell will be undefined...
3445 SetCurrentCell( 0, 0 );
3449 if ( !GetBatchCount() ) Refresh();
3459 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3461 // TODO: something with updateLabels flag
3465 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3471 if (IsCellEditControlEnabled())
3472 DisableCellEditControl();
3474 if ( m_table
->DeleteCols( pos
, numCols
) )
3476 // the table will have sent the results of the delete col
3477 // operation to this view object as a grid table message
3480 if ( !GetBatchCount() ) Refresh();
3490 // ----- event handlers
3493 // Generate a grid event based on a mouse event and
3494 // return the result of ProcessEvent()
3496 bool wxGrid::SendEvent( const wxEventType type
,
3498 wxMouseEvent
& mouseEv
)
3500 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3502 int rowOrCol
= (row
== -1 ? col
: row
);
3504 wxGridSizeEvent
gridEvt( GetId(),
3508 mouseEv
.GetX(), mouseEv
.GetY(),
3509 mouseEv
.ControlDown(),
3510 mouseEv
.ShiftDown(),
3512 mouseEv
.MetaDown() );
3514 return GetEventHandler()->ProcessEvent(gridEvt
);
3516 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3518 wxGridRangeSelectEvent
gridEvt( GetId(),
3522 m_selectedBottomRight
,
3523 mouseEv
.ControlDown(),
3524 mouseEv
.ShiftDown(),
3526 mouseEv
.MetaDown() );
3528 return GetEventHandler()->ProcessEvent(gridEvt
);
3532 wxGridEvent
gridEvt( GetId(),
3536 mouseEv
.GetX(), mouseEv
.GetY(),
3537 mouseEv
.ControlDown(),
3538 mouseEv
.ShiftDown(),
3540 mouseEv
.MetaDown() );
3542 return GetEventHandler()->ProcessEvent(gridEvt
);
3547 // Generate a grid event of specified type and return the result
3548 // of ProcessEvent().
3550 bool wxGrid::SendEvent( const wxEventType type
,
3553 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3555 int rowOrCol
= (row
== -1 ? col
: row
);
3557 wxGridSizeEvent
gridEvt( GetId(),
3562 return GetEventHandler()->ProcessEvent(gridEvt
);
3566 wxGridEvent
gridEvt( GetId(),
3571 return GetEventHandler()->ProcessEvent(gridEvt
);
3576 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3578 wxPaintDC
dc( this );
3580 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3581 m_numRows
&& m_numCols
)
3583 m_currentCellCoords
.Set(0, 0);
3584 SetEditControlValue();
3585 ShowCellEditControl();
3592 // This is just here to make sure that CalcDimensions gets called when
3593 // the grid view is resized... then the size event is skipped to allow
3594 // the box sizers to handle everything
3596 void wxGrid::OnSize( wxSizeEvent
& event
)
3603 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3605 if ( m_inOnKeyDown
)
3607 // shouldn't be here - we are going round in circles...
3609 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3612 m_inOnKeyDown
= TRUE
;
3614 // propagate the event up and see if it gets processed
3616 wxWindow
*parent
= GetParent();
3617 wxKeyEvent
keyEvt( event
);
3618 keyEvt
.SetEventObject( parent
);
3620 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3623 // TODO: Should also support Shift-cursor keys for
3624 // extending the selection. Maybe add a flag to
3625 // MoveCursorXXX() and MoveCursorXXXBlock() and
3626 // just send event.ShiftDown().
3628 // try local handlers
3630 switch ( event
.KeyCode() )
3633 if ( event
.ControlDown() )
3635 MoveCursorUpBlock();
3644 if ( event
.ControlDown() )
3646 MoveCursorDownBlock();
3655 if ( event
.ControlDown() )
3657 MoveCursorLeftBlock();
3666 if ( event
.ControlDown() )
3668 MoveCursorRightBlock();
3677 if ( event
.ControlDown() )
3679 event
.Skip(); // to let the edit control have the return
3688 if (event
.ShiftDown())
3695 if ( event
.ControlDown() )
3697 MakeCellVisible( 0, 0 );
3698 SetCurrentCell( 0, 0 );
3707 if ( event
.ControlDown() )
3709 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3710 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3726 // We don't want these keys to trigger the edit control, any others?
3735 if ( !IsEditable() )
3740 // Otherwise fall through to default
3743 // now try the cell edit control
3745 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
3747 EnableCellEditControl();
3748 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3749 attr
->GetEditor()->StartingKey(event
);
3756 m_inOnKeyDown
= FALSE
;
3760 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3766 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3768 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3770 // the event has been intercepted - do nothing
3775 m_currentCellCoords
!= wxGridNoCellCoords
)
3777 HideCellEditControl();
3778 SaveEditControlValue();
3779 DisableCellEditControl();
3781 // Clear the old current cell highlight
3782 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3784 // Otherwise refresh redraws the highlight!
3785 m_currentCellCoords
= coords
;
3787 m_gridWin
->Refresh( FALSE
, &r
);
3790 m_currentCellCoords
= coords
;
3792 SetEditControlValue();
3796 wxClientDC
dc(m_gridWin
);
3799 wxGridCellAttr
* attr
= GetCellAttr(coords
);
3800 DrawCellHighlight(dc
, attr
);
3803 if ( IsSelection() )
3805 wxRect
r( SelectionToDeviceRect() );
3807 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3814 // ------ functions to get/send data (see also public functions)
3817 bool wxGrid::GetModelValues()
3821 // all we need to do is repaint the grid
3823 m_gridWin
->Refresh();
3831 bool wxGrid::SetModelValues()
3837 for ( row
= 0; row
< m_numRows
; row
++ )
3839 for ( col
= 0; col
< m_numCols
; col
++ )
3841 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3853 // Note - this function only draws cells that are in the list of
3854 // exposed cells (usually set from the update region by
3855 // CalcExposedCells)
3857 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3859 if ( !m_numRows
|| !m_numCols
) return;
3862 size_t numCells
= m_cellsExposed
.GetCount();
3864 for ( i
= 0; i
< numCells
; i
++ )
3866 DrawCell( dc
, m_cellsExposed
[i
] );
3871 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3873 int row
= coords
.GetRow();
3874 int col
= coords
.GetCol();
3876 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3879 // we draw the cell border ourselves
3880 #if !WXGRID_DRAW_LINES
3881 if ( m_gridLinesEnabled
)
3882 DrawCellBorder( dc
, coords
);
3885 // but all the rest is drawn by the cell renderer and hence may be
3888 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3889 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3890 rect
.width
= m_colWidths
[col
]-1;
3891 rect
.height
= m_rowHeights
[row
]-1;
3893 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3894 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3896 if (m_currentCellCoords
== coords
)
3897 DrawCellHighlight(dc
, attr
);
3902 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
3904 int row
= m_currentCellCoords
.GetRow();
3905 int col
= m_currentCellCoords
.GetCol();
3907 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3911 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3912 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3913 rect
.width
= m_colWidths
[col
] - 1;
3914 rect
.height
= m_rowHeights
[row
] - 1;
3916 if ( attr
->IsReadOnly() )
3918 // hmmm... what could we do here to show that the cell is disabled?
3919 // for now, I just draw a thinner border than for the other ones, but
3920 // it doesn't look really good
3921 dc
.SetPen(wxPen(m_gridLineColour
, 2, wxSOLID
));
3922 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3924 dc
.DrawRectangle(rect
);
3928 // VZ: my experiments with 3d borders...
3930 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
3931 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3933 dc
.DrawRectangle(rect
);
3935 // FIXME we should properly set colours for arbitrary bg
3936 wxCoord x1
= rect
.x
,
3938 x2
= rect
.x
+ rect
.width
,
3939 y2
= rect
.y
+ rect
.height
;
3941 dc
.SetPen(*wxWHITE_PEN
);
3942 dc
.DrawLine(x1
, y1
, x2
- 1, y1
);
3943 dc
.DrawLine(x1
, y1
, x1
, y2
- 1);
3945 dc
.SetPen(*wxLIGHT_GREY_PEN
);
3946 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
3947 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
- 1);
3949 dc
.SetPen(*wxBLACK_PEN
);
3950 dc
.DrawLine(x1
, y2
, x2
, y2
);
3951 dc
.DrawLine(x2
, y1
, x2
, y2
);
3956 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3958 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3959 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3961 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3962 int row
= coords
.GetRow();
3963 int col
= coords
.GetCol();
3965 // right hand border
3967 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3968 m_colRights
[col
], m_rowBottoms
[row
] );
3972 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3973 m_colRights
[col
], m_rowBottoms
[row
] );
3977 // TODO: remove this ???
3978 // This is used to redraw all grid lines e.g. when the grid line colour
3981 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3983 if ( !m_gridLinesEnabled
||
3985 !m_numCols
) return;
3987 int top
, bottom
, left
, right
;
3991 m_gridWin
->GetClientSize(&cw
, &ch
);
3993 // virtual coords of visible area
3995 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3996 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4000 reg
.GetBox(x
, y
, w
, h
);
4001 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4002 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4005 // avoid drawing grid lines past the last row and col
4007 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
4008 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
4010 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4012 // horizontal grid lines
4015 for ( i
= 0; i
< m_numRows
; i
++ )
4017 if ( m_rowBottoms
[i
]-1 > bottom
)
4021 else if ( m_rowBottoms
[i
]-1 >= top
)
4023 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
4028 // vertical grid lines
4030 for ( i
= 0; i
< m_numCols
; i
++ )
4032 if ( m_colRights
[i
]-1 > right
)
4036 else if ( m_colRights
[i
]-1 >= left
)
4038 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
4044 void wxGrid::DrawRowLabels( wxDC
& dc
)
4046 if ( !m_numRows
|| !m_numCols
) return;
4049 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4051 for ( i
= 0; i
< numLabels
; i
++ )
4053 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4058 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4060 if ( m_rowHeights
[row
] <= 0 ) return;
4062 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4064 dc
.SetPen( *wxBLACK_PEN
);
4065 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4066 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4068 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4069 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4071 dc
.SetPen( *wxWHITE_PEN
);
4072 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4073 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4075 dc
.SetBackgroundMode( wxTRANSPARENT
);
4076 dc
.SetTextForeground( GetLabelTextColour() );
4077 dc
.SetFont( GetLabelFont() );
4080 GetRowLabelAlignment( &hAlign
, &vAlign
);
4084 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4085 rect
.SetWidth( m_rowLabelWidth
- 4 );
4086 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4087 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4091 void wxGrid::DrawColLabels( wxDC
& dc
)
4093 if ( !m_numRows
|| !m_numCols
) return;
4096 size_t numLabels
= m_colLabelsExposed
.GetCount();
4098 for ( i
= 0; i
< numLabels
; i
++ )
4100 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4105 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4107 if ( m_colWidths
[col
] <= 0 ) return;
4109 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4111 dc
.SetPen( *wxBLACK_PEN
);
4112 dc
.DrawLine( m_colRights
[col
]-1, 0,
4113 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4115 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4116 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4118 dc
.SetPen( *wxWHITE_PEN
);
4119 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4120 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4122 dc
.SetBackgroundMode( wxTRANSPARENT
);
4123 dc
.SetTextForeground( GetLabelTextColour() );
4124 dc
.SetFont( GetLabelFont() );
4126 dc
.SetBackgroundMode( wxTRANSPARENT
);
4127 dc
.SetTextForeground( GetLabelTextColour() );
4128 dc
.SetFont( GetLabelFont() );
4131 GetColLabelAlignment( &hAlign
, &vAlign
);
4134 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4136 rect
.SetWidth( m_colWidths
[col
] - 4 );
4137 rect
.SetHeight( m_colLabelHeight
- 4 );
4138 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4142 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4143 const wxString
& value
,
4148 long textWidth
, textHeight
;
4149 long lineWidth
, lineHeight
;
4150 wxArrayString lines
;
4152 dc
.SetClippingRegion( rect
);
4153 StringToLines( value
, lines
);
4154 if ( lines
.GetCount() )
4156 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4157 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4160 switch ( horizAlign
)
4163 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4167 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4176 switch ( vertAlign
)
4179 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4183 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4192 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4194 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4199 dc
.DestroyClippingRegion();
4203 // Split multi line text up into an array of strings. Any existing
4204 // contents of the string array are preserved.
4206 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4210 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4211 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4213 while ( startPos
< (int)tVal
.Length() )
4215 pos
= tVal
.Mid(startPos
).Find( eol
);
4220 else if ( pos
== 0 )
4222 lines
.Add( wxEmptyString
);
4226 lines
.Add( value
.Mid(startPos
, pos
) );
4230 if ( startPos
< (int)value
.Length() )
4232 lines
.Add( value
.Mid( startPos
) );
4237 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4238 wxArrayString
& lines
,
4239 long *width
, long *height
)
4246 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4248 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4249 w
= wxMax( w
, lineW
);
4259 // ------ Edit control functions
4263 void wxGrid::EnableEditing( bool edit
)
4265 // TODO: improve this ?
4267 if ( edit
!= m_editable
)
4271 // FIXME IMHO this won't disable the edit control if edit == FALSE
4272 // because of the check in the beginning of
4273 // EnableCellEditControl() just below (VZ)
4274 EnableCellEditControl(m_editable
);
4279 void wxGrid::EnableCellEditControl( bool enable
)
4284 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4285 SetCurrentCell( 0, 0 );
4287 if ( enable
!= m_cellEditCtrlEnabled
)
4289 // TODO allow the app to Veto() this event?
4290 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4294 // this should be checked by the caller!
4295 wxASSERT_MSG( CanEnableCellControl(),
4296 _T("can't enable editing for this cell!") );
4298 // do it before ShowCellEditControl()
4299 m_cellEditCtrlEnabled
= enable
;
4301 SetEditControlValue();
4302 ShowCellEditControl();
4306 HideCellEditControl();
4307 SaveEditControlValue();
4309 // do it after HideCellEditControl()
4310 m_cellEditCtrlEnabled
= enable
;
4315 bool wxGrid::IsCurrentCellReadOnly() const
4318 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4319 bool readonly
= attr
->IsReadOnly();
4325 bool wxGrid::CanEnableCellControl() const
4327 return m_editable
&& !IsCurrentCellReadOnly();
4330 bool wxGrid::IsCellEditControlEnabled() const
4332 // the cell edit control might be disable for all cells or just for the
4333 // current one if it's read only
4334 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
4337 void wxGrid::ShowCellEditControl()
4339 if ( IsCellEditControlEnabled() )
4341 if ( !IsVisible( m_currentCellCoords
) )
4347 wxRect rect
= CellToRect( m_currentCellCoords
);
4348 int row
= m_currentCellCoords
.GetRow();
4349 int col
= m_currentCellCoords
.GetCol();
4351 // convert to scrolled coords
4353 int left
, top
, right
, bottom
;
4354 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4355 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4356 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4358 m_gridWin
->GetClientSize( &cw
, &ch
);
4360 // Make the edit control large enough to allow for internal
4363 // TODO: remove this if the text ctrl sizing is improved esp. for
4367 #if defined(__WXMOTIF__)
4368 if ( row
== 0 || col
== 0 )
4377 if ( row
== 0 || col
== 0 )
4387 #if defined(__WXGTK__)
4390 if (left
!= 0) left_diff
++;
4391 if (top
!= 0) top_diff
++;
4392 rect
.SetLeft( left
+ left_diff
);
4393 rect
.SetTop( top
+ top_diff
);
4394 rect
.SetRight( rect
.GetRight() - left_diff
);
4395 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4397 rect
.SetLeft( wxMax(0, left
- extra
) );
4398 rect
.SetTop( wxMax(0, top
- extra
) );
4399 rect
.SetRight( rect
.GetRight() + 2*extra
);
4400 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4403 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4404 wxGridCellEditor
* editor
= attr
->GetEditor();
4405 if ( !editor
->IsCreated() )
4407 editor
->Create(m_gridWin
, -1,
4408 new wxGridCellEditorEvtHandler(this, editor
));
4411 editor
->SetSize( rect
);
4412 editor
->Show( TRUE
, attr
);
4413 editor
->BeginEdit(row
, col
, this);
4420 void wxGrid::HideCellEditControl()
4422 if ( IsCellEditControlEnabled() )
4424 int row
= m_currentCellCoords
.GetRow();
4425 int col
= m_currentCellCoords
.GetCol();
4427 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4428 attr
->GetEditor()->Show( FALSE
);
4430 m_gridWin
->SetFocus();
4435 void wxGrid::SetEditControlValue( const wxString
& value
)
4437 // RD: The new Editors get the value from the table themselves now. This
4438 // method can probably be removed...
4442 void wxGrid::SaveEditControlValue()
4444 if ( IsCellEditControlEnabled() )
4446 int row
= m_currentCellCoords
.GetRow();
4447 int col
= m_currentCellCoords
.GetCol();
4449 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4450 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4456 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4457 m_currentCellCoords
.GetRow(),
4458 m_currentCellCoords
.GetCol() );
4465 // ------ Grid location functions
4466 // Note that all of these functions work with the logical coordinates of
4467 // grid cells and labels so you will need to convert from device
4468 // coordinates for mouse events etc.
4471 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4473 int row
= YToRow(y
);
4474 int col
= XToCol(x
);
4476 if ( row
== -1 || col
== -1 )
4478 coords
= wxGridNoCellCoords
;
4482 coords
.Set( row
, col
);
4487 int wxGrid::YToRow( int y
)
4491 for ( i
= 0; i
< m_numRows
; i
++ )
4493 if ( y
< m_rowBottoms
[i
] ) return i
;
4496 return m_numRows
; //-1;
4500 int wxGrid::XToCol( int x
)
4504 for ( i
= 0; i
< m_numCols
; i
++ )
4506 if ( x
< m_colRights
[i
] ) return i
;
4509 return m_numCols
; //-1;
4513 // return the row number that that the y coord is near the edge of, or
4514 // -1 if not near an edge
4516 int wxGrid::YToEdgeOfRow( int y
)
4520 for ( i
= 0; i
< m_numRows
; i
++ )
4522 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4524 d
= abs( y
- m_rowBottoms
[i
] );
4526 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4535 // return the col number that that the x coord is near the edge of, or
4536 // -1 if not near an edge
4538 int wxGrid::XToEdgeOfCol( int x
)
4542 for ( i
= 0; i
< m_numCols
; i
++ )
4544 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4546 d
= abs( x
- m_colRights
[i
] );
4548 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4557 wxRect
wxGrid::CellToRect( int row
, int col
)
4559 wxRect
rect( -1, -1, -1, -1 );
4561 if ( row
>= 0 && row
< m_numRows
&&
4562 col
>= 0 && col
< m_numCols
)
4564 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4565 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4566 rect
.width
= m_colWidths
[col
];
4567 rect
.height
= m_rowHeights
[ row
];
4574 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4576 // get the cell rectangle in logical coords
4578 wxRect
r( CellToRect( row
, col
) );
4580 // convert to device coords
4582 int left
, top
, right
, bottom
;
4583 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4584 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4586 // check against the client area of the grid window
4589 m_gridWin
->GetClientSize( &cw
, &ch
);
4591 if ( wholeCellVisible
)
4593 // is the cell wholly visible ?
4595 return ( left
>= 0 && right
<= cw
&&
4596 top
>= 0 && bottom
<= ch
);
4600 // is the cell partly visible ?
4602 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4603 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4608 // make the specified cell location visible by doing a minimal amount
4611 void wxGrid::MakeCellVisible( int row
, int col
)
4614 int xpos
= -1, ypos
= -1;
4616 if ( row
>= 0 && row
< m_numRows
&&
4617 col
>= 0 && col
< m_numCols
)
4619 // get the cell rectangle in logical coords
4621 wxRect
r( CellToRect( row
, col
) );
4623 // convert to device coords
4625 int left
, top
, right
, bottom
;
4626 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4627 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4630 m_gridWin
->GetClientSize( &cw
, &ch
);
4636 else if ( bottom
> ch
)
4638 int h
= r
.GetHeight();
4640 for ( i
= row
-1; i
>= 0; i
-- )
4642 if ( h
+ m_rowHeights
[i
] > ch
) break;
4644 h
+= m_rowHeights
[i
];
4645 ypos
-= m_rowHeights
[i
];
4648 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4649 // have rounding errors (this is important, because if we do, we
4650 // might not scroll at all and some cells won't be redrawn)
4651 ypos
+= GRID_SCROLL_LINE
/ 2;
4658 else if ( right
> cw
)
4660 int w
= r
.GetWidth();
4662 for ( i
= col
-1; i
>= 0; i
-- )
4664 if ( w
+ m_colWidths
[i
] > cw
) break;
4666 w
+= m_colWidths
[i
];
4667 xpos
-= m_colWidths
[i
];
4670 // see comment for ypos above
4671 xpos
+= GRID_SCROLL_LINE
/ 2;
4674 if ( xpos
!= -1 || ypos
!= -1 )
4676 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4677 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4678 Scroll( xpos
, ypos
);
4686 // ------ Grid cursor movement functions
4689 bool wxGrid::MoveCursorUp()
4691 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4692 m_currentCellCoords
.GetRow() > 0 )
4694 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4695 m_currentCellCoords
.GetCol() );
4697 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4698 m_currentCellCoords
.GetCol() );
4707 bool wxGrid::MoveCursorDown()
4709 // TODO: allow for scrolling
4711 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4712 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4714 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4715 m_currentCellCoords
.GetCol() );
4717 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4718 m_currentCellCoords
.GetCol() );
4727 bool wxGrid::MoveCursorLeft()
4729 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4730 m_currentCellCoords
.GetCol() > 0 )
4732 MakeCellVisible( m_currentCellCoords
.GetRow(),
4733 m_currentCellCoords
.GetCol() - 1 );
4735 SetCurrentCell( m_currentCellCoords
.GetRow(),
4736 m_currentCellCoords
.GetCol() - 1 );
4745 bool wxGrid::MoveCursorRight()
4747 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4748 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4750 MakeCellVisible( m_currentCellCoords
.GetRow(),
4751 m_currentCellCoords
.GetCol() + 1 );
4753 SetCurrentCell( m_currentCellCoords
.GetRow(),
4754 m_currentCellCoords
.GetCol() + 1 );
4763 bool wxGrid::MovePageUp()
4765 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4767 int row
= m_currentCellCoords
.GetRow();
4771 m_gridWin
->GetClientSize( &cw
, &ch
);
4773 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4774 int newRow
= YToRow( y
- ch
+ 1 );
4779 else if ( newRow
== row
)
4784 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4785 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4793 bool wxGrid::MovePageDown()
4795 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4797 int row
= m_currentCellCoords
.GetRow();
4798 if ( row
< m_numRows
)
4801 m_gridWin
->GetClientSize( &cw
, &ch
);
4803 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4804 int newRow
= YToRow( y
+ ch
);
4807 newRow
= m_numRows
- 1;
4809 else if ( newRow
== row
)
4814 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4815 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4823 bool wxGrid::MoveCursorUpBlock()
4826 m_currentCellCoords
!= wxGridNoCellCoords
&&
4827 m_currentCellCoords
.GetRow() > 0 )
4829 int row
= m_currentCellCoords
.GetRow();
4830 int col
= m_currentCellCoords
.GetCol();
4832 if ( m_table
->IsEmptyCell(row
, col
) )
4834 // starting in an empty cell: find the next block of
4840 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4843 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4845 // starting at the top of a block: find the next block
4851 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4856 // starting within a block: find the top of the block
4861 if ( m_table
->IsEmptyCell(row
, col
) )
4869 MakeCellVisible( row
, col
);
4870 SetCurrentCell( row
, col
);
4878 bool wxGrid::MoveCursorDownBlock()
4881 m_currentCellCoords
!= wxGridNoCellCoords
&&
4882 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4884 int row
= m_currentCellCoords
.GetRow();
4885 int col
= m_currentCellCoords
.GetCol();
4887 if ( m_table
->IsEmptyCell(row
, col
) )
4889 // starting in an empty cell: find the next block of
4892 while ( row
< m_numRows
-1 )
4895 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4898 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4900 // starting at the bottom of a block: find the next block
4903 while ( row
< m_numRows
-1 )
4906 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4911 // starting within a block: find the bottom of the block
4913 while ( row
< m_numRows
-1 )
4916 if ( m_table
->IsEmptyCell(row
, col
) )
4924 MakeCellVisible( row
, col
);
4925 SetCurrentCell( row
, col
);
4933 bool wxGrid::MoveCursorLeftBlock()
4936 m_currentCellCoords
!= wxGridNoCellCoords
&&
4937 m_currentCellCoords
.GetCol() > 0 )
4939 int row
= m_currentCellCoords
.GetRow();
4940 int col
= m_currentCellCoords
.GetCol();
4942 if ( m_table
->IsEmptyCell(row
, col
) )
4944 // starting in an empty cell: find the next block of
4950 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4953 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4955 // starting at the left of a block: find the next block
4961 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4966 // starting within a block: find the left of the block
4971 if ( m_table
->IsEmptyCell(row
, col
) )
4979 MakeCellVisible( row
, col
);
4980 SetCurrentCell( row
, col
);
4988 bool wxGrid::MoveCursorRightBlock()
4991 m_currentCellCoords
!= wxGridNoCellCoords
&&
4992 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4994 int row
= m_currentCellCoords
.GetRow();
4995 int col
= m_currentCellCoords
.GetCol();
4997 if ( m_table
->IsEmptyCell(row
, col
) )
4999 // starting in an empty cell: find the next block of
5002 while ( col
< m_numCols
-1 )
5005 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5008 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5010 // starting at the right of a block: find the next block
5013 while ( col
< m_numCols
-1 )
5016 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5021 // starting within a block: find the right of the block
5023 while ( col
< m_numCols
-1 )
5026 if ( m_table
->IsEmptyCell(row
, col
) )
5034 MakeCellVisible( row
, col
);
5035 SetCurrentCell( row
, col
);
5046 // ------ Label values and formatting
5049 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5051 *horiz
= m_rowLabelHorizAlign
;
5052 *vert
= m_rowLabelVertAlign
;
5055 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5057 *horiz
= m_colLabelHorizAlign
;
5058 *vert
= m_colLabelVertAlign
;
5061 wxString
wxGrid::GetRowLabelValue( int row
)
5065 return m_table
->GetRowLabelValue( row
);
5075 wxString
wxGrid::GetColLabelValue( int col
)
5079 return m_table
->GetColLabelValue( col
);
5090 void wxGrid::SetRowLabelSize( int width
)
5092 width
= wxMax( width
, 0 );
5093 if ( width
!= m_rowLabelWidth
)
5097 m_rowLabelWin
->Show( FALSE
);
5098 m_cornerLabelWin
->Show( FALSE
);
5100 else if ( m_rowLabelWidth
== 0 )
5102 m_rowLabelWin
->Show( TRUE
);
5103 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5106 m_rowLabelWidth
= width
;
5113 void wxGrid::SetColLabelSize( int height
)
5115 height
= wxMax( height
, 0 );
5116 if ( height
!= m_colLabelHeight
)
5120 m_colLabelWin
->Show( FALSE
);
5121 m_cornerLabelWin
->Show( FALSE
);
5123 else if ( m_colLabelHeight
== 0 )
5125 m_colLabelWin
->Show( TRUE
);
5126 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5129 m_colLabelHeight
= height
;
5136 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5138 if ( m_labelBackgroundColour
!= colour
)
5140 m_labelBackgroundColour
= colour
;
5141 m_rowLabelWin
->SetBackgroundColour( colour
);
5142 m_colLabelWin
->SetBackgroundColour( colour
);
5143 m_cornerLabelWin
->SetBackgroundColour( colour
);
5145 if ( !GetBatchCount() )
5147 m_rowLabelWin
->Refresh();
5148 m_colLabelWin
->Refresh();
5149 m_cornerLabelWin
->Refresh();
5154 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5156 if ( m_labelTextColour
!= colour
)
5158 m_labelTextColour
= colour
;
5159 if ( !GetBatchCount() )
5161 m_rowLabelWin
->Refresh();
5162 m_colLabelWin
->Refresh();
5167 void wxGrid::SetLabelFont( const wxFont
& font
)
5170 if ( !GetBatchCount() )
5172 m_rowLabelWin
->Refresh();
5173 m_colLabelWin
->Refresh();
5177 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5179 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5181 m_rowLabelHorizAlign
= horiz
;
5184 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5186 m_rowLabelVertAlign
= vert
;
5189 if ( !GetBatchCount() )
5191 m_rowLabelWin
->Refresh();
5195 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5197 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5199 m_colLabelHorizAlign
= horiz
;
5202 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5204 m_colLabelVertAlign
= vert
;
5207 if ( !GetBatchCount() )
5209 m_colLabelWin
->Refresh();
5213 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5217 m_table
->SetRowLabelValue( row
, s
);
5218 if ( !GetBatchCount() )
5220 wxRect rect
= CellToRect( row
, 0);
5221 if ( rect
.height
> 0 )
5223 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5225 rect
.width
= m_rowLabelWidth
;
5226 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5232 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5236 m_table
->SetColLabelValue( col
, s
);
5237 if ( !GetBatchCount() )
5239 wxRect rect
= CellToRect( 0, col
);
5240 if ( rect
.width
> 0 )
5242 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5244 rect
.height
= m_colLabelHeight
;
5245 m_colLabelWin
->Refresh( TRUE
, &rect
);
5251 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5253 if ( m_gridLineColour
!= colour
)
5255 m_gridLineColour
= colour
;
5257 wxClientDC
dc( m_gridWin
);
5259 DrawAllGridLines( dc
, wxRegion() );
5263 void wxGrid::EnableGridLines( bool enable
)
5265 if ( enable
!= m_gridLinesEnabled
)
5267 m_gridLinesEnabled
= enable
;
5269 if ( !GetBatchCount() )
5273 wxClientDC
dc( m_gridWin
);
5275 DrawAllGridLines( dc
, wxRegion() );
5279 m_gridWin
->Refresh();
5286 int wxGrid::GetDefaultRowSize()
5288 return m_defaultRowHeight
;
5291 int wxGrid::GetRowSize( int row
)
5293 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5295 return m_rowHeights
[row
];
5298 int wxGrid::GetDefaultColSize()
5300 return m_defaultColWidth
;
5303 int wxGrid::GetColSize( int col
)
5305 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5307 return m_colWidths
[col
];
5310 // ============================================================================
5311 // access to the grid attributes: each of them has a default value in the grid
5312 // itself and may be overidden on a per-cell basis
5313 // ============================================================================
5315 // ----------------------------------------------------------------------------
5316 // setting default attributes
5317 // ----------------------------------------------------------------------------
5319 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5321 m_defaultCellAttr
->SetBackgroundColour(col
);
5324 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5326 m_defaultCellAttr
->SetTextColour(col
);
5329 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5331 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5334 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5336 m_defaultCellAttr
->SetFont(font
);
5339 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5341 m_defaultCellAttr
->SetRenderer(renderer
);
5344 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5346 m_defaultCellAttr
->SetEditor(editor
);
5349 // ----------------------------------------------------------------------------
5350 // access to the default attrbiutes
5351 // ----------------------------------------------------------------------------
5353 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5355 return m_defaultCellAttr
->GetBackgroundColour();
5358 wxColour
wxGrid::GetDefaultCellTextColour()
5360 return m_defaultCellAttr
->GetTextColour();
5363 wxFont
wxGrid::GetDefaultCellFont()
5365 return m_defaultCellAttr
->GetFont();
5368 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5370 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5373 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5375 return m_defaultCellAttr
->GetRenderer();
5378 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5380 return m_defaultCellAttr
->GetEditor();
5383 // ----------------------------------------------------------------------------
5384 // access to cell attributes
5385 // ----------------------------------------------------------------------------
5387 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5389 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5390 wxColour colour
= attr
->GetBackgroundColour();
5395 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5397 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5398 wxColour colour
= attr
->GetTextColour();
5403 wxFont
wxGrid::GetCellFont( int row
, int col
)
5405 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5406 wxFont font
= attr
->GetFont();
5411 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5413 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5414 attr
->GetAlignment(horiz
, vert
);
5418 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5420 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5421 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5426 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5428 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5429 wxGridCellEditor
* editor
= attr
->GetEditor();
5434 bool wxGrid::IsReadOnly(int row
, int col
) const
5436 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5437 bool isReadOnly
= attr
->IsReadOnly();
5442 // ----------------------------------------------------------------------------
5443 // attribute support: cache, automatic provider creation, ...
5444 // ----------------------------------------------------------------------------
5446 bool wxGrid::CanHaveAttributes()
5453 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5454 // table is providing the attributes itself??? In which case
5455 // I don't think the grid should create a Provider object for the
5456 // table but the table should be smart enough to do that on its own.
5457 if ( !m_table
->GetAttrProvider() )
5459 // use the default attr provider by default
5460 // (another choice would be to just return FALSE thus forcing the user
5462 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5468 void wxGrid::ClearAttrCache()
5470 if ( m_attrCache
.row
!= -1 )
5472 m_attrCache
.attr
->SafeDecRef();
5473 m_attrCache
.row
= -1;
5477 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5479 wxGrid
*self
= (wxGrid
*)this; // const_cast
5481 self
->ClearAttrCache();
5482 self
->m_attrCache
.row
= row
;
5483 self
->m_attrCache
.col
= col
;
5484 self
->m_attrCache
.attr
= attr
;
5488 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5490 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5492 *attr
= m_attrCache
.attr
;
5493 (*attr
)->SafeIncRef();
5495 #ifdef DEBUG_ATTR_CACHE
5496 gs_nAttrCacheHits
++;
5503 #ifdef DEBUG_ATTR_CACHE
5504 gs_nAttrCacheMisses
++;
5510 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5512 wxGridCellAttr
*attr
;
5513 if ( !LookupAttr(row
, col
, &attr
) )
5515 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5516 CacheAttr(row
, col
, attr
);
5519 attr
->SetDefAttr(m_defaultCellAttr
);
5521 attr
= m_defaultCellAttr
;
5528 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5530 wxGridCellAttr
*attr
;
5531 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5533 wxASSERT_MSG( m_table
,
5534 _T("we may only be called if CanHaveAttributes() "
5535 "returned TRUE and then m_table should be !NULL") );
5537 attr
= m_table
->GetAttr(row
, col
);
5540 attr
= new wxGridCellAttr
;
5542 // artificially inc the ref count to match DecRef() in caller
5545 m_table
->SetAttr(attr
, row
, col
);
5548 CacheAttr(row
, col
, attr
);
5550 attr
->SetDefAttr(m_defaultCellAttr
);
5554 // ----------------------------------------------------------------------------
5555 // setting cell attributes: this is forwarded to the table
5556 // ----------------------------------------------------------------------------
5558 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5560 if ( CanHaveAttributes() )
5562 m_table
->SetRowAttr(attr
, row
);
5570 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5572 if ( CanHaveAttributes() )
5574 m_table
->SetColAttr(attr
, col
);
5582 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5584 if ( CanHaveAttributes() )
5586 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5587 attr
->SetBackgroundColour(colour
);
5592 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5594 if ( CanHaveAttributes() )
5596 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5597 attr
->SetTextColour(colour
);
5602 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5604 if ( CanHaveAttributes() )
5606 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5607 attr
->SetFont(font
);
5612 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5614 if ( CanHaveAttributes() )
5616 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5617 attr
->SetAlignment(horiz
, vert
);
5622 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5624 if ( CanHaveAttributes() )
5626 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5627 attr
->SetRenderer(renderer
);
5632 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5634 if ( CanHaveAttributes() )
5636 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5637 attr
->SetEditor(editor
);
5642 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5644 if ( CanHaveAttributes() )
5646 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5647 attr
->SetReadOnly(isReadOnly
);
5652 // ----------------------------------------------------------------------------
5654 // ----------------------------------------------------------------------------
5656 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5658 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5660 if ( resizeExistingRows
)
5664 for ( row
= 0; row
< m_numRows
; row
++ )
5666 m_rowHeights
[row
] = m_defaultRowHeight
;
5667 bottom
+= m_defaultRowHeight
;
5668 m_rowBottoms
[row
] = bottom
;
5674 void wxGrid::SetRowSize( int row
, int height
)
5676 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5680 int h
= wxMax( 0, height
);
5681 int diff
= h
- m_rowHeights
[row
];
5683 m_rowHeights
[row
] = h
;
5684 for ( i
= row
; i
< m_numRows
; i
++ )
5686 m_rowBottoms
[i
] += diff
;
5691 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5693 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5695 if ( resizeExistingCols
)
5699 for ( col
= 0; col
< m_numCols
; col
++ )
5701 m_colWidths
[col
] = m_defaultColWidth
;
5702 right
+= m_defaultColWidth
;
5703 m_colRights
[col
] = right
;
5709 void wxGrid::SetColSize( int col
, int width
)
5711 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5715 int w
= wxMax( 0, width
);
5716 int diff
= w
- m_colWidths
[col
];
5717 m_colWidths
[col
] = w
;
5719 for ( i
= col
; i
< m_numCols
; i
++ )
5721 m_colRights
[i
] += diff
;
5728 // ------ cell value accessor functions
5731 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5735 m_table
->SetValue( row
, col
, s
.c_str() );
5736 if ( !GetBatchCount() )
5738 wxClientDC
dc( m_gridWin
);
5740 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5743 #if 0 // TODO: edit in place
5745 if ( m_currentCellCoords
.GetRow() == row
&&
5746 m_currentCellCoords
.GetCol() == col
)
5748 SetEditControlValue( s
);
5757 // ------ Block, row and col selection
5760 void wxGrid::SelectRow( int row
, bool addToSelected
)
5764 if ( IsSelection() && addToSelected
)
5767 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5770 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5771 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5772 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5773 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5777 need_refresh
[0] = TRUE
;
5778 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5779 wxGridCellCoords ( oldTop
- 1,
5781 m_selectedTopLeft
.SetRow( row
);
5786 need_refresh
[1] = TRUE
;
5787 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5788 wxGridCellCoords ( oldBottom
,
5791 m_selectedTopLeft
.SetCol( 0 );
5794 if ( oldBottom
< row
)
5796 need_refresh
[2] = TRUE
;
5797 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5798 wxGridCellCoords ( row
,
5800 m_selectedBottomRight
.SetRow( row
);
5803 if ( oldRight
< m_numCols
- 1 )
5805 need_refresh
[3] = TRUE
;
5806 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5808 wxGridCellCoords ( oldBottom
,
5810 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5813 for (i
= 0; i
< 4; i
++ )
5814 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5815 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5819 r
= SelectionToDeviceRect();
5821 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5823 m_selectedTopLeft
.Set( row
, 0 );
5824 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5825 r
= SelectionToDeviceRect();
5826 m_gridWin
->Refresh( FALSE
, &r
);
5829 wxGridRangeSelectEvent
gridEvt( GetId(),
5830 wxEVT_GRID_RANGE_SELECT
,
5833 m_selectedBottomRight
);
5835 GetEventHandler()->ProcessEvent(gridEvt
);
5839 void wxGrid::SelectCol( int col
, bool addToSelected
)
5841 if ( IsSelection() && addToSelected
)
5844 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5847 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5848 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5849 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5850 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5852 if ( oldLeft
> col
)
5854 need_refresh
[0] = TRUE
;
5855 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5856 wxGridCellCoords ( m_numRows
- 1,
5858 m_selectedTopLeft
.SetCol( col
);
5863 need_refresh
[1] = TRUE
;
5864 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5865 wxGridCellCoords ( oldTop
- 1,
5867 m_selectedTopLeft
.SetRow( 0 );
5870 if ( oldRight
< col
)
5872 need_refresh
[2] = TRUE
;
5873 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5874 wxGridCellCoords ( m_numRows
- 1,
5876 m_selectedBottomRight
.SetCol( col
);
5879 if ( oldBottom
< m_numRows
- 1 )
5881 need_refresh
[3] = TRUE
;
5882 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5884 wxGridCellCoords ( m_numRows
- 1,
5886 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5889 for (i
= 0; i
< 4; i
++ )
5890 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5891 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5897 r
= SelectionToDeviceRect();
5899 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5901 m_selectedTopLeft
.Set( 0, col
);
5902 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5903 r
= SelectionToDeviceRect();
5904 m_gridWin
->Refresh( FALSE
, &r
);
5907 wxGridRangeSelectEvent
gridEvt( GetId(),
5908 wxEVT_GRID_RANGE_SELECT
,
5911 m_selectedBottomRight
);
5913 GetEventHandler()->ProcessEvent(gridEvt
);
5917 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5920 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5922 if ( topRow
> bottomRow
)
5929 if ( leftCol
> rightCol
)
5936 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5937 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5939 if ( m_selectedTopLeft
!= updateTopLeft
||
5940 m_selectedBottomRight
!= updateBottomRight
)
5942 // Compute two optimal update rectangles:
5943 // Either one rectangle is a real subset of the
5944 // other, or they are (almost) disjoint!
5946 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5949 // Store intermediate values
5950 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5951 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5952 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5953 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5955 // Determine the outer/inner coordinates.
5956 if (oldLeft
> leftCol
)
5962 if (oldTop
> topRow
)
5968 if (oldRight
< rightCol
)
5971 oldRight
= rightCol
;
5974 if (oldBottom
< bottomRow
)
5977 oldBottom
= bottomRow
;
5981 // Now, either the stuff marked old is the outer
5982 // rectangle or we don't have a situation where one
5983 // is contained in the other.
5985 if ( oldLeft
< leftCol
)
5987 need_refresh
[0] = TRUE
;
5988 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5990 wxGridCellCoords ( oldBottom
,
5994 if ( oldTop
< topRow
)
5996 need_refresh
[1] = TRUE
;
5997 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5999 wxGridCellCoords ( topRow
- 1,
6003 if ( oldRight
> rightCol
)
6005 need_refresh
[2] = TRUE
;
6006 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6008 wxGridCellCoords ( oldBottom
,
6012 if ( oldBottom
> bottomRow
)
6014 need_refresh
[3] = TRUE
;
6015 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6017 wxGridCellCoords ( oldBottom
,
6023 m_selectedTopLeft
= updateTopLeft
;
6024 m_selectedBottomRight
= updateBottomRight
;
6026 // various Refresh() calls
6027 for (i
= 0; i
< 4; i
++ )
6028 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6029 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6032 // only generate an event if the block is not being selected by
6033 // dragging the mouse (in which case the event will be generated in
6034 // the mouse event handler)
6035 if ( !m_isDragging
)
6037 wxGridRangeSelectEvent
gridEvt( GetId(),
6038 wxEVT_GRID_RANGE_SELECT
,
6041 m_selectedBottomRight
);
6043 GetEventHandler()->ProcessEvent(gridEvt
);
6047 void wxGrid::SelectAll()
6049 m_selectedTopLeft
.Set( 0, 0 );
6050 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6052 m_gridWin
->Refresh();
6056 void wxGrid::ClearSelection()
6058 m_selectedTopLeft
= wxGridNoCellCoords
;
6059 m_selectedBottomRight
= wxGridNoCellCoords
;
6063 // This function returns the rectangle that encloses the given block
6064 // in device coords clipped to the client size of the grid window.
6066 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6067 const wxGridCellCoords
&bottomRight
)
6069 wxRect
rect( wxGridNoCellRect
);
6072 cellRect
= CellToRect( topLeft
);
6073 if ( cellRect
!= wxGridNoCellRect
)
6079 rect
= wxRect( 0, 0, 0, 0 );
6082 cellRect
= CellToRect( bottomRight
);
6083 if ( cellRect
!= wxGridNoCellRect
)
6089 return wxGridNoCellRect
;
6092 // convert to scrolled coords
6094 int left
, top
, right
, bottom
;
6095 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6096 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6099 m_gridWin
->GetClientSize( &cw
, &ch
);
6101 rect
.SetLeft( wxMax(0, left
) );
6102 rect
.SetTop( wxMax(0, top
) );
6103 rect
.SetRight( wxMin(cw
, right
) );
6104 rect
.SetBottom( wxMin(ch
, bottom
) );
6112 // ------ Grid event classes
6115 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6117 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6118 int row
, int col
, int x
, int y
,
6119 bool control
, bool shift
, bool alt
, bool meta
)
6120 : wxNotifyEvent( type
, id
)
6126 m_control
= control
;
6131 SetEventObject(obj
);
6135 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6137 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6138 int rowOrCol
, int x
, int y
,
6139 bool control
, bool shift
, bool alt
, bool meta
)
6140 : wxNotifyEvent( type
, id
)
6142 m_rowOrCol
= rowOrCol
;
6145 m_control
= control
;
6150 SetEventObject(obj
);
6154 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6156 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6157 const wxGridCellCoords
& topLeft
,
6158 const wxGridCellCoords
& bottomRight
,
6159 bool control
, bool shift
, bool alt
, bool meta
)
6160 : wxNotifyEvent( type
, id
)
6162 m_topLeft
= topLeft
;
6163 m_bottomRight
= bottomRight
;
6164 m_control
= control
;
6169 SetEventObject(obj
);
6173 #endif // ifndef wxUSE_NEW_GRID