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"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
46 // this include needs to be outside precomp for BCC
47 #include "wx/textfile.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
58 struct wxGridCellWithAttr
60 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
61 : coords(row
, col
), attr(attr_
)
70 wxGridCellCoords coords
;
74 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
76 #include "wx/arrimpl.cpp"
78 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
79 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
88 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
89 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
90 const wxPoint
&pos
, const wxSize
&size
);
95 void OnPaint( wxPaintEvent
& event
);
96 void OnMouseEvent( wxMouseEvent
& event
);
97 void OnKeyDown( wxKeyEvent
& event
);
99 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
100 DECLARE_EVENT_TABLE()
104 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
107 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
108 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
109 const wxPoint
&pos
, const wxSize
&size
);
114 void OnPaint( wxPaintEvent
&event
);
115 void OnMouseEvent( wxMouseEvent
& event
);
116 void OnKeyDown( wxKeyEvent
& event
);
118 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
119 DECLARE_EVENT_TABLE()
123 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
126 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
);
133 void OnMouseEvent( wxMouseEvent
& event
);
134 void OnKeyDown( wxKeyEvent
& event
);
135 void OnPaint( wxPaintEvent
& event
);
137 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
138 DECLARE_EVENT_TABLE()
141 class WXDLLEXPORT wxGridWindow
: public wxPanel
146 m_owner
= (wxGrid
*)NULL
;
147 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
148 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
151 wxGridWindow( wxGrid
*parent
,
152 wxGridRowLabelWindow
*rowLblWin
,
153 wxGridColLabelWindow
*colLblWin
,
154 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
157 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
161 wxGridRowLabelWindow
*m_rowLabelWin
;
162 wxGridColLabelWindow
*m_colLabelWin
;
164 void OnPaint( wxPaintEvent
&event
);
165 void OnMouseEvent( wxMouseEvent
& event
);
166 void OnKeyDown( wxKeyEvent
& );
167 void OnEraseBackground( wxEraseEvent
& );
170 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
171 DECLARE_EVENT_TABLE()
176 class wxGridCellEditorEvtHandler
: public wxEvtHandler
179 wxGridCellEditorEvtHandler()
180 : m_grid(0), m_editor(0)
182 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
183 : m_grid(grid
), m_editor(editor
)
186 void OnKeyDown(wxKeyEvent
& event
);
187 void OnChar(wxKeyEvent
& event
);
191 wxGridCellEditor
* m_editor
;
192 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
193 DECLARE_EVENT_TABLE()
197 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
198 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
199 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
200 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
205 // ----------------------------------------------------------------------------
206 // the internal data representation used by wxGridCellAttrProvider
207 // ----------------------------------------------------------------------------
209 // this class stores attributes set for cells
210 class WXDLLEXPORT wxGridCellAttrData
213 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
214 wxGridCellAttr
*GetAttr(int row
, int col
) const;
215 void UpdateAttrRows( size_t pos
, int numRows
);
216 void UpdateAttrCols( size_t pos
, int numCols
);
219 // searches for the attr for given cell, returns wxNOT_FOUND if not found
220 int FindIndex(int row
, int col
) const;
222 wxGridCellWithAttrArray m_attrs
;
225 // this class stores attributes set for rows or columns
226 class WXDLLEXPORT wxGridRowOrColAttrData
229 // empty ctor to suppress warnings
230 wxGridRowOrColAttrData() { }
231 ~wxGridRowOrColAttrData();
233 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
234 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
235 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
238 wxArrayInt m_rowsOrCols
;
239 wxArrayAttrs m_attrs
;
242 // NB: this is just a wrapper around 3 objects: one which stores cell
243 // attributes, and 2 others for row/col ones
244 class WXDLLEXPORT wxGridCellAttrProviderData
247 wxGridCellAttrData m_cellAttrs
;
248 wxGridRowOrColAttrData m_rowAttrs
,
252 // ----------------------------------------------------------------------------
253 // conditional compilation
254 // ----------------------------------------------------------------------------
256 #ifndef WXGRID_DRAW_LINES
257 #define WXGRID_DRAW_LINES 1
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 //#define DEBUG_ATTR_CACHE
265 #ifdef DEBUG_ATTR_CACHE
266 static size_t gs_nAttrCacheHits
= 0;
267 static size_t gs_nAttrCacheMisses
= 0;
268 #endif // DEBUG_ATTR_CACHE
270 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
271 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
274 // TODO: fixed so far - make configurable later (and also different for x/y)
275 static const size_t GRID_SCROLL_LINE
= 10;
277 // ============================================================================
279 // ============================================================================
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 wxGridCellEditor::wxGridCellEditor()
291 wxGridCellEditor::~wxGridCellEditor()
296 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
297 wxWindowID
WXUNUSED(id
),
298 wxEvtHandler
* evtHandler
)
301 m_control
->PushEventHandler(evtHandler
);
304 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
305 wxGridCellAttr
*attr
)
307 // erase the background because we might not fill the cell
308 wxClientDC
dc(m_control
->GetParent());
309 dc
.SetPen(*wxTRANSPARENT_PEN
);
310 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
311 dc
.DrawRectangle(rectCell
);
313 // redraw the control we just painted over
314 m_control
->Refresh();
317 void wxGridCellEditor::Destroy()
321 m_control
->Destroy();
326 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
328 wxASSERT_MSG(m_control
,
329 wxT("The wxGridCellEditor must be Created first!"));
330 m_control
->Show(show
);
334 // set the colours/fonts if we have any
337 if ( attr
->HasTextColour() )
339 m_colFgOld
= m_control
->GetForegroundColour();
340 m_control
->SetForegroundColour(attr
->GetTextColour());
343 if ( attr
->HasBackgroundColour() )
345 m_colBgOld
= m_control
->GetBackgroundColour();
346 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
349 if ( attr
->HasFont() )
351 m_fontOld
= m_control
->GetFont();
352 m_control
->SetFont(attr
->GetFont());
355 // can't do anything more in the base class version, the other
356 // attributes may only be used by the derived classes
361 // restore the standard colours fonts
362 if ( m_colFgOld
.Ok() )
364 m_control
->SetForegroundColour(m_colFgOld
);
365 m_colFgOld
= wxNullColour
;
368 if ( m_colBgOld
.Ok() )
370 m_control
->SetBackgroundColour(m_colBgOld
);
371 m_colBgOld
= wxNullColour
;
374 if ( m_fontOld
.Ok() )
376 m_control
->SetFont(m_fontOld
);
377 m_fontOld
= wxNullFont
;
382 void wxGridCellEditor::SetSize(const wxRect
& rect
)
384 wxASSERT_MSG(m_control
,
385 wxT("The wxGridCellEditor must be Created first!"));
386 m_control
->SetSize(rect
);
389 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
395 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
400 void wxGridCellEditor::StartingClick()
404 // ----------------------------------------------------------------------------
405 // wxGridCellTextEditor
406 // ----------------------------------------------------------------------------
408 wxGridCellTextEditor::wxGridCellTextEditor()
412 void wxGridCellTextEditor::Create(wxWindow
* parent
,
414 wxEvtHandler
* evtHandler
)
416 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
417 wxDefaultPosition
, wxDefaultSize
418 #if defined(__WXMSW__)
419 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
423 wxGridCellEditor::Create(parent
, id
, evtHandler
);
426 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
427 wxGridCellAttr
* WXUNUSED(attr
))
429 // as we fill the entire client area, don't do anything here to minimize
433 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
435 wxASSERT_MSG(m_control
,
436 wxT("The wxGridCellEditor must be Created first!"));
438 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
439 Text()->SetValue(m_startValue
);
440 Text()->SetInsertionPointEnd();
445 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
448 wxASSERT_MSG(m_control
,
449 wxT("The wxGridCellEditor must be Created first!"));
451 bool changed
= FALSE
;
452 wxString value
= Text()->GetValue();
453 if (value
!= m_startValue
)
457 grid
->GetTable()->SetValue(row
, col
, value
);
459 m_startValue
= wxEmptyString
;
460 Text()->SetValue(m_startValue
);
466 void wxGridCellTextEditor::Reset()
468 wxASSERT_MSG(m_control
,
469 wxT("The wxGridCellEditor must be Created first!"));
471 Text()->SetValue(m_startValue
);
472 Text()->SetInsertionPointEnd();
475 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
477 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
479 // insert the key in the control
480 long keycode
= event
.KeyCode();
481 if ( isprint(keycode
) )
483 // FIXME this is not going to work for non letters...
484 if ( !event
.ShiftDown() )
486 keycode
= tolower(keycode
);
489 Text()->AppendText((wxChar
)keycode
);
499 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
501 #if defined(__WXMOTIF__) || defined(__WXGTK__)
502 // wxMotif needs a little extra help...
503 int pos
= Text()->GetInsertionPoint();
504 wxString
s( Text()->GetValue() );
505 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
507 Text()->SetInsertionPoint( pos
);
509 // the other ports can handle a Return key press
515 // ----------------------------------------------------------------------------
516 // wxGridCellBoolEditor
517 // ----------------------------------------------------------------------------
519 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
521 wxEvtHandler
* evtHandler
)
523 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
524 wxDefaultPosition
, wxDefaultSize
,
527 wxGridCellEditor::Create(parent
, id
, evtHandler
);
530 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
532 // position it in the centre of the rectangle (TODO: support alignment?)
534 m_control
->GetSize(&w
, &h
);
536 // the checkbox without label still has some space to the right in wxGTK,
537 // so shift it to the right
542 m_control
->Move(r
.x
+ r
.width
/2 - w
/2, r
.y
+ r
.height
/2 - h
/2);
545 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
547 wxGridCellEditor::Show(show
, attr
);
550 // VZ: normally base class already does it, but it doesn't work (FIXME)
551 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
552 CBox()->SetBackgroundColour(colBg
);
556 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
558 wxASSERT_MSG(m_control
,
559 wxT("The wxGridCellEditor must be Created first!"));
561 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
); // FIXME-DATA
562 CBox()->SetValue(m_startValue
);
566 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
570 wxASSERT_MSG(m_control
,
571 wxT("The wxGridCellEditor must be Created first!"));
573 bool changed
= FALSE
;
574 bool value
= CBox()->GetValue();
575 if ( value
!= m_startValue
)
581 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
587 void wxGridCellBoolEditor::Reset()
589 wxASSERT_MSG(m_control
,
590 wxT("The wxGridCellEditor must be Created first!"));
592 CBox()->SetValue(m_startValue
);
595 void wxGridCellBoolEditor::StartingClick()
597 CBox()->SetValue(!CBox()->GetValue());
600 // ----------------------------------------------------------------------------
601 // wxGridCellEditorEvtHandler
602 // ----------------------------------------------------------------------------
604 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
606 switch ( event
.KeyCode() )
610 m_grid
->DisableCellEditControl();
614 event
.Skip( m_grid
->ProcessEvent( event
) );
618 if (!m_grid
->ProcessEvent(event
))
619 m_editor
->HandleReturn(event
);
628 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
630 switch ( event
.KeyCode() )
642 // ============================================================================
644 // ============================================================================
646 // ----------------------------------------------------------------------------
647 // wxGridCellRenderer
648 // ----------------------------------------------------------------------------
650 void wxGridCellRenderer::Draw(wxGrid
& grid
,
651 wxGridCellAttr
& attr
,
657 dc
.SetBackgroundMode( wxSOLID
);
661 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
665 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
668 dc
.SetPen( *wxTRANSPARENT_PEN
);
669 dc
.DrawRectangle(rect
);
672 // ----------------------------------------------------------------------------
673 // wxGridCellStringRenderer
674 // ----------------------------------------------------------------------------
676 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
677 wxGridCellAttr
& attr
,
679 const wxRect
& rectCell
,
683 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
685 // now we only have to draw the text
686 dc
.SetBackgroundMode( wxTRANSPARENT
);
688 // TODO some special colours for attr.IsReadOnly() case?
692 dc
.SetTextBackground( grid
.GetSelectionBackground() );
693 dc
.SetTextForeground( grid
.GetSelectionForeground() );
697 dc
.SetTextBackground( attr
.GetBackgroundColour() );
698 dc
.SetTextForeground( attr
.GetTextColour() );
700 dc
.SetFont( attr
.GetFont() );
703 attr
.GetAlignment(&hAlign
, &vAlign
);
705 wxRect rect
= rectCell
;
711 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
712 rect
, hAlign
, vAlign
);
715 // ----------------------------------------------------------------------------
716 // wxGridCellBoolRenderer
717 // ----------------------------------------------------------------------------
719 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
720 wxGridCellAttr
& attr
,
726 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
728 // between checkmark and box
729 static const wxCoord margin
= 4;
732 static wxCoord s_checkSize
= 0;
733 if ( s_checkSize
== 0 )
735 // compute it only once (no locks for MT safeness in GUI thread...)
736 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
737 wxSize size
= checkbox
->GetBestSize();
738 s_checkSize
= size
.y
+ margin
;
740 // FIXME wxGTK::wxCheckBox::GetBestSize() is really weird...
742 s_checkSize
-= size
.y
/ 2;
748 // draw a check mark in the centre (ignoring alignment - TODO)
750 rectMark
.x
= rect
.x
+ rect
.width
/2 - s_checkSize
/2;
751 rectMark
.y
= rect
.y
+ rect
.height
/2 - s_checkSize
/2;
752 rectMark
.width
= rectMark
.height
= s_checkSize
;
754 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
755 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
756 dc
.DrawRectangle(rectMark
);
758 rectMark
.Inflate(-margin
);
760 if ( !!grid
.GetTable()->GetValue(row
, col
) ) // FIXME-DATA
762 dc
.SetTextForeground(attr
.GetTextColour());
763 dc
.DrawCheckMark(rectMark
);
767 // ----------------------------------------------------------------------------
769 // ----------------------------------------------------------------------------
771 const wxColour
& wxGridCellAttr::GetTextColour() const
777 else if (m_defGridAttr
!= this)
779 return m_defGridAttr
->GetTextColour();
783 wxFAIL_MSG(wxT("Missing default cell attribute"));
789 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
791 if (HasBackgroundColour())
793 else if (m_defGridAttr
!= this)
794 return m_defGridAttr
->GetBackgroundColour();
797 wxFAIL_MSG(wxT("Missing default cell attribute"));
803 const wxFont
& wxGridCellAttr::GetFont() const
807 else if (m_defGridAttr
!= this)
808 return m_defGridAttr
->GetFont();
811 wxFAIL_MSG(wxT("Missing default cell attribute"));
817 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
821 if ( hAlign
) *hAlign
= m_hAlign
;
822 if ( vAlign
) *vAlign
= m_vAlign
;
824 else if (m_defGridAttr
!= this)
825 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
828 wxFAIL_MSG(wxT("Missing default cell attribute"));
833 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
837 else if (m_defGridAttr
!= this)
838 return m_defGridAttr
->GetRenderer();
841 wxFAIL_MSG(wxT("Missing default cell attribute"));
846 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
850 else if (m_defGridAttr
!= this)
851 return m_defGridAttr
->GetEditor();
854 wxFAIL_MSG(wxT("Missing default cell attribute"));
859 // ----------------------------------------------------------------------------
860 // wxGridCellAttrData
861 // ----------------------------------------------------------------------------
863 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
865 int n
= FindIndex(row
, col
);
866 if ( n
== wxNOT_FOUND
)
869 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
875 // change the attribute
876 m_attrs
[(size_t)n
].attr
= attr
;
880 // remove this attribute
881 m_attrs
.RemoveAt((size_t)n
);
886 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
888 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
890 int n
= FindIndex(row
, col
);
891 if ( n
!= wxNOT_FOUND
)
893 attr
= m_attrs
[(size_t)n
].attr
;
900 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
902 size_t count
= m_attrs
.GetCount();
903 for ( size_t n
= 0; n
< count
; n
++ )
905 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
906 wxCoord row
= coords
.GetRow();
907 if ((size_t)row
>= pos
)
911 // If rows inserted, include row counter where necessary
912 coords
.SetRow(row
+ numRows
);
914 else if (numRows
< 0)
916 // If rows deleted ...
917 if ((size_t)row
>= pos
- numRows
)
919 // ...either decrement row counter (if row still exists)...
920 coords
.SetRow(row
+ numRows
);
924 // ...or remove the attribute
925 m_attrs
.RemoveAt((size_t)n
);
933 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
935 size_t count
= m_attrs
.GetCount();
936 for ( size_t n
= 0; n
< count
; n
++ )
938 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
939 wxCoord col
= coords
.GetCol();
940 if ( (size_t)col
>= pos
)
944 // If rows inserted, include row counter where necessary
945 coords
.SetCol(col
+ numCols
);
947 else if (numCols
< 0)
949 // If rows deleted ...
950 if ((size_t)col
>= pos
- numCols
)
952 // ...either decrement row counter (if row still exists)...
953 coords
.SetCol(col
+ numCols
);
957 // ...or remove the attribute
958 m_attrs
.RemoveAt((size_t)n
);
966 int wxGridCellAttrData::FindIndex(int row
, int col
) const
968 size_t count
= m_attrs
.GetCount();
969 for ( size_t n
= 0; n
< count
; n
++ )
971 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
972 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
981 // ----------------------------------------------------------------------------
982 // wxGridRowOrColAttrData
983 // ----------------------------------------------------------------------------
985 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
987 size_t count
= m_attrs
.Count();
988 for ( size_t n
= 0; n
< count
; n
++ )
990 m_attrs
[n
]->DecRef();
994 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
996 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
998 int n
= m_rowsOrCols
.Index(rowOrCol
);
999 if ( n
!= wxNOT_FOUND
)
1001 attr
= m_attrs
[(size_t)n
];
1008 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1010 int n
= m_rowsOrCols
.Index(rowOrCol
);
1011 if ( n
== wxNOT_FOUND
)
1013 // add the attribute
1014 m_rowsOrCols
.Add(rowOrCol
);
1021 // change the attribute
1022 m_attrs
[(size_t)n
] = attr
;
1026 // remove this attribute
1027 m_attrs
[(size_t)n
]->DecRef();
1028 m_rowsOrCols
.RemoveAt((size_t)n
);
1029 m_attrs
.RemoveAt((size_t)n
);
1034 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1036 size_t count
= m_attrs
.GetCount();
1037 for ( size_t n
= 0; n
< count
; n
++ )
1039 int & rowOrCol
= m_rowsOrCols
[n
];
1040 if ( (size_t)rowOrCol
>= pos
)
1042 if ( numRowsOrCols
> 0 )
1044 // If rows inserted, include row counter where necessary
1045 rowOrCol
+= numRowsOrCols
;
1047 else if ( numRowsOrCols
< 0)
1049 // If rows deleted, either decrement row counter (if row still exists)
1050 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1051 rowOrCol
+= numRowsOrCols
;
1054 m_rowsOrCols
.RemoveAt((size_t)n
);
1055 m_attrs
.RemoveAt((size_t)n
);
1063 // ----------------------------------------------------------------------------
1064 // wxGridCellAttrProvider
1065 // ----------------------------------------------------------------------------
1067 wxGridCellAttrProvider::wxGridCellAttrProvider()
1069 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1072 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1077 void wxGridCellAttrProvider::InitData()
1079 m_data
= new wxGridCellAttrProviderData
;
1082 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1084 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1087 // first look for the attribute of this specific cell
1088 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1092 // then look for the col attr (col attributes are more common than
1093 // the row ones, hence they have priority)
1094 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1099 // finally try the row attributes
1100 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1107 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1113 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1116 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1121 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1124 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1129 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1132 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1136 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1138 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1142 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1146 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1148 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1152 // ----------------------------------------------------------------------------
1154 // ----------------------------------------------------------------------------
1156 //////////////////////////////////////////////////////////////////////
1158 // Abstract base class for grid data (the model)
1160 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1163 wxGridTableBase::wxGridTableBase()
1165 m_view
= (wxGrid
*) NULL
;
1166 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1169 wxGridTableBase::~wxGridTableBase()
1171 delete m_attrProvider
;
1174 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1176 delete m_attrProvider
;
1177 m_attrProvider
= attrProvider
;
1180 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1182 if ( m_attrProvider
)
1183 return m_attrProvider
->GetAttr(row
, col
);
1185 return (wxGridCellAttr
*)NULL
;
1188 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1190 if ( m_attrProvider
)
1192 m_attrProvider
->SetAttr(attr
, row
, col
);
1196 // as we take ownership of the pointer and don't store it, we must
1202 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1204 if ( m_attrProvider
)
1206 m_attrProvider
->SetRowAttr(attr
, row
);
1210 // as we take ownership of the pointer and don't store it, we must
1216 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1218 if ( m_attrProvider
)
1220 m_attrProvider
->SetColAttr(attr
, col
);
1224 // as we take ownership of the pointer and don't store it, we must
1230 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1232 if ( m_attrProvider
)
1234 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1238 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1240 if ( m_attrProvider
)
1242 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1246 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1248 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1249 "but your derived table class does not override this function") );
1254 bool wxGridTableBase::AppendRows( size_t numRows
)
1256 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1257 "but your derived table class does not override this function"));
1262 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1264 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1265 "but your derived table class does not override this function"));
1270 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1272 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1273 "but your derived table class does not override this function"));
1278 bool wxGridTableBase::AppendCols( size_t numCols
)
1280 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1281 "but your derived table class does not override this function"));
1286 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1288 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1289 "but your derived table class does not override this function"));
1295 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1302 wxString
wxGridTableBase::GetColLabelValue( int col
)
1304 // default col labels are:
1305 // cols 0 to 25 : A-Z
1306 // cols 26 to 675 : AA-ZZ
1311 for ( n
= 1; ; n
++ )
1313 s
+= (_T('A') + (wxChar
)( col%26
));
1315 if ( col
< 0 ) break;
1318 // reverse the string...
1320 for ( i
= 0; i
< n
; i
++ )
1330 //////////////////////////////////////////////////////////////////////
1332 // Message class for the grid table to send requests and notifications
1336 wxGridTableMessage::wxGridTableMessage()
1338 m_table
= (wxGridTableBase
*) NULL
;
1344 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1345 int commandInt1
, int commandInt2
)
1349 m_comInt1
= commandInt1
;
1350 m_comInt2
= commandInt2
;
1355 //////////////////////////////////////////////////////////////////////
1357 // A basic grid table for string data. An object of this class will
1358 // created by wxGrid if you don't specify an alternative table class.
1361 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1363 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1365 wxGridStringTable::wxGridStringTable()
1370 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1375 m_data
.Alloc( numRows
);
1378 sa
.Alloc( numCols
);
1379 for ( col
= 0; col
< numCols
; col
++ )
1381 sa
.Add( wxEmptyString
);
1384 for ( row
= 0; row
< numRows
; row
++ )
1390 wxGridStringTable::~wxGridStringTable()
1394 long wxGridStringTable::GetNumberRows()
1396 return m_data
.GetCount();
1399 long wxGridStringTable::GetNumberCols()
1401 if ( m_data
.GetCount() > 0 )
1402 return m_data
[0].GetCount();
1407 wxString
wxGridStringTable::GetValue( int row
, int col
)
1409 // TODO: bounds checking
1411 return m_data
[row
][col
];
1414 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1416 // TODO: bounds checking
1418 m_data
[row
][col
] = s
;
1421 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1423 // TODO: bounds checking
1425 return (m_data
[row
][col
] == wxEmptyString
);
1429 void wxGridStringTable::Clear()
1432 int numRows
, numCols
;
1434 numRows
= m_data
.GetCount();
1437 numCols
= m_data
[0].GetCount();
1439 for ( row
= 0; row
< numRows
; row
++ )
1441 for ( col
= 0; col
< numCols
; col
++ )
1443 m_data
[row
][col
] = wxEmptyString
;
1450 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1454 size_t curNumRows
= m_data
.GetCount();
1455 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1457 if ( pos
>= curNumRows
)
1459 return AppendRows( numRows
);
1463 sa
.Alloc( curNumCols
);
1464 for ( col
= 0; col
< curNumCols
; col
++ )
1466 sa
.Add( wxEmptyString
);
1469 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1471 m_data
.Insert( sa
, row
);
1473 UpdateAttrRows( pos
, numRows
);
1476 wxGridTableMessage
msg( this,
1477 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1481 GetView()->ProcessTableMessage( msg
);
1487 bool wxGridStringTable::AppendRows( size_t numRows
)
1491 size_t curNumRows
= m_data
.GetCount();
1492 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1495 if ( curNumCols
> 0 )
1497 sa
.Alloc( curNumCols
);
1498 for ( col
= 0; col
< curNumCols
; col
++ )
1500 sa
.Add( wxEmptyString
);
1504 for ( row
= 0; row
< numRows
; row
++ )
1511 wxGridTableMessage
msg( this,
1512 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1515 GetView()->ProcessTableMessage( msg
);
1521 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1525 size_t curNumRows
= m_data
.GetCount();
1527 if ( pos
>= curNumRows
)
1530 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1531 "Pos value is invalid for present table with %d rows",
1532 pos
, numRows
, curNumRows
);
1533 wxFAIL_MSG( wxT(errmsg
) );
1537 if ( numRows
> curNumRows
- pos
)
1539 numRows
= curNumRows
- pos
;
1542 if ( numRows
>= curNumRows
)
1544 m_data
.Empty(); // don't release memory just yet
1548 for ( n
= 0; n
< numRows
; n
++ )
1550 m_data
.Remove( pos
);
1553 UpdateAttrRows( pos
, -((int)numRows
) );
1556 wxGridTableMessage
msg( this,
1557 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1561 GetView()->ProcessTableMessage( msg
);
1567 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1571 size_t curNumRows
= m_data
.GetCount();
1572 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1574 if ( pos
>= curNumCols
)
1576 return AppendCols( numCols
);
1579 for ( row
= 0; row
< curNumRows
; row
++ )
1581 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1583 m_data
[row
].Insert( wxEmptyString
, col
);
1586 UpdateAttrCols( pos
, numCols
);
1589 wxGridTableMessage
msg( this,
1590 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1594 GetView()->ProcessTableMessage( msg
);
1600 bool wxGridStringTable::AppendCols( size_t numCols
)
1604 size_t curNumRows
= m_data
.GetCount();
1607 // TODO: something better than this ?
1609 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1610 "Call AppendRows() first") );
1614 for ( row
= 0; row
< curNumRows
; row
++ )
1616 for ( n
= 0; n
< numCols
; n
++ )
1618 m_data
[row
].Add( wxEmptyString
);
1624 wxGridTableMessage
msg( this,
1625 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1628 GetView()->ProcessTableMessage( msg
);
1634 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1638 size_t curNumRows
= m_data
.GetCount();
1639 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1641 if ( pos
>= curNumCols
)
1644 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1645 "Pos value is invalid for present table with %d cols",
1646 pos
, numCols
, curNumCols
);
1647 wxFAIL_MSG( wxT( errmsg
) );
1651 if ( numCols
> curNumCols
- pos
)
1653 numCols
= curNumCols
- pos
;
1656 for ( row
= 0; row
< curNumRows
; row
++ )
1658 if ( numCols
>= curNumCols
)
1660 m_data
[row
].Clear();
1664 for ( n
= 0; n
< numCols
; n
++ )
1666 m_data
[row
].Remove( pos
);
1670 UpdateAttrCols( pos
, -((int)numCols
) );
1673 wxGridTableMessage
msg( this,
1674 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1678 GetView()->ProcessTableMessage( msg
);
1684 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1686 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1688 // using default label
1690 return wxGridTableBase::GetRowLabelValue( row
);
1694 return m_rowLabels
[ row
];
1698 wxString
wxGridStringTable::GetColLabelValue( int col
)
1700 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1702 // using default label
1704 return wxGridTableBase::GetColLabelValue( col
);
1708 return m_colLabels
[ col
];
1712 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1714 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1716 int n
= m_rowLabels
.GetCount();
1718 for ( i
= n
; i
<= row
; i
++ )
1720 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1724 m_rowLabels
[row
] = value
;
1727 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1729 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1731 int n
= m_colLabels
.GetCount();
1733 for ( i
= n
; i
<= col
; i
++ )
1735 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1739 m_colLabels
[col
] = value
;
1744 //////////////////////////////////////////////////////////////////////
1745 //////////////////////////////////////////////////////////////////////
1747 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1749 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1750 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1751 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1752 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1755 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1757 const wxPoint
&pos
, const wxSize
&size
)
1758 : wxWindow( parent
, id
, pos
, size
)
1763 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1767 // NO - don't do this because it will set both the x and y origin
1768 // coords to match the parent scrolled window and we just want to
1769 // set the y coord - MB
1771 // m_owner->PrepareDC( dc );
1774 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1775 dc
.SetDeviceOrigin( 0, -y
);
1777 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1778 m_owner
->DrawRowLabels( dc
);
1782 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1784 m_owner
->ProcessRowLabelMouseEvent( event
);
1788 // This seems to be required for wxMotif otherwise the mouse
1789 // cursor must be in the cell edit control to get key events
1791 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1793 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1798 //////////////////////////////////////////////////////////////////////
1800 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1802 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1803 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1804 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1805 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1808 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1810 const wxPoint
&pos
, const wxSize
&size
)
1811 : wxWindow( parent
, id
, pos
, size
)
1816 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1820 // NO - don't do this because it will set both the x and y origin
1821 // coords to match the parent scrolled window and we just want to
1822 // set the x coord - MB
1824 // m_owner->PrepareDC( dc );
1827 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1828 dc
.SetDeviceOrigin( -x
, 0 );
1830 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1831 m_owner
->DrawColLabels( dc
);
1835 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1837 m_owner
->ProcessColLabelMouseEvent( event
);
1841 // This seems to be required for wxMotif otherwise the mouse
1842 // cursor must be in the cell edit control to get key events
1844 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1846 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1851 //////////////////////////////////////////////////////////////////////
1853 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1855 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1856 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1857 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1858 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1861 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1863 const wxPoint
&pos
, const wxSize
&size
)
1864 : wxWindow( parent
, id
, pos
, size
)
1869 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1873 int client_height
= 0;
1874 int client_width
= 0;
1875 GetClientSize( &client_width
, &client_height
);
1877 dc
.SetPen( *wxBLACK_PEN
);
1878 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1879 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1881 dc
.SetPen( *wxWHITE_PEN
);
1882 dc
.DrawLine( 0, 0, client_width
, 0 );
1883 dc
.DrawLine( 0, 0, 0, client_height
);
1887 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1889 m_owner
->ProcessCornerLabelMouseEvent( event
);
1893 // This seems to be required for wxMotif otherwise the mouse
1894 // cursor must be in the cell edit control to get key events
1896 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1898 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1903 //////////////////////////////////////////////////////////////////////
1905 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1907 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1908 EVT_PAINT( wxGridWindow::OnPaint
)
1909 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1910 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1911 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1914 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1915 wxGridRowLabelWindow
*rowLblWin
,
1916 wxGridColLabelWindow
*colLblWin
,
1917 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1918 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1921 m_rowLabelWin
= rowLblWin
;
1922 m_colLabelWin
= colLblWin
;
1923 SetBackgroundColour( "WHITE" );
1927 wxGridWindow::~wxGridWindow()
1932 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1934 wxPaintDC
dc( this );
1935 m_owner
->PrepareDC( dc
);
1936 wxRegion reg
= GetUpdateRegion();
1937 m_owner
->CalcCellsExposed( reg
);
1938 m_owner
->DrawGridCellArea( dc
);
1939 #if WXGRID_DRAW_LINES
1940 m_owner
->DrawAllGridLines( dc
, reg
);
1945 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1947 wxPanel::ScrollWindow( dx
, dy
, rect
);
1948 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1949 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1953 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1955 m_owner
->ProcessGridCellMouseEvent( event
);
1959 // This seems to be required for wxMotif otherwise the mouse
1960 // cursor must be in the cell edit control to get key events
1962 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1964 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1967 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1973 //////////////////////////////////////////////////////////////////////
1976 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1978 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1979 EVT_PAINT( wxGrid::OnPaint
)
1980 EVT_SIZE( wxGrid::OnSize
)
1981 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1982 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1985 wxGrid::wxGrid( wxWindow
*parent
,
1990 const wxString
& name
)
1991 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
2000 m_defaultCellAttr
->SafeDecRef();
2002 #ifdef DEBUG_ATTR_CACHE
2003 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2004 wxPrintf(_T("wxGrid attribute cache statistics: "
2005 "total: %u, hits: %u (%u%%)\n"),
2006 total
, gs_nAttrCacheHits
,
2007 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2016 // ----- internal init and update functions
2019 void wxGrid::Create()
2021 m_created
= FALSE
; // set to TRUE by CreateGrid
2022 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2024 m_table
= (wxGridTableBase
*) NULL
;
2027 m_cellEditCtrlEnabled
= FALSE
;
2029 m_defaultCellAttr
= new wxGridCellAttr
;
2030 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2031 // RD: Should we fill the default attrs now or is waiting until Init() okay?
2036 m_currentCellCoords
= wxGridNoCellCoords
;
2038 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2039 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2041 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2046 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2051 m_colLabelWin
= new wxGridColLabelWindow( this,
2056 m_gridWin
= new wxGridWindow( this,
2063 SetTargetWindow( m_gridWin
);
2067 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2071 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2076 m_numRows
= numRows
;
2077 m_numCols
= numCols
;
2079 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2080 m_table
->SetView( this );
2089 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2093 // RD: Actually, this should probably be allowed. I think it would be
2094 // nice to be able to switch multiple Tables in and out of a single
2095 // View at runtime. Is there anything in the implmentation that would
2098 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2103 m_numRows
= table
->GetNumberRows();
2104 m_numCols
= table
->GetNumberCols();
2107 m_table
->SetView( this );
2122 if ( m_numRows
<= 0 )
2123 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2125 if ( m_numCols
<= 0 )
2126 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2128 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2129 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2131 if ( m_rowLabelWin
)
2133 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2137 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2140 m_labelTextColour
= wxColour( _T("BLACK") );
2143 m_attrCache
.row
= -1;
2145 // TODO: something better than this ?
2147 m_labelFont
= this->GetFont();
2148 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2150 m_rowLabelHorizAlign
= wxLEFT
;
2151 m_rowLabelVertAlign
= wxCENTRE
;
2153 m_colLabelHorizAlign
= wxCENTRE
;
2154 m_colLabelVertAlign
= wxTOP
;
2156 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2157 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2159 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2160 m_defaultRowHeight
+= 8;
2162 m_defaultRowHeight
+= 4;
2165 m_rowHeights
.Alloc( m_numRows
);
2166 m_rowBottoms
.Alloc( m_numRows
);
2168 for ( i
= 0; i
< m_numRows
; i
++ )
2170 m_rowHeights
.Add( m_defaultRowHeight
);
2171 rowBottom
+= m_defaultRowHeight
;
2172 m_rowBottoms
.Add( rowBottom
);
2175 m_colWidths
.Alloc( m_numCols
);
2176 m_colRights
.Alloc( m_numCols
);
2178 for ( i
= 0; i
< m_numCols
; i
++ )
2180 m_colWidths
.Add( m_defaultColWidth
);
2181 colRight
+= m_defaultColWidth
;
2182 m_colRights
.Add( colRight
);
2185 // Set default cell attributes
2186 m_defaultCellAttr
->SetFont(GetFont());
2187 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2188 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2189 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2190 m_defaultCellAttr
->SetTextColour(
2191 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2192 m_defaultCellAttr
->SetBackgroundColour(
2193 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2196 m_gridLineColour
= wxColour( 128, 128, 255 );
2197 m_gridLinesEnabled
= TRUE
;
2199 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2200 m_winCapture
= (wxWindow
*)NULL
;
2202 m_dragRowOrCol
= -1;
2203 m_isDragging
= FALSE
;
2204 m_startDragPos
= wxDefaultPosition
;
2206 m_waitForSlowClick
= FALSE
;
2208 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2209 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2211 m_currentCellCoords
= wxGridNoCellCoords
;
2213 m_selectedTopLeft
= wxGridNoCellCoords
;
2214 m_selectedBottomRight
= wxGridNoCellCoords
;
2215 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2216 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2218 m_editable
= TRUE
; // default for whole grid
2220 m_inOnKeyDown
= FALSE
;
2226 void wxGrid::CalcDimensions()
2229 GetClientSize( &cw
, &ch
);
2231 if ( m_numRows
> 0 && m_numCols
> 0 )
2233 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2234 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2236 // TODO: restore the scroll position that we had before sizing
2239 GetViewStart( &x
, &y
);
2240 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2241 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2247 void wxGrid::CalcWindowSizes()
2250 GetClientSize( &cw
, &ch
);
2252 if ( m_cornerLabelWin
->IsShown() )
2253 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2255 if ( m_colLabelWin
->IsShown() )
2256 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2258 if ( m_rowLabelWin
->IsShown() )
2259 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2261 if ( m_gridWin
->IsShown() )
2262 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2266 // this is called when the grid table sends a message to say that it
2267 // has been redimensioned
2269 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2273 switch ( msg
.GetId() )
2275 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2277 size_t pos
= msg
.GetCommandInt();
2278 int numRows
= msg
.GetCommandInt2();
2279 for ( i
= 0; i
< numRows
; i
++ )
2281 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2282 m_rowBottoms
.Insert( 0, pos
);
2284 m_numRows
+= numRows
;
2287 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2289 for ( i
= pos
; i
< m_numRows
; i
++ )
2291 bottom
+= m_rowHeights
[i
];
2292 m_rowBottoms
[i
] = bottom
;
2298 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2300 int numRows
= msg
.GetCommandInt();
2301 for ( i
= 0; i
< numRows
; i
++ )
2303 m_rowHeights
.Add( m_defaultRowHeight
);
2304 m_rowBottoms
.Add( 0 );
2307 int oldNumRows
= m_numRows
;
2308 m_numRows
+= numRows
;
2311 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2313 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2315 bottom
+= m_rowHeights
[i
];
2316 m_rowBottoms
[i
] = bottom
;
2322 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2324 size_t pos
= msg
.GetCommandInt();
2325 int numRows
= msg
.GetCommandInt2();
2326 for ( i
= 0; i
< numRows
; i
++ )
2328 m_rowHeights
.Remove( pos
);
2329 m_rowBottoms
.Remove( pos
);
2331 m_numRows
-= numRows
;
2336 m_colWidths
.Clear();
2337 m_colRights
.Clear();
2338 m_currentCellCoords
= wxGridNoCellCoords
;
2342 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2343 m_currentCellCoords
.Set( 0, 0 );
2346 for ( i
= 0; i
< m_numRows
; i
++ )
2348 h
+= m_rowHeights
[i
];
2349 m_rowBottoms
[i
] = h
;
2357 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2359 size_t pos
= msg
.GetCommandInt();
2360 int numCols
= msg
.GetCommandInt2();
2361 for ( i
= 0; i
< numCols
; i
++ )
2363 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2364 m_colRights
.Insert( 0, pos
);
2366 m_numCols
+= numCols
;
2369 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2371 for ( i
= pos
; i
< m_numCols
; i
++ )
2373 right
+= m_colWidths
[i
];
2374 m_colRights
[i
] = right
;
2380 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2382 int numCols
= msg
.GetCommandInt();
2383 for ( i
= 0; i
< numCols
; i
++ )
2385 m_colWidths
.Add( m_defaultColWidth
);
2386 m_colRights
.Add( 0 );
2389 int oldNumCols
= m_numCols
;
2390 m_numCols
+= numCols
;
2393 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2395 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2397 right
+= m_colWidths
[i
];
2398 m_colRights
[i
] = right
;
2404 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2406 size_t pos
= msg
.GetCommandInt();
2407 int numCols
= msg
.GetCommandInt2();
2408 for ( i
= 0; i
< numCols
; i
++ )
2410 m_colWidths
.Remove( pos
);
2411 m_colRights
.Remove( pos
);
2413 m_numCols
-= numCols
;
2417 #if 0 // leave the row alone here so that AppendCols will work subsequently
2419 m_rowHeights
.Clear();
2420 m_rowBottoms
.Clear();
2422 m_currentCellCoords
= wxGridNoCellCoords
;
2426 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2427 m_currentCellCoords
.Set( 0, 0 );
2430 for ( i
= 0; i
< m_numCols
; i
++ )
2432 w
+= m_colWidths
[i
];
2445 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2447 wxRegionIterator
iter( reg
);
2450 m_rowLabelsExposed
.Empty();
2457 // TODO: remove this when we can...
2458 // There is a bug in wxMotif that gives garbage update
2459 // rectangles if you jump-scroll a long way by clicking the
2460 // scrollbar with middle button. This is a work-around
2462 #if defined(__WXMOTIF__)
2464 m_gridWin
->GetClientSize( &cw
, &ch
);
2465 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2466 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2469 // logical bounds of update region
2472 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2473 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2475 // find the row labels within these bounds
2479 for ( row
= 0; row
< m_numRows
; row
++ )
2481 if ( m_rowBottoms
[row
] < top
) continue;
2483 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2484 if ( rowTop
> bottom
) break;
2486 m_rowLabelsExposed
.Add( row
);
2494 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2496 wxRegionIterator
iter( reg
);
2499 m_colLabelsExposed
.Empty();
2506 // TODO: remove this when we can...
2507 // There is a bug in wxMotif that gives garbage update
2508 // rectangles if you jump-scroll a long way by clicking the
2509 // scrollbar with middle button. This is a work-around
2511 #if defined(__WXMOTIF__)
2513 m_gridWin
->GetClientSize( &cw
, &ch
);
2514 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2515 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2518 // logical bounds of update region
2521 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2522 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2524 // find the cells within these bounds
2528 for ( col
= 0; col
< m_numCols
; col
++ )
2530 if ( m_colRights
[col
] < left
) continue;
2532 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2533 if ( colLeft
> right
) break;
2535 m_colLabelsExposed
.Add( col
);
2543 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2545 wxRegionIterator
iter( reg
);
2548 m_cellsExposed
.Empty();
2549 m_rowsExposed
.Empty();
2550 m_colsExposed
.Empty();
2552 int left
, top
, right
, bottom
;
2557 // TODO: remove this when we can...
2558 // There is a bug in wxMotif that gives garbage update
2559 // rectangles if you jump-scroll a long way by clicking the
2560 // scrollbar with middle button. This is a work-around
2562 #if defined(__WXMOTIF__)
2564 m_gridWin
->GetClientSize( &cw
, &ch
);
2565 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2566 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2567 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2568 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2571 // logical bounds of update region
2573 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2574 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2576 // find the cells within these bounds
2579 int colLeft
, rowTop
;
2580 for ( row
= 0; row
< m_numRows
; row
++ )
2582 if ( m_rowBottoms
[row
] <= top
) continue;
2584 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2585 if ( rowTop
> bottom
) break;
2587 m_rowsExposed
.Add( row
);
2589 for ( col
= 0; col
< m_numCols
; col
++ )
2591 if ( m_colRights
[col
] <= left
) continue;
2593 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2594 if ( colLeft
> right
) break;
2596 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2597 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2606 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2609 wxPoint
pos( event
.GetPosition() );
2610 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2612 if ( event
.Dragging() )
2614 m_isDragging
= TRUE
;
2616 if ( event
.LeftIsDown() )
2618 switch( m_cursorMode
)
2620 case WXGRID_CURSOR_RESIZE_ROW
:
2622 int cw
, ch
, left
, dummy
;
2623 m_gridWin
->GetClientSize( &cw
, &ch
);
2624 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2626 wxClientDC
dc( m_gridWin
);
2629 m_rowBottoms
[m_dragRowOrCol
] -
2630 m_rowHeights
[m_dragRowOrCol
] +
2631 WXGRID_MIN_ROW_HEIGHT
);
2632 dc
.SetLogicalFunction(wxINVERT
);
2633 if ( m_dragLastPos
>= 0 )
2635 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2637 dc
.DrawLine( left
, y
, left
+cw
, y
);
2642 case WXGRID_CURSOR_SELECT_ROW
:
2643 if ( (row
= YToRow( y
)) >= 0 &&
2644 !IsInSelection( row
, 0 ) )
2646 SelectRow( row
, TRUE
);
2649 // default label to suppress warnings about "enumeration value
2650 // 'xxx' not handled in switch
2658 m_isDragging
= FALSE
;
2661 // ------------ Entering or leaving the window
2663 if ( event
.Entering() || event
.Leaving() )
2665 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2669 // ------------ Left button pressed
2671 else if ( event
.LeftDown() )
2673 // don't send a label click event for a hit on the
2674 // edge of the row label - this is probably the user
2675 // wanting to resize the row
2677 if ( YToEdgeOfRow(y
) < 0 )
2681 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2683 SelectRow( row
, event
.ShiftDown() );
2684 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2689 // starting to drag-resize a row
2691 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2696 // ------------ Left double click
2698 else if (event
.LeftDClick() )
2700 if ( YToEdgeOfRow(y
) < 0 )
2703 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2708 // ------------ Left button released
2710 else if ( event
.LeftUp() )
2712 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2714 DoEndDragResizeRow();
2716 // Note: we are ending the event *after* doing
2717 // default processing in this case
2719 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2722 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2727 // ------------ Right button down
2729 else if ( event
.RightDown() )
2732 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2734 // no default action at the moment
2739 // ------------ Right double click
2741 else if ( event
.RightDClick() )
2744 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2746 // no default action at the moment
2751 // ------------ No buttons down and mouse moving
2753 else if ( event
.Moving() )
2755 m_dragRowOrCol
= YToEdgeOfRow( y
);
2756 if ( m_dragRowOrCol
>= 0 )
2758 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2760 // don't capture the mouse yet
2761 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2764 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2766 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2772 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2775 wxPoint
pos( event
.GetPosition() );
2776 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2778 if ( event
.Dragging() )
2780 m_isDragging
= TRUE
;
2782 if ( event
.LeftIsDown() )
2784 switch( m_cursorMode
)
2786 case WXGRID_CURSOR_RESIZE_COL
:
2788 int cw
, ch
, dummy
, top
;
2789 m_gridWin
->GetClientSize( &cw
, &ch
);
2790 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2792 wxClientDC
dc( m_gridWin
);
2795 m_colRights
[m_dragRowOrCol
] -
2796 m_colWidths
[m_dragRowOrCol
] +
2797 WXGRID_MIN_COL_WIDTH
);
2798 dc
.SetLogicalFunction(wxINVERT
);
2799 if ( m_dragLastPos
>= 0 )
2801 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2803 dc
.DrawLine( x
, top
, x
, top
+ch
);
2808 case WXGRID_CURSOR_SELECT_COL
:
2809 if ( (col
= XToCol( x
)) >= 0 &&
2810 !IsInSelection( 0, col
) )
2812 SelectCol( col
, TRUE
);
2815 // default label to suppress warnings about "enumeration value
2816 // 'xxx' not handled in switch
2824 m_isDragging
= FALSE
;
2827 // ------------ Entering or leaving the window
2829 if ( event
.Entering() || event
.Leaving() )
2831 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2835 // ------------ Left button pressed
2837 else if ( event
.LeftDown() )
2839 // don't send a label click event for a hit on the
2840 // edge of the col label - this is probably the user
2841 // wanting to resize the col
2843 if ( XToEdgeOfCol(x
) < 0 )
2847 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2849 SelectCol( col
, event
.ShiftDown() );
2850 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2855 // starting to drag-resize a col
2857 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2862 // ------------ Left double click
2864 if ( event
.LeftDClick() )
2866 if ( XToEdgeOfCol(x
) < 0 )
2869 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2874 // ------------ Left button released
2876 else if ( event
.LeftUp() )
2878 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2880 DoEndDragResizeCol();
2882 // Note: we are ending the event *after* doing
2883 // default processing in this case
2885 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2888 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2893 // ------------ Right button down
2895 else if ( event
.RightDown() )
2898 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2900 // no default action at the moment
2905 // ------------ Right double click
2907 else if ( event
.RightDClick() )
2910 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2912 // no default action at the moment
2917 // ------------ No buttons down and mouse moving
2919 else if ( event
.Moving() )
2921 m_dragRowOrCol
= XToEdgeOfCol( x
);
2922 if ( m_dragRowOrCol
>= 0 )
2924 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2926 // don't capture the cursor yet
2927 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2930 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2932 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2938 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2940 if ( event
.LeftDown() )
2942 // indicate corner label by having both row and
2945 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2951 else if ( event
.LeftDClick() )
2953 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2956 else if ( event
.RightDown() )
2958 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2960 // no default action at the moment
2964 else if ( event
.RightDClick() )
2966 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2968 // no default action at the moment
2973 void wxGrid::ChangeCursorMode(CursorMode mode
,
2978 static const wxChar
*cursorModes
[] =
2987 wxLogTrace(_T("grid"),
2988 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2989 win
== m_colLabelWin
? _T("colLabelWin")
2990 : win
? _T("rowLabelWin")
2992 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2993 #endif // __WXDEBUG__
2995 if ( mode
== m_cursorMode
)
3000 // by default use the grid itself
3006 m_winCapture
->ReleaseMouse();
3007 m_winCapture
= (wxWindow
*)NULL
;
3010 m_cursorMode
= mode
;
3012 switch ( m_cursorMode
)
3014 case WXGRID_CURSOR_RESIZE_ROW
:
3015 win
->SetCursor( m_rowResizeCursor
);
3018 case WXGRID_CURSOR_RESIZE_COL
:
3019 win
->SetCursor( m_colResizeCursor
);
3023 win
->SetCursor( *wxSTANDARD_CURSOR
);
3026 // we need to capture mouse when resizing
3027 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3028 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3030 if ( captureMouse
&& resize
)
3032 win
->CaptureMouse();
3037 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3040 wxPoint
pos( event
.GetPosition() );
3041 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3043 wxGridCellCoords coords
;
3044 XYToCell( x
, y
, coords
);
3046 if ( event
.Dragging() )
3048 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3050 // Don't start doing anything until the mouse has been drug at
3051 // least 3 pixels in any direction...
3054 if (m_startDragPos
== wxDefaultPosition
)
3056 m_startDragPos
= pos
;
3059 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3063 m_isDragging
= TRUE
;
3064 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3066 // Hide the edit control, so it
3067 // won't interfer with drag-shrinking.
3068 if ( IsCellEditControlEnabled() )
3069 HideCellEditControl();
3071 // Have we captured the mouse yet?
3074 m_winCapture
= m_gridWin
;
3075 m_winCapture
->CaptureMouse();
3078 if ( coords
!= wxGridNoCellCoords
)
3080 if ( !IsSelection() )
3082 SelectBlock( coords
, coords
);
3086 SelectBlock( m_currentCellCoords
, coords
);
3089 if (! IsVisible(coords
))
3091 MakeCellVisible(coords
);
3092 // TODO: need to introduce a delay or something here. The
3093 // scrolling is way to fast, at least on MSW.
3097 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3099 int cw
, ch
, left
, dummy
;
3100 m_gridWin
->GetClientSize( &cw
, &ch
);
3101 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3103 wxClientDC
dc( m_gridWin
);
3106 m_rowBottoms
[m_dragRowOrCol
] -
3107 m_rowHeights
[m_dragRowOrCol
] +
3108 WXGRID_MIN_ROW_HEIGHT
);
3109 dc
.SetLogicalFunction(wxINVERT
);
3110 if ( m_dragLastPos
>= 0 )
3112 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3114 dc
.DrawLine( left
, y
, left
+cw
, y
);
3117 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3119 int cw
, ch
, dummy
, top
;
3120 m_gridWin
->GetClientSize( &cw
, &ch
);
3121 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3123 wxClientDC
dc( m_gridWin
);
3126 m_colRights
[m_dragRowOrCol
] -
3127 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
3128 dc
.SetLogicalFunction(wxINVERT
);
3129 if ( m_dragLastPos
>= 0 )
3131 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3133 dc
.DrawLine( x
, top
, x
, top
+ch
);
3140 m_isDragging
= FALSE
;
3141 m_startDragPos
= wxDefaultPosition
;
3144 if ( coords
!= wxGridNoCellCoords
)
3146 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3147 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3150 if ( event
.Entering() || event
.Leaving() )
3152 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3153 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3158 // ------------ Left button pressed
3160 if ( event
.LeftDown() )
3162 DisableCellEditControl();
3163 if ( event
.ShiftDown() )
3165 SelectBlock( m_currentCellCoords
, coords
);
3167 else if ( XToEdgeOfCol(x
) < 0 &&
3168 YToEdgeOfRow(y
) < 0 )
3170 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3175 MakeCellVisible( coords
);
3177 // if this is the second click on this cell then start
3179 if ( m_waitForSlowClick
&&
3180 (coords
== m_currentCellCoords
) &&
3181 CanEnableCellControl())
3183 EnableCellEditControl();
3185 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3186 attr
->GetEditor()->StartingClick();
3189 m_waitForSlowClick
= FALSE
;
3193 SetCurrentCell( coords
);
3194 m_waitForSlowClick
= TRUE
;
3201 // ------------ Left double click
3203 else if ( event
.LeftDClick() )
3205 DisableCellEditControl();
3206 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3208 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3216 // ------------ Left button released
3218 else if ( event
.LeftUp() )
3220 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3222 if ( IsSelection() )
3226 m_winCapture
->ReleaseMouse();
3227 m_winCapture
= NULL
;
3229 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3232 // Show the edit control, if it has been hidden for
3234 ShowCellEditControl();
3236 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3238 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3239 DoEndDragResizeRow();
3241 // Note: we are ending the event *after* doing
3242 // default processing in this case
3244 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3246 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3248 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3249 DoEndDragResizeCol();
3251 // Note: we are ending the event *after* doing
3252 // default processing in this case
3254 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3261 // ------------ Right button down
3263 else if ( event
.RightDown() )
3265 DisableCellEditControl();
3266 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3271 // no default action at the moment
3276 // ------------ Right double click
3278 else if ( event
.RightDClick() )
3280 DisableCellEditControl();
3281 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3286 // no default action at the moment
3290 // ------------ Moving and no button action
3292 else if ( event
.Moving() && !event
.IsButton() )
3294 int dragRow
= YToEdgeOfRow( y
);
3295 int dragCol
= XToEdgeOfCol( x
);
3297 // Dragging on the corner of a cell to resize in both
3298 // directions is not implemented yet...
3300 if ( dragRow
>= 0 && dragCol
>= 0 )
3302 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3308 m_dragRowOrCol
= dragRow
;
3310 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3312 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3320 m_dragRowOrCol
= dragCol
;
3322 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3324 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3330 // Neither on a row or col edge
3332 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3334 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3341 void wxGrid::DoEndDragResizeRow()
3343 if ( m_dragLastPos
>= 0 )
3345 // erase the last line and resize the row
3347 int cw
, ch
, left
, dummy
;
3348 m_gridWin
->GetClientSize( &cw
, &ch
);
3349 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3351 wxClientDC
dc( m_gridWin
);
3353 dc
.SetLogicalFunction( wxINVERT
);
3354 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3355 HideCellEditControl();
3357 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3358 SetRowSize( m_dragRowOrCol
,
3359 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3361 if ( !GetBatchCount() )
3363 // Only needed to get the correct rect.y:
3364 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3366 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3367 rect
.width
= m_rowLabelWidth
;
3368 rect
.height
= ch
- rect
.y
;
3369 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3371 m_gridWin
->Refresh( FALSE
, &rect
);
3374 ShowCellEditControl();
3379 void wxGrid::DoEndDragResizeCol()
3381 if ( m_dragLastPos
>= 0 )
3383 // erase the last line and resize the col
3385 int cw
, ch
, dummy
, top
;
3386 m_gridWin
->GetClientSize( &cw
, &ch
);
3387 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3389 wxClientDC
dc( m_gridWin
);
3391 dc
.SetLogicalFunction( wxINVERT
);
3392 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3393 HideCellEditControl();
3395 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3396 SetColSize( m_dragRowOrCol
,
3397 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3399 if ( !GetBatchCount() )
3401 // Only needed to get the correct rect.x:
3402 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3404 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3405 rect
.width
= cw
- rect
.x
;
3406 rect
.height
= m_colLabelHeight
;
3407 m_colLabelWin
->Refresh( TRUE
, &rect
);
3409 m_gridWin
->Refresh( FALSE
, &rect
);
3412 ShowCellEditControl();
3419 // ------ interaction with data model
3421 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3423 switch ( msg
.GetId() )
3425 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3426 return GetModelValues();
3428 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3429 return SetModelValues();
3431 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3432 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3433 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3434 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3435 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3436 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3437 return Redimension( msg
);
3446 // The behaviour of this function depends on the grid table class
3447 // Clear() function. For the default wxGridStringTable class the
3448 // behavious is to replace all cell contents with wxEmptyString but
3449 // not to change the number of rows or cols.
3451 void wxGrid::ClearGrid()
3456 SetEditControlValue();
3457 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3462 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3464 // TODO: something with updateLabels flag
3468 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3474 if (IsCellEditControlEnabled())
3475 DisableCellEditControl();
3477 bool ok
= m_table
->InsertRows( pos
, numRows
);
3479 // the table will have sent the results of the insert row
3480 // operation to this view object as a grid table message
3484 if ( m_numCols
== 0 )
3486 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3488 // TODO: perhaps instead of appending the default number of cols
3489 // we should remember what the last non-zero number of cols was ?
3493 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3495 // if we have just inserted cols into an empty grid the current
3496 // cell will be undefined...
3498 SetCurrentCell( 0, 0 );
3502 if ( !GetBatchCount() ) Refresh();
3505 SetEditControlValue();
3515 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3517 // TODO: something with updateLabels flag
3521 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3525 if ( m_table
&& m_table
->AppendRows( numRows
) )
3527 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3529 // if we have just inserted cols into an empty grid the current
3530 // cell will be undefined...
3532 SetCurrentCell( 0, 0 );
3535 // the table will have sent the results of the append row
3536 // operation to this view object as a grid table message
3539 if ( !GetBatchCount() ) Refresh();
3549 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3551 // TODO: something with updateLabels flag
3555 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3561 if (IsCellEditControlEnabled())
3562 DisableCellEditControl();
3564 if (m_table
->DeleteRows( pos
, numRows
))
3567 // the table will have sent the results of the delete row
3568 // operation to this view object as a grid table message
3571 if ( !GetBatchCount() ) Refresh();
3579 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3581 // TODO: something with updateLabels flag
3585 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3591 if (IsCellEditControlEnabled())
3592 DisableCellEditControl();
3594 bool ok
= m_table
->InsertCols( pos
, numCols
);
3596 // the table will have sent the results of the insert col
3597 // operation to this view object as a grid table message
3601 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3603 // if we have just inserted cols into an empty grid the current
3604 // cell will be undefined...
3606 SetCurrentCell( 0, 0 );
3610 if ( !GetBatchCount() ) Refresh();
3613 SetEditControlValue();
3623 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3625 // TODO: something with updateLabels flag
3629 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3633 if ( m_table
&& m_table
->AppendCols( numCols
) )
3635 // the table will have sent the results of the append col
3636 // operation to this view object as a grid table message
3638 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3640 // if we have just inserted cols into an empty grid the current
3641 // cell will be undefined...
3643 SetCurrentCell( 0, 0 );
3647 if ( !GetBatchCount() ) Refresh();
3657 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3659 // TODO: something with updateLabels flag
3663 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3669 if (IsCellEditControlEnabled())
3670 DisableCellEditControl();
3672 if ( m_table
->DeleteCols( pos
, numCols
) )
3674 // the table will have sent the results of the delete col
3675 // operation to this view object as a grid table message
3678 if ( !GetBatchCount() ) Refresh();
3688 // ----- event handlers
3691 // Generate a grid event based on a mouse event and
3692 // return the result of ProcessEvent()
3694 bool wxGrid::SendEvent( const wxEventType type
,
3696 wxMouseEvent
& mouseEv
)
3698 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3700 int rowOrCol
= (row
== -1 ? col
: row
);
3702 wxGridSizeEvent
gridEvt( GetId(),
3706 mouseEv
.GetX(), mouseEv
.GetY(),
3707 mouseEv
.ControlDown(),
3708 mouseEv
.ShiftDown(),
3710 mouseEv
.MetaDown() );
3712 return GetEventHandler()->ProcessEvent(gridEvt
);
3714 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3716 wxGridRangeSelectEvent
gridEvt( GetId(),
3720 m_selectedBottomRight
,
3721 mouseEv
.ControlDown(),
3722 mouseEv
.ShiftDown(),
3724 mouseEv
.MetaDown() );
3726 return GetEventHandler()->ProcessEvent(gridEvt
);
3730 wxGridEvent
gridEvt( GetId(),
3734 mouseEv
.GetX(), mouseEv
.GetY(),
3735 mouseEv
.ControlDown(),
3736 mouseEv
.ShiftDown(),
3738 mouseEv
.MetaDown() );
3740 return GetEventHandler()->ProcessEvent(gridEvt
);
3745 // Generate a grid event of specified type and return the result
3746 // of ProcessEvent().
3748 bool wxGrid::SendEvent( const wxEventType type
,
3751 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3753 int rowOrCol
= (row
== -1 ? col
: row
);
3755 wxGridSizeEvent
gridEvt( GetId(),
3760 return GetEventHandler()->ProcessEvent(gridEvt
);
3764 wxGridEvent
gridEvt( GetId(),
3769 return GetEventHandler()->ProcessEvent(gridEvt
);
3774 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3776 wxPaintDC
dc( this );
3778 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3779 m_numRows
&& m_numCols
)
3781 m_currentCellCoords
.Set(0, 0);
3782 SetEditControlValue();
3783 ShowCellEditControl();
3790 // This is just here to make sure that CalcDimensions gets called when
3791 // the grid view is resized... then the size event is skipped to allow
3792 // the box sizers to handle everything
3794 void wxGrid::OnSize( wxSizeEvent
& event
)
3801 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3803 if ( m_inOnKeyDown
)
3805 // shouldn't be here - we are going round in circles...
3807 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3810 m_inOnKeyDown
= TRUE
;
3812 // propagate the event up and see if it gets processed
3814 wxWindow
*parent
= GetParent();
3815 wxKeyEvent
keyEvt( event
);
3816 keyEvt
.SetEventObject( parent
);
3818 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3821 // TODO: Should also support Shift-cursor keys for
3822 // extending the selection. Maybe add a flag to
3823 // MoveCursorXXX() and MoveCursorXXXBlock() and
3824 // just send event.ShiftDown().
3826 // try local handlers
3828 switch ( event
.KeyCode() )
3831 if ( event
.ControlDown() )
3833 MoveCursorUpBlock();
3842 if ( event
.ControlDown() )
3844 MoveCursorDownBlock();
3853 if ( event
.ControlDown() )
3855 MoveCursorLeftBlock();
3864 if ( event
.ControlDown() )
3866 MoveCursorRightBlock();
3875 if ( event
.ControlDown() )
3877 event
.Skip(); // to let the edit control have the return
3886 if (event
.ShiftDown())
3893 if ( event
.ControlDown() )
3895 MakeCellVisible( 0, 0 );
3896 SetCurrentCell( 0, 0 );
3905 if ( event
.ControlDown() )
3907 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3908 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3924 // We don't want these keys to trigger the edit control, any others?
3933 if ( !IsEditable() )
3938 // Otherwise fall through to default
3941 // now try the cell edit control
3943 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
3945 EnableCellEditControl();
3946 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3947 attr
->GetEditor()->StartingKey(event
);
3954 m_inOnKeyDown
= FALSE
;
3958 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3962 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3964 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3966 // the event has been intercepted - do nothing
3971 m_currentCellCoords
!= wxGridNoCellCoords
)
3973 HideCellEditControl();
3974 SaveEditControlValue();
3975 DisableCellEditControl();
3977 // Clear the old current cell highlight
3978 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3980 // Otherwise refresh redraws the highlight!
3981 m_currentCellCoords
= coords
;
3983 m_gridWin
->Refresh( FALSE
, &r
);
3986 m_currentCellCoords
= coords
;
3988 SetEditControlValue();
3992 wxClientDC
dc(m_gridWin
);
3995 wxGridCellAttr
* attr
= GetCellAttr(coords
);
3996 DrawCellHighlight(dc
, attr
);
3999 if ( IsSelection() )
4001 wxRect
r( SelectionToDeviceRect() );
4003 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4010 // ------ functions to get/send data (see also public functions)
4013 bool wxGrid::GetModelValues()
4017 // all we need to do is repaint the grid
4019 m_gridWin
->Refresh();
4027 bool wxGrid::SetModelValues()
4033 for ( row
= 0; row
< m_numRows
; row
++ )
4035 for ( col
= 0; col
< m_numCols
; col
++ )
4037 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4049 // Note - this function only draws cells that are in the list of
4050 // exposed cells (usually set from the update region by
4051 // CalcExposedCells)
4053 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4055 if ( !m_numRows
|| !m_numCols
) return;
4058 size_t numCells
= m_cellsExposed
.GetCount();
4060 for ( i
= 0; i
< numCells
; i
++ )
4062 DrawCell( dc
, m_cellsExposed
[i
] );
4067 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4069 int row
= coords
.GetRow();
4070 int col
= coords
.GetCol();
4072 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4075 // we draw the cell border ourselves
4076 #if !WXGRID_DRAW_LINES
4077 if ( m_gridLinesEnabled
)
4078 DrawCellBorder( dc
, coords
);
4081 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4083 bool isCurrent
= coords
== m_currentCellCoords
;
4086 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4087 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4088 rect
.width
= m_colWidths
[col
] - 1;
4089 rect
.height
= m_rowHeights
[row
] - 1;
4091 // if the editor is shown, we should use it and not the renderer
4092 if ( isCurrent
&& IsCellEditControlEnabled() )
4094 attr
->GetEditor()->PaintBackground(rect
, attr
);
4098 // but all the rest is drawn by the cell renderer and hence may be
4100 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4104 DrawCellHighlight(dc
, attr
);
4111 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4113 int row
= m_currentCellCoords
.GetRow();
4114 int col
= m_currentCellCoords
.GetCol();
4116 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4120 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4121 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4122 rect
.width
= m_colWidths
[col
] - 1;
4123 rect
.height
= m_rowHeights
[row
] - 1;
4125 if ( attr
->IsReadOnly() )
4127 // hmmm... what could we do here to show that the cell is disabled?
4128 // for now, I just draw a thinner border than for the other ones, but
4129 // it doesn't look really good
4130 dc
.SetPen(wxPen(m_gridLineColour
, 2, wxSOLID
));
4131 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4133 dc
.DrawRectangle(rect
);
4137 // VZ: my experiments with 3d borders...
4139 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
4140 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4142 dc
.DrawRectangle(rect
);
4144 // FIXME we should properly set colours for arbitrary bg
4145 wxCoord x1
= rect
.x
,
4147 x2
= rect
.x
+ rect
.width
,
4148 y2
= rect
.y
+ rect
.height
;
4150 dc
.SetPen(*wxWHITE_PEN
);
4151 dc
.DrawLine(x1
, y1
, x2
- 1, y1
);
4152 dc
.DrawLine(x1
, y1
, x1
, y2
- 1);
4154 dc
.SetPen(*wxLIGHT_GREY_PEN
);
4155 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4156 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
- 1);
4158 dc
.SetPen(*wxBLACK_PEN
);
4159 dc
.DrawLine(x1
, y2
, x2
, y2
);
4160 dc
.DrawLine(x2
, y1
, x2
, y2
);
4165 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4167 if ( m_colWidths
[coords
.GetCol()] <=0 ||
4168 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
4170 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4171 int row
= coords
.GetRow();
4172 int col
= coords
.GetCol();
4174 // right hand border
4176 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
4177 m_colRights
[col
], m_rowBottoms
[row
] );
4181 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
4182 m_colRights
[col
], m_rowBottoms
[row
] );
4186 // TODO: remove this ???
4187 // This is used to redraw all grid lines e.g. when the grid line colour
4190 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4192 if ( !m_gridLinesEnabled
||
4194 !m_numCols
) return;
4196 int top
, bottom
, left
, right
;
4201 m_gridWin
->GetClientSize(&cw
, &ch
);
4203 // virtual coords of visible area
4205 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4206 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4211 reg
.GetBox(x
, y
, w
, h
);
4212 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4213 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4216 // avoid drawing grid lines past the last row and col
4218 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
4219 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
4221 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4223 // horizontal grid lines
4226 for ( i
= 0; i
< m_numRows
; i
++ )
4228 if ( m_rowBottoms
[i
]-1 > bottom
)
4232 else if ( m_rowBottoms
[i
]-1 >= top
)
4234 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
4239 // vertical grid lines
4241 for ( i
= 0; i
< m_numCols
; i
++ )
4243 if ( m_colRights
[i
]-1 > right
)
4247 else if ( m_colRights
[i
]-1 >= left
)
4249 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
4255 void wxGrid::DrawRowLabels( wxDC
& dc
)
4257 if ( !m_numRows
|| !m_numCols
) return;
4260 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4262 for ( i
= 0; i
< numLabels
; i
++ )
4264 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4269 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4271 if ( m_rowHeights
[row
] <= 0 ) return;
4273 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4275 dc
.SetPen( *wxBLACK_PEN
);
4276 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4277 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4279 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4280 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4282 dc
.SetPen( *wxWHITE_PEN
);
4283 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4284 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4286 dc
.SetBackgroundMode( wxTRANSPARENT
);
4287 dc
.SetTextForeground( GetLabelTextColour() );
4288 dc
.SetFont( GetLabelFont() );
4291 GetRowLabelAlignment( &hAlign
, &vAlign
);
4295 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4296 rect
.SetWidth( m_rowLabelWidth
- 4 );
4297 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4298 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4302 void wxGrid::DrawColLabels( wxDC
& dc
)
4304 if ( !m_numRows
|| !m_numCols
) return;
4307 size_t numLabels
= m_colLabelsExposed
.GetCount();
4309 for ( i
= 0; i
< numLabels
; i
++ )
4311 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4316 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4318 if ( m_colWidths
[col
] <= 0 ) return;
4320 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4322 dc
.SetPen( *wxBLACK_PEN
);
4323 dc
.DrawLine( m_colRights
[col
]-1, 0,
4324 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4326 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4327 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4329 dc
.SetPen( *wxWHITE_PEN
);
4330 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4331 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4333 dc
.SetBackgroundMode( wxTRANSPARENT
);
4334 dc
.SetTextForeground( GetLabelTextColour() );
4335 dc
.SetFont( GetLabelFont() );
4337 dc
.SetBackgroundMode( wxTRANSPARENT
);
4338 dc
.SetTextForeground( GetLabelTextColour() );
4339 dc
.SetFont( GetLabelFont() );
4342 GetColLabelAlignment( &hAlign
, &vAlign
);
4345 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4347 rect
.SetWidth( m_colWidths
[col
] - 4 );
4348 rect
.SetHeight( m_colLabelHeight
- 4 );
4349 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4353 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4354 const wxString
& value
,
4359 long textWidth
, textHeight
;
4360 long lineWidth
, lineHeight
;
4361 wxArrayString lines
;
4363 dc
.SetClippingRegion( rect
);
4364 StringToLines( value
, lines
);
4365 if ( lines
.GetCount() )
4367 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4368 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4371 switch ( horizAlign
)
4374 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4378 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4387 switch ( vertAlign
)
4390 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4394 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4403 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4405 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4410 dc
.DestroyClippingRegion();
4414 // Split multi line text up into an array of strings. Any existing
4415 // contents of the string array are preserved.
4417 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4421 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4422 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4424 while ( startPos
< (int)tVal
.Length() )
4426 pos
= tVal
.Mid(startPos
).Find( eol
);
4431 else if ( pos
== 0 )
4433 lines
.Add( wxEmptyString
);
4437 lines
.Add( value
.Mid(startPos
, pos
) );
4441 if ( startPos
< (int)value
.Length() )
4443 lines
.Add( value
.Mid( startPos
) );
4448 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4449 wxArrayString
& lines
,
4450 long *width
, long *height
)
4457 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4459 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4460 w
= wxMax( w
, lineW
);
4470 // ------ Edit control functions
4474 void wxGrid::EnableEditing( bool edit
)
4476 // TODO: improve this ?
4478 if ( edit
!= m_editable
)
4482 // FIXME IMHO this won't disable the edit control if edit == FALSE
4483 // because of the check in the beginning of
4484 // EnableCellEditControl() just below (VZ)
4485 EnableCellEditControl(m_editable
);
4490 void wxGrid::EnableCellEditControl( bool enable
)
4495 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4496 SetCurrentCell( 0, 0 );
4498 if ( enable
!= m_cellEditCtrlEnabled
)
4500 // TODO allow the app to Veto() this event?
4501 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4505 // this should be checked by the caller!
4506 wxASSERT_MSG( CanEnableCellControl(),
4507 _T("can't enable editing for this cell!") );
4509 // do it before ShowCellEditControl()
4510 m_cellEditCtrlEnabled
= enable
;
4512 SetEditControlValue();
4513 ShowCellEditControl();
4517 HideCellEditControl();
4518 SaveEditControlValue();
4520 // do it after HideCellEditControl()
4521 m_cellEditCtrlEnabled
= enable
;
4526 bool wxGrid::IsCurrentCellReadOnly() const
4529 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4530 bool readonly
= attr
->IsReadOnly();
4536 bool wxGrid::CanEnableCellControl() const
4538 return m_editable
&& !IsCurrentCellReadOnly();
4541 bool wxGrid::IsCellEditControlEnabled() const
4543 // the cell edit control might be disable for all cells or just for the
4544 // current one if it's read only
4545 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
4548 wxWindow
*wxGrid::GetGridWindow() const
4553 void wxGrid::ShowCellEditControl()
4555 if ( IsCellEditControlEnabled() )
4557 if ( !IsVisible( m_currentCellCoords
) )
4563 wxRect rect
= CellToRect( m_currentCellCoords
);
4564 int row
= m_currentCellCoords
.GetRow();
4565 int col
= m_currentCellCoords
.GetCol();
4567 // convert to scrolled coords
4569 int left
, top
, right
, bottom
;
4570 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4571 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4573 // cell is shifted by one pixel
4579 // Make the edit control large enough to allow for internal
4582 // TODO: remove this if the text ctrl sizing is improved esp. for
4586 #if defined(__WXMOTIF__)
4587 if ( row
== 0 || col
== 0 )
4596 if ( row
== 0 || col
== 0 )
4606 #if defined(__WXGTK__)
4609 if (left
!= 0) left_diff
++;
4610 if (top
!= 0) top_diff
++;
4611 rect
.SetLeft( left
+ left_diff
);
4612 rect
.SetTop( top
+ top_diff
);
4613 rect
.SetRight( rect
.GetRight() - left_diff
);
4614 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4616 rect
.SetLeft( wxMax(0, left
- extra
) );
4617 rect
.SetTop( wxMax(0, top
- extra
) );
4618 rect
.SetRight( rect
.GetRight() + 2*extra
);
4619 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4622 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4623 wxGridCellEditor
* editor
= attr
->GetEditor();
4624 if ( !editor
->IsCreated() )
4626 editor
->Create(m_gridWin
, -1,
4627 new wxGridCellEditorEvtHandler(this, editor
));
4630 editor
->SetSize( rect
);
4631 editor
->Show( TRUE
, attr
);
4632 editor
->BeginEdit(row
, col
, this);
4639 void wxGrid::HideCellEditControl()
4641 if ( IsCellEditControlEnabled() )
4643 int row
= m_currentCellCoords
.GetRow();
4644 int col
= m_currentCellCoords
.GetCol();
4646 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4647 attr
->GetEditor()->Show( FALSE
);
4649 m_gridWin
->SetFocus();
4654 void wxGrid::SetEditControlValue( const wxString
& value
)
4656 // RD: The new Editors get the value from the table themselves now. This
4657 // method can probably be removed...
4661 void wxGrid::SaveEditControlValue()
4663 if ( IsCellEditControlEnabled() )
4665 int row
= m_currentCellCoords
.GetRow();
4666 int col
= m_currentCellCoords
.GetCol();
4668 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4669 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4675 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4676 m_currentCellCoords
.GetRow(),
4677 m_currentCellCoords
.GetCol() );
4684 // ------ Grid location functions
4685 // Note that all of these functions work with the logical coordinates of
4686 // grid cells and labels so you will need to convert from device
4687 // coordinates for mouse events etc.
4690 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4692 int row
= YToRow(y
);
4693 int col
= XToCol(x
);
4695 if ( row
== -1 || col
== -1 )
4697 coords
= wxGridNoCellCoords
;
4701 coords
.Set( row
, col
);
4706 int wxGrid::YToRow( int y
)
4710 for ( i
= 0; i
< m_numRows
; i
++ )
4712 if ( y
< m_rowBottoms
[i
] ) return i
;
4715 return m_numRows
; //-1;
4719 int wxGrid::XToCol( int x
)
4723 for ( i
= 0; i
< m_numCols
; i
++ )
4725 if ( x
< m_colRights
[i
] ) return i
;
4728 return m_numCols
; //-1;
4732 // return the row number that that the y coord is near the edge of, or
4733 // -1 if not near an edge
4735 int wxGrid::YToEdgeOfRow( int y
)
4739 for ( i
= 0; i
< m_numRows
; i
++ )
4741 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4743 d
= abs( y
- m_rowBottoms
[i
] );
4745 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4754 // return the col number that that the x coord is near the edge of, or
4755 // -1 if not near an edge
4757 int wxGrid::XToEdgeOfCol( int x
)
4761 for ( i
= 0; i
< m_numCols
; i
++ )
4763 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4765 d
= abs( x
- m_colRights
[i
] );
4767 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4776 wxRect
wxGrid::CellToRect( int row
, int col
)
4778 wxRect
rect( -1, -1, -1, -1 );
4780 if ( row
>= 0 && row
< m_numRows
&&
4781 col
>= 0 && col
< m_numCols
)
4783 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4784 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4785 rect
.width
= m_colWidths
[col
];
4786 rect
.height
= m_rowHeights
[ row
];
4793 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4795 // get the cell rectangle in logical coords
4797 wxRect
r( CellToRect( row
, col
) );
4799 // convert to device coords
4801 int left
, top
, right
, bottom
;
4802 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4803 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4805 // check against the client area of the grid window
4808 m_gridWin
->GetClientSize( &cw
, &ch
);
4810 if ( wholeCellVisible
)
4812 // is the cell wholly visible ?
4814 return ( left
>= 0 && right
<= cw
&&
4815 top
>= 0 && bottom
<= ch
);
4819 // is the cell partly visible ?
4821 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4822 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4827 // make the specified cell location visible by doing a minimal amount
4830 void wxGrid::MakeCellVisible( int row
, int col
)
4833 int xpos
= -1, ypos
= -1;
4835 if ( row
>= 0 && row
< m_numRows
&&
4836 col
>= 0 && col
< m_numCols
)
4838 // get the cell rectangle in logical coords
4840 wxRect
r( CellToRect( row
, col
) );
4842 // convert to device coords
4844 int left
, top
, right
, bottom
;
4845 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4846 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4849 m_gridWin
->GetClientSize( &cw
, &ch
);
4855 else if ( bottom
> ch
)
4857 int h
= r
.GetHeight();
4859 for ( i
= row
-1; i
>= 0; i
-- )
4861 if ( h
+ m_rowHeights
[i
] > ch
) break;
4863 h
+= m_rowHeights
[i
];
4864 ypos
-= m_rowHeights
[i
];
4867 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4868 // have rounding errors (this is important, because if we do, we
4869 // might not scroll at all and some cells won't be redrawn)
4870 ypos
+= GRID_SCROLL_LINE
/ 2;
4877 else if ( right
> cw
)
4879 int w
= r
.GetWidth();
4881 for ( i
= col
-1; i
>= 0; i
-- )
4883 if ( w
+ m_colWidths
[i
] > cw
) break;
4885 w
+= m_colWidths
[i
];
4886 xpos
-= m_colWidths
[i
];
4889 // see comment for ypos above
4890 xpos
+= GRID_SCROLL_LINE
/ 2;
4893 if ( xpos
!= -1 || ypos
!= -1 )
4895 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4896 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4897 Scroll( xpos
, ypos
);
4905 // ------ Grid cursor movement functions
4908 bool wxGrid::MoveCursorUp()
4910 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4911 m_currentCellCoords
.GetRow() > 0 )
4913 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4914 m_currentCellCoords
.GetCol() );
4916 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4917 m_currentCellCoords
.GetCol() );
4926 bool wxGrid::MoveCursorDown()
4928 // TODO: allow for scrolling
4930 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4931 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4933 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4934 m_currentCellCoords
.GetCol() );
4936 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4937 m_currentCellCoords
.GetCol() );
4946 bool wxGrid::MoveCursorLeft()
4948 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4949 m_currentCellCoords
.GetCol() > 0 )
4951 MakeCellVisible( m_currentCellCoords
.GetRow(),
4952 m_currentCellCoords
.GetCol() - 1 );
4954 SetCurrentCell( m_currentCellCoords
.GetRow(),
4955 m_currentCellCoords
.GetCol() - 1 );
4964 bool wxGrid::MoveCursorRight()
4966 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4967 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4969 MakeCellVisible( m_currentCellCoords
.GetRow(),
4970 m_currentCellCoords
.GetCol() + 1 );
4972 SetCurrentCell( m_currentCellCoords
.GetRow(),
4973 m_currentCellCoords
.GetCol() + 1 );
4982 bool wxGrid::MovePageUp()
4984 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4986 int row
= m_currentCellCoords
.GetRow();
4990 m_gridWin
->GetClientSize( &cw
, &ch
);
4992 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4993 int newRow
= YToRow( y
- ch
+ 1 );
4998 else if ( newRow
== row
)
5003 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5004 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5012 bool wxGrid::MovePageDown()
5014 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5016 int row
= m_currentCellCoords
.GetRow();
5017 if ( row
< m_numRows
)
5020 m_gridWin
->GetClientSize( &cw
, &ch
);
5022 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
5023 int newRow
= YToRow( y
+ ch
);
5026 newRow
= m_numRows
- 1;
5028 else if ( newRow
== row
)
5033 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5034 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5042 bool wxGrid::MoveCursorUpBlock()
5045 m_currentCellCoords
!= wxGridNoCellCoords
&&
5046 m_currentCellCoords
.GetRow() > 0 )
5048 int row
= m_currentCellCoords
.GetRow();
5049 int col
= m_currentCellCoords
.GetCol();
5051 if ( m_table
->IsEmptyCell(row
, col
) )
5053 // starting in an empty cell: find the next block of
5059 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5062 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5064 // starting at the top of a block: find the next block
5070 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5075 // starting within a block: find the top of the block
5080 if ( m_table
->IsEmptyCell(row
, col
) )
5088 MakeCellVisible( row
, col
);
5089 SetCurrentCell( row
, col
);
5097 bool wxGrid::MoveCursorDownBlock()
5100 m_currentCellCoords
!= wxGridNoCellCoords
&&
5101 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5103 int row
= m_currentCellCoords
.GetRow();
5104 int col
= m_currentCellCoords
.GetCol();
5106 if ( m_table
->IsEmptyCell(row
, col
) )
5108 // starting in an empty cell: find the next block of
5111 while ( row
< m_numRows
-1 )
5114 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5117 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5119 // starting at the bottom of a block: find the next block
5122 while ( row
< m_numRows
-1 )
5125 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5130 // starting within a block: find the bottom of the block
5132 while ( row
< m_numRows
-1 )
5135 if ( m_table
->IsEmptyCell(row
, col
) )
5143 MakeCellVisible( row
, col
);
5144 SetCurrentCell( row
, col
);
5152 bool wxGrid::MoveCursorLeftBlock()
5155 m_currentCellCoords
!= wxGridNoCellCoords
&&
5156 m_currentCellCoords
.GetCol() > 0 )
5158 int row
= m_currentCellCoords
.GetRow();
5159 int col
= m_currentCellCoords
.GetCol();
5161 if ( m_table
->IsEmptyCell(row
, col
) )
5163 // starting in an empty cell: find the next block of
5169 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5172 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5174 // starting at the left of a block: find the next block
5180 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5185 // starting within a block: find the left of the block
5190 if ( m_table
->IsEmptyCell(row
, col
) )
5198 MakeCellVisible( row
, col
);
5199 SetCurrentCell( row
, col
);
5207 bool wxGrid::MoveCursorRightBlock()
5210 m_currentCellCoords
!= wxGridNoCellCoords
&&
5211 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5213 int row
= m_currentCellCoords
.GetRow();
5214 int col
= m_currentCellCoords
.GetCol();
5216 if ( m_table
->IsEmptyCell(row
, col
) )
5218 // starting in an empty cell: find the next block of
5221 while ( col
< m_numCols
-1 )
5224 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5227 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5229 // starting at the right of a block: find the next block
5232 while ( col
< m_numCols
-1 )
5235 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5240 // starting within a block: find the right of the block
5242 while ( col
< m_numCols
-1 )
5245 if ( m_table
->IsEmptyCell(row
, col
) )
5253 MakeCellVisible( row
, col
);
5254 SetCurrentCell( row
, col
);
5265 // ------ Label values and formatting
5268 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5270 *horiz
= m_rowLabelHorizAlign
;
5271 *vert
= m_rowLabelVertAlign
;
5274 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5276 *horiz
= m_colLabelHorizAlign
;
5277 *vert
= m_colLabelVertAlign
;
5280 wxString
wxGrid::GetRowLabelValue( int row
)
5284 return m_table
->GetRowLabelValue( row
);
5294 wxString
wxGrid::GetColLabelValue( int col
)
5298 return m_table
->GetColLabelValue( col
);
5309 void wxGrid::SetRowLabelSize( int width
)
5311 width
= wxMax( width
, 0 );
5312 if ( width
!= m_rowLabelWidth
)
5316 m_rowLabelWin
->Show( FALSE
);
5317 m_cornerLabelWin
->Show( FALSE
);
5319 else if ( m_rowLabelWidth
== 0 )
5321 m_rowLabelWin
->Show( TRUE
);
5322 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5325 m_rowLabelWidth
= width
;
5332 void wxGrid::SetColLabelSize( int height
)
5334 height
= wxMax( height
, 0 );
5335 if ( height
!= m_colLabelHeight
)
5339 m_colLabelWin
->Show( FALSE
);
5340 m_cornerLabelWin
->Show( FALSE
);
5342 else if ( m_colLabelHeight
== 0 )
5344 m_colLabelWin
->Show( TRUE
);
5345 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5348 m_colLabelHeight
= height
;
5355 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5357 if ( m_labelBackgroundColour
!= colour
)
5359 m_labelBackgroundColour
= colour
;
5360 m_rowLabelWin
->SetBackgroundColour( colour
);
5361 m_colLabelWin
->SetBackgroundColour( colour
);
5362 m_cornerLabelWin
->SetBackgroundColour( colour
);
5364 if ( !GetBatchCount() )
5366 m_rowLabelWin
->Refresh();
5367 m_colLabelWin
->Refresh();
5368 m_cornerLabelWin
->Refresh();
5373 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5375 if ( m_labelTextColour
!= colour
)
5377 m_labelTextColour
= colour
;
5378 if ( !GetBatchCount() )
5380 m_rowLabelWin
->Refresh();
5381 m_colLabelWin
->Refresh();
5386 void wxGrid::SetLabelFont( const wxFont
& font
)
5389 if ( !GetBatchCount() )
5391 m_rowLabelWin
->Refresh();
5392 m_colLabelWin
->Refresh();
5396 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5398 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5400 m_rowLabelHorizAlign
= horiz
;
5403 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5405 m_rowLabelVertAlign
= vert
;
5408 if ( !GetBatchCount() )
5410 m_rowLabelWin
->Refresh();
5414 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5416 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5418 m_colLabelHorizAlign
= horiz
;
5421 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5423 m_colLabelVertAlign
= vert
;
5426 if ( !GetBatchCount() )
5428 m_colLabelWin
->Refresh();
5432 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5436 m_table
->SetRowLabelValue( row
, s
);
5437 if ( !GetBatchCount() )
5439 wxRect rect
= CellToRect( row
, 0);
5440 if ( rect
.height
> 0 )
5442 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5444 rect
.width
= m_rowLabelWidth
;
5445 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5451 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5455 m_table
->SetColLabelValue( col
, s
);
5456 if ( !GetBatchCount() )
5458 wxRect rect
= CellToRect( 0, col
);
5459 if ( rect
.width
> 0 )
5461 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5463 rect
.height
= m_colLabelHeight
;
5464 m_colLabelWin
->Refresh( TRUE
, &rect
);
5470 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5472 if ( m_gridLineColour
!= colour
)
5474 m_gridLineColour
= colour
;
5476 wxClientDC
dc( m_gridWin
);
5478 DrawAllGridLines( dc
, wxRegion() );
5482 void wxGrid::EnableGridLines( bool enable
)
5484 if ( enable
!= m_gridLinesEnabled
)
5486 m_gridLinesEnabled
= enable
;
5488 if ( !GetBatchCount() )
5492 wxClientDC
dc( m_gridWin
);
5494 DrawAllGridLines( dc
, wxRegion() );
5498 m_gridWin
->Refresh();
5505 int wxGrid::GetDefaultRowSize()
5507 return m_defaultRowHeight
;
5510 int wxGrid::GetRowSize( int row
)
5512 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5514 return m_rowHeights
[row
];
5517 int wxGrid::GetDefaultColSize()
5519 return m_defaultColWidth
;
5522 int wxGrid::GetColSize( int col
)
5524 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5526 return m_colWidths
[col
];
5529 // ============================================================================
5530 // access to the grid attributes: each of them has a default value in the grid
5531 // itself and may be overidden on a per-cell basis
5532 // ============================================================================
5534 // ----------------------------------------------------------------------------
5535 // setting default attributes
5536 // ----------------------------------------------------------------------------
5538 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5540 m_defaultCellAttr
->SetBackgroundColour(col
);
5542 m_gridWin
->SetBackgroundColour(col
);
5546 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5548 m_defaultCellAttr
->SetTextColour(col
);
5551 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5553 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5556 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5558 m_defaultCellAttr
->SetFont(font
);
5561 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5563 m_defaultCellAttr
->SetRenderer(renderer
);
5566 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5568 m_defaultCellAttr
->SetEditor(editor
);
5571 // ----------------------------------------------------------------------------
5572 // access to the default attrbiutes
5573 // ----------------------------------------------------------------------------
5575 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5577 return m_defaultCellAttr
->GetBackgroundColour();
5580 wxColour
wxGrid::GetDefaultCellTextColour()
5582 return m_defaultCellAttr
->GetTextColour();
5585 wxFont
wxGrid::GetDefaultCellFont()
5587 return m_defaultCellAttr
->GetFont();
5590 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5592 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5595 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5597 return m_defaultCellAttr
->GetRenderer();
5600 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5602 return m_defaultCellAttr
->GetEditor();
5605 // ----------------------------------------------------------------------------
5606 // access to cell attributes
5607 // ----------------------------------------------------------------------------
5609 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5611 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5612 wxColour colour
= attr
->GetBackgroundColour();
5617 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5619 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5620 wxColour colour
= attr
->GetTextColour();
5625 wxFont
wxGrid::GetCellFont( int row
, int col
)
5627 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5628 wxFont font
= attr
->GetFont();
5633 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5635 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5636 attr
->GetAlignment(horiz
, vert
);
5640 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5642 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5643 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5648 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5650 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5651 wxGridCellEditor
* editor
= attr
->GetEditor();
5656 bool wxGrid::IsReadOnly(int row
, int col
) const
5658 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5659 bool isReadOnly
= attr
->IsReadOnly();
5664 // ----------------------------------------------------------------------------
5665 // attribute support: cache, automatic provider creation, ...
5666 // ----------------------------------------------------------------------------
5668 bool wxGrid::CanHaveAttributes()
5675 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5676 // table is providing the attributes itself??? In which case
5677 // I don't think the grid should create a Provider object for the
5678 // table but the table should be smart enough to do that on its own.
5679 if ( !m_table
->GetAttrProvider() )
5681 // use the default attr provider by default
5682 // (another choice would be to just return FALSE thus forcing the user
5684 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5690 void wxGrid::ClearAttrCache()
5692 if ( m_attrCache
.row
!= -1 )
5694 m_attrCache
.attr
->SafeDecRef();
5695 m_attrCache
.row
= -1;
5699 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5701 wxGrid
*self
= (wxGrid
*)this; // const_cast
5703 self
->ClearAttrCache();
5704 self
->m_attrCache
.row
= row
;
5705 self
->m_attrCache
.col
= col
;
5706 self
->m_attrCache
.attr
= attr
;
5710 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5712 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5714 *attr
= m_attrCache
.attr
;
5715 (*attr
)->SafeIncRef();
5717 #ifdef DEBUG_ATTR_CACHE
5718 gs_nAttrCacheHits
++;
5725 #ifdef DEBUG_ATTR_CACHE
5726 gs_nAttrCacheMisses
++;
5732 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5734 wxGridCellAttr
*attr
;
5735 if ( !LookupAttr(row
, col
, &attr
) )
5737 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5738 CacheAttr(row
, col
, attr
);
5742 attr
->SetDefAttr(m_defaultCellAttr
);
5746 attr
= m_defaultCellAttr
;
5753 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5755 wxGridCellAttr
*attr
;
5756 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5758 wxASSERT_MSG( m_table
,
5759 _T("we may only be called if CanHaveAttributes() "
5760 "returned TRUE and then m_table should be !NULL") );
5762 attr
= m_table
->GetAttr(row
, col
);
5765 attr
= new wxGridCellAttr
;
5767 // artificially inc the ref count to match DecRef() in caller
5770 m_table
->SetAttr(attr
, row
, col
);
5773 CacheAttr(row
, col
, attr
);
5775 attr
->SetDefAttr(m_defaultCellAttr
);
5779 // ----------------------------------------------------------------------------
5780 // setting cell attributes: this is forwarded to the table
5781 // ----------------------------------------------------------------------------
5783 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5785 if ( CanHaveAttributes() )
5787 m_table
->SetRowAttr(attr
, row
);
5795 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5797 if ( CanHaveAttributes() )
5799 m_table
->SetColAttr(attr
, col
);
5807 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5809 if ( CanHaveAttributes() )
5811 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5812 attr
->SetBackgroundColour(colour
);
5817 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5819 if ( CanHaveAttributes() )
5821 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5822 attr
->SetTextColour(colour
);
5827 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5829 if ( CanHaveAttributes() )
5831 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5832 attr
->SetFont(font
);
5837 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5839 if ( CanHaveAttributes() )
5841 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5842 attr
->SetAlignment(horiz
, vert
);
5847 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5849 if ( CanHaveAttributes() )
5851 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5852 attr
->SetRenderer(renderer
);
5857 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5859 if ( CanHaveAttributes() )
5861 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5862 attr
->SetEditor(editor
);
5867 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5869 if ( CanHaveAttributes() )
5871 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5872 attr
->SetReadOnly(isReadOnly
);
5877 // ----------------------------------------------------------------------------
5879 // ----------------------------------------------------------------------------
5881 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5883 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5885 if ( resizeExistingRows
)
5889 for ( row
= 0; row
< m_numRows
; row
++ )
5891 m_rowHeights
[row
] = m_defaultRowHeight
;
5892 bottom
+= m_defaultRowHeight
;
5893 m_rowBottoms
[row
] = bottom
;
5899 void wxGrid::SetRowSize( int row
, int height
)
5901 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5905 int h
= wxMax( 0, height
);
5906 int diff
= h
- m_rowHeights
[row
];
5908 m_rowHeights
[row
] = h
;
5909 for ( i
= row
; i
< m_numRows
; i
++ )
5911 m_rowBottoms
[i
] += diff
;
5916 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5918 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5920 if ( resizeExistingCols
)
5924 for ( col
= 0; col
< m_numCols
; col
++ )
5926 m_colWidths
[col
] = m_defaultColWidth
;
5927 right
+= m_defaultColWidth
;
5928 m_colRights
[col
] = right
;
5934 void wxGrid::SetColSize( int col
, int width
)
5936 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5940 int w
= wxMax( 0, width
);
5941 int diff
= w
- m_colWidths
[col
];
5942 m_colWidths
[col
] = w
;
5944 for ( i
= col
; i
< m_numCols
; i
++ )
5946 m_colRights
[i
] += diff
;
5953 // ------ cell value accessor functions
5956 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5960 m_table
->SetValue( row
, col
, s
.c_str() );
5961 if ( !GetBatchCount() )
5963 wxClientDC
dc( m_gridWin
);
5965 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5968 #if 0 // TODO: edit in place
5970 if ( m_currentCellCoords
.GetRow() == row
&&
5971 m_currentCellCoords
.GetCol() == col
)
5973 SetEditControlValue( s
);
5982 // ------ Block, row and col selection
5985 void wxGrid::SelectRow( int row
, bool addToSelected
)
5989 if ( IsSelection() && addToSelected
)
5992 bool need_refresh
[4];
5996 need_refresh
[3] = FALSE
;
6000 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6001 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6002 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6003 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6007 need_refresh
[0] = TRUE
;
6008 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6009 wxGridCellCoords ( oldTop
- 1,
6011 m_selectedTopLeft
.SetRow( row
);
6016 need_refresh
[1] = TRUE
;
6017 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6018 wxGridCellCoords ( oldBottom
,
6021 m_selectedTopLeft
.SetCol( 0 );
6024 if ( oldBottom
< row
)
6026 need_refresh
[2] = TRUE
;
6027 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6028 wxGridCellCoords ( row
,
6030 m_selectedBottomRight
.SetRow( row
);
6033 if ( oldRight
< m_numCols
- 1 )
6035 need_refresh
[3] = TRUE
;
6036 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6038 wxGridCellCoords ( oldBottom
,
6040 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6043 for (i
= 0; i
< 4; i
++ )
6044 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6045 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6049 r
= SelectionToDeviceRect();
6051 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6053 m_selectedTopLeft
.Set( row
, 0 );
6054 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6055 r
= SelectionToDeviceRect();
6056 m_gridWin
->Refresh( FALSE
, &r
);
6059 wxGridRangeSelectEvent
gridEvt( GetId(),
6060 wxEVT_GRID_RANGE_SELECT
,
6063 m_selectedBottomRight
);
6065 GetEventHandler()->ProcessEvent(gridEvt
);
6069 void wxGrid::SelectCol( int col
, bool addToSelected
)
6071 if ( IsSelection() && addToSelected
)
6074 bool need_refresh
[4];
6078 need_refresh
[3] = FALSE
;
6081 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6082 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6083 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6084 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6086 if ( oldLeft
> col
)
6088 need_refresh
[0] = TRUE
;
6089 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6090 wxGridCellCoords ( m_numRows
- 1,
6092 m_selectedTopLeft
.SetCol( col
);
6097 need_refresh
[1] = TRUE
;
6098 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6099 wxGridCellCoords ( oldTop
- 1,
6101 m_selectedTopLeft
.SetRow( 0 );
6104 if ( oldRight
< col
)
6106 need_refresh
[2] = TRUE
;
6107 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6108 wxGridCellCoords ( m_numRows
- 1,
6110 m_selectedBottomRight
.SetCol( col
);
6113 if ( oldBottom
< m_numRows
- 1 )
6115 need_refresh
[3] = TRUE
;
6116 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6118 wxGridCellCoords ( m_numRows
- 1,
6120 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6123 for (i
= 0; i
< 4; i
++ )
6124 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6125 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6131 r
= SelectionToDeviceRect();
6133 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6135 m_selectedTopLeft
.Set( 0, col
);
6136 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6137 r
= SelectionToDeviceRect();
6138 m_gridWin
->Refresh( FALSE
, &r
);
6141 wxGridRangeSelectEvent
gridEvt( GetId(),
6142 wxEVT_GRID_RANGE_SELECT
,
6145 m_selectedBottomRight
);
6147 GetEventHandler()->ProcessEvent(gridEvt
);
6151 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6154 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6156 if ( topRow
> bottomRow
)
6163 if ( leftCol
> rightCol
)
6170 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6171 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6173 if ( m_selectedTopLeft
!= updateTopLeft
||
6174 m_selectedBottomRight
!= updateBottomRight
)
6176 // Compute two optimal update rectangles:
6177 // Either one rectangle is a real subset of the
6178 // other, or they are (almost) disjoint!
6180 bool need_refresh
[4];
6184 need_refresh
[3] = FALSE
;
6187 // Store intermediate values
6188 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6189 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6190 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6191 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6193 // Determine the outer/inner coordinates.
6194 if (oldLeft
> leftCol
)
6200 if (oldTop
> topRow
)
6206 if (oldRight
< rightCol
)
6209 oldRight
= rightCol
;
6212 if (oldBottom
< bottomRow
)
6215 oldBottom
= bottomRow
;
6219 // Now, either the stuff marked old is the outer
6220 // rectangle or we don't have a situation where one
6221 // is contained in the other.
6223 if ( oldLeft
< leftCol
)
6225 need_refresh
[0] = TRUE
;
6226 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6228 wxGridCellCoords ( oldBottom
,
6232 if ( oldTop
< topRow
)
6234 need_refresh
[1] = TRUE
;
6235 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6237 wxGridCellCoords ( topRow
- 1,
6241 if ( oldRight
> rightCol
)
6243 need_refresh
[2] = TRUE
;
6244 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6246 wxGridCellCoords ( oldBottom
,
6250 if ( oldBottom
> bottomRow
)
6252 need_refresh
[3] = TRUE
;
6253 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6255 wxGridCellCoords ( oldBottom
,
6261 m_selectedTopLeft
= updateTopLeft
;
6262 m_selectedBottomRight
= updateBottomRight
;
6264 // various Refresh() calls
6265 for (i
= 0; i
< 4; i
++ )
6266 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6267 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6270 // only generate an event if the block is not being selected by
6271 // dragging the mouse (in which case the event will be generated in
6272 // the mouse event handler)
6273 if ( !m_isDragging
)
6275 wxGridRangeSelectEvent
gridEvt( GetId(),
6276 wxEVT_GRID_RANGE_SELECT
,
6279 m_selectedBottomRight
);
6281 GetEventHandler()->ProcessEvent(gridEvt
);
6285 void wxGrid::SelectAll()
6287 m_selectedTopLeft
.Set( 0, 0 );
6288 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6290 m_gridWin
->Refresh();
6294 void wxGrid::ClearSelection()
6296 m_selectedTopLeft
= wxGridNoCellCoords
;
6297 m_selectedBottomRight
= wxGridNoCellCoords
;
6301 // This function returns the rectangle that encloses the given block
6302 // in device coords clipped to the client size of the grid window.
6304 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6305 const wxGridCellCoords
&bottomRight
)
6307 wxRect
rect( wxGridNoCellRect
);
6310 cellRect
= CellToRect( topLeft
);
6311 if ( cellRect
!= wxGridNoCellRect
)
6317 rect
= wxRect( 0, 0, 0, 0 );
6320 cellRect
= CellToRect( bottomRight
);
6321 if ( cellRect
!= wxGridNoCellRect
)
6327 return wxGridNoCellRect
;
6330 // convert to scrolled coords
6332 int left
, top
, right
, bottom
;
6333 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6334 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6337 m_gridWin
->GetClientSize( &cw
, &ch
);
6339 rect
.SetLeft( wxMax(0, left
) );
6340 rect
.SetTop( wxMax(0, top
) );
6341 rect
.SetRight( wxMin(cw
, right
) );
6342 rect
.SetBottom( wxMin(ch
, bottom
) );
6350 // ------ Grid event classes
6353 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6355 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6356 int row
, int col
, int x
, int y
,
6357 bool control
, bool shift
, bool alt
, bool meta
)
6358 : wxNotifyEvent( type
, id
)
6364 m_control
= control
;
6369 SetEventObject(obj
);
6373 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6375 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6376 int rowOrCol
, int x
, int y
,
6377 bool control
, bool shift
, bool alt
, bool meta
)
6378 : wxNotifyEvent( type
, id
)
6380 m_rowOrCol
= rowOrCol
;
6383 m_control
= control
;
6388 SetEventObject(obj
);
6392 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6394 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6395 const wxGridCellCoords
& topLeft
,
6396 const wxGridCellCoords
& bottomRight
,
6397 bool control
, bool shift
, bool alt
, bool meta
)
6398 : wxNotifyEvent( type
, id
)
6400 m_topLeft
= topLeft
;
6401 m_bottomRight
= bottomRight
;
6402 m_control
= control
;
6407 SetEventObject(obj
);
6411 #endif // ifndef wxUSE_NEW_GRID