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::Destroy()
306 m_control
->Destroy();
311 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
313 wxASSERT_MSG(m_control
,
314 wxT("The wxGridCellEditor must be Created first!"));
315 m_control
->Show(show
);
319 // set the colours/fonts if we have any
322 if ( attr
->HasTextColour() )
324 m_colFgOld
= m_control
->GetForegroundColour();
325 m_control
->SetForegroundColour(attr
->GetTextColour());
328 if ( attr
->HasBackgroundColour() )
330 m_colBgOld
= m_control
->GetBackgroundColour();
331 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
334 if ( attr
->HasFont() )
336 m_fontOld
= m_control
->GetFont();
337 m_control
->SetFont(attr
->GetFont());
340 // can't do anything more in the base class version, the other
341 // attributes may only be used by the derived classes
346 // restore the standard colours fonts
347 if ( m_colFgOld
.Ok() )
349 m_control
->SetForegroundColour(m_colFgOld
);
350 m_colFgOld
= wxNullColour
;
353 if ( m_colBgOld
.Ok() )
355 m_control
->SetBackgroundColour(m_colBgOld
);
356 m_colBgOld
= wxNullColour
;
359 if ( m_fontOld
.Ok() )
361 m_control
->SetFont(m_fontOld
);
362 m_fontOld
= wxNullFont
;
367 void wxGridCellEditor::SetSize(const wxRect
& rect
)
369 wxASSERT_MSG(m_control
,
370 wxT("The wxGridCellEditor must be Created first!"));
371 m_control
->SetSize(rect
);
374 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
380 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
382 wxASSERT_MSG(m_control
,
383 wxT("The wxGridCellEditor must be Created first!"));
385 // pass the event to the control
386 m_control
->GetEventHandler()->ProcessEvent(event
);
389 // ----------------------------------------------------------------------------
390 // wxGridCellTextEditor
391 // ----------------------------------------------------------------------------
393 wxGridCellTextEditor::wxGridCellTextEditor()
397 void wxGridCellTextEditor::Create(wxWindow
* parent
,
399 wxEvtHandler
* evtHandler
)
401 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
402 wxDefaultPosition
, wxDefaultSize
403 #if defined(__WXMSW__)
404 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
408 wxGridCellEditor::Create(parent
, id
, evtHandler
);
412 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
414 wxASSERT_MSG(m_control
,
415 wxT("The wxGridCellEditor must be Created first!"));
417 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
418 Text()->SetValue(m_startValue
);
419 Text()->SetInsertionPointEnd();
424 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
427 wxASSERT_MSG(m_control
,
428 wxT("The wxGridCellEditor must be Created first!"));
430 bool changed
= FALSE
;
431 wxString value
= Text()->GetValue();
432 if (value
!= m_startValue
)
436 grid
->GetTable()->SetValue(row
, col
, value
);
438 m_startValue
= wxEmptyString
;
439 Text()->SetValue(m_startValue
);
445 void wxGridCellTextEditor::Reset()
447 wxASSERT_MSG(m_control
,
448 wxT("The wxGridCellEditor must be Created first!"));
450 Text()->SetValue(m_startValue
);
451 Text()->SetInsertionPointEnd();
454 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
456 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
458 // insert the key in the control
459 long keycode
= event
.KeyCode();
460 if ( isprint(keycode
) )
462 // FIXME this is not going to work for non letters...
463 if ( !event
.ShiftDown() )
465 keycode
= tolower(keycode
);
468 Text()->AppendText((wxChar
)keycode
);
478 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
480 #if defined(__WXMOTIF__) || defined(__WXGTK__)
481 // wxMotif needs a little extra help...
482 int pos
= Text()->GetInsertionPoint();
483 wxString
s( Text()->GetValue() );
484 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
486 Text()->SetInsertionPoint( pos
);
488 // the other ports can handle a Return key press
494 // ----------------------------------------------------------------------------
495 // wxGridCellBoolEditor
496 // ----------------------------------------------------------------------------
498 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
500 wxEvtHandler
* evtHandler
)
502 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
503 wxDefaultPosition
, wxDefaultSize
,
506 wxGridCellEditor::Create(parent
, id
, evtHandler
);
509 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
512 // position it in the centre of the rectangle (TODO: support alignment?)
514 m_control
->GetSize(&w
, &h
);
516 // the checkbox without label still has some space to the right in wxGTK,
517 // so shift it to the right
522 m_control
->Move(r
.x
+ r
.width
/2 - w
/2, r
.y
+ r
.height
/2 - h
/2);
525 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
527 wxGridCellEditor::Show(show
, attr
);
531 // get the bg colour to use
532 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
534 // erase the background because we don't fill the cell
535 if ( m_rectCell
.width
> 0 )
537 wxClientDC
dc(m_control
->GetParent());
538 dc
.SetPen(*wxTRANSPARENT_PEN
);
539 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
540 dc
.DrawRectangle(m_rectCell
);
542 m_rectCell
.width
= 0;
545 CBox()->SetBackgroundColour(colBg
);
548 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
550 wxASSERT_MSG(m_control
,
551 wxT("The wxGridCellEditor must be Created first!"));
553 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
); // FIXME-DATA
554 CBox()->SetValue(m_startValue
);
558 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
562 wxASSERT_MSG(m_control
,
563 wxT("The wxGridCellEditor must be Created first!"));
565 bool changed
= FALSE
;
566 bool value
= CBox()->GetValue();
567 if ( value
!= m_startValue
)
573 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
579 void wxGridCellBoolEditor::Reset()
581 wxASSERT_MSG(m_control
,
582 wxT("The wxGridCellEditor must be Created first!"));
584 CBox()->SetValue(m_startValue
);
587 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
592 // ----------------------------------------------------------------------------
593 // wxGridCellEditorEvtHandler
594 // ----------------------------------------------------------------------------
596 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
598 switch ( event
.KeyCode() )
602 m_grid
->DisableCellEditControl();
606 event
.Skip( m_grid
->ProcessEvent( event
) );
610 if (!m_grid
->ProcessEvent(event
))
611 m_editor
->HandleReturn(event
);
620 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
622 switch ( event
.KeyCode() )
634 // ============================================================================
636 // ============================================================================
638 // ----------------------------------------------------------------------------
639 // wxGridCellRenderer
640 // ----------------------------------------------------------------------------
642 void wxGridCellRenderer::Draw(wxGrid
& grid
,
643 wxGridCellAttr
& attr
,
649 dc
.SetBackgroundMode( wxSOLID
);
653 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
657 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
660 dc
.SetPen( *wxTRANSPARENT_PEN
);
661 dc
.DrawRectangle(rect
);
664 // ----------------------------------------------------------------------------
665 // wxGridCellStringRenderer
666 // ----------------------------------------------------------------------------
668 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
669 wxGridCellAttr
& attr
,
671 const wxRect
& rectCell
,
675 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
677 // now we only have to draw the text
678 dc
.SetBackgroundMode( wxTRANSPARENT
);
680 // TODO some special colours for attr.IsReadOnly() case?
684 dc
.SetTextBackground( grid
.GetSelectionBackground() );
685 dc
.SetTextForeground( grid
.GetSelectionForeground() );
689 dc
.SetTextBackground( attr
.GetBackgroundColour() );
690 dc
.SetTextForeground( attr
.GetTextColour() );
692 dc
.SetFont( attr
.GetFont() );
695 attr
.GetAlignment(&hAlign
, &vAlign
);
697 wxRect rect
= rectCell
;
703 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
704 rect
, hAlign
, vAlign
);
707 // ----------------------------------------------------------------------------
708 // wxGridCellBoolRenderer
709 // ----------------------------------------------------------------------------
711 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
712 wxGridCellAttr
& attr
,
718 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
720 // position it in the centre (ignoring alignment - TODO)
721 static const wxCoord checkSize
= 16;
724 rectMark
.x
= rect
.x
+ rect
.width
/2 - checkSize
/2;
725 rectMark
.y
= rect
.y
+ rect
.height
/2 - checkSize
/2;
726 rectMark
.width
= rectMark
.height
= checkSize
;
728 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
729 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
730 dc
.DrawRectangle(rectMark
);
732 rectMark
.Inflate(-4);
734 if ( !!grid
.GetTable()->GetValue(row
, col
) ) // FIXME-DATA
736 dc
.SetTextForeground(attr
.GetTextColour());
737 dc
.DrawCheckMark(rectMark
);
741 // ----------------------------------------------------------------------------
743 // ----------------------------------------------------------------------------
745 const wxColour
& wxGridCellAttr::GetTextColour() const
751 else if (m_defGridAttr
!= this)
753 return m_defGridAttr
->GetTextColour();
757 wxFAIL_MSG(wxT("Missing default cell attribute"));
763 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
765 if (HasBackgroundColour())
767 else if (m_defGridAttr
!= this)
768 return m_defGridAttr
->GetBackgroundColour();
771 wxFAIL_MSG(wxT("Missing default cell attribute"));
777 const wxFont
& wxGridCellAttr::GetFont() const
781 else if (m_defGridAttr
!= this)
782 return m_defGridAttr
->GetFont();
785 wxFAIL_MSG(wxT("Missing default cell attribute"));
791 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
795 if ( hAlign
) *hAlign
= m_hAlign
;
796 if ( vAlign
) *vAlign
= m_vAlign
;
798 else if (m_defGridAttr
!= this)
799 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
802 wxFAIL_MSG(wxT("Missing default cell attribute"));
807 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
811 else if (m_defGridAttr
!= this)
812 return m_defGridAttr
->GetRenderer();
815 wxFAIL_MSG(wxT("Missing default cell attribute"));
820 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
824 else if (m_defGridAttr
!= this)
825 return m_defGridAttr
->GetEditor();
828 wxFAIL_MSG(wxT("Missing default cell attribute"));
833 // ----------------------------------------------------------------------------
834 // wxGridCellAttrData
835 // ----------------------------------------------------------------------------
837 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
839 int n
= FindIndex(row
, col
);
840 if ( n
== wxNOT_FOUND
)
843 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
849 // change the attribute
850 m_attrs
[(size_t)n
].attr
= attr
;
854 // remove this attribute
855 m_attrs
.RemoveAt((size_t)n
);
860 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
862 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
864 int n
= FindIndex(row
, col
);
865 if ( n
!= wxNOT_FOUND
)
867 attr
= m_attrs
[(size_t)n
].attr
;
874 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
876 size_t count
= m_attrs
.GetCount();
877 for ( size_t n
= 0; n
< count
; n
++ )
879 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
880 wxCoord row
= coords
.GetRow();
881 if ((size_t)row
>= pos
)
885 // If rows inserted, include row counter where necessary
886 coords
.SetRow(row
+ numRows
);
888 else if (numRows
< 0)
890 // If rows deleted ...
891 if ((size_t)row
>= pos
- numRows
)
893 // ...either decrement row counter (if row still exists)...
894 coords
.SetRow(row
+ numRows
);
898 // ...or remove the attribute
899 m_attrs
.RemoveAt((size_t)n
);
907 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
909 size_t count
= m_attrs
.GetCount();
910 for ( size_t n
= 0; n
< count
; n
++ )
912 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
913 wxCoord col
= coords
.GetCol();
914 if ( (size_t)col
>= pos
)
918 // If rows inserted, include row counter where necessary
919 coords
.SetCol(col
+ numCols
);
921 else if (numCols
< 0)
923 // If rows deleted ...
924 if ((size_t)col
>= pos
- numCols
)
926 // ...either decrement row counter (if row still exists)...
927 coords
.SetCol(col
+ numCols
);
931 // ...or remove the attribute
932 m_attrs
.RemoveAt((size_t)n
);
940 int wxGridCellAttrData::FindIndex(int row
, int col
) const
942 size_t count
= m_attrs
.GetCount();
943 for ( size_t n
= 0; n
< count
; n
++ )
945 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
946 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
955 // ----------------------------------------------------------------------------
956 // wxGridRowOrColAttrData
957 // ----------------------------------------------------------------------------
959 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
961 size_t count
= m_attrs
.Count();
962 for ( size_t n
= 0; n
< count
; n
++ )
964 m_attrs
[n
]->DecRef();
968 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
970 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
972 int n
= m_rowsOrCols
.Index(rowOrCol
);
973 if ( n
!= wxNOT_FOUND
)
975 attr
= m_attrs
[(size_t)n
];
982 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
984 int n
= m_rowsOrCols
.Index(rowOrCol
);
985 if ( n
== wxNOT_FOUND
)
988 m_rowsOrCols
.Add(rowOrCol
);
995 // change the attribute
996 m_attrs
[(size_t)n
] = attr
;
1000 // remove this attribute
1001 m_attrs
[(size_t)n
]->DecRef();
1002 m_rowsOrCols
.RemoveAt((size_t)n
);
1003 m_attrs
.RemoveAt((size_t)n
);
1008 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1010 size_t count
= m_attrs
.GetCount();
1011 for ( size_t n
= 0; n
< count
; n
++ )
1013 int & rowOrCol
= m_rowsOrCols
[n
];
1014 if ( (size_t)rowOrCol
>= pos
)
1016 if ( numRowsOrCols
> 0 )
1018 // If rows inserted, include row counter where necessary
1019 rowOrCol
+= numRowsOrCols
;
1021 else if ( numRowsOrCols
< 0)
1023 // If rows deleted, either decrement row counter (if row still exists)
1024 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1025 rowOrCol
+= numRowsOrCols
;
1028 m_rowsOrCols
.RemoveAt((size_t)n
);
1029 m_attrs
.RemoveAt((size_t)n
);
1037 // ----------------------------------------------------------------------------
1038 // wxGridCellAttrProvider
1039 // ----------------------------------------------------------------------------
1041 wxGridCellAttrProvider::wxGridCellAttrProvider()
1043 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1046 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1051 void wxGridCellAttrProvider::InitData()
1053 m_data
= new wxGridCellAttrProviderData
;
1056 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1058 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1061 // first look for the attribute of this specific cell
1062 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1066 // then look for the col attr (col attributes are more common than
1067 // the row ones, hence they have priority)
1068 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1073 // finally try the row attributes
1074 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1081 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1087 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1090 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1095 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1098 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1103 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1106 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1110 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1112 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1116 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1120 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1122 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1126 // ----------------------------------------------------------------------------
1128 // ----------------------------------------------------------------------------
1130 //////////////////////////////////////////////////////////////////////
1132 // Abstract base class for grid data (the model)
1134 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1137 wxGridTableBase::wxGridTableBase()
1139 m_view
= (wxGrid
*) NULL
;
1140 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1143 wxGridTableBase::~wxGridTableBase()
1145 delete m_attrProvider
;
1148 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1150 delete m_attrProvider
;
1151 m_attrProvider
= attrProvider
;
1154 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1156 if ( m_attrProvider
)
1157 return m_attrProvider
->GetAttr(row
, col
);
1159 return (wxGridCellAttr
*)NULL
;
1162 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1164 if ( m_attrProvider
)
1166 m_attrProvider
->SetAttr(attr
, row
, col
);
1170 // as we take ownership of the pointer and don't store it, we must
1176 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1178 if ( m_attrProvider
)
1180 m_attrProvider
->SetRowAttr(attr
, row
);
1184 // as we take ownership of the pointer and don't store it, we must
1190 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1192 if ( m_attrProvider
)
1194 m_attrProvider
->SetColAttr(attr
, col
);
1198 // as we take ownership of the pointer and don't store it, we must
1204 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1206 if ( m_attrProvider
)
1208 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1212 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1214 if ( m_attrProvider
)
1216 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1220 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1222 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1223 "but your derived table class does not override this function") );
1228 bool wxGridTableBase::AppendRows( size_t numRows
)
1230 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1231 "but your derived table class does not override this function"));
1236 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1238 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1239 "but your derived table class does not override this function"));
1244 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1246 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1247 "but your derived table class does not override this function"));
1252 bool wxGridTableBase::AppendCols( size_t numCols
)
1254 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1255 "but your derived table class does not override this function"));
1260 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1262 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1263 "but your derived table class does not override this function"));
1269 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1276 wxString
wxGridTableBase::GetColLabelValue( int col
)
1278 // default col labels are:
1279 // cols 0 to 25 : A-Z
1280 // cols 26 to 675 : AA-ZZ
1285 for ( n
= 1; ; n
++ )
1287 s
+= (_T('A') + (wxChar
)( col%26
));
1289 if ( col
< 0 ) break;
1292 // reverse the string...
1294 for ( i
= 0; i
< n
; i
++ )
1304 //////////////////////////////////////////////////////////////////////
1306 // Message class for the grid table to send requests and notifications
1310 wxGridTableMessage::wxGridTableMessage()
1312 m_table
= (wxGridTableBase
*) NULL
;
1318 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1319 int commandInt1
, int commandInt2
)
1323 m_comInt1
= commandInt1
;
1324 m_comInt2
= commandInt2
;
1329 //////////////////////////////////////////////////////////////////////
1331 // A basic grid table for string data. An object of this class will
1332 // created by wxGrid if you don't specify an alternative table class.
1335 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1337 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1339 wxGridStringTable::wxGridStringTable()
1344 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1349 m_data
.Alloc( numRows
);
1352 sa
.Alloc( numCols
);
1353 for ( col
= 0; col
< numCols
; col
++ )
1355 sa
.Add( wxEmptyString
);
1358 for ( row
= 0; row
< numRows
; row
++ )
1364 wxGridStringTable::~wxGridStringTable()
1368 long wxGridStringTable::GetNumberRows()
1370 return m_data
.GetCount();
1373 long wxGridStringTable::GetNumberCols()
1375 if ( m_data
.GetCount() > 0 )
1376 return m_data
[0].GetCount();
1381 wxString
wxGridStringTable::GetValue( int row
, int col
)
1383 // TODO: bounds checking
1385 return m_data
[row
][col
];
1388 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1390 // TODO: bounds checking
1392 m_data
[row
][col
] = s
;
1395 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1397 // TODO: bounds checking
1399 return (m_data
[row
][col
] == wxEmptyString
);
1403 void wxGridStringTable::Clear()
1406 int numRows
, numCols
;
1408 numRows
= m_data
.GetCount();
1411 numCols
= m_data
[0].GetCount();
1413 for ( row
= 0; row
< numRows
; row
++ )
1415 for ( col
= 0; col
< numCols
; col
++ )
1417 m_data
[row
][col
] = wxEmptyString
;
1424 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1428 size_t curNumRows
= m_data
.GetCount();
1429 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1431 if ( pos
>= curNumRows
)
1433 return AppendRows( numRows
);
1437 sa
.Alloc( curNumCols
);
1438 for ( col
= 0; col
< curNumCols
; col
++ )
1440 sa
.Add( wxEmptyString
);
1443 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1445 m_data
.Insert( sa
, row
);
1447 UpdateAttrRows( pos
, numRows
);
1450 wxGridTableMessage
msg( this,
1451 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1455 GetView()->ProcessTableMessage( msg
);
1461 bool wxGridStringTable::AppendRows( size_t numRows
)
1465 size_t curNumRows
= m_data
.GetCount();
1466 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1469 if ( curNumCols
> 0 )
1471 sa
.Alloc( curNumCols
);
1472 for ( col
= 0; col
< curNumCols
; col
++ )
1474 sa
.Add( wxEmptyString
);
1478 for ( row
= 0; row
< numRows
; row
++ )
1485 wxGridTableMessage
msg( this,
1486 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1489 GetView()->ProcessTableMessage( msg
);
1495 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1499 size_t curNumRows
= m_data
.GetCount();
1501 if ( pos
>= curNumRows
)
1504 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1505 "Pos value is invalid for present table with %d rows",
1506 pos
, numRows
, curNumRows
);
1507 wxFAIL_MSG( wxT(errmsg
) );
1511 if ( numRows
> curNumRows
- pos
)
1513 numRows
= curNumRows
- pos
;
1516 if ( numRows
>= curNumRows
)
1518 m_data
.Empty(); // don't release memory just yet
1522 for ( n
= 0; n
< numRows
; n
++ )
1524 m_data
.Remove( pos
);
1527 UpdateAttrRows( pos
, -((int)numRows
) );
1530 wxGridTableMessage
msg( this,
1531 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1535 GetView()->ProcessTableMessage( msg
);
1541 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1545 size_t curNumRows
= m_data
.GetCount();
1546 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1548 if ( pos
>= curNumCols
)
1550 return AppendCols( numCols
);
1553 for ( row
= 0; row
< curNumRows
; row
++ )
1555 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1557 m_data
[row
].Insert( wxEmptyString
, col
);
1560 UpdateAttrCols( pos
, numCols
);
1563 wxGridTableMessage
msg( this,
1564 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1568 GetView()->ProcessTableMessage( msg
);
1574 bool wxGridStringTable::AppendCols( size_t numCols
)
1578 size_t curNumRows
= m_data
.GetCount();
1581 // TODO: something better than this ?
1583 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1584 "Call AppendRows() first") );
1588 for ( row
= 0; row
< curNumRows
; row
++ )
1590 for ( n
= 0; n
< numCols
; n
++ )
1592 m_data
[row
].Add( wxEmptyString
);
1598 wxGridTableMessage
msg( this,
1599 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1602 GetView()->ProcessTableMessage( msg
);
1608 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1612 size_t curNumRows
= m_data
.GetCount();
1613 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1615 if ( pos
>= curNumCols
)
1618 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1619 "Pos value is invalid for present table with %d cols",
1620 pos
, numCols
, curNumCols
);
1621 wxFAIL_MSG( wxT( errmsg
) );
1625 if ( numCols
> curNumCols
- pos
)
1627 numCols
= curNumCols
- pos
;
1630 for ( row
= 0; row
< curNumRows
; row
++ )
1632 if ( numCols
>= curNumCols
)
1634 m_data
[row
].Clear();
1638 for ( n
= 0; n
< numCols
; n
++ )
1640 m_data
[row
].Remove( pos
);
1644 UpdateAttrCols( pos
, -((int)numCols
) );
1647 wxGridTableMessage
msg( this,
1648 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1652 GetView()->ProcessTableMessage( msg
);
1658 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1660 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1662 // using default label
1664 return wxGridTableBase::GetRowLabelValue( row
);
1668 return m_rowLabels
[ row
];
1672 wxString
wxGridStringTable::GetColLabelValue( int col
)
1674 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1676 // using default label
1678 return wxGridTableBase::GetColLabelValue( col
);
1682 return m_colLabels
[ col
];
1686 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1688 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1690 int n
= m_rowLabels
.GetCount();
1692 for ( i
= n
; i
<= row
; i
++ )
1694 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1698 m_rowLabels
[row
] = value
;
1701 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1703 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1705 int n
= m_colLabels
.GetCount();
1707 for ( i
= n
; i
<= col
; i
++ )
1709 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1713 m_colLabels
[col
] = value
;
1718 //////////////////////////////////////////////////////////////////////
1719 //////////////////////////////////////////////////////////////////////
1721 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1723 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1724 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1725 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1726 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1729 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1731 const wxPoint
&pos
, const wxSize
&size
)
1732 : wxWindow( parent
, id
, pos
, size
)
1737 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1741 // NO - don't do this because it will set both the x and y origin
1742 // coords to match the parent scrolled window and we just want to
1743 // set the y coord - MB
1745 // m_owner->PrepareDC( dc );
1748 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1749 dc
.SetDeviceOrigin( 0, -y
);
1751 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1752 m_owner
->DrawRowLabels( dc
);
1756 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1758 m_owner
->ProcessRowLabelMouseEvent( event
);
1762 // This seems to be required for wxMotif otherwise the mouse
1763 // cursor must be in the cell edit control to get key events
1765 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1767 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1772 //////////////////////////////////////////////////////////////////////
1774 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1776 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1777 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1778 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1779 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1782 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1784 const wxPoint
&pos
, const wxSize
&size
)
1785 : wxWindow( parent
, id
, pos
, size
)
1790 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1794 // NO - don't do this because it will set both the x and y origin
1795 // coords to match the parent scrolled window and we just want to
1796 // set the x coord - MB
1798 // m_owner->PrepareDC( dc );
1801 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1802 dc
.SetDeviceOrigin( -x
, 0 );
1804 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1805 m_owner
->DrawColLabels( dc
);
1809 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1811 m_owner
->ProcessColLabelMouseEvent( event
);
1815 // This seems to be required for wxMotif otherwise the mouse
1816 // cursor must be in the cell edit control to get key events
1818 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1820 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1825 //////////////////////////////////////////////////////////////////////
1827 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1829 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1830 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1831 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1832 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1835 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1837 const wxPoint
&pos
, const wxSize
&size
)
1838 : wxWindow( parent
, id
, pos
, size
)
1843 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1847 int client_height
= 0;
1848 int client_width
= 0;
1849 GetClientSize( &client_width
, &client_height
);
1851 dc
.SetPen( *wxBLACK_PEN
);
1852 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1853 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1855 dc
.SetPen( *wxWHITE_PEN
);
1856 dc
.DrawLine( 0, 0, client_width
, 0 );
1857 dc
.DrawLine( 0, 0, 0, client_height
);
1861 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1863 m_owner
->ProcessCornerLabelMouseEvent( event
);
1867 // This seems to be required for wxMotif otherwise the mouse
1868 // cursor must be in the cell edit control to get key events
1870 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1872 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1877 //////////////////////////////////////////////////////////////////////
1879 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1881 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1882 EVT_PAINT( wxGridWindow::OnPaint
)
1883 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1884 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1885 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1888 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1889 wxGridRowLabelWindow
*rowLblWin
,
1890 wxGridColLabelWindow
*colLblWin
,
1891 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1892 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1895 m_rowLabelWin
= rowLblWin
;
1896 m_colLabelWin
= colLblWin
;
1897 SetBackgroundColour( "WHITE" );
1901 wxGridWindow::~wxGridWindow()
1906 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1908 wxPaintDC
dc( this );
1909 m_owner
->PrepareDC( dc
);
1910 wxRegion reg
= GetUpdateRegion();
1911 m_owner
->CalcCellsExposed( reg
);
1912 m_owner
->DrawGridCellArea( dc
);
1913 #if WXGRID_DRAW_LINES
1914 m_owner
->DrawAllGridLines( dc
, reg
);
1919 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1921 wxPanel::ScrollWindow( dx
, dy
, rect
);
1922 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1923 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1927 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1929 m_owner
->ProcessGridCellMouseEvent( event
);
1933 // This seems to be required for wxMotif otherwise the mouse
1934 // cursor must be in the cell edit control to get key events
1936 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1938 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1941 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1947 //////////////////////////////////////////////////////////////////////
1950 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1952 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1953 EVT_PAINT( wxGrid::OnPaint
)
1954 EVT_SIZE( wxGrid::OnSize
)
1955 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1956 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1959 wxGrid::wxGrid( wxWindow
*parent
,
1964 const wxString
& name
)
1965 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1974 m_defaultCellAttr
->SafeDecRef();
1976 #ifdef DEBUG_ATTR_CACHE
1977 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1978 wxPrintf(_T("wxGrid attribute cache statistics: "
1979 "total: %u, hits: %u (%u%%)\n"),
1980 total
, gs_nAttrCacheHits
,
1981 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1990 // ----- internal init and update functions
1993 void wxGrid::Create()
1995 m_created
= FALSE
; // set to TRUE by CreateGrid
1996 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
1998 m_table
= (wxGridTableBase
*) NULL
;
2001 m_cellEditCtrlEnabled
= FALSE
;
2003 m_defaultCellAttr
= new wxGridCellAttr
;
2004 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2005 // RD: Should we fill the default attrs now or is waiting until Init() okay?
2010 m_currentCellCoords
= wxGridNoCellCoords
;
2012 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2013 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2015 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2020 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2025 m_colLabelWin
= new wxGridColLabelWindow( this,
2030 m_gridWin
= new wxGridWindow( this,
2037 SetTargetWindow( m_gridWin
);
2041 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2045 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2050 m_numRows
= numRows
;
2051 m_numCols
= numCols
;
2053 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2054 m_table
->SetView( this );
2063 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2067 // RD: Actually, this should probably be allowed. I think it would be
2068 // nice to be able to switch multiple Tables in and out of a single
2069 // View at runtime. Is there anything in the implmentation that would
2072 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2077 m_numRows
= table
->GetNumberRows();
2078 m_numCols
= table
->GetNumberCols();
2081 m_table
->SetView( this );
2096 if ( m_numRows
<= 0 )
2097 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2099 if ( m_numCols
<= 0 )
2100 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2102 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2103 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2105 if ( m_rowLabelWin
)
2107 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2111 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2114 m_labelTextColour
= wxColour( _T("BLACK") );
2117 m_attrCache
.row
= -1;
2119 // TODO: something better than this ?
2121 m_labelFont
= this->GetFont();
2122 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2124 m_rowLabelHorizAlign
= wxLEFT
;
2125 m_rowLabelVertAlign
= wxCENTRE
;
2127 m_colLabelHorizAlign
= wxCENTRE
;
2128 m_colLabelVertAlign
= wxTOP
;
2130 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2131 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2133 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2134 m_defaultRowHeight
+= 8;
2136 m_defaultRowHeight
+= 4;
2139 m_rowHeights
.Alloc( m_numRows
);
2140 m_rowBottoms
.Alloc( m_numRows
);
2142 for ( i
= 0; i
< m_numRows
; i
++ )
2144 m_rowHeights
.Add( m_defaultRowHeight
);
2145 rowBottom
+= m_defaultRowHeight
;
2146 m_rowBottoms
.Add( rowBottom
);
2149 m_colWidths
.Alloc( m_numCols
);
2150 m_colRights
.Alloc( m_numCols
);
2152 for ( i
= 0; i
< m_numCols
; i
++ )
2154 m_colWidths
.Add( m_defaultColWidth
);
2155 colRight
+= m_defaultColWidth
;
2156 m_colRights
.Add( colRight
);
2159 // Set default cell attributes
2160 m_defaultCellAttr
->SetFont(GetFont());
2161 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2162 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2163 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2164 m_defaultCellAttr
->SetTextColour(
2165 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2166 m_defaultCellAttr
->SetBackgroundColour(
2167 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2170 m_gridLineColour
= wxColour( 128, 128, 255 );
2171 m_gridLinesEnabled
= TRUE
;
2173 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2174 m_winCapture
= (wxWindow
*)NULL
;
2176 m_dragRowOrCol
= -1;
2177 m_isDragging
= FALSE
;
2178 m_startDragPos
= wxDefaultPosition
;
2180 m_waitForSlowClick
= FALSE
;
2182 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2183 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2185 m_currentCellCoords
= wxGridNoCellCoords
;
2187 m_selectedTopLeft
= wxGridNoCellCoords
;
2188 m_selectedBottomRight
= wxGridNoCellCoords
;
2189 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2190 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2192 m_editable
= TRUE
; // default for whole grid
2194 m_inOnKeyDown
= FALSE
;
2200 void wxGrid::CalcDimensions()
2203 GetClientSize( &cw
, &ch
);
2205 if ( m_numRows
> 0 && m_numCols
> 0 )
2207 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2208 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2210 // TODO: restore the scroll position that we had before sizing
2213 GetViewStart( &x
, &y
);
2214 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2215 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2221 void wxGrid::CalcWindowSizes()
2224 GetClientSize( &cw
, &ch
);
2226 if ( m_cornerLabelWin
->IsShown() )
2227 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2229 if ( m_colLabelWin
->IsShown() )
2230 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2232 if ( m_rowLabelWin
->IsShown() )
2233 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2235 if ( m_gridWin
->IsShown() )
2236 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2240 // this is called when the grid table sends a message to say that it
2241 // has been redimensioned
2243 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2247 switch ( msg
.GetId() )
2249 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2251 size_t pos
= msg
.GetCommandInt();
2252 int numRows
= msg
.GetCommandInt2();
2253 for ( i
= 0; i
< numRows
; i
++ )
2255 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2256 m_rowBottoms
.Insert( 0, pos
);
2258 m_numRows
+= numRows
;
2261 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2263 for ( i
= pos
; i
< m_numRows
; i
++ )
2265 bottom
+= m_rowHeights
[i
];
2266 m_rowBottoms
[i
] = bottom
;
2272 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2274 int numRows
= msg
.GetCommandInt();
2275 for ( i
= 0; i
< numRows
; i
++ )
2277 m_rowHeights
.Add( m_defaultRowHeight
);
2278 m_rowBottoms
.Add( 0 );
2281 int oldNumRows
= m_numRows
;
2282 m_numRows
+= numRows
;
2285 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2287 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2289 bottom
+= m_rowHeights
[i
];
2290 m_rowBottoms
[i
] = bottom
;
2296 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2298 size_t pos
= msg
.GetCommandInt();
2299 int numRows
= msg
.GetCommandInt2();
2300 for ( i
= 0; i
< numRows
; i
++ )
2302 m_rowHeights
.Remove( pos
);
2303 m_rowBottoms
.Remove( pos
);
2305 m_numRows
-= numRows
;
2310 m_colWidths
.Clear();
2311 m_colRights
.Clear();
2312 m_currentCellCoords
= wxGridNoCellCoords
;
2316 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2317 m_currentCellCoords
.Set( 0, 0 );
2320 for ( i
= 0; i
< m_numRows
; i
++ )
2322 h
+= m_rowHeights
[i
];
2323 m_rowBottoms
[i
] = h
;
2331 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2333 size_t pos
= msg
.GetCommandInt();
2334 int numCols
= msg
.GetCommandInt2();
2335 for ( i
= 0; i
< numCols
; i
++ )
2337 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2338 m_colRights
.Insert( 0, pos
);
2340 m_numCols
+= numCols
;
2343 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2345 for ( i
= pos
; i
< m_numCols
; i
++ )
2347 right
+= m_colWidths
[i
];
2348 m_colRights
[i
] = right
;
2354 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2356 int numCols
= msg
.GetCommandInt();
2357 for ( i
= 0; i
< numCols
; i
++ )
2359 m_colWidths
.Add( m_defaultColWidth
);
2360 m_colRights
.Add( 0 );
2363 int oldNumCols
= m_numCols
;
2364 m_numCols
+= numCols
;
2367 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2369 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2371 right
+= m_colWidths
[i
];
2372 m_colRights
[i
] = right
;
2378 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2380 size_t pos
= msg
.GetCommandInt();
2381 int numCols
= msg
.GetCommandInt2();
2382 for ( i
= 0; i
< numCols
; i
++ )
2384 m_colWidths
.Remove( pos
);
2385 m_colRights
.Remove( pos
);
2387 m_numCols
-= numCols
;
2391 #if 0 // leave the row alone here so that AppendCols will work subsequently
2393 m_rowHeights
.Clear();
2394 m_rowBottoms
.Clear();
2396 m_currentCellCoords
= wxGridNoCellCoords
;
2400 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2401 m_currentCellCoords
.Set( 0, 0 );
2404 for ( i
= 0; i
< m_numCols
; i
++ )
2406 w
+= m_colWidths
[i
];
2419 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2421 wxRegionIterator
iter( reg
);
2424 m_rowLabelsExposed
.Empty();
2431 // TODO: remove this when we can...
2432 // There is a bug in wxMotif that gives garbage update
2433 // rectangles if you jump-scroll a long way by clicking the
2434 // scrollbar with middle button. This is a work-around
2436 #if defined(__WXMOTIF__)
2438 m_gridWin
->GetClientSize( &cw
, &ch
);
2439 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2440 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2443 // logical bounds of update region
2446 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2447 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2449 // find the row labels within these bounds
2453 for ( row
= 0; row
< m_numRows
; row
++ )
2455 if ( m_rowBottoms
[row
] < top
) continue;
2457 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2458 if ( rowTop
> bottom
) break;
2460 m_rowLabelsExposed
.Add( row
);
2468 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2470 wxRegionIterator
iter( reg
);
2473 m_colLabelsExposed
.Empty();
2480 // TODO: remove this when we can...
2481 // There is a bug in wxMotif that gives garbage update
2482 // rectangles if you jump-scroll a long way by clicking the
2483 // scrollbar with middle button. This is a work-around
2485 #if defined(__WXMOTIF__)
2487 m_gridWin
->GetClientSize( &cw
, &ch
);
2488 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2489 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2492 // logical bounds of update region
2495 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2496 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2498 // find the cells within these bounds
2502 for ( col
= 0; col
< m_numCols
; col
++ )
2504 if ( m_colRights
[col
] < left
) continue;
2506 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2507 if ( colLeft
> right
) break;
2509 m_colLabelsExposed
.Add( col
);
2517 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2519 wxRegionIterator
iter( reg
);
2522 m_cellsExposed
.Empty();
2523 m_rowsExposed
.Empty();
2524 m_colsExposed
.Empty();
2526 int left
, top
, right
, bottom
;
2531 // TODO: remove this when we can...
2532 // There is a bug in wxMotif that gives garbage update
2533 // rectangles if you jump-scroll a long way by clicking the
2534 // scrollbar with middle button. This is a work-around
2536 #if defined(__WXMOTIF__)
2538 m_gridWin
->GetClientSize( &cw
, &ch
);
2539 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2540 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2541 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2542 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2545 // logical bounds of update region
2547 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2548 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2550 // find the cells within these bounds
2553 int colLeft
, rowTop
;
2554 for ( row
= 0; row
< m_numRows
; row
++ )
2556 if ( m_rowBottoms
[row
] <= top
) continue;
2558 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2559 if ( rowTop
> bottom
) break;
2561 m_rowsExposed
.Add( row
);
2563 for ( col
= 0; col
< m_numCols
; col
++ )
2565 if ( m_colRights
[col
] <= left
) continue;
2567 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2568 if ( colLeft
> right
) break;
2570 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2571 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2580 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2583 wxPoint
pos( event
.GetPosition() );
2584 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2586 if ( event
.Dragging() )
2588 m_isDragging
= TRUE
;
2590 if ( event
.LeftIsDown() )
2592 switch( m_cursorMode
)
2594 case WXGRID_CURSOR_RESIZE_ROW
:
2596 int cw
, ch
, left
, dummy
;
2597 m_gridWin
->GetClientSize( &cw
, &ch
);
2598 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2600 wxClientDC
dc( m_gridWin
);
2603 m_rowBottoms
[m_dragRowOrCol
] -
2604 m_rowHeights
[m_dragRowOrCol
] +
2605 WXGRID_MIN_ROW_HEIGHT
);
2606 dc
.SetLogicalFunction(wxINVERT
);
2607 if ( m_dragLastPos
>= 0 )
2609 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2611 dc
.DrawLine( left
, y
, left
+cw
, y
);
2616 case WXGRID_CURSOR_SELECT_ROW
:
2617 if ( (row
= YToRow( y
)) >= 0 &&
2618 !IsInSelection( row
, 0 ) )
2620 SelectRow( row
, TRUE
);
2623 // default label to suppress warnings about "enumeration value
2624 // 'xxx' not handled in switch
2632 m_isDragging
= FALSE
;
2635 // ------------ Entering or leaving the window
2637 if ( event
.Entering() || event
.Leaving() )
2639 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2643 // ------------ Left button pressed
2645 else if ( event
.LeftDown() )
2647 // don't send a label click event for a hit on the
2648 // edge of the row label - this is probably the user
2649 // wanting to resize the row
2651 if ( YToEdgeOfRow(y
) < 0 )
2655 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2657 SelectRow( row
, event
.ShiftDown() );
2658 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2663 // starting to drag-resize a row
2665 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2670 // ------------ Left double click
2672 else if (event
.LeftDClick() )
2674 if ( YToEdgeOfRow(y
) < 0 )
2677 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2682 // ------------ Left button released
2684 else if ( event
.LeftUp() )
2686 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2688 DoEndDragResizeRow();
2690 // Note: we are ending the event *after* doing
2691 // default processing in this case
2693 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2696 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2701 // ------------ Right button down
2703 else if ( event
.RightDown() )
2706 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2708 // no default action at the moment
2713 // ------------ Right double click
2715 else if ( event
.RightDClick() )
2718 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2720 // no default action at the moment
2725 // ------------ No buttons down and mouse moving
2727 else if ( event
.Moving() )
2729 m_dragRowOrCol
= YToEdgeOfRow( y
);
2730 if ( m_dragRowOrCol
>= 0 )
2732 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2734 // don't capture the mouse yet
2735 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2738 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2740 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2746 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2749 wxPoint
pos( event
.GetPosition() );
2750 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2752 if ( event
.Dragging() )
2754 m_isDragging
= TRUE
;
2756 if ( event
.LeftIsDown() )
2758 switch( m_cursorMode
)
2760 case WXGRID_CURSOR_RESIZE_COL
:
2762 int cw
, ch
, dummy
, top
;
2763 m_gridWin
->GetClientSize( &cw
, &ch
);
2764 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2766 wxClientDC
dc( m_gridWin
);
2769 m_colRights
[m_dragRowOrCol
] -
2770 m_colWidths
[m_dragRowOrCol
] +
2771 WXGRID_MIN_COL_WIDTH
);
2772 dc
.SetLogicalFunction(wxINVERT
);
2773 if ( m_dragLastPos
>= 0 )
2775 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2777 dc
.DrawLine( x
, top
, x
, top
+ch
);
2782 case WXGRID_CURSOR_SELECT_COL
:
2783 if ( (col
= XToCol( x
)) >= 0 &&
2784 !IsInSelection( 0, col
) )
2786 SelectCol( col
, TRUE
);
2789 // default label to suppress warnings about "enumeration value
2790 // 'xxx' not handled in switch
2798 m_isDragging
= FALSE
;
2801 // ------------ Entering or leaving the window
2803 if ( event
.Entering() || event
.Leaving() )
2805 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2809 // ------------ Left button pressed
2811 else if ( event
.LeftDown() )
2813 // don't send a label click event for a hit on the
2814 // edge of the col label - this is probably the user
2815 // wanting to resize the col
2817 if ( XToEdgeOfCol(x
) < 0 )
2821 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2823 SelectCol( col
, event
.ShiftDown() );
2824 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2829 // starting to drag-resize a col
2831 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2836 // ------------ Left double click
2838 if ( event
.LeftDClick() )
2840 if ( XToEdgeOfCol(x
) < 0 )
2843 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2848 // ------------ Left button released
2850 else if ( event
.LeftUp() )
2852 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2854 DoEndDragResizeCol();
2856 // Note: we are ending the event *after* doing
2857 // default processing in this case
2859 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2862 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2867 // ------------ Right button down
2869 else if ( event
.RightDown() )
2872 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2874 // no default action at the moment
2879 // ------------ Right double click
2881 else if ( event
.RightDClick() )
2884 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2886 // no default action at the moment
2891 // ------------ No buttons down and mouse moving
2893 else if ( event
.Moving() )
2895 m_dragRowOrCol
= XToEdgeOfCol( x
);
2896 if ( m_dragRowOrCol
>= 0 )
2898 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2900 // don't capture the cursor yet
2901 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2904 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2906 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2912 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2914 if ( event
.LeftDown() )
2916 // indicate corner label by having both row and
2919 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2925 else if ( event
.LeftDClick() )
2927 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2930 else if ( event
.RightDown() )
2932 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2934 // no default action at the moment
2938 else if ( event
.RightDClick() )
2940 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2942 // no default action at the moment
2947 void wxGrid::ChangeCursorMode(CursorMode mode
,
2952 static const wxChar
*cursorModes
[] =
2961 wxLogTrace(_T("grid"),
2962 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2963 win
== m_colLabelWin
? _T("colLabelWin")
2964 : win
? _T("rowLabelWin")
2966 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2967 #endif // __WXDEBUG__
2969 if ( mode
== m_cursorMode
)
2974 // by default use the grid itself
2980 m_winCapture
->ReleaseMouse();
2981 m_winCapture
= (wxWindow
*)NULL
;
2984 m_cursorMode
= mode
;
2986 switch ( m_cursorMode
)
2988 case WXGRID_CURSOR_RESIZE_ROW
:
2989 win
->SetCursor( m_rowResizeCursor
);
2992 case WXGRID_CURSOR_RESIZE_COL
:
2993 win
->SetCursor( m_colResizeCursor
);
2997 win
->SetCursor( *wxSTANDARD_CURSOR
);
3000 // we need to capture mouse when resizing
3001 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3002 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3004 if ( captureMouse
&& resize
)
3006 win
->CaptureMouse();
3011 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3014 wxPoint
pos( event
.GetPosition() );
3015 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3017 wxGridCellCoords coords
;
3018 XYToCell( x
, y
, coords
);
3020 if ( event
.Dragging() )
3022 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3024 // Don't start doing anything until the mouse has been drug at
3025 // least 3 pixels in any direction...
3028 if (m_startDragPos
== wxDefaultPosition
)
3030 m_startDragPos
= pos
;
3033 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3037 m_isDragging
= TRUE
;
3038 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3040 // Hide the edit control, so it
3041 // won't interfer with drag-shrinking.
3042 if ( IsCellEditControlEnabled() )
3043 HideCellEditControl();
3045 // Have we captured the mouse yet?
3048 m_winCapture
= m_gridWin
;
3049 m_winCapture
->CaptureMouse();
3052 if ( coords
!= wxGridNoCellCoords
)
3054 if ( !IsSelection() )
3056 SelectBlock( coords
, coords
);
3060 SelectBlock( m_currentCellCoords
, coords
);
3063 if (! IsVisible(coords
))
3065 MakeCellVisible(coords
);
3066 // TODO: need to introduce a delay or something here. The
3067 // scrolling is way to fast, at least on MSW.
3071 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3073 int cw
, ch
, left
, dummy
;
3074 m_gridWin
->GetClientSize( &cw
, &ch
);
3075 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3077 wxClientDC
dc( m_gridWin
);
3080 m_rowBottoms
[m_dragRowOrCol
] -
3081 m_rowHeights
[m_dragRowOrCol
] +
3082 WXGRID_MIN_ROW_HEIGHT
);
3083 dc
.SetLogicalFunction(wxINVERT
);
3084 if ( m_dragLastPos
>= 0 )
3086 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3088 dc
.DrawLine( left
, y
, left
+cw
, y
);
3091 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3093 int cw
, ch
, dummy
, top
;
3094 m_gridWin
->GetClientSize( &cw
, &ch
);
3095 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3097 wxClientDC
dc( m_gridWin
);
3100 m_colRights
[m_dragRowOrCol
] -
3101 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
3102 dc
.SetLogicalFunction(wxINVERT
);
3103 if ( m_dragLastPos
>= 0 )
3105 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3107 dc
.DrawLine( x
, top
, x
, top
+ch
);
3114 m_isDragging
= FALSE
;
3115 m_startDragPos
= wxDefaultPosition
;
3118 if ( coords
!= wxGridNoCellCoords
)
3120 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3121 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3124 if ( event
.Entering() || event
.Leaving() )
3126 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3127 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3132 // ------------ Left button pressed
3134 if ( event
.LeftDown() )
3136 DisableCellEditControl();
3137 if ( event
.ShiftDown() )
3139 SelectBlock( m_currentCellCoords
, coords
);
3141 else if ( XToEdgeOfCol(x
) < 0 &&
3142 YToEdgeOfRow(y
) < 0 )
3144 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3149 MakeCellVisible( coords
);
3151 // if this is the second click on this cell then start
3153 if ( m_waitForSlowClick
&&
3154 (coords
== m_currentCellCoords
) &&
3155 CanEnableCellControl())
3157 EnableCellEditControl();
3158 m_waitForSlowClick
= FALSE
;
3162 SetCurrentCell( coords
);
3163 m_waitForSlowClick
= TRUE
;
3170 // ------------ Left double click
3172 else if ( event
.LeftDClick() )
3174 DisableCellEditControl();
3175 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3177 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3185 // ------------ Left button released
3187 else if ( event
.LeftUp() )
3189 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3191 if ( IsSelection() )
3195 m_winCapture
->ReleaseMouse();
3196 m_winCapture
= NULL
;
3198 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3201 // Show the edit control, if it has been hidden for
3203 ShowCellEditControl();
3205 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3207 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3208 DoEndDragResizeRow();
3210 // Note: we are ending the event *after* doing
3211 // default processing in this case
3213 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3215 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3217 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3218 DoEndDragResizeCol();
3220 // Note: we are ending the event *after* doing
3221 // default processing in this case
3223 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3230 // ------------ Right button down
3232 else if ( event
.RightDown() )
3234 DisableCellEditControl();
3235 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3240 // no default action at the moment
3245 // ------------ Right double click
3247 else if ( event
.RightDClick() )
3249 DisableCellEditControl();
3250 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3255 // no default action at the moment
3259 // ------------ Moving and no button action
3261 else if ( event
.Moving() && !event
.IsButton() )
3263 int dragRow
= YToEdgeOfRow( y
);
3264 int dragCol
= XToEdgeOfCol( x
);
3266 // Dragging on the corner of a cell to resize in both
3267 // directions is not implemented yet...
3269 if ( dragRow
>= 0 && dragCol
>= 0 )
3271 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3277 m_dragRowOrCol
= dragRow
;
3279 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3281 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3289 m_dragRowOrCol
= dragCol
;
3291 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3293 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3299 // Neither on a row or col edge
3301 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3303 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3310 void wxGrid::DoEndDragResizeRow()
3312 if ( m_dragLastPos
>= 0 )
3314 // erase the last line and resize the row
3316 int cw
, ch
, left
, dummy
;
3317 m_gridWin
->GetClientSize( &cw
, &ch
);
3318 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3320 wxClientDC
dc( m_gridWin
);
3322 dc
.SetLogicalFunction( wxINVERT
);
3323 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3324 HideCellEditControl();
3326 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3327 SetRowSize( m_dragRowOrCol
,
3328 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3330 if ( !GetBatchCount() )
3332 // Only needed to get the correct rect.y:
3333 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3335 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3336 rect
.width
= m_rowLabelWidth
;
3337 rect
.height
= ch
- rect
.y
;
3338 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3340 m_gridWin
->Refresh( FALSE
, &rect
);
3343 ShowCellEditControl();
3348 void wxGrid::DoEndDragResizeCol()
3350 if ( m_dragLastPos
>= 0 )
3352 // erase the last line and resize the col
3354 int cw
, ch
, dummy
, top
;
3355 m_gridWin
->GetClientSize( &cw
, &ch
);
3356 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3358 wxClientDC
dc( m_gridWin
);
3360 dc
.SetLogicalFunction( wxINVERT
);
3361 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3362 HideCellEditControl();
3364 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3365 SetColSize( m_dragRowOrCol
,
3366 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3368 if ( !GetBatchCount() )
3370 // Only needed to get the correct rect.x:
3371 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3373 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3374 rect
.width
= cw
- rect
.x
;
3375 rect
.height
= m_colLabelHeight
;
3376 m_colLabelWin
->Refresh( TRUE
, &rect
);
3378 m_gridWin
->Refresh( FALSE
, &rect
);
3381 ShowCellEditControl();
3388 // ------ interaction with data model
3390 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3392 switch ( msg
.GetId() )
3394 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3395 return GetModelValues();
3397 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3398 return SetModelValues();
3400 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3401 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3402 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3403 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3404 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3405 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3406 return Redimension( msg
);
3415 // The behaviour of this function depends on the grid table class
3416 // Clear() function. For the default wxGridStringTable class the
3417 // behavious is to replace all cell contents with wxEmptyString but
3418 // not to change the number of rows or cols.
3420 void wxGrid::ClearGrid()
3425 SetEditControlValue();
3426 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3431 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3433 // TODO: something with updateLabels flag
3437 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3443 if (IsCellEditControlEnabled())
3444 DisableCellEditControl();
3446 bool ok
= m_table
->InsertRows( pos
, numRows
);
3448 // the table will have sent the results of the insert row
3449 // operation to this view object as a grid table message
3453 if ( m_numCols
== 0 )
3455 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3457 // TODO: perhaps instead of appending the default number of cols
3458 // we should remember what the last non-zero number of cols was ?
3462 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3464 // if we have just inserted cols into an empty grid the current
3465 // cell will be undefined...
3467 SetCurrentCell( 0, 0 );
3471 if ( !GetBatchCount() ) Refresh();
3474 SetEditControlValue();
3484 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3486 // TODO: something with updateLabels flag
3490 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3494 if ( m_table
&& m_table
->AppendRows( numRows
) )
3496 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3498 // if we have just inserted cols into an empty grid the current
3499 // cell will be undefined...
3501 SetCurrentCell( 0, 0 );
3504 // the table will have sent the results of the append row
3505 // operation to this view object as a grid table message
3508 if ( !GetBatchCount() ) Refresh();
3518 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3520 // TODO: something with updateLabels flag
3524 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3530 if (IsCellEditControlEnabled())
3531 DisableCellEditControl();
3533 if (m_table
->DeleteRows( pos
, numRows
))
3536 // the table will have sent the results of the delete row
3537 // operation to this view object as a grid table message
3540 if ( !GetBatchCount() ) Refresh();
3548 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3550 // TODO: something with updateLabels flag
3554 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3560 if (IsCellEditControlEnabled())
3561 DisableCellEditControl();
3563 bool ok
= m_table
->InsertCols( pos
, numCols
);
3565 // the table will have sent the results of the insert col
3566 // operation to this view object as a grid table message
3570 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3572 // if we have just inserted cols into an empty grid the current
3573 // cell will be undefined...
3575 SetCurrentCell( 0, 0 );
3579 if ( !GetBatchCount() ) Refresh();
3582 SetEditControlValue();
3592 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3594 // TODO: something with updateLabels flag
3598 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3602 if ( m_table
&& m_table
->AppendCols( numCols
) )
3604 // the table will have sent the results of the append col
3605 // operation to this view object as a grid table message
3607 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3609 // if we have just inserted cols into an empty grid the current
3610 // cell will be undefined...
3612 SetCurrentCell( 0, 0 );
3616 if ( !GetBatchCount() ) Refresh();
3626 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3628 // TODO: something with updateLabels flag
3632 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3638 if (IsCellEditControlEnabled())
3639 DisableCellEditControl();
3641 if ( m_table
->DeleteCols( pos
, numCols
) )
3643 // the table will have sent the results of the delete col
3644 // operation to this view object as a grid table message
3647 if ( !GetBatchCount() ) Refresh();
3657 // ----- event handlers
3660 // Generate a grid event based on a mouse event and
3661 // return the result of ProcessEvent()
3663 bool wxGrid::SendEvent( const wxEventType type
,
3665 wxMouseEvent
& mouseEv
)
3667 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3669 int rowOrCol
= (row
== -1 ? col
: row
);
3671 wxGridSizeEvent
gridEvt( GetId(),
3675 mouseEv
.GetX(), mouseEv
.GetY(),
3676 mouseEv
.ControlDown(),
3677 mouseEv
.ShiftDown(),
3679 mouseEv
.MetaDown() );
3681 return GetEventHandler()->ProcessEvent(gridEvt
);
3683 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3685 wxGridRangeSelectEvent
gridEvt( GetId(),
3689 m_selectedBottomRight
,
3690 mouseEv
.ControlDown(),
3691 mouseEv
.ShiftDown(),
3693 mouseEv
.MetaDown() );
3695 return GetEventHandler()->ProcessEvent(gridEvt
);
3699 wxGridEvent
gridEvt( GetId(),
3703 mouseEv
.GetX(), mouseEv
.GetY(),
3704 mouseEv
.ControlDown(),
3705 mouseEv
.ShiftDown(),
3707 mouseEv
.MetaDown() );
3709 return GetEventHandler()->ProcessEvent(gridEvt
);
3714 // Generate a grid event of specified type and return the result
3715 // of ProcessEvent().
3717 bool wxGrid::SendEvent( const wxEventType type
,
3720 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3722 int rowOrCol
= (row
== -1 ? col
: row
);
3724 wxGridSizeEvent
gridEvt( GetId(),
3729 return GetEventHandler()->ProcessEvent(gridEvt
);
3733 wxGridEvent
gridEvt( GetId(),
3738 return GetEventHandler()->ProcessEvent(gridEvt
);
3743 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3745 wxPaintDC
dc( this );
3747 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3748 m_numRows
&& m_numCols
)
3750 m_currentCellCoords
.Set(0, 0);
3751 SetEditControlValue();
3752 ShowCellEditControl();
3759 // This is just here to make sure that CalcDimensions gets called when
3760 // the grid view is resized... then the size event is skipped to allow
3761 // the box sizers to handle everything
3763 void wxGrid::OnSize( wxSizeEvent
& event
)
3770 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3772 if ( m_inOnKeyDown
)
3774 // shouldn't be here - we are going round in circles...
3776 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3779 m_inOnKeyDown
= TRUE
;
3781 // propagate the event up and see if it gets processed
3783 wxWindow
*parent
= GetParent();
3784 wxKeyEvent
keyEvt( event
);
3785 keyEvt
.SetEventObject( parent
);
3787 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3790 // TODO: Should also support Shift-cursor keys for
3791 // extending the selection. Maybe add a flag to
3792 // MoveCursorXXX() and MoveCursorXXXBlock() and
3793 // just send event.ShiftDown().
3795 // try local handlers
3797 switch ( event
.KeyCode() )
3800 if ( event
.ControlDown() )
3802 MoveCursorUpBlock();
3811 if ( event
.ControlDown() )
3813 MoveCursorDownBlock();
3822 if ( event
.ControlDown() )
3824 MoveCursorLeftBlock();
3833 if ( event
.ControlDown() )
3835 MoveCursorRightBlock();
3844 if ( event
.ControlDown() )
3846 event
.Skip(); // to let the edit control have the return
3855 if (event
.ShiftDown())
3862 if ( event
.ControlDown() )
3864 MakeCellVisible( 0, 0 );
3865 SetCurrentCell( 0, 0 );
3874 if ( event
.ControlDown() )
3876 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3877 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3893 // We don't want these keys to trigger the edit control, any others?
3902 if ( !IsEditable() )
3907 // Otherwise fall through to default
3910 // now try the cell edit control
3912 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
3914 EnableCellEditControl();
3915 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3916 attr
->GetEditor()->StartingKey(event
);
3923 m_inOnKeyDown
= FALSE
;
3927 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3931 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3933 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3935 // the event has been intercepted - do nothing
3940 m_currentCellCoords
!= wxGridNoCellCoords
)
3942 HideCellEditControl();
3943 SaveEditControlValue();
3944 DisableCellEditControl();
3946 // Clear the old current cell highlight
3947 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3949 // Otherwise refresh redraws the highlight!
3950 m_currentCellCoords
= coords
;
3952 m_gridWin
->Refresh( FALSE
, &r
);
3955 m_currentCellCoords
= coords
;
3957 SetEditControlValue();
3961 wxClientDC
dc(m_gridWin
);
3964 wxGridCellAttr
* attr
= GetCellAttr(coords
);
3965 DrawCellHighlight(dc
, attr
);
3968 if ( IsSelection() )
3970 wxRect
r( SelectionToDeviceRect() );
3972 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3979 // ------ functions to get/send data (see also public functions)
3982 bool wxGrid::GetModelValues()
3986 // all we need to do is repaint the grid
3988 m_gridWin
->Refresh();
3996 bool wxGrid::SetModelValues()
4002 for ( row
= 0; row
< m_numRows
; row
++ )
4004 for ( col
= 0; col
< m_numCols
; col
++ )
4006 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4018 // Note - this function only draws cells that are in the list of
4019 // exposed cells (usually set from the update region by
4020 // CalcExposedCells)
4022 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4024 if ( !m_numRows
|| !m_numCols
) return;
4027 size_t numCells
= m_cellsExposed
.GetCount();
4029 for ( i
= 0; i
< numCells
; i
++ )
4031 DrawCell( dc
, m_cellsExposed
[i
] );
4036 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4038 int row
= coords
.GetRow();
4039 int col
= coords
.GetCol();
4041 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4044 // we draw the cell border ourselves
4045 #if !WXGRID_DRAW_LINES
4046 if ( m_gridLinesEnabled
)
4047 DrawCellBorder( dc
, coords
);
4050 // don't draw the cell over the active edit control!
4051 if ( (coords
== m_currentCellCoords
) && IsCellEditControlEnabled() )
4054 // but all the rest is drawn by the cell renderer and hence may be
4057 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4058 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4059 rect
.width
= m_colWidths
[col
]-1;
4060 rect
.height
= m_rowHeights
[row
]-1;
4062 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4063 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4065 if (m_currentCellCoords
== coords
)
4066 DrawCellHighlight(dc
, attr
);
4071 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4073 int row
= m_currentCellCoords
.GetRow();
4074 int col
= m_currentCellCoords
.GetCol();
4076 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4080 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4081 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4082 rect
.width
= m_colWidths
[col
] - 1;
4083 rect
.height
= m_rowHeights
[row
] - 1;
4085 if ( attr
->IsReadOnly() )
4087 // hmmm... what could we do here to show that the cell is disabled?
4088 // for now, I just draw a thinner border than for the other ones, but
4089 // it doesn't look really good
4090 dc
.SetPen(wxPen(m_gridLineColour
, 2, wxSOLID
));
4091 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4093 dc
.DrawRectangle(rect
);
4097 // VZ: my experiments with 3d borders...
4099 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
4100 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4102 dc
.DrawRectangle(rect
);
4104 // FIXME we should properly set colours for arbitrary bg
4105 wxCoord x1
= rect
.x
,
4107 x2
= rect
.x
+ rect
.width
,
4108 y2
= rect
.y
+ rect
.height
;
4110 dc
.SetPen(*wxWHITE_PEN
);
4111 dc
.DrawLine(x1
, y1
, x2
- 1, y1
);
4112 dc
.DrawLine(x1
, y1
, x1
, y2
- 1);
4114 dc
.SetPen(*wxLIGHT_GREY_PEN
);
4115 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4116 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
- 1);
4118 dc
.SetPen(*wxBLACK_PEN
);
4119 dc
.DrawLine(x1
, y2
, x2
, y2
);
4120 dc
.DrawLine(x2
, y1
, x2
, y2
);
4125 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4127 if ( m_colWidths
[coords
.GetCol()] <=0 ||
4128 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
4130 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4131 int row
= coords
.GetRow();
4132 int col
= coords
.GetCol();
4134 // right hand border
4136 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
4137 m_colRights
[col
], m_rowBottoms
[row
] );
4141 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
4142 m_colRights
[col
], m_rowBottoms
[row
] );
4146 // TODO: remove this ???
4147 // This is used to redraw all grid lines e.g. when the grid line colour
4150 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4152 if ( !m_gridLinesEnabled
||
4154 !m_numCols
) return;
4156 int top
, bottom
, left
, right
;
4161 m_gridWin
->GetClientSize(&cw
, &ch
);
4163 // virtual coords of visible area
4165 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4166 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4171 reg
.GetBox(x
, y
, w
, h
);
4172 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4173 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4176 // avoid drawing grid lines past the last row and col
4178 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
4179 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
4181 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4183 // horizontal grid lines
4186 for ( i
= 0; i
< m_numRows
; i
++ )
4188 if ( m_rowBottoms
[i
]-1 > bottom
)
4192 else if ( m_rowBottoms
[i
]-1 >= top
)
4194 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
4199 // vertical grid lines
4201 for ( i
= 0; i
< m_numCols
; i
++ )
4203 if ( m_colRights
[i
]-1 > right
)
4207 else if ( m_colRights
[i
]-1 >= left
)
4209 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
4215 void wxGrid::DrawRowLabels( wxDC
& dc
)
4217 if ( !m_numRows
|| !m_numCols
) return;
4220 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4222 for ( i
= 0; i
< numLabels
; i
++ )
4224 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4229 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4231 if ( m_rowHeights
[row
] <= 0 ) return;
4233 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4235 dc
.SetPen( *wxBLACK_PEN
);
4236 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4237 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4239 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4240 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4242 dc
.SetPen( *wxWHITE_PEN
);
4243 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4244 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4246 dc
.SetBackgroundMode( wxTRANSPARENT
);
4247 dc
.SetTextForeground( GetLabelTextColour() );
4248 dc
.SetFont( GetLabelFont() );
4251 GetRowLabelAlignment( &hAlign
, &vAlign
);
4255 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4256 rect
.SetWidth( m_rowLabelWidth
- 4 );
4257 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4258 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4262 void wxGrid::DrawColLabels( wxDC
& dc
)
4264 if ( !m_numRows
|| !m_numCols
) return;
4267 size_t numLabels
= m_colLabelsExposed
.GetCount();
4269 for ( i
= 0; i
< numLabels
; i
++ )
4271 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4276 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4278 if ( m_colWidths
[col
] <= 0 ) return;
4280 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4282 dc
.SetPen( *wxBLACK_PEN
);
4283 dc
.DrawLine( m_colRights
[col
]-1, 0,
4284 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4286 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4287 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4289 dc
.SetPen( *wxWHITE_PEN
);
4290 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4291 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4293 dc
.SetBackgroundMode( wxTRANSPARENT
);
4294 dc
.SetTextForeground( GetLabelTextColour() );
4295 dc
.SetFont( GetLabelFont() );
4297 dc
.SetBackgroundMode( wxTRANSPARENT
);
4298 dc
.SetTextForeground( GetLabelTextColour() );
4299 dc
.SetFont( GetLabelFont() );
4302 GetColLabelAlignment( &hAlign
, &vAlign
);
4305 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4307 rect
.SetWidth( m_colWidths
[col
] - 4 );
4308 rect
.SetHeight( m_colLabelHeight
- 4 );
4309 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4313 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4314 const wxString
& value
,
4319 long textWidth
, textHeight
;
4320 long lineWidth
, lineHeight
;
4321 wxArrayString lines
;
4323 dc
.SetClippingRegion( rect
);
4324 StringToLines( value
, lines
);
4325 if ( lines
.GetCount() )
4327 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4328 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4331 switch ( horizAlign
)
4334 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4338 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4347 switch ( vertAlign
)
4350 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4354 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4363 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4365 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4370 dc
.DestroyClippingRegion();
4374 // Split multi line text up into an array of strings. Any existing
4375 // contents of the string array are preserved.
4377 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4381 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4382 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4384 while ( startPos
< (int)tVal
.Length() )
4386 pos
= tVal
.Mid(startPos
).Find( eol
);
4391 else if ( pos
== 0 )
4393 lines
.Add( wxEmptyString
);
4397 lines
.Add( value
.Mid(startPos
, pos
) );
4401 if ( startPos
< (int)value
.Length() )
4403 lines
.Add( value
.Mid( startPos
) );
4408 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4409 wxArrayString
& lines
,
4410 long *width
, long *height
)
4417 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4419 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4420 w
= wxMax( w
, lineW
);
4430 // ------ Edit control functions
4434 void wxGrid::EnableEditing( bool edit
)
4436 // TODO: improve this ?
4438 if ( edit
!= m_editable
)
4442 // FIXME IMHO this won't disable the edit control if edit == FALSE
4443 // because of the check in the beginning of
4444 // EnableCellEditControl() just below (VZ)
4445 EnableCellEditControl(m_editable
);
4450 void wxGrid::EnableCellEditControl( bool enable
)
4455 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4456 SetCurrentCell( 0, 0 );
4458 if ( enable
!= m_cellEditCtrlEnabled
)
4460 // TODO allow the app to Veto() this event?
4461 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4465 // this should be checked by the caller!
4466 wxASSERT_MSG( CanEnableCellControl(),
4467 _T("can't enable editing for this cell!") );
4469 // do it before ShowCellEditControl()
4470 m_cellEditCtrlEnabled
= enable
;
4472 SetEditControlValue();
4473 ShowCellEditControl();
4477 HideCellEditControl();
4478 SaveEditControlValue();
4480 // do it after HideCellEditControl()
4481 m_cellEditCtrlEnabled
= enable
;
4486 bool wxGrid::IsCurrentCellReadOnly() const
4489 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4490 bool readonly
= attr
->IsReadOnly();
4496 bool wxGrid::CanEnableCellControl() const
4498 return m_editable
&& !IsCurrentCellReadOnly();
4501 bool wxGrid::IsCellEditControlEnabled() const
4503 // the cell edit control might be disable for all cells or just for the
4504 // current one if it's read only
4505 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
4508 wxWindow
*wxGrid::GetGridWindow() const
4513 void wxGrid::ShowCellEditControl()
4515 if ( IsCellEditControlEnabled() )
4517 if ( !IsVisible( m_currentCellCoords
) )
4523 wxRect rect
= CellToRect( m_currentCellCoords
);
4524 int row
= m_currentCellCoords
.GetRow();
4525 int col
= m_currentCellCoords
.GetCol();
4527 // convert to scrolled coords
4529 int left
, top
, right
, bottom
;
4530 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4531 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4533 // cell is shifted by one pixel
4539 // Make the edit control large enough to allow for internal
4542 // TODO: remove this if the text ctrl sizing is improved esp. for
4546 #if defined(__WXMOTIF__)
4547 if ( row
== 0 || col
== 0 )
4556 if ( row
== 0 || col
== 0 )
4566 #if defined(__WXGTK__)
4569 if (left
!= 0) left_diff
++;
4570 if (top
!= 0) top_diff
++;
4571 rect
.SetLeft( left
+ left_diff
);
4572 rect
.SetTop( top
+ top_diff
);
4573 rect
.SetRight( rect
.GetRight() - left_diff
);
4574 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4576 rect
.SetLeft( wxMax(0, left
- extra
) );
4577 rect
.SetTop( wxMax(0, top
- extra
) );
4578 rect
.SetRight( rect
.GetRight() + 2*extra
);
4579 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4582 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4583 wxGridCellEditor
* editor
= attr
->GetEditor();
4584 if ( !editor
->IsCreated() )
4586 editor
->Create(m_gridWin
, -1,
4587 new wxGridCellEditorEvtHandler(this, editor
));
4590 editor
->SetSize( rect
);
4591 editor
->Show( TRUE
, attr
);
4592 editor
->BeginEdit(row
, col
, this);
4599 void wxGrid::HideCellEditControl()
4601 if ( IsCellEditControlEnabled() )
4603 int row
= m_currentCellCoords
.GetRow();
4604 int col
= m_currentCellCoords
.GetCol();
4606 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4607 attr
->GetEditor()->Show( FALSE
);
4609 m_gridWin
->SetFocus();
4614 void wxGrid::SetEditControlValue( const wxString
& value
)
4616 // RD: The new Editors get the value from the table themselves now. This
4617 // method can probably be removed...
4621 void wxGrid::SaveEditControlValue()
4623 if ( IsCellEditControlEnabled() )
4625 int row
= m_currentCellCoords
.GetRow();
4626 int col
= m_currentCellCoords
.GetCol();
4628 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4629 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4635 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4636 m_currentCellCoords
.GetRow(),
4637 m_currentCellCoords
.GetCol() );
4644 // ------ Grid location functions
4645 // Note that all of these functions work with the logical coordinates of
4646 // grid cells and labels so you will need to convert from device
4647 // coordinates for mouse events etc.
4650 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4652 int row
= YToRow(y
);
4653 int col
= XToCol(x
);
4655 if ( row
== -1 || col
== -1 )
4657 coords
= wxGridNoCellCoords
;
4661 coords
.Set( row
, col
);
4666 int wxGrid::YToRow( int y
)
4670 for ( i
= 0; i
< m_numRows
; i
++ )
4672 if ( y
< m_rowBottoms
[i
] ) return i
;
4675 return m_numRows
; //-1;
4679 int wxGrid::XToCol( int x
)
4683 for ( i
= 0; i
< m_numCols
; i
++ )
4685 if ( x
< m_colRights
[i
] ) return i
;
4688 return m_numCols
; //-1;
4692 // return the row number that that the y coord is near the edge of, or
4693 // -1 if not near an edge
4695 int wxGrid::YToEdgeOfRow( int y
)
4699 for ( i
= 0; i
< m_numRows
; i
++ )
4701 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4703 d
= abs( y
- m_rowBottoms
[i
] );
4705 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4714 // return the col number that that the x coord is near the edge of, or
4715 // -1 if not near an edge
4717 int wxGrid::XToEdgeOfCol( int x
)
4721 for ( i
= 0; i
< m_numCols
; i
++ )
4723 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4725 d
= abs( x
- m_colRights
[i
] );
4727 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4736 wxRect
wxGrid::CellToRect( int row
, int col
)
4738 wxRect
rect( -1, -1, -1, -1 );
4740 if ( row
>= 0 && row
< m_numRows
&&
4741 col
>= 0 && col
< m_numCols
)
4743 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4744 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4745 rect
.width
= m_colWidths
[col
];
4746 rect
.height
= m_rowHeights
[ row
];
4753 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4755 // get the cell rectangle in logical coords
4757 wxRect
r( CellToRect( row
, col
) );
4759 // convert to device coords
4761 int left
, top
, right
, bottom
;
4762 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4763 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4765 // check against the client area of the grid window
4768 m_gridWin
->GetClientSize( &cw
, &ch
);
4770 if ( wholeCellVisible
)
4772 // is the cell wholly visible ?
4774 return ( left
>= 0 && right
<= cw
&&
4775 top
>= 0 && bottom
<= ch
);
4779 // is the cell partly visible ?
4781 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4782 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4787 // make the specified cell location visible by doing a minimal amount
4790 void wxGrid::MakeCellVisible( int row
, int col
)
4793 int xpos
= -1, ypos
= -1;
4795 if ( row
>= 0 && row
< m_numRows
&&
4796 col
>= 0 && col
< m_numCols
)
4798 // get the cell rectangle in logical coords
4800 wxRect
r( CellToRect( row
, col
) );
4802 // convert to device coords
4804 int left
, top
, right
, bottom
;
4805 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4806 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4809 m_gridWin
->GetClientSize( &cw
, &ch
);
4815 else if ( bottom
> ch
)
4817 int h
= r
.GetHeight();
4819 for ( i
= row
-1; i
>= 0; i
-- )
4821 if ( h
+ m_rowHeights
[i
] > ch
) break;
4823 h
+= m_rowHeights
[i
];
4824 ypos
-= m_rowHeights
[i
];
4827 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4828 // have rounding errors (this is important, because if we do, we
4829 // might not scroll at all and some cells won't be redrawn)
4830 ypos
+= GRID_SCROLL_LINE
/ 2;
4837 else if ( right
> cw
)
4839 int w
= r
.GetWidth();
4841 for ( i
= col
-1; i
>= 0; i
-- )
4843 if ( w
+ m_colWidths
[i
] > cw
) break;
4845 w
+= m_colWidths
[i
];
4846 xpos
-= m_colWidths
[i
];
4849 // see comment for ypos above
4850 xpos
+= GRID_SCROLL_LINE
/ 2;
4853 if ( xpos
!= -1 || ypos
!= -1 )
4855 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4856 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4857 Scroll( xpos
, ypos
);
4865 // ------ Grid cursor movement functions
4868 bool wxGrid::MoveCursorUp()
4870 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4871 m_currentCellCoords
.GetRow() > 0 )
4873 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4874 m_currentCellCoords
.GetCol() );
4876 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4877 m_currentCellCoords
.GetCol() );
4886 bool wxGrid::MoveCursorDown()
4888 // TODO: allow for scrolling
4890 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4891 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4893 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4894 m_currentCellCoords
.GetCol() );
4896 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4897 m_currentCellCoords
.GetCol() );
4906 bool wxGrid::MoveCursorLeft()
4908 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4909 m_currentCellCoords
.GetCol() > 0 )
4911 MakeCellVisible( m_currentCellCoords
.GetRow(),
4912 m_currentCellCoords
.GetCol() - 1 );
4914 SetCurrentCell( m_currentCellCoords
.GetRow(),
4915 m_currentCellCoords
.GetCol() - 1 );
4924 bool wxGrid::MoveCursorRight()
4926 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4927 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4929 MakeCellVisible( m_currentCellCoords
.GetRow(),
4930 m_currentCellCoords
.GetCol() + 1 );
4932 SetCurrentCell( m_currentCellCoords
.GetRow(),
4933 m_currentCellCoords
.GetCol() + 1 );
4942 bool wxGrid::MovePageUp()
4944 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4946 int row
= m_currentCellCoords
.GetRow();
4950 m_gridWin
->GetClientSize( &cw
, &ch
);
4952 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4953 int newRow
= YToRow( y
- ch
+ 1 );
4958 else if ( newRow
== row
)
4963 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4964 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4972 bool wxGrid::MovePageDown()
4974 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4976 int row
= m_currentCellCoords
.GetRow();
4977 if ( row
< m_numRows
)
4980 m_gridWin
->GetClientSize( &cw
, &ch
);
4982 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4983 int newRow
= YToRow( y
+ ch
);
4986 newRow
= m_numRows
- 1;
4988 else if ( newRow
== row
)
4993 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4994 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5002 bool wxGrid::MoveCursorUpBlock()
5005 m_currentCellCoords
!= wxGridNoCellCoords
&&
5006 m_currentCellCoords
.GetRow() > 0 )
5008 int row
= m_currentCellCoords
.GetRow();
5009 int col
= m_currentCellCoords
.GetCol();
5011 if ( m_table
->IsEmptyCell(row
, col
) )
5013 // starting in an empty cell: find the next block of
5019 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5022 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5024 // starting at the top of a block: find the next block
5030 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5035 // starting within a block: find the top of the block
5040 if ( m_table
->IsEmptyCell(row
, col
) )
5048 MakeCellVisible( row
, col
);
5049 SetCurrentCell( row
, col
);
5057 bool wxGrid::MoveCursorDownBlock()
5060 m_currentCellCoords
!= wxGridNoCellCoords
&&
5061 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5063 int row
= m_currentCellCoords
.GetRow();
5064 int col
= m_currentCellCoords
.GetCol();
5066 if ( m_table
->IsEmptyCell(row
, col
) )
5068 // starting in an empty cell: find the next block of
5071 while ( row
< m_numRows
-1 )
5074 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5077 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5079 // starting at the bottom of a block: find the next block
5082 while ( row
< m_numRows
-1 )
5085 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5090 // starting within a block: find the bottom of the block
5092 while ( row
< m_numRows
-1 )
5095 if ( m_table
->IsEmptyCell(row
, col
) )
5103 MakeCellVisible( row
, col
);
5104 SetCurrentCell( row
, col
);
5112 bool wxGrid::MoveCursorLeftBlock()
5115 m_currentCellCoords
!= wxGridNoCellCoords
&&
5116 m_currentCellCoords
.GetCol() > 0 )
5118 int row
= m_currentCellCoords
.GetRow();
5119 int col
= m_currentCellCoords
.GetCol();
5121 if ( m_table
->IsEmptyCell(row
, col
) )
5123 // starting in an empty cell: find the next block of
5129 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5132 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5134 // starting at the left of a block: find the next block
5140 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5145 // starting within a block: find the left of the block
5150 if ( m_table
->IsEmptyCell(row
, col
) )
5158 MakeCellVisible( row
, col
);
5159 SetCurrentCell( row
, col
);
5167 bool wxGrid::MoveCursorRightBlock()
5170 m_currentCellCoords
!= wxGridNoCellCoords
&&
5171 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5173 int row
= m_currentCellCoords
.GetRow();
5174 int col
= m_currentCellCoords
.GetCol();
5176 if ( m_table
->IsEmptyCell(row
, col
) )
5178 // starting in an empty cell: find the next block of
5181 while ( col
< m_numCols
-1 )
5184 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5187 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5189 // starting at the right of a block: find the next block
5192 while ( col
< m_numCols
-1 )
5195 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5200 // starting within a block: find the right of the block
5202 while ( col
< m_numCols
-1 )
5205 if ( m_table
->IsEmptyCell(row
, col
) )
5213 MakeCellVisible( row
, col
);
5214 SetCurrentCell( row
, col
);
5225 // ------ Label values and formatting
5228 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5230 *horiz
= m_rowLabelHorizAlign
;
5231 *vert
= m_rowLabelVertAlign
;
5234 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5236 *horiz
= m_colLabelHorizAlign
;
5237 *vert
= m_colLabelVertAlign
;
5240 wxString
wxGrid::GetRowLabelValue( int row
)
5244 return m_table
->GetRowLabelValue( row
);
5254 wxString
wxGrid::GetColLabelValue( int col
)
5258 return m_table
->GetColLabelValue( col
);
5269 void wxGrid::SetRowLabelSize( int width
)
5271 width
= wxMax( width
, 0 );
5272 if ( width
!= m_rowLabelWidth
)
5276 m_rowLabelWin
->Show( FALSE
);
5277 m_cornerLabelWin
->Show( FALSE
);
5279 else if ( m_rowLabelWidth
== 0 )
5281 m_rowLabelWin
->Show( TRUE
);
5282 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5285 m_rowLabelWidth
= width
;
5292 void wxGrid::SetColLabelSize( int height
)
5294 height
= wxMax( height
, 0 );
5295 if ( height
!= m_colLabelHeight
)
5299 m_colLabelWin
->Show( FALSE
);
5300 m_cornerLabelWin
->Show( FALSE
);
5302 else if ( m_colLabelHeight
== 0 )
5304 m_colLabelWin
->Show( TRUE
);
5305 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5308 m_colLabelHeight
= height
;
5315 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5317 if ( m_labelBackgroundColour
!= colour
)
5319 m_labelBackgroundColour
= colour
;
5320 m_rowLabelWin
->SetBackgroundColour( colour
);
5321 m_colLabelWin
->SetBackgroundColour( colour
);
5322 m_cornerLabelWin
->SetBackgroundColour( colour
);
5324 if ( !GetBatchCount() )
5326 m_rowLabelWin
->Refresh();
5327 m_colLabelWin
->Refresh();
5328 m_cornerLabelWin
->Refresh();
5333 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5335 if ( m_labelTextColour
!= colour
)
5337 m_labelTextColour
= colour
;
5338 if ( !GetBatchCount() )
5340 m_rowLabelWin
->Refresh();
5341 m_colLabelWin
->Refresh();
5346 void wxGrid::SetLabelFont( const wxFont
& font
)
5349 if ( !GetBatchCount() )
5351 m_rowLabelWin
->Refresh();
5352 m_colLabelWin
->Refresh();
5356 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5358 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5360 m_rowLabelHorizAlign
= horiz
;
5363 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5365 m_rowLabelVertAlign
= vert
;
5368 if ( !GetBatchCount() )
5370 m_rowLabelWin
->Refresh();
5374 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5376 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5378 m_colLabelHorizAlign
= horiz
;
5381 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5383 m_colLabelVertAlign
= vert
;
5386 if ( !GetBatchCount() )
5388 m_colLabelWin
->Refresh();
5392 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5396 m_table
->SetRowLabelValue( row
, s
);
5397 if ( !GetBatchCount() )
5399 wxRect rect
= CellToRect( row
, 0);
5400 if ( rect
.height
> 0 )
5402 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5404 rect
.width
= m_rowLabelWidth
;
5405 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5411 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5415 m_table
->SetColLabelValue( col
, s
);
5416 if ( !GetBatchCount() )
5418 wxRect rect
= CellToRect( 0, col
);
5419 if ( rect
.width
> 0 )
5421 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5423 rect
.height
= m_colLabelHeight
;
5424 m_colLabelWin
->Refresh( TRUE
, &rect
);
5430 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5432 if ( m_gridLineColour
!= colour
)
5434 m_gridLineColour
= colour
;
5436 wxClientDC
dc( m_gridWin
);
5438 DrawAllGridLines( dc
, wxRegion() );
5442 void wxGrid::EnableGridLines( bool enable
)
5444 if ( enable
!= m_gridLinesEnabled
)
5446 m_gridLinesEnabled
= enable
;
5448 if ( !GetBatchCount() )
5452 wxClientDC
dc( m_gridWin
);
5454 DrawAllGridLines( dc
, wxRegion() );
5458 m_gridWin
->Refresh();
5465 int wxGrid::GetDefaultRowSize()
5467 return m_defaultRowHeight
;
5470 int wxGrid::GetRowSize( int row
)
5472 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5474 return m_rowHeights
[row
];
5477 int wxGrid::GetDefaultColSize()
5479 return m_defaultColWidth
;
5482 int wxGrid::GetColSize( int col
)
5484 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5486 return m_colWidths
[col
];
5489 // ============================================================================
5490 // access to the grid attributes: each of them has a default value in the grid
5491 // itself and may be overidden on a per-cell basis
5492 // ============================================================================
5494 // ----------------------------------------------------------------------------
5495 // setting default attributes
5496 // ----------------------------------------------------------------------------
5498 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5500 m_defaultCellAttr
->SetBackgroundColour(col
);
5503 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5505 m_defaultCellAttr
->SetTextColour(col
);
5508 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5510 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5513 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5515 m_defaultCellAttr
->SetFont(font
);
5518 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5520 m_defaultCellAttr
->SetRenderer(renderer
);
5523 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5525 m_defaultCellAttr
->SetEditor(editor
);
5528 // ----------------------------------------------------------------------------
5529 // access to the default attrbiutes
5530 // ----------------------------------------------------------------------------
5532 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5534 return m_defaultCellAttr
->GetBackgroundColour();
5537 wxColour
wxGrid::GetDefaultCellTextColour()
5539 return m_defaultCellAttr
->GetTextColour();
5542 wxFont
wxGrid::GetDefaultCellFont()
5544 return m_defaultCellAttr
->GetFont();
5547 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5549 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5552 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5554 return m_defaultCellAttr
->GetRenderer();
5557 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5559 return m_defaultCellAttr
->GetEditor();
5562 // ----------------------------------------------------------------------------
5563 // access to cell attributes
5564 // ----------------------------------------------------------------------------
5566 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5568 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5569 wxColour colour
= attr
->GetBackgroundColour();
5574 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5576 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5577 wxColour colour
= attr
->GetTextColour();
5582 wxFont
wxGrid::GetCellFont( int row
, int col
)
5584 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5585 wxFont font
= attr
->GetFont();
5590 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5592 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5593 attr
->GetAlignment(horiz
, vert
);
5597 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5599 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5600 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5605 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5607 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5608 wxGridCellEditor
* editor
= attr
->GetEditor();
5613 bool wxGrid::IsReadOnly(int row
, int col
) const
5615 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5616 bool isReadOnly
= attr
->IsReadOnly();
5621 // ----------------------------------------------------------------------------
5622 // attribute support: cache, automatic provider creation, ...
5623 // ----------------------------------------------------------------------------
5625 bool wxGrid::CanHaveAttributes()
5632 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5633 // table is providing the attributes itself??? In which case
5634 // I don't think the grid should create a Provider object for the
5635 // table but the table should be smart enough to do that on its own.
5636 if ( !m_table
->GetAttrProvider() )
5638 // use the default attr provider by default
5639 // (another choice would be to just return FALSE thus forcing the user
5641 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5647 void wxGrid::ClearAttrCache()
5649 if ( m_attrCache
.row
!= -1 )
5651 m_attrCache
.attr
->SafeDecRef();
5652 m_attrCache
.row
= -1;
5656 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5658 wxGrid
*self
= (wxGrid
*)this; // const_cast
5660 self
->ClearAttrCache();
5661 self
->m_attrCache
.row
= row
;
5662 self
->m_attrCache
.col
= col
;
5663 self
->m_attrCache
.attr
= attr
;
5667 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5669 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5671 *attr
= m_attrCache
.attr
;
5672 (*attr
)->SafeIncRef();
5674 #ifdef DEBUG_ATTR_CACHE
5675 gs_nAttrCacheHits
++;
5682 #ifdef DEBUG_ATTR_CACHE
5683 gs_nAttrCacheMisses
++;
5689 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5691 wxGridCellAttr
*attr
;
5692 if ( !LookupAttr(row
, col
, &attr
) )
5694 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5695 CacheAttr(row
, col
, attr
);
5699 attr
->SetDefAttr(m_defaultCellAttr
);
5703 attr
= m_defaultCellAttr
;
5710 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5712 wxGridCellAttr
*attr
;
5713 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5715 wxASSERT_MSG( m_table
,
5716 _T("we may only be called if CanHaveAttributes() "
5717 "returned TRUE and then m_table should be !NULL") );
5719 attr
= m_table
->GetAttr(row
, col
);
5722 attr
= new wxGridCellAttr
;
5724 // artificially inc the ref count to match DecRef() in caller
5727 m_table
->SetAttr(attr
, row
, col
);
5730 CacheAttr(row
, col
, attr
);
5732 attr
->SetDefAttr(m_defaultCellAttr
);
5736 // ----------------------------------------------------------------------------
5737 // setting cell attributes: this is forwarded to the table
5738 // ----------------------------------------------------------------------------
5740 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5742 if ( CanHaveAttributes() )
5744 m_table
->SetRowAttr(attr
, row
);
5752 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5754 if ( CanHaveAttributes() )
5756 m_table
->SetColAttr(attr
, col
);
5764 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5766 if ( CanHaveAttributes() )
5768 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5769 attr
->SetBackgroundColour(colour
);
5774 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5776 if ( CanHaveAttributes() )
5778 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5779 attr
->SetTextColour(colour
);
5784 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5786 if ( CanHaveAttributes() )
5788 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5789 attr
->SetFont(font
);
5794 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5796 if ( CanHaveAttributes() )
5798 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5799 attr
->SetAlignment(horiz
, vert
);
5804 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5806 if ( CanHaveAttributes() )
5808 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5809 attr
->SetRenderer(renderer
);
5814 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5816 if ( CanHaveAttributes() )
5818 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5819 attr
->SetEditor(editor
);
5824 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5826 if ( CanHaveAttributes() )
5828 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5829 attr
->SetReadOnly(isReadOnly
);
5834 // ----------------------------------------------------------------------------
5836 // ----------------------------------------------------------------------------
5838 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5840 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5842 if ( resizeExistingRows
)
5846 for ( row
= 0; row
< m_numRows
; row
++ )
5848 m_rowHeights
[row
] = m_defaultRowHeight
;
5849 bottom
+= m_defaultRowHeight
;
5850 m_rowBottoms
[row
] = bottom
;
5856 void wxGrid::SetRowSize( int row
, int height
)
5858 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5862 int h
= wxMax( 0, height
);
5863 int diff
= h
- m_rowHeights
[row
];
5865 m_rowHeights
[row
] = h
;
5866 for ( i
= row
; i
< m_numRows
; i
++ )
5868 m_rowBottoms
[i
] += diff
;
5873 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5875 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5877 if ( resizeExistingCols
)
5881 for ( col
= 0; col
< m_numCols
; col
++ )
5883 m_colWidths
[col
] = m_defaultColWidth
;
5884 right
+= m_defaultColWidth
;
5885 m_colRights
[col
] = right
;
5891 void wxGrid::SetColSize( int col
, int width
)
5893 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5897 int w
= wxMax( 0, width
);
5898 int diff
= w
- m_colWidths
[col
];
5899 m_colWidths
[col
] = w
;
5901 for ( i
= col
; i
< m_numCols
; i
++ )
5903 m_colRights
[i
] += diff
;
5910 // ------ cell value accessor functions
5913 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5917 m_table
->SetValue( row
, col
, s
.c_str() );
5918 if ( !GetBatchCount() )
5920 wxClientDC
dc( m_gridWin
);
5922 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5925 #if 0 // TODO: edit in place
5927 if ( m_currentCellCoords
.GetRow() == row
&&
5928 m_currentCellCoords
.GetCol() == col
)
5930 SetEditControlValue( s
);
5939 // ------ Block, row and col selection
5942 void wxGrid::SelectRow( int row
, bool addToSelected
)
5946 if ( IsSelection() && addToSelected
)
5949 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5952 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5953 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5954 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5955 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5959 need_refresh
[0] = TRUE
;
5960 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5961 wxGridCellCoords ( oldTop
- 1,
5963 m_selectedTopLeft
.SetRow( row
);
5968 need_refresh
[1] = TRUE
;
5969 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5970 wxGridCellCoords ( oldBottom
,
5973 m_selectedTopLeft
.SetCol( 0 );
5976 if ( oldBottom
< row
)
5978 need_refresh
[2] = TRUE
;
5979 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5980 wxGridCellCoords ( row
,
5982 m_selectedBottomRight
.SetRow( row
);
5985 if ( oldRight
< m_numCols
- 1 )
5987 need_refresh
[3] = TRUE
;
5988 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5990 wxGridCellCoords ( oldBottom
,
5992 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5995 for (i
= 0; i
< 4; i
++ )
5996 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5997 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6001 r
= SelectionToDeviceRect();
6003 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6005 m_selectedTopLeft
.Set( row
, 0 );
6006 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6007 r
= SelectionToDeviceRect();
6008 m_gridWin
->Refresh( FALSE
, &r
);
6011 wxGridRangeSelectEvent
gridEvt( GetId(),
6012 wxEVT_GRID_RANGE_SELECT
,
6015 m_selectedBottomRight
);
6017 GetEventHandler()->ProcessEvent(gridEvt
);
6021 void wxGrid::SelectCol( int col
, bool addToSelected
)
6023 if ( IsSelection() && addToSelected
)
6026 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
6029 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6030 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6031 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6032 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6034 if ( oldLeft
> col
)
6036 need_refresh
[0] = TRUE
;
6037 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6038 wxGridCellCoords ( m_numRows
- 1,
6040 m_selectedTopLeft
.SetCol( col
);
6045 need_refresh
[1] = TRUE
;
6046 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6047 wxGridCellCoords ( oldTop
- 1,
6049 m_selectedTopLeft
.SetRow( 0 );
6052 if ( oldRight
< col
)
6054 need_refresh
[2] = TRUE
;
6055 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6056 wxGridCellCoords ( m_numRows
- 1,
6058 m_selectedBottomRight
.SetCol( col
);
6061 if ( oldBottom
< m_numRows
- 1 )
6063 need_refresh
[3] = TRUE
;
6064 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6066 wxGridCellCoords ( m_numRows
- 1,
6068 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6071 for (i
= 0; i
< 4; i
++ )
6072 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6073 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6079 r
= SelectionToDeviceRect();
6081 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6083 m_selectedTopLeft
.Set( 0, col
);
6084 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6085 r
= SelectionToDeviceRect();
6086 m_gridWin
->Refresh( FALSE
, &r
);
6089 wxGridRangeSelectEvent
gridEvt( GetId(),
6090 wxEVT_GRID_RANGE_SELECT
,
6093 m_selectedBottomRight
);
6095 GetEventHandler()->ProcessEvent(gridEvt
);
6099 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6102 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6104 if ( topRow
> bottomRow
)
6111 if ( leftCol
> rightCol
)
6118 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6119 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6121 if ( m_selectedTopLeft
!= updateTopLeft
||
6122 m_selectedBottomRight
!= updateBottomRight
)
6124 // Compute two optimal update rectangles:
6125 // Either one rectangle is a real subset of the
6126 // other, or they are (almost) disjoint!
6128 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
6131 // Store intermediate values
6132 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6133 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6134 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6135 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6137 // Determine the outer/inner coordinates.
6138 if (oldLeft
> leftCol
)
6144 if (oldTop
> topRow
)
6150 if (oldRight
< rightCol
)
6153 oldRight
= rightCol
;
6156 if (oldBottom
< bottomRow
)
6159 oldBottom
= bottomRow
;
6163 // Now, either the stuff marked old is the outer
6164 // rectangle or we don't have a situation where one
6165 // is contained in the other.
6167 if ( oldLeft
< leftCol
)
6169 need_refresh
[0] = TRUE
;
6170 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6172 wxGridCellCoords ( oldBottom
,
6176 if ( oldTop
< topRow
)
6178 need_refresh
[1] = TRUE
;
6179 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6181 wxGridCellCoords ( topRow
- 1,
6185 if ( oldRight
> rightCol
)
6187 need_refresh
[2] = TRUE
;
6188 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6190 wxGridCellCoords ( oldBottom
,
6194 if ( oldBottom
> bottomRow
)
6196 need_refresh
[3] = TRUE
;
6197 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6199 wxGridCellCoords ( oldBottom
,
6205 m_selectedTopLeft
= updateTopLeft
;
6206 m_selectedBottomRight
= updateBottomRight
;
6208 // various Refresh() calls
6209 for (i
= 0; i
< 4; i
++ )
6210 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6211 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6214 // only generate an event if the block is not being selected by
6215 // dragging the mouse (in which case the event will be generated in
6216 // the mouse event handler)
6217 if ( !m_isDragging
)
6219 wxGridRangeSelectEvent
gridEvt( GetId(),
6220 wxEVT_GRID_RANGE_SELECT
,
6223 m_selectedBottomRight
);
6225 GetEventHandler()->ProcessEvent(gridEvt
);
6229 void wxGrid::SelectAll()
6231 m_selectedTopLeft
.Set( 0, 0 );
6232 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6234 m_gridWin
->Refresh();
6238 void wxGrid::ClearSelection()
6240 m_selectedTopLeft
= wxGridNoCellCoords
;
6241 m_selectedBottomRight
= wxGridNoCellCoords
;
6245 // This function returns the rectangle that encloses the given block
6246 // in device coords clipped to the client size of the grid window.
6248 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6249 const wxGridCellCoords
&bottomRight
)
6251 wxRect
rect( wxGridNoCellRect
);
6254 cellRect
= CellToRect( topLeft
);
6255 if ( cellRect
!= wxGridNoCellRect
)
6261 rect
= wxRect( 0, 0, 0, 0 );
6264 cellRect
= CellToRect( bottomRight
);
6265 if ( cellRect
!= wxGridNoCellRect
)
6271 return wxGridNoCellRect
;
6274 // convert to scrolled coords
6276 int left
, top
, right
, bottom
;
6277 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6278 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6281 m_gridWin
->GetClientSize( &cw
, &ch
);
6283 rect
.SetLeft( wxMax(0, left
) );
6284 rect
.SetTop( wxMax(0, top
) );
6285 rect
.SetRight( wxMin(cw
, right
) );
6286 rect
.SetBottom( wxMin(ch
, bottom
) );
6294 // ------ Grid event classes
6297 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6299 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6300 int row
, int col
, int x
, int y
,
6301 bool control
, bool shift
, bool alt
, bool meta
)
6302 : wxNotifyEvent( type
, id
)
6308 m_control
= control
;
6313 SetEventObject(obj
);
6317 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6319 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6320 int rowOrCol
, int x
, int y
,
6321 bool control
, bool shift
, bool alt
, bool meta
)
6322 : wxNotifyEvent( type
, id
)
6324 m_rowOrCol
= rowOrCol
;
6327 m_control
= control
;
6332 SetEventObject(obj
);
6336 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6338 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6339 const wxGridCellCoords
& topLeft
,
6340 const wxGridCellCoords
& bottomRight
,
6341 bool control
, bool shift
, bool alt
, bool meta
)
6342 : wxNotifyEvent( type
, id
)
6344 m_topLeft
= topLeft
;
6345 m_bottomRight
= bottomRight
;
6346 m_control
= control
;
6351 SetEventObject(obj
);
6355 #endif // ifndef wxUSE_NEW_GRID