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 );
2152 if ( m_numRows
<= 0 )
2153 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2155 if ( m_numCols
<= 0 )
2156 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2158 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2159 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2161 if ( m_rowLabelWin
)
2163 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2167 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2170 m_labelTextColour
= wxColour( _T("BLACK") );
2173 m_attrCache
.row
= -1;
2175 // TODO: something better than this ?
2177 m_labelFont
= this->GetFont();
2178 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2180 m_rowLabelHorizAlign
= wxLEFT
;
2181 m_rowLabelVertAlign
= wxCENTRE
;
2183 m_colLabelHorizAlign
= wxCENTRE
;
2184 m_colLabelVertAlign
= wxTOP
;
2186 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2187 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2189 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2190 m_defaultRowHeight
+= 8;
2192 m_defaultRowHeight
+= 4;
2195 // Set default cell attributes
2196 m_defaultCellAttr
->SetFont(GetFont());
2197 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2198 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2199 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2200 m_defaultCellAttr
->SetTextColour(
2201 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2202 m_defaultCellAttr
->SetBackgroundColour(
2203 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2206 m_gridLineColour
= wxColour( 128, 128, 255 );
2207 m_gridLinesEnabled
= TRUE
;
2209 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2210 m_winCapture
= (wxWindow
*)NULL
;
2212 m_dragRowOrCol
= -1;
2213 m_isDragging
= FALSE
;
2214 m_startDragPos
= wxDefaultPosition
;
2216 m_waitForSlowClick
= FALSE
;
2218 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2219 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2221 m_currentCellCoords
= wxGridNoCellCoords
;
2223 m_selectedTopLeft
= wxGridNoCellCoords
;
2224 m_selectedBottomRight
= wxGridNoCellCoords
;
2225 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2226 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2228 m_editable
= TRUE
; // default for whole grid
2230 m_inOnKeyDown
= FALSE
;
2234 // ----------------------------------------------------------------------------
2235 // the idea is to call these functions only when necessary because they create
2236 // quite big arrays which eat memory mostly unnecessary - in particular, if
2237 // default widths/heights are used for all rows/columns, we may not use these
2240 // with some extra code, it should be possible to only store the
2241 // widths/heights different from default ones but this will be done later...
2242 // ----------------------------------------------------------------------------
2244 void wxGrid::InitRowHeights()
2246 m_rowHeights
.Empty();
2247 m_rowBottoms
.Empty();
2249 m_rowHeights
.Alloc( m_numRows
);
2250 m_rowBottoms
.Alloc( m_numRows
);
2253 for ( int i
= 0; i
< m_numRows
; i
++ )
2255 m_rowHeights
.Add( m_defaultRowHeight
);
2256 rowBottom
+= m_defaultRowHeight
;
2257 m_rowBottoms
.Add( rowBottom
);
2261 void wxGrid::InitColWidths()
2263 m_colWidths
.Empty();
2264 m_colRights
.Empty();
2266 m_colWidths
.Alloc( m_numCols
);
2267 m_colRights
.Alloc( m_numCols
);
2269 for ( int i
= 0; i
< m_numCols
; i
++ )
2271 m_colWidths
.Add( m_defaultColWidth
);
2272 colRight
+= m_defaultColWidth
;
2273 m_colRights
.Add( colRight
);
2277 int wxGrid::GetColWidth(int col
) const
2279 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2282 int wxGrid::GetColLeft(int col
) const
2284 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2285 : m_colRights
[col
] - m_colWidths
[col
];
2288 int wxGrid::GetColRight(int col
) const
2290 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
2294 int wxGrid::GetRowHeight(int row
) const
2296 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2299 int wxGrid::GetRowTop(int row
) const
2301 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2302 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2305 int wxGrid::GetRowBottom(int row
) const
2307 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2308 : m_rowBottoms
[row
];
2311 void wxGrid::CalcDimensions()
2314 GetClientSize( &cw
, &ch
);
2316 if ( m_numRows
> 0 && m_numCols
> 0 )
2318 int right
= GetColRight( m_numCols
-1 ) + 50;
2319 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
2321 // TODO: restore the scroll position that we had before sizing
2324 GetViewStart( &x
, &y
);
2325 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2326 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2332 void wxGrid::CalcWindowSizes()
2335 GetClientSize( &cw
, &ch
);
2337 if ( m_cornerLabelWin
->IsShown() )
2338 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2340 if ( m_colLabelWin
->IsShown() )
2341 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2343 if ( m_rowLabelWin
->IsShown() )
2344 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2346 if ( m_gridWin
->IsShown() )
2347 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2351 // this is called when the grid table sends a message to say that it
2352 // has been redimensioned
2354 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2358 // if we were using the default widths/heights so far, we must change them
2360 if ( m_colWidths
.IsEmpty() )
2365 if ( m_rowHeights
.IsEmpty() )
2370 switch ( msg
.GetId() )
2372 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2374 size_t pos
= msg
.GetCommandInt();
2375 int numRows
= msg
.GetCommandInt2();
2376 for ( i
= 0; i
< numRows
; i
++ )
2378 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2379 m_rowBottoms
.Insert( 0, pos
);
2381 m_numRows
+= numRows
;
2384 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2386 for ( i
= pos
; i
< m_numRows
; i
++ )
2388 bottom
+= m_rowHeights
[i
];
2389 m_rowBottoms
[i
] = bottom
;
2395 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2397 int numRows
= msg
.GetCommandInt();
2398 for ( i
= 0; i
< numRows
; i
++ )
2400 m_rowHeights
.Add( m_defaultRowHeight
);
2401 m_rowBottoms
.Add( 0 );
2404 int oldNumRows
= m_numRows
;
2405 m_numRows
+= numRows
;
2408 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2410 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2412 bottom
+= m_rowHeights
[i
];
2413 m_rowBottoms
[i
] = bottom
;
2419 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2421 size_t pos
= msg
.GetCommandInt();
2422 int numRows
= msg
.GetCommandInt2();
2423 for ( i
= 0; i
< numRows
; i
++ )
2425 m_rowHeights
.Remove( pos
);
2426 m_rowBottoms
.Remove( pos
);
2428 m_numRows
-= numRows
;
2433 m_colWidths
.Clear();
2434 m_colRights
.Clear();
2435 m_currentCellCoords
= wxGridNoCellCoords
;
2439 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2440 m_currentCellCoords
.Set( 0, 0 );
2443 for ( i
= 0; i
< m_numRows
; i
++ )
2445 h
+= m_rowHeights
[i
];
2446 m_rowBottoms
[i
] = h
;
2454 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2456 size_t pos
= msg
.GetCommandInt();
2457 int numCols
= msg
.GetCommandInt2();
2458 for ( i
= 0; i
< numCols
; i
++ )
2460 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2461 m_colRights
.Insert( 0, pos
);
2463 m_numCols
+= numCols
;
2466 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2468 for ( i
= pos
; i
< m_numCols
; i
++ )
2470 right
+= m_colWidths
[i
];
2471 m_colRights
[i
] = right
;
2477 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2479 int numCols
= msg
.GetCommandInt();
2480 for ( i
= 0; i
< numCols
; i
++ )
2482 m_colWidths
.Add( m_defaultColWidth
);
2483 m_colRights
.Add( 0 );
2486 int oldNumCols
= m_numCols
;
2487 m_numCols
+= numCols
;
2490 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2492 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2494 right
+= m_colWidths
[i
];
2495 m_colRights
[i
] = right
;
2501 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2503 size_t pos
= msg
.GetCommandInt();
2504 int numCols
= msg
.GetCommandInt2();
2505 for ( i
= 0; i
< numCols
; i
++ )
2507 m_colWidths
.Remove( pos
);
2508 m_colRights
.Remove( pos
);
2510 m_numCols
-= numCols
;
2514 #if 0 // leave the row alone here so that AppendCols will work subsequently
2516 m_rowHeights
.Clear();
2517 m_rowBottoms
.Clear();
2519 m_currentCellCoords
= wxGridNoCellCoords
;
2523 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2524 m_currentCellCoords
.Set( 0, 0 );
2527 for ( i
= 0; i
< m_numCols
; i
++ )
2529 w
+= m_colWidths
[i
];
2542 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2544 wxRegionIterator
iter( reg
);
2547 m_rowLabelsExposed
.Empty();
2554 // TODO: remove this when we can...
2555 // There is a bug in wxMotif that gives garbage update
2556 // rectangles if you jump-scroll a long way by clicking the
2557 // scrollbar with middle button. This is a work-around
2559 #if defined(__WXMOTIF__)
2561 m_gridWin
->GetClientSize( &cw
, &ch
);
2562 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2563 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2566 // logical bounds of update region
2569 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2570 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2572 // find the row labels within these bounds
2575 for ( row
= 0; row
< m_numRows
; row
++ )
2577 if ( GetRowBottom(row
) < top
)
2580 if ( GetRowTop(row
) > bottom
)
2583 m_rowLabelsExposed
.Add( row
);
2591 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2593 wxRegionIterator
iter( reg
);
2596 m_colLabelsExposed
.Empty();
2603 // TODO: remove this when we can...
2604 // There is a bug in wxMotif that gives garbage update
2605 // rectangles if you jump-scroll a long way by clicking the
2606 // scrollbar with middle button. This is a work-around
2608 #if defined(__WXMOTIF__)
2610 m_gridWin
->GetClientSize( &cw
, &ch
);
2611 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2612 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2615 // logical bounds of update region
2618 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2619 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2621 // find the cells within these bounds
2624 for ( col
= 0; col
< m_numCols
; col
++ )
2626 if ( GetColRight(col
) < left
)
2629 if ( GetColLeft(col
) > right
)
2632 m_colLabelsExposed
.Add( col
);
2640 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2642 wxRegionIterator
iter( reg
);
2645 m_cellsExposed
.Empty();
2646 m_rowsExposed
.Empty();
2647 m_colsExposed
.Empty();
2649 int left
, top
, right
, bottom
;
2654 // TODO: remove this when we can...
2655 // There is a bug in wxMotif that gives garbage update
2656 // rectangles if you jump-scroll a long way by clicking the
2657 // scrollbar with middle button. This is a work-around
2659 #if defined(__WXMOTIF__)
2661 m_gridWin
->GetClientSize( &cw
, &ch
);
2662 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2663 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2664 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2665 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2668 // logical bounds of update region
2670 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2671 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2673 // find the cells within these bounds
2676 for ( row
= 0; row
< m_numRows
; row
++ )
2678 if ( GetRowBottom(row
) <= top
)
2681 if ( GetRowTop(row
) > bottom
)
2684 m_rowsExposed
.Add( row
);
2686 for ( col
= 0; col
< m_numCols
; col
++ )
2688 if ( GetColRight(col
) <= left
)
2691 if ( GetColLeft(col
) > right
)
2694 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
2695 m_colsExposed
.Add( col
);
2696 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2705 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2708 wxPoint
pos( event
.GetPosition() );
2709 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2711 if ( event
.Dragging() )
2713 m_isDragging
= TRUE
;
2715 if ( event
.LeftIsDown() )
2717 switch( m_cursorMode
)
2719 case WXGRID_CURSOR_RESIZE_ROW
:
2721 int cw
, ch
, left
, dummy
;
2722 m_gridWin
->GetClientSize( &cw
, &ch
);
2723 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2725 wxClientDC
dc( m_gridWin
);
2727 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
2728 dc
.SetLogicalFunction(wxINVERT
);
2729 if ( m_dragLastPos
>= 0 )
2731 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2733 dc
.DrawLine( left
, y
, left
+cw
, y
);
2738 case WXGRID_CURSOR_SELECT_ROW
:
2739 if ( (row
= YToRow( y
)) >= 0 &&
2740 !IsInSelection( row
, 0 ) )
2742 SelectRow( row
, TRUE
);
2745 // default label to suppress warnings about "enumeration value
2746 // 'xxx' not handled in switch
2754 m_isDragging
= FALSE
;
2757 // ------------ Entering or leaving the window
2759 if ( event
.Entering() || event
.Leaving() )
2761 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2765 // ------------ Left button pressed
2767 else if ( event
.LeftDown() )
2769 // don't send a label click event for a hit on the
2770 // edge of the row label - this is probably the user
2771 // wanting to resize the row
2773 if ( YToEdgeOfRow(y
) < 0 )
2777 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2779 SelectRow( row
, event
.ShiftDown() );
2780 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2785 // starting to drag-resize a row
2787 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2792 // ------------ Left double click
2794 else if (event
.LeftDClick() )
2796 if ( YToEdgeOfRow(y
) < 0 )
2799 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2804 // ------------ Left button released
2806 else if ( event
.LeftUp() )
2808 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2810 DoEndDragResizeRow();
2812 // Note: we are ending the event *after* doing
2813 // default processing in this case
2815 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2818 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2823 // ------------ Right button down
2825 else if ( event
.RightDown() )
2828 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2830 // no default action at the moment
2835 // ------------ Right double click
2837 else if ( event
.RightDClick() )
2840 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2842 // no default action at the moment
2847 // ------------ No buttons down and mouse moving
2849 else if ( event
.Moving() )
2851 m_dragRowOrCol
= YToEdgeOfRow( y
);
2852 if ( m_dragRowOrCol
>= 0 )
2854 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2856 // don't capture the mouse yet
2857 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2860 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2862 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2868 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2871 wxPoint
pos( event
.GetPosition() );
2872 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2874 if ( event
.Dragging() )
2876 m_isDragging
= TRUE
;
2878 if ( event
.LeftIsDown() )
2880 switch( m_cursorMode
)
2882 case WXGRID_CURSOR_RESIZE_COL
:
2884 int cw
, ch
, dummy
, top
;
2885 m_gridWin
->GetClientSize( &cw
, &ch
);
2886 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2888 wxClientDC
dc( m_gridWin
);
2890 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + WXGRID_MIN_COL_WIDTH
);
2891 dc
.SetLogicalFunction(wxINVERT
);
2892 if ( m_dragLastPos
>= 0 )
2894 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2896 dc
.DrawLine( x
, top
, x
, top
+ch
);
2901 case WXGRID_CURSOR_SELECT_COL
:
2902 if ( (col
= XToCol( x
)) >= 0 &&
2903 !IsInSelection( 0, col
) )
2905 SelectCol( col
, TRUE
);
2908 // default label to suppress warnings about "enumeration value
2909 // 'xxx' not handled in switch
2917 m_isDragging
= FALSE
;
2920 // ------------ Entering or leaving the window
2922 if ( event
.Entering() || event
.Leaving() )
2924 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2928 // ------------ Left button pressed
2930 else if ( event
.LeftDown() )
2932 // don't send a label click event for a hit on the
2933 // edge of the col label - this is probably the user
2934 // wanting to resize the col
2936 if ( XToEdgeOfCol(x
) < 0 )
2940 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2942 SelectCol( col
, event
.ShiftDown() );
2943 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2948 // starting to drag-resize a col
2950 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2955 // ------------ Left double click
2957 if ( event
.LeftDClick() )
2959 if ( XToEdgeOfCol(x
) < 0 )
2962 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2967 // ------------ Left button released
2969 else if ( event
.LeftUp() )
2971 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2973 DoEndDragResizeCol();
2975 // Note: we are ending the event *after* doing
2976 // default processing in this case
2978 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2981 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2986 // ------------ Right button down
2988 else if ( event
.RightDown() )
2991 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2993 // no default action at the moment
2998 // ------------ Right double click
3000 else if ( event
.RightDClick() )
3003 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3005 // no default action at the moment
3010 // ------------ No buttons down and mouse moving
3012 else if ( event
.Moving() )
3014 m_dragRowOrCol
= XToEdgeOfCol( x
);
3015 if ( m_dragRowOrCol
>= 0 )
3017 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3019 // don't capture the cursor yet
3020 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3023 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3025 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3031 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3033 if ( event
.LeftDown() )
3035 // indicate corner label by having both row and
3038 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3044 else if ( event
.LeftDClick() )
3046 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3049 else if ( event
.RightDown() )
3051 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3053 // no default action at the moment
3057 else if ( event
.RightDClick() )
3059 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3061 // no default action at the moment
3066 void wxGrid::ChangeCursorMode(CursorMode mode
,
3071 static const wxChar
*cursorModes
[] =
3080 wxLogTrace(_T("grid"),
3081 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3082 win
== m_colLabelWin
? _T("colLabelWin")
3083 : win
? _T("rowLabelWin")
3085 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3086 #endif // __WXDEBUG__
3088 if ( mode
== m_cursorMode
)
3093 // by default use the grid itself
3099 m_winCapture
->ReleaseMouse();
3100 m_winCapture
= (wxWindow
*)NULL
;
3103 m_cursorMode
= mode
;
3105 switch ( m_cursorMode
)
3107 case WXGRID_CURSOR_RESIZE_ROW
:
3108 win
->SetCursor( m_rowResizeCursor
);
3111 case WXGRID_CURSOR_RESIZE_COL
:
3112 win
->SetCursor( m_colResizeCursor
);
3116 win
->SetCursor( *wxSTANDARD_CURSOR
);
3119 // we need to capture mouse when resizing
3120 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3121 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3123 if ( captureMouse
&& resize
)
3125 win
->CaptureMouse();
3130 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3133 wxPoint
pos( event
.GetPosition() );
3134 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3136 wxGridCellCoords coords
;
3137 XYToCell( x
, y
, coords
);
3139 if ( event
.Dragging() )
3141 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3143 // Don't start doing anything until the mouse has been drug at
3144 // least 3 pixels in any direction...
3147 if (m_startDragPos
== wxDefaultPosition
)
3149 m_startDragPos
= pos
;
3152 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3156 m_isDragging
= TRUE
;
3157 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3159 // Hide the edit control, so it
3160 // won't interfer with drag-shrinking.
3161 if ( IsCellEditControlEnabled() )
3162 HideCellEditControl();
3164 // Have we captured the mouse yet?
3167 m_winCapture
= m_gridWin
;
3168 m_winCapture
->CaptureMouse();
3171 if ( coords
!= wxGridNoCellCoords
)
3173 if ( !IsSelection() )
3175 SelectBlock( coords
, coords
);
3179 SelectBlock( m_currentCellCoords
, coords
);
3182 if (! IsVisible(coords
))
3184 MakeCellVisible(coords
);
3185 // TODO: need to introduce a delay or something here. The
3186 // scrolling is way to fast, at least on MSW.
3190 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3192 int cw
, ch
, left
, dummy
;
3193 m_gridWin
->GetClientSize( &cw
, &ch
);
3194 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3196 wxClientDC
dc( m_gridWin
);
3198 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3199 dc
.SetLogicalFunction(wxINVERT
);
3200 if ( m_dragLastPos
>= 0 )
3202 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3204 dc
.DrawLine( left
, y
, left
+cw
, y
);
3207 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3209 int cw
, ch
, dummy
, top
;
3210 m_gridWin
->GetClientSize( &cw
, &ch
);
3211 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3213 wxClientDC
dc( m_gridWin
);
3215 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + WXGRID_MIN_COL_WIDTH
);
3216 dc
.SetLogicalFunction(wxINVERT
);
3217 if ( m_dragLastPos
>= 0 )
3219 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3221 dc
.DrawLine( x
, top
, x
, top
+ch
);
3228 m_isDragging
= FALSE
;
3229 m_startDragPos
= wxDefaultPosition
;
3232 if ( coords
!= wxGridNoCellCoords
)
3234 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3235 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3238 if ( event
.Entering() || event
.Leaving() )
3240 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3241 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3246 // ------------ Left button pressed
3248 if ( event
.LeftDown() )
3250 DisableCellEditControl();
3251 if ( event
.ShiftDown() )
3253 SelectBlock( m_currentCellCoords
, coords
);
3255 else if ( XToEdgeOfCol(x
) < 0 &&
3256 YToEdgeOfRow(y
) < 0 )
3258 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3263 MakeCellVisible( coords
);
3265 // if this is the second click on this cell then start
3267 if ( m_waitForSlowClick
&&
3268 (coords
== m_currentCellCoords
) &&
3269 CanEnableCellControl())
3271 EnableCellEditControl();
3273 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3274 attr
->GetEditor()->StartingClick();
3277 m_waitForSlowClick
= FALSE
;
3281 SetCurrentCell( coords
);
3282 m_waitForSlowClick
= TRUE
;
3289 // ------------ Left double click
3291 else if ( event
.LeftDClick() )
3293 DisableCellEditControl();
3294 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3296 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3304 // ------------ Left button released
3306 else if ( event
.LeftUp() )
3308 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3310 if ( IsSelection() )
3314 m_winCapture
->ReleaseMouse();
3315 m_winCapture
= NULL
;
3317 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3320 // Show the edit control, if it has been hidden for
3322 ShowCellEditControl();
3324 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3326 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3327 DoEndDragResizeRow();
3329 // Note: we are ending the event *after* doing
3330 // default processing in this case
3332 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3334 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3336 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3337 DoEndDragResizeCol();
3339 // Note: we are ending the event *after* doing
3340 // default processing in this case
3342 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3349 // ------------ Right button down
3351 else if ( event
.RightDown() )
3353 DisableCellEditControl();
3354 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3359 // no default action at the moment
3364 // ------------ Right double click
3366 else if ( event
.RightDClick() )
3368 DisableCellEditControl();
3369 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3374 // no default action at the moment
3378 // ------------ Moving and no button action
3380 else if ( event
.Moving() && !event
.IsButton() )
3382 int dragRow
= YToEdgeOfRow( y
);
3383 int dragCol
= XToEdgeOfCol( x
);
3385 // Dragging on the corner of a cell to resize in both
3386 // directions is not implemented yet...
3388 if ( dragRow
>= 0 && dragCol
>= 0 )
3390 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3396 m_dragRowOrCol
= dragRow
;
3398 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3400 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3408 m_dragRowOrCol
= dragCol
;
3410 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3412 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3418 // Neither on a row or col edge
3420 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3422 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3429 void wxGrid::DoEndDragResizeRow()
3431 if ( m_dragLastPos
>= 0 )
3433 // erase the last line and resize the row
3435 int cw
, ch
, left
, dummy
;
3436 m_gridWin
->GetClientSize( &cw
, &ch
);
3437 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3439 wxClientDC
dc( m_gridWin
);
3441 dc
.SetLogicalFunction( wxINVERT
);
3442 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3443 HideCellEditControl();
3445 int rowTop
= GetRowTop(m_dragRowOrCol
);
3446 SetRowSize( m_dragRowOrCol
,
3447 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3449 if ( !GetBatchCount() )
3451 // Only needed to get the correct rect.y:
3452 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3454 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3455 rect
.width
= m_rowLabelWidth
;
3456 rect
.height
= ch
- rect
.y
;
3457 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3459 m_gridWin
->Refresh( FALSE
, &rect
);
3462 ShowCellEditControl();
3467 void wxGrid::DoEndDragResizeCol()
3469 if ( m_dragLastPos
>= 0 )
3471 // erase the last line and resize the col
3473 int cw
, ch
, dummy
, top
;
3474 m_gridWin
->GetClientSize( &cw
, &ch
);
3475 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3477 wxClientDC
dc( m_gridWin
);
3479 dc
.SetLogicalFunction( wxINVERT
);
3480 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3481 HideCellEditControl();
3483 int colLeft
= GetColLeft(m_dragRowOrCol
);
3484 SetColSize( m_dragRowOrCol
,
3485 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3487 if ( !GetBatchCount() )
3489 // Only needed to get the correct rect.x:
3490 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3492 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3493 rect
.width
= cw
- rect
.x
;
3494 rect
.height
= m_colLabelHeight
;
3495 m_colLabelWin
->Refresh( TRUE
, &rect
);
3497 m_gridWin
->Refresh( FALSE
, &rect
);
3500 ShowCellEditControl();
3507 // ------ interaction with data model
3509 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3511 switch ( msg
.GetId() )
3513 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3514 return GetModelValues();
3516 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3517 return SetModelValues();
3519 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3520 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3521 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3522 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3523 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3524 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3525 return Redimension( msg
);
3534 // The behaviour of this function depends on the grid table class
3535 // Clear() function. For the default wxGridStringTable class the
3536 // behavious is to replace all cell contents with wxEmptyString but
3537 // not to change the number of rows or cols.
3539 void wxGrid::ClearGrid()
3544 SetEditControlValue();
3545 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3550 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3552 // TODO: something with updateLabels flag
3556 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3562 if (IsCellEditControlEnabled())
3563 DisableCellEditControl();
3565 bool ok
= m_table
->InsertRows( pos
, numRows
);
3567 // the table will have sent the results of the insert row
3568 // operation to this view object as a grid table message
3572 if ( m_numCols
== 0 )
3574 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3576 // TODO: perhaps instead of appending the default number of cols
3577 // we should remember what the last non-zero number of cols was ?
3581 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3583 // if we have just inserted cols into an empty grid the current
3584 // cell will be undefined...
3586 SetCurrentCell( 0, 0 );
3590 if ( !GetBatchCount() ) Refresh();
3593 SetEditControlValue();
3603 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3605 // TODO: something with updateLabels flag
3609 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3613 if ( m_table
&& m_table
->AppendRows( numRows
) )
3615 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3617 // if we have just inserted cols into an empty grid the current
3618 // cell will be undefined...
3620 SetCurrentCell( 0, 0 );
3623 // the table will have sent the results of the append row
3624 // operation to this view object as a grid table message
3627 if ( !GetBatchCount() ) Refresh();
3637 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3639 // TODO: something with updateLabels flag
3643 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3649 if (IsCellEditControlEnabled())
3650 DisableCellEditControl();
3652 if (m_table
->DeleteRows( pos
, numRows
))
3655 // the table will have sent the results of the delete row
3656 // operation to this view object as a grid table message
3659 if ( !GetBatchCount() ) Refresh();
3667 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3669 // TODO: something with updateLabels flag
3673 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3679 if (IsCellEditControlEnabled())
3680 DisableCellEditControl();
3682 bool ok
= m_table
->InsertCols( pos
, numCols
);
3684 // the table will have sent the results of the insert col
3685 // operation to this view object as a grid table message
3689 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3691 // if we have just inserted cols into an empty grid the current
3692 // cell will be undefined...
3694 SetCurrentCell( 0, 0 );
3698 if ( !GetBatchCount() ) Refresh();
3701 SetEditControlValue();
3711 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3713 // TODO: something with updateLabels flag
3717 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3721 if ( m_table
&& m_table
->AppendCols( numCols
) )
3723 // the table will have sent the results of the append col
3724 // operation to this view object as a grid table message
3726 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3728 // if we have just inserted cols into an empty grid the current
3729 // cell will be undefined...
3731 SetCurrentCell( 0, 0 );
3735 if ( !GetBatchCount() ) Refresh();
3745 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3747 // TODO: something with updateLabels flag
3751 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3757 if (IsCellEditControlEnabled())
3758 DisableCellEditControl();
3760 if ( m_table
->DeleteCols( pos
, numCols
) )
3762 // the table will have sent the results of the delete col
3763 // operation to this view object as a grid table message
3766 if ( !GetBatchCount() ) Refresh();
3776 // ----- event handlers
3779 // Generate a grid event based on a mouse event and
3780 // return the result of ProcessEvent()
3782 bool wxGrid::SendEvent( const wxEventType type
,
3784 wxMouseEvent
& mouseEv
)
3786 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3788 int rowOrCol
= (row
== -1 ? col
: row
);
3790 wxGridSizeEvent
gridEvt( GetId(),
3794 mouseEv
.GetX(), mouseEv
.GetY(),
3795 mouseEv
.ControlDown(),
3796 mouseEv
.ShiftDown(),
3798 mouseEv
.MetaDown() );
3800 return GetEventHandler()->ProcessEvent(gridEvt
);
3802 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3804 wxGridRangeSelectEvent
gridEvt( GetId(),
3808 m_selectedBottomRight
,
3809 mouseEv
.ControlDown(),
3810 mouseEv
.ShiftDown(),
3812 mouseEv
.MetaDown() );
3814 return GetEventHandler()->ProcessEvent(gridEvt
);
3818 wxGridEvent
gridEvt( GetId(),
3822 mouseEv
.GetX(), mouseEv
.GetY(),
3823 mouseEv
.ControlDown(),
3824 mouseEv
.ShiftDown(),
3826 mouseEv
.MetaDown() );
3828 return GetEventHandler()->ProcessEvent(gridEvt
);
3833 // Generate a grid event of specified type and return the result
3834 // of ProcessEvent().
3836 bool wxGrid::SendEvent( const wxEventType type
,
3839 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3841 int rowOrCol
= (row
== -1 ? col
: row
);
3843 wxGridSizeEvent
gridEvt( GetId(),
3848 return GetEventHandler()->ProcessEvent(gridEvt
);
3852 wxGridEvent
gridEvt( GetId(),
3857 return GetEventHandler()->ProcessEvent(gridEvt
);
3862 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3864 wxPaintDC
dc( this );
3866 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3867 m_numRows
&& m_numCols
)
3869 m_currentCellCoords
.Set(0, 0);
3870 SetEditControlValue();
3871 ShowCellEditControl();
3878 // This is just here to make sure that CalcDimensions gets called when
3879 // the grid view is resized... then the size event is skipped to allow
3880 // the box sizers to handle everything
3882 void wxGrid::OnSize( wxSizeEvent
& event
)
3889 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3891 if ( m_inOnKeyDown
)
3893 // shouldn't be here - we are going round in circles...
3895 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3898 m_inOnKeyDown
= TRUE
;
3900 // propagate the event up and see if it gets processed
3902 wxWindow
*parent
= GetParent();
3903 wxKeyEvent
keyEvt( event
);
3904 keyEvt
.SetEventObject( parent
);
3906 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3909 // TODO: Should also support Shift-cursor keys for
3910 // extending the selection. Maybe add a flag to
3911 // MoveCursorXXX() and MoveCursorXXXBlock() and
3912 // just send event.ShiftDown().
3914 // try local handlers
3916 switch ( event
.KeyCode() )
3919 if ( event
.ControlDown() )
3921 MoveCursorUpBlock();
3930 if ( event
.ControlDown() )
3932 MoveCursorDownBlock();
3941 if ( event
.ControlDown() )
3943 MoveCursorLeftBlock();
3952 if ( event
.ControlDown() )
3954 MoveCursorRightBlock();
3963 if ( event
.ControlDown() )
3965 event
.Skip(); // to let the edit control have the return
3974 if (event
.ShiftDown())
3981 if ( event
.ControlDown() )
3983 MakeCellVisible( 0, 0 );
3984 SetCurrentCell( 0, 0 );
3993 if ( event
.ControlDown() )
3995 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3996 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4012 // We don't want these keys to trigger the edit control, any others?
4021 if ( !IsEditable() )
4026 // Otherwise fall through to default
4029 // now try the cell edit control
4031 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4033 EnableCellEditControl();
4034 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4035 attr
->GetEditor()->StartingKey(event
);
4040 // let others process char events for readonly cells
4047 m_inOnKeyDown
= FALSE
;
4051 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4055 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4057 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4059 // the event has been intercepted - do nothing
4064 m_currentCellCoords
!= wxGridNoCellCoords
)
4066 HideCellEditControl();
4067 SaveEditControlValue();
4068 DisableCellEditControl();
4070 // Clear the old current cell highlight
4071 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4073 // Otherwise refresh redraws the highlight!
4074 m_currentCellCoords
= coords
;
4076 m_gridWin
->Refresh( FALSE
, &r
);
4079 m_currentCellCoords
= coords
;
4081 SetEditControlValue();
4085 wxClientDC
dc(m_gridWin
);
4088 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4089 DrawCellHighlight(dc
, attr
);
4092 if ( IsSelection() )
4094 wxRect
r( SelectionToDeviceRect() );
4096 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4103 // ------ functions to get/send data (see also public functions)
4106 bool wxGrid::GetModelValues()
4110 // all we need to do is repaint the grid
4112 m_gridWin
->Refresh();
4120 bool wxGrid::SetModelValues()
4126 for ( row
= 0; row
< m_numRows
; row
++ )
4128 for ( col
= 0; col
< m_numCols
; col
++ )
4130 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4142 // Note - this function only draws cells that are in the list of
4143 // exposed cells (usually set from the update region by
4144 // CalcExposedCells)
4146 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4148 if ( !m_numRows
|| !m_numCols
) return;
4151 size_t numCells
= m_cellsExposed
.GetCount();
4153 for ( i
= 0; i
< numCells
; i
++ )
4155 DrawCell( dc
, m_cellsExposed
[i
] );
4160 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4162 int row
= coords
.GetRow();
4163 int col
= coords
.GetCol();
4165 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4168 // we draw the cell border ourselves
4169 #if !WXGRID_DRAW_LINES
4170 if ( m_gridLinesEnabled
)
4171 DrawCellBorder( dc
, coords
);
4174 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4176 bool isCurrent
= coords
== m_currentCellCoords
;
4179 rect
.x
= GetColLeft(col
);
4180 rect
.y
= GetRowTop(row
);
4181 rect
.width
= GetColWidth(col
) - 1;
4182 rect
.height
= GetRowHeight(row
) - 1;
4184 // if the editor is shown, we should use it and not the renderer
4185 if ( isCurrent
&& IsCellEditControlEnabled() )
4187 attr
->GetEditor()->PaintBackground(rect
, attr
);
4191 // but all the rest is drawn by the cell renderer and hence may be
4193 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4199 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4201 int row
= m_currentCellCoords
.GetRow();
4202 int col
= m_currentCellCoords
.GetCol();
4204 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4208 rect
.x
= GetColLeft(col
);
4209 rect
.y
= GetRowTop(row
);
4210 rect
.width
= GetColWidth(col
) - 1;
4211 rect
.height
= GetRowHeight(row
) - 1;
4213 // hmmm... what could we do here to show that the cell is disabled?
4214 // for now, I just draw a thinner border than for the other ones, but
4215 // it doesn't look really good
4216 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4217 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4219 dc
.DrawRectangle(rect
);
4222 // VZ: my experiments with 3d borders...
4224 // how to properly set colours for arbitrary bg?
4225 wxCoord x1
= rect
.x
,
4227 x2
= rect
.x
+ rect
.width
-1,
4228 y2
= rect
.y
+ rect
.height
-1;
4230 dc
.SetPen(*wxWHITE_PEN
);
4231 dc
.DrawLine(x1
, y1
, x2
, y1
);
4232 dc
.DrawLine(x1
, y1
, x1
, y2
);
4234 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4235 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4237 dc
.SetPen(*wxBLACK_PEN
);
4238 dc
.DrawLine(x1
, y2
, x2
, y2
);
4239 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4243 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4245 int row
= coords
.GetRow();
4246 int col
= coords
.GetCol();
4247 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4250 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4252 // right hand border
4254 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
4255 GetColRight(col
), GetRowBottom(row
) );
4259 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
4260 GetColRight(col
), GetRowBottom(row
) );
4263 void wxGrid::DrawHighlight(wxDC
& dc
)
4265 // if the active cell was repainted, repaint its highlight too because it
4266 // might have been damaged by the grid lines
4267 size_t count
= m_cellsExposed
.GetCount();
4268 for ( size_t n
= 0; n
< count
; n
++ )
4270 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4272 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4273 DrawCellHighlight(dc
, attr
);
4281 // TODO: remove this ???
4282 // This is used to redraw all grid lines e.g. when the grid line colour
4285 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4287 if ( !m_gridLinesEnabled
||
4289 !m_numCols
) return;
4291 int top
, bottom
, left
, right
;
4296 m_gridWin
->GetClientSize(&cw
, &ch
);
4298 // virtual coords of visible area
4300 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4301 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4306 reg
.GetBox(x
, y
, w
, h
);
4307 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4308 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4311 // avoid drawing grid lines past the last row and col
4313 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
4314 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
4316 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4318 // horizontal grid lines
4321 for ( i
= 0; i
< m_numRows
; i
++ )
4323 int bot
= GetRowBottom(i
) - 1;
4332 dc
.DrawLine( left
, bot
, right
, bot
);
4337 // vertical grid lines
4339 for ( i
= 0; i
< m_numCols
; i
++ )
4341 int colRight
= GetColRight(i
) - 1;
4342 if ( colRight
> right
)
4347 if ( colRight
>= left
)
4349 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
4355 void wxGrid::DrawRowLabels( wxDC
& dc
)
4357 if ( !m_numRows
|| !m_numCols
) return;
4360 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4362 for ( i
= 0; i
< numLabels
; i
++ )
4364 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4369 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4371 if ( GetRowHeight(row
) <= 0 )
4374 int rowTop
= GetRowTop(row
),
4375 rowBottom
= GetRowBottom(row
) - 1;
4377 dc
.SetPen( *wxBLACK_PEN
);
4378 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4379 m_rowLabelWidth
-1, rowBottom
);
4381 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
4383 dc
.SetPen( *wxWHITE_PEN
);
4384 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
4385 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4387 dc
.SetBackgroundMode( wxTRANSPARENT
);
4388 dc
.SetTextForeground( GetLabelTextColour() );
4389 dc
.SetFont( GetLabelFont() );
4392 GetRowLabelAlignment( &hAlign
, &vAlign
);
4396 rect
.SetY( GetRowTop(row
) + 2 );
4397 rect
.SetWidth( m_rowLabelWidth
- 4 );
4398 rect
.SetHeight( GetRowHeight(row
) - 4 );
4399 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4403 void wxGrid::DrawColLabels( wxDC
& dc
)
4405 if ( !m_numRows
|| !m_numCols
) return;
4408 size_t numLabels
= m_colLabelsExposed
.GetCount();
4410 for ( i
= 0; i
< numLabels
; i
++ )
4412 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4417 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4419 if ( GetColWidth(col
) <= 0 )
4422 int colLeft
= GetColLeft(col
),
4423 colRight
= GetColRight(col
) - 1;
4425 dc
.SetPen( *wxBLACK_PEN
);
4426 dc
.DrawLine( colRight
, 0,
4427 colRight
, m_colLabelHeight
-1 );
4429 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4430 colRight
, m_colLabelHeight
-1 );
4432 dc
.SetPen( *wxWHITE_PEN
);
4433 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4434 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
4436 dc
.SetBackgroundMode( wxTRANSPARENT
);
4437 dc
.SetTextForeground( GetLabelTextColour() );
4438 dc
.SetFont( GetLabelFont() );
4440 dc
.SetBackgroundMode( wxTRANSPARENT
);
4441 dc
.SetTextForeground( GetLabelTextColour() );
4442 dc
.SetFont( GetLabelFont() );
4445 GetColLabelAlignment( &hAlign
, &vAlign
);
4448 rect
.SetX( colLeft
+ 2 );
4450 rect
.SetWidth( GetColWidth(col
) - 4 );
4451 rect
.SetHeight( m_colLabelHeight
- 4 );
4452 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4456 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4457 const wxString
& value
,
4462 long textWidth
, textHeight
;
4463 long lineWidth
, lineHeight
;
4464 wxArrayString lines
;
4466 dc
.SetClippingRegion( rect
);
4467 StringToLines( value
, lines
);
4468 if ( lines
.GetCount() )
4470 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4471 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4474 switch ( horizAlign
)
4477 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4481 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4490 switch ( vertAlign
)
4493 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4497 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4506 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4508 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4513 dc
.DestroyClippingRegion();
4517 // Split multi line text up into an array of strings. Any existing
4518 // contents of the string array are preserved.
4520 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4524 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4525 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4527 while ( startPos
< (int)tVal
.Length() )
4529 pos
= tVal
.Mid(startPos
).Find( eol
);
4534 else if ( pos
== 0 )
4536 lines
.Add( wxEmptyString
);
4540 lines
.Add( value
.Mid(startPos
, pos
) );
4544 if ( startPos
< (int)value
.Length() )
4546 lines
.Add( value
.Mid( startPos
) );
4551 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4552 wxArrayString
& lines
,
4553 long *width
, long *height
)
4560 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4562 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4563 w
= wxMax( w
, lineW
);
4573 // ------ Edit control functions
4577 void wxGrid::EnableEditing( bool edit
)
4579 // TODO: improve this ?
4581 if ( edit
!= m_editable
)
4585 // FIXME IMHO this won't disable the edit control if edit == FALSE
4586 // because of the check in the beginning of
4587 // EnableCellEditControl() just below (VZ)
4588 EnableCellEditControl(m_editable
);
4593 void wxGrid::EnableCellEditControl( bool enable
)
4598 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4599 SetCurrentCell( 0, 0 );
4601 if ( enable
!= m_cellEditCtrlEnabled
)
4603 // TODO allow the app to Veto() this event?
4604 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4608 // this should be checked by the caller!
4609 wxASSERT_MSG( CanEnableCellControl(),
4610 _T("can't enable editing for this cell!") );
4612 // do it before ShowCellEditControl()
4613 m_cellEditCtrlEnabled
= enable
;
4615 SetEditControlValue();
4616 ShowCellEditControl();
4620 HideCellEditControl();
4621 SaveEditControlValue();
4623 // do it after HideCellEditControl()
4624 m_cellEditCtrlEnabled
= enable
;
4629 bool wxGrid::IsCurrentCellReadOnly() const
4632 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4633 bool readonly
= attr
->IsReadOnly();
4639 bool wxGrid::CanEnableCellControl() const
4641 return m_editable
&& !IsCurrentCellReadOnly();
4644 bool wxGrid::IsCellEditControlEnabled() const
4646 // the cell edit control might be disable for all cells or just for the
4647 // current one if it's read only
4648 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
4651 wxWindow
*wxGrid::GetGridWindow() const
4656 void wxGrid::ShowCellEditControl()
4658 if ( IsCellEditControlEnabled() )
4660 if ( !IsVisible( m_currentCellCoords
) )
4666 wxRect rect
= CellToRect( m_currentCellCoords
);
4667 int row
= m_currentCellCoords
.GetRow();
4668 int col
= m_currentCellCoords
.GetCol();
4670 // convert to scrolled coords
4672 int left
, top
, right
, bottom
;
4673 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4674 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4676 // cell is shifted by one pixel
4682 // Make the edit control large enough to allow for internal
4685 // TODO: remove this if the text ctrl sizing is improved esp. for
4689 #if defined(__WXMOTIF__)
4690 if ( row
== 0 || col
== 0 )
4699 if ( row
== 0 || col
== 0 )
4709 #if defined(__WXGTK__)
4712 if (left
!= 0) left_diff
++;
4713 if (top
!= 0) top_diff
++;
4714 rect
.SetLeft( left
+ left_diff
);
4715 rect
.SetTop( top
+ top_diff
);
4716 rect
.SetRight( rect
.GetRight() - left_diff
);
4717 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4719 rect
.SetLeft( wxMax(0, left
- extra
) );
4720 rect
.SetTop( wxMax(0, top
- extra
) );
4721 rect
.SetRight( rect
.GetRight() + 2*extra
);
4722 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4725 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4726 wxGridCellEditor
* editor
= attr
->GetEditor();
4727 if ( !editor
->IsCreated() )
4729 editor
->Create(m_gridWin
, -1,
4730 new wxGridCellEditorEvtHandler(this, editor
));
4733 editor
->SetSize( rect
);
4734 editor
->Show( TRUE
, attr
);
4735 editor
->BeginEdit(row
, col
, this);
4742 void wxGrid::HideCellEditControl()
4744 if ( IsCellEditControlEnabled() )
4746 int row
= m_currentCellCoords
.GetRow();
4747 int col
= m_currentCellCoords
.GetCol();
4749 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4750 attr
->GetEditor()->Show( FALSE
);
4752 m_gridWin
->SetFocus();
4757 void wxGrid::SetEditControlValue( const wxString
& value
)
4759 // RD: The new Editors get the value from the table themselves now. This
4760 // method can probably be removed...
4764 void wxGrid::SaveEditControlValue()
4766 if ( IsCellEditControlEnabled() )
4768 int row
= m_currentCellCoords
.GetRow();
4769 int col
= m_currentCellCoords
.GetCol();
4771 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4772 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4778 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4779 m_currentCellCoords
.GetRow(),
4780 m_currentCellCoords
.GetCol() );
4787 // ------ Grid location functions
4788 // Note that all of these functions work with the logical coordinates of
4789 // grid cells and labels so you will need to convert from device
4790 // coordinates for mouse events etc.
4793 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4795 int row
= YToRow(y
);
4796 int col
= XToCol(x
);
4798 if ( row
== -1 || col
== -1 )
4800 coords
= wxGridNoCellCoords
;
4804 coords
.Set( row
, col
);
4809 int wxGrid::YToRow( int y
)
4813 for ( i
= 0; i
< m_numRows
; i
++ )
4815 if ( y
< GetRowBottom(i
) )
4819 return m_numRows
; //-1;
4823 int wxGrid::XToCol( int x
)
4827 for ( i
= 0; i
< m_numCols
; i
++ )
4829 if ( x
< GetColRight(i
) )
4833 return m_numCols
; //-1;
4837 // return the row number that that the y coord is near the edge of, or
4838 // -1 if not near an edge
4840 int wxGrid::YToEdgeOfRow( int y
)
4844 for ( i
= 0; i
< m_numRows
; i
++ )
4846 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
4848 d
= abs( y
- GetRowBottom(i
) );
4849 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
4858 // return the col number that that the x coord is near the edge of, or
4859 // -1 if not near an edge
4861 int wxGrid::XToEdgeOfCol( int x
)
4865 for ( i
= 0; i
< m_numCols
; i
++ )
4867 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
4869 d
= abs( x
- GetColRight(i
) );
4870 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
4879 wxRect
wxGrid::CellToRect( int row
, int col
)
4881 wxRect
rect( -1, -1, -1, -1 );
4883 if ( row
>= 0 && row
< m_numRows
&&
4884 col
>= 0 && col
< m_numCols
)
4886 rect
.x
= GetColLeft(col
);
4887 rect
.y
= GetRowTop(row
);
4888 rect
.width
= GetColWidth(col
);
4889 rect
.height
= GetRowHeight(row
);
4896 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4898 // get the cell rectangle in logical coords
4900 wxRect
r( CellToRect( row
, col
) );
4902 // convert to device coords
4904 int left
, top
, right
, bottom
;
4905 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4906 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4908 // check against the client area of the grid window
4911 m_gridWin
->GetClientSize( &cw
, &ch
);
4913 if ( wholeCellVisible
)
4915 // is the cell wholly visible ?
4917 return ( left
>= 0 && right
<= cw
&&
4918 top
>= 0 && bottom
<= ch
);
4922 // is the cell partly visible ?
4924 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4925 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4930 // make the specified cell location visible by doing a minimal amount
4933 void wxGrid::MakeCellVisible( int row
, int col
)
4936 int xpos
= -1, ypos
= -1;
4938 if ( row
>= 0 && row
< m_numRows
&&
4939 col
>= 0 && col
< m_numCols
)
4941 // get the cell rectangle in logical coords
4943 wxRect
r( CellToRect( row
, col
) );
4945 // convert to device coords
4947 int left
, top
, right
, bottom
;
4948 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4949 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4952 m_gridWin
->GetClientSize( &cw
, &ch
);
4958 else if ( bottom
> ch
)
4960 int h
= r
.GetHeight();
4962 for ( i
= row
-1; i
>= 0; i
-- )
4964 int rowHeight
= GetRowHeight(i
);
4965 if ( h
+ rowHeight
> ch
)
4972 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4973 // have rounding errors (this is important, because if we do, we
4974 // might not scroll at all and some cells won't be redrawn)
4975 ypos
+= GRID_SCROLL_LINE
/ 2;
4982 else if ( right
> cw
)
4984 int w
= r
.GetWidth();
4986 for ( i
= col
-1; i
>= 0; i
-- )
4988 int colWidth
= GetColWidth(i
);
4989 if ( w
+ colWidth
> cw
)
4996 // see comment for ypos above
4997 xpos
+= GRID_SCROLL_LINE
/ 2;
5000 if ( xpos
!= -1 || ypos
!= -1 )
5002 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5003 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5004 Scroll( xpos
, ypos
);
5012 // ------ Grid cursor movement functions
5015 bool wxGrid::MoveCursorUp()
5017 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5018 m_currentCellCoords
.GetRow() > 0 )
5020 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5021 m_currentCellCoords
.GetCol() );
5023 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5024 m_currentCellCoords
.GetCol() );
5033 bool wxGrid::MoveCursorDown()
5035 // TODO: allow for scrolling
5037 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5038 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5040 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5041 m_currentCellCoords
.GetCol() );
5043 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5044 m_currentCellCoords
.GetCol() );
5053 bool wxGrid::MoveCursorLeft()
5055 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5056 m_currentCellCoords
.GetCol() > 0 )
5058 MakeCellVisible( m_currentCellCoords
.GetRow(),
5059 m_currentCellCoords
.GetCol() - 1 );
5061 SetCurrentCell( m_currentCellCoords
.GetRow(),
5062 m_currentCellCoords
.GetCol() - 1 );
5071 bool wxGrid::MoveCursorRight()
5073 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5074 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5076 MakeCellVisible( m_currentCellCoords
.GetRow(),
5077 m_currentCellCoords
.GetCol() + 1 );
5079 SetCurrentCell( m_currentCellCoords
.GetRow(),
5080 m_currentCellCoords
.GetCol() + 1 );
5089 bool wxGrid::MovePageUp()
5091 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5093 int row
= m_currentCellCoords
.GetRow();
5097 m_gridWin
->GetClientSize( &cw
, &ch
);
5099 int y
= GetRowTop(row
);
5100 int newRow
= YToRow( y
- ch
+ 1 );
5105 else if ( newRow
== row
)
5110 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5111 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5119 bool wxGrid::MovePageDown()
5121 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5123 int row
= m_currentCellCoords
.GetRow();
5124 if ( row
< m_numRows
)
5127 m_gridWin
->GetClientSize( &cw
, &ch
);
5129 int y
= GetRowTop(row
);
5130 int newRow
= YToRow( y
+ ch
);
5133 newRow
= m_numRows
- 1;
5135 else if ( newRow
== row
)
5140 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5141 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5149 bool wxGrid::MoveCursorUpBlock()
5152 m_currentCellCoords
!= wxGridNoCellCoords
&&
5153 m_currentCellCoords
.GetRow() > 0 )
5155 int row
= m_currentCellCoords
.GetRow();
5156 int col
= m_currentCellCoords
.GetCol();
5158 if ( m_table
->IsEmptyCell(row
, col
) )
5160 // starting in an empty cell: find the next block of
5166 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5169 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5171 // starting at the top of a block: find the next block
5177 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5182 // starting within a block: find the top of the block
5187 if ( m_table
->IsEmptyCell(row
, col
) )
5195 MakeCellVisible( row
, col
);
5196 SetCurrentCell( row
, col
);
5204 bool wxGrid::MoveCursorDownBlock()
5207 m_currentCellCoords
!= wxGridNoCellCoords
&&
5208 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5210 int row
= m_currentCellCoords
.GetRow();
5211 int col
= m_currentCellCoords
.GetCol();
5213 if ( m_table
->IsEmptyCell(row
, col
) )
5215 // starting in an empty cell: find the next block of
5218 while ( row
< m_numRows
-1 )
5221 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5224 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5226 // starting at the bottom of a block: find the next block
5229 while ( row
< m_numRows
-1 )
5232 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5237 // starting within a block: find the bottom of the block
5239 while ( row
< m_numRows
-1 )
5242 if ( m_table
->IsEmptyCell(row
, col
) )
5250 MakeCellVisible( row
, col
);
5251 SetCurrentCell( row
, col
);
5259 bool wxGrid::MoveCursorLeftBlock()
5262 m_currentCellCoords
!= wxGridNoCellCoords
&&
5263 m_currentCellCoords
.GetCol() > 0 )
5265 int row
= m_currentCellCoords
.GetRow();
5266 int col
= m_currentCellCoords
.GetCol();
5268 if ( m_table
->IsEmptyCell(row
, col
) )
5270 // starting in an empty cell: find the next block of
5276 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5279 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5281 // starting at the left of a block: find the next block
5287 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5292 // starting within a block: find the left of the block
5297 if ( m_table
->IsEmptyCell(row
, col
) )
5305 MakeCellVisible( row
, col
);
5306 SetCurrentCell( row
, col
);
5314 bool wxGrid::MoveCursorRightBlock()
5317 m_currentCellCoords
!= wxGridNoCellCoords
&&
5318 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5320 int row
= m_currentCellCoords
.GetRow();
5321 int col
= m_currentCellCoords
.GetCol();
5323 if ( m_table
->IsEmptyCell(row
, col
) )
5325 // starting in an empty cell: find the next block of
5328 while ( col
< m_numCols
-1 )
5331 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5334 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5336 // starting at the right of a block: find the next block
5339 while ( col
< m_numCols
-1 )
5342 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5347 // starting within a block: find the right of the block
5349 while ( col
< m_numCols
-1 )
5352 if ( m_table
->IsEmptyCell(row
, col
) )
5360 MakeCellVisible( row
, col
);
5361 SetCurrentCell( row
, col
);
5372 // ------ Label values and formatting
5375 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5377 *horiz
= m_rowLabelHorizAlign
;
5378 *vert
= m_rowLabelVertAlign
;
5381 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5383 *horiz
= m_colLabelHorizAlign
;
5384 *vert
= m_colLabelVertAlign
;
5387 wxString
wxGrid::GetRowLabelValue( int row
)
5391 return m_table
->GetRowLabelValue( row
);
5401 wxString
wxGrid::GetColLabelValue( int col
)
5405 return m_table
->GetColLabelValue( col
);
5416 void wxGrid::SetRowLabelSize( int width
)
5418 width
= wxMax( width
, 0 );
5419 if ( width
!= m_rowLabelWidth
)
5423 m_rowLabelWin
->Show( FALSE
);
5424 m_cornerLabelWin
->Show( FALSE
);
5426 else if ( m_rowLabelWidth
== 0 )
5428 m_rowLabelWin
->Show( TRUE
);
5429 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5432 m_rowLabelWidth
= width
;
5439 void wxGrid::SetColLabelSize( int height
)
5441 height
= wxMax( height
, 0 );
5442 if ( height
!= m_colLabelHeight
)
5446 m_colLabelWin
->Show( FALSE
);
5447 m_cornerLabelWin
->Show( FALSE
);
5449 else if ( m_colLabelHeight
== 0 )
5451 m_colLabelWin
->Show( TRUE
);
5452 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5455 m_colLabelHeight
= height
;
5462 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5464 if ( m_labelBackgroundColour
!= colour
)
5466 m_labelBackgroundColour
= colour
;
5467 m_rowLabelWin
->SetBackgroundColour( colour
);
5468 m_colLabelWin
->SetBackgroundColour( colour
);
5469 m_cornerLabelWin
->SetBackgroundColour( colour
);
5471 if ( !GetBatchCount() )
5473 m_rowLabelWin
->Refresh();
5474 m_colLabelWin
->Refresh();
5475 m_cornerLabelWin
->Refresh();
5480 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5482 if ( m_labelTextColour
!= colour
)
5484 m_labelTextColour
= colour
;
5485 if ( !GetBatchCount() )
5487 m_rowLabelWin
->Refresh();
5488 m_colLabelWin
->Refresh();
5493 void wxGrid::SetLabelFont( const wxFont
& font
)
5496 if ( !GetBatchCount() )
5498 m_rowLabelWin
->Refresh();
5499 m_colLabelWin
->Refresh();
5503 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5505 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5507 m_rowLabelHorizAlign
= horiz
;
5510 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5512 m_rowLabelVertAlign
= vert
;
5515 if ( !GetBatchCount() )
5517 m_rowLabelWin
->Refresh();
5521 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5523 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5525 m_colLabelHorizAlign
= horiz
;
5528 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5530 m_colLabelVertAlign
= vert
;
5533 if ( !GetBatchCount() )
5535 m_colLabelWin
->Refresh();
5539 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5543 m_table
->SetRowLabelValue( row
, s
);
5544 if ( !GetBatchCount() )
5546 wxRect rect
= CellToRect( row
, 0);
5547 if ( rect
.height
> 0 )
5549 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5551 rect
.width
= m_rowLabelWidth
;
5552 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5558 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5562 m_table
->SetColLabelValue( col
, s
);
5563 if ( !GetBatchCount() )
5565 wxRect rect
= CellToRect( 0, col
);
5566 if ( rect
.width
> 0 )
5568 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5570 rect
.height
= m_colLabelHeight
;
5571 m_colLabelWin
->Refresh( TRUE
, &rect
);
5577 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5579 if ( m_gridLineColour
!= colour
)
5581 m_gridLineColour
= colour
;
5583 wxClientDC
dc( m_gridWin
);
5585 DrawAllGridLines( dc
, wxRegion() );
5589 void wxGrid::EnableGridLines( bool enable
)
5591 if ( enable
!= m_gridLinesEnabled
)
5593 m_gridLinesEnabled
= enable
;
5595 if ( !GetBatchCount() )
5599 wxClientDC
dc( m_gridWin
);
5601 DrawAllGridLines( dc
, wxRegion() );
5605 m_gridWin
->Refresh();
5612 int wxGrid::GetDefaultRowSize()
5614 return m_defaultRowHeight
;
5617 int wxGrid::GetRowSize( int row
)
5619 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5621 return GetRowHeight(row
);
5624 int wxGrid::GetDefaultColSize()
5626 return m_defaultColWidth
;
5629 int wxGrid::GetColSize( int col
)
5631 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5633 return GetColWidth(col
);
5636 // ============================================================================
5637 // access to the grid attributes: each of them has a default value in the grid
5638 // itself and may be overidden on a per-cell basis
5639 // ============================================================================
5641 // ----------------------------------------------------------------------------
5642 // setting default attributes
5643 // ----------------------------------------------------------------------------
5645 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5647 m_defaultCellAttr
->SetBackgroundColour(col
);
5649 m_gridWin
->SetBackgroundColour(col
);
5653 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5655 m_defaultCellAttr
->SetTextColour(col
);
5658 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5660 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5663 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5665 m_defaultCellAttr
->SetFont(font
);
5668 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5670 m_defaultCellAttr
->SetRenderer(renderer
);
5673 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5675 m_defaultCellAttr
->SetEditor(editor
);
5678 // ----------------------------------------------------------------------------
5679 // access to the default attrbiutes
5680 // ----------------------------------------------------------------------------
5682 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5684 return m_defaultCellAttr
->GetBackgroundColour();
5687 wxColour
wxGrid::GetDefaultCellTextColour()
5689 return m_defaultCellAttr
->GetTextColour();
5692 wxFont
wxGrid::GetDefaultCellFont()
5694 return m_defaultCellAttr
->GetFont();
5697 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5699 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5702 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5704 return m_defaultCellAttr
->GetRenderer();
5707 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5709 return m_defaultCellAttr
->GetEditor();
5712 // ----------------------------------------------------------------------------
5713 // access to cell attributes
5714 // ----------------------------------------------------------------------------
5716 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5718 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5719 wxColour colour
= attr
->GetBackgroundColour();
5724 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5726 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5727 wxColour colour
= attr
->GetTextColour();
5732 wxFont
wxGrid::GetCellFont( int row
, int col
)
5734 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5735 wxFont font
= attr
->GetFont();
5740 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5742 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5743 attr
->GetAlignment(horiz
, vert
);
5747 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5749 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5750 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5755 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5757 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5758 wxGridCellEditor
* editor
= attr
->GetEditor();
5763 bool wxGrid::IsReadOnly(int row
, int col
) const
5765 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5766 bool isReadOnly
= attr
->IsReadOnly();
5771 // ----------------------------------------------------------------------------
5772 // attribute support: cache, automatic provider creation, ...
5773 // ----------------------------------------------------------------------------
5775 bool wxGrid::CanHaveAttributes()
5782 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5783 // table is providing the attributes itself??? In which case
5784 // I don't think the grid should create a Provider object for the
5785 // table but the table should be smart enough to do that on its own.
5786 if ( !m_table
->GetAttrProvider() )
5788 // use the default attr provider by default
5789 // (another choice would be to just return FALSE thus forcing the user
5791 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5797 void wxGrid::ClearAttrCache()
5799 if ( m_attrCache
.row
!= -1 )
5801 m_attrCache
.attr
->SafeDecRef();
5802 m_attrCache
.row
= -1;
5806 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5808 wxGrid
*self
= (wxGrid
*)this; // const_cast
5810 self
->ClearAttrCache();
5811 self
->m_attrCache
.row
= row
;
5812 self
->m_attrCache
.col
= col
;
5813 self
->m_attrCache
.attr
= attr
;
5817 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5819 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5821 *attr
= m_attrCache
.attr
;
5822 (*attr
)->SafeIncRef();
5824 #ifdef DEBUG_ATTR_CACHE
5825 gs_nAttrCacheHits
++;
5832 #ifdef DEBUG_ATTR_CACHE
5833 gs_nAttrCacheMisses
++;
5839 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5841 wxGridCellAttr
*attr
;
5842 if ( !LookupAttr(row
, col
, &attr
) )
5844 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5845 CacheAttr(row
, col
, attr
);
5849 attr
->SetDefAttr(m_defaultCellAttr
);
5853 attr
= m_defaultCellAttr
;
5860 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5862 wxGridCellAttr
*attr
;
5863 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5865 wxASSERT_MSG( m_table
,
5866 _T("we may only be called if CanHaveAttributes() "
5867 "returned TRUE and then m_table should be !NULL") );
5869 attr
= m_table
->GetAttr(row
, col
);
5872 attr
= new wxGridCellAttr
;
5874 // artificially inc the ref count to match DecRef() in caller
5877 m_table
->SetAttr(attr
, row
, col
);
5880 CacheAttr(row
, col
, attr
);
5882 attr
->SetDefAttr(m_defaultCellAttr
);
5886 // ----------------------------------------------------------------------------
5887 // setting cell attributes: this is forwarded to the table
5888 // ----------------------------------------------------------------------------
5890 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5892 if ( CanHaveAttributes() )
5894 m_table
->SetRowAttr(attr
, row
);
5902 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5904 if ( CanHaveAttributes() )
5906 m_table
->SetColAttr(attr
, col
);
5914 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5916 if ( CanHaveAttributes() )
5918 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5919 attr
->SetBackgroundColour(colour
);
5924 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5926 if ( CanHaveAttributes() )
5928 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5929 attr
->SetTextColour(colour
);
5934 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5936 if ( CanHaveAttributes() )
5938 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5939 attr
->SetFont(font
);
5944 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5946 if ( CanHaveAttributes() )
5948 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5949 attr
->SetAlignment(horiz
, vert
);
5954 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5956 if ( CanHaveAttributes() )
5958 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5959 attr
->SetRenderer(renderer
);
5964 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5966 if ( CanHaveAttributes() )
5968 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5969 attr
->SetEditor(editor
);
5974 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5976 if ( CanHaveAttributes() )
5978 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5979 attr
->SetReadOnly(isReadOnly
);
5984 // ----------------------------------------------------------------------------
5986 // ----------------------------------------------------------------------------
5988 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5990 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5992 if ( resizeExistingRows
)
6000 void wxGrid::SetRowSize( int row
, int height
)
6002 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6004 if ( m_rowHeights
.IsEmpty() )
6006 // need to really create the array
6010 int h
= wxMax( 0, height
);
6011 int diff
= h
- m_rowHeights
[row
];
6013 m_rowHeights
[row
] = h
;
6015 for ( i
= row
; i
< m_numRows
; i
++ )
6017 m_rowBottoms
[i
] += diff
;
6022 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6024 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6026 if ( resizeExistingCols
)
6034 void wxGrid::SetColSize( int col
, int width
)
6036 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6038 if ( m_colWidths
.IsEmpty() )
6040 // need to really create the array
6044 int w
= wxMax( 0, width
);
6045 int diff
= w
- m_colWidths
[col
];
6046 m_colWidths
[col
] = w
;
6049 for ( i
= col
; i
< m_numCols
; i
++ )
6051 m_colRights
[i
] += diff
;
6058 // ------ cell value accessor functions
6061 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6065 m_table
->SetValue( row
, col
, s
.c_str() );
6066 if ( !GetBatchCount() )
6068 wxClientDC
dc( m_gridWin
);
6070 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6073 #if 0 // TODO: edit in place
6075 if ( m_currentCellCoords
.GetRow() == row
&&
6076 m_currentCellCoords
.GetCol() == col
)
6078 SetEditControlValue( s
);
6087 // ------ Block, row and col selection
6090 void wxGrid::SelectRow( int row
, bool addToSelected
)
6094 if ( IsSelection() && addToSelected
)
6097 bool need_refresh
[4];
6101 need_refresh
[3] = FALSE
;
6105 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6106 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6107 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6108 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6112 need_refresh
[0] = TRUE
;
6113 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6114 wxGridCellCoords ( oldTop
- 1,
6116 m_selectedTopLeft
.SetRow( row
);
6121 need_refresh
[1] = TRUE
;
6122 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6123 wxGridCellCoords ( oldBottom
,
6126 m_selectedTopLeft
.SetCol( 0 );
6129 if ( oldBottom
< row
)
6131 need_refresh
[2] = TRUE
;
6132 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6133 wxGridCellCoords ( row
,
6135 m_selectedBottomRight
.SetRow( row
);
6138 if ( oldRight
< m_numCols
- 1 )
6140 need_refresh
[3] = TRUE
;
6141 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6143 wxGridCellCoords ( oldBottom
,
6145 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6148 for (i
= 0; i
< 4; i
++ )
6149 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6150 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6154 r
= SelectionToDeviceRect();
6156 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6158 m_selectedTopLeft
.Set( row
, 0 );
6159 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6160 r
= SelectionToDeviceRect();
6161 m_gridWin
->Refresh( FALSE
, &r
);
6164 wxGridRangeSelectEvent
gridEvt( GetId(),
6165 wxEVT_GRID_RANGE_SELECT
,
6168 m_selectedBottomRight
);
6170 GetEventHandler()->ProcessEvent(gridEvt
);
6174 void wxGrid::SelectCol( int col
, bool addToSelected
)
6176 if ( IsSelection() && addToSelected
)
6179 bool need_refresh
[4];
6183 need_refresh
[3] = FALSE
;
6186 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6187 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6188 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6189 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6191 if ( oldLeft
> col
)
6193 need_refresh
[0] = TRUE
;
6194 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6195 wxGridCellCoords ( m_numRows
- 1,
6197 m_selectedTopLeft
.SetCol( col
);
6202 need_refresh
[1] = TRUE
;
6203 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6204 wxGridCellCoords ( oldTop
- 1,
6206 m_selectedTopLeft
.SetRow( 0 );
6209 if ( oldRight
< col
)
6211 need_refresh
[2] = TRUE
;
6212 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6213 wxGridCellCoords ( m_numRows
- 1,
6215 m_selectedBottomRight
.SetCol( col
);
6218 if ( oldBottom
< m_numRows
- 1 )
6220 need_refresh
[3] = TRUE
;
6221 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6223 wxGridCellCoords ( m_numRows
- 1,
6225 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6228 for (i
= 0; i
< 4; i
++ )
6229 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6230 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6236 r
= SelectionToDeviceRect();
6238 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6240 m_selectedTopLeft
.Set( 0, col
);
6241 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6242 r
= SelectionToDeviceRect();
6243 m_gridWin
->Refresh( FALSE
, &r
);
6246 wxGridRangeSelectEvent
gridEvt( GetId(),
6247 wxEVT_GRID_RANGE_SELECT
,
6250 m_selectedBottomRight
);
6252 GetEventHandler()->ProcessEvent(gridEvt
);
6256 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6259 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6261 if ( topRow
> bottomRow
)
6268 if ( leftCol
> rightCol
)
6275 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6276 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6278 if ( m_selectedTopLeft
!= updateTopLeft
||
6279 m_selectedBottomRight
!= updateBottomRight
)
6281 // Compute two optimal update rectangles:
6282 // Either one rectangle is a real subset of the
6283 // other, or they are (almost) disjoint!
6285 bool need_refresh
[4];
6289 need_refresh
[3] = FALSE
;
6292 // Store intermediate values
6293 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6294 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6295 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6296 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6298 // Determine the outer/inner coordinates.
6299 if (oldLeft
> leftCol
)
6305 if (oldTop
> topRow
)
6311 if (oldRight
< rightCol
)
6314 oldRight
= rightCol
;
6317 if (oldBottom
< bottomRow
)
6320 oldBottom
= bottomRow
;
6324 // Now, either the stuff marked old is the outer
6325 // rectangle or we don't have a situation where one
6326 // is contained in the other.
6328 if ( oldLeft
< leftCol
)
6330 need_refresh
[0] = TRUE
;
6331 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6333 wxGridCellCoords ( oldBottom
,
6337 if ( oldTop
< topRow
)
6339 need_refresh
[1] = TRUE
;
6340 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6342 wxGridCellCoords ( topRow
- 1,
6346 if ( oldRight
> rightCol
)
6348 need_refresh
[2] = TRUE
;
6349 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6351 wxGridCellCoords ( oldBottom
,
6355 if ( oldBottom
> bottomRow
)
6357 need_refresh
[3] = TRUE
;
6358 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6360 wxGridCellCoords ( oldBottom
,
6366 m_selectedTopLeft
= updateTopLeft
;
6367 m_selectedBottomRight
= updateBottomRight
;
6369 // various Refresh() calls
6370 for (i
= 0; i
< 4; i
++ )
6371 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6372 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6375 // only generate an event if the block is not being selected by
6376 // dragging the mouse (in which case the event will be generated in
6377 // the mouse event handler)
6378 if ( !m_isDragging
)
6380 wxGridRangeSelectEvent
gridEvt( GetId(),
6381 wxEVT_GRID_RANGE_SELECT
,
6384 m_selectedBottomRight
);
6386 GetEventHandler()->ProcessEvent(gridEvt
);
6390 void wxGrid::SelectAll()
6392 m_selectedTopLeft
.Set( 0, 0 );
6393 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6395 m_gridWin
->Refresh();
6399 void wxGrid::ClearSelection()
6401 m_selectedTopLeft
= wxGridNoCellCoords
;
6402 m_selectedBottomRight
= wxGridNoCellCoords
;
6406 // This function returns the rectangle that encloses the given block
6407 // in device coords clipped to the client size of the grid window.
6409 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6410 const wxGridCellCoords
&bottomRight
)
6412 wxRect
rect( wxGridNoCellRect
);
6415 cellRect
= CellToRect( topLeft
);
6416 if ( cellRect
!= wxGridNoCellRect
)
6422 rect
= wxRect( 0, 0, 0, 0 );
6425 cellRect
= CellToRect( bottomRight
);
6426 if ( cellRect
!= wxGridNoCellRect
)
6432 return wxGridNoCellRect
;
6435 // convert to scrolled coords
6437 int left
, top
, right
, bottom
;
6438 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6439 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6442 m_gridWin
->GetClientSize( &cw
, &ch
);
6444 rect
.SetLeft( wxMax(0, left
) );
6445 rect
.SetTop( wxMax(0, top
) );
6446 rect
.SetRight( wxMin(cw
, right
) );
6447 rect
.SetBottom( wxMin(ch
, bottom
) );
6455 // ------ Grid event classes
6458 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6460 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6461 int row
, int col
, int x
, int y
,
6462 bool control
, bool shift
, bool alt
, bool meta
)
6463 : wxNotifyEvent( type
, id
)
6469 m_control
= control
;
6474 SetEventObject(obj
);
6478 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6480 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6481 int rowOrCol
, int x
, int y
,
6482 bool control
, bool shift
, bool alt
, bool meta
)
6483 : wxNotifyEvent( type
, id
)
6485 m_rowOrCol
= rowOrCol
;
6488 m_control
= control
;
6493 SetEventObject(obj
);
6497 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6499 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6500 const wxGridCellCoords
& topLeft
,
6501 const wxGridCellCoords
& bottomRight
,
6502 bool control
, bool shift
, bool alt
, bool meta
)
6503 : wxNotifyEvent( type
, id
)
6505 m_topLeft
= topLeft
;
6506 m_bottomRight
= bottomRight
;
6507 m_control
= control
;
6512 SetEventObject(obj
);
6516 #endif // ifndef wxUSE_NEW_GRID