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
)
395 wxASSERT_MSG(m_control
,
396 wxT("The wxGridCellEditor must be Created first!"));
398 // pass the event to the control
399 m_control
->GetEventHandler()->ProcessEvent(event
);
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::StartingKey(wxKeyEvent
& event
)
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 // position it in the centre (ignoring alignment - TODO)
727 static const wxCoord checkSize
= 16;
730 rectMark
.x
= rect
.x
+ rect
.width
/2 - checkSize
/2;
731 rectMark
.y
= rect
.y
+ rect
.height
/2 - checkSize
/2;
732 rectMark
.width
= rectMark
.height
= checkSize
;
734 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
735 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
736 dc
.DrawRectangle(rectMark
);
738 rectMark
.Inflate(-4);
740 if ( !!grid
.GetTable()->GetValue(row
, col
) ) // FIXME-DATA
742 dc
.SetTextForeground(attr
.GetTextColour());
743 dc
.DrawCheckMark(rectMark
);
747 // ----------------------------------------------------------------------------
749 // ----------------------------------------------------------------------------
751 const wxColour
& wxGridCellAttr::GetTextColour() const
757 else if (m_defGridAttr
!= this)
759 return m_defGridAttr
->GetTextColour();
763 wxFAIL_MSG(wxT("Missing default cell attribute"));
769 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
771 if (HasBackgroundColour())
773 else if (m_defGridAttr
!= this)
774 return m_defGridAttr
->GetBackgroundColour();
777 wxFAIL_MSG(wxT("Missing default cell attribute"));
783 const wxFont
& wxGridCellAttr::GetFont() const
787 else if (m_defGridAttr
!= this)
788 return m_defGridAttr
->GetFont();
791 wxFAIL_MSG(wxT("Missing default cell attribute"));
797 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
801 if ( hAlign
) *hAlign
= m_hAlign
;
802 if ( vAlign
) *vAlign
= m_vAlign
;
804 else if (m_defGridAttr
!= this)
805 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
808 wxFAIL_MSG(wxT("Missing default cell attribute"));
813 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
817 else if (m_defGridAttr
!= this)
818 return m_defGridAttr
->GetRenderer();
821 wxFAIL_MSG(wxT("Missing default cell attribute"));
826 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
830 else if (m_defGridAttr
!= this)
831 return m_defGridAttr
->GetEditor();
834 wxFAIL_MSG(wxT("Missing default cell attribute"));
839 // ----------------------------------------------------------------------------
840 // wxGridCellAttrData
841 // ----------------------------------------------------------------------------
843 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
845 int n
= FindIndex(row
, col
);
846 if ( n
== wxNOT_FOUND
)
849 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
855 // change the attribute
856 m_attrs
[(size_t)n
].attr
= attr
;
860 // remove this attribute
861 m_attrs
.RemoveAt((size_t)n
);
866 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
868 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
870 int n
= FindIndex(row
, col
);
871 if ( n
!= wxNOT_FOUND
)
873 attr
= m_attrs
[(size_t)n
].attr
;
880 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
882 size_t count
= m_attrs
.GetCount();
883 for ( size_t n
= 0; n
< count
; n
++ )
885 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
886 wxCoord row
= coords
.GetRow();
887 if ((size_t)row
>= pos
)
891 // If rows inserted, include row counter where necessary
892 coords
.SetRow(row
+ numRows
);
894 else if (numRows
< 0)
896 // If rows deleted ...
897 if ((size_t)row
>= pos
- numRows
)
899 // ...either decrement row counter (if row still exists)...
900 coords
.SetRow(row
+ numRows
);
904 // ...or remove the attribute
905 m_attrs
.RemoveAt((size_t)n
);
913 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
915 size_t count
= m_attrs
.GetCount();
916 for ( size_t n
= 0; n
< count
; n
++ )
918 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
919 wxCoord col
= coords
.GetCol();
920 if ( (size_t)col
>= pos
)
924 // If rows inserted, include row counter where necessary
925 coords
.SetCol(col
+ numCols
);
927 else if (numCols
< 0)
929 // If rows deleted ...
930 if ((size_t)col
>= pos
- numCols
)
932 // ...either decrement row counter (if row still exists)...
933 coords
.SetCol(col
+ numCols
);
937 // ...or remove the attribute
938 m_attrs
.RemoveAt((size_t)n
);
946 int wxGridCellAttrData::FindIndex(int row
, int col
) const
948 size_t count
= m_attrs
.GetCount();
949 for ( size_t n
= 0; n
< count
; n
++ )
951 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
952 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
961 // ----------------------------------------------------------------------------
962 // wxGridRowOrColAttrData
963 // ----------------------------------------------------------------------------
965 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
967 size_t count
= m_attrs
.Count();
968 for ( size_t n
= 0; n
< count
; n
++ )
970 m_attrs
[n
]->DecRef();
974 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
976 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
978 int n
= m_rowsOrCols
.Index(rowOrCol
);
979 if ( n
!= wxNOT_FOUND
)
981 attr
= m_attrs
[(size_t)n
];
988 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
990 int n
= m_rowsOrCols
.Index(rowOrCol
);
991 if ( n
== wxNOT_FOUND
)
994 m_rowsOrCols
.Add(rowOrCol
);
1001 // change the attribute
1002 m_attrs
[(size_t)n
] = attr
;
1006 // remove this attribute
1007 m_attrs
[(size_t)n
]->DecRef();
1008 m_rowsOrCols
.RemoveAt((size_t)n
);
1009 m_attrs
.RemoveAt((size_t)n
);
1014 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1016 size_t count
= m_attrs
.GetCount();
1017 for ( size_t n
= 0; n
< count
; n
++ )
1019 int & rowOrCol
= m_rowsOrCols
[n
];
1020 if ( (size_t)rowOrCol
>= pos
)
1022 if ( numRowsOrCols
> 0 )
1024 // If rows inserted, include row counter where necessary
1025 rowOrCol
+= numRowsOrCols
;
1027 else if ( numRowsOrCols
< 0)
1029 // If rows deleted, either decrement row counter (if row still exists)
1030 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1031 rowOrCol
+= numRowsOrCols
;
1034 m_rowsOrCols
.RemoveAt((size_t)n
);
1035 m_attrs
.RemoveAt((size_t)n
);
1043 // ----------------------------------------------------------------------------
1044 // wxGridCellAttrProvider
1045 // ----------------------------------------------------------------------------
1047 wxGridCellAttrProvider::wxGridCellAttrProvider()
1049 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1052 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1057 void wxGridCellAttrProvider::InitData()
1059 m_data
= new wxGridCellAttrProviderData
;
1062 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1064 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1067 // first look for the attribute of this specific cell
1068 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1072 // then look for the col attr (col attributes are more common than
1073 // the row ones, hence they have priority)
1074 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1079 // finally try the row attributes
1080 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1087 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1093 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1096 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1101 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1104 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1109 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1112 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1116 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1118 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1122 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1126 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1128 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1132 // ----------------------------------------------------------------------------
1134 // ----------------------------------------------------------------------------
1136 //////////////////////////////////////////////////////////////////////
1138 // Abstract base class for grid data (the model)
1140 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1143 wxGridTableBase::wxGridTableBase()
1145 m_view
= (wxGrid
*) NULL
;
1146 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1149 wxGridTableBase::~wxGridTableBase()
1151 delete m_attrProvider
;
1154 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1156 delete m_attrProvider
;
1157 m_attrProvider
= attrProvider
;
1160 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1162 if ( m_attrProvider
)
1163 return m_attrProvider
->GetAttr(row
, col
);
1165 return (wxGridCellAttr
*)NULL
;
1168 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1170 if ( m_attrProvider
)
1172 m_attrProvider
->SetAttr(attr
, row
, col
);
1176 // as we take ownership of the pointer and don't store it, we must
1182 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1184 if ( m_attrProvider
)
1186 m_attrProvider
->SetRowAttr(attr
, row
);
1190 // as we take ownership of the pointer and don't store it, we must
1196 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1198 if ( m_attrProvider
)
1200 m_attrProvider
->SetColAttr(attr
, col
);
1204 // as we take ownership of the pointer and don't store it, we must
1210 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1212 if ( m_attrProvider
)
1214 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1218 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1220 if ( m_attrProvider
)
1222 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1226 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1228 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1229 "but your derived table class does not override this function") );
1234 bool wxGridTableBase::AppendRows( size_t numRows
)
1236 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1237 "but your derived table class does not override this function"));
1242 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1244 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1245 "but your derived table class does not override this function"));
1250 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1252 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1253 "but your derived table class does not override this function"));
1258 bool wxGridTableBase::AppendCols( size_t numCols
)
1260 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1261 "but your derived table class does not override this function"));
1266 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1268 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1269 "but your derived table class does not override this function"));
1275 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1282 wxString
wxGridTableBase::GetColLabelValue( int col
)
1284 // default col labels are:
1285 // cols 0 to 25 : A-Z
1286 // cols 26 to 675 : AA-ZZ
1291 for ( n
= 1; ; n
++ )
1293 s
+= (_T('A') + (wxChar
)( col%26
));
1295 if ( col
< 0 ) break;
1298 // reverse the string...
1300 for ( i
= 0; i
< n
; i
++ )
1310 //////////////////////////////////////////////////////////////////////
1312 // Message class for the grid table to send requests and notifications
1316 wxGridTableMessage::wxGridTableMessage()
1318 m_table
= (wxGridTableBase
*) NULL
;
1324 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1325 int commandInt1
, int commandInt2
)
1329 m_comInt1
= commandInt1
;
1330 m_comInt2
= commandInt2
;
1335 //////////////////////////////////////////////////////////////////////
1337 // A basic grid table for string data. An object of this class will
1338 // created by wxGrid if you don't specify an alternative table class.
1341 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1343 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1345 wxGridStringTable::wxGridStringTable()
1350 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1355 m_data
.Alloc( numRows
);
1358 sa
.Alloc( numCols
);
1359 for ( col
= 0; col
< numCols
; col
++ )
1361 sa
.Add( wxEmptyString
);
1364 for ( row
= 0; row
< numRows
; row
++ )
1370 wxGridStringTable::~wxGridStringTable()
1374 long wxGridStringTable::GetNumberRows()
1376 return m_data
.GetCount();
1379 long wxGridStringTable::GetNumberCols()
1381 if ( m_data
.GetCount() > 0 )
1382 return m_data
[0].GetCount();
1387 wxString
wxGridStringTable::GetValue( int row
, int col
)
1389 // TODO: bounds checking
1391 return m_data
[row
][col
];
1394 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1396 // TODO: bounds checking
1398 m_data
[row
][col
] = s
;
1401 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1403 // TODO: bounds checking
1405 return (m_data
[row
][col
] == wxEmptyString
);
1409 void wxGridStringTable::Clear()
1412 int numRows
, numCols
;
1414 numRows
= m_data
.GetCount();
1417 numCols
= m_data
[0].GetCount();
1419 for ( row
= 0; row
< numRows
; row
++ )
1421 for ( col
= 0; col
< numCols
; col
++ )
1423 m_data
[row
][col
] = wxEmptyString
;
1430 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1434 size_t curNumRows
= m_data
.GetCount();
1435 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1437 if ( pos
>= curNumRows
)
1439 return AppendRows( numRows
);
1443 sa
.Alloc( curNumCols
);
1444 for ( col
= 0; col
< curNumCols
; col
++ )
1446 sa
.Add( wxEmptyString
);
1449 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1451 m_data
.Insert( sa
, row
);
1453 UpdateAttrRows( pos
, numRows
);
1456 wxGridTableMessage
msg( this,
1457 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1461 GetView()->ProcessTableMessage( msg
);
1467 bool wxGridStringTable::AppendRows( size_t numRows
)
1471 size_t curNumRows
= m_data
.GetCount();
1472 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1475 if ( curNumCols
> 0 )
1477 sa
.Alloc( curNumCols
);
1478 for ( col
= 0; col
< curNumCols
; col
++ )
1480 sa
.Add( wxEmptyString
);
1484 for ( row
= 0; row
< numRows
; row
++ )
1491 wxGridTableMessage
msg( this,
1492 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1495 GetView()->ProcessTableMessage( msg
);
1501 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1505 size_t curNumRows
= m_data
.GetCount();
1507 if ( pos
>= curNumRows
)
1510 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1511 "Pos value is invalid for present table with %d rows",
1512 pos
, numRows
, curNumRows
);
1513 wxFAIL_MSG( wxT(errmsg
) );
1517 if ( numRows
> curNumRows
- pos
)
1519 numRows
= curNumRows
- pos
;
1522 if ( numRows
>= curNumRows
)
1524 m_data
.Empty(); // don't release memory just yet
1528 for ( n
= 0; n
< numRows
; n
++ )
1530 m_data
.Remove( pos
);
1533 UpdateAttrRows( pos
, -((int)numRows
) );
1536 wxGridTableMessage
msg( this,
1537 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1541 GetView()->ProcessTableMessage( msg
);
1547 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1551 size_t curNumRows
= m_data
.GetCount();
1552 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1554 if ( pos
>= curNumCols
)
1556 return AppendCols( numCols
);
1559 for ( row
= 0; row
< curNumRows
; row
++ )
1561 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1563 m_data
[row
].Insert( wxEmptyString
, col
);
1566 UpdateAttrCols( pos
, numCols
);
1569 wxGridTableMessage
msg( this,
1570 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1574 GetView()->ProcessTableMessage( msg
);
1580 bool wxGridStringTable::AppendCols( size_t numCols
)
1584 size_t curNumRows
= m_data
.GetCount();
1587 // TODO: something better than this ?
1589 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1590 "Call AppendRows() first") );
1594 for ( row
= 0; row
< curNumRows
; row
++ )
1596 for ( n
= 0; n
< numCols
; n
++ )
1598 m_data
[row
].Add( wxEmptyString
);
1604 wxGridTableMessage
msg( this,
1605 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1608 GetView()->ProcessTableMessage( msg
);
1614 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1618 size_t curNumRows
= m_data
.GetCount();
1619 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1621 if ( pos
>= curNumCols
)
1624 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1625 "Pos value is invalid for present table with %d cols",
1626 pos
, numCols
, curNumCols
);
1627 wxFAIL_MSG( wxT( errmsg
) );
1631 if ( numCols
> curNumCols
- pos
)
1633 numCols
= curNumCols
- pos
;
1636 for ( row
= 0; row
< curNumRows
; row
++ )
1638 if ( numCols
>= curNumCols
)
1640 m_data
[row
].Clear();
1644 for ( n
= 0; n
< numCols
; n
++ )
1646 m_data
[row
].Remove( pos
);
1650 UpdateAttrCols( pos
, -((int)numCols
) );
1653 wxGridTableMessage
msg( this,
1654 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1658 GetView()->ProcessTableMessage( msg
);
1664 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1666 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1668 // using default label
1670 return wxGridTableBase::GetRowLabelValue( row
);
1674 return m_rowLabels
[ row
];
1678 wxString
wxGridStringTable::GetColLabelValue( int col
)
1680 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1682 // using default label
1684 return wxGridTableBase::GetColLabelValue( col
);
1688 return m_colLabels
[ col
];
1692 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1694 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1696 int n
= m_rowLabels
.GetCount();
1698 for ( i
= n
; i
<= row
; i
++ )
1700 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1704 m_rowLabels
[row
] = value
;
1707 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1709 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1711 int n
= m_colLabels
.GetCount();
1713 for ( i
= n
; i
<= col
; i
++ )
1715 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1719 m_colLabels
[col
] = value
;
1724 //////////////////////////////////////////////////////////////////////
1725 //////////////////////////////////////////////////////////////////////
1727 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1729 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1730 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1731 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1732 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1735 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1737 const wxPoint
&pos
, const wxSize
&size
)
1738 : wxWindow( parent
, id
, pos
, size
)
1743 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1747 // NO - don't do this because it will set both the x and y origin
1748 // coords to match the parent scrolled window and we just want to
1749 // set the y coord - MB
1751 // m_owner->PrepareDC( dc );
1754 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1755 dc
.SetDeviceOrigin( 0, -y
);
1757 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1758 m_owner
->DrawRowLabels( dc
);
1762 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1764 m_owner
->ProcessRowLabelMouseEvent( event
);
1768 // This seems to be required for wxMotif otherwise the mouse
1769 // cursor must be in the cell edit control to get key events
1771 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1773 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1778 //////////////////////////////////////////////////////////////////////
1780 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1782 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1783 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1784 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1785 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1788 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1790 const wxPoint
&pos
, const wxSize
&size
)
1791 : wxWindow( parent
, id
, pos
, size
)
1796 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1800 // NO - don't do this because it will set both the x and y origin
1801 // coords to match the parent scrolled window and we just want to
1802 // set the x coord - MB
1804 // m_owner->PrepareDC( dc );
1807 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1808 dc
.SetDeviceOrigin( -x
, 0 );
1810 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1811 m_owner
->DrawColLabels( dc
);
1815 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1817 m_owner
->ProcessColLabelMouseEvent( event
);
1821 // This seems to be required for wxMotif otherwise the mouse
1822 // cursor must be in the cell edit control to get key events
1824 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1826 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1831 //////////////////////////////////////////////////////////////////////
1833 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1835 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1836 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1837 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1838 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1841 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1843 const wxPoint
&pos
, const wxSize
&size
)
1844 : wxWindow( parent
, id
, pos
, size
)
1849 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1853 int client_height
= 0;
1854 int client_width
= 0;
1855 GetClientSize( &client_width
, &client_height
);
1857 dc
.SetPen( *wxBLACK_PEN
);
1858 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1859 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1861 dc
.SetPen( *wxWHITE_PEN
);
1862 dc
.DrawLine( 0, 0, client_width
, 0 );
1863 dc
.DrawLine( 0, 0, 0, client_height
);
1867 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1869 m_owner
->ProcessCornerLabelMouseEvent( event
);
1873 // This seems to be required for wxMotif otherwise the mouse
1874 // cursor must be in the cell edit control to get key events
1876 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1878 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1883 //////////////////////////////////////////////////////////////////////
1885 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1887 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1888 EVT_PAINT( wxGridWindow::OnPaint
)
1889 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1890 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1891 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1894 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1895 wxGridRowLabelWindow
*rowLblWin
,
1896 wxGridColLabelWindow
*colLblWin
,
1897 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1898 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1901 m_rowLabelWin
= rowLblWin
;
1902 m_colLabelWin
= colLblWin
;
1903 SetBackgroundColour( "WHITE" );
1907 wxGridWindow::~wxGridWindow()
1912 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1914 wxPaintDC
dc( this );
1915 m_owner
->PrepareDC( dc
);
1916 wxRegion reg
= GetUpdateRegion();
1917 m_owner
->CalcCellsExposed( reg
);
1918 m_owner
->DrawGridCellArea( dc
);
1919 #if WXGRID_DRAW_LINES
1920 m_owner
->DrawAllGridLines( dc
, reg
);
1925 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1927 wxPanel::ScrollWindow( dx
, dy
, rect
);
1928 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1929 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1933 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1935 m_owner
->ProcessGridCellMouseEvent( event
);
1939 // This seems to be required for wxMotif otherwise the mouse
1940 // cursor must be in the cell edit control to get key events
1942 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1944 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1947 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1953 //////////////////////////////////////////////////////////////////////
1956 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1958 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1959 EVT_PAINT( wxGrid::OnPaint
)
1960 EVT_SIZE( wxGrid::OnSize
)
1961 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1962 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1965 wxGrid::wxGrid( wxWindow
*parent
,
1970 const wxString
& name
)
1971 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1980 m_defaultCellAttr
->SafeDecRef();
1982 #ifdef DEBUG_ATTR_CACHE
1983 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1984 wxPrintf(_T("wxGrid attribute cache statistics: "
1985 "total: %u, hits: %u (%u%%)\n"),
1986 total
, gs_nAttrCacheHits
,
1987 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1996 // ----- internal init and update functions
1999 void wxGrid::Create()
2001 m_created
= FALSE
; // set to TRUE by CreateGrid
2002 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2004 m_table
= (wxGridTableBase
*) NULL
;
2007 m_cellEditCtrlEnabled
= FALSE
;
2009 m_defaultCellAttr
= new wxGridCellAttr
;
2010 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2011 // RD: Should we fill the default attrs now or is waiting until Init() okay?
2016 m_currentCellCoords
= wxGridNoCellCoords
;
2018 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2019 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2021 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2026 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2031 m_colLabelWin
= new wxGridColLabelWindow( this,
2036 m_gridWin
= new wxGridWindow( this,
2043 SetTargetWindow( m_gridWin
);
2047 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2051 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2056 m_numRows
= numRows
;
2057 m_numCols
= numCols
;
2059 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2060 m_table
->SetView( this );
2069 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2073 // RD: Actually, this should probably be allowed. I think it would be
2074 // nice to be able to switch multiple Tables in and out of a single
2075 // View at runtime. Is there anything in the implmentation that would
2078 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2083 m_numRows
= table
->GetNumberRows();
2084 m_numCols
= table
->GetNumberCols();
2087 m_table
->SetView( this );
2102 if ( m_numRows
<= 0 )
2103 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2105 if ( m_numCols
<= 0 )
2106 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2108 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2109 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2111 if ( m_rowLabelWin
)
2113 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2117 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2120 m_labelTextColour
= wxColour( _T("BLACK") );
2123 m_attrCache
.row
= -1;
2125 // TODO: something better than this ?
2127 m_labelFont
= this->GetFont();
2128 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2130 m_rowLabelHorizAlign
= wxLEFT
;
2131 m_rowLabelVertAlign
= wxCENTRE
;
2133 m_colLabelHorizAlign
= wxCENTRE
;
2134 m_colLabelVertAlign
= wxTOP
;
2136 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2137 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2139 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2140 m_defaultRowHeight
+= 8;
2142 m_defaultRowHeight
+= 4;
2145 m_rowHeights
.Alloc( m_numRows
);
2146 m_rowBottoms
.Alloc( m_numRows
);
2148 for ( i
= 0; i
< m_numRows
; i
++ )
2150 m_rowHeights
.Add( m_defaultRowHeight
);
2151 rowBottom
+= m_defaultRowHeight
;
2152 m_rowBottoms
.Add( rowBottom
);
2155 m_colWidths
.Alloc( m_numCols
);
2156 m_colRights
.Alloc( m_numCols
);
2158 for ( i
= 0; i
< m_numCols
; i
++ )
2160 m_colWidths
.Add( m_defaultColWidth
);
2161 colRight
+= m_defaultColWidth
;
2162 m_colRights
.Add( colRight
);
2165 // Set default cell attributes
2166 m_defaultCellAttr
->SetFont(GetFont());
2167 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2168 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2169 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2170 m_defaultCellAttr
->SetTextColour(
2171 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2172 m_defaultCellAttr
->SetBackgroundColour(
2173 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2176 m_gridLineColour
= wxColour( 128, 128, 255 );
2177 m_gridLinesEnabled
= TRUE
;
2179 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2180 m_winCapture
= (wxWindow
*)NULL
;
2182 m_dragRowOrCol
= -1;
2183 m_isDragging
= FALSE
;
2184 m_startDragPos
= wxDefaultPosition
;
2186 m_waitForSlowClick
= FALSE
;
2188 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2189 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2191 m_currentCellCoords
= wxGridNoCellCoords
;
2193 m_selectedTopLeft
= wxGridNoCellCoords
;
2194 m_selectedBottomRight
= wxGridNoCellCoords
;
2195 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2196 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2198 m_editable
= TRUE
; // default for whole grid
2200 m_inOnKeyDown
= FALSE
;
2206 void wxGrid::CalcDimensions()
2209 GetClientSize( &cw
, &ch
);
2211 if ( m_numRows
> 0 && m_numCols
> 0 )
2213 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2214 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2216 // TODO: restore the scroll position that we had before sizing
2219 GetViewStart( &x
, &y
);
2220 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2221 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2227 void wxGrid::CalcWindowSizes()
2230 GetClientSize( &cw
, &ch
);
2232 if ( m_cornerLabelWin
->IsShown() )
2233 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2235 if ( m_colLabelWin
->IsShown() )
2236 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2238 if ( m_rowLabelWin
->IsShown() )
2239 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2241 if ( m_gridWin
->IsShown() )
2242 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2246 // this is called when the grid table sends a message to say that it
2247 // has been redimensioned
2249 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2253 switch ( msg
.GetId() )
2255 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2257 size_t pos
= msg
.GetCommandInt();
2258 int numRows
= msg
.GetCommandInt2();
2259 for ( i
= 0; i
< numRows
; i
++ )
2261 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2262 m_rowBottoms
.Insert( 0, pos
);
2264 m_numRows
+= numRows
;
2267 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2269 for ( i
= pos
; i
< m_numRows
; i
++ )
2271 bottom
+= m_rowHeights
[i
];
2272 m_rowBottoms
[i
] = bottom
;
2278 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2280 int numRows
= msg
.GetCommandInt();
2281 for ( i
= 0; i
< numRows
; i
++ )
2283 m_rowHeights
.Add( m_defaultRowHeight
);
2284 m_rowBottoms
.Add( 0 );
2287 int oldNumRows
= m_numRows
;
2288 m_numRows
+= numRows
;
2291 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2293 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2295 bottom
+= m_rowHeights
[i
];
2296 m_rowBottoms
[i
] = bottom
;
2302 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2304 size_t pos
= msg
.GetCommandInt();
2305 int numRows
= msg
.GetCommandInt2();
2306 for ( i
= 0; i
< numRows
; i
++ )
2308 m_rowHeights
.Remove( pos
);
2309 m_rowBottoms
.Remove( pos
);
2311 m_numRows
-= numRows
;
2316 m_colWidths
.Clear();
2317 m_colRights
.Clear();
2318 m_currentCellCoords
= wxGridNoCellCoords
;
2322 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2323 m_currentCellCoords
.Set( 0, 0 );
2326 for ( i
= 0; i
< m_numRows
; i
++ )
2328 h
+= m_rowHeights
[i
];
2329 m_rowBottoms
[i
] = h
;
2337 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2339 size_t pos
= msg
.GetCommandInt();
2340 int numCols
= msg
.GetCommandInt2();
2341 for ( i
= 0; i
< numCols
; i
++ )
2343 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2344 m_colRights
.Insert( 0, pos
);
2346 m_numCols
+= numCols
;
2349 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2351 for ( i
= pos
; i
< m_numCols
; i
++ )
2353 right
+= m_colWidths
[i
];
2354 m_colRights
[i
] = right
;
2360 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2362 int numCols
= msg
.GetCommandInt();
2363 for ( i
= 0; i
< numCols
; i
++ )
2365 m_colWidths
.Add( m_defaultColWidth
);
2366 m_colRights
.Add( 0 );
2369 int oldNumCols
= m_numCols
;
2370 m_numCols
+= numCols
;
2373 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2375 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2377 right
+= m_colWidths
[i
];
2378 m_colRights
[i
] = right
;
2384 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2386 size_t pos
= msg
.GetCommandInt();
2387 int numCols
= msg
.GetCommandInt2();
2388 for ( i
= 0; i
< numCols
; i
++ )
2390 m_colWidths
.Remove( pos
);
2391 m_colRights
.Remove( pos
);
2393 m_numCols
-= numCols
;
2397 #if 0 // leave the row alone here so that AppendCols will work subsequently
2399 m_rowHeights
.Clear();
2400 m_rowBottoms
.Clear();
2402 m_currentCellCoords
= wxGridNoCellCoords
;
2406 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2407 m_currentCellCoords
.Set( 0, 0 );
2410 for ( i
= 0; i
< m_numCols
; i
++ )
2412 w
+= m_colWidths
[i
];
2425 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2427 wxRegionIterator
iter( reg
);
2430 m_rowLabelsExposed
.Empty();
2437 // TODO: remove this when we can...
2438 // There is a bug in wxMotif that gives garbage update
2439 // rectangles if you jump-scroll a long way by clicking the
2440 // scrollbar with middle button. This is a work-around
2442 #if defined(__WXMOTIF__)
2444 m_gridWin
->GetClientSize( &cw
, &ch
);
2445 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2446 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2449 // logical bounds of update region
2452 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2453 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2455 // find the row labels within these bounds
2459 for ( row
= 0; row
< m_numRows
; row
++ )
2461 if ( m_rowBottoms
[row
] < top
) continue;
2463 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2464 if ( rowTop
> bottom
) break;
2466 m_rowLabelsExposed
.Add( row
);
2474 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2476 wxRegionIterator
iter( reg
);
2479 m_colLabelsExposed
.Empty();
2486 // TODO: remove this when we can...
2487 // There is a bug in wxMotif that gives garbage update
2488 // rectangles if you jump-scroll a long way by clicking the
2489 // scrollbar with middle button. This is a work-around
2491 #if defined(__WXMOTIF__)
2493 m_gridWin
->GetClientSize( &cw
, &ch
);
2494 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2495 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2498 // logical bounds of update region
2501 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2502 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2504 // find the cells within these bounds
2508 for ( col
= 0; col
< m_numCols
; col
++ )
2510 if ( m_colRights
[col
] < left
) continue;
2512 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2513 if ( colLeft
> right
) break;
2515 m_colLabelsExposed
.Add( col
);
2523 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2525 wxRegionIterator
iter( reg
);
2528 m_cellsExposed
.Empty();
2529 m_rowsExposed
.Empty();
2530 m_colsExposed
.Empty();
2532 int left
, top
, right
, bottom
;
2537 // TODO: remove this when we can...
2538 // There is a bug in wxMotif that gives garbage update
2539 // rectangles if you jump-scroll a long way by clicking the
2540 // scrollbar with middle button. This is a work-around
2542 #if defined(__WXMOTIF__)
2544 m_gridWin
->GetClientSize( &cw
, &ch
);
2545 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2546 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2547 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2548 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2551 // logical bounds of update region
2553 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2554 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2556 // find the cells within these bounds
2559 int colLeft
, rowTop
;
2560 for ( row
= 0; row
< m_numRows
; row
++ )
2562 if ( m_rowBottoms
[row
] <= top
) continue;
2564 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2565 if ( rowTop
> bottom
) break;
2567 m_rowsExposed
.Add( row
);
2569 for ( col
= 0; col
< m_numCols
; col
++ )
2571 if ( m_colRights
[col
] <= left
) continue;
2573 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2574 if ( colLeft
> right
) break;
2576 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2577 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2586 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2589 wxPoint
pos( event
.GetPosition() );
2590 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2592 if ( event
.Dragging() )
2594 m_isDragging
= TRUE
;
2596 if ( event
.LeftIsDown() )
2598 switch( m_cursorMode
)
2600 case WXGRID_CURSOR_RESIZE_ROW
:
2602 int cw
, ch
, left
, dummy
;
2603 m_gridWin
->GetClientSize( &cw
, &ch
);
2604 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2606 wxClientDC
dc( m_gridWin
);
2609 m_rowBottoms
[m_dragRowOrCol
] -
2610 m_rowHeights
[m_dragRowOrCol
] +
2611 WXGRID_MIN_ROW_HEIGHT
);
2612 dc
.SetLogicalFunction(wxINVERT
);
2613 if ( m_dragLastPos
>= 0 )
2615 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2617 dc
.DrawLine( left
, y
, left
+cw
, y
);
2622 case WXGRID_CURSOR_SELECT_ROW
:
2623 if ( (row
= YToRow( y
)) >= 0 &&
2624 !IsInSelection( row
, 0 ) )
2626 SelectRow( row
, TRUE
);
2629 // default label to suppress warnings about "enumeration value
2630 // 'xxx' not handled in switch
2638 m_isDragging
= FALSE
;
2641 // ------------ Entering or leaving the window
2643 if ( event
.Entering() || event
.Leaving() )
2645 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2649 // ------------ Left button pressed
2651 else if ( event
.LeftDown() )
2653 // don't send a label click event for a hit on the
2654 // edge of the row label - this is probably the user
2655 // wanting to resize the row
2657 if ( YToEdgeOfRow(y
) < 0 )
2661 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2663 SelectRow( row
, event
.ShiftDown() );
2664 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2669 // starting to drag-resize a row
2671 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2676 // ------------ Left double click
2678 else if (event
.LeftDClick() )
2680 if ( YToEdgeOfRow(y
) < 0 )
2683 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2688 // ------------ Left button released
2690 else if ( event
.LeftUp() )
2692 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2694 DoEndDragResizeRow();
2696 // Note: we are ending the event *after* doing
2697 // default processing in this case
2699 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2702 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2707 // ------------ Right button down
2709 else if ( event
.RightDown() )
2712 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2714 // no default action at the moment
2719 // ------------ Right double click
2721 else if ( event
.RightDClick() )
2724 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2726 // no default action at the moment
2731 // ------------ No buttons down and mouse moving
2733 else if ( event
.Moving() )
2735 m_dragRowOrCol
= YToEdgeOfRow( y
);
2736 if ( m_dragRowOrCol
>= 0 )
2738 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2740 // don't capture the mouse yet
2741 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2744 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2746 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2752 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2755 wxPoint
pos( event
.GetPosition() );
2756 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2758 if ( event
.Dragging() )
2760 m_isDragging
= TRUE
;
2762 if ( event
.LeftIsDown() )
2764 switch( m_cursorMode
)
2766 case WXGRID_CURSOR_RESIZE_COL
:
2768 int cw
, ch
, dummy
, top
;
2769 m_gridWin
->GetClientSize( &cw
, &ch
);
2770 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2772 wxClientDC
dc( m_gridWin
);
2775 m_colRights
[m_dragRowOrCol
] -
2776 m_colWidths
[m_dragRowOrCol
] +
2777 WXGRID_MIN_COL_WIDTH
);
2778 dc
.SetLogicalFunction(wxINVERT
);
2779 if ( m_dragLastPos
>= 0 )
2781 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2783 dc
.DrawLine( x
, top
, x
, top
+ch
);
2788 case WXGRID_CURSOR_SELECT_COL
:
2789 if ( (col
= XToCol( x
)) >= 0 &&
2790 !IsInSelection( 0, col
) )
2792 SelectCol( col
, TRUE
);
2795 // default label to suppress warnings about "enumeration value
2796 // 'xxx' not handled in switch
2804 m_isDragging
= FALSE
;
2807 // ------------ Entering or leaving the window
2809 if ( event
.Entering() || event
.Leaving() )
2811 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2815 // ------------ Left button pressed
2817 else if ( event
.LeftDown() )
2819 // don't send a label click event for a hit on the
2820 // edge of the col label - this is probably the user
2821 // wanting to resize the col
2823 if ( XToEdgeOfCol(x
) < 0 )
2827 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2829 SelectCol( col
, event
.ShiftDown() );
2830 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2835 // starting to drag-resize a col
2837 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2842 // ------------ Left double click
2844 if ( event
.LeftDClick() )
2846 if ( XToEdgeOfCol(x
) < 0 )
2849 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2854 // ------------ Left button released
2856 else if ( event
.LeftUp() )
2858 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2860 DoEndDragResizeCol();
2862 // Note: we are ending the event *after* doing
2863 // default processing in this case
2865 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2868 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2873 // ------------ Right button down
2875 else if ( event
.RightDown() )
2878 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2880 // no default action at the moment
2885 // ------------ Right double click
2887 else if ( event
.RightDClick() )
2890 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2892 // no default action at the moment
2897 // ------------ No buttons down and mouse moving
2899 else if ( event
.Moving() )
2901 m_dragRowOrCol
= XToEdgeOfCol( x
);
2902 if ( m_dragRowOrCol
>= 0 )
2904 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2906 // don't capture the cursor yet
2907 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2910 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2912 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2918 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2920 if ( event
.LeftDown() )
2922 // indicate corner label by having both row and
2925 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2931 else if ( event
.LeftDClick() )
2933 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2936 else if ( event
.RightDown() )
2938 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2940 // no default action at the moment
2944 else if ( event
.RightDClick() )
2946 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2948 // no default action at the moment
2953 void wxGrid::ChangeCursorMode(CursorMode mode
,
2958 static const wxChar
*cursorModes
[] =
2967 wxLogTrace(_T("grid"),
2968 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2969 win
== m_colLabelWin
? _T("colLabelWin")
2970 : win
? _T("rowLabelWin")
2972 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2973 #endif // __WXDEBUG__
2975 if ( mode
== m_cursorMode
)
2980 // by default use the grid itself
2986 m_winCapture
->ReleaseMouse();
2987 m_winCapture
= (wxWindow
*)NULL
;
2990 m_cursorMode
= mode
;
2992 switch ( m_cursorMode
)
2994 case WXGRID_CURSOR_RESIZE_ROW
:
2995 win
->SetCursor( m_rowResizeCursor
);
2998 case WXGRID_CURSOR_RESIZE_COL
:
2999 win
->SetCursor( m_colResizeCursor
);
3003 win
->SetCursor( *wxSTANDARD_CURSOR
);
3006 // we need to capture mouse when resizing
3007 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3008 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3010 if ( captureMouse
&& resize
)
3012 win
->CaptureMouse();
3017 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3020 wxPoint
pos( event
.GetPosition() );
3021 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3023 wxGridCellCoords coords
;
3024 XYToCell( x
, y
, coords
);
3026 if ( event
.Dragging() )
3028 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3030 // Don't start doing anything until the mouse has been drug at
3031 // least 3 pixels in any direction...
3034 if (m_startDragPos
== wxDefaultPosition
)
3036 m_startDragPos
= pos
;
3039 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3043 m_isDragging
= TRUE
;
3044 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3046 // Hide the edit control, so it
3047 // won't interfer with drag-shrinking.
3048 if ( IsCellEditControlEnabled() )
3049 HideCellEditControl();
3051 // Have we captured the mouse yet?
3054 m_winCapture
= m_gridWin
;
3055 m_winCapture
->CaptureMouse();
3058 if ( coords
!= wxGridNoCellCoords
)
3060 if ( !IsSelection() )
3062 SelectBlock( coords
, coords
);
3066 SelectBlock( m_currentCellCoords
, coords
);
3069 if (! IsVisible(coords
))
3071 MakeCellVisible(coords
);
3072 // TODO: need to introduce a delay or something here. The
3073 // scrolling is way to fast, at least on MSW.
3077 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3079 int cw
, ch
, left
, dummy
;
3080 m_gridWin
->GetClientSize( &cw
, &ch
);
3081 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3083 wxClientDC
dc( m_gridWin
);
3086 m_rowBottoms
[m_dragRowOrCol
] -
3087 m_rowHeights
[m_dragRowOrCol
] +
3088 WXGRID_MIN_ROW_HEIGHT
);
3089 dc
.SetLogicalFunction(wxINVERT
);
3090 if ( m_dragLastPos
>= 0 )
3092 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3094 dc
.DrawLine( left
, y
, left
+cw
, y
);
3097 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3099 int cw
, ch
, dummy
, top
;
3100 m_gridWin
->GetClientSize( &cw
, &ch
);
3101 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3103 wxClientDC
dc( m_gridWin
);
3106 m_colRights
[m_dragRowOrCol
] -
3107 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
3108 dc
.SetLogicalFunction(wxINVERT
);
3109 if ( m_dragLastPos
>= 0 )
3111 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3113 dc
.DrawLine( x
, top
, x
, top
+ch
);
3120 m_isDragging
= FALSE
;
3121 m_startDragPos
= wxDefaultPosition
;
3124 if ( coords
!= wxGridNoCellCoords
)
3126 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3127 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3130 if ( event
.Entering() || event
.Leaving() )
3132 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3133 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3138 // ------------ Left button pressed
3140 if ( event
.LeftDown() )
3142 DisableCellEditControl();
3143 if ( event
.ShiftDown() )
3145 SelectBlock( m_currentCellCoords
, coords
);
3147 else if ( XToEdgeOfCol(x
) < 0 &&
3148 YToEdgeOfRow(y
) < 0 )
3150 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3155 MakeCellVisible( coords
);
3157 // if this is the second click on this cell then start
3159 if ( m_waitForSlowClick
&&
3160 (coords
== m_currentCellCoords
) &&
3161 CanEnableCellControl())
3163 EnableCellEditControl();
3164 m_waitForSlowClick
= FALSE
;
3168 SetCurrentCell( coords
);
3169 m_waitForSlowClick
= TRUE
;
3176 // ------------ Left double click
3178 else if ( event
.LeftDClick() )
3180 DisableCellEditControl();
3181 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3183 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3191 // ------------ Left button released
3193 else if ( event
.LeftUp() )
3195 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3197 if ( IsSelection() )
3201 m_winCapture
->ReleaseMouse();
3202 m_winCapture
= NULL
;
3204 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3207 // Show the edit control, if it has been hidden for
3209 ShowCellEditControl();
3211 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3213 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3214 DoEndDragResizeRow();
3216 // Note: we are ending the event *after* doing
3217 // default processing in this case
3219 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3221 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3223 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3224 DoEndDragResizeCol();
3226 // Note: we are ending the event *after* doing
3227 // default processing in this case
3229 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3236 // ------------ Right button down
3238 else if ( event
.RightDown() )
3240 DisableCellEditControl();
3241 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3246 // no default action at the moment
3251 // ------------ Right double click
3253 else if ( event
.RightDClick() )
3255 DisableCellEditControl();
3256 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3261 // no default action at the moment
3265 // ------------ Moving and no button action
3267 else if ( event
.Moving() && !event
.IsButton() )
3269 int dragRow
= YToEdgeOfRow( y
);
3270 int dragCol
= XToEdgeOfCol( x
);
3272 // Dragging on the corner of a cell to resize in both
3273 // directions is not implemented yet...
3275 if ( dragRow
>= 0 && dragCol
>= 0 )
3277 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3283 m_dragRowOrCol
= dragRow
;
3285 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3287 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3295 m_dragRowOrCol
= dragCol
;
3297 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3299 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3305 // Neither on a row or col edge
3307 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3309 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3316 void wxGrid::DoEndDragResizeRow()
3318 if ( m_dragLastPos
>= 0 )
3320 // erase the last line and resize the row
3322 int cw
, ch
, left
, dummy
;
3323 m_gridWin
->GetClientSize( &cw
, &ch
);
3324 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3326 wxClientDC
dc( m_gridWin
);
3328 dc
.SetLogicalFunction( wxINVERT
);
3329 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3330 HideCellEditControl();
3332 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3333 SetRowSize( m_dragRowOrCol
,
3334 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3336 if ( !GetBatchCount() )
3338 // Only needed to get the correct rect.y:
3339 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3341 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3342 rect
.width
= m_rowLabelWidth
;
3343 rect
.height
= ch
- rect
.y
;
3344 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3346 m_gridWin
->Refresh( FALSE
, &rect
);
3349 ShowCellEditControl();
3354 void wxGrid::DoEndDragResizeCol()
3356 if ( m_dragLastPos
>= 0 )
3358 // erase the last line and resize the col
3360 int cw
, ch
, dummy
, top
;
3361 m_gridWin
->GetClientSize( &cw
, &ch
);
3362 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3364 wxClientDC
dc( m_gridWin
);
3366 dc
.SetLogicalFunction( wxINVERT
);
3367 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3368 HideCellEditControl();
3370 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3371 SetColSize( m_dragRowOrCol
,
3372 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3374 if ( !GetBatchCount() )
3376 // Only needed to get the correct rect.x:
3377 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3379 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3380 rect
.width
= cw
- rect
.x
;
3381 rect
.height
= m_colLabelHeight
;
3382 m_colLabelWin
->Refresh( TRUE
, &rect
);
3384 m_gridWin
->Refresh( FALSE
, &rect
);
3387 ShowCellEditControl();
3394 // ------ interaction with data model
3396 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3398 switch ( msg
.GetId() )
3400 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3401 return GetModelValues();
3403 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3404 return SetModelValues();
3406 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3407 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3408 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3409 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3410 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3411 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3412 return Redimension( msg
);
3421 // The behaviour of this function depends on the grid table class
3422 // Clear() function. For the default wxGridStringTable class the
3423 // behavious is to replace all cell contents with wxEmptyString but
3424 // not to change the number of rows or cols.
3426 void wxGrid::ClearGrid()
3431 SetEditControlValue();
3432 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3437 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3439 // TODO: something with updateLabels flag
3443 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3449 if (IsCellEditControlEnabled())
3450 DisableCellEditControl();
3452 bool ok
= m_table
->InsertRows( pos
, numRows
);
3454 // the table will have sent the results of the insert row
3455 // operation to this view object as a grid table message
3459 if ( m_numCols
== 0 )
3461 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3463 // TODO: perhaps instead of appending the default number of cols
3464 // we should remember what the last non-zero number of cols was ?
3468 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3470 // if we have just inserted cols into an empty grid the current
3471 // cell will be undefined...
3473 SetCurrentCell( 0, 0 );
3477 if ( !GetBatchCount() ) Refresh();
3480 SetEditControlValue();
3490 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3492 // TODO: something with updateLabels flag
3496 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3500 if ( m_table
&& m_table
->AppendRows( numRows
) )
3502 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3504 // if we have just inserted cols into an empty grid the current
3505 // cell will be undefined...
3507 SetCurrentCell( 0, 0 );
3510 // the table will have sent the results of the append row
3511 // operation to this view object as a grid table message
3514 if ( !GetBatchCount() ) Refresh();
3524 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3526 // TODO: something with updateLabels flag
3530 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3536 if (IsCellEditControlEnabled())
3537 DisableCellEditControl();
3539 if (m_table
->DeleteRows( pos
, numRows
))
3542 // the table will have sent the results of the delete row
3543 // operation to this view object as a grid table message
3546 if ( !GetBatchCount() ) Refresh();
3554 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3556 // TODO: something with updateLabels flag
3560 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3566 if (IsCellEditControlEnabled())
3567 DisableCellEditControl();
3569 bool ok
= m_table
->InsertCols( pos
, numCols
);
3571 // the table will have sent the results of the insert col
3572 // operation to this view object as a grid table message
3576 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3578 // if we have just inserted cols into an empty grid the current
3579 // cell will be undefined...
3581 SetCurrentCell( 0, 0 );
3585 if ( !GetBatchCount() ) Refresh();
3588 SetEditControlValue();
3598 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3600 // TODO: something with updateLabels flag
3604 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3608 if ( m_table
&& m_table
->AppendCols( numCols
) )
3610 // the table will have sent the results of the append col
3611 // operation to this view object as a grid table message
3613 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3615 // if we have just inserted cols into an empty grid the current
3616 // cell will be undefined...
3618 SetCurrentCell( 0, 0 );
3622 if ( !GetBatchCount() ) Refresh();
3632 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3634 // TODO: something with updateLabels flag
3638 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3644 if (IsCellEditControlEnabled())
3645 DisableCellEditControl();
3647 if ( m_table
->DeleteCols( pos
, numCols
) )
3649 // the table will have sent the results of the delete col
3650 // operation to this view object as a grid table message
3653 if ( !GetBatchCount() ) Refresh();
3663 // ----- event handlers
3666 // Generate a grid event based on a mouse event and
3667 // return the result of ProcessEvent()
3669 bool wxGrid::SendEvent( const wxEventType type
,
3671 wxMouseEvent
& mouseEv
)
3673 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3675 int rowOrCol
= (row
== -1 ? col
: row
);
3677 wxGridSizeEvent
gridEvt( GetId(),
3681 mouseEv
.GetX(), mouseEv
.GetY(),
3682 mouseEv
.ControlDown(),
3683 mouseEv
.ShiftDown(),
3685 mouseEv
.MetaDown() );
3687 return GetEventHandler()->ProcessEvent(gridEvt
);
3689 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3691 wxGridRangeSelectEvent
gridEvt( GetId(),
3695 m_selectedBottomRight
,
3696 mouseEv
.ControlDown(),
3697 mouseEv
.ShiftDown(),
3699 mouseEv
.MetaDown() );
3701 return GetEventHandler()->ProcessEvent(gridEvt
);
3705 wxGridEvent
gridEvt( GetId(),
3709 mouseEv
.GetX(), mouseEv
.GetY(),
3710 mouseEv
.ControlDown(),
3711 mouseEv
.ShiftDown(),
3713 mouseEv
.MetaDown() );
3715 return GetEventHandler()->ProcessEvent(gridEvt
);
3720 // Generate a grid event of specified type and return the result
3721 // of ProcessEvent().
3723 bool wxGrid::SendEvent( const wxEventType type
,
3726 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3728 int rowOrCol
= (row
== -1 ? col
: row
);
3730 wxGridSizeEvent
gridEvt( GetId(),
3735 return GetEventHandler()->ProcessEvent(gridEvt
);
3739 wxGridEvent
gridEvt( GetId(),
3744 return GetEventHandler()->ProcessEvent(gridEvt
);
3749 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3751 wxPaintDC
dc( this );
3753 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3754 m_numRows
&& m_numCols
)
3756 m_currentCellCoords
.Set(0, 0);
3757 SetEditControlValue();
3758 ShowCellEditControl();
3765 // This is just here to make sure that CalcDimensions gets called when
3766 // the grid view is resized... then the size event is skipped to allow
3767 // the box sizers to handle everything
3769 void wxGrid::OnSize( wxSizeEvent
& event
)
3776 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3778 if ( m_inOnKeyDown
)
3780 // shouldn't be here - we are going round in circles...
3782 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3785 m_inOnKeyDown
= TRUE
;
3787 // propagate the event up and see if it gets processed
3789 wxWindow
*parent
= GetParent();
3790 wxKeyEvent
keyEvt( event
);
3791 keyEvt
.SetEventObject( parent
);
3793 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3796 // TODO: Should also support Shift-cursor keys for
3797 // extending the selection. Maybe add a flag to
3798 // MoveCursorXXX() and MoveCursorXXXBlock() and
3799 // just send event.ShiftDown().
3801 // try local handlers
3803 switch ( event
.KeyCode() )
3806 if ( event
.ControlDown() )
3808 MoveCursorUpBlock();
3817 if ( event
.ControlDown() )
3819 MoveCursorDownBlock();
3828 if ( event
.ControlDown() )
3830 MoveCursorLeftBlock();
3839 if ( event
.ControlDown() )
3841 MoveCursorRightBlock();
3850 if ( event
.ControlDown() )
3852 event
.Skip(); // to let the edit control have the return
3861 if (event
.ShiftDown())
3868 if ( event
.ControlDown() )
3870 MakeCellVisible( 0, 0 );
3871 SetCurrentCell( 0, 0 );
3880 if ( event
.ControlDown() )
3882 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3883 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3899 // We don't want these keys to trigger the edit control, any others?
3908 if ( !IsEditable() )
3913 // Otherwise fall through to default
3916 // now try the cell edit control
3918 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
3920 EnableCellEditControl();
3921 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3922 attr
->GetEditor()->StartingKey(event
);
3929 m_inOnKeyDown
= FALSE
;
3933 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3937 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3939 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3941 // the event has been intercepted - do nothing
3946 m_currentCellCoords
!= wxGridNoCellCoords
)
3948 HideCellEditControl();
3949 SaveEditControlValue();
3950 DisableCellEditControl();
3952 // Clear the old current cell highlight
3953 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3955 // Otherwise refresh redraws the highlight!
3956 m_currentCellCoords
= coords
;
3958 m_gridWin
->Refresh( FALSE
, &r
);
3961 m_currentCellCoords
= coords
;
3963 SetEditControlValue();
3967 wxClientDC
dc(m_gridWin
);
3970 wxGridCellAttr
* attr
= GetCellAttr(coords
);
3971 DrawCellHighlight(dc
, attr
);
3974 if ( IsSelection() )
3976 wxRect
r( SelectionToDeviceRect() );
3978 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3985 // ------ functions to get/send data (see also public functions)
3988 bool wxGrid::GetModelValues()
3992 // all we need to do is repaint the grid
3994 m_gridWin
->Refresh();
4002 bool wxGrid::SetModelValues()
4008 for ( row
= 0; row
< m_numRows
; row
++ )
4010 for ( col
= 0; col
< m_numCols
; col
++ )
4012 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4024 // Note - this function only draws cells that are in the list of
4025 // exposed cells (usually set from the update region by
4026 // CalcExposedCells)
4028 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4030 if ( !m_numRows
|| !m_numCols
) return;
4033 size_t numCells
= m_cellsExposed
.GetCount();
4035 for ( i
= 0; i
< numCells
; i
++ )
4037 DrawCell( dc
, m_cellsExposed
[i
] );
4042 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4044 int row
= coords
.GetRow();
4045 int col
= coords
.GetCol();
4047 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4050 // we draw the cell border ourselves
4051 #if !WXGRID_DRAW_LINES
4052 if ( m_gridLinesEnabled
)
4053 DrawCellBorder( dc
, coords
);
4056 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4058 bool isCurrent
= coords
== m_currentCellCoords
;
4061 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4062 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4063 rect
.width
= m_colWidths
[col
] - 1;
4064 rect
.height
= m_rowHeights
[row
] - 1;
4066 // if the editor is shown, we should use it and not the renderer
4067 if ( isCurrent
&& IsCellEditControlEnabled() )
4069 attr
->GetEditor()->PaintBackground(rect
, attr
);
4073 // but all the rest is drawn by the cell renderer and hence may be
4075 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4079 DrawCellHighlight(dc
, attr
);
4086 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4088 int row
= m_currentCellCoords
.GetRow();
4089 int col
= m_currentCellCoords
.GetCol();
4091 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4095 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4096 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4097 rect
.width
= m_colWidths
[col
] - 1;
4098 rect
.height
= m_rowHeights
[row
] - 1;
4100 if ( attr
->IsReadOnly() )
4102 // hmmm... what could we do here to show that the cell is disabled?
4103 // for now, I just draw a thinner border than for the other ones, but
4104 // it doesn't look really good
4105 dc
.SetPen(wxPen(m_gridLineColour
, 2, wxSOLID
));
4106 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4108 dc
.DrawRectangle(rect
);
4112 // VZ: my experiments with 3d borders...
4114 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
4115 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4117 dc
.DrawRectangle(rect
);
4119 // FIXME we should properly set colours for arbitrary bg
4120 wxCoord x1
= rect
.x
,
4122 x2
= rect
.x
+ rect
.width
,
4123 y2
= rect
.y
+ rect
.height
;
4125 dc
.SetPen(*wxWHITE_PEN
);
4126 dc
.DrawLine(x1
, y1
, x2
- 1, y1
);
4127 dc
.DrawLine(x1
, y1
, x1
, y2
- 1);
4129 dc
.SetPen(*wxLIGHT_GREY_PEN
);
4130 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4131 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
- 1);
4133 dc
.SetPen(*wxBLACK_PEN
);
4134 dc
.DrawLine(x1
, y2
, x2
, y2
);
4135 dc
.DrawLine(x2
, y1
, x2
, y2
);
4140 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4142 if ( m_colWidths
[coords
.GetCol()] <=0 ||
4143 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
4145 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4146 int row
= coords
.GetRow();
4147 int col
= coords
.GetCol();
4149 // right hand border
4151 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
4152 m_colRights
[col
], m_rowBottoms
[row
] );
4156 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
4157 m_colRights
[col
], m_rowBottoms
[row
] );
4161 // TODO: remove this ???
4162 // This is used to redraw all grid lines e.g. when the grid line colour
4165 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4167 if ( !m_gridLinesEnabled
||
4169 !m_numCols
) return;
4171 int top
, bottom
, left
, right
;
4176 m_gridWin
->GetClientSize(&cw
, &ch
);
4178 // virtual coords of visible area
4180 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4181 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4186 reg
.GetBox(x
, y
, w
, h
);
4187 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4188 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4191 // avoid drawing grid lines past the last row and col
4193 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
4194 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
4196 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4198 // horizontal grid lines
4201 for ( i
= 0; i
< m_numRows
; i
++ )
4203 if ( m_rowBottoms
[i
]-1 > bottom
)
4207 else if ( m_rowBottoms
[i
]-1 >= top
)
4209 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
4214 // vertical grid lines
4216 for ( i
= 0; i
< m_numCols
; i
++ )
4218 if ( m_colRights
[i
]-1 > right
)
4222 else if ( m_colRights
[i
]-1 >= left
)
4224 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
4230 void wxGrid::DrawRowLabels( wxDC
& dc
)
4232 if ( !m_numRows
|| !m_numCols
) return;
4235 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4237 for ( i
= 0; i
< numLabels
; i
++ )
4239 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4244 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4246 if ( m_rowHeights
[row
] <= 0 ) return;
4248 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4250 dc
.SetPen( *wxBLACK_PEN
);
4251 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4252 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4254 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4255 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4257 dc
.SetPen( *wxWHITE_PEN
);
4258 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4259 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4261 dc
.SetBackgroundMode( wxTRANSPARENT
);
4262 dc
.SetTextForeground( GetLabelTextColour() );
4263 dc
.SetFont( GetLabelFont() );
4266 GetRowLabelAlignment( &hAlign
, &vAlign
);
4270 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4271 rect
.SetWidth( m_rowLabelWidth
- 4 );
4272 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4273 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4277 void wxGrid::DrawColLabels( wxDC
& dc
)
4279 if ( !m_numRows
|| !m_numCols
) return;
4282 size_t numLabels
= m_colLabelsExposed
.GetCount();
4284 for ( i
= 0; i
< numLabels
; i
++ )
4286 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4291 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4293 if ( m_colWidths
[col
] <= 0 ) return;
4295 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4297 dc
.SetPen( *wxBLACK_PEN
);
4298 dc
.DrawLine( m_colRights
[col
]-1, 0,
4299 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4301 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4302 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4304 dc
.SetPen( *wxWHITE_PEN
);
4305 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4306 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4308 dc
.SetBackgroundMode( wxTRANSPARENT
);
4309 dc
.SetTextForeground( GetLabelTextColour() );
4310 dc
.SetFont( GetLabelFont() );
4312 dc
.SetBackgroundMode( wxTRANSPARENT
);
4313 dc
.SetTextForeground( GetLabelTextColour() );
4314 dc
.SetFont( GetLabelFont() );
4317 GetColLabelAlignment( &hAlign
, &vAlign
);
4320 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4322 rect
.SetWidth( m_colWidths
[col
] - 4 );
4323 rect
.SetHeight( m_colLabelHeight
- 4 );
4324 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4328 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4329 const wxString
& value
,
4334 long textWidth
, textHeight
;
4335 long lineWidth
, lineHeight
;
4336 wxArrayString lines
;
4338 dc
.SetClippingRegion( rect
);
4339 StringToLines( value
, lines
);
4340 if ( lines
.GetCount() )
4342 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4343 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4346 switch ( horizAlign
)
4349 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4353 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4362 switch ( vertAlign
)
4365 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4369 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4378 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4380 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4385 dc
.DestroyClippingRegion();
4389 // Split multi line text up into an array of strings. Any existing
4390 // contents of the string array are preserved.
4392 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4396 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4397 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4399 while ( startPos
< (int)tVal
.Length() )
4401 pos
= tVal
.Mid(startPos
).Find( eol
);
4406 else if ( pos
== 0 )
4408 lines
.Add( wxEmptyString
);
4412 lines
.Add( value
.Mid(startPos
, pos
) );
4416 if ( startPos
< (int)value
.Length() )
4418 lines
.Add( value
.Mid( startPos
) );
4423 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4424 wxArrayString
& lines
,
4425 long *width
, long *height
)
4432 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4434 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4435 w
= wxMax( w
, lineW
);
4445 // ------ Edit control functions
4449 void wxGrid::EnableEditing( bool edit
)
4451 // TODO: improve this ?
4453 if ( edit
!= m_editable
)
4457 // FIXME IMHO this won't disable the edit control if edit == FALSE
4458 // because of the check in the beginning of
4459 // EnableCellEditControl() just below (VZ)
4460 EnableCellEditControl(m_editable
);
4465 void wxGrid::EnableCellEditControl( bool enable
)
4470 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4471 SetCurrentCell( 0, 0 );
4473 if ( enable
!= m_cellEditCtrlEnabled
)
4475 // TODO allow the app to Veto() this event?
4476 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4480 // this should be checked by the caller!
4481 wxASSERT_MSG( CanEnableCellControl(),
4482 _T("can't enable editing for this cell!") );
4484 // do it before ShowCellEditControl()
4485 m_cellEditCtrlEnabled
= enable
;
4487 SetEditControlValue();
4488 ShowCellEditControl();
4492 HideCellEditControl();
4493 SaveEditControlValue();
4495 // do it after HideCellEditControl()
4496 m_cellEditCtrlEnabled
= enable
;
4501 bool wxGrid::IsCurrentCellReadOnly() const
4504 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4505 bool readonly
= attr
->IsReadOnly();
4511 bool wxGrid::CanEnableCellControl() const
4513 return m_editable
&& !IsCurrentCellReadOnly();
4516 bool wxGrid::IsCellEditControlEnabled() const
4518 // the cell edit control might be disable for all cells or just for the
4519 // current one if it's read only
4520 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
4523 wxWindow
*wxGrid::GetGridWindow() const
4528 void wxGrid::ShowCellEditControl()
4530 if ( IsCellEditControlEnabled() )
4532 if ( !IsVisible( m_currentCellCoords
) )
4538 wxRect rect
= CellToRect( m_currentCellCoords
);
4539 int row
= m_currentCellCoords
.GetRow();
4540 int col
= m_currentCellCoords
.GetCol();
4542 // convert to scrolled coords
4544 int left
, top
, right
, bottom
;
4545 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4546 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4548 // cell is shifted by one pixel
4554 // Make the edit control large enough to allow for internal
4557 // TODO: remove this if the text ctrl sizing is improved esp. for
4561 #if defined(__WXMOTIF__)
4562 if ( row
== 0 || col
== 0 )
4571 if ( row
== 0 || col
== 0 )
4581 #if defined(__WXGTK__)
4584 if (left
!= 0) left_diff
++;
4585 if (top
!= 0) top_diff
++;
4586 rect
.SetLeft( left
+ left_diff
);
4587 rect
.SetTop( top
+ top_diff
);
4588 rect
.SetRight( rect
.GetRight() - left_diff
);
4589 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4591 rect
.SetLeft( wxMax(0, left
- extra
) );
4592 rect
.SetTop( wxMax(0, top
- extra
) );
4593 rect
.SetRight( rect
.GetRight() + 2*extra
);
4594 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4597 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4598 wxGridCellEditor
* editor
= attr
->GetEditor();
4599 if ( !editor
->IsCreated() )
4601 editor
->Create(m_gridWin
, -1,
4602 new wxGridCellEditorEvtHandler(this, editor
));
4605 editor
->SetSize( rect
);
4606 editor
->Show( TRUE
, attr
);
4607 editor
->BeginEdit(row
, col
, this);
4614 void wxGrid::HideCellEditControl()
4616 if ( IsCellEditControlEnabled() )
4618 int row
= m_currentCellCoords
.GetRow();
4619 int col
= m_currentCellCoords
.GetCol();
4621 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4622 attr
->GetEditor()->Show( FALSE
);
4624 m_gridWin
->SetFocus();
4629 void wxGrid::SetEditControlValue( const wxString
& value
)
4631 // RD: The new Editors get the value from the table themselves now. This
4632 // method can probably be removed...
4636 void wxGrid::SaveEditControlValue()
4638 if ( IsCellEditControlEnabled() )
4640 int row
= m_currentCellCoords
.GetRow();
4641 int col
= m_currentCellCoords
.GetCol();
4643 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4644 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4650 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4651 m_currentCellCoords
.GetRow(),
4652 m_currentCellCoords
.GetCol() );
4659 // ------ Grid location functions
4660 // Note that all of these functions work with the logical coordinates of
4661 // grid cells and labels so you will need to convert from device
4662 // coordinates for mouse events etc.
4665 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4667 int row
= YToRow(y
);
4668 int col
= XToCol(x
);
4670 if ( row
== -1 || col
== -1 )
4672 coords
= wxGridNoCellCoords
;
4676 coords
.Set( row
, col
);
4681 int wxGrid::YToRow( int y
)
4685 for ( i
= 0; i
< m_numRows
; i
++ )
4687 if ( y
< m_rowBottoms
[i
] ) return i
;
4690 return m_numRows
; //-1;
4694 int wxGrid::XToCol( int x
)
4698 for ( i
= 0; i
< m_numCols
; i
++ )
4700 if ( x
< m_colRights
[i
] ) return i
;
4703 return m_numCols
; //-1;
4707 // return the row number that that the y coord is near the edge of, or
4708 // -1 if not near an edge
4710 int wxGrid::YToEdgeOfRow( int y
)
4714 for ( i
= 0; i
< m_numRows
; i
++ )
4716 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4718 d
= abs( y
- m_rowBottoms
[i
] );
4720 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4729 // return the col number that that the x coord is near the edge of, or
4730 // -1 if not near an edge
4732 int wxGrid::XToEdgeOfCol( int x
)
4736 for ( i
= 0; i
< m_numCols
; i
++ )
4738 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4740 d
= abs( x
- m_colRights
[i
] );
4742 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4751 wxRect
wxGrid::CellToRect( int row
, int col
)
4753 wxRect
rect( -1, -1, -1, -1 );
4755 if ( row
>= 0 && row
< m_numRows
&&
4756 col
>= 0 && col
< m_numCols
)
4758 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4759 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4760 rect
.width
= m_colWidths
[col
];
4761 rect
.height
= m_rowHeights
[ row
];
4768 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4770 // get the cell rectangle in logical coords
4772 wxRect
r( CellToRect( row
, col
) );
4774 // convert to device coords
4776 int left
, top
, right
, bottom
;
4777 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4778 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4780 // check against the client area of the grid window
4783 m_gridWin
->GetClientSize( &cw
, &ch
);
4785 if ( wholeCellVisible
)
4787 // is the cell wholly visible ?
4789 return ( left
>= 0 && right
<= cw
&&
4790 top
>= 0 && bottom
<= ch
);
4794 // is the cell partly visible ?
4796 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4797 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4802 // make the specified cell location visible by doing a minimal amount
4805 void wxGrid::MakeCellVisible( int row
, int col
)
4808 int xpos
= -1, ypos
= -1;
4810 if ( row
>= 0 && row
< m_numRows
&&
4811 col
>= 0 && col
< m_numCols
)
4813 // get the cell rectangle in logical coords
4815 wxRect
r( CellToRect( row
, col
) );
4817 // convert to device coords
4819 int left
, top
, right
, bottom
;
4820 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4821 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4824 m_gridWin
->GetClientSize( &cw
, &ch
);
4830 else if ( bottom
> ch
)
4832 int h
= r
.GetHeight();
4834 for ( i
= row
-1; i
>= 0; i
-- )
4836 if ( h
+ m_rowHeights
[i
] > ch
) break;
4838 h
+= m_rowHeights
[i
];
4839 ypos
-= m_rowHeights
[i
];
4842 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4843 // have rounding errors (this is important, because if we do, we
4844 // might not scroll at all and some cells won't be redrawn)
4845 ypos
+= GRID_SCROLL_LINE
/ 2;
4852 else if ( right
> cw
)
4854 int w
= r
.GetWidth();
4856 for ( i
= col
-1; i
>= 0; i
-- )
4858 if ( w
+ m_colWidths
[i
] > cw
) break;
4860 w
+= m_colWidths
[i
];
4861 xpos
-= m_colWidths
[i
];
4864 // see comment for ypos above
4865 xpos
+= GRID_SCROLL_LINE
/ 2;
4868 if ( xpos
!= -1 || ypos
!= -1 )
4870 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4871 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4872 Scroll( xpos
, ypos
);
4880 // ------ Grid cursor movement functions
4883 bool wxGrid::MoveCursorUp()
4885 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4886 m_currentCellCoords
.GetRow() > 0 )
4888 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4889 m_currentCellCoords
.GetCol() );
4891 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4892 m_currentCellCoords
.GetCol() );
4901 bool wxGrid::MoveCursorDown()
4903 // TODO: allow for scrolling
4905 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4906 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4908 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4909 m_currentCellCoords
.GetCol() );
4911 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4912 m_currentCellCoords
.GetCol() );
4921 bool wxGrid::MoveCursorLeft()
4923 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4924 m_currentCellCoords
.GetCol() > 0 )
4926 MakeCellVisible( m_currentCellCoords
.GetRow(),
4927 m_currentCellCoords
.GetCol() - 1 );
4929 SetCurrentCell( m_currentCellCoords
.GetRow(),
4930 m_currentCellCoords
.GetCol() - 1 );
4939 bool wxGrid::MoveCursorRight()
4941 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4942 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4944 MakeCellVisible( m_currentCellCoords
.GetRow(),
4945 m_currentCellCoords
.GetCol() + 1 );
4947 SetCurrentCell( m_currentCellCoords
.GetRow(),
4948 m_currentCellCoords
.GetCol() + 1 );
4957 bool wxGrid::MovePageUp()
4959 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4961 int row
= m_currentCellCoords
.GetRow();
4965 m_gridWin
->GetClientSize( &cw
, &ch
);
4967 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4968 int newRow
= YToRow( y
- ch
+ 1 );
4973 else if ( newRow
== row
)
4978 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4979 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4987 bool wxGrid::MovePageDown()
4989 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4991 int row
= m_currentCellCoords
.GetRow();
4992 if ( row
< m_numRows
)
4995 m_gridWin
->GetClientSize( &cw
, &ch
);
4997 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4998 int newRow
= YToRow( y
+ ch
);
5001 newRow
= m_numRows
- 1;
5003 else if ( newRow
== row
)
5008 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5009 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5017 bool wxGrid::MoveCursorUpBlock()
5020 m_currentCellCoords
!= wxGridNoCellCoords
&&
5021 m_currentCellCoords
.GetRow() > 0 )
5023 int row
= m_currentCellCoords
.GetRow();
5024 int col
= m_currentCellCoords
.GetCol();
5026 if ( m_table
->IsEmptyCell(row
, col
) )
5028 // starting in an empty cell: find the next block of
5034 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5037 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5039 // starting at the top of a block: find the next block
5045 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5050 // starting within a block: find the top of the block
5055 if ( m_table
->IsEmptyCell(row
, col
) )
5063 MakeCellVisible( row
, col
);
5064 SetCurrentCell( row
, col
);
5072 bool wxGrid::MoveCursorDownBlock()
5075 m_currentCellCoords
!= wxGridNoCellCoords
&&
5076 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5078 int row
= m_currentCellCoords
.GetRow();
5079 int col
= m_currentCellCoords
.GetCol();
5081 if ( m_table
->IsEmptyCell(row
, col
) )
5083 // starting in an empty cell: find the next block of
5086 while ( row
< m_numRows
-1 )
5089 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5092 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5094 // starting at the bottom of a block: find the next block
5097 while ( row
< m_numRows
-1 )
5100 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5105 // starting within a block: find the bottom of the block
5107 while ( row
< m_numRows
-1 )
5110 if ( m_table
->IsEmptyCell(row
, col
) )
5118 MakeCellVisible( row
, col
);
5119 SetCurrentCell( row
, col
);
5127 bool wxGrid::MoveCursorLeftBlock()
5130 m_currentCellCoords
!= wxGridNoCellCoords
&&
5131 m_currentCellCoords
.GetCol() > 0 )
5133 int row
= m_currentCellCoords
.GetRow();
5134 int col
= m_currentCellCoords
.GetCol();
5136 if ( m_table
->IsEmptyCell(row
, col
) )
5138 // starting in an empty cell: find the next block of
5144 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5147 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5149 // starting at the left of a block: find the next block
5155 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5160 // starting within a block: find the left of the block
5165 if ( m_table
->IsEmptyCell(row
, col
) )
5173 MakeCellVisible( row
, col
);
5174 SetCurrentCell( row
, col
);
5182 bool wxGrid::MoveCursorRightBlock()
5185 m_currentCellCoords
!= wxGridNoCellCoords
&&
5186 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5188 int row
= m_currentCellCoords
.GetRow();
5189 int col
= m_currentCellCoords
.GetCol();
5191 if ( m_table
->IsEmptyCell(row
, col
) )
5193 // starting in an empty cell: find the next block of
5196 while ( col
< m_numCols
-1 )
5199 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5202 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5204 // starting at the right of a block: find the next block
5207 while ( col
< m_numCols
-1 )
5210 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5215 // starting within a block: find the right of the block
5217 while ( col
< m_numCols
-1 )
5220 if ( m_table
->IsEmptyCell(row
, col
) )
5228 MakeCellVisible( row
, col
);
5229 SetCurrentCell( row
, col
);
5240 // ------ Label values and formatting
5243 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5245 *horiz
= m_rowLabelHorizAlign
;
5246 *vert
= m_rowLabelVertAlign
;
5249 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5251 *horiz
= m_colLabelHorizAlign
;
5252 *vert
= m_colLabelVertAlign
;
5255 wxString
wxGrid::GetRowLabelValue( int row
)
5259 return m_table
->GetRowLabelValue( row
);
5269 wxString
wxGrid::GetColLabelValue( int col
)
5273 return m_table
->GetColLabelValue( col
);
5284 void wxGrid::SetRowLabelSize( int width
)
5286 width
= wxMax( width
, 0 );
5287 if ( width
!= m_rowLabelWidth
)
5291 m_rowLabelWin
->Show( FALSE
);
5292 m_cornerLabelWin
->Show( FALSE
);
5294 else if ( m_rowLabelWidth
== 0 )
5296 m_rowLabelWin
->Show( TRUE
);
5297 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5300 m_rowLabelWidth
= width
;
5307 void wxGrid::SetColLabelSize( int height
)
5309 height
= wxMax( height
, 0 );
5310 if ( height
!= m_colLabelHeight
)
5314 m_colLabelWin
->Show( FALSE
);
5315 m_cornerLabelWin
->Show( FALSE
);
5317 else if ( m_colLabelHeight
== 0 )
5319 m_colLabelWin
->Show( TRUE
);
5320 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5323 m_colLabelHeight
= height
;
5330 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5332 if ( m_labelBackgroundColour
!= colour
)
5334 m_labelBackgroundColour
= colour
;
5335 m_rowLabelWin
->SetBackgroundColour( colour
);
5336 m_colLabelWin
->SetBackgroundColour( colour
);
5337 m_cornerLabelWin
->SetBackgroundColour( colour
);
5339 if ( !GetBatchCount() )
5341 m_rowLabelWin
->Refresh();
5342 m_colLabelWin
->Refresh();
5343 m_cornerLabelWin
->Refresh();
5348 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5350 if ( m_labelTextColour
!= colour
)
5352 m_labelTextColour
= colour
;
5353 if ( !GetBatchCount() )
5355 m_rowLabelWin
->Refresh();
5356 m_colLabelWin
->Refresh();
5361 void wxGrid::SetLabelFont( const wxFont
& font
)
5364 if ( !GetBatchCount() )
5366 m_rowLabelWin
->Refresh();
5367 m_colLabelWin
->Refresh();
5371 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5373 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5375 m_rowLabelHorizAlign
= horiz
;
5378 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5380 m_rowLabelVertAlign
= vert
;
5383 if ( !GetBatchCount() )
5385 m_rowLabelWin
->Refresh();
5389 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5391 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5393 m_colLabelHorizAlign
= horiz
;
5396 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5398 m_colLabelVertAlign
= vert
;
5401 if ( !GetBatchCount() )
5403 m_colLabelWin
->Refresh();
5407 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5411 m_table
->SetRowLabelValue( row
, s
);
5412 if ( !GetBatchCount() )
5414 wxRect rect
= CellToRect( row
, 0);
5415 if ( rect
.height
> 0 )
5417 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5419 rect
.width
= m_rowLabelWidth
;
5420 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5426 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5430 m_table
->SetColLabelValue( col
, s
);
5431 if ( !GetBatchCount() )
5433 wxRect rect
= CellToRect( 0, col
);
5434 if ( rect
.width
> 0 )
5436 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5438 rect
.height
= m_colLabelHeight
;
5439 m_colLabelWin
->Refresh( TRUE
, &rect
);
5445 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5447 if ( m_gridLineColour
!= colour
)
5449 m_gridLineColour
= colour
;
5451 wxClientDC
dc( m_gridWin
);
5453 DrawAllGridLines( dc
, wxRegion() );
5457 void wxGrid::EnableGridLines( bool enable
)
5459 if ( enable
!= m_gridLinesEnabled
)
5461 m_gridLinesEnabled
= enable
;
5463 if ( !GetBatchCount() )
5467 wxClientDC
dc( m_gridWin
);
5469 DrawAllGridLines( dc
, wxRegion() );
5473 m_gridWin
->Refresh();
5480 int wxGrid::GetDefaultRowSize()
5482 return m_defaultRowHeight
;
5485 int wxGrid::GetRowSize( int row
)
5487 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5489 return m_rowHeights
[row
];
5492 int wxGrid::GetDefaultColSize()
5494 return m_defaultColWidth
;
5497 int wxGrid::GetColSize( int col
)
5499 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5501 return m_colWidths
[col
];
5504 // ============================================================================
5505 // access to the grid attributes: each of them has a default value in the grid
5506 // itself and may be overidden on a per-cell basis
5507 // ============================================================================
5509 // ----------------------------------------------------------------------------
5510 // setting default attributes
5511 // ----------------------------------------------------------------------------
5513 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5515 m_defaultCellAttr
->SetBackgroundColour(col
);
5518 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5520 m_defaultCellAttr
->SetTextColour(col
);
5523 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5525 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5528 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5530 m_defaultCellAttr
->SetFont(font
);
5533 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5535 m_defaultCellAttr
->SetRenderer(renderer
);
5538 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5540 m_defaultCellAttr
->SetEditor(editor
);
5543 // ----------------------------------------------------------------------------
5544 // access to the default attrbiutes
5545 // ----------------------------------------------------------------------------
5547 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5549 return m_defaultCellAttr
->GetBackgroundColour();
5552 wxColour
wxGrid::GetDefaultCellTextColour()
5554 return m_defaultCellAttr
->GetTextColour();
5557 wxFont
wxGrid::GetDefaultCellFont()
5559 return m_defaultCellAttr
->GetFont();
5562 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5564 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5567 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5569 return m_defaultCellAttr
->GetRenderer();
5572 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5574 return m_defaultCellAttr
->GetEditor();
5577 // ----------------------------------------------------------------------------
5578 // access to cell attributes
5579 // ----------------------------------------------------------------------------
5581 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5583 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5584 wxColour colour
= attr
->GetBackgroundColour();
5589 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5591 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5592 wxColour colour
= attr
->GetTextColour();
5597 wxFont
wxGrid::GetCellFont( int row
, int col
)
5599 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5600 wxFont font
= attr
->GetFont();
5605 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5607 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5608 attr
->GetAlignment(horiz
, vert
);
5612 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5614 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5615 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5620 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5622 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5623 wxGridCellEditor
* editor
= attr
->GetEditor();
5628 bool wxGrid::IsReadOnly(int row
, int col
) const
5630 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5631 bool isReadOnly
= attr
->IsReadOnly();
5636 // ----------------------------------------------------------------------------
5637 // attribute support: cache, automatic provider creation, ...
5638 // ----------------------------------------------------------------------------
5640 bool wxGrid::CanHaveAttributes()
5647 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5648 // table is providing the attributes itself??? In which case
5649 // I don't think the grid should create a Provider object for the
5650 // table but the table should be smart enough to do that on its own.
5651 if ( !m_table
->GetAttrProvider() )
5653 // use the default attr provider by default
5654 // (another choice would be to just return FALSE thus forcing the user
5656 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5662 void wxGrid::ClearAttrCache()
5664 if ( m_attrCache
.row
!= -1 )
5666 m_attrCache
.attr
->SafeDecRef();
5667 m_attrCache
.row
= -1;
5671 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5673 wxGrid
*self
= (wxGrid
*)this; // const_cast
5675 self
->ClearAttrCache();
5676 self
->m_attrCache
.row
= row
;
5677 self
->m_attrCache
.col
= col
;
5678 self
->m_attrCache
.attr
= attr
;
5682 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5684 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5686 *attr
= m_attrCache
.attr
;
5687 (*attr
)->SafeIncRef();
5689 #ifdef DEBUG_ATTR_CACHE
5690 gs_nAttrCacheHits
++;
5697 #ifdef DEBUG_ATTR_CACHE
5698 gs_nAttrCacheMisses
++;
5704 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5706 wxGridCellAttr
*attr
;
5707 if ( !LookupAttr(row
, col
, &attr
) )
5709 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5710 CacheAttr(row
, col
, attr
);
5714 attr
->SetDefAttr(m_defaultCellAttr
);
5718 attr
= m_defaultCellAttr
;
5725 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5727 wxGridCellAttr
*attr
;
5728 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5730 wxASSERT_MSG( m_table
,
5731 _T("we may only be called if CanHaveAttributes() "
5732 "returned TRUE and then m_table should be !NULL") );
5734 attr
= m_table
->GetAttr(row
, col
);
5737 attr
= new wxGridCellAttr
;
5739 // artificially inc the ref count to match DecRef() in caller
5742 m_table
->SetAttr(attr
, row
, col
);
5745 CacheAttr(row
, col
, attr
);
5747 attr
->SetDefAttr(m_defaultCellAttr
);
5751 // ----------------------------------------------------------------------------
5752 // setting cell attributes: this is forwarded to the table
5753 // ----------------------------------------------------------------------------
5755 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5757 if ( CanHaveAttributes() )
5759 m_table
->SetRowAttr(attr
, row
);
5767 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5769 if ( CanHaveAttributes() )
5771 m_table
->SetColAttr(attr
, col
);
5779 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5781 if ( CanHaveAttributes() )
5783 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5784 attr
->SetBackgroundColour(colour
);
5789 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5791 if ( CanHaveAttributes() )
5793 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5794 attr
->SetTextColour(colour
);
5799 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5801 if ( CanHaveAttributes() )
5803 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5804 attr
->SetFont(font
);
5809 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5811 if ( CanHaveAttributes() )
5813 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5814 attr
->SetAlignment(horiz
, vert
);
5819 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5821 if ( CanHaveAttributes() )
5823 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5824 attr
->SetRenderer(renderer
);
5829 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5831 if ( CanHaveAttributes() )
5833 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5834 attr
->SetEditor(editor
);
5839 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5841 if ( CanHaveAttributes() )
5843 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5844 attr
->SetReadOnly(isReadOnly
);
5849 // ----------------------------------------------------------------------------
5851 // ----------------------------------------------------------------------------
5853 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5855 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5857 if ( resizeExistingRows
)
5861 for ( row
= 0; row
< m_numRows
; row
++ )
5863 m_rowHeights
[row
] = m_defaultRowHeight
;
5864 bottom
+= m_defaultRowHeight
;
5865 m_rowBottoms
[row
] = bottom
;
5871 void wxGrid::SetRowSize( int row
, int height
)
5873 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5877 int h
= wxMax( 0, height
);
5878 int diff
= h
- m_rowHeights
[row
];
5880 m_rowHeights
[row
] = h
;
5881 for ( i
= row
; i
< m_numRows
; i
++ )
5883 m_rowBottoms
[i
] += diff
;
5888 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5890 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5892 if ( resizeExistingCols
)
5896 for ( col
= 0; col
< m_numCols
; col
++ )
5898 m_colWidths
[col
] = m_defaultColWidth
;
5899 right
+= m_defaultColWidth
;
5900 m_colRights
[col
] = right
;
5906 void wxGrid::SetColSize( int col
, int width
)
5908 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5912 int w
= wxMax( 0, width
);
5913 int diff
= w
- m_colWidths
[col
];
5914 m_colWidths
[col
] = w
;
5916 for ( i
= col
; i
< m_numCols
; i
++ )
5918 m_colRights
[i
] += diff
;
5925 // ------ cell value accessor functions
5928 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5932 m_table
->SetValue( row
, col
, s
.c_str() );
5933 if ( !GetBatchCount() )
5935 wxClientDC
dc( m_gridWin
);
5937 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5940 #if 0 // TODO: edit in place
5942 if ( m_currentCellCoords
.GetRow() == row
&&
5943 m_currentCellCoords
.GetCol() == col
)
5945 SetEditControlValue( s
);
5954 // ------ Block, row and col selection
5957 void wxGrid::SelectRow( int row
, bool addToSelected
)
5961 if ( IsSelection() && addToSelected
)
5964 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5967 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5968 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5969 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5970 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5974 need_refresh
[0] = TRUE
;
5975 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5976 wxGridCellCoords ( oldTop
- 1,
5978 m_selectedTopLeft
.SetRow( row
);
5983 need_refresh
[1] = TRUE
;
5984 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5985 wxGridCellCoords ( oldBottom
,
5988 m_selectedTopLeft
.SetCol( 0 );
5991 if ( oldBottom
< row
)
5993 need_refresh
[2] = TRUE
;
5994 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5995 wxGridCellCoords ( row
,
5997 m_selectedBottomRight
.SetRow( row
);
6000 if ( oldRight
< m_numCols
- 1 )
6002 need_refresh
[3] = TRUE
;
6003 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6005 wxGridCellCoords ( oldBottom
,
6007 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6010 for (i
= 0; i
< 4; i
++ )
6011 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6012 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6016 r
= SelectionToDeviceRect();
6018 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6020 m_selectedTopLeft
.Set( row
, 0 );
6021 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6022 r
= SelectionToDeviceRect();
6023 m_gridWin
->Refresh( FALSE
, &r
);
6026 wxGridRangeSelectEvent
gridEvt( GetId(),
6027 wxEVT_GRID_RANGE_SELECT
,
6030 m_selectedBottomRight
);
6032 GetEventHandler()->ProcessEvent(gridEvt
);
6036 void wxGrid::SelectCol( int col
, bool addToSelected
)
6038 if ( IsSelection() && addToSelected
)
6041 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
6044 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6045 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6046 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6047 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6049 if ( oldLeft
> col
)
6051 need_refresh
[0] = TRUE
;
6052 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6053 wxGridCellCoords ( m_numRows
- 1,
6055 m_selectedTopLeft
.SetCol( col
);
6060 need_refresh
[1] = TRUE
;
6061 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6062 wxGridCellCoords ( oldTop
- 1,
6064 m_selectedTopLeft
.SetRow( 0 );
6067 if ( oldRight
< col
)
6069 need_refresh
[2] = TRUE
;
6070 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6071 wxGridCellCoords ( m_numRows
- 1,
6073 m_selectedBottomRight
.SetCol( col
);
6076 if ( oldBottom
< m_numRows
- 1 )
6078 need_refresh
[3] = TRUE
;
6079 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6081 wxGridCellCoords ( m_numRows
- 1,
6083 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6086 for (i
= 0; i
< 4; i
++ )
6087 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6088 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6094 r
= SelectionToDeviceRect();
6096 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6098 m_selectedTopLeft
.Set( 0, col
);
6099 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6100 r
= SelectionToDeviceRect();
6101 m_gridWin
->Refresh( FALSE
, &r
);
6104 wxGridRangeSelectEvent
gridEvt( GetId(),
6105 wxEVT_GRID_RANGE_SELECT
,
6108 m_selectedBottomRight
);
6110 GetEventHandler()->ProcessEvent(gridEvt
);
6114 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6117 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6119 if ( topRow
> bottomRow
)
6126 if ( leftCol
> rightCol
)
6133 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6134 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6136 if ( m_selectedTopLeft
!= updateTopLeft
||
6137 m_selectedBottomRight
!= updateBottomRight
)
6139 // Compute two optimal update rectangles:
6140 // Either one rectangle is a real subset of the
6141 // other, or they are (almost) disjoint!
6143 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
6146 // Store intermediate values
6147 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6148 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6149 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6150 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6152 // Determine the outer/inner coordinates.
6153 if (oldLeft
> leftCol
)
6159 if (oldTop
> topRow
)
6165 if (oldRight
< rightCol
)
6168 oldRight
= rightCol
;
6171 if (oldBottom
< bottomRow
)
6174 oldBottom
= bottomRow
;
6178 // Now, either the stuff marked old is the outer
6179 // rectangle or we don't have a situation where one
6180 // is contained in the other.
6182 if ( oldLeft
< leftCol
)
6184 need_refresh
[0] = TRUE
;
6185 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6187 wxGridCellCoords ( oldBottom
,
6191 if ( oldTop
< topRow
)
6193 need_refresh
[1] = TRUE
;
6194 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6196 wxGridCellCoords ( topRow
- 1,
6200 if ( oldRight
> rightCol
)
6202 need_refresh
[2] = TRUE
;
6203 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6205 wxGridCellCoords ( oldBottom
,
6209 if ( oldBottom
> bottomRow
)
6211 need_refresh
[3] = TRUE
;
6212 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6214 wxGridCellCoords ( oldBottom
,
6220 m_selectedTopLeft
= updateTopLeft
;
6221 m_selectedBottomRight
= updateBottomRight
;
6223 // various Refresh() calls
6224 for (i
= 0; i
< 4; i
++ )
6225 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6226 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6229 // only generate an event if the block is not being selected by
6230 // dragging the mouse (in which case the event will be generated in
6231 // the mouse event handler)
6232 if ( !m_isDragging
)
6234 wxGridRangeSelectEvent
gridEvt( GetId(),
6235 wxEVT_GRID_RANGE_SELECT
,
6238 m_selectedBottomRight
);
6240 GetEventHandler()->ProcessEvent(gridEvt
);
6244 void wxGrid::SelectAll()
6246 m_selectedTopLeft
.Set( 0, 0 );
6247 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6249 m_gridWin
->Refresh();
6253 void wxGrid::ClearSelection()
6255 m_selectedTopLeft
= wxGridNoCellCoords
;
6256 m_selectedBottomRight
= wxGridNoCellCoords
;
6260 // This function returns the rectangle that encloses the given block
6261 // in device coords clipped to the client size of the grid window.
6263 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6264 const wxGridCellCoords
&bottomRight
)
6266 wxRect
rect( wxGridNoCellRect
);
6269 cellRect
= CellToRect( topLeft
);
6270 if ( cellRect
!= wxGridNoCellRect
)
6276 rect
= wxRect( 0, 0, 0, 0 );
6279 cellRect
= CellToRect( bottomRight
);
6280 if ( cellRect
!= wxGridNoCellRect
)
6286 return wxGridNoCellRect
;
6289 // convert to scrolled coords
6291 int left
, top
, right
, bottom
;
6292 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6293 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6296 m_gridWin
->GetClientSize( &cw
, &ch
);
6298 rect
.SetLeft( wxMax(0, left
) );
6299 rect
.SetTop( wxMax(0, top
) );
6300 rect
.SetRight( wxMin(cw
, right
) );
6301 rect
.SetBottom( wxMin(ch
, bottom
) );
6309 // ------ Grid event classes
6312 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6314 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6315 int row
, int col
, int x
, int y
,
6316 bool control
, bool shift
, bool alt
, bool meta
)
6317 : wxNotifyEvent( type
, id
)
6323 m_control
= control
;
6328 SetEventObject(obj
);
6332 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6334 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6335 int rowOrCol
, int x
, int y
,
6336 bool control
, bool shift
, bool alt
, bool meta
)
6337 : wxNotifyEvent( type
, id
)
6339 m_rowOrCol
= rowOrCol
;
6342 m_control
= control
;
6347 SetEventObject(obj
);
6351 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6353 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6354 const wxGridCellCoords
& topLeft
,
6355 const wxGridCellCoords
& bottomRight
,
6356 bool control
, bool shift
, bool alt
, bool meta
)
6357 : wxNotifyEvent( type
, id
)
6359 m_topLeft
= topLeft
;
6360 m_bottomRight
= bottomRight
;
6361 m_control
= control
;
6366 SetEventObject(obj
);
6370 #endif // ifndef wxUSE_NEW_GRID