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"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
57 struct wxGridCellWithAttr
59 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
60 : coords(row
, col
), attr(attr_
)
69 wxGridCellCoords coords
;
73 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
75 #include "wx/arrimpl.cpp"
77 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
78 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
87 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
88 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
89 const wxPoint
&pos
, const wxSize
&size
);
94 void OnPaint( wxPaintEvent
& event
);
95 void OnMouseEvent( wxMouseEvent
& event
);
96 void OnKeyDown( wxKeyEvent
& event
);
98 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
103 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
106 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
107 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
108 const wxPoint
&pos
, const wxSize
&size
);
113 void OnPaint( wxPaintEvent
&event
);
114 void OnMouseEvent( wxMouseEvent
& event
);
115 void OnKeyDown( wxKeyEvent
& event
);
117 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
118 DECLARE_EVENT_TABLE()
122 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
125 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
126 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
127 const wxPoint
&pos
, const wxSize
&size
);
132 void OnMouseEvent( wxMouseEvent
& event
);
133 void OnKeyDown( wxKeyEvent
& event
);
134 void OnPaint( wxPaintEvent
& event
);
136 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
137 DECLARE_EVENT_TABLE()
140 class WXDLLEXPORT wxGridWindow
: public wxPanel
145 m_owner
= (wxGrid
*)NULL
;
146 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
147 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
150 wxGridWindow( wxGrid
*parent
,
151 wxGridRowLabelWindow
*rowLblWin
,
152 wxGridColLabelWindow
*colLblWin
,
153 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
156 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
160 wxGridRowLabelWindow
*m_rowLabelWin
;
161 wxGridColLabelWindow
*m_colLabelWin
;
163 void OnPaint( wxPaintEvent
&event
);
164 void OnMouseEvent( wxMouseEvent
& event
);
165 void OnKeyDown( wxKeyEvent
& );
166 void OnEraseBackground( wxEraseEvent
& );
169 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
170 DECLARE_EVENT_TABLE()
175 class wxGridCellEditorEvtHandler
: public wxEvtHandler
178 wxGridCellEditorEvtHandler()
179 : m_grid(0), m_editor(0)
181 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
182 : m_grid(grid
), m_editor(editor
)
185 void OnKeyDown(wxKeyEvent
& event
);
186 void OnChar(wxKeyEvent
& event
);
190 wxGridCellEditor
* m_editor
;
191 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
192 DECLARE_EVENT_TABLE()
196 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
197 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
198 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
199 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
204 // ----------------------------------------------------------------------------
205 // the internal data representation used by wxGridCellAttrProvider
206 // ----------------------------------------------------------------------------
208 // this class stores attributes set for cells
209 class WXDLLEXPORT wxGridCellAttrData
212 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
213 wxGridCellAttr
*GetAttr(int row
, int col
) const;
214 void UpdateAttrRows( size_t pos
, int numRows
);
215 void UpdateAttrCols( size_t pos
, int numCols
);
218 // searches for the attr for given cell, returns wxNOT_FOUND if not found
219 int FindIndex(int row
, int col
) const;
221 wxGridCellWithAttrArray m_attrs
;
224 // this class stores attributes set for rows or columns
225 class WXDLLEXPORT wxGridRowOrColAttrData
228 // empty ctor to suppress warnings
229 wxGridRowOrColAttrData() { }
230 ~wxGridRowOrColAttrData();
232 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
233 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
234 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
237 wxArrayInt m_rowsOrCols
;
238 wxArrayAttrs m_attrs
;
241 // NB: this is just a wrapper around 3 objects: one which stores cell
242 // attributes, and 2 others for row/col ones
243 class WXDLLEXPORT wxGridCellAttrProviderData
246 wxGridCellAttrData m_cellAttrs
;
247 wxGridRowOrColAttrData m_rowAttrs
,
251 // ----------------------------------------------------------------------------
252 // conditional compilation
253 // ----------------------------------------------------------------------------
255 #ifndef WXGRID_DRAW_LINES
256 #define WXGRID_DRAW_LINES 1
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 //#define DEBUG_ATTR_CACHE
264 #ifdef DEBUG_ATTR_CACHE
265 static size_t gs_nAttrCacheHits
= 0;
266 static size_t gs_nAttrCacheMisses
= 0;
267 #endif // DEBUG_ATTR_CACHE
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
274 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
277 // TODO: fixed so far - make configurable later (and also different for x/y)
278 static const size_t GRID_SCROLL_LINE
= 10;
280 // the size of hash tables used a bit everywhere (the max number of elements
281 // in these hash tables is the number of rows/columns)
282 static const int GRID_HASH_SIZE
= 100;
284 // ============================================================================
286 // ============================================================================
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 wxGridCellEditor::wxGridCellEditor()
298 wxGridCellEditor::~wxGridCellEditor()
303 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
304 wxWindowID
WXUNUSED(id
),
305 wxEvtHandler
* evtHandler
)
308 m_control
->PushEventHandler(evtHandler
);
311 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
312 wxGridCellAttr
*attr
)
314 // erase the background because we might not fill the cell
315 wxClientDC
dc(m_control
->GetParent());
316 dc
.SetPen(*wxTRANSPARENT_PEN
);
317 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
318 dc
.DrawRectangle(rectCell
);
320 // redraw the control we just painted over
321 m_control
->Refresh();
324 void wxGridCellEditor::Destroy()
328 m_control
->Destroy();
333 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
335 wxASSERT_MSG(m_control
,
336 wxT("The wxGridCellEditor must be Created first!"));
337 m_control
->Show(show
);
341 // set the colours/fonts if we have any
344 if ( attr
->HasTextColour() )
346 m_colFgOld
= m_control
->GetForegroundColour();
347 m_control
->SetForegroundColour(attr
->GetTextColour());
350 if ( attr
->HasBackgroundColour() )
352 m_colBgOld
= m_control
->GetBackgroundColour();
353 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
356 if ( attr
->HasFont() )
358 m_fontOld
= m_control
->GetFont();
359 m_control
->SetFont(attr
->GetFont());
362 // can't do anything more in the base class version, the other
363 // attributes may only be used by the derived classes
368 // restore the standard colours fonts
369 if ( m_colFgOld
.Ok() )
371 m_control
->SetForegroundColour(m_colFgOld
);
372 m_colFgOld
= wxNullColour
;
375 if ( m_colBgOld
.Ok() )
377 m_control
->SetBackgroundColour(m_colBgOld
);
378 m_colBgOld
= wxNullColour
;
381 if ( m_fontOld
.Ok() )
383 m_control
->SetFont(m_fontOld
);
384 m_fontOld
= wxNullFont
;
389 void wxGridCellEditor::SetSize(const wxRect
& rect
)
391 wxASSERT_MSG(m_control
,
392 wxT("The wxGridCellEditor must be Created first!"));
393 m_control
->SetSize(rect
);
396 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
402 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
407 void wxGridCellEditor::StartingClick()
411 // ----------------------------------------------------------------------------
412 // wxGridCellTextEditor
413 // ----------------------------------------------------------------------------
415 wxGridCellTextEditor::wxGridCellTextEditor()
419 void wxGridCellTextEditor::Create(wxWindow
* parent
,
421 wxEvtHandler
* evtHandler
)
423 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
424 wxDefaultPosition
, wxDefaultSize
425 #if defined(__WXMSW__)
426 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
430 wxGridCellEditor::Create(parent
, id
, evtHandler
);
433 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
434 wxGridCellAttr
* WXUNUSED(attr
))
436 // as we fill the entire client area, don't do anything here to minimize
440 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
442 wxASSERT_MSG(m_control
,
443 wxT("The wxGridCellEditor must be Created first!"));
445 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
446 Text()->SetValue(m_startValue
);
447 Text()->SetInsertionPointEnd();
452 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
455 wxASSERT_MSG(m_control
,
456 wxT("The wxGridCellEditor must be Created first!"));
458 bool changed
= FALSE
;
459 wxString value
= Text()->GetValue();
460 if (value
!= m_startValue
)
464 grid
->GetTable()->SetValue(row
, col
, value
);
466 m_startValue
= wxEmptyString
;
467 Text()->SetValue(m_startValue
);
473 void wxGridCellTextEditor::Reset()
475 wxASSERT_MSG(m_control
,
476 wxT("The wxGridCellEditor must be Created first!"));
478 Text()->SetValue(m_startValue
);
479 Text()->SetInsertionPointEnd();
482 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
484 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
486 // insert the key in the control
487 long keycode
= event
.KeyCode();
488 if ( isprint(keycode
) )
490 // FIXME this is not going to work for non letters...
491 if ( !event
.ShiftDown() )
493 keycode
= tolower(keycode
);
496 Text()->AppendText((wxChar
)keycode
);
506 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
508 #if defined(__WXMOTIF__) || defined(__WXGTK__)
509 // wxMotif needs a little extra help...
510 int pos
= Text()->GetInsertionPoint();
511 wxString
s( Text()->GetValue() );
512 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
514 Text()->SetInsertionPoint( pos
);
516 // the other ports can handle a Return key press
522 // ----------------------------------------------------------------------------
523 // wxGridCellBoolEditor
524 // ----------------------------------------------------------------------------
526 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
528 wxEvtHandler
* evtHandler
)
530 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
531 wxDefaultPosition
, wxDefaultSize
,
534 wxGridCellEditor::Create(parent
, id
, evtHandler
);
537 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
539 // position it in the centre of the rectangle (TODO: support alignment?)
541 m_control
->GetSize(&w
, &h
);
543 // the checkbox without label still has some space to the right in wxGTK,
544 // so shift it to the right
549 m_control
->Move(r
.x
+ r
.width
/2 - w
/2, r
.y
+ r
.height
/2 - h
/2);
552 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
554 wxGridCellEditor::Show(show
, attr
);
557 // VZ: normally base class already does it, but it doesn't work (FIXME)
558 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
559 CBox()->SetBackgroundColour(colBg
);
563 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
565 wxASSERT_MSG(m_control
,
566 wxT("The wxGridCellEditor must be Created first!"));
568 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
); // FIXME-DATA
569 CBox()->SetValue(m_startValue
);
573 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
577 wxASSERT_MSG(m_control
,
578 wxT("The wxGridCellEditor must be Created first!"));
580 bool changed
= FALSE
;
581 bool value
= CBox()->GetValue();
582 if ( value
!= m_startValue
)
588 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
594 void wxGridCellBoolEditor::Reset()
596 wxASSERT_MSG(m_control
,
597 wxT("The wxGridCellEditor must be Created first!"));
599 CBox()->SetValue(m_startValue
);
602 void wxGridCellBoolEditor::StartingClick()
604 CBox()->SetValue(!CBox()->GetValue());
607 // ----------------------------------------------------------------------------
608 // wxGridCellEditorEvtHandler
609 // ----------------------------------------------------------------------------
611 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
613 switch ( event
.KeyCode() )
617 m_grid
->DisableCellEditControl();
621 event
.Skip( m_grid
->ProcessEvent( event
) );
625 if (!m_grid
->ProcessEvent(event
))
626 m_editor
->HandleReturn(event
);
635 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
637 switch ( event
.KeyCode() )
649 // ============================================================================
651 // ============================================================================
653 // ----------------------------------------------------------------------------
654 // wxGridCellRenderer
655 // ----------------------------------------------------------------------------
657 void wxGridCellRenderer::Draw(wxGrid
& grid
,
658 wxGridCellAttr
& attr
,
664 dc
.SetBackgroundMode( wxSOLID
);
668 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
672 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
675 dc
.SetPen( *wxTRANSPARENT_PEN
);
676 dc
.DrawRectangle(rect
);
679 // ----------------------------------------------------------------------------
680 // wxGridCellStringRenderer
681 // ----------------------------------------------------------------------------
683 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
684 wxGridCellAttr
& attr
,
686 const wxRect
& rectCell
,
690 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
692 // now we only have to draw the text
693 dc
.SetBackgroundMode( wxTRANSPARENT
);
695 // TODO some special colours for attr.IsReadOnly() case?
699 dc
.SetTextBackground( grid
.GetSelectionBackground() );
700 dc
.SetTextForeground( grid
.GetSelectionForeground() );
704 dc
.SetTextBackground( attr
.GetBackgroundColour() );
705 dc
.SetTextForeground( attr
.GetTextColour() );
707 dc
.SetFont( attr
.GetFont() );
710 attr
.GetAlignment(&hAlign
, &vAlign
);
712 wxRect rect
= rectCell
;
718 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
719 rect
, hAlign
, vAlign
);
722 // ----------------------------------------------------------------------------
723 // wxGridCellBoolRenderer
724 // ----------------------------------------------------------------------------
726 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
727 wxGridCellAttr
& attr
,
733 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
735 // between checkmark and box
736 static const wxCoord margin
= 4;
739 static wxCoord s_checkSize
= 0;
740 if ( s_checkSize
== 0 )
742 // compute it only once (no locks for MT safeness in GUI thread...)
743 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
744 wxSize size
= checkbox
->GetBestSize();
745 s_checkSize
= size
.y
+ margin
;
747 // FIXME wxGTK::wxCheckBox::GetBestSize() is really weird...
749 s_checkSize
-= size
.y
/ 2;
755 // draw a check mark in the centre (ignoring alignment - TODO)
757 rectMark
.x
= rect
.x
+ rect
.width
/2 - s_checkSize
/2;
758 rectMark
.y
= rect
.y
+ rect
.height
/2 - s_checkSize
/2;
759 rectMark
.width
= rectMark
.height
= s_checkSize
;
761 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
762 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
763 dc
.DrawRectangle(rectMark
);
765 rectMark
.Inflate(-margin
);
767 if ( !!grid
.GetTable()->GetValue(row
, col
) ) // FIXME-DATA
769 dc
.SetTextForeground(attr
.GetTextColour());
770 dc
.DrawCheckMark(rectMark
);
774 // ----------------------------------------------------------------------------
776 // ----------------------------------------------------------------------------
778 const wxColour
& wxGridCellAttr::GetTextColour() const
784 else if (m_defGridAttr
!= this)
786 return m_defGridAttr
->GetTextColour();
790 wxFAIL_MSG(wxT("Missing default cell attribute"));
796 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
798 if (HasBackgroundColour())
800 else if (m_defGridAttr
!= this)
801 return m_defGridAttr
->GetBackgroundColour();
804 wxFAIL_MSG(wxT("Missing default cell attribute"));
810 const wxFont
& wxGridCellAttr::GetFont() const
814 else if (m_defGridAttr
!= this)
815 return m_defGridAttr
->GetFont();
818 wxFAIL_MSG(wxT("Missing default cell attribute"));
824 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
828 if ( hAlign
) *hAlign
= m_hAlign
;
829 if ( vAlign
) *vAlign
= m_vAlign
;
831 else if (m_defGridAttr
!= this)
832 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
835 wxFAIL_MSG(wxT("Missing default cell attribute"));
840 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
844 else if (m_defGridAttr
!= this)
845 return m_defGridAttr
->GetRenderer();
848 wxFAIL_MSG(wxT("Missing default cell attribute"));
853 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
857 else if (m_defGridAttr
!= this)
858 return m_defGridAttr
->GetEditor();
861 wxFAIL_MSG(wxT("Missing default cell attribute"));
866 // ----------------------------------------------------------------------------
867 // wxGridCellAttrData
868 // ----------------------------------------------------------------------------
870 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
872 int n
= FindIndex(row
, col
);
873 if ( n
== wxNOT_FOUND
)
876 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
882 // change the attribute
883 m_attrs
[(size_t)n
].attr
= attr
;
887 // remove this attribute
888 m_attrs
.RemoveAt((size_t)n
);
893 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
895 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
897 int n
= FindIndex(row
, col
);
898 if ( n
!= wxNOT_FOUND
)
900 attr
= m_attrs
[(size_t)n
].attr
;
907 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
909 size_t count
= m_attrs
.GetCount();
910 for ( size_t n
= 0; n
< count
; n
++ )
912 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
913 wxCoord row
= coords
.GetRow();
914 if ((size_t)row
>= pos
)
918 // If rows inserted, include row counter where necessary
919 coords
.SetRow(row
+ numRows
);
921 else if (numRows
< 0)
923 // If rows deleted ...
924 if ((size_t)row
>= pos
- numRows
)
926 // ...either decrement row counter (if row still exists)...
927 coords
.SetRow(row
+ numRows
);
931 // ...or remove the attribute
932 m_attrs
.RemoveAt((size_t)n
);
940 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
942 size_t count
= m_attrs
.GetCount();
943 for ( size_t n
= 0; n
< count
; n
++ )
945 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
946 wxCoord col
= coords
.GetCol();
947 if ( (size_t)col
>= pos
)
951 // If rows inserted, include row counter where necessary
952 coords
.SetCol(col
+ numCols
);
954 else if (numCols
< 0)
956 // If rows deleted ...
957 if ((size_t)col
>= pos
- numCols
)
959 // ...either decrement row counter (if row still exists)...
960 coords
.SetCol(col
+ numCols
);
964 // ...or remove the attribute
965 m_attrs
.RemoveAt((size_t)n
);
973 int wxGridCellAttrData::FindIndex(int row
, int col
) const
975 size_t count
= m_attrs
.GetCount();
976 for ( size_t n
= 0; n
< count
; n
++ )
978 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
979 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
988 // ----------------------------------------------------------------------------
989 // wxGridRowOrColAttrData
990 // ----------------------------------------------------------------------------
992 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
994 size_t count
= m_attrs
.Count();
995 for ( size_t n
= 0; n
< count
; n
++ )
997 m_attrs
[n
]->DecRef();
1001 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1003 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1005 int n
= m_rowsOrCols
.Index(rowOrCol
);
1006 if ( n
!= wxNOT_FOUND
)
1008 attr
= m_attrs
[(size_t)n
];
1015 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1017 int n
= m_rowsOrCols
.Index(rowOrCol
);
1018 if ( n
== wxNOT_FOUND
)
1020 // add the attribute
1021 m_rowsOrCols
.Add(rowOrCol
);
1028 // change the attribute
1029 m_attrs
[(size_t)n
] = attr
;
1033 // remove this attribute
1034 m_attrs
[(size_t)n
]->DecRef();
1035 m_rowsOrCols
.RemoveAt((size_t)n
);
1036 m_attrs
.RemoveAt((size_t)n
);
1041 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1043 size_t count
= m_attrs
.GetCount();
1044 for ( size_t n
= 0; n
< count
; n
++ )
1046 int & rowOrCol
= m_rowsOrCols
[n
];
1047 if ( (size_t)rowOrCol
>= pos
)
1049 if ( numRowsOrCols
> 0 )
1051 // If rows inserted, include row counter where necessary
1052 rowOrCol
+= numRowsOrCols
;
1054 else if ( numRowsOrCols
< 0)
1056 // If rows deleted, either decrement row counter (if row still exists)
1057 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1058 rowOrCol
+= numRowsOrCols
;
1061 m_rowsOrCols
.RemoveAt((size_t)n
);
1062 m_attrs
.RemoveAt((size_t)n
);
1070 // ----------------------------------------------------------------------------
1071 // wxGridCellAttrProvider
1072 // ----------------------------------------------------------------------------
1074 wxGridCellAttrProvider::wxGridCellAttrProvider()
1076 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1079 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1084 void wxGridCellAttrProvider::InitData()
1086 m_data
= new wxGridCellAttrProviderData
;
1089 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1091 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1094 // first look for the attribute of this specific cell
1095 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1099 // then look for the col attr (col attributes are more common than
1100 // the row ones, hence they have priority)
1101 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1106 // finally try the row attributes
1107 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1114 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1120 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1123 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1128 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1131 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1136 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1139 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1143 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1145 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1149 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1153 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1155 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1159 // ----------------------------------------------------------------------------
1161 // ----------------------------------------------------------------------------
1163 //////////////////////////////////////////////////////////////////////
1165 // Abstract base class for grid data (the model)
1167 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1170 wxGridTableBase::wxGridTableBase()
1172 m_view
= (wxGrid
*) NULL
;
1173 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1176 wxGridTableBase::~wxGridTableBase()
1178 delete m_attrProvider
;
1181 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1183 delete m_attrProvider
;
1184 m_attrProvider
= attrProvider
;
1187 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1189 if ( m_attrProvider
)
1190 return m_attrProvider
->GetAttr(row
, col
);
1192 return (wxGridCellAttr
*)NULL
;
1195 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1197 if ( m_attrProvider
)
1199 m_attrProvider
->SetAttr(attr
, row
, col
);
1203 // as we take ownership of the pointer and don't store it, we must
1209 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1211 if ( m_attrProvider
)
1213 m_attrProvider
->SetRowAttr(attr
, row
);
1217 // as we take ownership of the pointer and don't store it, we must
1223 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1225 if ( m_attrProvider
)
1227 m_attrProvider
->SetColAttr(attr
, col
);
1231 // as we take ownership of the pointer and don't store it, we must
1237 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1239 if ( m_attrProvider
)
1241 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1245 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1247 if ( m_attrProvider
)
1249 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1253 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1255 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1256 "but your derived table class does not override this function") );
1261 bool wxGridTableBase::AppendRows( size_t numRows
)
1263 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1264 "but your derived table class does not override this function"));
1269 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1271 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1272 "but your derived table class does not override this function"));
1277 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1279 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1280 "but your derived table class does not override this function"));
1285 bool wxGridTableBase::AppendCols( size_t numCols
)
1287 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1288 "but your derived table class does not override this function"));
1293 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1295 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1296 "but your derived table class does not override this function"));
1302 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1309 wxString
wxGridTableBase::GetColLabelValue( int col
)
1311 // default col labels are:
1312 // cols 0 to 25 : A-Z
1313 // cols 26 to 675 : AA-ZZ
1318 for ( n
= 1; ; n
++ )
1320 s
+= (_T('A') + (wxChar
)( col%26
));
1322 if ( col
< 0 ) break;
1325 // reverse the string...
1327 for ( i
= 0; i
< n
; i
++ )
1337 //////////////////////////////////////////////////////////////////////
1339 // Message class for the grid table to send requests and notifications
1343 wxGridTableMessage::wxGridTableMessage()
1345 m_table
= (wxGridTableBase
*) NULL
;
1351 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1352 int commandInt1
, int commandInt2
)
1356 m_comInt1
= commandInt1
;
1357 m_comInt2
= commandInt2
;
1362 //////////////////////////////////////////////////////////////////////
1364 // A basic grid table for string data. An object of this class will
1365 // created by wxGrid if you don't specify an alternative table class.
1368 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1370 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1372 wxGridStringTable::wxGridStringTable()
1377 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1382 m_data
.Alloc( numRows
);
1385 sa
.Alloc( numCols
);
1386 for ( col
= 0; col
< numCols
; col
++ )
1388 sa
.Add( wxEmptyString
);
1391 for ( row
= 0; row
< numRows
; row
++ )
1397 wxGridStringTable::~wxGridStringTable()
1401 long wxGridStringTable::GetNumberRows()
1403 return m_data
.GetCount();
1406 long wxGridStringTable::GetNumberCols()
1408 if ( m_data
.GetCount() > 0 )
1409 return m_data
[0].GetCount();
1414 wxString
wxGridStringTable::GetValue( int row
, int col
)
1416 // TODO: bounds checking
1418 return m_data
[row
][col
];
1421 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1423 // TODO: bounds checking
1425 m_data
[row
][col
] = s
;
1428 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1430 // TODO: bounds checking
1432 return (m_data
[row
][col
] == wxEmptyString
);
1436 void wxGridStringTable::Clear()
1439 int numRows
, numCols
;
1441 numRows
= m_data
.GetCount();
1444 numCols
= m_data
[0].GetCount();
1446 for ( row
= 0; row
< numRows
; row
++ )
1448 for ( col
= 0; col
< numCols
; col
++ )
1450 m_data
[row
][col
] = wxEmptyString
;
1457 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1461 size_t curNumRows
= m_data
.GetCount();
1462 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1464 if ( pos
>= curNumRows
)
1466 return AppendRows( numRows
);
1470 sa
.Alloc( curNumCols
);
1471 for ( col
= 0; col
< curNumCols
; col
++ )
1473 sa
.Add( wxEmptyString
);
1476 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1478 m_data
.Insert( sa
, row
);
1480 UpdateAttrRows( pos
, numRows
);
1483 wxGridTableMessage
msg( this,
1484 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1488 GetView()->ProcessTableMessage( msg
);
1494 bool wxGridStringTable::AppendRows( size_t numRows
)
1498 size_t curNumRows
= m_data
.GetCount();
1499 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1502 if ( curNumCols
> 0 )
1504 sa
.Alloc( curNumCols
);
1505 for ( col
= 0; col
< curNumCols
; col
++ )
1507 sa
.Add( wxEmptyString
);
1511 for ( row
= 0; row
< numRows
; row
++ )
1518 wxGridTableMessage
msg( this,
1519 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1522 GetView()->ProcessTableMessage( msg
);
1528 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1532 size_t curNumRows
= m_data
.GetCount();
1534 if ( pos
>= curNumRows
)
1537 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1538 "Pos value is invalid for present table with %d rows",
1539 pos
, numRows
, curNumRows
);
1540 wxFAIL_MSG( wxT(errmsg
) );
1544 if ( numRows
> curNumRows
- pos
)
1546 numRows
= curNumRows
- pos
;
1549 if ( numRows
>= curNumRows
)
1551 m_data
.Empty(); // don't release memory just yet
1555 for ( n
= 0; n
< numRows
; n
++ )
1557 m_data
.Remove( pos
);
1560 UpdateAttrRows( pos
, -((int)numRows
) );
1563 wxGridTableMessage
msg( this,
1564 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1568 GetView()->ProcessTableMessage( msg
);
1574 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1578 size_t curNumRows
= m_data
.GetCount();
1579 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1581 if ( pos
>= curNumCols
)
1583 return AppendCols( numCols
);
1586 for ( row
= 0; row
< curNumRows
; row
++ )
1588 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1590 m_data
[row
].Insert( wxEmptyString
, col
);
1593 UpdateAttrCols( pos
, numCols
);
1596 wxGridTableMessage
msg( this,
1597 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1601 GetView()->ProcessTableMessage( msg
);
1607 bool wxGridStringTable::AppendCols( size_t numCols
)
1611 size_t curNumRows
= m_data
.GetCount();
1614 // TODO: something better than this ?
1616 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1617 "Call AppendRows() first") );
1621 for ( row
= 0; row
< curNumRows
; row
++ )
1623 for ( n
= 0; n
< numCols
; n
++ )
1625 m_data
[row
].Add( wxEmptyString
);
1631 wxGridTableMessage
msg( this,
1632 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1635 GetView()->ProcessTableMessage( msg
);
1641 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1645 size_t curNumRows
= m_data
.GetCount();
1646 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1648 if ( pos
>= curNumCols
)
1651 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1652 "Pos value is invalid for present table with %d cols",
1653 pos
, numCols
, curNumCols
);
1654 wxFAIL_MSG( wxT( errmsg
) );
1658 if ( numCols
> curNumCols
- pos
)
1660 numCols
= curNumCols
- pos
;
1663 for ( row
= 0; row
< curNumRows
; row
++ )
1665 if ( numCols
>= curNumCols
)
1667 m_data
[row
].Clear();
1671 for ( n
= 0; n
< numCols
; n
++ )
1673 m_data
[row
].Remove( pos
);
1677 UpdateAttrCols( pos
, -((int)numCols
) );
1680 wxGridTableMessage
msg( this,
1681 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1685 GetView()->ProcessTableMessage( msg
);
1691 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1693 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1695 // using default label
1697 return wxGridTableBase::GetRowLabelValue( row
);
1701 return m_rowLabels
[ row
];
1705 wxString
wxGridStringTable::GetColLabelValue( int col
)
1707 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1709 // using default label
1711 return wxGridTableBase::GetColLabelValue( col
);
1715 return m_colLabels
[ col
];
1719 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1721 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1723 int n
= m_rowLabels
.GetCount();
1725 for ( i
= n
; i
<= row
; i
++ )
1727 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1731 m_rowLabels
[row
] = value
;
1734 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1736 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1738 int n
= m_colLabels
.GetCount();
1740 for ( i
= n
; i
<= col
; i
++ )
1742 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1746 m_colLabels
[col
] = value
;
1751 //////////////////////////////////////////////////////////////////////
1752 //////////////////////////////////////////////////////////////////////
1754 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1756 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1757 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1758 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1759 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1762 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1764 const wxPoint
&pos
, const wxSize
&size
)
1765 : wxWindow( parent
, id
, pos
, size
)
1770 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1774 // NO - don't do this because it will set both the x and y origin
1775 // coords to match the parent scrolled window and we just want to
1776 // set the y coord - MB
1778 // m_owner->PrepareDC( dc );
1781 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1782 dc
.SetDeviceOrigin( 0, -y
);
1784 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1785 m_owner
->DrawRowLabels( dc
);
1789 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1791 m_owner
->ProcessRowLabelMouseEvent( event
);
1795 // This seems to be required for wxMotif otherwise the mouse
1796 // cursor must be in the cell edit control to get key events
1798 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1800 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1805 //////////////////////////////////////////////////////////////////////
1807 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1809 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1810 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1811 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1812 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1815 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1817 const wxPoint
&pos
, const wxSize
&size
)
1818 : wxWindow( parent
, id
, pos
, size
)
1823 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1827 // NO - don't do this because it will set both the x and y origin
1828 // coords to match the parent scrolled window and we just want to
1829 // set the x coord - MB
1831 // m_owner->PrepareDC( dc );
1834 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1835 dc
.SetDeviceOrigin( -x
, 0 );
1837 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1838 m_owner
->DrawColLabels( dc
);
1842 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1844 m_owner
->ProcessColLabelMouseEvent( event
);
1848 // This seems to be required for wxMotif otherwise the mouse
1849 // cursor must be in the cell edit control to get key events
1851 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1853 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1858 //////////////////////////////////////////////////////////////////////
1860 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1862 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1863 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1864 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1865 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1868 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1870 const wxPoint
&pos
, const wxSize
&size
)
1871 : wxWindow( parent
, id
, pos
, size
)
1876 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1880 int client_height
= 0;
1881 int client_width
= 0;
1882 GetClientSize( &client_width
, &client_height
);
1884 dc
.SetPen( *wxBLACK_PEN
);
1885 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1886 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1888 dc
.SetPen( *wxWHITE_PEN
);
1889 dc
.DrawLine( 0, 0, client_width
, 0 );
1890 dc
.DrawLine( 0, 0, 0, client_height
);
1894 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1896 m_owner
->ProcessCornerLabelMouseEvent( event
);
1900 // This seems to be required for wxMotif otherwise the mouse
1901 // cursor must be in the cell edit control to get key events
1903 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1905 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1910 //////////////////////////////////////////////////////////////////////
1912 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1914 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1915 EVT_PAINT( wxGridWindow::OnPaint
)
1916 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1917 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1918 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1921 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1922 wxGridRowLabelWindow
*rowLblWin
,
1923 wxGridColLabelWindow
*colLblWin
,
1924 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1925 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1928 m_rowLabelWin
= rowLblWin
;
1929 m_colLabelWin
= colLblWin
;
1930 SetBackgroundColour( "WHITE" );
1934 wxGridWindow::~wxGridWindow()
1939 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1941 wxPaintDC
dc( this );
1942 m_owner
->PrepareDC( dc
);
1943 wxRegion reg
= GetUpdateRegion();
1944 m_owner
->CalcCellsExposed( reg
);
1945 m_owner
->DrawGridCellArea( dc
);
1946 #if WXGRID_DRAW_LINES
1947 m_owner
->DrawAllGridLines( dc
, reg
);
1949 m_owner
->DrawHighlight( dc
);
1953 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1955 wxPanel::ScrollWindow( dx
, dy
, rect
);
1956 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1957 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1961 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1963 m_owner
->ProcessGridCellMouseEvent( event
);
1967 // This seems to be required for wxMotif otherwise the mouse
1968 // cursor must be in the cell edit control to get key events
1970 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1972 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1975 // We are trapping erase background events to reduce flicker under MSW
1976 // and GTK but this can leave junk in the space beyond the last row and
1977 // col. So here we paint these spaces if they are visible.
1979 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
1982 GetClientSize( &cw
, &ch
);
1985 m_owner
->CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
1988 rightRect
= m_owner
->CellToRect( 0, m_owner
->GetNumberCols()-1 );
1991 bottomRect
= m_owner
->CellToRect( m_owner
->GetNumberRows()-1, 0 );
1993 if ( right
> rightRect
.GetRight() || bottom
> bottomRect
.GetBottom() )
1996 m_owner
->CalcUnscrolledPosition( 0, 0, &left
, &top
);
1998 wxClientDC
dc( this );
1999 m_owner
->PrepareDC( dc
);
2000 dc
.SetBrush( wxBrush(m_owner
->GetDefaultCellBackgroundColour(), wxSOLID
) );
2001 dc
.SetPen( *wxTRANSPARENT_PEN
);
2003 if ( right
> rightRect
.GetRight() )
2004 dc
.DrawRectangle( rightRect
.GetRight()+1, top
, right
- rightRect
.GetRight(), ch
);
2006 if ( bottom
> bottomRect
.GetBottom() )
2007 dc
.DrawRectangle( left
, bottomRect
.GetBottom()+1, cw
, bottom
- bottomRect
.GetBottom() );
2012 //////////////////////////////////////////////////////////////////////
2015 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2017 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2018 EVT_PAINT( wxGrid::OnPaint
)
2019 EVT_SIZE( wxGrid::OnSize
)
2020 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2021 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2024 wxGrid::wxGrid( wxWindow
*parent
,
2029 const wxString
& name
)
2030 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2031 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2040 m_defaultCellAttr
->SafeDecRef();
2042 #ifdef DEBUG_ATTR_CACHE
2043 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2044 wxPrintf(_T("wxGrid attribute cache statistics: "
2045 "total: %u, hits: %u (%u%%)\n"),
2046 total
, gs_nAttrCacheHits
,
2047 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2056 // ----- internal init and update functions
2059 void wxGrid::Create()
2061 m_created
= FALSE
; // set to TRUE by CreateGrid
2062 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2064 m_table
= (wxGridTableBase
*) NULL
;
2067 m_cellEditCtrlEnabled
= FALSE
;
2069 m_defaultCellAttr
= new wxGridCellAttr
;
2070 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2071 // RD: Should we fill the default attrs now or is waiting until Init() okay?
2076 m_currentCellCoords
= wxGridNoCellCoords
;
2078 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2079 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2081 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2086 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2091 m_colLabelWin
= new wxGridColLabelWindow( this,
2096 m_gridWin
= new wxGridWindow( this,
2103 SetTargetWindow( m_gridWin
);
2107 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2111 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2116 m_numRows
= numRows
;
2117 m_numCols
= numCols
;
2119 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2120 m_table
->SetView( this );
2129 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2133 // RD: Actually, this should probably be allowed. I think it would be
2134 // nice to be able to switch multiple Tables in and out of a single
2135 // View at runtime. Is there anything in the implmentation that would
2138 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2143 m_numRows
= table
->GetNumberRows();
2144 m_numCols
= table
->GetNumberCols();
2147 m_table
->SetView( this );
2160 if ( m_numRows
<= 0 )
2161 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2163 if ( m_numCols
<= 0 )
2164 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2166 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2167 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2169 if ( m_rowLabelWin
)
2171 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2175 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2178 m_labelTextColour
= wxColour( _T("BLACK") );
2181 m_attrCache
.row
= -1;
2183 // TODO: something better than this ?
2185 m_labelFont
= this->GetFont();
2186 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2188 m_rowLabelHorizAlign
= wxLEFT
;
2189 m_rowLabelVertAlign
= wxCENTRE
;
2191 m_colLabelHorizAlign
= wxCENTRE
;
2192 m_colLabelVertAlign
= wxTOP
;
2194 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2195 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2197 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2198 m_defaultRowHeight
+= 8;
2200 m_defaultRowHeight
+= 4;
2203 // Set default cell attributes
2204 m_defaultCellAttr
->SetFont(GetFont());
2205 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2206 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2207 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2208 m_defaultCellAttr
->SetTextColour(
2209 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2210 m_defaultCellAttr
->SetBackgroundColour(
2211 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2214 m_gridLineColour
= wxColour( 128, 128, 255 );
2215 m_gridLinesEnabled
= TRUE
;
2217 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2218 m_winCapture
= (wxWindow
*)NULL
;
2220 m_dragRowOrCol
= -1;
2221 m_isDragging
= FALSE
;
2222 m_startDragPos
= wxDefaultPosition
;
2224 m_waitForSlowClick
= FALSE
;
2226 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2227 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2229 m_currentCellCoords
= wxGridNoCellCoords
;
2231 m_selectedTopLeft
= wxGridNoCellCoords
;
2232 m_selectedBottomRight
= wxGridNoCellCoords
;
2233 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2234 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2236 m_editable
= TRUE
; // default for whole grid
2238 m_inOnKeyDown
= FALSE
;
2242 // ----------------------------------------------------------------------------
2243 // the idea is to call these functions only when necessary because they create
2244 // quite big arrays which eat memory mostly unnecessary - in particular, if
2245 // default widths/heights are used for all rows/columns, we may not use these
2248 // with some extra code, it should be possible to only store the
2249 // widths/heights different from default ones but this will be done later...
2250 // ----------------------------------------------------------------------------
2252 void wxGrid::InitRowHeights()
2254 m_rowHeights
.Empty();
2255 m_rowBottoms
.Empty();
2257 m_rowHeights
.Alloc( m_numRows
);
2258 m_rowBottoms
.Alloc( m_numRows
);
2261 for ( int i
= 0; i
< m_numRows
; i
++ )
2263 m_rowHeights
.Add( m_defaultRowHeight
);
2264 rowBottom
+= m_defaultRowHeight
;
2265 m_rowBottoms
.Add( rowBottom
);
2269 void wxGrid::InitColWidths()
2271 m_colWidths
.Empty();
2272 m_colRights
.Empty();
2274 m_colWidths
.Alloc( m_numCols
);
2275 m_colRights
.Alloc( m_numCols
);
2277 for ( int i
= 0; i
< m_numCols
; i
++ )
2279 m_colWidths
.Add( m_defaultColWidth
);
2280 colRight
+= m_defaultColWidth
;
2281 m_colRights
.Add( colRight
);
2285 int wxGrid::GetColWidth(int col
) const
2287 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2290 int wxGrid::GetColLeft(int col
) const
2292 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2293 : m_colRights
[col
] - m_colWidths
[col
];
2296 int wxGrid::GetColRight(int col
) const
2298 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
2302 int wxGrid::GetRowHeight(int row
) const
2304 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2307 int wxGrid::GetRowTop(int row
) const
2309 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2310 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2313 int wxGrid::GetRowBottom(int row
) const
2315 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2316 : m_rowBottoms
[row
];
2319 void wxGrid::CalcDimensions()
2322 GetClientSize( &cw
, &ch
);
2324 if ( m_numRows
> 0 && m_numCols
> 0 )
2326 int right
= GetColRight( m_numCols
-1 ) + 50;
2327 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
2329 // TODO: restore the scroll position that we had before sizing
2332 GetViewStart( &x
, &y
);
2333 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2334 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2340 void wxGrid::CalcWindowSizes()
2343 GetClientSize( &cw
, &ch
);
2345 if ( m_cornerLabelWin
->IsShown() )
2346 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2348 if ( m_colLabelWin
->IsShown() )
2349 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2351 if ( m_rowLabelWin
->IsShown() )
2352 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2354 if ( m_gridWin
->IsShown() )
2355 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2359 // this is called when the grid table sends a message to say that it
2360 // has been redimensioned
2362 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2366 // if we were using the default widths/heights so far, we must change them
2368 if ( m_colWidths
.IsEmpty() )
2373 if ( m_rowHeights
.IsEmpty() )
2378 switch ( msg
.GetId() )
2380 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2382 size_t pos
= msg
.GetCommandInt();
2383 int numRows
= msg
.GetCommandInt2();
2384 for ( i
= 0; i
< numRows
; i
++ )
2386 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2387 m_rowBottoms
.Insert( 0, pos
);
2389 m_numRows
+= numRows
;
2392 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2394 for ( i
= pos
; i
< m_numRows
; i
++ )
2396 bottom
+= m_rowHeights
[i
];
2397 m_rowBottoms
[i
] = bottom
;
2403 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2405 int numRows
= msg
.GetCommandInt();
2406 for ( i
= 0; i
< numRows
; i
++ )
2408 m_rowHeights
.Add( m_defaultRowHeight
);
2409 m_rowBottoms
.Add( 0 );
2412 int oldNumRows
= m_numRows
;
2413 m_numRows
+= numRows
;
2416 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2418 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2420 bottom
+= m_rowHeights
[i
];
2421 m_rowBottoms
[i
] = bottom
;
2427 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2429 size_t pos
= msg
.GetCommandInt();
2430 int numRows
= msg
.GetCommandInt2();
2431 for ( i
= 0; i
< numRows
; i
++ )
2433 m_rowHeights
.Remove( pos
);
2434 m_rowBottoms
.Remove( pos
);
2436 m_numRows
-= numRows
;
2441 m_colWidths
.Clear();
2442 m_colRights
.Clear();
2443 m_currentCellCoords
= wxGridNoCellCoords
;
2447 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2448 m_currentCellCoords
.Set( 0, 0 );
2451 for ( i
= 0; i
< m_numRows
; i
++ )
2453 h
+= m_rowHeights
[i
];
2454 m_rowBottoms
[i
] = h
;
2462 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2464 size_t pos
= msg
.GetCommandInt();
2465 int numCols
= msg
.GetCommandInt2();
2466 for ( i
= 0; i
< numCols
; i
++ )
2468 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2469 m_colRights
.Insert( 0, pos
);
2471 m_numCols
+= numCols
;
2474 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2476 for ( i
= pos
; i
< m_numCols
; i
++ )
2478 right
+= m_colWidths
[i
];
2479 m_colRights
[i
] = right
;
2485 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2487 int numCols
= msg
.GetCommandInt();
2488 for ( i
= 0; i
< numCols
; i
++ )
2490 m_colWidths
.Add( m_defaultColWidth
);
2491 m_colRights
.Add( 0 );
2494 int oldNumCols
= m_numCols
;
2495 m_numCols
+= numCols
;
2498 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2500 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2502 right
+= m_colWidths
[i
];
2503 m_colRights
[i
] = right
;
2509 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2511 size_t pos
= msg
.GetCommandInt();
2512 int numCols
= msg
.GetCommandInt2();
2513 for ( i
= 0; i
< numCols
; i
++ )
2515 m_colWidths
.Remove( pos
);
2516 m_colRights
.Remove( pos
);
2518 m_numCols
-= numCols
;
2522 #if 0 // leave the row alone here so that AppendCols will work subsequently
2524 m_rowHeights
.Clear();
2525 m_rowBottoms
.Clear();
2527 m_currentCellCoords
= wxGridNoCellCoords
;
2531 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2532 m_currentCellCoords
.Set( 0, 0 );
2535 for ( i
= 0; i
< m_numCols
; i
++ )
2537 w
+= m_colWidths
[i
];
2550 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2552 wxRegionIterator
iter( reg
);
2555 m_rowLabelsExposed
.Empty();
2562 // TODO: remove this when we can...
2563 // There is a bug in wxMotif that gives garbage update
2564 // rectangles if you jump-scroll a long way by clicking the
2565 // scrollbar with middle button. This is a work-around
2567 #if defined(__WXMOTIF__)
2569 m_gridWin
->GetClientSize( &cw
, &ch
);
2570 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2571 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2574 // logical bounds of update region
2577 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2578 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2580 // find the row labels within these bounds
2583 for ( row
= 0; row
< m_numRows
; row
++ )
2585 if ( GetRowBottom(row
) < top
)
2588 if ( GetRowTop(row
) > bottom
)
2591 m_rowLabelsExposed
.Add( row
);
2599 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2601 wxRegionIterator
iter( reg
);
2604 m_colLabelsExposed
.Empty();
2611 // TODO: remove this when we can...
2612 // There is a bug in wxMotif that gives garbage update
2613 // rectangles if you jump-scroll a long way by clicking the
2614 // scrollbar with middle button. This is a work-around
2616 #if defined(__WXMOTIF__)
2618 m_gridWin
->GetClientSize( &cw
, &ch
);
2619 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2620 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2623 // logical bounds of update region
2626 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2627 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2629 // find the cells within these bounds
2632 for ( col
= 0; col
< m_numCols
; col
++ )
2634 if ( GetColRight(col
) < left
)
2637 if ( GetColLeft(col
) > right
)
2640 m_colLabelsExposed
.Add( col
);
2648 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2650 wxRegionIterator
iter( reg
);
2653 m_cellsExposed
.Empty();
2654 m_rowsExposed
.Empty();
2655 m_colsExposed
.Empty();
2657 int left
, top
, right
, bottom
;
2662 // TODO: remove this when we can...
2663 // There is a bug in wxMotif that gives garbage update
2664 // rectangles if you jump-scroll a long way by clicking the
2665 // scrollbar with middle button. This is a work-around
2667 #if defined(__WXMOTIF__)
2669 m_gridWin
->GetClientSize( &cw
, &ch
);
2670 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2671 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2672 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2673 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2676 // logical bounds of update region
2678 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2679 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2681 // find the cells within these bounds
2684 for ( row
= 0; row
< m_numRows
; row
++ )
2686 if ( GetRowBottom(row
) <= top
)
2689 if ( GetRowTop(row
) > bottom
)
2692 m_rowsExposed
.Add( row
);
2694 for ( col
= 0; col
< m_numCols
; col
++ )
2696 if ( GetColRight(col
) <= left
)
2699 if ( GetColLeft(col
) > right
)
2702 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
2703 m_colsExposed
.Add( col
);
2704 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2713 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2716 wxPoint
pos( event
.GetPosition() );
2717 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2719 if ( event
.Dragging() )
2721 m_isDragging
= TRUE
;
2723 if ( event
.LeftIsDown() )
2725 switch( m_cursorMode
)
2727 case WXGRID_CURSOR_RESIZE_ROW
:
2729 int cw
, ch
, left
, dummy
;
2730 m_gridWin
->GetClientSize( &cw
, &ch
);
2731 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2733 wxClientDC
dc( m_gridWin
);
2735 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
2736 dc
.SetLogicalFunction(wxINVERT
);
2737 if ( m_dragLastPos
>= 0 )
2739 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2741 dc
.DrawLine( left
, y
, left
+cw
, y
);
2746 case WXGRID_CURSOR_SELECT_ROW
:
2747 if ( (row
= YToRow( y
)) >= 0 &&
2748 !IsInSelection( row
, 0 ) )
2750 SelectRow( row
, TRUE
);
2753 // default label to suppress warnings about "enumeration value
2754 // 'xxx' not handled in switch
2762 m_isDragging
= FALSE
;
2765 // ------------ Entering or leaving the window
2767 if ( event
.Entering() || event
.Leaving() )
2769 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2773 // ------------ Left button pressed
2775 else if ( event
.LeftDown() )
2777 // don't send a label click event for a hit on the
2778 // edge of the row label - this is probably the user
2779 // wanting to resize the row
2781 if ( YToEdgeOfRow(y
) < 0 )
2785 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2787 SelectRow( row
, event
.ShiftDown() );
2788 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2793 // starting to drag-resize a row
2795 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2800 // ------------ Left double click
2802 else if (event
.LeftDClick() )
2804 if ( YToEdgeOfRow(y
) < 0 )
2807 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2812 // ------------ Left button released
2814 else if ( event
.LeftUp() )
2816 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2818 DoEndDragResizeRow();
2820 // Note: we are ending the event *after* doing
2821 // default processing in this case
2823 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2826 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2831 // ------------ Right button down
2833 else if ( event
.RightDown() )
2836 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2838 // no default action at the moment
2843 // ------------ Right double click
2845 else if ( event
.RightDClick() )
2848 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2850 // no default action at the moment
2855 // ------------ No buttons down and mouse moving
2857 else if ( event
.Moving() )
2859 m_dragRowOrCol
= YToEdgeOfRow( y
);
2860 if ( m_dragRowOrCol
>= 0 )
2862 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2864 // don't capture the mouse yet
2865 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2868 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2870 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2876 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2879 wxPoint
pos( event
.GetPosition() );
2880 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2882 if ( event
.Dragging() )
2884 m_isDragging
= TRUE
;
2886 if ( event
.LeftIsDown() )
2888 switch( m_cursorMode
)
2890 case WXGRID_CURSOR_RESIZE_COL
:
2892 int cw
, ch
, dummy
, top
;
2893 m_gridWin
->GetClientSize( &cw
, &ch
);
2894 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2896 wxClientDC
dc( m_gridWin
);
2899 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
2900 GetColMinimalWidth(m_dragRowOrCol
));
2901 dc
.SetLogicalFunction(wxINVERT
);
2902 if ( m_dragLastPos
>= 0 )
2904 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2906 dc
.DrawLine( x
, top
, x
, top
+ch
);
2911 case WXGRID_CURSOR_SELECT_COL
:
2912 if ( (col
= XToCol( x
)) >= 0 &&
2913 !IsInSelection( 0, col
) )
2915 SelectCol( col
, TRUE
);
2918 // default label to suppress warnings about "enumeration value
2919 // 'xxx' not handled in switch
2927 m_isDragging
= FALSE
;
2930 // ------------ Entering or leaving the window
2932 if ( event
.Entering() || event
.Leaving() )
2934 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2938 // ------------ Left button pressed
2940 else if ( event
.LeftDown() )
2942 // don't send a label click event for a hit on the
2943 // edge of the col label - this is probably the user
2944 // wanting to resize the col
2946 if ( XToEdgeOfCol(x
) < 0 )
2950 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2952 SelectCol( col
, event
.ShiftDown() );
2953 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2958 // starting to drag-resize a col
2960 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2965 // ------------ Left double click
2967 if ( event
.LeftDClick() )
2969 if ( XToEdgeOfCol(x
) < 0 )
2972 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2977 // ------------ Left button released
2979 else if ( event
.LeftUp() )
2981 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2983 DoEndDragResizeCol();
2985 // Note: we are ending the event *after* doing
2986 // default processing in this case
2988 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2991 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2996 // ------------ Right button down
2998 else if ( event
.RightDown() )
3001 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3003 // no default action at the moment
3008 // ------------ Right double click
3010 else if ( event
.RightDClick() )
3013 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3015 // no default action at the moment
3020 // ------------ No buttons down and mouse moving
3022 else if ( event
.Moving() )
3024 m_dragRowOrCol
= XToEdgeOfCol( x
);
3025 if ( m_dragRowOrCol
>= 0 )
3027 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3029 // don't capture the cursor yet
3030 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3033 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3035 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3041 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3043 if ( event
.LeftDown() )
3045 // indicate corner label by having both row and
3048 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3054 else if ( event
.LeftDClick() )
3056 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3059 else if ( event
.RightDown() )
3061 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3063 // no default action at the moment
3067 else if ( event
.RightDClick() )
3069 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3071 // no default action at the moment
3076 void wxGrid::ChangeCursorMode(CursorMode mode
,
3081 static const wxChar
*cursorModes
[] =
3090 wxLogTrace(_T("grid"),
3091 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3092 win
== m_colLabelWin
? _T("colLabelWin")
3093 : win
? _T("rowLabelWin")
3095 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3096 #endif // __WXDEBUG__
3098 if ( mode
== m_cursorMode
)
3103 // by default use the grid itself
3109 m_winCapture
->ReleaseMouse();
3110 m_winCapture
= (wxWindow
*)NULL
;
3113 m_cursorMode
= mode
;
3115 switch ( m_cursorMode
)
3117 case WXGRID_CURSOR_RESIZE_ROW
:
3118 win
->SetCursor( m_rowResizeCursor
);
3121 case WXGRID_CURSOR_RESIZE_COL
:
3122 win
->SetCursor( m_colResizeCursor
);
3126 win
->SetCursor( *wxSTANDARD_CURSOR
);
3129 // we need to capture mouse when resizing
3130 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3131 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3133 if ( captureMouse
&& resize
)
3135 win
->CaptureMouse();
3140 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3143 wxPoint
pos( event
.GetPosition() );
3144 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3146 wxGridCellCoords coords
;
3147 XYToCell( x
, y
, coords
);
3149 if ( event
.Dragging() )
3151 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3153 // Don't start doing anything until the mouse has been drug at
3154 // least 3 pixels in any direction...
3157 if (m_startDragPos
== wxDefaultPosition
)
3159 m_startDragPos
= pos
;
3162 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3166 m_isDragging
= TRUE
;
3167 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3169 // Hide the edit control, so it
3170 // won't interfer with drag-shrinking.
3171 if ( IsCellEditControlEnabled() )
3172 HideCellEditControl();
3174 // Have we captured the mouse yet?
3177 m_winCapture
= m_gridWin
;
3178 m_winCapture
->CaptureMouse();
3181 if ( coords
!= wxGridNoCellCoords
)
3183 if ( !IsSelection() )
3185 SelectBlock( coords
, coords
);
3189 SelectBlock( m_currentCellCoords
, coords
);
3192 if (! IsVisible(coords
))
3194 MakeCellVisible(coords
);
3195 // TODO: need to introduce a delay or something here. The
3196 // scrolling is way to fast, at least on MSW.
3200 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3202 int cw
, ch
, left
, dummy
;
3203 m_gridWin
->GetClientSize( &cw
, &ch
);
3204 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3206 wxClientDC
dc( m_gridWin
);
3208 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3209 dc
.SetLogicalFunction(wxINVERT
);
3210 if ( m_dragLastPos
>= 0 )
3212 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3214 dc
.DrawLine( left
, y
, left
+cw
, y
);
3217 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3219 int cw
, ch
, dummy
, top
;
3220 m_gridWin
->GetClientSize( &cw
, &ch
);
3221 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3223 wxClientDC
dc( m_gridWin
);
3225 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3226 GetColMinimalWidth(m_dragRowOrCol
) );
3227 dc
.SetLogicalFunction(wxINVERT
);
3228 if ( m_dragLastPos
>= 0 )
3230 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3232 dc
.DrawLine( x
, top
, x
, top
+ch
);
3239 m_isDragging
= FALSE
;
3240 m_startDragPos
= wxDefaultPosition
;
3243 if ( coords
!= wxGridNoCellCoords
)
3245 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3246 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3249 if ( event
.Entering() || event
.Leaving() )
3251 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3252 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3257 // ------------ Left button pressed
3259 if ( event
.LeftDown() )
3261 DisableCellEditControl();
3262 if ( event
.ShiftDown() )
3264 SelectBlock( m_currentCellCoords
, coords
);
3266 else if ( XToEdgeOfCol(x
) < 0 &&
3267 YToEdgeOfRow(y
) < 0 )
3269 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3274 MakeCellVisible( coords
);
3276 // if this is the second click on this cell then start
3278 if ( m_waitForSlowClick
&&
3279 (coords
== m_currentCellCoords
) &&
3280 CanEnableCellControl())
3282 EnableCellEditControl();
3284 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3285 attr
->GetEditor()->StartingClick();
3288 m_waitForSlowClick
= FALSE
;
3292 SetCurrentCell( coords
);
3293 m_waitForSlowClick
= TRUE
;
3300 // ------------ Left double click
3302 else if ( event
.LeftDClick() )
3304 DisableCellEditControl();
3305 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3307 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3315 // ------------ Left button released
3317 else if ( event
.LeftUp() )
3319 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3321 if ( IsSelection() )
3325 m_winCapture
->ReleaseMouse();
3326 m_winCapture
= NULL
;
3328 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3331 // Show the edit control, if it has been hidden for
3333 ShowCellEditControl();
3335 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3337 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3338 DoEndDragResizeRow();
3340 // Note: we are ending the event *after* doing
3341 // default processing in this case
3343 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3345 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3347 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3348 DoEndDragResizeCol();
3350 // Note: we are ending the event *after* doing
3351 // default processing in this case
3353 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3360 // ------------ Right button down
3362 else if ( event
.RightDown() )
3364 DisableCellEditControl();
3365 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3370 // no default action at the moment
3375 // ------------ Right double click
3377 else if ( event
.RightDClick() )
3379 DisableCellEditControl();
3380 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3385 // no default action at the moment
3389 // ------------ Moving and no button action
3391 else if ( event
.Moving() && !event
.IsButton() )
3393 int dragRow
= YToEdgeOfRow( y
);
3394 int dragCol
= XToEdgeOfCol( x
);
3396 // Dragging on the corner of a cell to resize in both
3397 // directions is not implemented yet...
3399 if ( dragRow
>= 0 && dragCol
>= 0 )
3401 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3407 m_dragRowOrCol
= dragRow
;
3409 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3411 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3419 m_dragRowOrCol
= dragCol
;
3421 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3423 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3429 // Neither on a row or col edge
3431 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3433 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3440 void wxGrid::DoEndDragResizeRow()
3442 if ( m_dragLastPos
>= 0 )
3444 // erase the last line and resize the row
3446 int cw
, ch
, left
, dummy
;
3447 m_gridWin
->GetClientSize( &cw
, &ch
);
3448 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3450 wxClientDC
dc( m_gridWin
);
3452 dc
.SetLogicalFunction( wxINVERT
);
3453 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3454 HideCellEditControl();
3456 int rowTop
= GetRowTop(m_dragRowOrCol
);
3457 SetRowSize( m_dragRowOrCol
,
3458 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3460 if ( !GetBatchCount() )
3462 // Only needed to get the correct rect.y:
3463 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3465 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3466 rect
.width
= m_rowLabelWidth
;
3467 rect
.height
= ch
- rect
.y
;
3468 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3470 m_gridWin
->Refresh( FALSE
, &rect
);
3473 ShowCellEditControl();
3478 void wxGrid::DoEndDragResizeCol()
3480 if ( m_dragLastPos
>= 0 )
3482 // erase the last line and resize the col
3484 int cw
, ch
, dummy
, top
;
3485 m_gridWin
->GetClientSize( &cw
, &ch
);
3486 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3488 wxClientDC
dc( m_gridWin
);
3490 dc
.SetLogicalFunction( wxINVERT
);
3491 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3492 HideCellEditControl();
3494 int colLeft
= GetColLeft(m_dragRowOrCol
);
3495 SetColSize( m_dragRowOrCol
,
3496 wxMax( m_dragLastPos
- colLeft
,
3497 GetColMinimalWidth(m_dragRowOrCol
) ) );
3499 if ( !GetBatchCount() )
3501 // Only needed to get the correct rect.x:
3502 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3504 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3505 rect
.width
= cw
- rect
.x
;
3506 rect
.height
= m_colLabelHeight
;
3507 m_colLabelWin
->Refresh( TRUE
, &rect
);
3509 m_gridWin
->Refresh( FALSE
, &rect
);
3512 ShowCellEditControl();
3519 // ------ interaction with data model
3521 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3523 switch ( msg
.GetId() )
3525 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3526 return GetModelValues();
3528 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3529 return SetModelValues();
3531 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3532 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3533 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3534 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3535 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3536 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3537 return Redimension( msg
);
3546 // The behaviour of this function depends on the grid table class
3547 // Clear() function. For the default wxGridStringTable class the
3548 // behavious is to replace all cell contents with wxEmptyString but
3549 // not to change the number of rows or cols.
3551 void wxGrid::ClearGrid()
3556 SetEditControlValue();
3557 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3562 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3564 // TODO: something with updateLabels flag
3568 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3574 if (IsCellEditControlEnabled())
3575 DisableCellEditControl();
3577 bool ok
= m_table
->InsertRows( pos
, numRows
);
3579 // the table will have sent the results of the insert row
3580 // operation to this view object as a grid table message
3584 if ( m_numCols
== 0 )
3586 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3588 // TODO: perhaps instead of appending the default number of cols
3589 // we should remember what the last non-zero number of cols was ?
3593 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3595 // if we have just inserted cols into an empty grid the current
3596 // cell will be undefined...
3598 SetCurrentCell( 0, 0 );
3602 if ( !GetBatchCount() ) Refresh();
3605 SetEditControlValue();
3615 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3617 // TODO: something with updateLabels flag
3621 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3625 if ( m_table
&& m_table
->AppendRows( numRows
) )
3627 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3629 // if we have just inserted cols into an empty grid the current
3630 // cell will be undefined...
3632 SetCurrentCell( 0, 0 );
3635 // the table will have sent the results of the append row
3636 // operation to this view object as a grid table message
3639 if ( !GetBatchCount() ) Refresh();
3649 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3651 // TODO: something with updateLabels flag
3655 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3661 if (IsCellEditControlEnabled())
3662 DisableCellEditControl();
3664 if (m_table
->DeleteRows( pos
, numRows
))
3667 // the table will have sent the results of the delete row
3668 // operation to this view object as a grid table message
3671 if ( !GetBatchCount() ) Refresh();
3679 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3681 // TODO: something with updateLabels flag
3685 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3691 if (IsCellEditControlEnabled())
3692 DisableCellEditControl();
3694 bool ok
= m_table
->InsertCols( pos
, numCols
);
3696 // the table will have sent the results of the insert col
3697 // operation to this view object as a grid table message
3701 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3703 // if we have just inserted cols into an empty grid the current
3704 // cell will be undefined...
3706 SetCurrentCell( 0, 0 );
3710 if ( !GetBatchCount() ) Refresh();
3713 SetEditControlValue();
3723 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3725 // TODO: something with updateLabels flag
3729 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3733 if ( m_table
&& m_table
->AppendCols( numCols
) )
3735 // the table will have sent the results of the append col
3736 // operation to this view object as a grid table message
3738 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3740 // if we have just inserted cols into an empty grid the current
3741 // cell will be undefined...
3743 SetCurrentCell( 0, 0 );
3747 if ( !GetBatchCount() ) Refresh();
3757 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3759 // TODO: something with updateLabels flag
3763 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3769 if (IsCellEditControlEnabled())
3770 DisableCellEditControl();
3772 if ( m_table
->DeleteCols( pos
, numCols
) )
3774 // the table will have sent the results of the delete col
3775 // operation to this view object as a grid table message
3778 if ( !GetBatchCount() ) Refresh();
3788 // ----- event handlers
3791 // Generate a grid event based on a mouse event and
3792 // return the result of ProcessEvent()
3794 bool wxGrid::SendEvent( const wxEventType type
,
3796 wxMouseEvent
& mouseEv
)
3798 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3800 int rowOrCol
= (row
== -1 ? col
: row
);
3802 wxGridSizeEvent
gridEvt( GetId(),
3806 mouseEv
.GetX(), mouseEv
.GetY(),
3807 mouseEv
.ControlDown(),
3808 mouseEv
.ShiftDown(),
3810 mouseEv
.MetaDown() );
3812 return GetEventHandler()->ProcessEvent(gridEvt
);
3814 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3816 wxGridRangeSelectEvent
gridEvt( GetId(),
3820 m_selectedBottomRight
,
3821 mouseEv
.ControlDown(),
3822 mouseEv
.ShiftDown(),
3824 mouseEv
.MetaDown() );
3826 return GetEventHandler()->ProcessEvent(gridEvt
);
3830 wxGridEvent
gridEvt( GetId(),
3834 mouseEv
.GetX(), mouseEv
.GetY(),
3835 mouseEv
.ControlDown(),
3836 mouseEv
.ShiftDown(),
3838 mouseEv
.MetaDown() );
3840 return GetEventHandler()->ProcessEvent(gridEvt
);
3845 // Generate a grid event of specified type and return the result
3846 // of ProcessEvent().
3848 bool wxGrid::SendEvent( const wxEventType type
,
3851 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3853 int rowOrCol
= (row
== -1 ? col
: row
);
3855 wxGridSizeEvent
gridEvt( GetId(),
3860 return GetEventHandler()->ProcessEvent(gridEvt
);
3864 wxGridEvent
gridEvt( GetId(),
3869 return GetEventHandler()->ProcessEvent(gridEvt
);
3874 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3876 wxPaintDC
dc( this );
3878 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3879 m_numRows
&& m_numCols
)
3881 m_currentCellCoords
.Set(0, 0);
3882 SetEditControlValue();
3883 ShowCellEditControl();
3890 // This is just here to make sure that CalcDimensions gets called when
3891 // the grid view is resized... then the size event is skipped to allow
3892 // the box sizers to handle everything
3894 void wxGrid::OnSize( wxSizeEvent
& event
)
3901 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3903 if ( m_inOnKeyDown
)
3905 // shouldn't be here - we are going round in circles...
3907 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3910 m_inOnKeyDown
= TRUE
;
3912 // propagate the event up and see if it gets processed
3914 wxWindow
*parent
= GetParent();
3915 wxKeyEvent
keyEvt( event
);
3916 keyEvt
.SetEventObject( parent
);
3918 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3921 // TODO: Should also support Shift-cursor keys for
3922 // extending the selection. Maybe add a flag to
3923 // MoveCursorXXX() and MoveCursorXXXBlock() and
3924 // just send event.ShiftDown().
3926 // try local handlers
3928 switch ( event
.KeyCode() )
3931 if ( event
.ControlDown() )
3933 MoveCursorUpBlock();
3942 if ( event
.ControlDown() )
3944 MoveCursorDownBlock();
3953 if ( event
.ControlDown() )
3955 MoveCursorLeftBlock();
3964 if ( event
.ControlDown() )
3966 MoveCursorRightBlock();
3975 if ( event
.ControlDown() )
3977 event
.Skip(); // to let the edit control have the return
3986 if (event
.ShiftDown())
3993 if ( event
.ControlDown() )
3995 MakeCellVisible( 0, 0 );
3996 SetCurrentCell( 0, 0 );
4005 if ( event
.ControlDown() )
4007 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4008 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4024 // We don't want these keys to trigger the edit control, any others?
4033 if ( !IsEditable() )
4038 // Otherwise fall through to default
4041 // now try the cell edit control
4043 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4045 EnableCellEditControl();
4046 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4047 attr
->GetEditor()->StartingKey(event
);
4052 // let others process char events for readonly cells
4059 m_inOnKeyDown
= FALSE
;
4063 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4067 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4069 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4071 // the event has been intercepted - do nothing
4076 m_currentCellCoords
!= wxGridNoCellCoords
)
4078 HideCellEditControl();
4079 SaveEditControlValue();
4080 DisableCellEditControl();
4082 // Clear the old current cell highlight
4083 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4085 // Otherwise refresh redraws the highlight!
4086 m_currentCellCoords
= coords
;
4088 m_gridWin
->Refresh( FALSE
, &r
);
4091 m_currentCellCoords
= coords
;
4093 SetEditControlValue();
4097 wxClientDC
dc(m_gridWin
);
4100 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4101 DrawCellHighlight(dc
, attr
);
4104 if ( IsSelection() )
4106 wxRect
r( SelectionToDeviceRect() );
4108 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4115 // ------ functions to get/send data (see also public functions)
4118 bool wxGrid::GetModelValues()
4122 // all we need to do is repaint the grid
4124 m_gridWin
->Refresh();
4132 bool wxGrid::SetModelValues()
4138 for ( row
= 0; row
< m_numRows
; row
++ )
4140 for ( col
= 0; col
< m_numCols
; col
++ )
4142 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4154 // Note - this function only draws cells that are in the list of
4155 // exposed cells (usually set from the update region by
4156 // CalcExposedCells)
4158 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4160 if ( !m_numRows
|| !m_numCols
) return;
4163 size_t numCells
= m_cellsExposed
.GetCount();
4165 for ( i
= 0; i
< numCells
; i
++ )
4167 DrawCell( dc
, m_cellsExposed
[i
] );
4172 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4174 int row
= coords
.GetRow();
4175 int col
= coords
.GetCol();
4177 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4180 // we draw the cell border ourselves
4181 #if !WXGRID_DRAW_LINES
4182 if ( m_gridLinesEnabled
)
4183 DrawCellBorder( dc
, coords
);
4186 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4188 bool isCurrent
= coords
== m_currentCellCoords
;
4191 rect
.x
= GetColLeft(col
);
4192 rect
.y
= GetRowTop(row
);
4193 rect
.width
= GetColWidth(col
) - 1;
4194 rect
.height
= GetRowHeight(row
) - 1;
4196 // if the editor is shown, we should use it and not the renderer
4197 if ( isCurrent
&& IsCellEditControlEnabled() )
4199 attr
->GetEditor()->PaintBackground(rect
, attr
);
4203 // but all the rest is drawn by the cell renderer and hence may be
4205 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4211 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4213 int row
= m_currentCellCoords
.GetRow();
4214 int col
= m_currentCellCoords
.GetCol();
4216 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4220 rect
.x
= GetColLeft(col
);
4221 rect
.y
= GetRowTop(row
);
4222 rect
.width
= GetColWidth(col
) - 1;
4223 rect
.height
= GetRowHeight(row
) - 1;
4225 // hmmm... what could we do here to show that the cell is disabled?
4226 // for now, I just draw a thinner border than for the other ones, but
4227 // it doesn't look really good
4228 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4229 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4231 dc
.DrawRectangle(rect
);
4234 // VZ: my experiments with 3d borders...
4236 // how to properly set colours for arbitrary bg?
4237 wxCoord x1
= rect
.x
,
4239 x2
= rect
.x
+ rect
.width
-1,
4240 y2
= rect
.y
+ rect
.height
-1;
4242 dc
.SetPen(*wxWHITE_PEN
);
4243 dc
.DrawLine(x1
, y1
, x2
, y1
);
4244 dc
.DrawLine(x1
, y1
, x1
, y2
);
4246 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4247 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4249 dc
.SetPen(*wxBLACK_PEN
);
4250 dc
.DrawLine(x1
, y2
, x2
, y2
);
4251 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4255 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4257 int row
= coords
.GetRow();
4258 int col
= coords
.GetCol();
4259 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4262 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4264 // right hand border
4266 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
4267 GetColRight(col
), GetRowBottom(row
) );
4271 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
4272 GetColRight(col
), GetRowBottom(row
) );
4275 void wxGrid::DrawHighlight(wxDC
& dc
)
4277 // if the active cell was repainted, repaint its highlight too because it
4278 // might have been damaged by the grid lines
4279 size_t count
= m_cellsExposed
.GetCount();
4280 for ( size_t n
= 0; n
< count
; n
++ )
4282 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4284 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4285 DrawCellHighlight(dc
, attr
);
4293 // TODO: remove this ???
4294 // This is used to redraw all grid lines e.g. when the grid line colour
4297 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4299 if ( !m_gridLinesEnabled
||
4301 !m_numCols
) return;
4303 int top
, bottom
, left
, right
;
4308 m_gridWin
->GetClientSize(&cw
, &ch
);
4310 // virtual coords of visible area
4312 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4313 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4318 reg
.GetBox(x
, y
, w
, h
);
4319 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4320 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4323 // avoid drawing grid lines past the last row and col
4325 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
4326 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
4328 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4330 // horizontal grid lines
4333 for ( i
= 0; i
< m_numRows
; i
++ )
4335 int bot
= GetRowBottom(i
) - 1;
4344 dc
.DrawLine( left
, bot
, right
, bot
);
4349 // vertical grid lines
4351 for ( i
= 0; i
< m_numCols
; i
++ )
4353 int colRight
= GetColRight(i
) - 1;
4354 if ( colRight
> right
)
4359 if ( colRight
>= left
)
4361 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
4367 void wxGrid::DrawRowLabels( wxDC
& dc
)
4369 if ( !m_numRows
|| !m_numCols
) return;
4372 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4374 for ( i
= 0; i
< numLabels
; i
++ )
4376 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4381 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4383 if ( GetRowHeight(row
) <= 0 )
4386 int rowTop
= GetRowTop(row
),
4387 rowBottom
= GetRowBottom(row
) - 1;
4389 dc
.SetPen( *wxBLACK_PEN
);
4390 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4391 m_rowLabelWidth
-1, rowBottom
);
4393 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
4395 dc
.SetPen( *wxWHITE_PEN
);
4396 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
4397 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4399 dc
.SetBackgroundMode( wxTRANSPARENT
);
4400 dc
.SetTextForeground( GetLabelTextColour() );
4401 dc
.SetFont( GetLabelFont() );
4404 GetRowLabelAlignment( &hAlign
, &vAlign
);
4408 rect
.SetY( GetRowTop(row
) + 2 );
4409 rect
.SetWidth( m_rowLabelWidth
- 4 );
4410 rect
.SetHeight( GetRowHeight(row
) - 4 );
4411 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4415 void wxGrid::DrawColLabels( wxDC
& dc
)
4417 if ( !m_numRows
|| !m_numCols
) return;
4420 size_t numLabels
= m_colLabelsExposed
.GetCount();
4422 for ( i
= 0; i
< numLabels
; i
++ )
4424 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4429 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4431 if ( GetColWidth(col
) <= 0 )
4434 int colLeft
= GetColLeft(col
),
4435 colRight
= GetColRight(col
) - 1;
4437 dc
.SetPen( *wxBLACK_PEN
);
4438 dc
.DrawLine( colRight
, 0,
4439 colRight
, m_colLabelHeight
-1 );
4441 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4442 colRight
, m_colLabelHeight
-1 );
4444 dc
.SetPen( *wxWHITE_PEN
);
4445 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4446 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
4448 dc
.SetBackgroundMode( wxTRANSPARENT
);
4449 dc
.SetTextForeground( GetLabelTextColour() );
4450 dc
.SetFont( GetLabelFont() );
4452 dc
.SetBackgroundMode( wxTRANSPARENT
);
4453 dc
.SetTextForeground( GetLabelTextColour() );
4454 dc
.SetFont( GetLabelFont() );
4457 GetColLabelAlignment( &hAlign
, &vAlign
);
4460 rect
.SetX( colLeft
+ 2 );
4462 rect
.SetWidth( GetColWidth(col
) - 4 );
4463 rect
.SetHeight( m_colLabelHeight
- 4 );
4464 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4468 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4469 const wxString
& value
,
4474 long textWidth
, textHeight
;
4475 long lineWidth
, lineHeight
;
4476 wxArrayString lines
;
4478 dc
.SetClippingRegion( rect
);
4479 StringToLines( value
, lines
);
4480 if ( lines
.GetCount() )
4482 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4483 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4486 switch ( horizAlign
)
4489 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4493 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4502 switch ( vertAlign
)
4505 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4509 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4518 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4520 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4525 dc
.DestroyClippingRegion();
4529 // Split multi line text up into an array of strings. Any existing
4530 // contents of the string array are preserved.
4532 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4536 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4537 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4539 while ( startPos
< (int)tVal
.Length() )
4541 pos
= tVal
.Mid(startPos
).Find( eol
);
4546 else if ( pos
== 0 )
4548 lines
.Add( wxEmptyString
);
4552 lines
.Add( value
.Mid(startPos
, pos
) );
4556 if ( startPos
< (int)value
.Length() )
4558 lines
.Add( value
.Mid( startPos
) );
4563 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4564 wxArrayString
& lines
,
4565 long *width
, long *height
)
4572 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4574 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4575 w
= wxMax( w
, lineW
);
4585 // ------ Edit control functions
4589 void wxGrid::EnableEditing( bool edit
)
4591 // TODO: improve this ?
4593 if ( edit
!= m_editable
)
4597 // FIXME IMHO this won't disable the edit control if edit == FALSE
4598 // because of the check in the beginning of
4599 // EnableCellEditControl() just below (VZ)
4600 EnableCellEditControl(m_editable
);
4605 void wxGrid::EnableCellEditControl( bool enable
)
4610 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4611 SetCurrentCell( 0, 0 );
4613 if ( enable
!= m_cellEditCtrlEnabled
)
4615 // TODO allow the app to Veto() this event?
4616 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4620 // this should be checked by the caller!
4621 wxASSERT_MSG( CanEnableCellControl(),
4622 _T("can't enable editing for this cell!") );
4624 // do it before ShowCellEditControl()
4625 m_cellEditCtrlEnabled
= enable
;
4627 SetEditControlValue();
4628 ShowCellEditControl();
4632 HideCellEditControl();
4633 SaveEditControlValue();
4635 // do it after HideCellEditControl()
4636 m_cellEditCtrlEnabled
= enable
;
4641 bool wxGrid::IsCurrentCellReadOnly() const
4644 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4645 bool readonly
= attr
->IsReadOnly();
4651 bool wxGrid::CanEnableCellControl() const
4653 return m_editable
&& !IsCurrentCellReadOnly();
4656 bool wxGrid::IsCellEditControlEnabled() const
4658 // the cell edit control might be disable for all cells or just for the
4659 // current one if it's read only
4660 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
4663 wxWindow
*wxGrid::GetGridWindow() const
4668 void wxGrid::ShowCellEditControl()
4670 if ( IsCellEditControlEnabled() )
4672 if ( !IsVisible( m_currentCellCoords
) )
4678 wxRect rect
= CellToRect( m_currentCellCoords
);
4679 int row
= m_currentCellCoords
.GetRow();
4680 int col
= m_currentCellCoords
.GetCol();
4682 // convert to scrolled coords
4684 int left
, top
, right
, bottom
;
4685 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4686 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4688 // cell is shifted by one pixel
4694 // Make the edit control large enough to allow for internal
4697 // TODO: remove this if the text ctrl sizing is improved esp. for
4701 #if defined(__WXMOTIF__)
4702 if ( row
== 0 || col
== 0 )
4711 if ( row
== 0 || col
== 0 )
4721 #if defined(__WXGTK__)
4724 if (left
!= 0) left_diff
++;
4725 if (top
!= 0) top_diff
++;
4726 rect
.SetLeft( left
+ left_diff
);
4727 rect
.SetTop( top
+ top_diff
);
4728 rect
.SetRight( rect
.GetRight() - left_diff
);
4729 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4731 rect
.SetLeft( wxMax(0, left
- extra
) );
4732 rect
.SetTop( wxMax(0, top
- extra
) );
4733 rect
.SetRight( rect
.GetRight() + 2*extra
);
4734 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4737 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4738 wxGridCellEditor
* editor
= attr
->GetEditor();
4739 if ( !editor
->IsCreated() )
4741 editor
->Create(m_gridWin
, -1,
4742 new wxGridCellEditorEvtHandler(this, editor
));
4745 editor
->SetSize( rect
);
4746 editor
->Show( TRUE
, attr
);
4747 editor
->BeginEdit(row
, col
, this);
4754 void wxGrid::HideCellEditControl()
4756 if ( IsCellEditControlEnabled() )
4758 int row
= m_currentCellCoords
.GetRow();
4759 int col
= m_currentCellCoords
.GetCol();
4761 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4762 attr
->GetEditor()->Show( FALSE
);
4764 m_gridWin
->SetFocus();
4769 void wxGrid::SetEditControlValue( const wxString
& value
)
4771 // RD: The new Editors get the value from the table themselves now. This
4772 // method can probably be removed...
4776 void wxGrid::SaveEditControlValue()
4778 if ( IsCellEditControlEnabled() )
4780 int row
= m_currentCellCoords
.GetRow();
4781 int col
= m_currentCellCoords
.GetCol();
4783 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4784 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4790 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4791 m_currentCellCoords
.GetRow(),
4792 m_currentCellCoords
.GetCol() );
4799 // ------ Grid location functions
4800 // Note that all of these functions work with the logical coordinates of
4801 // grid cells and labels so you will need to convert from device
4802 // coordinates for mouse events etc.
4805 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4807 int row
= YToRow(y
);
4808 int col
= XToCol(x
);
4810 if ( row
== -1 || col
== -1 )
4812 coords
= wxGridNoCellCoords
;
4816 coords
.Set( row
, col
);
4821 int wxGrid::YToRow( int y
)
4825 for ( i
= 0; i
< m_numRows
; i
++ )
4827 if ( y
< GetRowBottom(i
) )
4831 return m_numRows
; //-1;
4835 int wxGrid::XToCol( int x
)
4839 for ( i
= 0; i
< m_numCols
; i
++ )
4841 if ( x
< GetColRight(i
) )
4845 return m_numCols
; //-1;
4849 // return the row number that that the y coord is near the edge of, or
4850 // -1 if not near an edge
4852 int wxGrid::YToEdgeOfRow( int y
)
4856 for ( i
= 0; i
< m_numRows
; i
++ )
4858 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
4860 d
= abs( y
- GetRowBottom(i
) );
4861 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
4870 // return the col number that that the x coord is near the edge of, or
4871 // -1 if not near an edge
4873 int wxGrid::XToEdgeOfCol( int x
)
4877 for ( i
= 0; i
< m_numCols
; i
++ )
4879 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
4881 d
= abs( x
- GetColRight(i
) );
4882 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
4891 wxRect
wxGrid::CellToRect( int row
, int col
)
4893 wxRect
rect( -1, -1, -1, -1 );
4895 if ( row
>= 0 && row
< m_numRows
&&
4896 col
>= 0 && col
< m_numCols
)
4898 rect
.x
= GetColLeft(col
);
4899 rect
.y
= GetRowTop(row
);
4900 rect
.width
= GetColWidth(col
);
4901 rect
.height
= GetRowHeight(row
);
4908 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4910 // get the cell rectangle in logical coords
4912 wxRect
r( CellToRect( row
, col
) );
4914 // convert to device coords
4916 int left
, top
, right
, bottom
;
4917 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4918 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4920 // check against the client area of the grid window
4923 m_gridWin
->GetClientSize( &cw
, &ch
);
4925 if ( wholeCellVisible
)
4927 // is the cell wholly visible ?
4929 return ( left
>= 0 && right
<= cw
&&
4930 top
>= 0 && bottom
<= ch
);
4934 // is the cell partly visible ?
4936 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4937 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4942 // make the specified cell location visible by doing a minimal amount
4945 void wxGrid::MakeCellVisible( int row
, int col
)
4948 int xpos
= -1, ypos
= -1;
4950 if ( row
>= 0 && row
< m_numRows
&&
4951 col
>= 0 && col
< m_numCols
)
4953 // get the cell rectangle in logical coords
4955 wxRect
r( CellToRect( row
, col
) );
4957 // convert to device coords
4959 int left
, top
, right
, bottom
;
4960 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4961 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4964 m_gridWin
->GetClientSize( &cw
, &ch
);
4970 else if ( bottom
> ch
)
4972 int h
= r
.GetHeight();
4974 for ( i
= row
-1; i
>= 0; i
-- )
4976 int rowHeight
= GetRowHeight(i
);
4977 if ( h
+ rowHeight
> ch
)
4984 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4985 // have rounding errors (this is important, because if we do, we
4986 // might not scroll at all and some cells won't be redrawn)
4987 ypos
+= GRID_SCROLL_LINE
/ 2;
4994 else if ( right
> cw
)
4996 int w
= r
.GetWidth();
4998 for ( i
= col
-1; i
>= 0; i
-- )
5000 int colWidth
= GetColWidth(i
);
5001 if ( w
+ colWidth
> cw
)
5008 // see comment for ypos above
5009 xpos
+= GRID_SCROLL_LINE
/ 2;
5012 if ( xpos
!= -1 || ypos
!= -1 )
5014 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5015 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5016 Scroll( xpos
, ypos
);
5024 // ------ Grid cursor movement functions
5027 bool wxGrid::MoveCursorUp()
5029 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5030 m_currentCellCoords
.GetRow() > 0 )
5032 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5033 m_currentCellCoords
.GetCol() );
5035 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5036 m_currentCellCoords
.GetCol() );
5045 bool wxGrid::MoveCursorDown()
5047 // TODO: allow for scrolling
5049 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5050 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5052 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5053 m_currentCellCoords
.GetCol() );
5055 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5056 m_currentCellCoords
.GetCol() );
5065 bool wxGrid::MoveCursorLeft()
5067 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5068 m_currentCellCoords
.GetCol() > 0 )
5070 MakeCellVisible( m_currentCellCoords
.GetRow(),
5071 m_currentCellCoords
.GetCol() - 1 );
5073 SetCurrentCell( m_currentCellCoords
.GetRow(),
5074 m_currentCellCoords
.GetCol() - 1 );
5083 bool wxGrid::MoveCursorRight()
5085 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5086 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5088 MakeCellVisible( m_currentCellCoords
.GetRow(),
5089 m_currentCellCoords
.GetCol() + 1 );
5091 SetCurrentCell( m_currentCellCoords
.GetRow(),
5092 m_currentCellCoords
.GetCol() + 1 );
5101 bool wxGrid::MovePageUp()
5103 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5105 int row
= m_currentCellCoords
.GetRow();
5109 m_gridWin
->GetClientSize( &cw
, &ch
);
5111 int y
= GetRowTop(row
);
5112 int newRow
= YToRow( y
- ch
+ 1 );
5117 else if ( newRow
== row
)
5122 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5123 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5131 bool wxGrid::MovePageDown()
5133 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5135 int row
= m_currentCellCoords
.GetRow();
5136 if ( row
< m_numRows
)
5139 m_gridWin
->GetClientSize( &cw
, &ch
);
5141 int y
= GetRowTop(row
);
5142 int newRow
= YToRow( y
+ ch
);
5145 newRow
= m_numRows
- 1;
5147 else if ( newRow
== row
)
5152 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5153 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5161 bool wxGrid::MoveCursorUpBlock()
5164 m_currentCellCoords
!= wxGridNoCellCoords
&&
5165 m_currentCellCoords
.GetRow() > 0 )
5167 int row
= m_currentCellCoords
.GetRow();
5168 int col
= m_currentCellCoords
.GetCol();
5170 if ( m_table
->IsEmptyCell(row
, col
) )
5172 // starting in an empty cell: find the next block of
5178 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5181 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5183 // starting at the top of a block: find the next block
5189 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5194 // starting within a block: find the top of the block
5199 if ( m_table
->IsEmptyCell(row
, col
) )
5207 MakeCellVisible( row
, col
);
5208 SetCurrentCell( row
, col
);
5216 bool wxGrid::MoveCursorDownBlock()
5219 m_currentCellCoords
!= wxGridNoCellCoords
&&
5220 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5222 int row
= m_currentCellCoords
.GetRow();
5223 int col
= m_currentCellCoords
.GetCol();
5225 if ( m_table
->IsEmptyCell(row
, col
) )
5227 // starting in an empty cell: find the next block of
5230 while ( row
< m_numRows
-1 )
5233 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5236 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5238 // starting at the bottom of a block: find the next block
5241 while ( row
< m_numRows
-1 )
5244 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5249 // starting within a block: find the bottom of the block
5251 while ( row
< m_numRows
-1 )
5254 if ( m_table
->IsEmptyCell(row
, col
) )
5262 MakeCellVisible( row
, col
);
5263 SetCurrentCell( row
, col
);
5271 bool wxGrid::MoveCursorLeftBlock()
5274 m_currentCellCoords
!= wxGridNoCellCoords
&&
5275 m_currentCellCoords
.GetCol() > 0 )
5277 int row
= m_currentCellCoords
.GetRow();
5278 int col
= m_currentCellCoords
.GetCol();
5280 if ( m_table
->IsEmptyCell(row
, col
) )
5282 // starting in an empty cell: find the next block of
5288 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5291 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5293 // starting at the left of a block: find the next block
5299 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5304 // starting within a block: find the left of the block
5309 if ( m_table
->IsEmptyCell(row
, col
) )
5317 MakeCellVisible( row
, col
);
5318 SetCurrentCell( row
, col
);
5326 bool wxGrid::MoveCursorRightBlock()
5329 m_currentCellCoords
!= wxGridNoCellCoords
&&
5330 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5332 int row
= m_currentCellCoords
.GetRow();
5333 int col
= m_currentCellCoords
.GetCol();
5335 if ( m_table
->IsEmptyCell(row
, col
) )
5337 // starting in an empty cell: find the next block of
5340 while ( col
< m_numCols
-1 )
5343 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5346 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5348 // starting at the right of a block: find the next block
5351 while ( col
< m_numCols
-1 )
5354 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5359 // starting within a block: find the right of the block
5361 while ( col
< m_numCols
-1 )
5364 if ( m_table
->IsEmptyCell(row
, col
) )
5372 MakeCellVisible( row
, col
);
5373 SetCurrentCell( row
, col
);
5384 // ------ Label values and formatting
5387 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5389 *horiz
= m_rowLabelHorizAlign
;
5390 *vert
= m_rowLabelVertAlign
;
5393 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5395 *horiz
= m_colLabelHorizAlign
;
5396 *vert
= m_colLabelVertAlign
;
5399 wxString
wxGrid::GetRowLabelValue( int row
)
5403 return m_table
->GetRowLabelValue( row
);
5413 wxString
wxGrid::GetColLabelValue( int col
)
5417 return m_table
->GetColLabelValue( col
);
5428 void wxGrid::SetRowLabelSize( int width
)
5430 width
= wxMax( width
, 0 );
5431 if ( width
!= m_rowLabelWidth
)
5435 m_rowLabelWin
->Show( FALSE
);
5436 m_cornerLabelWin
->Show( FALSE
);
5438 else if ( m_rowLabelWidth
== 0 )
5440 m_rowLabelWin
->Show( TRUE
);
5441 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5444 m_rowLabelWidth
= width
;
5451 void wxGrid::SetColLabelSize( int height
)
5453 height
= wxMax( height
, 0 );
5454 if ( height
!= m_colLabelHeight
)
5458 m_colLabelWin
->Show( FALSE
);
5459 m_cornerLabelWin
->Show( FALSE
);
5461 else if ( m_colLabelHeight
== 0 )
5463 m_colLabelWin
->Show( TRUE
);
5464 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5467 m_colLabelHeight
= height
;
5474 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5476 if ( m_labelBackgroundColour
!= colour
)
5478 m_labelBackgroundColour
= colour
;
5479 m_rowLabelWin
->SetBackgroundColour( colour
);
5480 m_colLabelWin
->SetBackgroundColour( colour
);
5481 m_cornerLabelWin
->SetBackgroundColour( colour
);
5483 if ( !GetBatchCount() )
5485 m_rowLabelWin
->Refresh();
5486 m_colLabelWin
->Refresh();
5487 m_cornerLabelWin
->Refresh();
5492 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5494 if ( m_labelTextColour
!= colour
)
5496 m_labelTextColour
= colour
;
5497 if ( !GetBatchCount() )
5499 m_rowLabelWin
->Refresh();
5500 m_colLabelWin
->Refresh();
5505 void wxGrid::SetLabelFont( const wxFont
& font
)
5508 if ( !GetBatchCount() )
5510 m_rowLabelWin
->Refresh();
5511 m_colLabelWin
->Refresh();
5515 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5517 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5519 m_rowLabelHorizAlign
= horiz
;
5522 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5524 m_rowLabelVertAlign
= vert
;
5527 if ( !GetBatchCount() )
5529 m_rowLabelWin
->Refresh();
5533 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5535 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5537 m_colLabelHorizAlign
= horiz
;
5540 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5542 m_colLabelVertAlign
= vert
;
5545 if ( !GetBatchCount() )
5547 m_colLabelWin
->Refresh();
5551 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5555 m_table
->SetRowLabelValue( row
, s
);
5556 if ( !GetBatchCount() )
5558 wxRect rect
= CellToRect( row
, 0);
5559 if ( rect
.height
> 0 )
5561 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5563 rect
.width
= m_rowLabelWidth
;
5564 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5570 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5574 m_table
->SetColLabelValue( col
, s
);
5575 if ( !GetBatchCount() )
5577 wxRect rect
= CellToRect( 0, col
);
5578 if ( rect
.width
> 0 )
5580 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5582 rect
.height
= m_colLabelHeight
;
5583 m_colLabelWin
->Refresh( TRUE
, &rect
);
5589 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5591 if ( m_gridLineColour
!= colour
)
5593 m_gridLineColour
= colour
;
5595 wxClientDC
dc( m_gridWin
);
5597 DrawAllGridLines( dc
, wxRegion() );
5601 void wxGrid::EnableGridLines( bool enable
)
5603 if ( enable
!= m_gridLinesEnabled
)
5605 m_gridLinesEnabled
= enable
;
5607 if ( !GetBatchCount() )
5611 wxClientDC
dc( m_gridWin
);
5613 DrawAllGridLines( dc
, wxRegion() );
5617 m_gridWin
->Refresh();
5624 int wxGrid::GetDefaultRowSize()
5626 return m_defaultRowHeight
;
5629 int wxGrid::GetRowSize( int row
)
5631 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5633 return GetRowHeight(row
);
5636 int wxGrid::GetDefaultColSize()
5638 return m_defaultColWidth
;
5641 int wxGrid::GetColSize( int col
)
5643 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5645 return GetColWidth(col
);
5648 // ============================================================================
5649 // access to the grid attributes: each of them has a default value in the grid
5650 // itself and may be overidden on a per-cell basis
5651 // ============================================================================
5653 // ----------------------------------------------------------------------------
5654 // setting default attributes
5655 // ----------------------------------------------------------------------------
5657 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5659 m_defaultCellAttr
->SetBackgroundColour(col
);
5661 m_gridWin
->SetBackgroundColour(col
);
5665 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5667 m_defaultCellAttr
->SetTextColour(col
);
5670 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5672 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5675 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5677 m_defaultCellAttr
->SetFont(font
);
5680 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5682 m_defaultCellAttr
->SetRenderer(renderer
);
5685 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5687 m_defaultCellAttr
->SetEditor(editor
);
5690 // ----------------------------------------------------------------------------
5691 // access to the default attrbiutes
5692 // ----------------------------------------------------------------------------
5694 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5696 return m_defaultCellAttr
->GetBackgroundColour();
5699 wxColour
wxGrid::GetDefaultCellTextColour()
5701 return m_defaultCellAttr
->GetTextColour();
5704 wxFont
wxGrid::GetDefaultCellFont()
5706 return m_defaultCellAttr
->GetFont();
5709 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5711 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5714 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5716 return m_defaultCellAttr
->GetRenderer();
5719 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5721 return m_defaultCellAttr
->GetEditor();
5724 // ----------------------------------------------------------------------------
5725 // access to cell attributes
5726 // ----------------------------------------------------------------------------
5728 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5730 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5731 wxColour colour
= attr
->GetBackgroundColour();
5736 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5738 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5739 wxColour colour
= attr
->GetTextColour();
5744 wxFont
wxGrid::GetCellFont( int row
, int col
)
5746 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5747 wxFont font
= attr
->GetFont();
5752 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5754 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5755 attr
->GetAlignment(horiz
, vert
);
5759 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5761 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5762 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5767 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5769 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5770 wxGridCellEditor
* editor
= attr
->GetEditor();
5775 bool wxGrid::IsReadOnly(int row
, int col
) const
5777 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5778 bool isReadOnly
= attr
->IsReadOnly();
5783 // ----------------------------------------------------------------------------
5784 // attribute support: cache, automatic provider creation, ...
5785 // ----------------------------------------------------------------------------
5787 bool wxGrid::CanHaveAttributes()
5794 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5795 // table is providing the attributes itself??? In which case
5796 // I don't think the grid should create a Provider object for the
5797 // table but the table should be smart enough to do that on its own.
5798 if ( !m_table
->GetAttrProvider() )
5800 // use the default attr provider by default
5801 // (another choice would be to just return FALSE thus forcing the user
5803 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5809 void wxGrid::ClearAttrCache()
5811 if ( m_attrCache
.row
!= -1 )
5813 m_attrCache
.attr
->SafeDecRef();
5814 m_attrCache
.row
= -1;
5818 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5820 wxGrid
*self
= (wxGrid
*)this; // const_cast
5822 self
->ClearAttrCache();
5823 self
->m_attrCache
.row
= row
;
5824 self
->m_attrCache
.col
= col
;
5825 self
->m_attrCache
.attr
= attr
;
5829 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5831 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5833 *attr
= m_attrCache
.attr
;
5834 (*attr
)->SafeIncRef();
5836 #ifdef DEBUG_ATTR_CACHE
5837 gs_nAttrCacheHits
++;
5844 #ifdef DEBUG_ATTR_CACHE
5845 gs_nAttrCacheMisses
++;
5851 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5853 wxGridCellAttr
*attr
;
5854 if ( !LookupAttr(row
, col
, &attr
) )
5856 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5857 CacheAttr(row
, col
, attr
);
5861 attr
->SetDefAttr(m_defaultCellAttr
);
5865 attr
= m_defaultCellAttr
;
5872 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5874 wxGridCellAttr
*attr
;
5875 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5877 wxASSERT_MSG( m_table
,
5878 _T("we may only be called if CanHaveAttributes() "
5879 "returned TRUE and then m_table should be !NULL") );
5881 attr
= m_table
->GetAttr(row
, col
);
5884 attr
= new wxGridCellAttr
;
5886 // artificially inc the ref count to match DecRef() in caller
5889 m_table
->SetAttr(attr
, row
, col
);
5892 CacheAttr(row
, col
, attr
);
5894 attr
->SetDefAttr(m_defaultCellAttr
);
5898 // ----------------------------------------------------------------------------
5899 // setting cell attributes: this is forwarded to the table
5900 // ----------------------------------------------------------------------------
5902 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5904 if ( CanHaveAttributes() )
5906 m_table
->SetRowAttr(attr
, row
);
5914 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5916 if ( CanHaveAttributes() )
5918 m_table
->SetColAttr(attr
, col
);
5926 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5928 if ( CanHaveAttributes() )
5930 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5931 attr
->SetBackgroundColour(colour
);
5936 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5938 if ( CanHaveAttributes() )
5940 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5941 attr
->SetTextColour(colour
);
5946 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5948 if ( CanHaveAttributes() )
5950 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5951 attr
->SetFont(font
);
5956 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5958 if ( CanHaveAttributes() )
5960 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5961 attr
->SetAlignment(horiz
, vert
);
5966 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5968 if ( CanHaveAttributes() )
5970 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5971 attr
->SetRenderer(renderer
);
5976 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5978 if ( CanHaveAttributes() )
5980 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5981 attr
->SetEditor(editor
);
5986 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5988 if ( CanHaveAttributes() )
5990 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5991 attr
->SetReadOnly(isReadOnly
);
5996 // ----------------------------------------------------------------------------
5998 // ----------------------------------------------------------------------------
6000 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6002 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6004 if ( resizeExistingRows
)
6012 void wxGrid::SetRowSize( int row
, int height
)
6014 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6016 if ( m_rowHeights
.IsEmpty() )
6018 // need to really create the array
6022 int h
= wxMax( 0, height
);
6023 int diff
= h
- m_rowHeights
[row
];
6025 m_rowHeights
[row
] = h
;
6027 for ( i
= row
; i
< m_numRows
; i
++ )
6029 m_rowBottoms
[i
] += diff
;
6034 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6036 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6038 if ( resizeExistingCols
)
6046 void wxGrid::SetColSize( int col
, int width
)
6048 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6050 // should we check that it's bigger than GetColMinimalWidth(col) here?
6052 if ( m_colWidths
.IsEmpty() )
6054 // need to really create the array
6058 int w
= wxMax( 0, width
);
6059 int diff
= w
- m_colWidths
[col
];
6060 m_colWidths
[col
] = w
;
6063 for ( i
= col
; i
< m_numCols
; i
++ )
6065 m_colRights
[i
] += diff
;
6071 void wxGrid::SetColMinimalWidth( int col
, int width
)
6073 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6076 int wxGrid::GetColMinimalWidth(int col
) const
6078 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6079 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6083 // ------ cell value accessor functions
6086 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6090 m_table
->SetValue( row
, col
, s
.c_str() );
6091 if ( !GetBatchCount() )
6093 wxClientDC
dc( m_gridWin
);
6095 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6098 #if 0 // TODO: edit in place
6100 if ( m_currentCellCoords
.GetRow() == row
&&
6101 m_currentCellCoords
.GetCol() == col
)
6103 SetEditControlValue( s
);
6112 // ------ Block, row and col selection
6115 void wxGrid::SelectRow( int row
, bool addToSelected
)
6119 if ( IsSelection() && addToSelected
)
6122 bool need_refresh
[4];
6126 need_refresh
[3] = FALSE
;
6130 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6131 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6132 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6133 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6137 need_refresh
[0] = TRUE
;
6138 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6139 wxGridCellCoords ( oldTop
- 1,
6141 m_selectedTopLeft
.SetRow( row
);
6146 need_refresh
[1] = TRUE
;
6147 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6148 wxGridCellCoords ( oldBottom
,
6151 m_selectedTopLeft
.SetCol( 0 );
6154 if ( oldBottom
< row
)
6156 need_refresh
[2] = TRUE
;
6157 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6158 wxGridCellCoords ( row
,
6160 m_selectedBottomRight
.SetRow( row
);
6163 if ( oldRight
< m_numCols
- 1 )
6165 need_refresh
[3] = TRUE
;
6166 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6168 wxGridCellCoords ( oldBottom
,
6170 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6173 for (i
= 0; i
< 4; i
++ )
6174 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6175 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6179 r
= SelectionToDeviceRect();
6181 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6183 m_selectedTopLeft
.Set( row
, 0 );
6184 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6185 r
= SelectionToDeviceRect();
6186 m_gridWin
->Refresh( FALSE
, &r
);
6189 wxGridRangeSelectEvent
gridEvt( GetId(),
6190 wxEVT_GRID_RANGE_SELECT
,
6193 m_selectedBottomRight
);
6195 GetEventHandler()->ProcessEvent(gridEvt
);
6199 void wxGrid::SelectCol( int col
, bool addToSelected
)
6201 if ( IsSelection() && addToSelected
)
6204 bool need_refresh
[4];
6208 need_refresh
[3] = FALSE
;
6211 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6212 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6213 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6214 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6216 if ( oldLeft
> col
)
6218 need_refresh
[0] = TRUE
;
6219 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6220 wxGridCellCoords ( m_numRows
- 1,
6222 m_selectedTopLeft
.SetCol( col
);
6227 need_refresh
[1] = TRUE
;
6228 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6229 wxGridCellCoords ( oldTop
- 1,
6231 m_selectedTopLeft
.SetRow( 0 );
6234 if ( oldRight
< col
)
6236 need_refresh
[2] = TRUE
;
6237 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6238 wxGridCellCoords ( m_numRows
- 1,
6240 m_selectedBottomRight
.SetCol( col
);
6243 if ( oldBottom
< m_numRows
- 1 )
6245 need_refresh
[3] = TRUE
;
6246 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6248 wxGridCellCoords ( m_numRows
- 1,
6250 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6253 for (i
= 0; i
< 4; i
++ )
6254 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6255 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6261 r
= SelectionToDeviceRect();
6263 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6265 m_selectedTopLeft
.Set( 0, col
);
6266 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6267 r
= SelectionToDeviceRect();
6268 m_gridWin
->Refresh( FALSE
, &r
);
6271 wxGridRangeSelectEvent
gridEvt( GetId(),
6272 wxEVT_GRID_RANGE_SELECT
,
6275 m_selectedBottomRight
);
6277 GetEventHandler()->ProcessEvent(gridEvt
);
6281 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6284 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6286 if ( topRow
> bottomRow
)
6293 if ( leftCol
> rightCol
)
6300 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6301 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6303 if ( m_selectedTopLeft
!= updateTopLeft
||
6304 m_selectedBottomRight
!= updateBottomRight
)
6306 // Compute two optimal update rectangles:
6307 // Either one rectangle is a real subset of the
6308 // other, or they are (almost) disjoint!
6310 bool need_refresh
[4];
6314 need_refresh
[3] = FALSE
;
6317 // Store intermediate values
6318 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6319 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6320 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6321 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6323 // Determine the outer/inner coordinates.
6324 if (oldLeft
> leftCol
)
6330 if (oldTop
> topRow
)
6336 if (oldRight
< rightCol
)
6339 oldRight
= rightCol
;
6342 if (oldBottom
< bottomRow
)
6345 oldBottom
= bottomRow
;
6349 // Now, either the stuff marked old is the outer
6350 // rectangle or we don't have a situation where one
6351 // is contained in the other.
6353 if ( oldLeft
< leftCol
)
6355 need_refresh
[0] = TRUE
;
6356 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6358 wxGridCellCoords ( oldBottom
,
6362 if ( oldTop
< topRow
)
6364 need_refresh
[1] = TRUE
;
6365 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6367 wxGridCellCoords ( topRow
- 1,
6371 if ( oldRight
> rightCol
)
6373 need_refresh
[2] = TRUE
;
6374 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6376 wxGridCellCoords ( oldBottom
,
6380 if ( oldBottom
> bottomRow
)
6382 need_refresh
[3] = TRUE
;
6383 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6385 wxGridCellCoords ( oldBottom
,
6391 m_selectedTopLeft
= updateTopLeft
;
6392 m_selectedBottomRight
= updateBottomRight
;
6394 // various Refresh() calls
6395 for (i
= 0; i
< 4; i
++ )
6396 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6397 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6400 // only generate an event if the block is not being selected by
6401 // dragging the mouse (in which case the event will be generated in
6402 // the mouse event handler)
6403 if ( !m_isDragging
)
6405 wxGridRangeSelectEvent
gridEvt( GetId(),
6406 wxEVT_GRID_RANGE_SELECT
,
6409 m_selectedBottomRight
);
6411 GetEventHandler()->ProcessEvent(gridEvt
);
6415 void wxGrid::SelectAll()
6417 m_selectedTopLeft
.Set( 0, 0 );
6418 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6420 m_gridWin
->Refresh();
6424 void wxGrid::ClearSelection()
6426 m_selectedTopLeft
= wxGridNoCellCoords
;
6427 m_selectedBottomRight
= wxGridNoCellCoords
;
6431 // This function returns the rectangle that encloses the given block
6432 // in device coords clipped to the client size of the grid window.
6434 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6435 const wxGridCellCoords
&bottomRight
)
6437 wxRect
rect( wxGridNoCellRect
);
6440 cellRect
= CellToRect( topLeft
);
6441 if ( cellRect
!= wxGridNoCellRect
)
6447 rect
= wxRect( 0, 0, 0, 0 );
6450 cellRect
= CellToRect( bottomRight
);
6451 if ( cellRect
!= wxGridNoCellRect
)
6457 return wxGridNoCellRect
;
6460 // convert to scrolled coords
6462 int left
, top
, right
, bottom
;
6463 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6464 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6467 m_gridWin
->GetClientSize( &cw
, &ch
);
6469 rect
.SetLeft( wxMax(0, left
) );
6470 rect
.SetTop( wxMax(0, top
) );
6471 rect
.SetRight( wxMin(cw
, right
) );
6472 rect
.SetBottom( wxMin(ch
, bottom
) );
6480 // ------ Grid event classes
6483 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6485 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6486 int row
, int col
, int x
, int y
,
6487 bool control
, bool shift
, bool alt
, bool meta
)
6488 : wxNotifyEvent( type
, id
)
6494 m_control
= control
;
6499 SetEventObject(obj
);
6503 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6505 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6506 int rowOrCol
, int x
, int y
,
6507 bool control
, bool shift
, bool alt
, bool meta
)
6508 : wxNotifyEvent( type
, id
)
6510 m_rowOrCol
= rowOrCol
;
6513 m_control
= control
;
6518 SetEventObject(obj
);
6522 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6524 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6525 const wxGridCellCoords
& topLeft
,
6526 const wxGridCellCoords
& bottomRight
,
6527 bool control
, bool shift
, bool alt
, bool meta
)
6528 : wxNotifyEvent( type
, id
)
6530 m_topLeft
= topLeft
;
6531 m_bottomRight
= bottomRight
;
6532 m_control
= control
;
6537 SetEventObject(obj
);
6541 #endif // ifndef wxUSE_NEW_GRID