1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
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
);
1942 m_owner
->DrawHighlight( dc
);
1946 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1948 wxPanel::ScrollWindow( dx
, dy
, rect
);
1949 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1950 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1954 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1956 m_owner
->ProcessGridCellMouseEvent( event
);
1960 // This seems to be required for wxMotif otherwise the mouse
1961 // cursor must be in the cell edit control to get key events
1963 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1965 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1968 // We are trapping erase background events to reduce flicker under MSW
1969 // and GTK but this can leave junk in the space beyond the last row and
1970 // col. So here we paint these spaces if they are visible.
1972 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
1975 GetClientSize( &cw
, &ch
);
1978 m_owner
->CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
1981 rightRect
= m_owner
->CellToRect( 0, m_owner
->GetNumberCols()-1 );
1984 bottomRect
= m_owner
->CellToRect( m_owner
->GetNumberRows()-1, 0 );
1986 if ( right
> rightRect
.GetRight() || bottom
> bottomRect
.GetBottom() )
1989 m_owner
->CalcUnscrolledPosition( 0, 0, &left
, &top
);
1991 wxClientDC
dc( this );
1992 m_owner
->PrepareDC( dc
);
1993 dc
.SetBrush( wxBrush(m_owner
->GetDefaultCellBackgroundColour(), wxSOLID
) );
1994 dc
.SetPen( *wxTRANSPARENT_PEN
);
1996 if ( right
> rightRect
.GetRight() )
1997 dc
.DrawRectangle( rightRect
.GetRight()+1, top
, right
- rightRect
.GetRight(), ch
);
1999 if ( bottom
> bottomRect
.GetBottom() )
2000 dc
.DrawRectangle( left
, bottomRect
.GetBottom()+1, cw
, bottom
- bottomRect
.GetBottom() );
2005 //////////////////////////////////////////////////////////////////////
2008 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2010 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2011 EVT_PAINT( wxGrid::OnPaint
)
2012 EVT_SIZE( wxGrid::OnSize
)
2013 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2014 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2017 wxGrid::wxGrid( wxWindow
*parent
,
2022 const wxString
& name
)
2023 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
2032 m_defaultCellAttr
->SafeDecRef();
2034 #ifdef DEBUG_ATTR_CACHE
2035 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2036 wxPrintf(_T("wxGrid attribute cache statistics: "
2037 "total: %u, hits: %u (%u%%)\n"),
2038 total
, gs_nAttrCacheHits
,
2039 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2048 // ----- internal init and update functions
2051 void wxGrid::Create()
2053 m_created
= FALSE
; // set to TRUE by CreateGrid
2054 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2056 m_table
= (wxGridTableBase
*) NULL
;
2059 m_cellEditCtrlEnabled
= FALSE
;
2061 m_defaultCellAttr
= new wxGridCellAttr
;
2062 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2063 // RD: Should we fill the default attrs now or is waiting until Init() okay?
2068 m_currentCellCoords
= wxGridNoCellCoords
;
2070 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2071 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2073 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2078 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2083 m_colLabelWin
= new wxGridColLabelWindow( this,
2088 m_gridWin
= new wxGridWindow( this,
2095 SetTargetWindow( m_gridWin
);
2099 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2103 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2108 m_numRows
= numRows
;
2109 m_numCols
= numCols
;
2111 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2112 m_table
->SetView( this );
2121 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2125 // RD: Actually, this should probably be allowed. I think it would be
2126 // nice to be able to switch multiple Tables in and out of a single
2127 // View at runtime. Is there anything in the implmentation that would
2130 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2135 m_numRows
= table
->GetNumberRows();
2136 m_numCols
= table
->GetNumberCols();
2139 m_table
->SetView( this );
2154 if ( m_numRows
<= 0 )
2155 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2157 if ( m_numCols
<= 0 )
2158 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2160 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2161 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2163 if ( m_rowLabelWin
)
2165 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2169 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2172 m_labelTextColour
= wxColour( _T("BLACK") );
2175 m_attrCache
.row
= -1;
2177 // TODO: something better than this ?
2179 m_labelFont
= this->GetFont();
2180 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2182 m_rowLabelHorizAlign
= wxLEFT
;
2183 m_rowLabelVertAlign
= wxCENTRE
;
2185 m_colLabelHorizAlign
= wxCENTRE
;
2186 m_colLabelVertAlign
= wxTOP
;
2188 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2189 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2191 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2192 m_defaultRowHeight
+= 8;
2194 m_defaultRowHeight
+= 4;
2197 m_rowHeights
.Alloc( m_numRows
);
2198 m_rowBottoms
.Alloc( m_numRows
);
2200 for ( i
= 0; i
< m_numRows
; i
++ )
2202 m_rowHeights
.Add( m_defaultRowHeight
);
2203 rowBottom
+= m_defaultRowHeight
;
2204 m_rowBottoms
.Add( rowBottom
);
2207 m_colWidths
.Alloc( m_numCols
);
2208 m_colRights
.Alloc( m_numCols
);
2210 for ( i
= 0; i
< m_numCols
; i
++ )
2212 m_colWidths
.Add( m_defaultColWidth
);
2213 colRight
+= m_defaultColWidth
;
2214 m_colRights
.Add( colRight
);
2217 // Set default cell attributes
2218 m_defaultCellAttr
->SetFont(GetFont());
2219 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2220 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2221 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2222 m_defaultCellAttr
->SetTextColour(
2223 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2224 m_defaultCellAttr
->SetBackgroundColour(
2225 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2228 m_gridLineColour
= wxColour( 128, 128, 255 );
2229 m_gridLinesEnabled
= TRUE
;
2231 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2232 m_winCapture
= (wxWindow
*)NULL
;
2234 m_dragRowOrCol
= -1;
2235 m_isDragging
= FALSE
;
2236 m_startDragPos
= wxDefaultPosition
;
2238 m_waitForSlowClick
= FALSE
;
2240 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2241 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2243 m_currentCellCoords
= wxGridNoCellCoords
;
2245 m_selectedTopLeft
= wxGridNoCellCoords
;
2246 m_selectedBottomRight
= wxGridNoCellCoords
;
2247 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2248 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2250 m_editable
= TRUE
; // default for whole grid
2252 m_inOnKeyDown
= FALSE
;
2258 void wxGrid::CalcDimensions()
2261 GetClientSize( &cw
, &ch
);
2263 if ( m_numRows
> 0 && m_numCols
> 0 )
2265 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2266 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2268 // TODO: restore the scroll position that we had before sizing
2271 GetViewStart( &x
, &y
);
2272 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2273 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2279 void wxGrid::CalcWindowSizes()
2282 GetClientSize( &cw
, &ch
);
2284 if ( m_cornerLabelWin
->IsShown() )
2285 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2287 if ( m_colLabelWin
->IsShown() )
2288 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2290 if ( m_rowLabelWin
->IsShown() )
2291 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2293 if ( m_gridWin
->IsShown() )
2294 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2298 // this is called when the grid table sends a message to say that it
2299 // has been redimensioned
2301 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2305 switch ( msg
.GetId() )
2307 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2309 size_t pos
= msg
.GetCommandInt();
2310 int numRows
= msg
.GetCommandInt2();
2311 for ( i
= 0; i
< numRows
; i
++ )
2313 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2314 m_rowBottoms
.Insert( 0, pos
);
2316 m_numRows
+= numRows
;
2319 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2321 for ( i
= pos
; i
< m_numRows
; i
++ )
2323 bottom
+= m_rowHeights
[i
];
2324 m_rowBottoms
[i
] = bottom
;
2330 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2332 int numRows
= msg
.GetCommandInt();
2333 for ( i
= 0; i
< numRows
; i
++ )
2335 m_rowHeights
.Add( m_defaultRowHeight
);
2336 m_rowBottoms
.Add( 0 );
2339 int oldNumRows
= m_numRows
;
2340 m_numRows
+= numRows
;
2343 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2345 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2347 bottom
+= m_rowHeights
[i
];
2348 m_rowBottoms
[i
] = bottom
;
2354 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2356 size_t pos
= msg
.GetCommandInt();
2357 int numRows
= msg
.GetCommandInt2();
2358 for ( i
= 0; i
< numRows
; i
++ )
2360 m_rowHeights
.Remove( pos
);
2361 m_rowBottoms
.Remove( pos
);
2363 m_numRows
-= numRows
;
2368 m_colWidths
.Clear();
2369 m_colRights
.Clear();
2370 m_currentCellCoords
= wxGridNoCellCoords
;
2374 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2375 m_currentCellCoords
.Set( 0, 0 );
2378 for ( i
= 0; i
< m_numRows
; i
++ )
2380 h
+= m_rowHeights
[i
];
2381 m_rowBottoms
[i
] = h
;
2389 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2391 size_t pos
= msg
.GetCommandInt();
2392 int numCols
= msg
.GetCommandInt2();
2393 for ( i
= 0; i
< numCols
; i
++ )
2395 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2396 m_colRights
.Insert( 0, pos
);
2398 m_numCols
+= numCols
;
2401 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2403 for ( i
= pos
; i
< m_numCols
; i
++ )
2405 right
+= m_colWidths
[i
];
2406 m_colRights
[i
] = right
;
2412 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2414 int numCols
= msg
.GetCommandInt();
2415 for ( i
= 0; i
< numCols
; i
++ )
2417 m_colWidths
.Add( m_defaultColWidth
);
2418 m_colRights
.Add( 0 );
2421 int oldNumCols
= m_numCols
;
2422 m_numCols
+= numCols
;
2425 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2427 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2429 right
+= m_colWidths
[i
];
2430 m_colRights
[i
] = right
;
2436 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2438 size_t pos
= msg
.GetCommandInt();
2439 int numCols
= msg
.GetCommandInt2();
2440 for ( i
= 0; i
< numCols
; i
++ )
2442 m_colWidths
.Remove( pos
);
2443 m_colRights
.Remove( pos
);
2445 m_numCols
-= numCols
;
2449 #if 0 // leave the row alone here so that AppendCols will work subsequently
2451 m_rowHeights
.Clear();
2452 m_rowBottoms
.Clear();
2454 m_currentCellCoords
= wxGridNoCellCoords
;
2458 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2459 m_currentCellCoords
.Set( 0, 0 );
2462 for ( i
= 0; i
< m_numCols
; i
++ )
2464 w
+= m_colWidths
[i
];
2477 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2479 wxRegionIterator
iter( reg
);
2482 m_rowLabelsExposed
.Empty();
2489 // TODO: remove this when we can...
2490 // There is a bug in wxMotif that gives garbage update
2491 // rectangles if you jump-scroll a long way by clicking the
2492 // scrollbar with middle button. This is a work-around
2494 #if defined(__WXMOTIF__)
2496 m_gridWin
->GetClientSize( &cw
, &ch
);
2497 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2498 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2501 // logical bounds of update region
2504 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2505 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2507 // find the row labels within these bounds
2511 for ( row
= 0; row
< m_numRows
; row
++ )
2513 if ( m_rowBottoms
[row
] < top
) continue;
2515 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2516 if ( rowTop
> bottom
) break;
2518 m_rowLabelsExposed
.Add( row
);
2526 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2528 wxRegionIterator
iter( reg
);
2531 m_colLabelsExposed
.Empty();
2538 // TODO: remove this when we can...
2539 // There is a bug in wxMotif that gives garbage update
2540 // rectangles if you jump-scroll a long way by clicking the
2541 // scrollbar with middle button. This is a work-around
2543 #if defined(__WXMOTIF__)
2545 m_gridWin
->GetClientSize( &cw
, &ch
);
2546 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2547 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2550 // logical bounds of update region
2553 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2554 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2556 // find the cells within these bounds
2560 for ( col
= 0; col
< m_numCols
; col
++ )
2562 if ( m_colRights
[col
] < left
) continue;
2564 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2565 if ( colLeft
> right
) break;
2567 m_colLabelsExposed
.Add( col
);
2575 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2577 wxRegionIterator
iter( reg
);
2580 m_cellsExposed
.Empty();
2581 m_rowsExposed
.Empty();
2582 m_colsExposed
.Empty();
2584 int left
, top
, right
, bottom
;
2589 // TODO: remove this when we can...
2590 // There is a bug in wxMotif that gives garbage update
2591 // rectangles if you jump-scroll a long way by clicking the
2592 // scrollbar with middle button. This is a work-around
2594 #if defined(__WXMOTIF__)
2596 m_gridWin
->GetClientSize( &cw
, &ch
);
2597 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2598 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2599 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2600 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2603 // logical bounds of update region
2605 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2606 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2608 // find the cells within these bounds
2611 int colLeft
, rowTop
;
2612 for ( row
= 0; row
< m_numRows
; row
++ )
2614 if ( m_rowBottoms
[row
] <= top
) continue;
2616 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2617 if ( rowTop
> bottom
) break;
2619 m_rowsExposed
.Add( row
);
2621 for ( col
= 0; col
< m_numCols
; col
++ )
2623 if ( m_colRights
[col
] <= left
) continue;
2625 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2626 if ( colLeft
> right
) break;
2628 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2629 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2638 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2641 wxPoint
pos( event
.GetPosition() );
2642 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2644 if ( event
.Dragging() )
2646 m_isDragging
= TRUE
;
2648 if ( event
.LeftIsDown() )
2650 switch( m_cursorMode
)
2652 case WXGRID_CURSOR_RESIZE_ROW
:
2654 int cw
, ch
, left
, dummy
;
2655 m_gridWin
->GetClientSize( &cw
, &ch
);
2656 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2658 wxClientDC
dc( m_gridWin
);
2661 m_rowBottoms
[m_dragRowOrCol
] -
2662 m_rowHeights
[m_dragRowOrCol
] +
2663 WXGRID_MIN_ROW_HEIGHT
);
2664 dc
.SetLogicalFunction(wxINVERT
);
2665 if ( m_dragLastPos
>= 0 )
2667 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2669 dc
.DrawLine( left
, y
, left
+cw
, y
);
2674 case WXGRID_CURSOR_SELECT_ROW
:
2675 if ( (row
= YToRow( y
)) >= 0 &&
2676 !IsInSelection( row
, 0 ) )
2678 SelectRow( row
, TRUE
);
2681 // default label to suppress warnings about "enumeration value
2682 // 'xxx' not handled in switch
2690 m_isDragging
= FALSE
;
2693 // ------------ Entering or leaving the window
2695 if ( event
.Entering() || event
.Leaving() )
2697 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2701 // ------------ Left button pressed
2703 else if ( event
.LeftDown() )
2705 // don't send a label click event for a hit on the
2706 // edge of the row label - this is probably the user
2707 // wanting to resize the row
2709 if ( YToEdgeOfRow(y
) < 0 )
2713 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2715 SelectRow( row
, event
.ShiftDown() );
2716 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2721 // starting to drag-resize a row
2723 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2728 // ------------ Left double click
2730 else if (event
.LeftDClick() )
2732 if ( YToEdgeOfRow(y
) < 0 )
2735 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2740 // ------------ Left button released
2742 else if ( event
.LeftUp() )
2744 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2746 DoEndDragResizeRow();
2748 // Note: we are ending the event *after* doing
2749 // default processing in this case
2751 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2754 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2759 // ------------ Right button down
2761 else if ( event
.RightDown() )
2764 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2766 // no default action at the moment
2771 // ------------ Right double click
2773 else if ( event
.RightDClick() )
2776 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2778 // no default action at the moment
2783 // ------------ No buttons down and mouse moving
2785 else if ( event
.Moving() )
2787 m_dragRowOrCol
= YToEdgeOfRow( y
);
2788 if ( m_dragRowOrCol
>= 0 )
2790 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2792 // don't capture the mouse yet
2793 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2796 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2798 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2804 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2807 wxPoint
pos( event
.GetPosition() );
2808 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2810 if ( event
.Dragging() )
2812 m_isDragging
= TRUE
;
2814 if ( event
.LeftIsDown() )
2816 switch( m_cursorMode
)
2818 case WXGRID_CURSOR_RESIZE_COL
:
2820 int cw
, ch
, dummy
, top
;
2821 m_gridWin
->GetClientSize( &cw
, &ch
);
2822 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2824 wxClientDC
dc( m_gridWin
);
2827 m_colRights
[m_dragRowOrCol
] -
2828 m_colWidths
[m_dragRowOrCol
] +
2829 WXGRID_MIN_COL_WIDTH
);
2830 dc
.SetLogicalFunction(wxINVERT
);
2831 if ( m_dragLastPos
>= 0 )
2833 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2835 dc
.DrawLine( x
, top
, x
, top
+ch
);
2840 case WXGRID_CURSOR_SELECT_COL
:
2841 if ( (col
= XToCol( x
)) >= 0 &&
2842 !IsInSelection( 0, col
) )
2844 SelectCol( col
, TRUE
);
2847 // default label to suppress warnings about "enumeration value
2848 // 'xxx' not handled in switch
2856 m_isDragging
= FALSE
;
2859 // ------------ Entering or leaving the window
2861 if ( event
.Entering() || event
.Leaving() )
2863 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2867 // ------------ Left button pressed
2869 else if ( event
.LeftDown() )
2871 // don't send a label click event for a hit on the
2872 // edge of the col label - this is probably the user
2873 // wanting to resize the col
2875 if ( XToEdgeOfCol(x
) < 0 )
2879 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2881 SelectCol( col
, event
.ShiftDown() );
2882 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2887 // starting to drag-resize a col
2889 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2894 // ------------ Left double click
2896 if ( event
.LeftDClick() )
2898 if ( XToEdgeOfCol(x
) < 0 )
2901 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2906 // ------------ Left button released
2908 else if ( event
.LeftUp() )
2910 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2912 DoEndDragResizeCol();
2914 // Note: we are ending the event *after* doing
2915 // default processing in this case
2917 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2920 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2925 // ------------ Right button down
2927 else if ( event
.RightDown() )
2930 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2932 // no default action at the moment
2937 // ------------ Right double click
2939 else if ( event
.RightDClick() )
2942 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2944 // no default action at the moment
2949 // ------------ No buttons down and mouse moving
2951 else if ( event
.Moving() )
2953 m_dragRowOrCol
= XToEdgeOfCol( x
);
2954 if ( m_dragRowOrCol
>= 0 )
2956 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2958 // don't capture the cursor yet
2959 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2962 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2964 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2970 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2972 if ( event
.LeftDown() )
2974 // indicate corner label by having both row and
2977 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2983 else if ( event
.LeftDClick() )
2985 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2988 else if ( event
.RightDown() )
2990 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2992 // no default action at the moment
2996 else if ( event
.RightDClick() )
2998 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3000 // no default action at the moment
3005 void wxGrid::ChangeCursorMode(CursorMode mode
,
3010 static const wxChar
*cursorModes
[] =
3019 wxLogTrace(_T("grid"),
3020 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3021 win
== m_colLabelWin
? _T("colLabelWin")
3022 : win
? _T("rowLabelWin")
3024 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3025 #endif // __WXDEBUG__
3027 if ( mode
== m_cursorMode
)
3032 // by default use the grid itself
3038 m_winCapture
->ReleaseMouse();
3039 m_winCapture
= (wxWindow
*)NULL
;
3042 m_cursorMode
= mode
;
3044 switch ( m_cursorMode
)
3046 case WXGRID_CURSOR_RESIZE_ROW
:
3047 win
->SetCursor( m_rowResizeCursor
);
3050 case WXGRID_CURSOR_RESIZE_COL
:
3051 win
->SetCursor( m_colResizeCursor
);
3055 win
->SetCursor( *wxSTANDARD_CURSOR
);
3058 // we need to capture mouse when resizing
3059 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3060 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3062 if ( captureMouse
&& resize
)
3064 win
->CaptureMouse();
3069 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3072 wxPoint
pos( event
.GetPosition() );
3073 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3075 wxGridCellCoords coords
;
3076 XYToCell( x
, y
, coords
);
3078 if ( event
.Dragging() )
3080 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3082 // Don't start doing anything until the mouse has been drug at
3083 // least 3 pixels in any direction...
3086 if (m_startDragPos
== wxDefaultPosition
)
3088 m_startDragPos
= pos
;
3091 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3095 m_isDragging
= TRUE
;
3096 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3098 // Hide the edit control, so it
3099 // won't interfer with drag-shrinking.
3100 if ( IsCellEditControlEnabled() )
3101 HideCellEditControl();
3103 // Have we captured the mouse yet?
3106 m_winCapture
= m_gridWin
;
3107 m_winCapture
->CaptureMouse();
3110 if ( coords
!= wxGridNoCellCoords
)
3112 if ( !IsSelection() )
3114 SelectBlock( coords
, coords
);
3118 SelectBlock( m_currentCellCoords
, coords
);
3121 if (! IsVisible(coords
))
3123 MakeCellVisible(coords
);
3124 // TODO: need to introduce a delay or something here. The
3125 // scrolling is way to fast, at least on MSW.
3129 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3131 int cw
, ch
, left
, dummy
;
3132 m_gridWin
->GetClientSize( &cw
, &ch
);
3133 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3135 wxClientDC
dc( m_gridWin
);
3138 m_rowBottoms
[m_dragRowOrCol
] -
3139 m_rowHeights
[m_dragRowOrCol
] +
3140 WXGRID_MIN_ROW_HEIGHT
);
3141 dc
.SetLogicalFunction(wxINVERT
);
3142 if ( m_dragLastPos
>= 0 )
3144 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3146 dc
.DrawLine( left
, y
, left
+cw
, y
);
3149 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3151 int cw
, ch
, dummy
, top
;
3152 m_gridWin
->GetClientSize( &cw
, &ch
);
3153 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3155 wxClientDC
dc( m_gridWin
);
3158 m_colRights
[m_dragRowOrCol
] -
3159 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
3160 dc
.SetLogicalFunction(wxINVERT
);
3161 if ( m_dragLastPos
>= 0 )
3163 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3165 dc
.DrawLine( x
, top
, x
, top
+ch
);
3172 m_isDragging
= FALSE
;
3173 m_startDragPos
= wxDefaultPosition
;
3176 if ( coords
!= wxGridNoCellCoords
)
3178 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3179 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3182 if ( event
.Entering() || event
.Leaving() )
3184 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3185 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3190 // ------------ Left button pressed
3192 if ( event
.LeftDown() )
3194 DisableCellEditControl();
3195 if ( event
.ShiftDown() )
3197 SelectBlock( m_currentCellCoords
, coords
);
3199 else if ( XToEdgeOfCol(x
) < 0 &&
3200 YToEdgeOfRow(y
) < 0 )
3202 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3207 MakeCellVisible( coords
);
3209 // if this is the second click on this cell then start
3211 if ( m_waitForSlowClick
&&
3212 (coords
== m_currentCellCoords
) &&
3213 CanEnableCellControl())
3215 EnableCellEditControl();
3217 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3218 attr
->GetEditor()->StartingClick();
3221 m_waitForSlowClick
= FALSE
;
3225 SetCurrentCell( coords
);
3226 m_waitForSlowClick
= TRUE
;
3233 // ------------ Left double click
3235 else if ( event
.LeftDClick() )
3237 DisableCellEditControl();
3238 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3240 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3248 // ------------ Left button released
3250 else if ( event
.LeftUp() )
3252 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3254 if ( IsSelection() )
3258 m_winCapture
->ReleaseMouse();
3259 m_winCapture
= NULL
;
3261 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3264 // Show the edit control, if it has been hidden for
3266 ShowCellEditControl();
3268 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3270 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3271 DoEndDragResizeRow();
3273 // Note: we are ending the event *after* doing
3274 // default processing in this case
3276 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3278 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3280 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3281 DoEndDragResizeCol();
3283 // Note: we are ending the event *after* doing
3284 // default processing in this case
3286 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3293 // ------------ Right button down
3295 else if ( event
.RightDown() )
3297 DisableCellEditControl();
3298 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3303 // no default action at the moment
3308 // ------------ Right double click
3310 else if ( event
.RightDClick() )
3312 DisableCellEditControl();
3313 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3318 // no default action at the moment
3322 // ------------ Moving and no button action
3324 else if ( event
.Moving() && !event
.IsButton() )
3326 int dragRow
= YToEdgeOfRow( y
);
3327 int dragCol
= XToEdgeOfCol( x
);
3329 // Dragging on the corner of a cell to resize in both
3330 // directions is not implemented yet...
3332 if ( dragRow
>= 0 && dragCol
>= 0 )
3334 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3340 m_dragRowOrCol
= dragRow
;
3342 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3344 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3352 m_dragRowOrCol
= dragCol
;
3354 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3356 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3362 // Neither on a row or col edge
3364 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3366 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3373 void wxGrid::DoEndDragResizeRow()
3375 if ( m_dragLastPos
>= 0 )
3377 // erase the last line and resize the row
3379 int cw
, ch
, left
, dummy
;
3380 m_gridWin
->GetClientSize( &cw
, &ch
);
3381 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3383 wxClientDC
dc( m_gridWin
);
3385 dc
.SetLogicalFunction( wxINVERT
);
3386 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3387 HideCellEditControl();
3389 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3390 SetRowSize( m_dragRowOrCol
,
3391 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3393 if ( !GetBatchCount() )
3395 // Only needed to get the correct rect.y:
3396 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3398 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3399 rect
.width
= m_rowLabelWidth
;
3400 rect
.height
= ch
- rect
.y
;
3401 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3403 m_gridWin
->Refresh( FALSE
, &rect
);
3406 ShowCellEditControl();
3411 void wxGrid::DoEndDragResizeCol()
3413 if ( m_dragLastPos
>= 0 )
3415 // erase the last line and resize the col
3417 int cw
, ch
, dummy
, top
;
3418 m_gridWin
->GetClientSize( &cw
, &ch
);
3419 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3421 wxClientDC
dc( m_gridWin
);
3423 dc
.SetLogicalFunction( wxINVERT
);
3424 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3425 HideCellEditControl();
3427 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3428 SetColSize( m_dragRowOrCol
,
3429 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3431 if ( !GetBatchCount() )
3433 // Only needed to get the correct rect.x:
3434 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3436 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3437 rect
.width
= cw
- rect
.x
;
3438 rect
.height
= m_colLabelHeight
;
3439 m_colLabelWin
->Refresh( TRUE
, &rect
);
3441 m_gridWin
->Refresh( FALSE
, &rect
);
3444 ShowCellEditControl();
3451 // ------ interaction with data model
3453 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3455 switch ( msg
.GetId() )
3457 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3458 return GetModelValues();
3460 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3461 return SetModelValues();
3463 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3464 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3465 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3466 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3467 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3468 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3469 return Redimension( msg
);
3478 // The behaviour of this function depends on the grid table class
3479 // Clear() function. For the default wxGridStringTable class the
3480 // behavious is to replace all cell contents with wxEmptyString but
3481 // not to change the number of rows or cols.
3483 void wxGrid::ClearGrid()
3488 SetEditControlValue();
3489 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3494 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3496 // TODO: something with updateLabels flag
3500 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3506 if (IsCellEditControlEnabled())
3507 DisableCellEditControl();
3509 bool ok
= m_table
->InsertRows( pos
, numRows
);
3511 // the table will have sent the results of the insert row
3512 // operation to this view object as a grid table message
3516 if ( m_numCols
== 0 )
3518 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3520 // TODO: perhaps instead of appending the default number of cols
3521 // we should remember what the last non-zero number of cols was ?
3525 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3527 // if we have just inserted cols into an empty grid the current
3528 // cell will be undefined...
3530 SetCurrentCell( 0, 0 );
3534 if ( !GetBatchCount() ) Refresh();
3537 SetEditControlValue();
3547 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3549 // TODO: something with updateLabels flag
3553 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3557 if ( m_table
&& m_table
->AppendRows( numRows
) )
3559 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3561 // if we have just inserted cols into an empty grid the current
3562 // cell will be undefined...
3564 SetCurrentCell( 0, 0 );
3567 // the table will have sent the results of the append row
3568 // operation to this view object as a grid table message
3571 if ( !GetBatchCount() ) Refresh();
3581 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3583 // TODO: something with updateLabels flag
3587 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3593 if (IsCellEditControlEnabled())
3594 DisableCellEditControl();
3596 if (m_table
->DeleteRows( pos
, numRows
))
3599 // the table will have sent the results of the delete row
3600 // operation to this view object as a grid table message
3603 if ( !GetBatchCount() ) Refresh();
3611 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3613 // TODO: something with updateLabels flag
3617 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3623 if (IsCellEditControlEnabled())
3624 DisableCellEditControl();
3626 bool ok
= m_table
->InsertCols( pos
, numCols
);
3628 // the table will have sent the results of the insert col
3629 // operation to this view object as a grid table message
3633 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3635 // if we have just inserted cols into an empty grid the current
3636 // cell will be undefined...
3638 SetCurrentCell( 0, 0 );
3642 if ( !GetBatchCount() ) Refresh();
3645 SetEditControlValue();
3655 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3657 // TODO: something with updateLabels flag
3661 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3665 if ( m_table
&& m_table
->AppendCols( numCols
) )
3667 // the table will have sent the results of the append col
3668 // operation to this view object as a grid table message
3670 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3672 // if we have just inserted cols into an empty grid the current
3673 // cell will be undefined...
3675 SetCurrentCell( 0, 0 );
3679 if ( !GetBatchCount() ) Refresh();
3689 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3691 // TODO: something with updateLabels flag
3695 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3701 if (IsCellEditControlEnabled())
3702 DisableCellEditControl();
3704 if ( m_table
->DeleteCols( pos
, numCols
) )
3706 // the table will have sent the results of the delete col
3707 // operation to this view object as a grid table message
3710 if ( !GetBatchCount() ) Refresh();
3720 // ----- event handlers
3723 // Generate a grid event based on a mouse event and
3724 // return the result of ProcessEvent()
3726 bool wxGrid::SendEvent( const wxEventType type
,
3728 wxMouseEvent
& mouseEv
)
3730 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3732 int rowOrCol
= (row
== -1 ? col
: row
);
3734 wxGridSizeEvent
gridEvt( GetId(),
3738 mouseEv
.GetX(), mouseEv
.GetY(),
3739 mouseEv
.ControlDown(),
3740 mouseEv
.ShiftDown(),
3742 mouseEv
.MetaDown() );
3744 return GetEventHandler()->ProcessEvent(gridEvt
);
3746 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3748 wxGridRangeSelectEvent
gridEvt( GetId(),
3752 m_selectedBottomRight
,
3753 mouseEv
.ControlDown(),
3754 mouseEv
.ShiftDown(),
3756 mouseEv
.MetaDown() );
3758 return GetEventHandler()->ProcessEvent(gridEvt
);
3762 wxGridEvent
gridEvt( GetId(),
3766 mouseEv
.GetX(), mouseEv
.GetY(),
3767 mouseEv
.ControlDown(),
3768 mouseEv
.ShiftDown(),
3770 mouseEv
.MetaDown() );
3772 return GetEventHandler()->ProcessEvent(gridEvt
);
3777 // Generate a grid event of specified type and return the result
3778 // of ProcessEvent().
3780 bool wxGrid::SendEvent( const wxEventType type
,
3783 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3785 int rowOrCol
= (row
== -1 ? col
: row
);
3787 wxGridSizeEvent
gridEvt( GetId(),
3792 return GetEventHandler()->ProcessEvent(gridEvt
);
3796 wxGridEvent
gridEvt( GetId(),
3801 return GetEventHandler()->ProcessEvent(gridEvt
);
3806 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3808 wxPaintDC
dc( this );
3810 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3811 m_numRows
&& m_numCols
)
3813 m_currentCellCoords
.Set(0, 0);
3814 SetEditControlValue();
3815 ShowCellEditControl();
3822 // This is just here to make sure that CalcDimensions gets called when
3823 // the grid view is resized... then the size event is skipped to allow
3824 // the box sizers to handle everything
3826 void wxGrid::OnSize( wxSizeEvent
& event
)
3833 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3835 if ( m_inOnKeyDown
)
3837 // shouldn't be here - we are going round in circles...
3839 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3842 m_inOnKeyDown
= TRUE
;
3844 // propagate the event up and see if it gets processed
3846 wxWindow
*parent
= GetParent();
3847 wxKeyEvent
keyEvt( event
);
3848 keyEvt
.SetEventObject( parent
);
3850 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3853 // TODO: Should also support Shift-cursor keys for
3854 // extending the selection. Maybe add a flag to
3855 // MoveCursorXXX() and MoveCursorXXXBlock() and
3856 // just send event.ShiftDown().
3858 // try local handlers
3860 switch ( event
.KeyCode() )
3863 if ( event
.ControlDown() )
3865 MoveCursorUpBlock();
3874 if ( event
.ControlDown() )
3876 MoveCursorDownBlock();
3885 if ( event
.ControlDown() )
3887 MoveCursorLeftBlock();
3896 if ( event
.ControlDown() )
3898 MoveCursorRightBlock();
3907 if ( event
.ControlDown() )
3909 event
.Skip(); // to let the edit control have the return
3918 if (event
.ShiftDown())
3925 if ( event
.ControlDown() )
3927 MakeCellVisible( 0, 0 );
3928 SetCurrentCell( 0, 0 );
3937 if ( event
.ControlDown() )
3939 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3940 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3956 // We don't want these keys to trigger the edit control, any others?
3965 if ( !IsEditable() )
3970 // Otherwise fall through to default
3973 // now try the cell edit control
3975 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
3977 EnableCellEditControl();
3978 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3979 attr
->GetEditor()->StartingKey(event
);
3984 // let others process char events for readonly cells
3991 m_inOnKeyDown
= FALSE
;
3995 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3999 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4001 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4003 // the event has been intercepted - do nothing
4008 m_currentCellCoords
!= wxGridNoCellCoords
)
4010 HideCellEditControl();
4011 SaveEditControlValue();
4012 DisableCellEditControl();
4014 // Clear the old current cell highlight
4015 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4017 // Otherwise refresh redraws the highlight!
4018 m_currentCellCoords
= coords
;
4020 m_gridWin
->Refresh( FALSE
, &r
);
4023 m_currentCellCoords
= coords
;
4025 SetEditControlValue();
4029 wxClientDC
dc(m_gridWin
);
4032 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4033 DrawCellHighlight(dc
, attr
);
4036 if ( IsSelection() )
4038 wxRect
r( SelectionToDeviceRect() );
4040 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4047 // ------ functions to get/send data (see also public functions)
4050 bool wxGrid::GetModelValues()
4054 // all we need to do is repaint the grid
4056 m_gridWin
->Refresh();
4064 bool wxGrid::SetModelValues()
4070 for ( row
= 0; row
< m_numRows
; row
++ )
4072 for ( col
= 0; col
< m_numCols
; col
++ )
4074 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4086 // Note - this function only draws cells that are in the list of
4087 // exposed cells (usually set from the update region by
4088 // CalcExposedCells)
4090 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4092 if ( !m_numRows
|| !m_numCols
) return;
4095 size_t numCells
= m_cellsExposed
.GetCount();
4097 for ( i
= 0; i
< numCells
; i
++ )
4099 DrawCell( dc
, m_cellsExposed
[i
] );
4104 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4106 int row
= coords
.GetRow();
4107 int col
= coords
.GetCol();
4109 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4112 // we draw the cell border ourselves
4113 #if !WXGRID_DRAW_LINES
4114 if ( m_gridLinesEnabled
)
4115 DrawCellBorder( dc
, coords
);
4118 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4120 bool isCurrent
= coords
== m_currentCellCoords
;
4123 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4124 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4125 rect
.width
= m_colWidths
[col
] - 1;
4126 rect
.height
= m_rowHeights
[row
] - 1;
4128 // if the editor is shown, we should use it and not the renderer
4129 if ( isCurrent
&& IsCellEditControlEnabled() )
4131 attr
->GetEditor()->PaintBackground(rect
, attr
);
4135 // but all the rest is drawn by the cell renderer and hence may be
4137 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4143 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4145 int row
= m_currentCellCoords
.GetRow();
4146 int col
= m_currentCellCoords
.GetCol();
4148 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4152 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4153 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4154 rect
.width
= m_colWidths
[col
] - 1;
4155 rect
.height
= m_rowHeights
[row
] - 1;
4157 // hmmm... what could we do here to show that the cell is disabled?
4158 // for now, I just draw a thinner border than for the other ones, but
4159 // it doesn't look really good
4160 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4161 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4163 dc
.DrawRectangle(rect
);
4166 // VZ: my experiments with 3d borders...
4168 // how to properly set colours for arbitrary bg?
4169 wxCoord x1
= rect
.x
,
4171 x2
= rect
.x
+ rect
.width
-1,
4172 y2
= rect
.y
+ rect
.height
-1;
4174 dc
.SetPen(*wxWHITE_PEN
);
4175 dc
.DrawLine(x1
, y1
, x2
, y1
);
4176 dc
.DrawLine(x1
, y1
, x1
, y2
);
4178 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4179 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4181 dc
.SetPen(*wxBLACK_PEN
);
4182 dc
.DrawLine(x1
, y2
, x2
, y2
);
4183 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4187 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4189 if ( m_colWidths
[coords
.GetCol()] <=0 ||
4190 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
4192 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4193 int row
= coords
.GetRow();
4194 int col
= coords
.GetCol();
4196 // right hand border
4198 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
4199 m_colRights
[col
], m_rowBottoms
[row
] );
4203 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
4204 m_colRights
[col
], m_rowBottoms
[row
] );
4207 void wxGrid::DrawHighlight(wxDC
& dc
)
4209 // if the active cell was repainted, repaint its highlight too because it
4210 // might have been damaged by the grid lines
4211 size_t count
= m_cellsExposed
.GetCount();
4212 for ( size_t n
= 0; n
< count
; n
++ )
4214 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4216 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4217 DrawCellHighlight(dc
, attr
);
4225 // TODO: remove this ???
4226 // This is used to redraw all grid lines e.g. when the grid line colour
4229 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4231 if ( !m_gridLinesEnabled
||
4233 !m_numCols
) return;
4235 int top
, bottom
, left
, right
;
4240 m_gridWin
->GetClientSize(&cw
, &ch
);
4242 // virtual coords of visible area
4244 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4245 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4250 reg
.GetBox(x
, y
, w
, h
);
4251 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4252 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4255 // avoid drawing grid lines past the last row and col
4257 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
4258 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
4260 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4262 // horizontal grid lines
4265 for ( i
= 0; i
< m_numRows
; i
++ )
4267 if ( m_rowBottoms
[i
]-1 > bottom
)
4271 else if ( m_rowBottoms
[i
]-1 >= top
)
4273 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
4278 // vertical grid lines
4280 for ( i
= 0; i
< m_numCols
; i
++ )
4282 if ( m_colRights
[i
]-1 > right
)
4286 else if ( m_colRights
[i
]-1 >= left
)
4288 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
4294 void wxGrid::DrawRowLabels( wxDC
& dc
)
4296 if ( !m_numRows
|| !m_numCols
) return;
4299 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4301 for ( i
= 0; i
< numLabels
; i
++ )
4303 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4308 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4310 if ( m_rowHeights
[row
] <= 0 ) return;
4312 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4314 dc
.SetPen( *wxBLACK_PEN
);
4315 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4316 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4318 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4319 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4321 dc
.SetPen( *wxWHITE_PEN
);
4322 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4323 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4325 dc
.SetBackgroundMode( wxTRANSPARENT
);
4326 dc
.SetTextForeground( GetLabelTextColour() );
4327 dc
.SetFont( GetLabelFont() );
4330 GetRowLabelAlignment( &hAlign
, &vAlign
);
4334 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4335 rect
.SetWidth( m_rowLabelWidth
- 4 );
4336 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4337 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4341 void wxGrid::DrawColLabels( wxDC
& dc
)
4343 if ( !m_numRows
|| !m_numCols
) return;
4346 size_t numLabels
= m_colLabelsExposed
.GetCount();
4348 for ( i
= 0; i
< numLabels
; i
++ )
4350 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4355 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4357 if ( m_colWidths
[col
] <= 0 ) return;
4359 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4361 dc
.SetPen( *wxBLACK_PEN
);
4362 dc
.DrawLine( m_colRights
[col
]-1, 0,
4363 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4365 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4366 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4368 dc
.SetPen( *wxWHITE_PEN
);
4369 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4370 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4372 dc
.SetBackgroundMode( wxTRANSPARENT
);
4373 dc
.SetTextForeground( GetLabelTextColour() );
4374 dc
.SetFont( GetLabelFont() );
4376 dc
.SetBackgroundMode( wxTRANSPARENT
);
4377 dc
.SetTextForeground( GetLabelTextColour() );
4378 dc
.SetFont( GetLabelFont() );
4381 GetColLabelAlignment( &hAlign
, &vAlign
);
4384 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4386 rect
.SetWidth( m_colWidths
[col
] - 4 );
4387 rect
.SetHeight( m_colLabelHeight
- 4 );
4388 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4392 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4393 const wxString
& value
,
4398 long textWidth
, textHeight
;
4399 long lineWidth
, lineHeight
;
4400 wxArrayString lines
;
4402 dc
.SetClippingRegion( rect
);
4403 StringToLines( value
, lines
);
4404 if ( lines
.GetCount() )
4406 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4407 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4410 switch ( horizAlign
)
4413 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4417 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4426 switch ( vertAlign
)
4429 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4433 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4442 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4444 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4449 dc
.DestroyClippingRegion();
4453 // Split multi line text up into an array of strings. Any existing
4454 // contents of the string array are preserved.
4456 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4460 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4461 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4463 while ( startPos
< (int)tVal
.Length() )
4465 pos
= tVal
.Mid(startPos
).Find( eol
);
4470 else if ( pos
== 0 )
4472 lines
.Add( wxEmptyString
);
4476 lines
.Add( value
.Mid(startPos
, pos
) );
4480 if ( startPos
< (int)value
.Length() )
4482 lines
.Add( value
.Mid( startPos
) );
4487 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4488 wxArrayString
& lines
,
4489 long *width
, long *height
)
4496 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4498 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4499 w
= wxMax( w
, lineW
);
4509 // ------ Edit control functions
4513 void wxGrid::EnableEditing( bool edit
)
4515 // TODO: improve this ?
4517 if ( edit
!= m_editable
)
4521 // FIXME IMHO this won't disable the edit control if edit == FALSE
4522 // because of the check in the beginning of
4523 // EnableCellEditControl() just below (VZ)
4524 EnableCellEditControl(m_editable
);
4529 void wxGrid::EnableCellEditControl( bool enable
)
4534 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4535 SetCurrentCell( 0, 0 );
4537 if ( enable
!= m_cellEditCtrlEnabled
)
4539 // TODO allow the app to Veto() this event?
4540 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4544 // this should be checked by the caller!
4545 wxASSERT_MSG( CanEnableCellControl(),
4546 _T("can't enable editing for this cell!") );
4548 // do it before ShowCellEditControl()
4549 m_cellEditCtrlEnabled
= enable
;
4551 SetEditControlValue();
4552 ShowCellEditControl();
4556 HideCellEditControl();
4557 SaveEditControlValue();
4559 // do it after HideCellEditControl()
4560 m_cellEditCtrlEnabled
= enable
;
4565 bool wxGrid::IsCurrentCellReadOnly() const
4568 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4569 bool readonly
= attr
->IsReadOnly();
4575 bool wxGrid::CanEnableCellControl() const
4577 return m_editable
&& !IsCurrentCellReadOnly();
4580 bool wxGrid::IsCellEditControlEnabled() const
4582 // the cell edit control might be disable for all cells or just for the
4583 // current one if it's read only
4584 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
4587 wxWindow
*wxGrid::GetGridWindow() const
4592 void wxGrid::ShowCellEditControl()
4594 if ( IsCellEditControlEnabled() )
4596 if ( !IsVisible( m_currentCellCoords
) )
4602 wxRect rect
= CellToRect( m_currentCellCoords
);
4603 int row
= m_currentCellCoords
.GetRow();
4604 int col
= m_currentCellCoords
.GetCol();
4606 // convert to scrolled coords
4608 int left
, top
, right
, bottom
;
4609 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4610 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4612 // cell is shifted by one pixel
4618 // Make the edit control large enough to allow for internal
4621 // TODO: remove this if the text ctrl sizing is improved esp. for
4625 #if defined(__WXMOTIF__)
4626 if ( row
== 0 || col
== 0 )
4635 if ( row
== 0 || col
== 0 )
4645 #if defined(__WXGTK__)
4648 if (left
!= 0) left_diff
++;
4649 if (top
!= 0) top_diff
++;
4650 rect
.SetLeft( left
+ left_diff
);
4651 rect
.SetTop( top
+ top_diff
);
4652 rect
.SetRight( rect
.GetRight() - left_diff
);
4653 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4655 rect
.SetLeft( wxMax(0, left
- extra
) );
4656 rect
.SetTop( wxMax(0, top
- extra
) );
4657 rect
.SetRight( rect
.GetRight() + 2*extra
);
4658 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4661 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4662 wxGridCellEditor
* editor
= attr
->GetEditor();
4663 if ( !editor
->IsCreated() )
4665 editor
->Create(m_gridWin
, -1,
4666 new wxGridCellEditorEvtHandler(this, editor
));
4669 editor
->SetSize( rect
);
4670 editor
->Show( TRUE
, attr
);
4671 editor
->BeginEdit(row
, col
, this);
4678 void wxGrid::HideCellEditControl()
4680 if ( IsCellEditControlEnabled() )
4682 int row
= m_currentCellCoords
.GetRow();
4683 int col
= m_currentCellCoords
.GetCol();
4685 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4686 attr
->GetEditor()->Show( FALSE
);
4688 m_gridWin
->SetFocus();
4693 void wxGrid::SetEditControlValue( const wxString
& value
)
4695 // RD: The new Editors get the value from the table themselves now. This
4696 // method can probably be removed...
4700 void wxGrid::SaveEditControlValue()
4702 if ( IsCellEditControlEnabled() )
4704 int row
= m_currentCellCoords
.GetRow();
4705 int col
= m_currentCellCoords
.GetCol();
4707 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4708 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4714 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4715 m_currentCellCoords
.GetRow(),
4716 m_currentCellCoords
.GetCol() );
4723 // ------ Grid location functions
4724 // Note that all of these functions work with the logical coordinates of
4725 // grid cells and labels so you will need to convert from device
4726 // coordinates for mouse events etc.
4729 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4731 int row
= YToRow(y
);
4732 int col
= XToCol(x
);
4734 if ( row
== -1 || col
== -1 )
4736 coords
= wxGridNoCellCoords
;
4740 coords
.Set( row
, col
);
4745 int wxGrid::YToRow( int y
)
4749 for ( i
= 0; i
< m_numRows
; i
++ )
4751 if ( y
< m_rowBottoms
[i
] ) return i
;
4754 return m_numRows
; //-1;
4758 int wxGrid::XToCol( int x
)
4762 for ( i
= 0; i
< m_numCols
; i
++ )
4764 if ( x
< m_colRights
[i
] ) return i
;
4767 return m_numCols
; //-1;
4771 // return the row number that that the y coord is near the edge of, or
4772 // -1 if not near an edge
4774 int wxGrid::YToEdgeOfRow( int y
)
4778 for ( i
= 0; i
< m_numRows
; i
++ )
4780 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4782 d
= abs( y
- m_rowBottoms
[i
] );
4784 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4793 // return the col number that that the x coord is near the edge of, or
4794 // -1 if not near an edge
4796 int wxGrid::XToEdgeOfCol( int x
)
4800 for ( i
= 0; i
< m_numCols
; i
++ )
4802 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4804 d
= abs( x
- m_colRights
[i
] );
4806 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4815 wxRect
wxGrid::CellToRect( int row
, int col
)
4817 wxRect
rect( -1, -1, -1, -1 );
4819 if ( row
>= 0 && row
< m_numRows
&&
4820 col
>= 0 && col
< m_numCols
)
4822 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4823 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4824 rect
.width
= m_colWidths
[col
];
4825 rect
.height
= m_rowHeights
[ row
];
4832 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4834 // get the cell rectangle in logical coords
4836 wxRect
r( CellToRect( row
, col
) );
4838 // convert to device coords
4840 int left
, top
, right
, bottom
;
4841 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4842 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4844 // check against the client area of the grid window
4847 m_gridWin
->GetClientSize( &cw
, &ch
);
4849 if ( wholeCellVisible
)
4851 // is the cell wholly visible ?
4853 return ( left
>= 0 && right
<= cw
&&
4854 top
>= 0 && bottom
<= ch
);
4858 // is the cell partly visible ?
4860 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4861 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4866 // make the specified cell location visible by doing a minimal amount
4869 void wxGrid::MakeCellVisible( int row
, int col
)
4872 int xpos
= -1, ypos
= -1;
4874 if ( row
>= 0 && row
< m_numRows
&&
4875 col
>= 0 && col
< m_numCols
)
4877 // get the cell rectangle in logical coords
4879 wxRect
r( CellToRect( row
, col
) );
4881 // convert to device coords
4883 int left
, top
, right
, bottom
;
4884 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4885 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4888 m_gridWin
->GetClientSize( &cw
, &ch
);
4894 else if ( bottom
> ch
)
4896 int h
= r
.GetHeight();
4898 for ( i
= row
-1; i
>= 0; i
-- )
4900 if ( h
+ m_rowHeights
[i
] > ch
) break;
4902 h
+= m_rowHeights
[i
];
4903 ypos
-= m_rowHeights
[i
];
4906 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4907 // have rounding errors (this is important, because if we do, we
4908 // might not scroll at all and some cells won't be redrawn)
4909 ypos
+= GRID_SCROLL_LINE
/ 2;
4916 else if ( right
> cw
)
4918 int w
= r
.GetWidth();
4920 for ( i
= col
-1; i
>= 0; i
-- )
4922 if ( w
+ m_colWidths
[i
] > cw
) break;
4924 w
+= m_colWidths
[i
];
4925 xpos
-= m_colWidths
[i
];
4928 // see comment for ypos above
4929 xpos
+= GRID_SCROLL_LINE
/ 2;
4932 if ( xpos
!= -1 || ypos
!= -1 )
4934 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4935 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4936 Scroll( xpos
, ypos
);
4944 // ------ Grid cursor movement functions
4947 bool wxGrid::MoveCursorUp()
4949 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4950 m_currentCellCoords
.GetRow() > 0 )
4952 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4953 m_currentCellCoords
.GetCol() );
4955 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4956 m_currentCellCoords
.GetCol() );
4965 bool wxGrid::MoveCursorDown()
4967 // TODO: allow for scrolling
4969 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4970 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4972 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4973 m_currentCellCoords
.GetCol() );
4975 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4976 m_currentCellCoords
.GetCol() );
4985 bool wxGrid::MoveCursorLeft()
4987 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4988 m_currentCellCoords
.GetCol() > 0 )
4990 MakeCellVisible( m_currentCellCoords
.GetRow(),
4991 m_currentCellCoords
.GetCol() - 1 );
4993 SetCurrentCell( m_currentCellCoords
.GetRow(),
4994 m_currentCellCoords
.GetCol() - 1 );
5003 bool wxGrid::MoveCursorRight()
5005 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5006 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5008 MakeCellVisible( m_currentCellCoords
.GetRow(),
5009 m_currentCellCoords
.GetCol() + 1 );
5011 SetCurrentCell( m_currentCellCoords
.GetRow(),
5012 m_currentCellCoords
.GetCol() + 1 );
5021 bool wxGrid::MovePageUp()
5023 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5025 int row
= m_currentCellCoords
.GetRow();
5029 m_gridWin
->GetClientSize( &cw
, &ch
);
5031 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
5032 int newRow
= YToRow( y
- ch
+ 1 );
5037 else if ( newRow
== row
)
5042 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5043 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5051 bool wxGrid::MovePageDown()
5053 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5055 int row
= m_currentCellCoords
.GetRow();
5056 if ( row
< m_numRows
)
5059 m_gridWin
->GetClientSize( &cw
, &ch
);
5061 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
5062 int newRow
= YToRow( y
+ ch
);
5065 newRow
= m_numRows
- 1;
5067 else if ( newRow
== row
)
5072 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5073 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5081 bool wxGrid::MoveCursorUpBlock()
5084 m_currentCellCoords
!= wxGridNoCellCoords
&&
5085 m_currentCellCoords
.GetRow() > 0 )
5087 int row
= m_currentCellCoords
.GetRow();
5088 int col
= m_currentCellCoords
.GetCol();
5090 if ( m_table
->IsEmptyCell(row
, col
) )
5092 // starting in an empty cell: find the next block of
5098 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5101 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5103 // starting at the top of a block: find the next block
5109 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5114 // starting within a block: find the top of the block
5119 if ( m_table
->IsEmptyCell(row
, col
) )
5127 MakeCellVisible( row
, col
);
5128 SetCurrentCell( row
, col
);
5136 bool wxGrid::MoveCursorDownBlock()
5139 m_currentCellCoords
!= wxGridNoCellCoords
&&
5140 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5142 int row
= m_currentCellCoords
.GetRow();
5143 int col
= m_currentCellCoords
.GetCol();
5145 if ( m_table
->IsEmptyCell(row
, col
) )
5147 // starting in an empty cell: find the next block of
5150 while ( row
< m_numRows
-1 )
5153 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5156 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5158 // starting at the bottom of a block: find the next block
5161 while ( row
< m_numRows
-1 )
5164 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5169 // starting within a block: find the bottom of the block
5171 while ( row
< m_numRows
-1 )
5174 if ( m_table
->IsEmptyCell(row
, col
) )
5182 MakeCellVisible( row
, col
);
5183 SetCurrentCell( row
, col
);
5191 bool wxGrid::MoveCursorLeftBlock()
5194 m_currentCellCoords
!= wxGridNoCellCoords
&&
5195 m_currentCellCoords
.GetCol() > 0 )
5197 int row
= m_currentCellCoords
.GetRow();
5198 int col
= m_currentCellCoords
.GetCol();
5200 if ( m_table
->IsEmptyCell(row
, col
) )
5202 // starting in an empty cell: find the next block of
5208 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5211 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5213 // starting at the left of a block: find the next block
5219 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5224 // starting within a block: find the left of the block
5229 if ( m_table
->IsEmptyCell(row
, col
) )
5237 MakeCellVisible( row
, col
);
5238 SetCurrentCell( row
, col
);
5246 bool wxGrid::MoveCursorRightBlock()
5249 m_currentCellCoords
!= wxGridNoCellCoords
&&
5250 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5252 int row
= m_currentCellCoords
.GetRow();
5253 int col
= m_currentCellCoords
.GetCol();
5255 if ( m_table
->IsEmptyCell(row
, col
) )
5257 // starting in an empty cell: find the next block of
5260 while ( col
< m_numCols
-1 )
5263 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5266 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5268 // starting at the right of a block: find the next block
5271 while ( col
< m_numCols
-1 )
5274 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5279 // starting within a block: find the right of the block
5281 while ( col
< m_numCols
-1 )
5284 if ( m_table
->IsEmptyCell(row
, col
) )
5292 MakeCellVisible( row
, col
);
5293 SetCurrentCell( row
, col
);
5304 // ------ Label values and formatting
5307 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5309 *horiz
= m_rowLabelHorizAlign
;
5310 *vert
= m_rowLabelVertAlign
;
5313 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5315 *horiz
= m_colLabelHorizAlign
;
5316 *vert
= m_colLabelVertAlign
;
5319 wxString
wxGrid::GetRowLabelValue( int row
)
5323 return m_table
->GetRowLabelValue( row
);
5333 wxString
wxGrid::GetColLabelValue( int col
)
5337 return m_table
->GetColLabelValue( col
);
5348 void wxGrid::SetRowLabelSize( int width
)
5350 width
= wxMax( width
, 0 );
5351 if ( width
!= m_rowLabelWidth
)
5355 m_rowLabelWin
->Show( FALSE
);
5356 m_cornerLabelWin
->Show( FALSE
);
5358 else if ( m_rowLabelWidth
== 0 )
5360 m_rowLabelWin
->Show( TRUE
);
5361 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5364 m_rowLabelWidth
= width
;
5371 void wxGrid::SetColLabelSize( int height
)
5373 height
= wxMax( height
, 0 );
5374 if ( height
!= m_colLabelHeight
)
5378 m_colLabelWin
->Show( FALSE
);
5379 m_cornerLabelWin
->Show( FALSE
);
5381 else if ( m_colLabelHeight
== 0 )
5383 m_colLabelWin
->Show( TRUE
);
5384 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5387 m_colLabelHeight
= height
;
5394 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5396 if ( m_labelBackgroundColour
!= colour
)
5398 m_labelBackgroundColour
= colour
;
5399 m_rowLabelWin
->SetBackgroundColour( colour
);
5400 m_colLabelWin
->SetBackgroundColour( colour
);
5401 m_cornerLabelWin
->SetBackgroundColour( colour
);
5403 if ( !GetBatchCount() )
5405 m_rowLabelWin
->Refresh();
5406 m_colLabelWin
->Refresh();
5407 m_cornerLabelWin
->Refresh();
5412 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5414 if ( m_labelTextColour
!= colour
)
5416 m_labelTextColour
= colour
;
5417 if ( !GetBatchCount() )
5419 m_rowLabelWin
->Refresh();
5420 m_colLabelWin
->Refresh();
5425 void wxGrid::SetLabelFont( const wxFont
& font
)
5428 if ( !GetBatchCount() )
5430 m_rowLabelWin
->Refresh();
5431 m_colLabelWin
->Refresh();
5435 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5437 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5439 m_rowLabelHorizAlign
= horiz
;
5442 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5444 m_rowLabelVertAlign
= vert
;
5447 if ( !GetBatchCount() )
5449 m_rowLabelWin
->Refresh();
5453 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5455 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5457 m_colLabelHorizAlign
= horiz
;
5460 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5462 m_colLabelVertAlign
= vert
;
5465 if ( !GetBatchCount() )
5467 m_colLabelWin
->Refresh();
5471 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5475 m_table
->SetRowLabelValue( row
, s
);
5476 if ( !GetBatchCount() )
5478 wxRect rect
= CellToRect( row
, 0);
5479 if ( rect
.height
> 0 )
5481 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5483 rect
.width
= m_rowLabelWidth
;
5484 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5490 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5494 m_table
->SetColLabelValue( col
, s
);
5495 if ( !GetBatchCount() )
5497 wxRect rect
= CellToRect( 0, col
);
5498 if ( rect
.width
> 0 )
5500 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5502 rect
.height
= m_colLabelHeight
;
5503 m_colLabelWin
->Refresh( TRUE
, &rect
);
5509 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5511 if ( m_gridLineColour
!= colour
)
5513 m_gridLineColour
= colour
;
5515 wxClientDC
dc( m_gridWin
);
5517 DrawAllGridLines( dc
, wxRegion() );
5521 void wxGrid::EnableGridLines( bool enable
)
5523 if ( enable
!= m_gridLinesEnabled
)
5525 m_gridLinesEnabled
= enable
;
5527 if ( !GetBatchCount() )
5531 wxClientDC
dc( m_gridWin
);
5533 DrawAllGridLines( dc
, wxRegion() );
5537 m_gridWin
->Refresh();
5544 int wxGrid::GetDefaultRowSize()
5546 return m_defaultRowHeight
;
5549 int wxGrid::GetRowSize( int row
)
5551 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5553 return m_rowHeights
[row
];
5556 int wxGrid::GetDefaultColSize()
5558 return m_defaultColWidth
;
5561 int wxGrid::GetColSize( int col
)
5563 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5565 return m_colWidths
[col
];
5568 // ============================================================================
5569 // access to the grid attributes: each of them has a default value in the grid
5570 // itself and may be overidden on a per-cell basis
5571 // ============================================================================
5573 // ----------------------------------------------------------------------------
5574 // setting default attributes
5575 // ----------------------------------------------------------------------------
5577 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5579 m_defaultCellAttr
->SetBackgroundColour(col
);
5581 m_gridWin
->SetBackgroundColour(col
);
5585 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5587 m_defaultCellAttr
->SetTextColour(col
);
5590 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5592 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5595 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5597 m_defaultCellAttr
->SetFont(font
);
5600 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5602 m_defaultCellAttr
->SetRenderer(renderer
);
5605 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5607 m_defaultCellAttr
->SetEditor(editor
);
5610 // ----------------------------------------------------------------------------
5611 // access to the default attrbiutes
5612 // ----------------------------------------------------------------------------
5614 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5616 return m_defaultCellAttr
->GetBackgroundColour();
5619 wxColour
wxGrid::GetDefaultCellTextColour()
5621 return m_defaultCellAttr
->GetTextColour();
5624 wxFont
wxGrid::GetDefaultCellFont()
5626 return m_defaultCellAttr
->GetFont();
5629 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5631 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5634 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5636 return m_defaultCellAttr
->GetRenderer();
5639 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5641 return m_defaultCellAttr
->GetEditor();
5644 // ----------------------------------------------------------------------------
5645 // access to cell attributes
5646 // ----------------------------------------------------------------------------
5648 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5650 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5651 wxColour colour
= attr
->GetBackgroundColour();
5656 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5658 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5659 wxColour colour
= attr
->GetTextColour();
5664 wxFont
wxGrid::GetCellFont( int row
, int col
)
5666 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5667 wxFont font
= attr
->GetFont();
5672 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5674 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5675 attr
->GetAlignment(horiz
, vert
);
5679 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5681 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5682 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5687 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5689 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5690 wxGridCellEditor
* editor
= attr
->GetEditor();
5695 bool wxGrid::IsReadOnly(int row
, int col
) const
5697 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5698 bool isReadOnly
= attr
->IsReadOnly();
5703 // ----------------------------------------------------------------------------
5704 // attribute support: cache, automatic provider creation, ...
5705 // ----------------------------------------------------------------------------
5707 bool wxGrid::CanHaveAttributes()
5714 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5715 // table is providing the attributes itself??? In which case
5716 // I don't think the grid should create a Provider object for the
5717 // table but the table should be smart enough to do that on its own.
5718 if ( !m_table
->GetAttrProvider() )
5720 // use the default attr provider by default
5721 // (another choice would be to just return FALSE thus forcing the user
5723 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5729 void wxGrid::ClearAttrCache()
5731 if ( m_attrCache
.row
!= -1 )
5733 m_attrCache
.attr
->SafeDecRef();
5734 m_attrCache
.row
= -1;
5738 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5740 wxGrid
*self
= (wxGrid
*)this; // const_cast
5742 self
->ClearAttrCache();
5743 self
->m_attrCache
.row
= row
;
5744 self
->m_attrCache
.col
= col
;
5745 self
->m_attrCache
.attr
= attr
;
5749 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5751 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5753 *attr
= m_attrCache
.attr
;
5754 (*attr
)->SafeIncRef();
5756 #ifdef DEBUG_ATTR_CACHE
5757 gs_nAttrCacheHits
++;
5764 #ifdef DEBUG_ATTR_CACHE
5765 gs_nAttrCacheMisses
++;
5771 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5773 wxGridCellAttr
*attr
;
5774 if ( !LookupAttr(row
, col
, &attr
) )
5776 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5777 CacheAttr(row
, col
, attr
);
5781 attr
->SetDefAttr(m_defaultCellAttr
);
5785 attr
= m_defaultCellAttr
;
5792 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5794 wxGridCellAttr
*attr
;
5795 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5797 wxASSERT_MSG( m_table
,
5798 _T("we may only be called if CanHaveAttributes() "
5799 "returned TRUE and then m_table should be !NULL") );
5801 attr
= m_table
->GetAttr(row
, col
);
5804 attr
= new wxGridCellAttr
;
5806 // artificially inc the ref count to match DecRef() in caller
5809 m_table
->SetAttr(attr
, row
, col
);
5812 CacheAttr(row
, col
, attr
);
5814 attr
->SetDefAttr(m_defaultCellAttr
);
5818 // ----------------------------------------------------------------------------
5819 // setting cell attributes: this is forwarded to the table
5820 // ----------------------------------------------------------------------------
5822 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5824 if ( CanHaveAttributes() )
5826 m_table
->SetRowAttr(attr
, row
);
5834 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5836 if ( CanHaveAttributes() )
5838 m_table
->SetColAttr(attr
, col
);
5846 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5848 if ( CanHaveAttributes() )
5850 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5851 attr
->SetBackgroundColour(colour
);
5856 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5858 if ( CanHaveAttributes() )
5860 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5861 attr
->SetTextColour(colour
);
5866 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5868 if ( CanHaveAttributes() )
5870 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5871 attr
->SetFont(font
);
5876 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5878 if ( CanHaveAttributes() )
5880 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5881 attr
->SetAlignment(horiz
, vert
);
5886 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5888 if ( CanHaveAttributes() )
5890 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5891 attr
->SetRenderer(renderer
);
5896 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5898 if ( CanHaveAttributes() )
5900 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5901 attr
->SetEditor(editor
);
5906 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5908 if ( CanHaveAttributes() )
5910 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5911 attr
->SetReadOnly(isReadOnly
);
5916 // ----------------------------------------------------------------------------
5918 // ----------------------------------------------------------------------------
5920 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5922 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5924 if ( resizeExistingRows
)
5928 for ( row
= 0; row
< m_numRows
; row
++ )
5930 m_rowHeights
[row
] = m_defaultRowHeight
;
5931 bottom
+= m_defaultRowHeight
;
5932 m_rowBottoms
[row
] = bottom
;
5938 void wxGrid::SetRowSize( int row
, int height
)
5940 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5944 int h
= wxMax( 0, height
);
5945 int diff
= h
- m_rowHeights
[row
];
5947 m_rowHeights
[row
] = h
;
5948 for ( i
= row
; i
< m_numRows
; i
++ )
5950 m_rowBottoms
[i
] += diff
;
5955 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5957 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5959 if ( resizeExistingCols
)
5963 for ( col
= 0; col
< m_numCols
; col
++ )
5965 m_colWidths
[col
] = m_defaultColWidth
;
5966 right
+= m_defaultColWidth
;
5967 m_colRights
[col
] = right
;
5973 void wxGrid::SetColSize( int col
, int width
)
5975 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5979 int w
= wxMax( 0, width
);
5980 int diff
= w
- m_colWidths
[col
];
5981 m_colWidths
[col
] = w
;
5983 for ( i
= col
; i
< m_numCols
; i
++ )
5985 m_colRights
[i
] += diff
;
5992 // ------ cell value accessor functions
5995 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5999 m_table
->SetValue( row
, col
, s
.c_str() );
6000 if ( !GetBatchCount() )
6002 wxClientDC
dc( m_gridWin
);
6004 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6007 #if 0 // TODO: edit in place
6009 if ( m_currentCellCoords
.GetRow() == row
&&
6010 m_currentCellCoords
.GetCol() == col
)
6012 SetEditControlValue( s
);
6021 // ------ Block, row and col selection
6024 void wxGrid::SelectRow( int row
, bool addToSelected
)
6028 if ( IsSelection() && addToSelected
)
6031 bool need_refresh
[4];
6035 need_refresh
[3] = FALSE
;
6039 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6040 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6041 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6042 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6046 need_refresh
[0] = TRUE
;
6047 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6048 wxGridCellCoords ( oldTop
- 1,
6050 m_selectedTopLeft
.SetRow( row
);
6055 need_refresh
[1] = TRUE
;
6056 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6057 wxGridCellCoords ( oldBottom
,
6060 m_selectedTopLeft
.SetCol( 0 );
6063 if ( oldBottom
< row
)
6065 need_refresh
[2] = TRUE
;
6066 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6067 wxGridCellCoords ( row
,
6069 m_selectedBottomRight
.SetRow( row
);
6072 if ( oldRight
< m_numCols
- 1 )
6074 need_refresh
[3] = TRUE
;
6075 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6077 wxGridCellCoords ( oldBottom
,
6079 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6082 for (i
= 0; i
< 4; i
++ )
6083 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6084 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6088 r
= SelectionToDeviceRect();
6090 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6092 m_selectedTopLeft
.Set( row
, 0 );
6093 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6094 r
= SelectionToDeviceRect();
6095 m_gridWin
->Refresh( FALSE
, &r
);
6098 wxGridRangeSelectEvent
gridEvt( GetId(),
6099 wxEVT_GRID_RANGE_SELECT
,
6102 m_selectedBottomRight
);
6104 GetEventHandler()->ProcessEvent(gridEvt
);
6108 void wxGrid::SelectCol( int col
, bool addToSelected
)
6110 if ( IsSelection() && addToSelected
)
6113 bool need_refresh
[4];
6117 need_refresh
[3] = FALSE
;
6120 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6121 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6122 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6123 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6125 if ( oldLeft
> col
)
6127 need_refresh
[0] = TRUE
;
6128 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6129 wxGridCellCoords ( m_numRows
- 1,
6131 m_selectedTopLeft
.SetCol( col
);
6136 need_refresh
[1] = TRUE
;
6137 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6138 wxGridCellCoords ( oldTop
- 1,
6140 m_selectedTopLeft
.SetRow( 0 );
6143 if ( oldRight
< col
)
6145 need_refresh
[2] = TRUE
;
6146 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6147 wxGridCellCoords ( m_numRows
- 1,
6149 m_selectedBottomRight
.SetCol( col
);
6152 if ( oldBottom
< m_numRows
- 1 )
6154 need_refresh
[3] = TRUE
;
6155 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6157 wxGridCellCoords ( m_numRows
- 1,
6159 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6162 for (i
= 0; i
< 4; i
++ )
6163 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6164 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6170 r
= SelectionToDeviceRect();
6172 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6174 m_selectedTopLeft
.Set( 0, col
);
6175 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6176 r
= SelectionToDeviceRect();
6177 m_gridWin
->Refresh( FALSE
, &r
);
6180 wxGridRangeSelectEvent
gridEvt( GetId(),
6181 wxEVT_GRID_RANGE_SELECT
,
6184 m_selectedBottomRight
);
6186 GetEventHandler()->ProcessEvent(gridEvt
);
6190 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6193 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6195 if ( topRow
> bottomRow
)
6202 if ( leftCol
> rightCol
)
6209 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6210 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6212 if ( m_selectedTopLeft
!= updateTopLeft
||
6213 m_selectedBottomRight
!= updateBottomRight
)
6215 // Compute two optimal update rectangles:
6216 // Either one rectangle is a real subset of the
6217 // other, or they are (almost) disjoint!
6219 bool need_refresh
[4];
6223 need_refresh
[3] = FALSE
;
6226 // Store intermediate values
6227 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6228 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6229 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6230 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6232 // Determine the outer/inner coordinates.
6233 if (oldLeft
> leftCol
)
6239 if (oldTop
> topRow
)
6245 if (oldRight
< rightCol
)
6248 oldRight
= rightCol
;
6251 if (oldBottom
< bottomRow
)
6254 oldBottom
= bottomRow
;
6258 // Now, either the stuff marked old is the outer
6259 // rectangle or we don't have a situation where one
6260 // is contained in the other.
6262 if ( oldLeft
< leftCol
)
6264 need_refresh
[0] = TRUE
;
6265 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6267 wxGridCellCoords ( oldBottom
,
6271 if ( oldTop
< topRow
)
6273 need_refresh
[1] = TRUE
;
6274 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6276 wxGridCellCoords ( topRow
- 1,
6280 if ( oldRight
> rightCol
)
6282 need_refresh
[2] = TRUE
;
6283 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6285 wxGridCellCoords ( oldBottom
,
6289 if ( oldBottom
> bottomRow
)
6291 need_refresh
[3] = TRUE
;
6292 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6294 wxGridCellCoords ( oldBottom
,
6300 m_selectedTopLeft
= updateTopLeft
;
6301 m_selectedBottomRight
= updateBottomRight
;
6303 // various Refresh() calls
6304 for (i
= 0; i
< 4; i
++ )
6305 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6306 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6309 // only generate an event if the block is not being selected by
6310 // dragging the mouse (in which case the event will be generated in
6311 // the mouse event handler)
6312 if ( !m_isDragging
)
6314 wxGridRangeSelectEvent
gridEvt( GetId(),
6315 wxEVT_GRID_RANGE_SELECT
,
6318 m_selectedBottomRight
);
6320 GetEventHandler()->ProcessEvent(gridEvt
);
6324 void wxGrid::SelectAll()
6326 m_selectedTopLeft
.Set( 0, 0 );
6327 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6329 m_gridWin
->Refresh();
6333 void wxGrid::ClearSelection()
6335 m_selectedTopLeft
= wxGridNoCellCoords
;
6336 m_selectedBottomRight
= wxGridNoCellCoords
;
6340 // This function returns the rectangle that encloses the given block
6341 // in device coords clipped to the client size of the grid window.
6343 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6344 const wxGridCellCoords
&bottomRight
)
6346 wxRect
rect( wxGridNoCellRect
);
6349 cellRect
= CellToRect( topLeft
);
6350 if ( cellRect
!= wxGridNoCellRect
)
6356 rect
= wxRect( 0, 0, 0, 0 );
6359 cellRect
= CellToRect( bottomRight
);
6360 if ( cellRect
!= wxGridNoCellRect
)
6366 return wxGridNoCellRect
;
6369 // convert to scrolled coords
6371 int left
, top
, right
, bottom
;
6372 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6373 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6376 m_gridWin
->GetClientSize( &cw
, &ch
);
6378 rect
.SetLeft( wxMax(0, left
) );
6379 rect
.SetTop( wxMax(0, top
) );
6380 rect
.SetRight( wxMin(cw
, right
) );
6381 rect
.SetBottom( wxMin(ch
, bottom
) );
6389 // ------ Grid event classes
6392 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6394 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6395 int row
, int col
, int x
, int y
,
6396 bool control
, bool shift
, bool alt
, bool meta
)
6397 : wxNotifyEvent( type
, id
)
6403 m_control
= control
;
6408 SetEventObject(obj
);
6412 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6414 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6415 int rowOrCol
, int x
, int y
,
6416 bool control
, bool shift
, bool alt
, bool meta
)
6417 : wxNotifyEvent( type
, id
)
6419 m_rowOrCol
= rowOrCol
;
6422 m_control
= control
;
6427 SetEventObject(obj
);
6431 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6433 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6434 const wxGridCellCoords
& topLeft
,
6435 const wxGridCellCoords
& bottomRight
,
6436 bool control
, bool shift
, bool alt
, bool meta
)
6437 : wxNotifyEvent( type
, id
)
6439 m_topLeft
= topLeft
;
6440 m_bottomRight
= bottomRight
;
6441 m_control
= control
;
6446 SetEventObject(obj
);
6450 #endif // ifndef wxUSE_NEW_GRID