1 ///////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
46 // this include needs to be outside precomp for BCC
47 #include "wx/textfile.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
58 struct wxGridCellWithAttr
60 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
61 : coords(row
, col
), attr(attr_
)
70 wxGridCellCoords coords
;
74 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
76 #include "wx/arrimpl.cpp"
78 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
79 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
88 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
89 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
90 const wxPoint
&pos
, const wxSize
&size
);
95 void OnPaint( wxPaintEvent
& event
);
96 void OnMouseEvent( wxMouseEvent
& event
);
97 void OnKeyDown( wxKeyEvent
& event
);
99 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
100 DECLARE_EVENT_TABLE()
104 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
107 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
108 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
109 const wxPoint
&pos
, const wxSize
&size
);
114 void OnPaint( wxPaintEvent
&event
);
115 void OnMouseEvent( wxMouseEvent
& event
);
116 void OnKeyDown( wxKeyEvent
& event
);
118 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
119 DECLARE_EVENT_TABLE()
123 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
126 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
);
133 void OnMouseEvent( wxMouseEvent
& event
);
134 void OnKeyDown( wxKeyEvent
& event
);
135 void OnPaint( wxPaintEvent
& event
);
137 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
138 DECLARE_EVENT_TABLE()
141 class WXDLLEXPORT wxGridWindow
: public wxPanel
146 m_owner
= (wxGrid
*)NULL
;
147 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
148 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
151 wxGridWindow( wxGrid
*parent
,
152 wxGridRowLabelWindow
*rowLblWin
,
153 wxGridColLabelWindow
*colLblWin
,
154 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
157 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
161 wxGridRowLabelWindow
*m_rowLabelWin
;
162 wxGridColLabelWindow
*m_colLabelWin
;
164 void OnPaint( wxPaintEvent
&event
);
165 void OnMouseEvent( wxMouseEvent
& event
);
166 void OnKeyDown( wxKeyEvent
& );
167 void OnEraseBackground( wxEraseEvent
& );
170 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
171 DECLARE_EVENT_TABLE()
176 class wxGridCellEditorEvtHandler
: public wxEvtHandler
179 wxGridCellEditorEvtHandler()
180 : m_grid(0), m_editor(0)
182 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
183 : m_grid(grid
), m_editor(editor
)
186 void OnKeyDown(wxKeyEvent
& event
);
187 void OnChar(wxKeyEvent
& event
);
191 wxGridCellEditor
* m_editor
;
192 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
193 DECLARE_EVENT_TABLE()
197 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
198 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
199 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
200 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
205 // ----------------------------------------------------------------------------
206 // the internal data representation used by wxGridCellAttrProvider
207 // ----------------------------------------------------------------------------
209 // this class stores attributes set for cells
210 class WXDLLEXPORT wxGridCellAttrData
213 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
214 wxGridCellAttr
*GetAttr(int row
, int col
) const;
215 void UpdateAttrRows( size_t pos
, int numRows
);
216 void UpdateAttrCols( size_t pos
, int numCols
);
219 // searches for the attr for given cell, returns wxNOT_FOUND if not found
220 int FindIndex(int row
, int col
) const;
222 wxGridCellWithAttrArray m_attrs
;
225 // this class stores attributes set for rows or columns
226 class WXDLLEXPORT wxGridRowOrColAttrData
229 ~wxGridRowOrColAttrData();
231 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
232 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
233 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
236 wxArrayInt m_rowsOrCols
;
237 wxArrayAttrs m_attrs
;
240 // NB: this is just a wrapper around 3 objects: one which stores cell
241 // attributes, and 2 others for row/col ones
242 class WXDLLEXPORT wxGridCellAttrProviderData
245 wxGridCellAttrData m_cellAttrs
;
246 wxGridRowOrColAttrData m_rowAttrs
,
250 // ----------------------------------------------------------------------------
251 // conditional compilation
252 // ----------------------------------------------------------------------------
254 #ifndef WXGRID_DRAW_LINES
255 #define WXGRID_DRAW_LINES 1
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 //#define DEBUG_ATTR_CACHE
263 #ifdef DEBUG_ATTR_CACHE
264 static size_t gs_nAttrCacheHits
= 0;
265 static size_t gs_nAttrCacheMisses
= 0;
266 #endif // DEBUG_ATTR_CACHE
268 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
269 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
272 // TODO: fixed so far - make configurable later (and also different for x/y)
273 static const size_t GRID_SCROLL_LINE
= 10;
275 // ============================================================================
277 // ============================================================================
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 wxGridCellEditor::wxGridCellEditor()
289 wxGridCellEditor::~wxGridCellEditor()
294 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
295 wxWindowID
WXUNUSED(id
),
296 wxEvtHandler
* evtHandler
)
299 m_control
->PushEventHandler(evtHandler
);
302 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
303 wxGridCellAttr
*attr
)
305 // erase the background because we might not fill the cell
306 wxClientDC
dc(m_control
->GetParent());
307 dc
.SetPen(*wxTRANSPARENT_PEN
);
308 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
309 dc
.DrawRectangle(rectCell
);
311 // redraw the control we just painted over
312 m_control
->Refresh();
315 void wxGridCellEditor::Destroy()
319 m_control
->Destroy();
324 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
326 wxASSERT_MSG(m_control
,
327 wxT("The wxGridCellEditor must be Created first!"));
328 m_control
->Show(show
);
332 // set the colours/fonts if we have any
335 if ( attr
->HasTextColour() )
337 m_colFgOld
= m_control
->GetForegroundColour();
338 m_control
->SetForegroundColour(attr
->GetTextColour());
341 if ( attr
->HasBackgroundColour() )
343 m_colBgOld
= m_control
->GetBackgroundColour();
344 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
347 if ( attr
->HasFont() )
349 m_fontOld
= m_control
->GetFont();
350 m_control
->SetFont(attr
->GetFont());
353 // can't do anything more in the base class version, the other
354 // attributes may only be used by the derived classes
359 // restore the standard colours fonts
360 if ( m_colFgOld
.Ok() )
362 m_control
->SetForegroundColour(m_colFgOld
);
363 m_colFgOld
= wxNullColour
;
366 if ( m_colBgOld
.Ok() )
368 m_control
->SetBackgroundColour(m_colBgOld
);
369 m_colBgOld
= wxNullColour
;
372 if ( m_fontOld
.Ok() )
374 m_control
->SetFont(m_fontOld
);
375 m_fontOld
= wxNullFont
;
380 void wxGridCellEditor::SetSize(const wxRect
& rect
)
382 wxASSERT_MSG(m_control
,
383 wxT("The wxGridCellEditor must be Created first!"));
384 m_control
->SetSize(rect
);
387 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
393 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
398 void wxGridCellEditor::StartingClick()
402 // ----------------------------------------------------------------------------
403 // wxGridCellTextEditor
404 // ----------------------------------------------------------------------------
406 wxGridCellTextEditor::wxGridCellTextEditor()
410 void wxGridCellTextEditor::Create(wxWindow
* parent
,
412 wxEvtHandler
* evtHandler
)
414 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
415 wxDefaultPosition
, wxDefaultSize
416 #if defined(__WXMSW__)
417 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
421 wxGridCellEditor::Create(parent
, id
, evtHandler
);
424 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
425 wxGridCellAttr
* WXUNUSED(attr
))
427 // as we fill the entire client area, don't do anything here to minimize
431 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
433 wxASSERT_MSG(m_control
,
434 wxT("The wxGridCellEditor must be Created first!"));
436 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
437 Text()->SetValue(m_startValue
);
438 Text()->SetInsertionPointEnd();
443 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
446 wxASSERT_MSG(m_control
,
447 wxT("The wxGridCellEditor must be Created first!"));
449 bool changed
= FALSE
;
450 wxString value
= Text()->GetValue();
451 if (value
!= m_startValue
)
455 grid
->GetTable()->SetValue(row
, col
, value
);
457 m_startValue
= wxEmptyString
;
458 Text()->SetValue(m_startValue
);
464 void wxGridCellTextEditor::Reset()
466 wxASSERT_MSG(m_control
,
467 wxT("The wxGridCellEditor must be Created first!"));
469 Text()->SetValue(m_startValue
);
470 Text()->SetInsertionPointEnd();
473 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
475 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
477 // insert the key in the control
478 long keycode
= event
.KeyCode();
479 if ( isprint(keycode
) )
481 // FIXME this is not going to work for non letters...
482 if ( !event
.ShiftDown() )
484 keycode
= tolower(keycode
);
487 Text()->AppendText((wxChar
)keycode
);
497 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
499 #if defined(__WXMOTIF__) || defined(__WXGTK__)
500 // wxMotif needs a little extra help...
501 int pos
= Text()->GetInsertionPoint();
502 wxString
s( Text()->GetValue() );
503 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
505 Text()->SetInsertionPoint( pos
);
507 // the other ports can handle a Return key press
513 // ----------------------------------------------------------------------------
514 // wxGridCellBoolEditor
515 // ----------------------------------------------------------------------------
517 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
519 wxEvtHandler
* evtHandler
)
521 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
522 wxDefaultPosition
, wxDefaultSize
,
525 wxGridCellEditor::Create(parent
, id
, evtHandler
);
528 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
530 // position it in the centre of the rectangle (TODO: support alignment?)
532 m_control
->GetSize(&w
, &h
);
534 // the checkbox without label still has some space to the right in wxGTK,
535 // so shift it to the right
540 m_control
->Move(r
.x
+ r
.width
/2 - w
/2, r
.y
+ r
.height
/2 - h
/2);
543 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
545 wxGridCellEditor::Show(show
, attr
);
548 // VZ: normally base class already does it, but it doesn't work (FIXME)
549 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
550 CBox()->SetBackgroundColour(colBg
);
554 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
556 wxASSERT_MSG(m_control
,
557 wxT("The wxGridCellEditor must be Created first!"));
559 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
); // FIXME-DATA
560 CBox()->SetValue(m_startValue
);
564 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
568 wxASSERT_MSG(m_control
,
569 wxT("The wxGridCellEditor must be Created first!"));
571 bool changed
= FALSE
;
572 bool value
= CBox()->GetValue();
573 if ( value
!= m_startValue
)
579 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
585 void wxGridCellBoolEditor::Reset()
587 wxASSERT_MSG(m_control
,
588 wxT("The wxGridCellEditor must be Created first!"));
590 CBox()->SetValue(m_startValue
);
593 void wxGridCellBoolEditor::StartingClick()
595 CBox()->SetValue(!CBox()->GetValue());
598 // ----------------------------------------------------------------------------
599 // wxGridCellEditorEvtHandler
600 // ----------------------------------------------------------------------------
602 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
604 switch ( event
.KeyCode() )
608 m_grid
->DisableCellEditControl();
612 event
.Skip( m_grid
->ProcessEvent( event
) );
616 if (!m_grid
->ProcessEvent(event
))
617 m_editor
->HandleReturn(event
);
626 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
628 switch ( event
.KeyCode() )
640 // ============================================================================
642 // ============================================================================
644 // ----------------------------------------------------------------------------
645 // wxGridCellRenderer
646 // ----------------------------------------------------------------------------
648 void wxGridCellRenderer::Draw(wxGrid
& grid
,
649 wxGridCellAttr
& attr
,
655 dc
.SetBackgroundMode( wxSOLID
);
659 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
663 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
666 dc
.SetPen( *wxTRANSPARENT_PEN
);
667 dc
.DrawRectangle(rect
);
670 // ----------------------------------------------------------------------------
671 // wxGridCellStringRenderer
672 // ----------------------------------------------------------------------------
674 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
675 wxGridCellAttr
& attr
,
677 const wxRect
& rectCell
,
681 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
683 // now we only have to draw the text
684 dc
.SetBackgroundMode( wxTRANSPARENT
);
686 // TODO some special colours for attr.IsReadOnly() case?
690 dc
.SetTextBackground( grid
.GetSelectionBackground() );
691 dc
.SetTextForeground( grid
.GetSelectionForeground() );
695 dc
.SetTextBackground( attr
.GetBackgroundColour() );
696 dc
.SetTextForeground( attr
.GetTextColour() );
698 dc
.SetFont( attr
.GetFont() );
701 attr
.GetAlignment(&hAlign
, &vAlign
);
703 wxRect rect
= rectCell
;
709 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
710 rect
, hAlign
, vAlign
);
713 // ----------------------------------------------------------------------------
714 // wxGridCellBoolRenderer
715 // ----------------------------------------------------------------------------
717 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
718 wxGridCellAttr
& attr
,
724 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
726 // between checkmark and box
727 static const wxCoord margin
= 4;
730 static wxCoord s_checkSize
= 0;
731 if ( s_checkSize
== 0 )
733 // compute it only once (no locks for MT safeness in GUI thread...)
734 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
735 wxSize size
= checkbox
->GetBestSize();
736 s_checkSize
= size
.y
+ margin
;
738 // FIXME wxGTK::wxCheckBox::GetBestSize() is really weird...
740 s_checkSize
-= size
.y
/ 2;
746 // draw a check mark in the centre (ignoring alignment - TODO)
748 rectMark
.x
= rect
.x
+ rect
.width
/2 - s_checkSize
/2;
749 rectMark
.y
= rect
.y
+ rect
.height
/2 - s_checkSize
/2;
750 rectMark
.width
= rectMark
.height
= s_checkSize
;
752 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
753 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
754 dc
.DrawRectangle(rectMark
);
756 rectMark
.Inflate(-margin
);
758 if ( !!grid
.GetTable()->GetValue(row
, col
) ) // FIXME-DATA
760 dc
.SetTextForeground(attr
.GetTextColour());
761 dc
.DrawCheckMark(rectMark
);
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
769 const wxColour
& wxGridCellAttr::GetTextColour() const
775 else if (m_defGridAttr
!= this)
777 return m_defGridAttr
->GetTextColour();
781 wxFAIL_MSG(wxT("Missing default cell attribute"));
787 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
789 if (HasBackgroundColour())
791 else if (m_defGridAttr
!= this)
792 return m_defGridAttr
->GetBackgroundColour();
795 wxFAIL_MSG(wxT("Missing default cell attribute"));
801 const wxFont
& wxGridCellAttr::GetFont() const
805 else if (m_defGridAttr
!= this)
806 return m_defGridAttr
->GetFont();
809 wxFAIL_MSG(wxT("Missing default cell attribute"));
815 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
819 if ( hAlign
) *hAlign
= m_hAlign
;
820 if ( vAlign
) *vAlign
= m_vAlign
;
822 else if (m_defGridAttr
!= this)
823 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
826 wxFAIL_MSG(wxT("Missing default cell attribute"));
831 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
835 else if (m_defGridAttr
!= this)
836 return m_defGridAttr
->GetRenderer();
839 wxFAIL_MSG(wxT("Missing default cell attribute"));
844 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
848 else if (m_defGridAttr
!= this)
849 return m_defGridAttr
->GetEditor();
852 wxFAIL_MSG(wxT("Missing default cell attribute"));
857 // ----------------------------------------------------------------------------
858 // wxGridCellAttrData
859 // ----------------------------------------------------------------------------
861 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
863 int n
= FindIndex(row
, col
);
864 if ( n
== wxNOT_FOUND
)
867 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
873 // change the attribute
874 m_attrs
[(size_t)n
].attr
= attr
;
878 // remove this attribute
879 m_attrs
.RemoveAt((size_t)n
);
884 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
886 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
888 int n
= FindIndex(row
, col
);
889 if ( n
!= wxNOT_FOUND
)
891 attr
= m_attrs
[(size_t)n
].attr
;
898 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
900 size_t count
= m_attrs
.GetCount();
901 for ( size_t n
= 0; n
< count
; n
++ )
903 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
904 wxCoord row
= coords
.GetRow();
905 if ((size_t)row
>= pos
)
909 // If rows inserted, include row counter where necessary
910 coords
.SetRow(row
+ numRows
);
912 else if (numRows
< 0)
914 // If rows deleted ...
915 if ((size_t)row
>= pos
- numRows
)
917 // ...either decrement row counter (if row still exists)...
918 coords
.SetRow(row
+ numRows
);
922 // ...or remove the attribute
923 m_attrs
.RemoveAt((size_t)n
);
931 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
933 size_t count
= m_attrs
.GetCount();
934 for ( size_t n
= 0; n
< count
; n
++ )
936 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
937 wxCoord col
= coords
.GetCol();
938 if ( (size_t)col
>= pos
)
942 // If rows inserted, include row counter where necessary
943 coords
.SetCol(col
+ numCols
);
945 else if (numCols
< 0)
947 // If rows deleted ...
948 if ((size_t)col
>= pos
- numCols
)
950 // ...either decrement row counter (if row still exists)...
951 coords
.SetCol(col
+ numCols
);
955 // ...or remove the attribute
956 m_attrs
.RemoveAt((size_t)n
);
964 int wxGridCellAttrData::FindIndex(int row
, int col
) const
966 size_t count
= m_attrs
.GetCount();
967 for ( size_t n
= 0; n
< count
; n
++ )
969 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
970 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
979 // ----------------------------------------------------------------------------
980 // wxGridRowOrColAttrData
981 // ----------------------------------------------------------------------------
983 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
985 size_t count
= m_attrs
.Count();
986 for ( size_t n
= 0; n
< count
; n
++ )
988 m_attrs
[n
]->DecRef();
992 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
994 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
996 int n
= m_rowsOrCols
.Index(rowOrCol
);
997 if ( n
!= wxNOT_FOUND
)
999 attr
= m_attrs
[(size_t)n
];
1006 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1008 int n
= m_rowsOrCols
.Index(rowOrCol
);
1009 if ( n
== wxNOT_FOUND
)
1011 // add the attribute
1012 m_rowsOrCols
.Add(rowOrCol
);
1019 // change the attribute
1020 m_attrs
[(size_t)n
] = attr
;
1024 // remove this attribute
1025 m_attrs
[(size_t)n
]->DecRef();
1026 m_rowsOrCols
.RemoveAt((size_t)n
);
1027 m_attrs
.RemoveAt((size_t)n
);
1032 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1034 size_t count
= m_attrs
.GetCount();
1035 for ( size_t n
= 0; n
< count
; n
++ )
1037 int & rowOrCol
= m_rowsOrCols
[n
];
1038 if ( (size_t)rowOrCol
>= pos
)
1040 if ( numRowsOrCols
> 0 )
1042 // If rows inserted, include row counter where necessary
1043 rowOrCol
+= numRowsOrCols
;
1045 else if ( numRowsOrCols
< 0)
1047 // If rows deleted, either decrement row counter (if row still exists)
1048 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1049 rowOrCol
+= numRowsOrCols
;
1052 m_rowsOrCols
.RemoveAt((size_t)n
);
1053 m_attrs
.RemoveAt((size_t)n
);
1061 // ----------------------------------------------------------------------------
1062 // wxGridCellAttrProvider
1063 // ----------------------------------------------------------------------------
1065 wxGridCellAttrProvider::wxGridCellAttrProvider()
1067 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1070 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1075 void wxGridCellAttrProvider::InitData()
1077 m_data
= new wxGridCellAttrProviderData
;
1080 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1082 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1085 // first look for the attribute of this specific cell
1086 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1090 // then look for the col attr (col attributes are more common than
1091 // the row ones, hence they have priority)
1092 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1097 // finally try the row attributes
1098 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1105 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1111 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1114 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1119 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1122 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1127 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1130 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1134 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1136 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1140 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1144 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1146 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1150 // ----------------------------------------------------------------------------
1152 // ----------------------------------------------------------------------------
1154 //////////////////////////////////////////////////////////////////////
1156 // Abstract base class for grid data (the model)
1158 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1161 wxGridTableBase::wxGridTableBase()
1163 m_view
= (wxGrid
*) NULL
;
1164 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1167 wxGridTableBase::~wxGridTableBase()
1169 delete m_attrProvider
;
1172 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1174 delete m_attrProvider
;
1175 m_attrProvider
= attrProvider
;
1178 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1180 if ( m_attrProvider
)
1181 return m_attrProvider
->GetAttr(row
, col
);
1183 return (wxGridCellAttr
*)NULL
;
1186 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1188 if ( m_attrProvider
)
1190 m_attrProvider
->SetAttr(attr
, row
, col
);
1194 // as we take ownership of the pointer and don't store it, we must
1200 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1202 if ( m_attrProvider
)
1204 m_attrProvider
->SetRowAttr(attr
, row
);
1208 // as we take ownership of the pointer and don't store it, we must
1214 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1216 if ( m_attrProvider
)
1218 m_attrProvider
->SetColAttr(attr
, col
);
1222 // as we take ownership of the pointer and don't store it, we must
1228 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1230 if ( m_attrProvider
)
1232 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1236 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1238 if ( m_attrProvider
)
1240 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1244 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1246 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1247 "but your derived table class does not override this function") );
1252 bool wxGridTableBase::AppendRows( size_t numRows
)
1254 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1255 "but your derived table class does not override this function"));
1260 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1262 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1263 "but your derived table class does not override this function"));
1268 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1270 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1271 "but your derived table class does not override this function"));
1276 bool wxGridTableBase::AppendCols( size_t numCols
)
1278 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1279 "but your derived table class does not override this function"));
1284 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1286 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1287 "but your derived table class does not override this function"));
1293 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1300 wxString
wxGridTableBase::GetColLabelValue( int col
)
1302 // default col labels are:
1303 // cols 0 to 25 : A-Z
1304 // cols 26 to 675 : AA-ZZ
1309 for ( n
= 1; ; n
++ )
1311 s
+= (_T('A') + (wxChar
)( col%26
));
1313 if ( col
< 0 ) break;
1316 // reverse the string...
1318 for ( i
= 0; i
< n
; i
++ )
1328 //////////////////////////////////////////////////////////////////////
1330 // Message class for the grid table to send requests and notifications
1334 wxGridTableMessage::wxGridTableMessage()
1336 m_table
= (wxGridTableBase
*) NULL
;
1342 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1343 int commandInt1
, int commandInt2
)
1347 m_comInt1
= commandInt1
;
1348 m_comInt2
= commandInt2
;
1353 //////////////////////////////////////////////////////////////////////
1355 // A basic grid table for string data. An object of this class will
1356 // created by wxGrid if you don't specify an alternative table class.
1359 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1361 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1363 wxGridStringTable::wxGridStringTable()
1368 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1373 m_data
.Alloc( numRows
);
1376 sa
.Alloc( numCols
);
1377 for ( col
= 0; col
< numCols
; col
++ )
1379 sa
.Add( wxEmptyString
);
1382 for ( row
= 0; row
< numRows
; row
++ )
1388 wxGridStringTable::~wxGridStringTable()
1392 long wxGridStringTable::GetNumberRows()
1394 return m_data
.GetCount();
1397 long wxGridStringTable::GetNumberCols()
1399 if ( m_data
.GetCount() > 0 )
1400 return m_data
[0].GetCount();
1405 wxString
wxGridStringTable::GetValue( int row
, int col
)
1407 // TODO: bounds checking
1409 return m_data
[row
][col
];
1412 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1414 // TODO: bounds checking
1416 m_data
[row
][col
] = s
;
1419 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1421 // TODO: bounds checking
1423 return (m_data
[row
][col
] == wxEmptyString
);
1427 void wxGridStringTable::Clear()
1430 int numRows
, numCols
;
1432 numRows
= m_data
.GetCount();
1435 numCols
= m_data
[0].GetCount();
1437 for ( row
= 0; row
< numRows
; row
++ )
1439 for ( col
= 0; col
< numCols
; col
++ )
1441 m_data
[row
][col
] = wxEmptyString
;
1448 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1452 size_t curNumRows
= m_data
.GetCount();
1453 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1455 if ( pos
>= curNumRows
)
1457 return AppendRows( numRows
);
1461 sa
.Alloc( curNumCols
);
1462 for ( col
= 0; col
< curNumCols
; col
++ )
1464 sa
.Add( wxEmptyString
);
1467 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1469 m_data
.Insert( sa
, row
);
1471 UpdateAttrRows( pos
, numRows
);
1474 wxGridTableMessage
msg( this,
1475 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1479 GetView()->ProcessTableMessage( msg
);
1485 bool wxGridStringTable::AppendRows( size_t numRows
)
1489 size_t curNumRows
= m_data
.GetCount();
1490 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1493 if ( curNumCols
> 0 )
1495 sa
.Alloc( curNumCols
);
1496 for ( col
= 0; col
< curNumCols
; col
++ )
1498 sa
.Add( wxEmptyString
);
1502 for ( row
= 0; row
< numRows
; row
++ )
1509 wxGridTableMessage
msg( this,
1510 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1513 GetView()->ProcessTableMessage( msg
);
1519 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1523 size_t curNumRows
= m_data
.GetCount();
1525 if ( pos
>= curNumRows
)
1528 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1529 "Pos value is invalid for present table with %d rows",
1530 pos
, numRows
, curNumRows
);
1531 wxFAIL_MSG( wxT(errmsg
) );
1535 if ( numRows
> curNumRows
- pos
)
1537 numRows
= curNumRows
- pos
;
1540 if ( numRows
>= curNumRows
)
1542 m_data
.Empty(); // don't release memory just yet
1546 for ( n
= 0; n
< numRows
; n
++ )
1548 m_data
.Remove( pos
);
1551 UpdateAttrRows( pos
, -((int)numRows
) );
1554 wxGridTableMessage
msg( this,
1555 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1559 GetView()->ProcessTableMessage( msg
);
1565 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1569 size_t curNumRows
= m_data
.GetCount();
1570 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1572 if ( pos
>= curNumCols
)
1574 return AppendCols( numCols
);
1577 for ( row
= 0; row
< curNumRows
; row
++ )
1579 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1581 m_data
[row
].Insert( wxEmptyString
, col
);
1584 UpdateAttrCols( pos
, numCols
);
1587 wxGridTableMessage
msg( this,
1588 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1592 GetView()->ProcessTableMessage( msg
);
1598 bool wxGridStringTable::AppendCols( size_t numCols
)
1602 size_t curNumRows
= m_data
.GetCount();
1605 // TODO: something better than this ?
1607 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1608 "Call AppendRows() first") );
1612 for ( row
= 0; row
< curNumRows
; row
++ )
1614 for ( n
= 0; n
< numCols
; n
++ )
1616 m_data
[row
].Add( wxEmptyString
);
1622 wxGridTableMessage
msg( this,
1623 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1626 GetView()->ProcessTableMessage( msg
);
1632 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1636 size_t curNumRows
= m_data
.GetCount();
1637 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1639 if ( pos
>= curNumCols
)
1642 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1643 "Pos value is invalid for present table with %d cols",
1644 pos
, numCols
, curNumCols
);
1645 wxFAIL_MSG( wxT( errmsg
) );
1649 if ( numCols
> curNumCols
- pos
)
1651 numCols
= curNumCols
- pos
;
1654 for ( row
= 0; row
< curNumRows
; row
++ )
1656 if ( numCols
>= curNumCols
)
1658 m_data
[row
].Clear();
1662 for ( n
= 0; n
< numCols
; n
++ )
1664 m_data
[row
].Remove( pos
);
1668 UpdateAttrCols( pos
, -((int)numCols
) );
1671 wxGridTableMessage
msg( this,
1672 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1676 GetView()->ProcessTableMessage( msg
);
1682 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1684 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1686 // using default label
1688 return wxGridTableBase::GetRowLabelValue( row
);
1692 return m_rowLabels
[ row
];
1696 wxString
wxGridStringTable::GetColLabelValue( int col
)
1698 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1700 // using default label
1702 return wxGridTableBase::GetColLabelValue( col
);
1706 return m_colLabels
[ col
];
1710 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1712 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1714 int n
= m_rowLabels
.GetCount();
1716 for ( i
= n
; i
<= row
; i
++ )
1718 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1722 m_rowLabels
[row
] = value
;
1725 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1727 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1729 int n
= m_colLabels
.GetCount();
1731 for ( i
= n
; i
<= col
; i
++ )
1733 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1737 m_colLabels
[col
] = value
;
1742 //////////////////////////////////////////////////////////////////////
1743 //////////////////////////////////////////////////////////////////////
1745 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1747 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1748 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1749 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1750 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1753 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1755 const wxPoint
&pos
, const wxSize
&size
)
1756 : wxWindow( parent
, id
, pos
, size
)
1761 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1765 // NO - don't do this because it will set both the x and y origin
1766 // coords to match the parent scrolled window and we just want to
1767 // set the y coord - MB
1769 // m_owner->PrepareDC( dc );
1772 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1773 dc
.SetDeviceOrigin( 0, -y
);
1775 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1776 m_owner
->DrawRowLabels( dc
);
1780 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1782 m_owner
->ProcessRowLabelMouseEvent( event
);
1786 // This seems to be required for wxMotif otherwise the mouse
1787 // cursor must be in the cell edit control to get key events
1789 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1791 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1796 //////////////////////////////////////////////////////////////////////
1798 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1800 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1801 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1802 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1803 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1806 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1808 const wxPoint
&pos
, const wxSize
&size
)
1809 : wxWindow( parent
, id
, pos
, size
)
1814 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1818 // NO - don't do this because it will set both the x and y origin
1819 // coords to match the parent scrolled window and we just want to
1820 // set the x coord - MB
1822 // m_owner->PrepareDC( dc );
1825 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1826 dc
.SetDeviceOrigin( -x
, 0 );
1828 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1829 m_owner
->DrawColLabels( dc
);
1833 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1835 m_owner
->ProcessColLabelMouseEvent( event
);
1839 // This seems to be required for wxMotif otherwise the mouse
1840 // cursor must be in the cell edit control to get key events
1842 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1844 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1849 //////////////////////////////////////////////////////////////////////
1851 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1853 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1854 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1855 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1856 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1859 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1861 const wxPoint
&pos
, const wxSize
&size
)
1862 : wxWindow( parent
, id
, pos
, size
)
1867 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1871 int client_height
= 0;
1872 int client_width
= 0;
1873 GetClientSize( &client_width
, &client_height
);
1875 dc
.SetPen( *wxBLACK_PEN
);
1876 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1877 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1879 dc
.SetPen( *wxWHITE_PEN
);
1880 dc
.DrawLine( 0, 0, client_width
, 0 );
1881 dc
.DrawLine( 0, 0, 0, client_height
);
1885 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1887 m_owner
->ProcessCornerLabelMouseEvent( event
);
1891 // This seems to be required for wxMotif otherwise the mouse
1892 // cursor must be in the cell edit control to get key events
1894 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1896 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1901 //////////////////////////////////////////////////////////////////////
1903 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1905 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1906 EVT_PAINT( wxGridWindow::OnPaint
)
1907 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1908 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1909 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1912 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1913 wxGridRowLabelWindow
*rowLblWin
,
1914 wxGridColLabelWindow
*colLblWin
,
1915 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1916 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1919 m_rowLabelWin
= rowLblWin
;
1920 m_colLabelWin
= colLblWin
;
1921 SetBackgroundColour( "WHITE" );
1925 wxGridWindow::~wxGridWindow()
1930 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1932 wxPaintDC
dc( this );
1933 m_owner
->PrepareDC( dc
);
1934 wxRegion reg
= GetUpdateRegion();
1935 m_owner
->CalcCellsExposed( reg
);
1936 m_owner
->DrawGridCellArea( dc
);
1937 #if WXGRID_DRAW_LINES
1938 m_owner
->DrawAllGridLines( dc
, reg
);
1943 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1945 wxPanel::ScrollWindow( dx
, dy
, rect
);
1946 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1947 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1951 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1953 m_owner
->ProcessGridCellMouseEvent( event
);
1957 // This seems to be required for wxMotif otherwise the mouse
1958 // cursor must be in the cell edit control to get key events
1960 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1962 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1965 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1971 //////////////////////////////////////////////////////////////////////
1974 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1976 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1977 EVT_PAINT( wxGrid::OnPaint
)
1978 EVT_SIZE( wxGrid::OnSize
)
1979 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1980 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1983 wxGrid::wxGrid( wxWindow
*parent
,
1988 const wxString
& name
)
1989 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1998 m_defaultCellAttr
->SafeDecRef();
2000 #ifdef DEBUG_ATTR_CACHE
2001 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2002 wxPrintf(_T("wxGrid attribute cache statistics: "
2003 "total: %u, hits: %u (%u%%)\n"),
2004 total
, gs_nAttrCacheHits
,
2005 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2014 // ----- internal init and update functions
2017 void wxGrid::Create()
2019 m_created
= FALSE
; // set to TRUE by CreateGrid
2020 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2022 m_table
= (wxGridTableBase
*) NULL
;
2025 m_cellEditCtrlEnabled
= FALSE
;
2027 m_defaultCellAttr
= new wxGridCellAttr
;
2028 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2029 // RD: Should we fill the default attrs now or is waiting until Init() okay?
2034 m_currentCellCoords
= wxGridNoCellCoords
;
2036 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2037 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2039 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2044 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2049 m_colLabelWin
= new wxGridColLabelWindow( this,
2054 m_gridWin
= new wxGridWindow( this,
2061 SetTargetWindow( m_gridWin
);
2065 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2069 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2074 m_numRows
= numRows
;
2075 m_numCols
= numCols
;
2077 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2078 m_table
->SetView( this );
2087 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2091 // RD: Actually, this should probably be allowed. I think it would be
2092 // nice to be able to switch multiple Tables in and out of a single
2093 // View at runtime. Is there anything in the implmentation that would
2096 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2101 m_numRows
= table
->GetNumberRows();
2102 m_numCols
= table
->GetNumberCols();
2105 m_table
->SetView( this );
2120 if ( m_numRows
<= 0 )
2121 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2123 if ( m_numCols
<= 0 )
2124 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2126 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2127 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2129 if ( m_rowLabelWin
)
2131 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2135 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2138 m_labelTextColour
= wxColour( _T("BLACK") );
2141 m_attrCache
.row
= -1;
2143 // TODO: something better than this ?
2145 m_labelFont
= this->GetFont();
2146 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2148 m_rowLabelHorizAlign
= wxLEFT
;
2149 m_rowLabelVertAlign
= wxCENTRE
;
2151 m_colLabelHorizAlign
= wxCENTRE
;
2152 m_colLabelVertAlign
= wxTOP
;
2154 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2155 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2157 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2158 m_defaultRowHeight
+= 8;
2160 m_defaultRowHeight
+= 4;
2163 m_rowHeights
.Alloc( m_numRows
);
2164 m_rowBottoms
.Alloc( m_numRows
);
2166 for ( i
= 0; i
< m_numRows
; i
++ )
2168 m_rowHeights
.Add( m_defaultRowHeight
);
2169 rowBottom
+= m_defaultRowHeight
;
2170 m_rowBottoms
.Add( rowBottom
);
2173 m_colWidths
.Alloc( m_numCols
);
2174 m_colRights
.Alloc( m_numCols
);
2176 for ( i
= 0; i
< m_numCols
; i
++ )
2178 m_colWidths
.Add( m_defaultColWidth
);
2179 colRight
+= m_defaultColWidth
;
2180 m_colRights
.Add( colRight
);
2183 // Set default cell attributes
2184 m_defaultCellAttr
->SetFont(GetFont());
2185 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2186 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2187 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2188 m_defaultCellAttr
->SetTextColour(
2189 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2190 m_defaultCellAttr
->SetBackgroundColour(
2191 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2194 m_gridLineColour
= wxColour( 128, 128, 255 );
2195 m_gridLinesEnabled
= TRUE
;
2197 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2198 m_winCapture
= (wxWindow
*)NULL
;
2200 m_dragRowOrCol
= -1;
2201 m_isDragging
= FALSE
;
2202 m_startDragPos
= wxDefaultPosition
;
2204 m_waitForSlowClick
= FALSE
;
2206 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2207 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2209 m_currentCellCoords
= wxGridNoCellCoords
;
2211 m_selectedTopLeft
= wxGridNoCellCoords
;
2212 m_selectedBottomRight
= wxGridNoCellCoords
;
2213 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2214 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2216 m_editable
= TRUE
; // default for whole grid
2218 m_inOnKeyDown
= FALSE
;
2224 void wxGrid::CalcDimensions()
2227 GetClientSize( &cw
, &ch
);
2229 if ( m_numRows
> 0 && m_numCols
> 0 )
2231 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2232 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2234 // TODO: restore the scroll position that we had before sizing
2237 GetViewStart( &x
, &y
);
2238 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2239 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2245 void wxGrid::CalcWindowSizes()
2248 GetClientSize( &cw
, &ch
);
2250 if ( m_cornerLabelWin
->IsShown() )
2251 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2253 if ( m_colLabelWin
->IsShown() )
2254 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2256 if ( m_rowLabelWin
->IsShown() )
2257 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2259 if ( m_gridWin
->IsShown() )
2260 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2264 // this is called when the grid table sends a message to say that it
2265 // has been redimensioned
2267 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2271 switch ( msg
.GetId() )
2273 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2275 size_t pos
= msg
.GetCommandInt();
2276 int numRows
= msg
.GetCommandInt2();
2277 for ( i
= 0; i
< numRows
; i
++ )
2279 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2280 m_rowBottoms
.Insert( 0, pos
);
2282 m_numRows
+= numRows
;
2285 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2287 for ( i
= pos
; i
< m_numRows
; i
++ )
2289 bottom
+= m_rowHeights
[i
];
2290 m_rowBottoms
[i
] = bottom
;
2296 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2298 int numRows
= msg
.GetCommandInt();
2299 for ( i
= 0; i
< numRows
; i
++ )
2301 m_rowHeights
.Add( m_defaultRowHeight
);
2302 m_rowBottoms
.Add( 0 );
2305 int oldNumRows
= m_numRows
;
2306 m_numRows
+= numRows
;
2309 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2311 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2313 bottom
+= m_rowHeights
[i
];
2314 m_rowBottoms
[i
] = bottom
;
2320 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2322 size_t pos
= msg
.GetCommandInt();
2323 int numRows
= msg
.GetCommandInt2();
2324 for ( i
= 0; i
< numRows
; i
++ )
2326 m_rowHeights
.Remove( pos
);
2327 m_rowBottoms
.Remove( pos
);
2329 m_numRows
-= numRows
;
2334 m_colWidths
.Clear();
2335 m_colRights
.Clear();
2336 m_currentCellCoords
= wxGridNoCellCoords
;
2340 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2341 m_currentCellCoords
.Set( 0, 0 );
2344 for ( i
= 0; i
< m_numRows
; i
++ )
2346 h
+= m_rowHeights
[i
];
2347 m_rowBottoms
[i
] = h
;
2355 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2357 size_t pos
= msg
.GetCommandInt();
2358 int numCols
= msg
.GetCommandInt2();
2359 for ( i
= 0; i
< numCols
; i
++ )
2361 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2362 m_colRights
.Insert( 0, pos
);
2364 m_numCols
+= numCols
;
2367 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2369 for ( i
= pos
; i
< m_numCols
; i
++ )
2371 right
+= m_colWidths
[i
];
2372 m_colRights
[i
] = right
;
2378 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2380 int numCols
= msg
.GetCommandInt();
2381 for ( i
= 0; i
< numCols
; i
++ )
2383 m_colWidths
.Add( m_defaultColWidth
);
2384 m_colRights
.Add( 0 );
2387 int oldNumCols
= m_numCols
;
2388 m_numCols
+= numCols
;
2391 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2393 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2395 right
+= m_colWidths
[i
];
2396 m_colRights
[i
] = right
;
2402 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2404 size_t pos
= msg
.GetCommandInt();
2405 int numCols
= msg
.GetCommandInt2();
2406 for ( i
= 0; i
< numCols
; i
++ )
2408 m_colWidths
.Remove( pos
);
2409 m_colRights
.Remove( pos
);
2411 m_numCols
-= numCols
;
2415 #if 0 // leave the row alone here so that AppendCols will work subsequently
2417 m_rowHeights
.Clear();
2418 m_rowBottoms
.Clear();
2420 m_currentCellCoords
= wxGridNoCellCoords
;
2424 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2425 m_currentCellCoords
.Set( 0, 0 );
2428 for ( i
= 0; i
< m_numCols
; i
++ )
2430 w
+= m_colWidths
[i
];
2443 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2445 wxRegionIterator
iter( reg
);
2448 m_rowLabelsExposed
.Empty();
2455 // TODO: remove this when we can...
2456 // There is a bug in wxMotif that gives garbage update
2457 // rectangles if you jump-scroll a long way by clicking the
2458 // scrollbar with middle button. This is a work-around
2460 #if defined(__WXMOTIF__)
2462 m_gridWin
->GetClientSize( &cw
, &ch
);
2463 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2464 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2467 // logical bounds of update region
2470 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2471 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2473 // find the row labels within these bounds
2477 for ( row
= 0; row
< m_numRows
; row
++ )
2479 if ( m_rowBottoms
[row
] < top
) continue;
2481 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2482 if ( rowTop
> bottom
) break;
2484 m_rowLabelsExposed
.Add( row
);
2492 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2494 wxRegionIterator
iter( reg
);
2497 m_colLabelsExposed
.Empty();
2504 // TODO: remove this when we can...
2505 // There is a bug in wxMotif that gives garbage update
2506 // rectangles if you jump-scroll a long way by clicking the
2507 // scrollbar with middle button. This is a work-around
2509 #if defined(__WXMOTIF__)
2511 m_gridWin
->GetClientSize( &cw
, &ch
);
2512 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2513 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2516 // logical bounds of update region
2519 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2520 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2522 // find the cells within these bounds
2526 for ( col
= 0; col
< m_numCols
; col
++ )
2528 if ( m_colRights
[col
] < left
) continue;
2530 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2531 if ( colLeft
> right
) break;
2533 m_colLabelsExposed
.Add( col
);
2541 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2543 wxRegionIterator
iter( reg
);
2546 m_cellsExposed
.Empty();
2547 m_rowsExposed
.Empty();
2548 m_colsExposed
.Empty();
2550 int left
, top
, right
, bottom
;
2555 // TODO: remove this when we can...
2556 // There is a bug in wxMotif that gives garbage update
2557 // rectangles if you jump-scroll a long way by clicking the
2558 // scrollbar with middle button. This is a work-around
2560 #if defined(__WXMOTIF__)
2562 m_gridWin
->GetClientSize( &cw
, &ch
);
2563 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2564 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2565 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2566 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2569 // logical bounds of update region
2571 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2572 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2574 // find the cells within these bounds
2577 int colLeft
, rowTop
;
2578 for ( row
= 0; row
< m_numRows
; row
++ )
2580 if ( m_rowBottoms
[row
] <= top
) continue;
2582 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2583 if ( rowTop
> bottom
) break;
2585 m_rowsExposed
.Add( row
);
2587 for ( col
= 0; col
< m_numCols
; col
++ )
2589 if ( m_colRights
[col
] <= left
) continue;
2591 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2592 if ( colLeft
> right
) break;
2594 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2595 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2604 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2607 wxPoint
pos( event
.GetPosition() );
2608 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2610 if ( event
.Dragging() )
2612 m_isDragging
= TRUE
;
2614 if ( event
.LeftIsDown() )
2616 switch( m_cursorMode
)
2618 case WXGRID_CURSOR_RESIZE_ROW
:
2620 int cw
, ch
, left
, dummy
;
2621 m_gridWin
->GetClientSize( &cw
, &ch
);
2622 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2624 wxClientDC
dc( m_gridWin
);
2627 m_rowBottoms
[m_dragRowOrCol
] -
2628 m_rowHeights
[m_dragRowOrCol
] +
2629 WXGRID_MIN_ROW_HEIGHT
);
2630 dc
.SetLogicalFunction(wxINVERT
);
2631 if ( m_dragLastPos
>= 0 )
2633 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2635 dc
.DrawLine( left
, y
, left
+cw
, y
);
2640 case WXGRID_CURSOR_SELECT_ROW
:
2641 if ( (row
= YToRow( y
)) >= 0 &&
2642 !IsInSelection( row
, 0 ) )
2644 SelectRow( row
, TRUE
);
2647 // default label to suppress warnings about "enumeration value
2648 // 'xxx' not handled in switch
2656 m_isDragging
= FALSE
;
2659 // ------------ Entering or leaving the window
2661 if ( event
.Entering() || event
.Leaving() )
2663 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2667 // ------------ Left button pressed
2669 else if ( event
.LeftDown() )
2671 // don't send a label click event for a hit on the
2672 // edge of the row label - this is probably the user
2673 // wanting to resize the row
2675 if ( YToEdgeOfRow(y
) < 0 )
2679 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2681 SelectRow( row
, event
.ShiftDown() );
2682 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2687 // starting to drag-resize a row
2689 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2694 // ------------ Left double click
2696 else if (event
.LeftDClick() )
2698 if ( YToEdgeOfRow(y
) < 0 )
2701 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2706 // ------------ Left button released
2708 else if ( event
.LeftUp() )
2710 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2712 DoEndDragResizeRow();
2714 // Note: we are ending the event *after* doing
2715 // default processing in this case
2717 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2720 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2725 // ------------ Right button down
2727 else if ( event
.RightDown() )
2730 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2732 // no default action at the moment
2737 // ------------ Right double click
2739 else if ( event
.RightDClick() )
2742 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2744 // no default action at the moment
2749 // ------------ No buttons down and mouse moving
2751 else if ( event
.Moving() )
2753 m_dragRowOrCol
= YToEdgeOfRow( y
);
2754 if ( m_dragRowOrCol
>= 0 )
2756 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2758 // don't capture the mouse yet
2759 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2762 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2764 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2770 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2773 wxPoint
pos( event
.GetPosition() );
2774 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2776 if ( event
.Dragging() )
2778 m_isDragging
= TRUE
;
2780 if ( event
.LeftIsDown() )
2782 switch( m_cursorMode
)
2784 case WXGRID_CURSOR_RESIZE_COL
:
2786 int cw
, ch
, dummy
, top
;
2787 m_gridWin
->GetClientSize( &cw
, &ch
);
2788 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2790 wxClientDC
dc( m_gridWin
);
2793 m_colRights
[m_dragRowOrCol
] -
2794 m_colWidths
[m_dragRowOrCol
] +
2795 WXGRID_MIN_COL_WIDTH
);
2796 dc
.SetLogicalFunction(wxINVERT
);
2797 if ( m_dragLastPos
>= 0 )
2799 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2801 dc
.DrawLine( x
, top
, x
, top
+ch
);
2806 case WXGRID_CURSOR_SELECT_COL
:
2807 if ( (col
= XToCol( x
)) >= 0 &&
2808 !IsInSelection( 0, col
) )
2810 SelectCol( col
, TRUE
);
2813 // default label to suppress warnings about "enumeration value
2814 // 'xxx' not handled in switch
2822 m_isDragging
= FALSE
;
2825 // ------------ Entering or leaving the window
2827 if ( event
.Entering() || event
.Leaving() )
2829 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2833 // ------------ Left button pressed
2835 else if ( event
.LeftDown() )
2837 // don't send a label click event for a hit on the
2838 // edge of the col label - this is probably the user
2839 // wanting to resize the col
2841 if ( XToEdgeOfCol(x
) < 0 )
2845 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2847 SelectCol( col
, event
.ShiftDown() );
2848 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2853 // starting to drag-resize a col
2855 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2860 // ------------ Left double click
2862 if ( event
.LeftDClick() )
2864 if ( XToEdgeOfCol(x
) < 0 )
2867 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2872 // ------------ Left button released
2874 else if ( event
.LeftUp() )
2876 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2878 DoEndDragResizeCol();
2880 // Note: we are ending the event *after* doing
2881 // default processing in this case
2883 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2886 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2891 // ------------ Right button down
2893 else if ( event
.RightDown() )
2896 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2898 // no default action at the moment
2903 // ------------ Right double click
2905 else if ( event
.RightDClick() )
2908 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2910 // no default action at the moment
2915 // ------------ No buttons down and mouse moving
2917 else if ( event
.Moving() )
2919 m_dragRowOrCol
= XToEdgeOfCol( x
);
2920 if ( m_dragRowOrCol
>= 0 )
2922 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2924 // don't capture the cursor yet
2925 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2928 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2930 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2936 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2938 if ( event
.LeftDown() )
2940 // indicate corner label by having both row and
2943 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2949 else if ( event
.LeftDClick() )
2951 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2954 else if ( event
.RightDown() )
2956 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2958 // no default action at the moment
2962 else if ( event
.RightDClick() )
2964 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2966 // no default action at the moment
2971 void wxGrid::ChangeCursorMode(CursorMode mode
,
2976 static const wxChar
*cursorModes
[] =
2985 wxLogTrace(_T("grid"),
2986 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2987 win
== m_colLabelWin
? _T("colLabelWin")
2988 : win
? _T("rowLabelWin")
2990 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2991 #endif // __WXDEBUG__
2993 if ( mode
== m_cursorMode
)
2998 // by default use the grid itself
3004 m_winCapture
->ReleaseMouse();
3005 m_winCapture
= (wxWindow
*)NULL
;
3008 m_cursorMode
= mode
;
3010 switch ( m_cursorMode
)
3012 case WXGRID_CURSOR_RESIZE_ROW
:
3013 win
->SetCursor( m_rowResizeCursor
);
3016 case WXGRID_CURSOR_RESIZE_COL
:
3017 win
->SetCursor( m_colResizeCursor
);
3021 win
->SetCursor( *wxSTANDARD_CURSOR
);
3024 // we need to capture mouse when resizing
3025 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3026 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3028 if ( captureMouse
&& resize
)
3030 win
->CaptureMouse();
3035 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3038 wxPoint
pos( event
.GetPosition() );
3039 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3041 wxGridCellCoords coords
;
3042 XYToCell( x
, y
, coords
);
3044 if ( event
.Dragging() )
3046 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3048 // Don't start doing anything until the mouse has been drug at
3049 // least 3 pixels in any direction...
3052 if (m_startDragPos
== wxDefaultPosition
)
3054 m_startDragPos
= pos
;
3057 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3061 m_isDragging
= TRUE
;
3062 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3064 // Hide the edit control, so it
3065 // won't interfer with drag-shrinking.
3066 if ( IsCellEditControlEnabled() )
3067 HideCellEditControl();
3069 // Have we captured the mouse yet?
3072 m_winCapture
= m_gridWin
;
3073 m_winCapture
->CaptureMouse();
3076 if ( coords
!= wxGridNoCellCoords
)
3078 if ( !IsSelection() )
3080 SelectBlock( coords
, coords
);
3084 SelectBlock( m_currentCellCoords
, coords
);
3087 if (! IsVisible(coords
))
3089 MakeCellVisible(coords
);
3090 // TODO: need to introduce a delay or something here. The
3091 // scrolling is way to fast, at least on MSW.
3095 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3097 int cw
, ch
, left
, dummy
;
3098 m_gridWin
->GetClientSize( &cw
, &ch
);
3099 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3101 wxClientDC
dc( m_gridWin
);
3104 m_rowBottoms
[m_dragRowOrCol
] -
3105 m_rowHeights
[m_dragRowOrCol
] +
3106 WXGRID_MIN_ROW_HEIGHT
);
3107 dc
.SetLogicalFunction(wxINVERT
);
3108 if ( m_dragLastPos
>= 0 )
3110 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3112 dc
.DrawLine( left
, y
, left
+cw
, y
);
3115 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3117 int cw
, ch
, dummy
, top
;
3118 m_gridWin
->GetClientSize( &cw
, &ch
);
3119 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3121 wxClientDC
dc( m_gridWin
);
3124 m_colRights
[m_dragRowOrCol
] -
3125 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
3126 dc
.SetLogicalFunction(wxINVERT
);
3127 if ( m_dragLastPos
>= 0 )
3129 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3131 dc
.DrawLine( x
, top
, x
, top
+ch
);
3138 m_isDragging
= FALSE
;
3139 m_startDragPos
= wxDefaultPosition
;
3142 if ( coords
!= wxGridNoCellCoords
)
3144 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3145 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3148 if ( event
.Entering() || event
.Leaving() )
3150 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3151 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3156 // ------------ Left button pressed
3158 if ( event
.LeftDown() )
3160 DisableCellEditControl();
3161 if ( event
.ShiftDown() )
3163 SelectBlock( m_currentCellCoords
, coords
);
3165 else if ( XToEdgeOfCol(x
) < 0 &&
3166 YToEdgeOfRow(y
) < 0 )
3168 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3173 MakeCellVisible( coords
);
3175 // if this is the second click on this cell then start
3177 if ( m_waitForSlowClick
&&
3178 (coords
== m_currentCellCoords
) &&
3179 CanEnableCellControl())
3181 EnableCellEditControl();
3183 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3184 attr
->GetEditor()->StartingClick();
3187 m_waitForSlowClick
= FALSE
;
3191 SetCurrentCell( coords
);
3192 m_waitForSlowClick
= TRUE
;
3199 // ------------ Left double click
3201 else if ( event
.LeftDClick() )
3203 DisableCellEditControl();
3204 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3206 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3214 // ------------ Left button released
3216 else if ( event
.LeftUp() )
3218 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3220 if ( IsSelection() )
3224 m_winCapture
->ReleaseMouse();
3225 m_winCapture
= NULL
;
3227 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3230 // Show the edit control, if it has been hidden for
3232 ShowCellEditControl();
3234 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3236 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3237 DoEndDragResizeRow();
3239 // Note: we are ending the event *after* doing
3240 // default processing in this case
3242 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3244 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3246 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3247 DoEndDragResizeCol();
3249 // Note: we are ending the event *after* doing
3250 // default processing in this case
3252 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3259 // ------------ Right button down
3261 else if ( event
.RightDown() )
3263 DisableCellEditControl();
3264 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3269 // no default action at the moment
3274 // ------------ Right double click
3276 else if ( event
.RightDClick() )
3278 DisableCellEditControl();
3279 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3284 // no default action at the moment
3288 // ------------ Moving and no button action
3290 else if ( event
.Moving() && !event
.IsButton() )
3292 int dragRow
= YToEdgeOfRow( y
);
3293 int dragCol
= XToEdgeOfCol( x
);
3295 // Dragging on the corner of a cell to resize in both
3296 // directions is not implemented yet...
3298 if ( dragRow
>= 0 && dragCol
>= 0 )
3300 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3306 m_dragRowOrCol
= dragRow
;
3308 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3310 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3318 m_dragRowOrCol
= dragCol
;
3320 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3322 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3328 // Neither on a row or col edge
3330 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3332 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3339 void wxGrid::DoEndDragResizeRow()
3341 if ( m_dragLastPos
>= 0 )
3343 // erase the last line and resize the row
3345 int cw
, ch
, left
, dummy
;
3346 m_gridWin
->GetClientSize( &cw
, &ch
);
3347 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3349 wxClientDC
dc( m_gridWin
);
3351 dc
.SetLogicalFunction( wxINVERT
);
3352 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3353 HideCellEditControl();
3355 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3356 SetRowSize( m_dragRowOrCol
,
3357 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3359 if ( !GetBatchCount() )
3361 // Only needed to get the correct rect.y:
3362 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3364 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3365 rect
.width
= m_rowLabelWidth
;
3366 rect
.height
= ch
- rect
.y
;
3367 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3369 m_gridWin
->Refresh( FALSE
, &rect
);
3372 ShowCellEditControl();
3377 void wxGrid::DoEndDragResizeCol()
3379 if ( m_dragLastPos
>= 0 )
3381 // erase the last line and resize the col
3383 int cw
, ch
, dummy
, top
;
3384 m_gridWin
->GetClientSize( &cw
, &ch
);
3385 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3387 wxClientDC
dc( m_gridWin
);
3389 dc
.SetLogicalFunction( wxINVERT
);
3390 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3391 HideCellEditControl();
3393 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3394 SetColSize( m_dragRowOrCol
,
3395 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3397 if ( !GetBatchCount() )
3399 // Only needed to get the correct rect.x:
3400 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3402 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3403 rect
.width
= cw
- rect
.x
;
3404 rect
.height
= m_colLabelHeight
;
3405 m_colLabelWin
->Refresh( TRUE
, &rect
);
3407 m_gridWin
->Refresh( FALSE
, &rect
);
3410 ShowCellEditControl();
3417 // ------ interaction with data model
3419 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3421 switch ( msg
.GetId() )
3423 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3424 return GetModelValues();
3426 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3427 return SetModelValues();
3429 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3430 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3431 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3432 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3433 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3434 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3435 return Redimension( msg
);
3444 // The behaviour of this function depends on the grid table class
3445 // Clear() function. For the default wxGridStringTable class the
3446 // behavious is to replace all cell contents with wxEmptyString but
3447 // not to change the number of rows or cols.
3449 void wxGrid::ClearGrid()
3454 SetEditControlValue();
3455 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3460 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3462 // TODO: something with updateLabels flag
3466 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3472 if (IsCellEditControlEnabled())
3473 DisableCellEditControl();
3475 bool ok
= m_table
->InsertRows( pos
, numRows
);
3477 // the table will have sent the results of the insert row
3478 // operation to this view object as a grid table message
3482 if ( m_numCols
== 0 )
3484 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3486 // TODO: perhaps instead of appending the default number of cols
3487 // we should remember what the last non-zero number of cols was ?
3491 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3493 // if we have just inserted cols into an empty grid the current
3494 // cell will be undefined...
3496 SetCurrentCell( 0, 0 );
3500 if ( !GetBatchCount() ) Refresh();
3503 SetEditControlValue();
3513 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3515 // TODO: something with updateLabels flag
3519 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3523 if ( m_table
&& m_table
->AppendRows( numRows
) )
3525 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3527 // if we have just inserted cols into an empty grid the current
3528 // cell will be undefined...
3530 SetCurrentCell( 0, 0 );
3533 // the table will have sent the results of the append row
3534 // operation to this view object as a grid table message
3537 if ( !GetBatchCount() ) Refresh();
3547 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3549 // TODO: something with updateLabels flag
3553 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3559 if (IsCellEditControlEnabled())
3560 DisableCellEditControl();
3562 if (m_table
->DeleteRows( pos
, numRows
))
3565 // the table will have sent the results of the delete row
3566 // operation to this view object as a grid table message
3569 if ( !GetBatchCount() ) Refresh();
3577 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3579 // TODO: something with updateLabels flag
3583 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3589 if (IsCellEditControlEnabled())
3590 DisableCellEditControl();
3592 bool ok
= m_table
->InsertCols( pos
, numCols
);
3594 // the table will have sent the results of the insert col
3595 // operation to this view object as a grid table message
3599 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3601 // if we have just inserted cols into an empty grid the current
3602 // cell will be undefined...
3604 SetCurrentCell( 0, 0 );
3608 if ( !GetBatchCount() ) Refresh();
3611 SetEditControlValue();
3621 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3623 // TODO: something with updateLabels flag
3627 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3631 if ( m_table
&& m_table
->AppendCols( numCols
) )
3633 // the table will have sent the results of the append col
3634 // operation to this view object as a grid table message
3636 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3638 // if we have just inserted cols into an empty grid the current
3639 // cell will be undefined...
3641 SetCurrentCell( 0, 0 );
3645 if ( !GetBatchCount() ) Refresh();
3655 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3657 // TODO: something with updateLabels flag
3661 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3667 if (IsCellEditControlEnabled())
3668 DisableCellEditControl();
3670 if ( m_table
->DeleteCols( pos
, numCols
) )
3672 // the table will have sent the results of the delete col
3673 // operation to this view object as a grid table message
3676 if ( !GetBatchCount() ) Refresh();
3686 // ----- event handlers
3689 // Generate a grid event based on a mouse event and
3690 // return the result of ProcessEvent()
3692 bool wxGrid::SendEvent( const wxEventType type
,
3694 wxMouseEvent
& mouseEv
)
3696 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3698 int rowOrCol
= (row
== -1 ? col
: row
);
3700 wxGridSizeEvent
gridEvt( GetId(),
3704 mouseEv
.GetX(), mouseEv
.GetY(),
3705 mouseEv
.ControlDown(),
3706 mouseEv
.ShiftDown(),
3708 mouseEv
.MetaDown() );
3710 return GetEventHandler()->ProcessEvent(gridEvt
);
3712 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3714 wxGridRangeSelectEvent
gridEvt( GetId(),
3718 m_selectedBottomRight
,
3719 mouseEv
.ControlDown(),
3720 mouseEv
.ShiftDown(),
3722 mouseEv
.MetaDown() );
3724 return GetEventHandler()->ProcessEvent(gridEvt
);
3728 wxGridEvent
gridEvt( GetId(),
3732 mouseEv
.GetX(), mouseEv
.GetY(),
3733 mouseEv
.ControlDown(),
3734 mouseEv
.ShiftDown(),
3736 mouseEv
.MetaDown() );
3738 return GetEventHandler()->ProcessEvent(gridEvt
);
3743 // Generate a grid event of specified type and return the result
3744 // of ProcessEvent().
3746 bool wxGrid::SendEvent( const wxEventType type
,
3749 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3751 int rowOrCol
= (row
== -1 ? col
: row
);
3753 wxGridSizeEvent
gridEvt( GetId(),
3758 return GetEventHandler()->ProcessEvent(gridEvt
);
3762 wxGridEvent
gridEvt( GetId(),
3767 return GetEventHandler()->ProcessEvent(gridEvt
);
3772 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3774 wxPaintDC
dc( this );
3776 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3777 m_numRows
&& m_numCols
)
3779 m_currentCellCoords
.Set(0, 0);
3780 SetEditControlValue();
3781 ShowCellEditControl();
3788 // This is just here to make sure that CalcDimensions gets called when
3789 // the grid view is resized... then the size event is skipped to allow
3790 // the box sizers to handle everything
3792 void wxGrid::OnSize( wxSizeEvent
& event
)
3799 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3801 if ( m_inOnKeyDown
)
3803 // shouldn't be here - we are going round in circles...
3805 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3808 m_inOnKeyDown
= TRUE
;
3810 // propagate the event up and see if it gets processed
3812 wxWindow
*parent
= GetParent();
3813 wxKeyEvent
keyEvt( event
);
3814 keyEvt
.SetEventObject( parent
);
3816 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3819 // TODO: Should also support Shift-cursor keys for
3820 // extending the selection. Maybe add a flag to
3821 // MoveCursorXXX() and MoveCursorXXXBlock() and
3822 // just send event.ShiftDown().
3824 // try local handlers
3826 switch ( event
.KeyCode() )
3829 if ( event
.ControlDown() )
3831 MoveCursorUpBlock();
3840 if ( event
.ControlDown() )
3842 MoveCursorDownBlock();
3851 if ( event
.ControlDown() )
3853 MoveCursorLeftBlock();
3862 if ( event
.ControlDown() )
3864 MoveCursorRightBlock();
3873 if ( event
.ControlDown() )
3875 event
.Skip(); // to let the edit control have the return
3884 if (event
.ShiftDown())
3891 if ( event
.ControlDown() )
3893 MakeCellVisible( 0, 0 );
3894 SetCurrentCell( 0, 0 );
3903 if ( event
.ControlDown() )
3905 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3906 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3922 // We don't want these keys to trigger the edit control, any others?
3931 if ( !IsEditable() )
3936 // Otherwise fall through to default
3939 // now try the cell edit control
3941 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
3943 EnableCellEditControl();
3944 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3945 attr
->GetEditor()->StartingKey(event
);
3952 m_inOnKeyDown
= FALSE
;
3956 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3960 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3962 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3964 // the event has been intercepted - do nothing
3969 m_currentCellCoords
!= wxGridNoCellCoords
)
3971 HideCellEditControl();
3972 SaveEditControlValue();
3973 DisableCellEditControl();
3975 // Clear the old current cell highlight
3976 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3978 // Otherwise refresh redraws the highlight!
3979 m_currentCellCoords
= coords
;
3981 m_gridWin
->Refresh( FALSE
, &r
);
3984 m_currentCellCoords
= coords
;
3986 SetEditControlValue();
3990 wxClientDC
dc(m_gridWin
);
3993 wxGridCellAttr
* attr
= GetCellAttr(coords
);
3994 DrawCellHighlight(dc
, attr
);
3997 if ( IsSelection() )
3999 wxRect
r( SelectionToDeviceRect() );
4001 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4008 // ------ functions to get/send data (see also public functions)
4011 bool wxGrid::GetModelValues()
4015 // all we need to do is repaint the grid
4017 m_gridWin
->Refresh();
4025 bool wxGrid::SetModelValues()
4031 for ( row
= 0; row
< m_numRows
; row
++ )
4033 for ( col
= 0; col
< m_numCols
; col
++ )
4035 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4047 // Note - this function only draws cells that are in the list of
4048 // exposed cells (usually set from the update region by
4049 // CalcExposedCells)
4051 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4053 if ( !m_numRows
|| !m_numCols
) return;
4056 size_t numCells
= m_cellsExposed
.GetCount();
4058 for ( i
= 0; i
< numCells
; i
++ )
4060 DrawCell( dc
, m_cellsExposed
[i
] );
4065 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4067 int row
= coords
.GetRow();
4068 int col
= coords
.GetCol();
4070 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4073 // we draw the cell border ourselves
4074 #if !WXGRID_DRAW_LINES
4075 if ( m_gridLinesEnabled
)
4076 DrawCellBorder( dc
, coords
);
4079 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4081 bool isCurrent
= coords
== m_currentCellCoords
;
4084 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4085 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4086 rect
.width
= m_colWidths
[col
] - 1;
4087 rect
.height
= m_rowHeights
[row
] - 1;
4089 // if the editor is shown, we should use it and not the renderer
4090 if ( isCurrent
&& IsCellEditControlEnabled() )
4092 attr
->GetEditor()->PaintBackground(rect
, attr
);
4096 // but all the rest is drawn by the cell renderer and hence may be
4098 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4102 DrawCellHighlight(dc
, attr
);
4109 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4111 int row
= m_currentCellCoords
.GetRow();
4112 int col
= m_currentCellCoords
.GetCol();
4114 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4118 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4119 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4120 rect
.width
= m_colWidths
[col
] - 1;
4121 rect
.height
= m_rowHeights
[row
] - 1;
4123 if ( attr
->IsReadOnly() )
4125 // hmmm... what could we do here to show that the cell is disabled?
4126 // for now, I just draw a thinner border than for the other ones, but
4127 // it doesn't look really good
4128 dc
.SetPen(wxPen(m_gridLineColour
, 2, wxSOLID
));
4129 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4131 dc
.DrawRectangle(rect
);
4135 // VZ: my experiments with 3d borders...
4137 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
4138 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4140 dc
.DrawRectangle(rect
);
4142 // FIXME we should properly set colours for arbitrary bg
4143 wxCoord x1
= rect
.x
,
4145 x2
= rect
.x
+ rect
.width
,
4146 y2
= rect
.y
+ rect
.height
;
4148 dc
.SetPen(*wxWHITE_PEN
);
4149 dc
.DrawLine(x1
, y1
, x2
- 1, y1
);
4150 dc
.DrawLine(x1
, y1
, x1
, y2
- 1);
4152 dc
.SetPen(*wxLIGHT_GREY_PEN
);
4153 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4154 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
- 1);
4156 dc
.SetPen(*wxBLACK_PEN
);
4157 dc
.DrawLine(x1
, y2
, x2
, y2
);
4158 dc
.DrawLine(x2
, y1
, x2
, y2
);
4163 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4165 if ( m_colWidths
[coords
.GetCol()] <=0 ||
4166 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
4168 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4169 int row
= coords
.GetRow();
4170 int col
= coords
.GetCol();
4172 // right hand border
4174 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
4175 m_colRights
[col
], m_rowBottoms
[row
] );
4179 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
4180 m_colRights
[col
], m_rowBottoms
[row
] );
4184 // TODO: remove this ???
4185 // This is used to redraw all grid lines e.g. when the grid line colour
4188 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4190 if ( !m_gridLinesEnabled
||
4192 !m_numCols
) return;
4194 int top
, bottom
, left
, right
;
4199 m_gridWin
->GetClientSize(&cw
, &ch
);
4201 // virtual coords of visible area
4203 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4204 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4209 reg
.GetBox(x
, y
, w
, h
);
4210 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4211 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4214 // avoid drawing grid lines past the last row and col
4216 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
4217 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
4219 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4221 // horizontal grid lines
4224 for ( i
= 0; i
< m_numRows
; i
++ )
4226 if ( m_rowBottoms
[i
]-1 > bottom
)
4230 else if ( m_rowBottoms
[i
]-1 >= top
)
4232 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
4237 // vertical grid lines
4239 for ( i
= 0; i
< m_numCols
; i
++ )
4241 if ( m_colRights
[i
]-1 > right
)
4245 else if ( m_colRights
[i
]-1 >= left
)
4247 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
4253 void wxGrid::DrawRowLabels( wxDC
& dc
)
4255 if ( !m_numRows
|| !m_numCols
) return;
4258 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4260 for ( i
= 0; i
< numLabels
; i
++ )
4262 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4267 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4269 if ( m_rowHeights
[row
] <= 0 ) return;
4271 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4273 dc
.SetPen( *wxBLACK_PEN
);
4274 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4275 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4277 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4278 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4280 dc
.SetPen( *wxWHITE_PEN
);
4281 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4282 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4284 dc
.SetBackgroundMode( wxTRANSPARENT
);
4285 dc
.SetTextForeground( GetLabelTextColour() );
4286 dc
.SetFont( GetLabelFont() );
4289 GetRowLabelAlignment( &hAlign
, &vAlign
);
4293 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4294 rect
.SetWidth( m_rowLabelWidth
- 4 );
4295 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4296 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4300 void wxGrid::DrawColLabels( wxDC
& dc
)
4302 if ( !m_numRows
|| !m_numCols
) return;
4305 size_t numLabels
= m_colLabelsExposed
.GetCount();
4307 for ( i
= 0; i
< numLabels
; i
++ )
4309 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4314 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4316 if ( m_colWidths
[col
] <= 0 ) return;
4318 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4320 dc
.SetPen( *wxBLACK_PEN
);
4321 dc
.DrawLine( m_colRights
[col
]-1, 0,
4322 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4324 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4325 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4327 dc
.SetPen( *wxWHITE_PEN
);
4328 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4329 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4331 dc
.SetBackgroundMode( wxTRANSPARENT
);
4332 dc
.SetTextForeground( GetLabelTextColour() );
4333 dc
.SetFont( GetLabelFont() );
4335 dc
.SetBackgroundMode( wxTRANSPARENT
);
4336 dc
.SetTextForeground( GetLabelTextColour() );
4337 dc
.SetFont( GetLabelFont() );
4340 GetColLabelAlignment( &hAlign
, &vAlign
);
4343 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4345 rect
.SetWidth( m_colWidths
[col
] - 4 );
4346 rect
.SetHeight( m_colLabelHeight
- 4 );
4347 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4351 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4352 const wxString
& value
,
4357 long textWidth
, textHeight
;
4358 long lineWidth
, lineHeight
;
4359 wxArrayString lines
;
4361 dc
.SetClippingRegion( rect
);
4362 StringToLines( value
, lines
);
4363 if ( lines
.GetCount() )
4365 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4366 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4369 switch ( horizAlign
)
4372 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4376 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4385 switch ( vertAlign
)
4388 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4392 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4401 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4403 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4408 dc
.DestroyClippingRegion();
4412 // Split multi line text up into an array of strings. Any existing
4413 // contents of the string array are preserved.
4415 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4419 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4420 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4422 while ( startPos
< (int)tVal
.Length() )
4424 pos
= tVal
.Mid(startPos
).Find( eol
);
4429 else if ( pos
== 0 )
4431 lines
.Add( wxEmptyString
);
4435 lines
.Add( value
.Mid(startPos
, pos
) );
4439 if ( startPos
< (int)value
.Length() )
4441 lines
.Add( value
.Mid( startPos
) );
4446 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4447 wxArrayString
& lines
,
4448 long *width
, long *height
)
4455 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4457 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4458 w
= wxMax( w
, lineW
);
4468 // ------ Edit control functions
4472 void wxGrid::EnableEditing( bool edit
)
4474 // TODO: improve this ?
4476 if ( edit
!= m_editable
)
4480 // FIXME IMHO this won't disable the edit control if edit == FALSE
4481 // because of the check in the beginning of
4482 // EnableCellEditControl() just below (VZ)
4483 EnableCellEditControl(m_editable
);
4488 void wxGrid::EnableCellEditControl( bool enable
)
4493 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4494 SetCurrentCell( 0, 0 );
4496 if ( enable
!= m_cellEditCtrlEnabled
)
4498 // TODO allow the app to Veto() this event?
4499 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4503 // this should be checked by the caller!
4504 wxASSERT_MSG( CanEnableCellControl(),
4505 _T("can't enable editing for this cell!") );
4507 // do it before ShowCellEditControl()
4508 m_cellEditCtrlEnabled
= enable
;
4510 SetEditControlValue();
4511 ShowCellEditControl();
4515 HideCellEditControl();
4516 SaveEditControlValue();
4518 // do it after HideCellEditControl()
4519 m_cellEditCtrlEnabled
= enable
;
4524 bool wxGrid::IsCurrentCellReadOnly() const
4527 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4528 bool readonly
= attr
->IsReadOnly();
4534 bool wxGrid::CanEnableCellControl() const
4536 return m_editable
&& !IsCurrentCellReadOnly();
4539 bool wxGrid::IsCellEditControlEnabled() const
4541 // the cell edit control might be disable for all cells or just for the
4542 // current one if it's read only
4543 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
4546 wxWindow
*wxGrid::GetGridWindow() const
4551 void wxGrid::ShowCellEditControl()
4553 if ( IsCellEditControlEnabled() )
4555 if ( !IsVisible( m_currentCellCoords
) )
4561 wxRect rect
= CellToRect( m_currentCellCoords
);
4562 int row
= m_currentCellCoords
.GetRow();
4563 int col
= m_currentCellCoords
.GetCol();
4565 // convert to scrolled coords
4567 int left
, top
, right
, bottom
;
4568 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4569 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4571 // cell is shifted by one pixel
4577 // Make the edit control large enough to allow for internal
4580 // TODO: remove this if the text ctrl sizing is improved esp. for
4584 #if defined(__WXMOTIF__)
4585 if ( row
== 0 || col
== 0 )
4594 if ( row
== 0 || col
== 0 )
4604 #if defined(__WXGTK__)
4607 if (left
!= 0) left_diff
++;
4608 if (top
!= 0) top_diff
++;
4609 rect
.SetLeft( left
+ left_diff
);
4610 rect
.SetTop( top
+ top_diff
);
4611 rect
.SetRight( rect
.GetRight() - left_diff
);
4612 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4614 rect
.SetLeft( wxMax(0, left
- extra
) );
4615 rect
.SetTop( wxMax(0, top
- extra
) );
4616 rect
.SetRight( rect
.GetRight() + 2*extra
);
4617 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4620 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4621 wxGridCellEditor
* editor
= attr
->GetEditor();
4622 if ( !editor
->IsCreated() )
4624 editor
->Create(m_gridWin
, -1,
4625 new wxGridCellEditorEvtHandler(this, editor
));
4628 editor
->SetSize( rect
);
4629 editor
->Show( TRUE
, attr
);
4630 editor
->BeginEdit(row
, col
, this);
4637 void wxGrid::HideCellEditControl()
4639 if ( IsCellEditControlEnabled() )
4641 int row
= m_currentCellCoords
.GetRow();
4642 int col
= m_currentCellCoords
.GetCol();
4644 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4645 attr
->GetEditor()->Show( FALSE
);
4647 m_gridWin
->SetFocus();
4652 void wxGrid::SetEditControlValue( const wxString
& value
)
4654 // RD: The new Editors get the value from the table themselves now. This
4655 // method can probably be removed...
4659 void wxGrid::SaveEditControlValue()
4661 if ( IsCellEditControlEnabled() )
4663 int row
= m_currentCellCoords
.GetRow();
4664 int col
= m_currentCellCoords
.GetCol();
4666 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4667 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4673 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4674 m_currentCellCoords
.GetRow(),
4675 m_currentCellCoords
.GetCol() );
4682 // ------ Grid location functions
4683 // Note that all of these functions work with the logical coordinates of
4684 // grid cells and labels so you will need to convert from device
4685 // coordinates for mouse events etc.
4688 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4690 int row
= YToRow(y
);
4691 int col
= XToCol(x
);
4693 if ( row
== -1 || col
== -1 )
4695 coords
= wxGridNoCellCoords
;
4699 coords
.Set( row
, col
);
4704 int wxGrid::YToRow( int y
)
4708 for ( i
= 0; i
< m_numRows
; i
++ )
4710 if ( y
< m_rowBottoms
[i
] ) return i
;
4713 return m_numRows
; //-1;
4717 int wxGrid::XToCol( int x
)
4721 for ( i
= 0; i
< m_numCols
; i
++ )
4723 if ( x
< m_colRights
[i
] ) return i
;
4726 return m_numCols
; //-1;
4730 // return the row number that that the y coord is near the edge of, or
4731 // -1 if not near an edge
4733 int wxGrid::YToEdgeOfRow( int y
)
4737 for ( i
= 0; i
< m_numRows
; i
++ )
4739 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4741 d
= abs( y
- m_rowBottoms
[i
] );
4743 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4752 // return the col number that that the x coord is near the edge of, or
4753 // -1 if not near an edge
4755 int wxGrid::XToEdgeOfCol( int x
)
4759 for ( i
= 0; i
< m_numCols
; i
++ )
4761 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4763 d
= abs( x
- m_colRights
[i
] );
4765 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4774 wxRect
wxGrid::CellToRect( int row
, int col
)
4776 wxRect
rect( -1, -1, -1, -1 );
4778 if ( row
>= 0 && row
< m_numRows
&&
4779 col
>= 0 && col
< m_numCols
)
4781 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4782 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4783 rect
.width
= m_colWidths
[col
];
4784 rect
.height
= m_rowHeights
[ row
];
4791 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4793 // get the cell rectangle in logical coords
4795 wxRect
r( CellToRect( row
, col
) );
4797 // convert to device coords
4799 int left
, top
, right
, bottom
;
4800 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4801 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4803 // check against the client area of the grid window
4806 m_gridWin
->GetClientSize( &cw
, &ch
);
4808 if ( wholeCellVisible
)
4810 // is the cell wholly visible ?
4812 return ( left
>= 0 && right
<= cw
&&
4813 top
>= 0 && bottom
<= ch
);
4817 // is the cell partly visible ?
4819 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4820 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4825 // make the specified cell location visible by doing a minimal amount
4828 void wxGrid::MakeCellVisible( int row
, int col
)
4831 int xpos
= -1, ypos
= -1;
4833 if ( row
>= 0 && row
< m_numRows
&&
4834 col
>= 0 && col
< m_numCols
)
4836 // get the cell rectangle in logical coords
4838 wxRect
r( CellToRect( row
, col
) );
4840 // convert to device coords
4842 int left
, top
, right
, bottom
;
4843 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4844 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4847 m_gridWin
->GetClientSize( &cw
, &ch
);
4853 else if ( bottom
> ch
)
4855 int h
= r
.GetHeight();
4857 for ( i
= row
-1; i
>= 0; i
-- )
4859 if ( h
+ m_rowHeights
[i
] > ch
) break;
4861 h
+= m_rowHeights
[i
];
4862 ypos
-= m_rowHeights
[i
];
4865 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4866 // have rounding errors (this is important, because if we do, we
4867 // might not scroll at all and some cells won't be redrawn)
4868 ypos
+= GRID_SCROLL_LINE
/ 2;
4875 else if ( right
> cw
)
4877 int w
= r
.GetWidth();
4879 for ( i
= col
-1; i
>= 0; i
-- )
4881 if ( w
+ m_colWidths
[i
] > cw
) break;
4883 w
+= m_colWidths
[i
];
4884 xpos
-= m_colWidths
[i
];
4887 // see comment for ypos above
4888 xpos
+= GRID_SCROLL_LINE
/ 2;
4891 if ( xpos
!= -1 || ypos
!= -1 )
4893 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4894 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4895 Scroll( xpos
, ypos
);
4903 // ------ Grid cursor movement functions
4906 bool wxGrid::MoveCursorUp()
4908 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4909 m_currentCellCoords
.GetRow() > 0 )
4911 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4912 m_currentCellCoords
.GetCol() );
4914 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4915 m_currentCellCoords
.GetCol() );
4924 bool wxGrid::MoveCursorDown()
4926 // TODO: allow for scrolling
4928 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4929 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4931 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4932 m_currentCellCoords
.GetCol() );
4934 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4935 m_currentCellCoords
.GetCol() );
4944 bool wxGrid::MoveCursorLeft()
4946 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4947 m_currentCellCoords
.GetCol() > 0 )
4949 MakeCellVisible( m_currentCellCoords
.GetRow(),
4950 m_currentCellCoords
.GetCol() - 1 );
4952 SetCurrentCell( m_currentCellCoords
.GetRow(),
4953 m_currentCellCoords
.GetCol() - 1 );
4962 bool wxGrid::MoveCursorRight()
4964 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4965 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4967 MakeCellVisible( m_currentCellCoords
.GetRow(),
4968 m_currentCellCoords
.GetCol() + 1 );
4970 SetCurrentCell( m_currentCellCoords
.GetRow(),
4971 m_currentCellCoords
.GetCol() + 1 );
4980 bool wxGrid::MovePageUp()
4982 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4984 int row
= m_currentCellCoords
.GetRow();
4988 m_gridWin
->GetClientSize( &cw
, &ch
);
4990 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4991 int newRow
= YToRow( y
- ch
+ 1 );
4996 else if ( newRow
== row
)
5001 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5002 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5010 bool wxGrid::MovePageDown()
5012 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5014 int row
= m_currentCellCoords
.GetRow();
5015 if ( row
< m_numRows
)
5018 m_gridWin
->GetClientSize( &cw
, &ch
);
5020 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
5021 int newRow
= YToRow( y
+ ch
);
5024 newRow
= m_numRows
- 1;
5026 else if ( newRow
== row
)
5031 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5032 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5040 bool wxGrid::MoveCursorUpBlock()
5043 m_currentCellCoords
!= wxGridNoCellCoords
&&
5044 m_currentCellCoords
.GetRow() > 0 )
5046 int row
= m_currentCellCoords
.GetRow();
5047 int col
= m_currentCellCoords
.GetCol();
5049 if ( m_table
->IsEmptyCell(row
, col
) )
5051 // starting in an empty cell: find the next block of
5057 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5060 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5062 // starting at the top of a block: find the next block
5068 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5073 // starting within a block: find the top of the block
5078 if ( m_table
->IsEmptyCell(row
, col
) )
5086 MakeCellVisible( row
, col
);
5087 SetCurrentCell( row
, col
);
5095 bool wxGrid::MoveCursorDownBlock()
5098 m_currentCellCoords
!= wxGridNoCellCoords
&&
5099 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5101 int row
= m_currentCellCoords
.GetRow();
5102 int col
= m_currentCellCoords
.GetCol();
5104 if ( m_table
->IsEmptyCell(row
, col
) )
5106 // starting in an empty cell: find the next block of
5109 while ( row
< m_numRows
-1 )
5112 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5115 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5117 // starting at the bottom of a block: find the next block
5120 while ( row
< m_numRows
-1 )
5123 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5128 // starting within a block: find the bottom of the block
5130 while ( row
< m_numRows
-1 )
5133 if ( m_table
->IsEmptyCell(row
, col
) )
5141 MakeCellVisible( row
, col
);
5142 SetCurrentCell( row
, col
);
5150 bool wxGrid::MoveCursorLeftBlock()
5153 m_currentCellCoords
!= wxGridNoCellCoords
&&
5154 m_currentCellCoords
.GetCol() > 0 )
5156 int row
= m_currentCellCoords
.GetRow();
5157 int col
= m_currentCellCoords
.GetCol();
5159 if ( m_table
->IsEmptyCell(row
, col
) )
5161 // starting in an empty cell: find the next block of
5167 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5170 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5172 // starting at the left of a block: find the next block
5178 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5183 // starting within a block: find the left of the block
5188 if ( m_table
->IsEmptyCell(row
, col
) )
5196 MakeCellVisible( row
, col
);
5197 SetCurrentCell( row
, col
);
5205 bool wxGrid::MoveCursorRightBlock()
5208 m_currentCellCoords
!= wxGridNoCellCoords
&&
5209 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5211 int row
= m_currentCellCoords
.GetRow();
5212 int col
= m_currentCellCoords
.GetCol();
5214 if ( m_table
->IsEmptyCell(row
, col
) )
5216 // starting in an empty cell: find the next block of
5219 while ( col
< m_numCols
-1 )
5222 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5225 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5227 // starting at the right of a block: find the next block
5230 while ( col
< m_numCols
-1 )
5233 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5238 // starting within a block: find the right of the block
5240 while ( col
< m_numCols
-1 )
5243 if ( m_table
->IsEmptyCell(row
, col
) )
5251 MakeCellVisible( row
, col
);
5252 SetCurrentCell( row
, col
);
5263 // ------ Label values and formatting
5266 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5268 *horiz
= m_rowLabelHorizAlign
;
5269 *vert
= m_rowLabelVertAlign
;
5272 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5274 *horiz
= m_colLabelHorizAlign
;
5275 *vert
= m_colLabelVertAlign
;
5278 wxString
wxGrid::GetRowLabelValue( int row
)
5282 return m_table
->GetRowLabelValue( row
);
5292 wxString
wxGrid::GetColLabelValue( int col
)
5296 return m_table
->GetColLabelValue( col
);
5307 void wxGrid::SetRowLabelSize( int width
)
5309 width
= wxMax( width
, 0 );
5310 if ( width
!= m_rowLabelWidth
)
5314 m_rowLabelWin
->Show( FALSE
);
5315 m_cornerLabelWin
->Show( FALSE
);
5317 else if ( m_rowLabelWidth
== 0 )
5319 m_rowLabelWin
->Show( TRUE
);
5320 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5323 m_rowLabelWidth
= width
;
5330 void wxGrid::SetColLabelSize( int height
)
5332 height
= wxMax( height
, 0 );
5333 if ( height
!= m_colLabelHeight
)
5337 m_colLabelWin
->Show( FALSE
);
5338 m_cornerLabelWin
->Show( FALSE
);
5340 else if ( m_colLabelHeight
== 0 )
5342 m_colLabelWin
->Show( TRUE
);
5343 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5346 m_colLabelHeight
= height
;
5353 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5355 if ( m_labelBackgroundColour
!= colour
)
5357 m_labelBackgroundColour
= colour
;
5358 m_rowLabelWin
->SetBackgroundColour( colour
);
5359 m_colLabelWin
->SetBackgroundColour( colour
);
5360 m_cornerLabelWin
->SetBackgroundColour( colour
);
5362 if ( !GetBatchCount() )
5364 m_rowLabelWin
->Refresh();
5365 m_colLabelWin
->Refresh();
5366 m_cornerLabelWin
->Refresh();
5371 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5373 if ( m_labelTextColour
!= colour
)
5375 m_labelTextColour
= colour
;
5376 if ( !GetBatchCount() )
5378 m_rowLabelWin
->Refresh();
5379 m_colLabelWin
->Refresh();
5384 void wxGrid::SetLabelFont( const wxFont
& font
)
5387 if ( !GetBatchCount() )
5389 m_rowLabelWin
->Refresh();
5390 m_colLabelWin
->Refresh();
5394 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5396 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5398 m_rowLabelHorizAlign
= horiz
;
5401 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5403 m_rowLabelVertAlign
= vert
;
5406 if ( !GetBatchCount() )
5408 m_rowLabelWin
->Refresh();
5412 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5414 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5416 m_colLabelHorizAlign
= horiz
;
5419 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5421 m_colLabelVertAlign
= vert
;
5424 if ( !GetBatchCount() )
5426 m_colLabelWin
->Refresh();
5430 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5434 m_table
->SetRowLabelValue( row
, s
);
5435 if ( !GetBatchCount() )
5437 wxRect rect
= CellToRect( row
, 0);
5438 if ( rect
.height
> 0 )
5440 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5442 rect
.width
= m_rowLabelWidth
;
5443 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5449 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5453 m_table
->SetColLabelValue( col
, s
);
5454 if ( !GetBatchCount() )
5456 wxRect rect
= CellToRect( 0, col
);
5457 if ( rect
.width
> 0 )
5459 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5461 rect
.height
= m_colLabelHeight
;
5462 m_colLabelWin
->Refresh( TRUE
, &rect
);
5468 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5470 if ( m_gridLineColour
!= colour
)
5472 m_gridLineColour
= colour
;
5474 wxClientDC
dc( m_gridWin
);
5476 DrawAllGridLines( dc
, wxRegion() );
5480 void wxGrid::EnableGridLines( bool enable
)
5482 if ( enable
!= m_gridLinesEnabled
)
5484 m_gridLinesEnabled
= enable
;
5486 if ( !GetBatchCount() )
5490 wxClientDC
dc( m_gridWin
);
5492 DrawAllGridLines( dc
, wxRegion() );
5496 m_gridWin
->Refresh();
5503 int wxGrid::GetDefaultRowSize()
5505 return m_defaultRowHeight
;
5508 int wxGrid::GetRowSize( int row
)
5510 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5512 return m_rowHeights
[row
];
5515 int wxGrid::GetDefaultColSize()
5517 return m_defaultColWidth
;
5520 int wxGrid::GetColSize( int col
)
5522 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5524 return m_colWidths
[col
];
5527 // ============================================================================
5528 // access to the grid attributes: each of them has a default value in the grid
5529 // itself and may be overidden on a per-cell basis
5530 // ============================================================================
5532 // ----------------------------------------------------------------------------
5533 // setting default attributes
5534 // ----------------------------------------------------------------------------
5536 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5538 m_defaultCellAttr
->SetBackgroundColour(col
);
5541 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5543 m_defaultCellAttr
->SetTextColour(col
);
5546 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5548 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5551 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5553 m_defaultCellAttr
->SetFont(font
);
5556 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5558 m_defaultCellAttr
->SetRenderer(renderer
);
5561 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5563 m_defaultCellAttr
->SetEditor(editor
);
5566 // ----------------------------------------------------------------------------
5567 // access to the default attrbiutes
5568 // ----------------------------------------------------------------------------
5570 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5572 return m_defaultCellAttr
->GetBackgroundColour();
5575 wxColour
wxGrid::GetDefaultCellTextColour()
5577 return m_defaultCellAttr
->GetTextColour();
5580 wxFont
wxGrid::GetDefaultCellFont()
5582 return m_defaultCellAttr
->GetFont();
5585 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5587 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5590 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5592 return m_defaultCellAttr
->GetRenderer();
5595 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5597 return m_defaultCellAttr
->GetEditor();
5600 // ----------------------------------------------------------------------------
5601 // access to cell attributes
5602 // ----------------------------------------------------------------------------
5604 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5606 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5607 wxColour colour
= attr
->GetBackgroundColour();
5612 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5614 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5615 wxColour colour
= attr
->GetTextColour();
5620 wxFont
wxGrid::GetCellFont( int row
, int col
)
5622 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5623 wxFont font
= attr
->GetFont();
5628 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5630 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5631 attr
->GetAlignment(horiz
, vert
);
5635 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5637 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5638 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5643 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5645 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5646 wxGridCellEditor
* editor
= attr
->GetEditor();
5651 bool wxGrid::IsReadOnly(int row
, int col
) const
5653 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5654 bool isReadOnly
= attr
->IsReadOnly();
5659 // ----------------------------------------------------------------------------
5660 // attribute support: cache, automatic provider creation, ...
5661 // ----------------------------------------------------------------------------
5663 bool wxGrid::CanHaveAttributes()
5670 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5671 // table is providing the attributes itself??? In which case
5672 // I don't think the grid should create a Provider object for the
5673 // table but the table should be smart enough to do that on its own.
5674 if ( !m_table
->GetAttrProvider() )
5676 // use the default attr provider by default
5677 // (another choice would be to just return FALSE thus forcing the user
5679 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5685 void wxGrid::ClearAttrCache()
5687 if ( m_attrCache
.row
!= -1 )
5689 m_attrCache
.attr
->SafeDecRef();
5690 m_attrCache
.row
= -1;
5694 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5696 wxGrid
*self
= (wxGrid
*)this; // const_cast
5698 self
->ClearAttrCache();
5699 self
->m_attrCache
.row
= row
;
5700 self
->m_attrCache
.col
= col
;
5701 self
->m_attrCache
.attr
= attr
;
5705 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5707 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5709 *attr
= m_attrCache
.attr
;
5710 (*attr
)->SafeIncRef();
5712 #ifdef DEBUG_ATTR_CACHE
5713 gs_nAttrCacheHits
++;
5720 #ifdef DEBUG_ATTR_CACHE
5721 gs_nAttrCacheMisses
++;
5727 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5729 wxGridCellAttr
*attr
;
5730 if ( !LookupAttr(row
, col
, &attr
) )
5732 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5733 CacheAttr(row
, col
, attr
);
5737 attr
->SetDefAttr(m_defaultCellAttr
);
5741 attr
= m_defaultCellAttr
;
5748 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5750 wxGridCellAttr
*attr
;
5751 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5753 wxASSERT_MSG( m_table
,
5754 _T("we may only be called if CanHaveAttributes() "
5755 "returned TRUE and then m_table should be !NULL") );
5757 attr
= m_table
->GetAttr(row
, col
);
5760 attr
= new wxGridCellAttr
;
5762 // artificially inc the ref count to match DecRef() in caller
5765 m_table
->SetAttr(attr
, row
, col
);
5768 CacheAttr(row
, col
, attr
);
5770 attr
->SetDefAttr(m_defaultCellAttr
);
5774 // ----------------------------------------------------------------------------
5775 // setting cell attributes: this is forwarded to the table
5776 // ----------------------------------------------------------------------------
5778 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5780 if ( CanHaveAttributes() )
5782 m_table
->SetRowAttr(attr
, row
);
5790 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5792 if ( CanHaveAttributes() )
5794 m_table
->SetColAttr(attr
, col
);
5802 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5804 if ( CanHaveAttributes() )
5806 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5807 attr
->SetBackgroundColour(colour
);
5812 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5814 if ( CanHaveAttributes() )
5816 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5817 attr
->SetTextColour(colour
);
5822 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5824 if ( CanHaveAttributes() )
5826 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5827 attr
->SetFont(font
);
5832 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5834 if ( CanHaveAttributes() )
5836 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5837 attr
->SetAlignment(horiz
, vert
);
5842 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5844 if ( CanHaveAttributes() )
5846 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5847 attr
->SetRenderer(renderer
);
5852 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5854 if ( CanHaveAttributes() )
5856 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5857 attr
->SetEditor(editor
);
5862 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5864 if ( CanHaveAttributes() )
5866 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5867 attr
->SetReadOnly(isReadOnly
);
5872 // ----------------------------------------------------------------------------
5874 // ----------------------------------------------------------------------------
5876 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5878 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5880 if ( resizeExistingRows
)
5884 for ( row
= 0; row
< m_numRows
; row
++ )
5886 m_rowHeights
[row
] = m_defaultRowHeight
;
5887 bottom
+= m_defaultRowHeight
;
5888 m_rowBottoms
[row
] = bottom
;
5894 void wxGrid::SetRowSize( int row
, int height
)
5896 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5900 int h
= wxMax( 0, height
);
5901 int diff
= h
- m_rowHeights
[row
];
5903 m_rowHeights
[row
] = h
;
5904 for ( i
= row
; i
< m_numRows
; i
++ )
5906 m_rowBottoms
[i
] += diff
;
5911 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5913 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5915 if ( resizeExistingCols
)
5919 for ( col
= 0; col
< m_numCols
; col
++ )
5921 m_colWidths
[col
] = m_defaultColWidth
;
5922 right
+= m_defaultColWidth
;
5923 m_colRights
[col
] = right
;
5929 void wxGrid::SetColSize( int col
, int width
)
5931 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5935 int w
= wxMax( 0, width
);
5936 int diff
= w
- m_colWidths
[col
];
5937 m_colWidths
[col
] = w
;
5939 for ( i
= col
; i
< m_numCols
; i
++ )
5941 m_colRights
[i
] += diff
;
5948 // ------ cell value accessor functions
5951 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5955 m_table
->SetValue( row
, col
, s
.c_str() );
5956 if ( !GetBatchCount() )
5958 wxClientDC
dc( m_gridWin
);
5960 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5963 #if 0 // TODO: edit in place
5965 if ( m_currentCellCoords
.GetRow() == row
&&
5966 m_currentCellCoords
.GetCol() == col
)
5968 SetEditControlValue( s
);
5977 // ------ Block, row and col selection
5980 void wxGrid::SelectRow( int row
, bool addToSelected
)
5984 if ( IsSelection() && addToSelected
)
5987 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5990 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5991 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5992 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5993 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5997 need_refresh
[0] = TRUE
;
5998 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5999 wxGridCellCoords ( oldTop
- 1,
6001 m_selectedTopLeft
.SetRow( row
);
6006 need_refresh
[1] = TRUE
;
6007 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6008 wxGridCellCoords ( oldBottom
,
6011 m_selectedTopLeft
.SetCol( 0 );
6014 if ( oldBottom
< row
)
6016 need_refresh
[2] = TRUE
;
6017 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6018 wxGridCellCoords ( row
,
6020 m_selectedBottomRight
.SetRow( row
);
6023 if ( oldRight
< m_numCols
- 1 )
6025 need_refresh
[3] = TRUE
;
6026 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6028 wxGridCellCoords ( oldBottom
,
6030 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6033 for (i
= 0; i
< 4; i
++ )
6034 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6035 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6039 r
= SelectionToDeviceRect();
6041 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6043 m_selectedTopLeft
.Set( row
, 0 );
6044 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6045 r
= SelectionToDeviceRect();
6046 m_gridWin
->Refresh( FALSE
, &r
);
6049 wxGridRangeSelectEvent
gridEvt( GetId(),
6050 wxEVT_GRID_RANGE_SELECT
,
6053 m_selectedBottomRight
);
6055 GetEventHandler()->ProcessEvent(gridEvt
);
6059 void wxGrid::SelectCol( int col
, bool addToSelected
)
6061 if ( IsSelection() && addToSelected
)
6064 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
6067 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6068 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6069 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6070 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6072 if ( oldLeft
> col
)
6074 need_refresh
[0] = TRUE
;
6075 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6076 wxGridCellCoords ( m_numRows
- 1,
6078 m_selectedTopLeft
.SetCol( col
);
6083 need_refresh
[1] = TRUE
;
6084 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6085 wxGridCellCoords ( oldTop
- 1,
6087 m_selectedTopLeft
.SetRow( 0 );
6090 if ( oldRight
< col
)
6092 need_refresh
[2] = TRUE
;
6093 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6094 wxGridCellCoords ( m_numRows
- 1,
6096 m_selectedBottomRight
.SetCol( col
);
6099 if ( oldBottom
< m_numRows
- 1 )
6101 need_refresh
[3] = TRUE
;
6102 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6104 wxGridCellCoords ( m_numRows
- 1,
6106 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6109 for (i
= 0; i
< 4; i
++ )
6110 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6111 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6117 r
= SelectionToDeviceRect();
6119 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6121 m_selectedTopLeft
.Set( 0, col
);
6122 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6123 r
= SelectionToDeviceRect();
6124 m_gridWin
->Refresh( FALSE
, &r
);
6127 wxGridRangeSelectEvent
gridEvt( GetId(),
6128 wxEVT_GRID_RANGE_SELECT
,
6131 m_selectedBottomRight
);
6133 GetEventHandler()->ProcessEvent(gridEvt
);
6137 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6140 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6142 if ( topRow
> bottomRow
)
6149 if ( leftCol
> rightCol
)
6156 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6157 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6159 if ( m_selectedTopLeft
!= updateTopLeft
||
6160 m_selectedBottomRight
!= updateBottomRight
)
6162 // Compute two optimal update rectangles:
6163 // Either one rectangle is a real subset of the
6164 // other, or they are (almost) disjoint!
6166 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
6169 // Store intermediate values
6170 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6171 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6172 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6173 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6175 // Determine the outer/inner coordinates.
6176 if (oldLeft
> leftCol
)
6182 if (oldTop
> topRow
)
6188 if (oldRight
< rightCol
)
6191 oldRight
= rightCol
;
6194 if (oldBottom
< bottomRow
)
6197 oldBottom
= bottomRow
;
6201 // Now, either the stuff marked old is the outer
6202 // rectangle or we don't have a situation where one
6203 // is contained in the other.
6205 if ( oldLeft
< leftCol
)
6207 need_refresh
[0] = TRUE
;
6208 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6210 wxGridCellCoords ( oldBottom
,
6214 if ( oldTop
< topRow
)
6216 need_refresh
[1] = TRUE
;
6217 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6219 wxGridCellCoords ( topRow
- 1,
6223 if ( oldRight
> rightCol
)
6225 need_refresh
[2] = TRUE
;
6226 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6228 wxGridCellCoords ( oldBottom
,
6232 if ( oldBottom
> bottomRow
)
6234 need_refresh
[3] = TRUE
;
6235 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6237 wxGridCellCoords ( oldBottom
,
6243 m_selectedTopLeft
= updateTopLeft
;
6244 m_selectedBottomRight
= updateBottomRight
;
6246 // various Refresh() calls
6247 for (i
= 0; i
< 4; i
++ )
6248 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6249 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6252 // only generate an event if the block is not being selected by
6253 // dragging the mouse (in which case the event will be generated in
6254 // the mouse event handler)
6255 if ( !m_isDragging
)
6257 wxGridRangeSelectEvent
gridEvt( GetId(),
6258 wxEVT_GRID_RANGE_SELECT
,
6261 m_selectedBottomRight
);
6263 GetEventHandler()->ProcessEvent(gridEvt
);
6267 void wxGrid::SelectAll()
6269 m_selectedTopLeft
.Set( 0, 0 );
6270 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6272 m_gridWin
->Refresh();
6276 void wxGrid::ClearSelection()
6278 m_selectedTopLeft
= wxGridNoCellCoords
;
6279 m_selectedBottomRight
= wxGridNoCellCoords
;
6283 // This function returns the rectangle that encloses the given block
6284 // in device coords clipped to the client size of the grid window.
6286 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6287 const wxGridCellCoords
&bottomRight
)
6289 wxRect
rect( wxGridNoCellRect
);
6292 cellRect
= CellToRect( topLeft
);
6293 if ( cellRect
!= wxGridNoCellRect
)
6299 rect
= wxRect( 0, 0, 0, 0 );
6302 cellRect
= CellToRect( bottomRight
);
6303 if ( cellRect
!= wxGridNoCellRect
)
6309 return wxGridNoCellRect
;
6312 // convert to scrolled coords
6314 int left
, top
, right
, bottom
;
6315 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6316 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6319 m_gridWin
->GetClientSize( &cw
, &ch
);
6321 rect
.SetLeft( wxMax(0, left
) );
6322 rect
.SetTop( wxMax(0, top
) );
6323 rect
.SetRight( wxMin(cw
, right
) );
6324 rect
.SetBottom( wxMin(ch
, bottom
) );
6332 // ------ Grid event classes
6335 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6337 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6338 int row
, int col
, int x
, int y
,
6339 bool control
, bool shift
, bool alt
, bool meta
)
6340 : wxNotifyEvent( type
, id
)
6346 m_control
= control
;
6351 SetEventObject(obj
);
6355 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6357 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6358 int rowOrCol
, int x
, int y
,
6359 bool control
, bool shift
, bool alt
, bool meta
)
6360 : wxNotifyEvent( type
, id
)
6362 m_rowOrCol
= rowOrCol
;
6365 m_control
= control
;
6370 SetEventObject(obj
);
6374 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6376 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6377 const wxGridCellCoords
& topLeft
,
6378 const wxGridCellCoords
& bottomRight
,
6379 bool control
, bool shift
, bool alt
, bool meta
)
6380 : wxNotifyEvent( type
, id
)
6382 m_topLeft
= topLeft
;
6383 m_bottomRight
= bottomRight
;
6384 m_control
= control
;
6389 SetEventObject(obj
);
6393 #endif // ifndef wxUSE_NEW_GRID