1 ///////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
46 // this include needs to be outside precomp for BCC
47 #include "wx/textfile.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
58 struct wxGridCellWithAttr
60 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
61 : coords(row
, col
), attr(attr_
)
70 wxGridCellCoords coords
;
74 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
76 #include "wx/arrimpl.cpp"
78 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
79 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
88 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
89 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
90 const wxPoint
&pos
, const wxSize
&size
);
95 void OnPaint( wxPaintEvent
& event
);
96 void OnMouseEvent( wxMouseEvent
& event
);
97 void OnKeyDown( wxKeyEvent
& event
);
99 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
100 DECLARE_EVENT_TABLE()
104 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
107 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
108 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
109 const wxPoint
&pos
, const wxSize
&size
);
114 void OnPaint( wxPaintEvent
&event
);
115 void OnMouseEvent( wxMouseEvent
& event
);
116 void OnKeyDown( wxKeyEvent
& event
);
118 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
119 DECLARE_EVENT_TABLE()
123 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
126 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
);
133 void OnMouseEvent( wxMouseEvent
& event
);
134 void OnKeyDown( wxKeyEvent
& event
);
135 void OnPaint( wxPaintEvent
& event
);
137 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
138 DECLARE_EVENT_TABLE()
141 class WXDLLEXPORT wxGridWindow
: public wxPanel
146 m_owner
= (wxGrid
*)NULL
;
147 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
148 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
151 wxGridWindow( wxGrid
*parent
,
152 wxGridRowLabelWindow
*rowLblWin
,
153 wxGridColLabelWindow
*colLblWin
,
154 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
157 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
161 wxGridRowLabelWindow
*m_rowLabelWin
;
162 wxGridColLabelWindow
*m_colLabelWin
;
164 void OnPaint( wxPaintEvent
&event
);
165 void OnMouseEvent( wxMouseEvent
& event
);
166 void OnKeyDown( wxKeyEvent
& );
167 void OnEraseBackground( wxEraseEvent
& );
170 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
171 DECLARE_EVENT_TABLE()
176 class wxGridCellEditorEvtHandler
: public wxEvtHandler
179 wxGridCellEditorEvtHandler()
180 : m_grid(0), m_editor(0)
182 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
183 : m_grid(grid
), m_editor(editor
)
186 void OnKeyDown(wxKeyEvent
& event
);
187 void OnChar(wxKeyEvent
& event
);
191 wxGridCellEditor
* m_editor
;
192 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
193 DECLARE_EVENT_TABLE()
197 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
198 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
199 EVT_KEY_DOWN( wxGridCellEditorEvtHandler
::OnKeyDown
)
200 EVT_CHAR( wxGridCellEditorEvtHandler
::OnChar
)
205 // ----------------------------------------------------------------------------
206 // the internal data representation used by wxGridCellAttrProvider
207 // ----------------------------------------------------------------------------
209 // this class stores attributes set for cells
210 class WXDLLEXPORT wxGridCellAttrData
213 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
214 wxGridCellAttr
*GetAttr(int row
, int col
) const;
215 void UpdateAttrRows( size_t pos
, int numRows
);
216 void UpdateAttrCols( size_t pos
, int numCols
);
219 // searches for the attr for given cell, returns wxNOT_FOUND if not found
220 int FindIndex(int row
, int col
) const;
222 wxGridCellWithAttrArray m_attrs
;
225 // this class stores attributes set for rows or columns
226 class WXDLLEXPORT wxGridRowOrColAttrData
229 ~wxGridRowOrColAttrData();
231 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
232 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
233 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
236 wxArrayInt m_rowsOrCols
;
237 wxArrayAttrs m_attrs
;
240 // NB: this is just a wrapper around 3 objects: one which stores cell
241 // attributes, and 2 others for row/col ones
242 class WXDLLEXPORT wxGridCellAttrProviderData
245 wxGridCellAttrData m_cellAttrs
;
246 wxGridRowOrColAttrData m_rowAttrs
,
250 // ----------------------------------------------------------------------------
251 // conditional compilation
252 // ----------------------------------------------------------------------------
254 #ifndef WXGRID_DRAW_LINES
255 #define WXGRID_DRAW_LINES 1
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 //#define DEBUG_ATTR_CACHE
263 #ifdef DEBUG_ATTR_CACHE
264 static size_t gs_nAttrCacheHits
= 0;
265 static size_t gs_nAttrCacheMisses
= 0;
266 #endif // DEBUG_ATTR_CACHE
268 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
269 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
272 // TODO: fixed so far - make configurable later (and also different for x/y)
273 static const size_t GRID_SCROLL_LINE
= 10;
275 // ============================================================================
277 // ============================================================================
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 wxGridCellEditor
::wxGridCellEditor()
289 wxGridCellEditor
::~wxGridCellEditor()
294 void wxGridCellEditor
::Create(wxWindow
* WXUNUSED(parent
),
295 wxWindowID
WXUNUSED(id
),
296 wxEvtHandler
* evtHandler
)
299 m_control
->PushEventHandler(evtHandler
);
302 void wxGridCellEditor
::PaintBackground(const wxRect
& rectCell
,
303 wxGridCellAttr
*attr
)
305 // erase the background because we might not fill the cell
306 wxClientDC
dc(m_control
->GetParent());
307 dc
.SetPen(*wxTRANSPARENT_PEN
);
308 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
309 dc
.DrawRectangle(rectCell
);
311 // redraw the control we just painted over
312 m_control
->Refresh();
315 void wxGridCellEditor
::Destroy()
319 m_control
->Destroy();
324 void wxGridCellEditor
::Show(bool show
, wxGridCellAttr
*attr
)
326 wxASSERT_MSG(m_control
,
327 wxT("The wxGridCellEditor must be Created first!"));
328 m_control
->Show(show
);
332 // set the colours/fonts if we have any
335 if ( attr
->HasTextColour() )
337 m_colFgOld
= m_control
->GetForegroundColour();
338 m_control
->SetForegroundColour(attr
->GetTextColour());
341 if ( attr
->HasBackgroundColour() )
343 m_colBgOld
= m_control
->GetBackgroundColour();
344 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
347 if ( attr
->HasFont() )
349 m_fontOld
= m_control
->GetFont();
350 m_control
->SetFont(attr
->GetFont());
353 // can't do anything more in the base class version, the other
354 // attributes may only be used by the derived classes
359 // restore the standard colours fonts
360 if ( m_colFgOld
.Ok() )
362 m_control
->SetForegroundColour(m_colFgOld
);
363 m_colFgOld
= wxNullColour
;
366 if ( m_colBgOld
.Ok() )
368 m_control
->SetBackgroundColour(m_colBgOld
);
369 m_colBgOld
= wxNullColour
;
372 if ( m_fontOld
.Ok() )
374 m_control
->SetFont(m_fontOld
);
375 m_fontOld
= wxNullFont
;
380 void wxGridCellEditor
::SetSize(const wxRect
& rect
)
382 wxASSERT_MSG(m_control
,
383 wxT("The wxGridCellEditor must be Created first!"));
384 m_control
->SetSize(rect
);
387 void wxGridCellEditor
::HandleReturn(wxKeyEvent
& event
)
393 void wxGridCellEditor
::StartingKey(wxKeyEvent
& event
)
395 wxASSERT_MSG(m_control
,
396 wxT("The wxGridCellEditor must be Created first!"));
398 // pass the event to the control
399 m_control
->GetEventHandler()->ProcessEvent(event
);
402 // ----------------------------------------------------------------------------
403 // wxGridCellTextEditor
404 // ----------------------------------------------------------------------------
406 wxGridCellTextEditor
::wxGridCellTextEditor()
410 void wxGridCellTextEditor
::Create(wxWindow
* parent
,
412 wxEvtHandler
* evtHandler
)
414 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
415 wxDefaultPosition
, wxDefaultSize
416 #if defined(__WXMSW__)
417 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
421 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
424 void wxGridCellTextEditor
::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
425 wxGridCellAttr
* WXUNUSED(attr
))
427 // as we fill the entire client area, don't do anything here to minimize
431 void wxGridCellTextEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
433 wxASSERT_MSG(m_control
,
434 wxT("The wxGridCellEditor must be Created first!"));
436 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
437 Text()->SetValue(m_startValue
);
438 Text()->SetInsertionPointEnd();
443 bool wxGridCellTextEditor
::EndEdit(int row
, int col
, bool saveValue
,
446 wxASSERT_MSG(m_control
,
447 wxT("The wxGridCellEditor must be Created first!"));
449 bool changed
= FALSE
;
450 wxString value
= Text()->GetValue();
451 if (value
!= m_startValue
)
455 grid
->GetTable()->SetValue(row
, col
, value
);
457 m_startValue
= wxEmptyString
;
458 Text()->SetValue(m_startValue
);
464 void wxGridCellTextEditor
::Reset()
466 wxASSERT_MSG(m_control
,
467 wxT("The wxGridCellEditor must be Created first!"));
469 Text()->SetValue(m_startValue
);
470 Text()->SetInsertionPointEnd();
473 void wxGridCellTextEditor
::StartingKey(wxKeyEvent
& event
)
475 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
477 // insert the key in the control
478 long keycode
= event
.KeyCode();
479 if ( isprint(keycode
) )
481 // FIXME this is not going to work for non letters...
482 if ( !event
.ShiftDown() )
484 keycode
= tolower(keycode
);
487 Text()->AppendText((wxChar
)keycode
);
497 void wxGridCellTextEditor
::HandleReturn(wxKeyEvent
& event
)
499 #if defined(__WXMOTIF__) || defined(__WXGTK__)
500 // wxMotif needs a little extra help...
501 int pos
= Text()->GetInsertionPoint();
502 wxString
s( Text()->GetValue() );
503 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
505 Text()->SetInsertionPoint( pos
);
507 // the other ports can handle a Return key press
513 // ----------------------------------------------------------------------------
514 // wxGridCellBoolEditor
515 // ----------------------------------------------------------------------------
517 void wxGridCellBoolEditor
::Create(wxWindow
* parent
,
519 wxEvtHandler
* evtHandler
)
521 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
522 wxDefaultPosition
, wxDefaultSize
,
525 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
528 void wxGridCellBoolEditor
::SetSize(const wxRect
& r
)
530 // position it in the centre of the rectangle (TODO: support alignment?)
532 m_control
->GetSize(&w
, &h
);
534 // the checkbox without label still has some space to the right in wxGTK,
535 // so shift it to the right
540 m_control
->Move(r
.x
+ r
.width
/2 - w
/2, r
.y
+ r
.height
/2 - h
/2);
543 void wxGridCellBoolEditor
::Show(bool show
, wxGridCellAttr
*attr
)
545 wxGridCellEditor
::Show(show
, attr
);
548 // VZ: normally base class already does it, but it doesn't work (FIXME)
549 wxColour colBg
= attr ? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
550 CBox()->SetBackgroundColour(colBg
);
554 void wxGridCellBoolEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
556 wxASSERT_MSG(m_control
,
557 wxT("The wxGridCellEditor must be Created first!"));
559 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
); // FIXME-DATA
560 CBox()->SetValue(m_startValue
);
564 bool wxGridCellBoolEditor
::EndEdit(int row
, int col
,
568 wxASSERT_MSG(m_control
,
569 wxT("The wxGridCellEditor must be Created first!"));
571 bool changed
= FALSE
;
572 bool value
= CBox()->GetValue();
573 if ( value
!= m_startValue
)
579 grid
->GetTable()->SetValue(row
, col
, value ?
_T("1") : wxEmptyString
);
585 void wxGridCellBoolEditor
::Reset()
587 wxASSERT_MSG(m_control
,
588 wxT("The wxGridCellEditor must be Created first!"));
590 CBox()->SetValue(m_startValue
);
593 void wxGridCellBoolEditor
::StartingKey(wxKeyEvent
& event
)
598 // ----------------------------------------------------------------------------
599 // wxGridCellEditorEvtHandler
600 // ----------------------------------------------------------------------------
602 void wxGridCellEditorEvtHandler
::OnKeyDown(wxKeyEvent
& event
)
604 switch ( event
.KeyCode() )
608 m_grid
->DisableCellEditControl();
612 event
.Skip( m_grid
->ProcessEvent( event
) );
616 if (!m_grid
->ProcessEvent(event
))
617 m_editor
->HandleReturn(event
);
626 void wxGridCellEditorEvtHandler
::OnChar(wxKeyEvent
& event
)
628 switch ( event
.KeyCode() )
640 // ============================================================================
642 // ============================================================================
644 // ----------------------------------------------------------------------------
645 // wxGridCellRenderer
646 // ----------------------------------------------------------------------------
648 void wxGridCellRenderer
::Draw(wxGrid
& grid
,
649 wxGridCellAttr
& attr
,
655 dc
.SetBackgroundMode( wxSOLID
);
659 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
663 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
666 dc
.SetPen( *wxTRANSPARENT_PEN
);
667 dc
.DrawRectangle(rect
);
670 // ----------------------------------------------------------------------------
671 // wxGridCellStringRenderer
672 // ----------------------------------------------------------------------------
674 void wxGridCellStringRenderer
::Draw(wxGrid
& grid
,
675 wxGridCellAttr
& attr
,
677 const wxRect
& rectCell
,
681 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
683 // now we only have to draw the text
684 dc
.SetBackgroundMode( wxTRANSPARENT
);
686 // TODO some special colours for attr.IsReadOnly() case?
690 dc
.SetTextBackground( grid
.GetSelectionBackground() );
691 dc
.SetTextForeground( grid
.GetSelectionForeground() );
695 dc
.SetTextBackground( attr
.GetBackgroundColour() );
696 dc
.SetTextForeground( attr
.GetTextColour() );
698 dc
.SetFont( attr
.GetFont() );
701 attr
.GetAlignment(&hAlign
, &vAlign
);
703 wxRect rect
= rectCell
;
709 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
710 rect
, hAlign
, vAlign
);
713 // ----------------------------------------------------------------------------
714 // wxGridCellBoolRenderer
715 // ----------------------------------------------------------------------------
717 void wxGridCellBoolRenderer
::Draw(wxGrid
& grid
,
718 wxGridCellAttr
& attr
,
724 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
726 // between checkmark and box
727 static const wxCoord margin
= 4;
730 static wxCoord s_checkSize
= 0;
731 if ( s_checkSize
== 0 )
733 // compute it only once (no locks for MT safeness in GUI thread...)
734 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
735 wxSize size
= checkbox
->GetBestSize();
736 s_checkSize
= size
.y
+ margin
;
738 // FIXME wxGTK::wxCheckBox::GetBestSize() is really weird...
740 s_checkSize
-= size
.y
/ 2;
746 // draw a check mark in the centre (ignoring alignment - TODO)
748 rectMark
.x
= rect
.x
+ rect
.width
/2 - s_checkSize
/2;
749 rectMark
.y
= rect
.y
+ rect
.height
/2 - s_checkSize
/2;
750 rectMark
.width
= rectMark
.height
= s_checkSize
;
752 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
753 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
754 dc
.DrawRectangle(rectMark
);
756 rectMark
.Inflate(-margin
);
758 if ( !!grid
.GetTable()->GetValue(row
, col
) ) // FIXME-DATA
760 dc
.SetTextForeground(attr
.GetTextColour());
761 dc
.DrawCheckMark(rectMark
);
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
769 const wxColour
& wxGridCellAttr
::GetTextColour() const
775 else if (m_defGridAttr
!= this)
777 return m_defGridAttr
->GetTextColour();
781 wxFAIL_MSG(wxT("Missing default cell attribute"));
787 const wxColour
& wxGridCellAttr
::GetBackgroundColour() const
789 if (HasBackgroundColour())
791 else if (m_defGridAttr
!= this)
792 return m_defGridAttr
->GetBackgroundColour();
795 wxFAIL_MSG(wxT("Missing default cell attribute"));
801 const wxFont
& wxGridCellAttr
::GetFont() const
805 else if (m_defGridAttr
!= this)
806 return m_defGridAttr
->GetFont();
809 wxFAIL_MSG(wxT("Missing default cell attribute"));
815 void wxGridCellAttr
::GetAlignment(int *hAlign
, int *vAlign
) const
819 if ( hAlign
) *hAlign
= m_hAlign
;
820 if ( vAlign
) *vAlign
= m_vAlign
;
822 else if (m_defGridAttr
!= this)
823 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
826 wxFAIL_MSG(wxT("Missing default cell attribute"));
831 wxGridCellRenderer
* wxGridCellAttr
::GetRenderer() const
835 else if (m_defGridAttr
!= this)
836 return m_defGridAttr
->GetRenderer();
839 wxFAIL_MSG(wxT("Missing default cell attribute"));
844 wxGridCellEditor
* wxGridCellAttr
::GetEditor() const
848 else if (m_defGridAttr
!= this)
849 return m_defGridAttr
->GetEditor();
852 wxFAIL_MSG(wxT("Missing default cell attribute"));
857 // ----------------------------------------------------------------------------
858 // wxGridCellAttrData
859 // ----------------------------------------------------------------------------
861 void wxGridCellAttrData
::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
863 int n
= FindIndex(row
, col
);
864 if ( n
== wxNOT_FOUND
)
867 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
873 // change the attribute
874 m_attrs
[(size_t)n
].attr
= attr
;
878 // remove this attribute
879 m_attrs
.RemoveAt((size_t)n
);
884 wxGridCellAttr
*wxGridCellAttrData
::GetAttr(int row
, int col
) const
886 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
888 int n
= FindIndex(row
, col
);
889 if ( n
!= wxNOT_FOUND
)
891 attr
= m_attrs
[(size_t)n
].attr
;
898 void wxGridCellAttrData
::UpdateAttrRows( size_t pos
, int numRows
)
900 size_t count
= m_attrs
.GetCount();
901 for ( size_t n
= 0; n
< count
; n
++ )
903 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
904 wxCoord row
= coords
.GetRow();
905 if ((size_t)row
>= pos
)
909 // If rows inserted, include row counter where necessary
910 coords
.SetRow(row
+ numRows
);
912 else if (numRows
< 0)
914 // If rows deleted ...
915 if ((size_t)row
>= pos
- numRows
)
917 // ...either decrement row counter (if row still exists)...
918 coords
.SetRow(row
+ numRows
);
922 // ...or remove the attribute
923 m_attrs
.RemoveAt((size_t)n
);
931 void wxGridCellAttrData
::UpdateAttrCols( size_t pos
, int numCols
)
933 size_t count
= m_attrs
.GetCount();
934 for ( size_t n
= 0; n
< count
; n
++ )
936 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
937 wxCoord col
= coords
.GetCol();
938 if ( (size_t)col
>= pos
)
942 // If rows inserted, include row counter where necessary
943 coords
.SetCol(col
+ numCols
);
945 else if (numCols
< 0)
947 // If rows deleted ...
948 if ((size_t)col
>= pos
- numCols
)
950 // ...either decrement row counter (if row still exists)...
951 coords
.SetCol(col
+ numCols
);
955 // ...or remove the attribute
956 m_attrs
.RemoveAt((size_t)n
);
964 int wxGridCellAttrData
::FindIndex(int row
, int col
) const
966 size_t count
= m_attrs
.GetCount();
967 for ( size_t n
= 0; n
< count
; n
++ )
969 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
970 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
979 // ----------------------------------------------------------------------------
980 // wxGridRowOrColAttrData
981 // ----------------------------------------------------------------------------
983 wxGridRowOrColAttrData
::~wxGridRowOrColAttrData()
985 size_t count
= m_attrs
.Count();
986 for ( size_t n
= 0; n
< count
; n
++ )
988 m_attrs
[n
]->DecRef();
992 wxGridCellAttr
*wxGridRowOrColAttrData
::GetAttr(int rowOrCol
) const
994 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
996 int n
= m_rowsOrCols
.Index(rowOrCol
);
997 if ( n
!= wxNOT_FOUND
)
999 attr
= m_attrs
[(size_t)n
];
1006 void wxGridRowOrColAttrData
::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1008 int n
= m_rowsOrCols
.Index(rowOrCol
);
1009 if ( n
== wxNOT_FOUND
)
1011 // add the attribute
1012 m_rowsOrCols
.Add(rowOrCol
);
1019 // change the attribute
1020 m_attrs
[(size_t)n
] = attr
;
1024 // remove this attribute
1025 m_attrs
[(size_t)n
]->DecRef();
1026 m_rowsOrCols
.RemoveAt((size_t)n
);
1027 m_attrs
.RemoveAt((size_t)n
);
1032 void wxGridRowOrColAttrData
::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1034 size_t count
= m_attrs
.GetCount();
1035 for ( size_t n
= 0; n
< count
; n
++ )
1037 int & rowOrCol
= m_rowsOrCols
[n
];
1038 if ( (size_t)rowOrCol
>= pos
)
1040 if ( numRowsOrCols
> 0 )
1042 // If rows inserted, include row counter where necessary
1043 rowOrCol
+= numRowsOrCols
;
1045 else if ( numRowsOrCols
< 0)
1047 // If rows deleted, either decrement row counter (if row still exists)
1048 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1049 rowOrCol
+= numRowsOrCols
;
1052 m_rowsOrCols
.RemoveAt((size_t)n
);
1053 m_attrs
.RemoveAt((size_t)n
);
1061 // ----------------------------------------------------------------------------
1062 // wxGridCellAttrProvider
1063 // ----------------------------------------------------------------------------
1065 wxGridCellAttrProvider
::wxGridCellAttrProvider()
1067 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1070 wxGridCellAttrProvider
::~wxGridCellAttrProvider()
1075 void wxGridCellAttrProvider
::InitData()
1077 m_data
= new wxGridCellAttrProviderData
;
1080 wxGridCellAttr
*wxGridCellAttrProvider
::GetAttr(int row
, int col
) const
1082 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1085 // first look for the attribute of this specific cell
1086 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1090 // then look for the col attr (col attributes are more common than
1091 // the row ones, hence they have priority)
1092 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1097 // finally try the row attributes
1098 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1105 void wxGridCellAttrProvider
::SetAttr(wxGridCellAttr
*attr
,
1111 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1114 void wxGridCellAttrProvider
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1119 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1122 void wxGridCellAttrProvider
::SetColAttr(wxGridCellAttr
*attr
, int col
)
1127 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1130 void wxGridCellAttrProvider
::UpdateAttrRows( size_t pos
, int numRows
)
1134 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1136 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1140 void wxGridCellAttrProvider
::UpdateAttrCols( size_t pos
, int numCols
)
1144 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1146 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1150 // ----------------------------------------------------------------------------
1152 // ----------------------------------------------------------------------------
1154 //////////////////////////////////////////////////////////////////////
1156 // Abstract base class for grid data (the model)
1158 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1161 wxGridTableBase
::wxGridTableBase()
1163 m_view
= (wxGrid
*) NULL
;
1164 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1167 wxGridTableBase
::~wxGridTableBase()
1169 delete m_attrProvider
;
1172 void wxGridTableBase
::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1174 delete m_attrProvider
;
1175 m_attrProvider
= attrProvider
;
1178 wxGridCellAttr
*wxGridTableBase
::GetAttr(int row
, int col
)
1180 if ( m_attrProvider
)
1181 return m_attrProvider
->GetAttr(row
, col
);
1183 return (wxGridCellAttr
*)NULL
;
1186 void wxGridTableBase
::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1188 if ( m_attrProvider
)
1190 m_attrProvider
->SetAttr(attr
, row
, col
);
1194 // as we take ownership of the pointer and don't store it, we must
1200 void wxGridTableBase
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1202 if ( m_attrProvider
)
1204 m_attrProvider
->SetRowAttr(attr
, row
);
1208 // as we take ownership of the pointer and don't store it, we must
1214 void wxGridTableBase
::SetColAttr(wxGridCellAttr
*attr
, int col
)
1216 if ( m_attrProvider
)
1218 m_attrProvider
->SetColAttr(attr
, col
);
1222 // as we take ownership of the pointer and don't store it, we must
1228 void wxGridTableBase
::UpdateAttrRows( size_t pos
, int numRows
)
1230 if ( m_attrProvider
)
1232 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1236 void wxGridTableBase
::UpdateAttrCols( size_t pos
, int numCols
)
1238 if ( m_attrProvider
)
1240 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1244 bool wxGridTableBase
::InsertRows( size_t pos
, size_t numRows
)
1246 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1247 "but your derived table class does not override this function") );
1252 bool wxGridTableBase
::AppendRows( size_t numRows
)
1254 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1255 "but your derived table class does not override this function"));
1260 bool wxGridTableBase
::DeleteRows( size_t pos
, size_t numRows
)
1262 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1263 "but your derived table class does not override this function"));
1268 bool wxGridTableBase
::InsertCols( size_t pos
, size_t numCols
)
1270 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1271 "but your derived table class does not override this function"));
1276 bool wxGridTableBase
::AppendCols( size_t numCols
)
1278 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1279 "but your derived table class does not override this function"));
1284 bool wxGridTableBase
::DeleteCols( size_t pos
, size_t numCols
)
1286 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1287 "but your derived table class does not override this function"));
1293 wxString wxGridTableBase
::GetRowLabelValue( int row
)
1300 wxString wxGridTableBase
::GetColLabelValue( int col
)
1302 // default col labels are:
1303 // cols 0 to 25 : A-Z
1304 // cols 26 to 675 : AA-ZZ
1309 for ( n
= 1; ; n
++ )
1311 s
+= (_T('A') + (wxChar
)( col
%26 ));
1313 if ( col
< 0 ) break;
1316 // reverse the string...
1318 for ( i
= 0; i
< n
; i
++ )
1328 //////////////////////////////////////////////////////////////////////
1330 // Message class for the grid table to send requests and notifications
1334 wxGridTableMessage
::wxGridTableMessage()
1336 m_table
= (wxGridTableBase
*) NULL
;
1342 wxGridTableMessage
::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1343 int commandInt1
, int commandInt2
)
1347 m_comInt1
= commandInt1
;
1348 m_comInt2
= commandInt2
;
1353 //////////////////////////////////////////////////////////////////////
1355 // A basic grid table for string data. An object of this class will
1356 // created by wxGrid if you don't specify an alternative table class.
1359 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1361 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1363 wxGridStringTable
::wxGridStringTable()
1368 wxGridStringTable
::wxGridStringTable( int numRows
, int numCols
)
1373 m_data
.Alloc( numRows
);
1376 sa
.Alloc( numCols
);
1377 for ( col
= 0; col
< numCols
; col
++ )
1379 sa
.Add( wxEmptyString
);
1382 for ( row
= 0; row
< numRows
; row
++ )
1388 wxGridStringTable
::~wxGridStringTable()
1392 long wxGridStringTable
::GetNumberRows()
1394 return m_data
.GetCount();
1397 long wxGridStringTable
::GetNumberCols()
1399 if ( m_data
.GetCount() > 0 )
1400 return m_data
[0].GetCount();
1405 wxString wxGridStringTable
::GetValue( int row
, int col
)
1407 // TODO: bounds checking
1409 return m_data
[row
][col
];
1412 void wxGridStringTable
::SetValue( int row
, int col
, const wxString
& s
)
1414 // TODO: bounds checking
1416 m_data
[row
][col
] = s
;
1419 bool wxGridStringTable
::IsEmptyCell( int row
, int col
)
1421 // TODO: bounds checking
1423 return (m_data
[row
][col
] == wxEmptyString
);
1427 void wxGridStringTable
::Clear()
1430 int numRows
, numCols
;
1432 numRows
= m_data
.GetCount();
1435 numCols
= m_data
[0].GetCount();
1437 for ( row
= 0; row
< numRows
; row
++ )
1439 for ( col
= 0; col
< numCols
; col
++ )
1441 m_data
[row
][col
] = wxEmptyString
;
1448 bool wxGridStringTable
::InsertRows( size_t pos
, size_t numRows
)
1452 size_t curNumRows
= m_data
.GetCount();
1453 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1455 if ( pos
>= curNumRows
)
1457 return AppendRows( numRows
);
1461 sa
.Alloc( curNumCols
);
1462 for ( col
= 0; col
< curNumCols
; col
++ )
1464 sa
.Add( wxEmptyString
);
1467 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1469 m_data
.Insert( sa
, row
);
1471 UpdateAttrRows( pos
, numRows
);
1474 wxGridTableMessage
msg( this,
1475 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1479 GetView()->ProcessTableMessage( msg
);
1485 bool wxGridStringTable
::AppendRows( size_t numRows
)
1489 size_t curNumRows
= m_data
.GetCount();
1490 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1493 if ( curNumCols
> 0 )
1495 sa
.Alloc( curNumCols
);
1496 for ( col
= 0; col
< curNumCols
; col
++ )
1498 sa
.Add( wxEmptyString
);
1502 for ( row
= 0; row
< numRows
; row
++ )
1509 wxGridTableMessage
msg( this,
1510 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1513 GetView()->ProcessTableMessage( msg
);
1519 bool wxGridStringTable
::DeleteRows( size_t pos
, size_t numRows
)
1523 size_t curNumRows
= m_data
.GetCount();
1525 if ( pos
>= curNumRows
)
1528 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1529 "Pos value is invalid for present table with %d rows",
1530 pos
, numRows
, curNumRows
);
1531 wxFAIL_MSG( wxT(errmsg
) );
1535 if ( numRows
> curNumRows
- pos
)
1537 numRows
= curNumRows
- pos
;
1540 if ( numRows
>= curNumRows
)
1542 m_data
.Empty(); // don't release memory just yet
1546 for ( n
= 0; n
< numRows
; n
++ )
1548 m_data
.Remove( pos
);
1551 UpdateAttrRows( pos
, -((int)numRows
) );
1554 wxGridTableMessage
msg( this,
1555 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1559 GetView()->ProcessTableMessage( msg
);
1565 bool wxGridStringTable
::InsertCols( size_t pos
, size_t numCols
)
1569 size_t curNumRows
= m_data
.GetCount();
1570 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1572 if ( pos
>= curNumCols
)
1574 return AppendCols( numCols
);
1577 for ( row
= 0; row
< curNumRows
; row
++ )
1579 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1581 m_data
[row
].Insert( wxEmptyString
, col
);
1584 UpdateAttrCols( pos
, numCols
);
1587 wxGridTableMessage
msg( this,
1588 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1592 GetView()->ProcessTableMessage( msg
);
1598 bool wxGridStringTable
::AppendCols( size_t numCols
)
1602 size_t curNumRows
= m_data
.GetCount();
1605 // TODO: something better than this ?
1607 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1608 "Call AppendRows() first") );
1612 for ( row
= 0; row
< curNumRows
; row
++ )
1614 for ( n
= 0; n
< numCols
; n
++ )
1616 m_data
[row
].Add( wxEmptyString
);
1622 wxGridTableMessage
msg( this,
1623 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1626 GetView()->ProcessTableMessage( msg
);
1632 bool wxGridStringTable
::DeleteCols( size_t pos
, size_t numCols
)
1636 size_t curNumRows
= m_data
.GetCount();
1637 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1639 if ( pos
>= curNumCols
)
1642 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1643 "Pos value is invalid for present table with %d cols",
1644 pos
, numCols
, curNumCols
);
1645 wxFAIL_MSG( wxT( errmsg
) );
1649 if ( numCols
> curNumCols
- pos
)
1651 numCols
= curNumCols
- pos
;
1654 for ( row
= 0; row
< curNumRows
; row
++ )
1656 if ( numCols
>= curNumCols
)
1658 m_data
[row
].Clear();
1662 for ( n
= 0; n
< numCols
; n
++ )
1664 m_data
[row
].Remove( pos
);
1668 UpdateAttrCols( pos
, -((int)numCols
) );
1671 wxGridTableMessage
msg( this,
1672 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1676 GetView()->ProcessTableMessage( msg
);
1682 wxString wxGridStringTable
::GetRowLabelValue( int row
)
1684 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1686 // using default label
1688 return wxGridTableBase
::GetRowLabelValue( row
);
1692 return m_rowLabels
[ row
];
1696 wxString wxGridStringTable
::GetColLabelValue( int col
)
1698 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1700 // using default label
1702 return wxGridTableBase
::GetColLabelValue( col
);
1706 return m_colLabels
[ col
];
1710 void wxGridStringTable
::SetRowLabelValue( int row
, const wxString
& value
)
1712 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1714 int n
= m_rowLabels
.GetCount();
1716 for ( i
= n
; i
<= row
; i
++ )
1718 m_rowLabels
.Add( wxGridTableBase
::GetRowLabelValue(i
) );
1722 m_rowLabels
[row
] = value
;
1725 void wxGridStringTable
::SetColLabelValue( int col
, const wxString
& value
)
1727 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1729 int n
= m_colLabels
.GetCount();
1731 for ( i
= n
; i
<= col
; i
++ )
1733 m_colLabels
.Add( wxGridTableBase
::GetColLabelValue(i
) );
1737 m_colLabels
[col
] = value
;
1742 //////////////////////////////////////////////////////////////////////
1743 //////////////////////////////////////////////////////////////////////
1745 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1747 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1748 EVT_PAINT( wxGridRowLabelWindow
::OnPaint
)
1749 EVT_MOUSE_EVENTS( wxGridRowLabelWindow
::OnMouseEvent
)
1750 EVT_KEY_DOWN( wxGridRowLabelWindow
::OnKeyDown
)
1753 wxGridRowLabelWindow
::wxGridRowLabelWindow( wxGrid
*parent
,
1755 const wxPoint
&pos
, const wxSize
&size
)
1756 : wxWindow( parent
, id
, pos
, size
)
1761 void wxGridRowLabelWindow
::OnPaint( wxPaintEvent
&event
)
1765 // NO - don't do this because it will set both the x and y origin
1766 // coords to match the parent scrolled window and we just want to
1767 // set the y coord - MB
1769 // m_owner->PrepareDC( dc );
1772 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1773 dc
.SetDeviceOrigin( 0, -y
);
1775 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1776 m_owner
->DrawRowLabels( dc
);
1780 void wxGridRowLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1782 m_owner
->ProcessRowLabelMouseEvent( event
);
1786 // This seems to be required for wxMotif otherwise the mouse
1787 // cursor must be in the cell edit control to get key events
1789 void wxGridRowLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
1791 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1796 //////////////////////////////////////////////////////////////////////
1798 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1800 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1801 EVT_PAINT( wxGridColLabelWindow
::OnPaint
)
1802 EVT_MOUSE_EVENTS( wxGridColLabelWindow
::OnMouseEvent
)
1803 EVT_KEY_DOWN( wxGridColLabelWindow
::OnKeyDown
)
1806 wxGridColLabelWindow
::wxGridColLabelWindow( wxGrid
*parent
,
1808 const wxPoint
&pos
, const wxSize
&size
)
1809 : wxWindow( parent
, id
, pos
, size
)
1814 void wxGridColLabelWindow
::OnPaint( wxPaintEvent
&event
)
1818 // NO - don't do this because it will set both the x and y origin
1819 // coords to match the parent scrolled window and we just want to
1820 // set the x coord - MB
1822 // m_owner->PrepareDC( dc );
1825 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1826 dc
.SetDeviceOrigin( -x
, 0 );
1828 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1829 m_owner
->DrawColLabels( dc
);
1833 void wxGridColLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1835 m_owner
->ProcessColLabelMouseEvent( event
);
1839 // This seems to be required for wxMotif otherwise the mouse
1840 // cursor must be in the cell edit control to get key events
1842 void wxGridColLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
1844 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1849 //////////////////////////////////////////////////////////////////////
1851 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1853 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1854 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow
::OnMouseEvent
)
1855 EVT_PAINT( wxGridCornerLabelWindow
::OnPaint
)
1856 EVT_KEY_DOWN( wxGridCornerLabelWindow
::OnKeyDown
)
1859 wxGridCornerLabelWindow
::wxGridCornerLabelWindow( wxGrid
*parent
,
1861 const wxPoint
&pos
, const wxSize
&size
)
1862 : wxWindow( parent
, id
, pos
, size
)
1867 void wxGridCornerLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1871 int client_height
= 0;
1872 int client_width
= 0;
1873 GetClientSize( &client_width
, &client_height
);
1875 dc
.SetPen( *wxBLACK_PEN
);
1876 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1877 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1879 dc
.SetPen( *wxWHITE_PEN
);
1880 dc
.DrawLine( 0, 0, client_width
, 0 );
1881 dc
.DrawLine( 0, 0, 0, client_height
);
1885 void wxGridCornerLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1887 m_owner
->ProcessCornerLabelMouseEvent( event
);
1891 // This seems to be required for wxMotif otherwise the mouse
1892 // cursor must be in the cell edit control to get key events
1894 void wxGridCornerLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
1896 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1901 //////////////////////////////////////////////////////////////////////
1903 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1905 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1906 EVT_PAINT( wxGridWindow
::OnPaint
)
1907 EVT_MOUSE_EVENTS( wxGridWindow
::OnMouseEvent
)
1908 EVT_KEY_DOWN( wxGridWindow
::OnKeyDown
)
1909 EVT_ERASE_BACKGROUND( wxGridWindow
::OnEraseBackground
)
1912 wxGridWindow
::wxGridWindow( wxGrid
*parent
,
1913 wxGridRowLabelWindow
*rowLblWin
,
1914 wxGridColLabelWindow
*colLblWin
,
1915 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1916 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1919 m_rowLabelWin
= rowLblWin
;
1920 m_colLabelWin
= colLblWin
;
1921 SetBackgroundColour( "WHITE" );
1925 wxGridWindow
::~wxGridWindow()
1930 void wxGridWindow
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1932 wxPaintDC
dc( this );
1933 m_owner
->PrepareDC( dc
);
1934 wxRegion reg
= GetUpdateRegion();
1935 m_owner
->CalcCellsExposed( reg
);
1936 m_owner
->DrawGridCellArea( dc
);
1937 #if WXGRID_DRAW_LINES
1938 m_owner
->DrawAllGridLines( dc
, reg
);
1943 void wxGridWindow
::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1945 wxPanel
::ScrollWindow( dx
, dy
, rect
);
1946 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1947 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1951 void wxGridWindow
::OnMouseEvent( wxMouseEvent
& event
)
1953 m_owner
->ProcessGridCellMouseEvent( event
);
1957 // This seems to be required for wxMotif otherwise the mouse
1958 // cursor must be in the cell edit control to get key events
1960 void wxGridWindow
::OnKeyDown( wxKeyEvent
& event
)
1962 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1965 void wxGridWindow
::OnEraseBackground(wxEraseEvent
&)
1971 //////////////////////////////////////////////////////////////////////
1974 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1976 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1977 EVT_PAINT( wxGrid
::OnPaint
)
1978 EVT_SIZE( wxGrid
::OnSize
)
1979 EVT_KEY_DOWN( wxGrid
::OnKeyDown
)
1980 EVT_ERASE_BACKGROUND( wxGrid
::OnEraseBackground
)
1983 wxGrid
::wxGrid( wxWindow
*parent
,
1988 const wxString
& name
)
1989 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1998 m_defaultCellAttr
->SafeDecRef();
2000 #ifdef DEBUG_ATTR_CACHE
2001 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2002 wxPrintf(_T("wxGrid attribute cache statistics: "
2003 "total: %u, hits: %u (%u%%)\n"),
2004 total
, gs_nAttrCacheHits
,
2005 total ?
(gs_nAttrCacheHits
*100) / total
: 0);
2014 // ----- internal init and update functions
2017 void wxGrid
::Create()
2019 m_created
= FALSE
; // set to TRUE by CreateGrid
2020 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2022 m_table
= (wxGridTableBase
*) NULL
;
2025 m_cellEditCtrlEnabled
= FALSE
;
2027 m_defaultCellAttr
= new wxGridCellAttr
;
2028 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2029 // RD: Should we fill the default attrs now or is waiting until Init() okay?
2034 m_currentCellCoords
= wxGridNoCellCoords
;
2036 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2037 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2039 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2044 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2049 m_colLabelWin
= new wxGridColLabelWindow( this,
2054 m_gridWin
= new wxGridWindow( this,
2061 SetTargetWindow( m_gridWin
);
2065 bool wxGrid
::CreateGrid( int numRows
, int numCols
)
2069 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2074 m_numRows
= numRows
;
2075 m_numCols
= numCols
;
2077 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2078 m_table
->SetView( this );
2087 bool wxGrid
::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2091 // RD: Actually, this should probably be allowed. I think it would be
2092 // nice to be able to switch multiple Tables in and out of a single
2093 // View at runtime. Is there anything in the implmentation that would
2096 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2101 m_numRows
= table
->GetNumberRows();
2102 m_numCols
= table
->GetNumberCols();
2105 m_table
->SetView( this );
2120 if ( m_numRows
<= 0 )
2121 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2123 if ( m_numCols
<= 0 )
2124 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2126 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2127 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2129 if ( m_rowLabelWin
)
2131 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2135 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2138 m_labelTextColour
= wxColour( _T("BLACK") );
2141 m_attrCache
.row
= -1;
2143 // TODO: something better than this ?
2145 m_labelFont
= this->GetFont();
2146 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2148 m_rowLabelHorizAlign
= wxLEFT
;
2149 m_rowLabelVertAlign
= wxCENTRE
;
2151 m_colLabelHorizAlign
= wxCENTRE
;
2152 m_colLabelVertAlign
= wxTOP
;
2154 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2155 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2157 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2158 m_defaultRowHeight
+= 8;
2160 m_defaultRowHeight
+= 4;
2163 m_rowHeights
.Alloc( m_numRows
);
2164 m_rowBottoms
.Alloc( m_numRows
);
2166 for ( i
= 0; i
< m_numRows
; i
++ )
2168 m_rowHeights
.Add( m_defaultRowHeight
);
2169 rowBottom
+= m_defaultRowHeight
;
2170 m_rowBottoms
.Add( rowBottom
);
2173 m_colWidths
.Alloc( m_numCols
);
2174 m_colRights
.Alloc( m_numCols
);
2176 for ( i
= 0; i
< m_numCols
; i
++ )
2178 m_colWidths
.Add( m_defaultColWidth
);
2179 colRight
+= m_defaultColWidth
;
2180 m_colRights
.Add( colRight
);
2183 // Set default cell attributes
2184 m_defaultCellAttr
->SetFont(GetFont());
2185 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2186 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2187 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2188 m_defaultCellAttr
->SetTextColour(
2189 wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2190 m_defaultCellAttr
->SetBackgroundColour(
2191 wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2194 m_gridLineColour
= wxColour( 128, 128, 255 );
2195 m_gridLinesEnabled
= TRUE
;
2197 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2198 m_winCapture
= (wxWindow
*)NULL
;
2200 m_dragRowOrCol
= -1;
2201 m_isDragging
= FALSE
;
2202 m_startDragPos
= wxDefaultPosition
;
2204 m_waitForSlowClick
= FALSE
;
2206 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2207 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2209 m_currentCellCoords
= wxGridNoCellCoords
;
2211 m_selectedTopLeft
= wxGridNoCellCoords
;
2212 m_selectedBottomRight
= wxGridNoCellCoords
;
2213 m_selectionBackground
= wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2214 m_selectionForeground
= wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2216 m_editable
= TRUE
; // default for whole grid
2218 m_inOnKeyDown
= FALSE
;
2224 void wxGrid
::CalcDimensions()
2227 GetClientSize( &cw
, &ch
);
2229 if ( m_numRows
> 0 && m_numCols
> 0 )
2231 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2232 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2234 // TODO: restore the scroll position that we had before sizing
2237 GetViewStart( &x
, &y
);
2238 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2239 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2245 void wxGrid
::CalcWindowSizes()
2248 GetClientSize( &cw
, &ch
);
2250 if ( m_cornerLabelWin
->IsShown() )
2251 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2253 if ( m_colLabelWin
->IsShown() )
2254 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2256 if ( m_rowLabelWin
->IsShown() )
2257 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2259 if ( m_gridWin
->IsShown() )
2260 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2264 // this is called when the grid table sends a message to say that it
2265 // has been redimensioned
2267 bool wxGrid
::Redimension( wxGridTableMessage
& msg
)
2271 switch ( msg
.GetId() )
2273 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2275 size_t pos
= msg
.GetCommandInt();
2276 int numRows
= msg
.GetCommandInt2();
2277 for ( i
= 0; i
< numRows
; i
++ )
2279 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2280 m_rowBottoms
.Insert( 0, pos
);
2282 m_numRows
+= numRows
;
2285 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2287 for ( i
= pos
; i
< m_numRows
; i
++ )
2289 bottom
+= m_rowHeights
[i
];
2290 m_rowBottoms
[i
] = bottom
;
2296 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2298 int numRows
= msg
.GetCommandInt();
2299 for ( i
= 0; i
< numRows
; i
++ )
2301 m_rowHeights
.Add( m_defaultRowHeight
);
2302 m_rowBottoms
.Add( 0 );
2305 int oldNumRows
= m_numRows
;
2306 m_numRows
+= numRows
;
2309 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2311 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2313 bottom
+= m_rowHeights
[i
];
2314 m_rowBottoms
[i
] = bottom
;
2320 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2322 size_t pos
= msg
.GetCommandInt();
2323 int numRows
= msg
.GetCommandInt2();
2324 for ( i
= 0; i
< numRows
; i
++ )
2326 m_rowHeights
.Remove( pos
);
2327 m_rowBottoms
.Remove( pos
);
2329 m_numRows
-= numRows
;
2334 m_colWidths
.Clear();
2335 m_colRights
.Clear();
2336 m_currentCellCoords
= wxGridNoCellCoords
;
2340 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2341 m_currentCellCoords
.Set( 0, 0 );
2344 for ( i
= 0; i
< m_numRows
; i
++ )
2346 h
+= m_rowHeights
[i
];
2347 m_rowBottoms
[i
] = h
;
2355 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2357 size_t pos
= msg
.GetCommandInt();
2358 int numCols
= msg
.GetCommandInt2();
2359 for ( i
= 0; i
< numCols
; i
++ )
2361 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2362 m_colRights
.Insert( 0, pos
);
2364 m_numCols
+= numCols
;
2367 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2369 for ( i
= pos
; i
< m_numCols
; i
++ )
2371 right
+= m_colWidths
[i
];
2372 m_colRights
[i
] = right
;
2378 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2380 int numCols
= msg
.GetCommandInt();
2381 for ( i
= 0; i
< numCols
; i
++ )
2383 m_colWidths
.Add( m_defaultColWidth
);
2384 m_colRights
.Add( 0 );
2387 int oldNumCols
= m_numCols
;
2388 m_numCols
+= numCols
;
2391 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2393 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2395 right
+= m_colWidths
[i
];
2396 m_colRights
[i
] = right
;
2402 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2404 size_t pos
= msg
.GetCommandInt();
2405 int numCols
= msg
.GetCommandInt2();
2406 for ( i
= 0; i
< numCols
; i
++ )
2408 m_colWidths
.Remove( pos
);
2409 m_colRights
.Remove( pos
);
2411 m_numCols
-= numCols
;
2415 #if 0 // leave the row alone here so that AppendCols will work subsequently
2417 m_rowHeights
.Clear();
2418 m_rowBottoms
.Clear();
2420 m_currentCellCoords
= wxGridNoCellCoords
;
2424 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2425 m_currentCellCoords
.Set( 0, 0 );
2428 for ( i
= 0; i
< m_numCols
; i
++ )
2430 w
+= m_colWidths
[i
];
2443 void wxGrid
::CalcRowLabelsExposed( wxRegion
& reg
)
2445 wxRegionIterator
iter( reg
);
2448 m_rowLabelsExposed
.Empty();
2455 // TODO: remove this when we can...
2456 // There is a bug in wxMotif that gives garbage update
2457 // rectangles if you jump-scroll a long way by clicking the
2458 // scrollbar with middle button. This is a work-around
2460 #if defined(__WXMOTIF__)
2462 m_gridWin
->GetClientSize( &cw
, &ch
);
2463 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2464 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2467 // logical bounds of update region
2470 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2471 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2473 // find the row labels within these bounds
2477 for ( row
= 0; row
< m_numRows
; row
++ )
2479 if ( m_rowBottoms
[row
] < top
) continue;
2481 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2482 if ( rowTop
> bottom
) break;
2484 m_rowLabelsExposed
.Add( row
);
2492 void wxGrid
::CalcColLabelsExposed( wxRegion
& reg
)
2494 wxRegionIterator
iter( reg
);
2497 m_colLabelsExposed
.Empty();
2504 // TODO: remove this when we can...
2505 // There is a bug in wxMotif that gives garbage update
2506 // rectangles if you jump-scroll a long way by clicking the
2507 // scrollbar with middle button. This is a work-around
2509 #if defined(__WXMOTIF__)
2511 m_gridWin
->GetClientSize( &cw
, &ch
);
2512 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2513 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2516 // logical bounds of update region
2519 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2520 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2522 // find the cells within these bounds
2526 for ( col
= 0; col
< m_numCols
; col
++ )
2528 if ( m_colRights
[col
] < left
) continue;
2530 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2531 if ( colLeft
> right
) break;
2533 m_colLabelsExposed
.Add( col
);
2541 void wxGrid
::CalcCellsExposed( wxRegion
& reg
)
2543 wxRegionIterator
iter( reg
);
2546 m_cellsExposed
.Empty();
2547 m_rowsExposed
.Empty();
2548 m_colsExposed
.Empty();
2550 int left
, top
, right
, bottom
;
2555 // TODO: remove this when we can...
2556 // There is a bug in wxMotif that gives garbage update
2557 // rectangles if you jump-scroll a long way by clicking the
2558 // scrollbar with middle button. This is a work-around
2560 #if defined(__WXMOTIF__)
2562 m_gridWin
->GetClientSize( &cw
, &ch
);
2563 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2564 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2565 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2566 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2569 // logical bounds of update region
2571 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2572 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2574 // find the cells within these bounds
2577 int colLeft
, rowTop
;
2578 for ( row
= 0; row
< m_numRows
; row
++ )
2580 if ( m_rowBottoms
[row
] <= top
) continue;
2582 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2583 if ( rowTop
> bottom
) break;
2585 m_rowsExposed
.Add( row
);
2587 for ( col
= 0; col
< m_numCols
; col
++ )
2589 if ( m_colRights
[col
] <= left
) continue;
2591 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2592 if ( colLeft
> right
) break;
2594 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2595 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2604 void wxGrid
::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2607 wxPoint
pos( event
.GetPosition() );
2608 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2610 if ( event
.Dragging() )
2612 m_isDragging
= TRUE
;
2614 if ( event
.LeftIsDown() )
2616 switch( m_cursorMode
)
2618 case WXGRID_CURSOR_RESIZE_ROW
:
2620 int cw
, ch
, left
, dummy
;
2621 m_gridWin
->GetClientSize( &cw
, &ch
);
2622 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2624 wxClientDC
dc( m_gridWin
);
2627 m_rowBottoms
[m_dragRowOrCol
] -
2628 m_rowHeights
[m_dragRowOrCol
] +
2629 WXGRID_MIN_ROW_HEIGHT
);
2630 dc
.SetLogicalFunction(wxINVERT
);
2631 if ( m_dragLastPos
>= 0 )
2633 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2635 dc
.DrawLine( left
, y
, left
+cw
, y
);
2640 case WXGRID_CURSOR_SELECT_ROW
:
2641 if ( (row
= YToRow( y
)) >= 0 &&
2642 !IsInSelection( row
, 0 ) )
2644 SelectRow( row
, TRUE
);
2647 // default label to suppress warnings about "enumeration value
2648 // 'xxx' not handled in switch
2656 m_isDragging
= FALSE
;
2659 // ------------ Entering or leaving the window
2661 if ( event
.Entering() || event
.Leaving() )
2663 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2667 // ------------ Left button pressed
2669 else if ( event
.LeftDown() )
2671 // don't send a label click event for a hit on the
2672 // edge of the row label - this is probably the user
2673 // wanting to resize the row
2675 if ( YToEdgeOfRow(y
) < 0 )
2679 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2681 SelectRow( row
, event
.ShiftDown() );
2682 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2687 // starting to drag-resize a row
2689 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2694 // ------------ Left double click
2696 else if (event
.LeftDClick() )
2698 if ( YToEdgeOfRow(y
) < 0 )
2701 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2706 // ------------ Left button released
2708 else if ( event
.LeftUp() )
2710 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2712 DoEndDragResizeRow();
2714 // Note: we are ending the event *after* doing
2715 // default processing in this case
2717 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2720 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2725 // ------------ Right button down
2727 else if ( event
.RightDown() )
2730 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2732 // no default action at the moment
2737 // ------------ Right double click
2739 else if ( event
.RightDClick() )
2742 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2744 // no default action at the moment
2749 // ------------ No buttons down and mouse moving
2751 else if ( event
.Moving() )
2753 m_dragRowOrCol
= YToEdgeOfRow( y
);
2754 if ( m_dragRowOrCol
>= 0 )
2756 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2758 // don't capture the mouse yet
2759 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2762 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2764 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2770 void wxGrid
::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2773 wxPoint
pos( event
.GetPosition() );
2774 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2776 if ( event
.Dragging() )
2778 m_isDragging
= TRUE
;
2780 if ( event
.LeftIsDown() )
2782 switch( m_cursorMode
)
2784 case WXGRID_CURSOR_RESIZE_COL
:
2786 int cw
, ch
, dummy
, top
;
2787 m_gridWin
->GetClientSize( &cw
, &ch
);
2788 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2790 wxClientDC
dc( m_gridWin
);
2793 m_colRights
[m_dragRowOrCol
] -
2794 m_colWidths
[m_dragRowOrCol
] +
2795 WXGRID_MIN_COL_WIDTH
);
2796 dc
.SetLogicalFunction(wxINVERT
);
2797 if ( m_dragLastPos
>= 0 )
2799 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2801 dc
.DrawLine( x
, top
, x
, top
+ch
);
2806 case WXGRID_CURSOR_SELECT_COL
:
2807 if ( (col
= XToCol( x
)) >= 0 &&
2808 !IsInSelection( 0, col
) )
2810 SelectCol( col
, TRUE
);
2813 // default label to suppress warnings about "enumeration value
2814 // 'xxx' not handled in switch
2822 m_isDragging
= FALSE
;
2825 // ------------ Entering or leaving the window
2827 if ( event
.Entering() || event
.Leaving() )
2829 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2833 // ------------ Left button pressed
2835 else if ( event
.LeftDown() )
2837 // don't send a label click event for a hit on the
2838 // edge of the col label - this is probably the user
2839 // wanting to resize the col
2841 if ( XToEdgeOfCol(x
) < 0 )
2845 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2847 SelectCol( col
, event
.ShiftDown() );
2848 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2853 // starting to drag-resize a col
2855 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2860 // ------------ Left double click
2862 if ( event
.LeftDClick() )
2864 if ( XToEdgeOfCol(x
) < 0 )
2867 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2872 // ------------ Left button released
2874 else if ( event
.LeftUp() )
2876 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2878 DoEndDragResizeCol();
2880 // Note: we are ending the event *after* doing
2881 // default processing in this case
2883 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2886 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2891 // ------------ Right button down
2893 else if ( event
.RightDown() )
2896 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2898 // no default action at the moment
2903 // ------------ Right double click
2905 else if ( event
.RightDClick() )
2908 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2910 // no default action at the moment
2915 // ------------ No buttons down and mouse moving
2917 else if ( event
.Moving() )
2919 m_dragRowOrCol
= XToEdgeOfCol( x
);
2920 if ( m_dragRowOrCol
>= 0 )
2922 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2924 // don't capture the cursor yet
2925 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2928 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2930 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2936 void wxGrid
::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2938 if ( event
.LeftDown() )
2940 // indicate corner label by having both row and
2943 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2949 else if ( event
.LeftDClick() )
2951 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2954 else if ( event
.RightDown() )
2956 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2958 // no default action at the moment
2962 else if ( event
.RightDClick() )
2964 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2966 // no default action at the moment
2971 void wxGrid
::ChangeCursorMode(CursorMode mode
,
2976 static const wxChar
*cursorModes
[] =
2985 wxLogTrace(_T("grid"),
2986 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2987 win
== m_colLabelWin ?
_T("colLabelWin")
2988 : win ?
_T("rowLabelWin")
2990 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2991 #endif // __WXDEBUG__
2993 if ( mode
== m_cursorMode
)
2998 // by default use the grid itself
3004 m_winCapture
->ReleaseMouse();
3005 m_winCapture
= (wxWindow
*)NULL
;
3008 m_cursorMode
= mode
;
3010 switch ( m_cursorMode
)
3012 case WXGRID_CURSOR_RESIZE_ROW
:
3013 win
->SetCursor( m_rowResizeCursor
);
3016 case WXGRID_CURSOR_RESIZE_COL
:
3017 win
->SetCursor( m_colResizeCursor
);
3021 win
->SetCursor( *wxSTANDARD_CURSOR
);
3024 // we need to capture mouse when resizing
3025 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3026 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3028 if ( captureMouse
&& resize
)
3030 win
->CaptureMouse();
3035 void wxGrid
::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3038 wxPoint
pos( event
.GetPosition() );
3039 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3041 wxGridCellCoords coords
;
3042 XYToCell( x
, y
, coords
);
3044 if ( event
.Dragging() )
3046 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3048 // Don't start doing anything until the mouse has been drug at
3049 // least 3 pixels in any direction...
3052 if (m_startDragPos
== wxDefaultPosition
)
3054 m_startDragPos
= pos
;
3057 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3061 m_isDragging
= TRUE
;
3062 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3064 // Hide the edit control, so it
3065 // won't interfer with drag-shrinking.
3066 if ( IsCellEditControlEnabled() )
3067 HideCellEditControl();
3069 // Have we captured the mouse yet?
3072 m_winCapture
= m_gridWin
;
3073 m_winCapture
->CaptureMouse();
3076 if ( coords
!= wxGridNoCellCoords
)
3078 if ( !IsSelection() )
3080 SelectBlock( coords
, coords
);
3084 SelectBlock( m_currentCellCoords
, coords
);
3087 if (! IsVisible(coords
))
3089 MakeCellVisible(coords
);
3090 // TODO: need to introduce a delay or something here. The
3091 // scrolling is way to fast, at least on MSW.
3095 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3097 int cw
, ch
, left
, dummy
;
3098 m_gridWin
->GetClientSize( &cw
, &ch
);
3099 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3101 wxClientDC
dc( m_gridWin
);
3104 m_rowBottoms
[m_dragRowOrCol
] -
3105 m_rowHeights
[m_dragRowOrCol
] +
3106 WXGRID_MIN_ROW_HEIGHT
);
3107 dc
.SetLogicalFunction(wxINVERT
);
3108 if ( m_dragLastPos
>= 0 )
3110 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3112 dc
.DrawLine( left
, y
, left
+cw
, y
);
3115 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3117 int cw
, ch
, dummy
, top
;
3118 m_gridWin
->GetClientSize( &cw
, &ch
);
3119 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3121 wxClientDC
dc( m_gridWin
);
3124 m_colRights
[m_dragRowOrCol
] -
3125 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
3126 dc
.SetLogicalFunction(wxINVERT
);
3127 if ( m_dragLastPos
>= 0 )
3129 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3131 dc
.DrawLine( x
, top
, x
, top
+ch
);
3138 m_isDragging
= FALSE
;
3139 m_startDragPos
= wxDefaultPosition
;
3142 if ( coords
!= wxGridNoCellCoords
)
3144 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3145 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3148 if ( event
.Entering() || event
.Leaving() )
3150 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3151 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3156 // ------------ Left button pressed
3158 if ( event
.LeftDown() )
3160 DisableCellEditControl();
3161 if ( event
.ShiftDown() )
3163 SelectBlock( m_currentCellCoords
, coords
);
3165 else if ( XToEdgeOfCol(x
) < 0 &&
3166 YToEdgeOfRow(y
) < 0 )
3168 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3173 MakeCellVisible( coords
);
3175 // if this is the second click on this cell then start
3177 if ( m_waitForSlowClick
&&
3178 (coords
== m_currentCellCoords
) &&
3179 CanEnableCellControl())
3181 EnableCellEditControl();
3182 m_waitForSlowClick
= FALSE
;
3186 SetCurrentCell( coords
);
3187 m_waitForSlowClick
= TRUE
;
3194 // ------------ Left double click
3196 else if ( event
.LeftDClick() )
3198 DisableCellEditControl();
3199 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3201 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3209 // ------------ Left button released
3211 else if ( event
.LeftUp() )
3213 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3215 if ( IsSelection() )
3219 m_winCapture
->ReleaseMouse();
3220 m_winCapture
= NULL
;
3222 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3225 // Show the edit control, if it has been hidden for
3227 ShowCellEditControl();
3229 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3231 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3232 DoEndDragResizeRow();
3234 // Note: we are ending the event *after* doing
3235 // default processing in this case
3237 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3239 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3241 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3242 DoEndDragResizeCol();
3244 // Note: we are ending the event *after* doing
3245 // default processing in this case
3247 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3254 // ------------ Right button down
3256 else if ( event
.RightDown() )
3258 DisableCellEditControl();
3259 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3264 // no default action at the moment
3269 // ------------ Right double click
3271 else if ( event
.RightDClick() )
3273 DisableCellEditControl();
3274 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
3279 // no default action at the moment
3283 // ------------ Moving and no button action
3285 else if ( event
.Moving() && !event
.IsButton() )
3287 int dragRow
= YToEdgeOfRow( y
);
3288 int dragCol
= XToEdgeOfCol( x
);
3290 // Dragging on the corner of a cell to resize in both
3291 // directions is not implemented yet...
3293 if ( dragRow
>= 0 && dragCol
>= 0 )
3295 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3301 m_dragRowOrCol
= dragRow
;
3303 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3305 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3313 m_dragRowOrCol
= dragCol
;
3315 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3317 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3323 // Neither on a row or col edge
3325 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3327 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3334 void wxGrid
::DoEndDragResizeRow()
3336 if ( m_dragLastPos
>= 0 )
3338 // erase the last line and resize the row
3340 int cw
, ch
, left
, dummy
;
3341 m_gridWin
->GetClientSize( &cw
, &ch
);
3342 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3344 wxClientDC
dc( m_gridWin
);
3346 dc
.SetLogicalFunction( wxINVERT
);
3347 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3348 HideCellEditControl();
3350 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3351 SetRowSize( m_dragRowOrCol
,
3352 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3354 if ( !GetBatchCount() )
3356 // Only needed to get the correct rect.y:
3357 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3359 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3360 rect
.width
= m_rowLabelWidth
;
3361 rect
.height
= ch
- rect
.y
;
3362 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3364 m_gridWin
->Refresh( FALSE
, &rect
);
3367 ShowCellEditControl();
3372 void wxGrid
::DoEndDragResizeCol()
3374 if ( m_dragLastPos
>= 0 )
3376 // erase the last line and resize the col
3378 int cw
, ch
, dummy
, top
;
3379 m_gridWin
->GetClientSize( &cw
, &ch
);
3380 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3382 wxClientDC
dc( m_gridWin
);
3384 dc
.SetLogicalFunction( wxINVERT
);
3385 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3386 HideCellEditControl();
3388 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3389 SetColSize( m_dragRowOrCol
,
3390 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3392 if ( !GetBatchCount() )
3394 // Only needed to get the correct rect.x:
3395 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3397 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3398 rect
.width
= cw
- rect
.x
;
3399 rect
.height
= m_colLabelHeight
;
3400 m_colLabelWin
->Refresh( TRUE
, &rect
);
3402 m_gridWin
->Refresh( FALSE
, &rect
);
3405 ShowCellEditControl();
3412 // ------ interaction with data model
3414 bool wxGrid
::ProcessTableMessage( wxGridTableMessage
& msg
)
3416 switch ( msg
.GetId() )
3418 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3419 return GetModelValues();
3421 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3422 return SetModelValues();
3424 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3425 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3426 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3427 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3428 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3429 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3430 return Redimension( msg
);
3439 // The behaviour of this function depends on the grid table class
3440 // Clear() function. For the default wxGridStringTable class the
3441 // behavious is to replace all cell contents with wxEmptyString but
3442 // not to change the number of rows or cols.
3444 void wxGrid
::ClearGrid()
3449 SetEditControlValue();
3450 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3455 bool wxGrid
::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3457 // TODO: something with updateLabels flag
3461 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3467 if (IsCellEditControlEnabled())
3468 DisableCellEditControl();
3470 bool ok
= m_table
->InsertRows( pos
, numRows
);
3472 // the table will have sent the results of the insert row
3473 // operation to this view object as a grid table message
3477 if ( m_numCols
== 0 )
3479 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3481 // TODO: perhaps instead of appending the default number of cols
3482 // we should remember what the last non-zero number of cols was ?
3486 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3488 // if we have just inserted cols into an empty grid the current
3489 // cell will be undefined...
3491 SetCurrentCell( 0, 0 );
3495 if ( !GetBatchCount() ) Refresh();
3498 SetEditControlValue();
3508 bool wxGrid
::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3510 // TODO: something with updateLabels flag
3514 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3518 if ( m_table
&& m_table
->AppendRows( numRows
) )
3520 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3522 // if we have just inserted cols into an empty grid the current
3523 // cell will be undefined...
3525 SetCurrentCell( 0, 0 );
3528 // the table will have sent the results of the append row
3529 // operation to this view object as a grid table message
3532 if ( !GetBatchCount() ) Refresh();
3542 bool wxGrid
::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3544 // TODO: something with updateLabels flag
3548 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3554 if (IsCellEditControlEnabled())
3555 DisableCellEditControl();
3557 if (m_table
->DeleteRows( pos
, numRows
))
3560 // the table will have sent the results of the delete row
3561 // operation to this view object as a grid table message
3564 if ( !GetBatchCount() ) Refresh();
3572 bool wxGrid
::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3574 // TODO: something with updateLabels flag
3578 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3584 if (IsCellEditControlEnabled())
3585 DisableCellEditControl();
3587 bool ok
= m_table
->InsertCols( pos
, numCols
);
3589 // the table will have sent the results of the insert col
3590 // operation to this view object as a grid table message
3594 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3596 // if we have just inserted cols into an empty grid the current
3597 // cell will be undefined...
3599 SetCurrentCell( 0, 0 );
3603 if ( !GetBatchCount() ) Refresh();
3606 SetEditControlValue();
3616 bool wxGrid
::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3618 // TODO: something with updateLabels flag
3622 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3626 if ( m_table
&& m_table
->AppendCols( numCols
) )
3628 // the table will have sent the results of the append col
3629 // operation to this view object as a grid table message
3631 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3633 // if we have just inserted cols into an empty grid the current
3634 // cell will be undefined...
3636 SetCurrentCell( 0, 0 );
3640 if ( !GetBatchCount() ) Refresh();
3650 bool wxGrid
::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3652 // TODO: something with updateLabels flag
3656 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3662 if (IsCellEditControlEnabled())
3663 DisableCellEditControl();
3665 if ( m_table
->DeleteCols( pos
, numCols
) )
3667 // the table will have sent the results of the delete col
3668 // operation to this view object as a grid table message
3671 if ( !GetBatchCount() ) Refresh();
3681 // ----- event handlers
3684 // Generate a grid event based on a mouse event and
3685 // return the result of ProcessEvent()
3687 bool wxGrid
::SendEvent( const wxEventType type
,
3689 wxMouseEvent
& mouseEv
)
3691 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3693 int rowOrCol
= (row
== -1 ? col
: row
);
3695 wxGridSizeEvent
gridEvt( GetId(),
3699 mouseEv
.GetX(), mouseEv
.GetY(),
3700 mouseEv
.ControlDown(),
3701 mouseEv
.ShiftDown(),
3703 mouseEv
.MetaDown() );
3705 return GetEventHandler()->ProcessEvent(gridEvt
);
3707 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
3709 wxGridRangeSelectEvent
gridEvt( GetId(),
3713 m_selectedBottomRight
,
3714 mouseEv
.ControlDown(),
3715 mouseEv
.ShiftDown(),
3717 mouseEv
.MetaDown() );
3719 return GetEventHandler()->ProcessEvent(gridEvt
);
3723 wxGridEvent
gridEvt( GetId(),
3727 mouseEv
.GetX(), mouseEv
.GetY(),
3728 mouseEv
.ControlDown(),
3729 mouseEv
.ShiftDown(),
3731 mouseEv
.MetaDown() );
3733 return GetEventHandler()->ProcessEvent(gridEvt
);
3738 // Generate a grid event of specified type and return the result
3739 // of ProcessEvent().
3741 bool wxGrid
::SendEvent( const wxEventType type
,
3744 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
3746 int rowOrCol
= (row
== -1 ? col
: row
);
3748 wxGridSizeEvent
gridEvt( GetId(),
3753 return GetEventHandler()->ProcessEvent(gridEvt
);
3757 wxGridEvent
gridEvt( GetId(),
3762 return GetEventHandler()->ProcessEvent(gridEvt
);
3767 void wxGrid
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3769 wxPaintDC
dc( this );
3771 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3772 m_numRows
&& m_numCols
)
3774 m_currentCellCoords
.Set(0, 0);
3775 SetEditControlValue();
3776 ShowCellEditControl();
3783 // This is just here to make sure that CalcDimensions gets called when
3784 // the grid view is resized... then the size event is skipped to allow
3785 // the box sizers to handle everything
3787 void wxGrid
::OnSize( wxSizeEvent
& event
)
3794 void wxGrid
::OnKeyDown( wxKeyEvent
& event
)
3796 if ( m_inOnKeyDown
)
3798 // shouldn't be here - we are going round in circles...
3800 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3803 m_inOnKeyDown
= TRUE
;
3805 // propagate the event up and see if it gets processed
3807 wxWindow
*parent
= GetParent();
3808 wxKeyEvent
keyEvt( event
);
3809 keyEvt
.SetEventObject( parent
);
3811 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3814 // TODO: Should also support Shift-cursor keys for
3815 // extending the selection. Maybe add a flag to
3816 // MoveCursorXXX() and MoveCursorXXXBlock() and
3817 // just send event.ShiftDown().
3819 // try local handlers
3821 switch ( event
.KeyCode() )
3824 if ( event
.ControlDown() )
3826 MoveCursorUpBlock();
3835 if ( event
.ControlDown() )
3837 MoveCursorDownBlock();
3846 if ( event
.ControlDown() )
3848 MoveCursorLeftBlock();
3857 if ( event
.ControlDown() )
3859 MoveCursorRightBlock();
3868 if ( event
.ControlDown() )
3870 event
.Skip(); // to let the edit control have the return
3879 if (event
.ShiftDown())
3886 if ( event
.ControlDown() )
3888 MakeCellVisible( 0, 0 );
3889 SetCurrentCell( 0, 0 );
3898 if ( event
.ControlDown() )
3900 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3901 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3917 // We don't want these keys to trigger the edit control, any others?
3926 if ( !IsEditable() )
3931 // Otherwise fall through to default
3934 // now try the cell edit control
3936 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
3938 EnableCellEditControl();
3939 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3940 attr
->GetEditor()->StartingKey(event
);
3947 m_inOnKeyDown
= FALSE
;
3951 void wxGrid
::OnEraseBackground(wxEraseEvent
&)
3955 void wxGrid
::SetCurrentCell( const wxGridCellCoords
& coords
)
3957 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3959 // the event has been intercepted - do nothing
3964 m_currentCellCoords
!= wxGridNoCellCoords
)
3966 HideCellEditControl();
3967 SaveEditControlValue();
3968 DisableCellEditControl();
3970 // Clear the old current cell highlight
3971 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3973 // Otherwise refresh redraws the highlight!
3974 m_currentCellCoords
= coords
;
3976 m_gridWin
->Refresh( FALSE
, &r
);
3979 m_currentCellCoords
= coords
;
3981 SetEditControlValue();
3985 wxClientDC
dc(m_gridWin
);
3988 wxGridCellAttr
* attr
= GetCellAttr(coords
);
3989 DrawCellHighlight(dc
, attr
);
3992 if ( IsSelection() )
3994 wxRect
r( SelectionToDeviceRect() );
3996 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4003 // ------ functions to get/send data (see also public functions)
4006 bool wxGrid
::GetModelValues()
4010 // all we need to do is repaint the grid
4012 m_gridWin
->Refresh();
4020 bool wxGrid
::SetModelValues()
4026 for ( row
= 0; row
< m_numRows
; row
++ )
4028 for ( col
= 0; col
< m_numCols
; col
++ )
4030 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4042 // Note - this function only draws cells that are in the list of
4043 // exposed cells (usually set from the update region by
4044 // CalcExposedCells)
4046 void wxGrid
::DrawGridCellArea( wxDC
& dc
)
4048 if ( !m_numRows
|| !m_numCols
) return;
4051 size_t numCells
= m_cellsExposed
.GetCount();
4053 for ( i
= 0; i
< numCells
; i
++ )
4055 DrawCell( dc
, m_cellsExposed
[i
] );
4060 void wxGrid
::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4062 int row
= coords
.GetRow();
4063 int col
= coords
.GetCol();
4065 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4068 // we draw the cell border ourselves
4069 #if !WXGRID_DRAW_LINES
4070 if ( m_gridLinesEnabled
)
4071 DrawCellBorder( dc
, coords
);
4074 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4076 bool isCurrent
= coords
== m_currentCellCoords
;
4079 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4080 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4081 rect
.width
= m_colWidths
[col
] - 1;
4082 rect
.height
= m_rowHeights
[row
] - 1;
4084 // if the editor is shown, we should use it and not the renderer
4085 if ( isCurrent
&& IsCellEditControlEnabled() )
4087 attr
->GetEditor()->PaintBackground(rect
, attr
);
4091 // but all the rest is drawn by the cell renderer and hence may be
4093 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4097 DrawCellHighlight(dc
, attr
);
4104 void wxGrid
::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4106 int row
= m_currentCellCoords
.GetRow();
4107 int col
= m_currentCellCoords
.GetCol();
4109 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
4113 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4114 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4115 rect
.width
= m_colWidths
[col
] - 1;
4116 rect
.height
= m_rowHeights
[row
] - 1;
4118 if ( attr
->IsReadOnly() )
4120 // hmmm... what could we do here to show that the cell is disabled?
4121 // for now, I just draw a thinner border than for the other ones, but
4122 // it doesn't look really good
4123 dc
.SetPen(wxPen(m_gridLineColour
, 2, wxSOLID
));
4124 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4126 dc
.DrawRectangle(rect
);
4130 // VZ: my experiments with 3d borders...
4132 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
4133 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4135 dc
.DrawRectangle(rect
);
4137 // FIXME we should properly set colours for arbitrary bg
4138 wxCoord x1
= rect
.x
,
4140 x2
= rect
.x
+ rect
.width
,
4141 y2
= rect
.y
+ rect
.height
;
4143 dc
.SetPen(*wxWHITE_PEN
);
4144 dc
.DrawLine(x1
, y1
, x2
- 1, y1
);
4145 dc
.DrawLine(x1
, y1
, x1
, y2
- 1);
4147 dc
.SetPen(*wxLIGHT_GREY_PEN
);
4148 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4149 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
- 1);
4151 dc
.SetPen(*wxBLACK_PEN
);
4152 dc
.DrawLine(x1
, y2
, x2
, y2
);
4153 dc
.DrawLine(x2
, y1
, x2
, y2
);
4158 void wxGrid
::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4160 if ( m_colWidths
[coords
.GetCol()] <=0 ||
4161 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
4163 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4164 int row
= coords
.GetRow();
4165 int col
= coords
.GetCol();
4167 // right hand border
4169 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
4170 m_colRights
[col
], m_rowBottoms
[row
] );
4174 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
4175 m_colRights
[col
], m_rowBottoms
[row
] );
4179 // TODO: remove this ???
4180 // This is used to redraw all grid lines e.g. when the grid line colour
4183 void wxGrid
::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4185 if ( !m_gridLinesEnabled
||
4187 !m_numCols
) return;
4189 int top
, bottom
, left
, right
;
4194 m_gridWin
->GetClientSize(&cw
, &ch
);
4196 // virtual coords of visible area
4198 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4199 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4204 reg
.GetBox(x
, y
, w
, h
);
4205 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4206 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4209 // avoid drawing grid lines past the last row and col
4211 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
4212 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
4214 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4216 // horizontal grid lines
4219 for ( i
= 0; i
< m_numRows
; i
++ )
4221 if ( m_rowBottoms
[i
]-1 > bottom
)
4225 else if ( m_rowBottoms
[i
]-1 >= top
)
4227 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
4232 // vertical grid lines
4234 for ( i
= 0; i
< m_numCols
; i
++ )
4236 if ( m_colRights
[i
]-1 > right
)
4240 else if ( m_colRights
[i
]-1 >= left
)
4242 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
4248 void wxGrid
::DrawRowLabels( wxDC
& dc
)
4250 if ( !m_numRows
|| !m_numCols
) return;
4253 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4255 for ( i
= 0; i
< numLabels
; i
++ )
4257 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4262 void wxGrid
::DrawRowLabel( wxDC
& dc
, int row
)
4264 if ( m_rowHeights
[row
] <= 0 ) return;
4266 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4268 dc
.SetPen( *wxBLACK_PEN
);
4269 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4270 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4272 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4273 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4275 dc
.SetPen( *wxWHITE_PEN
);
4276 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4277 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4279 dc
.SetBackgroundMode( wxTRANSPARENT
);
4280 dc
.SetTextForeground( GetLabelTextColour() );
4281 dc
.SetFont( GetLabelFont() );
4284 GetRowLabelAlignment( &hAlign
, &vAlign
);
4288 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4289 rect
.SetWidth( m_rowLabelWidth
- 4 );
4290 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4291 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4295 void wxGrid
::DrawColLabels( wxDC
& dc
)
4297 if ( !m_numRows
|| !m_numCols
) return;
4300 size_t numLabels
= m_colLabelsExposed
.GetCount();
4302 for ( i
= 0; i
< numLabels
; i
++ )
4304 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4309 void wxGrid
::DrawColLabel( wxDC
& dc
, int col
)
4311 if ( m_colWidths
[col
] <= 0 ) return;
4313 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4315 dc
.SetPen( *wxBLACK_PEN
);
4316 dc
.DrawLine( m_colRights
[col
]-1, 0,
4317 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4319 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4320 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4322 dc
.SetPen( *wxWHITE_PEN
);
4323 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4324 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4326 dc
.SetBackgroundMode( wxTRANSPARENT
);
4327 dc
.SetTextForeground( GetLabelTextColour() );
4328 dc
.SetFont( GetLabelFont() );
4330 dc
.SetBackgroundMode( wxTRANSPARENT
);
4331 dc
.SetTextForeground( GetLabelTextColour() );
4332 dc
.SetFont( GetLabelFont() );
4335 GetColLabelAlignment( &hAlign
, &vAlign
);
4338 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4340 rect
.SetWidth( m_colWidths
[col
] - 4 );
4341 rect
.SetHeight( m_colLabelHeight
- 4 );
4342 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4346 void wxGrid
::DrawTextRectangle( wxDC
& dc
,
4347 const wxString
& value
,
4352 long textWidth
, textHeight
;
4353 long lineWidth
, lineHeight
;
4354 wxArrayString lines
;
4356 dc
.SetClippingRegion( rect
);
4357 StringToLines( value
, lines
);
4358 if ( lines
.GetCount() )
4360 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4361 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4364 switch ( horizAlign
)
4367 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4371 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4380 switch ( vertAlign
)
4383 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4387 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4396 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4398 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4403 dc
.DestroyClippingRegion();
4407 // Split multi line text up into an array of strings. Any existing
4408 // contents of the string array are preserved.
4410 void wxGrid
::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4414 wxString eol
= wxTextFile
::GetEOL( wxTextFileType_Unix
);
4415 wxString tVal
= wxTextFile
::Translate( value
, wxTextFileType_Unix
);
4417 while ( startPos
< (int)tVal
.Length() )
4419 pos
= tVal
.Mid(startPos
).Find( eol
);
4424 else if ( pos
== 0 )
4426 lines
.Add( wxEmptyString
);
4430 lines
.Add( value
.Mid(startPos
, pos
) );
4434 if ( startPos
< (int)value
.Length() )
4436 lines
.Add( value
.Mid( startPos
) );
4441 void wxGrid
::GetTextBoxSize( wxDC
& dc
,
4442 wxArrayString
& lines
,
4443 long *width
, long *height
)
4450 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4452 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4453 w
= wxMax( w
, lineW
);
4463 // ------ Edit control functions
4467 void wxGrid
::EnableEditing( bool edit
)
4469 // TODO: improve this ?
4471 if ( edit
!= m_editable
)
4475 // FIXME IMHO this won't disable the edit control if edit == FALSE
4476 // because of the check in the beginning of
4477 // EnableCellEditControl() just below (VZ)
4478 EnableCellEditControl(m_editable
);
4483 void wxGrid
::EnableCellEditControl( bool enable
)
4488 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4489 SetCurrentCell( 0, 0 );
4491 if ( enable
!= m_cellEditCtrlEnabled
)
4493 // TODO allow the app to Veto() this event?
4494 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
4498 // this should be checked by the caller!
4499 wxASSERT_MSG( CanEnableCellControl(),
4500 _T("can't enable editing for this cell!") );
4502 // do it before ShowCellEditControl()
4503 m_cellEditCtrlEnabled
= enable
;
4505 SetEditControlValue();
4506 ShowCellEditControl();
4510 HideCellEditControl();
4511 SaveEditControlValue();
4513 // do it after HideCellEditControl()
4514 m_cellEditCtrlEnabled
= enable
;
4519 bool wxGrid
::IsCurrentCellReadOnly() const
4522 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
4523 bool readonly
= attr
->IsReadOnly();
4529 bool wxGrid
::CanEnableCellControl() const
4531 return m_editable
&& !IsCurrentCellReadOnly();
4534 bool wxGrid
::IsCellEditControlEnabled() const
4536 // the cell edit control might be disable for all cells or just for the
4537 // current one if it's read only
4538 return m_cellEditCtrlEnabled ?
!IsCurrentCellReadOnly() : FALSE
;
4541 wxWindow
*wxGrid
::GetGridWindow() const
4546 void wxGrid
::ShowCellEditControl()
4548 if ( IsCellEditControlEnabled() )
4550 if ( !IsVisible( m_currentCellCoords
) )
4556 wxRect rect
= CellToRect( m_currentCellCoords
);
4557 int row
= m_currentCellCoords
.GetRow();
4558 int col
= m_currentCellCoords
.GetCol();
4560 // convert to scrolled coords
4562 int left
, top
, right
, bottom
;
4563 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4564 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4566 // cell is shifted by one pixel
4572 // Make the edit control large enough to allow for internal
4575 // TODO: remove this if the text ctrl sizing is improved esp. for
4579 #if defined(__WXMOTIF__)
4580 if ( row
== 0 || col
== 0 )
4589 if ( row
== 0 || col
== 0 )
4599 #if defined(__WXGTK__)
4602 if (left
!= 0) left_diff
++;
4603 if (top
!= 0) top_diff
++;
4604 rect
.SetLeft( left
+ left_diff
);
4605 rect
.SetTop( top
+ top_diff
);
4606 rect
.SetRight( rect
.GetRight() - left_diff
);
4607 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4609 rect
.SetLeft( wxMax(0, left
- extra
) );
4610 rect
.SetTop( wxMax(0, top
- extra
) );
4611 rect
.SetRight( rect
.GetRight() + 2*extra
);
4612 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4615 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4616 wxGridCellEditor
* editor
= attr
->GetEditor();
4617 if ( !editor
->IsCreated() )
4619 editor
->Create(m_gridWin
, -1,
4620 new wxGridCellEditorEvtHandler(this, editor
));
4623 editor
->SetSize( rect
);
4624 editor
->Show( TRUE
, attr
);
4625 editor
->BeginEdit(row
, col
, this);
4632 void wxGrid
::HideCellEditControl()
4634 if ( IsCellEditControlEnabled() )
4636 int row
= m_currentCellCoords
.GetRow();
4637 int col
= m_currentCellCoords
.GetCol();
4639 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4640 attr
->GetEditor()->Show( FALSE
);
4642 m_gridWin
->SetFocus();
4647 void wxGrid
::SetEditControlValue( const wxString
& value
)
4649 // RD: The new Editors get the value from the table themselves now. This
4650 // method can probably be removed...
4654 void wxGrid
::SaveEditControlValue()
4656 if ( IsCellEditControlEnabled() )
4658 int row
= m_currentCellCoords
.GetRow();
4659 int col
= m_currentCellCoords
.GetCol();
4661 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4662 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4668 SendEvent( wxEVT_GRID_CELL_CHANGE
,
4669 m_currentCellCoords
.GetRow(),
4670 m_currentCellCoords
.GetCol() );
4677 // ------ Grid location functions
4678 // Note that all of these functions work with the logical coordinates of
4679 // grid cells and labels so you will need to convert from device
4680 // coordinates for mouse events etc.
4683 void wxGrid
::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4685 int row
= YToRow(y
);
4686 int col
= XToCol(x
);
4688 if ( row
== -1 || col
== -1 )
4690 coords
= wxGridNoCellCoords
;
4694 coords
.Set( row
, col
);
4699 int wxGrid
::YToRow( int y
)
4703 for ( i
= 0; i
< m_numRows
; i
++ )
4705 if ( y
< m_rowBottoms
[i
] ) return i
;
4708 return m_numRows
; //-1;
4712 int wxGrid
::XToCol( int x
)
4716 for ( i
= 0; i
< m_numCols
; i
++ )
4718 if ( x
< m_colRights
[i
] ) return i
;
4721 return m_numCols
; //-1;
4725 // return the row number that that the y coord is near the edge of, or
4726 // -1 if not near an edge
4728 int wxGrid
::YToEdgeOfRow( int y
)
4732 for ( i
= 0; i
< m_numRows
; i
++ )
4734 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4736 d
= abs( y
- m_rowBottoms
[i
] );
4738 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4747 // return the col number that that the x coord is near the edge of, or
4748 // -1 if not near an edge
4750 int wxGrid
::XToEdgeOfCol( int x
)
4754 for ( i
= 0; i
< m_numCols
; i
++ )
4756 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4758 d
= abs( x
- m_colRights
[i
] );
4760 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4769 wxRect wxGrid
::CellToRect( int row
, int col
)
4771 wxRect
rect( -1, -1, -1, -1 );
4773 if ( row
>= 0 && row
< m_numRows
&&
4774 col
>= 0 && col
< m_numCols
)
4776 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4777 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4778 rect
.width
= m_colWidths
[col
];
4779 rect
.height
= m_rowHeights
[ row
];
4786 bool wxGrid
::IsVisible( int row
, int col
, bool wholeCellVisible
)
4788 // get the cell rectangle in logical coords
4790 wxRect
r( CellToRect( row
, col
) );
4792 // convert to device coords
4794 int left
, top
, right
, bottom
;
4795 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4796 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4798 // check against the client area of the grid window
4801 m_gridWin
->GetClientSize( &cw
, &ch
);
4803 if ( wholeCellVisible
)
4805 // is the cell wholly visible ?
4807 return ( left
>= 0 && right
<= cw
&&
4808 top
>= 0 && bottom
<= ch
);
4812 // is the cell partly visible ?
4814 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4815 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4820 // make the specified cell location visible by doing a minimal amount
4823 void wxGrid
::MakeCellVisible( int row
, int col
)
4826 int xpos
= -1, ypos
= -1;
4828 if ( row
>= 0 && row
< m_numRows
&&
4829 col
>= 0 && col
< m_numCols
)
4831 // get the cell rectangle in logical coords
4833 wxRect
r( CellToRect( row
, col
) );
4835 // convert to device coords
4837 int left
, top
, right
, bottom
;
4838 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4839 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4842 m_gridWin
->GetClientSize( &cw
, &ch
);
4848 else if ( bottom
> ch
)
4850 int h
= r
.GetHeight();
4852 for ( i
= row
-1; i
>= 0; i
-- )
4854 if ( h
+ m_rowHeights
[i
] > ch
) break;
4856 h
+= m_rowHeights
[i
];
4857 ypos
-= m_rowHeights
[i
];
4860 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4861 // have rounding errors (this is important, because if we do, we
4862 // might not scroll at all and some cells won't be redrawn)
4863 ypos
+= GRID_SCROLL_LINE
/ 2;
4870 else if ( right
> cw
)
4872 int w
= r
.GetWidth();
4874 for ( i
= col
-1; i
>= 0; i
-- )
4876 if ( w
+ m_colWidths
[i
] > cw
) break;
4878 w
+= m_colWidths
[i
];
4879 xpos
-= m_colWidths
[i
];
4882 // see comment for ypos above
4883 xpos
+= GRID_SCROLL_LINE
/ 2;
4886 if ( xpos
!= -1 || ypos
!= -1 )
4888 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4889 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4890 Scroll( xpos
, ypos
);
4898 // ------ Grid cursor movement functions
4901 bool wxGrid
::MoveCursorUp()
4903 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4904 m_currentCellCoords
.GetRow() > 0 )
4906 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4907 m_currentCellCoords
.GetCol() );
4909 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4910 m_currentCellCoords
.GetCol() );
4919 bool wxGrid
::MoveCursorDown()
4921 // TODO: allow for scrolling
4923 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4924 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4926 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4927 m_currentCellCoords
.GetCol() );
4929 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4930 m_currentCellCoords
.GetCol() );
4939 bool wxGrid
::MoveCursorLeft()
4941 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4942 m_currentCellCoords
.GetCol() > 0 )
4944 MakeCellVisible( m_currentCellCoords
.GetRow(),
4945 m_currentCellCoords
.GetCol() - 1 );
4947 SetCurrentCell( m_currentCellCoords
.GetRow(),
4948 m_currentCellCoords
.GetCol() - 1 );
4957 bool wxGrid
::MoveCursorRight()
4959 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4960 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4962 MakeCellVisible( m_currentCellCoords
.GetRow(),
4963 m_currentCellCoords
.GetCol() + 1 );
4965 SetCurrentCell( m_currentCellCoords
.GetRow(),
4966 m_currentCellCoords
.GetCol() + 1 );
4975 bool wxGrid
::MovePageUp()
4977 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4979 int row
= m_currentCellCoords
.GetRow();
4983 m_gridWin
->GetClientSize( &cw
, &ch
);
4985 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4986 int newRow
= YToRow( y
- ch
+ 1 );
4991 else if ( newRow
== row
)
4996 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4997 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5005 bool wxGrid
::MovePageDown()
5007 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5009 int row
= m_currentCellCoords
.GetRow();
5010 if ( row
< m_numRows
)
5013 m_gridWin
->GetClientSize( &cw
, &ch
);
5015 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
5016 int newRow
= YToRow( y
+ ch
);
5019 newRow
= m_numRows
- 1;
5021 else if ( newRow
== row
)
5026 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5027 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5035 bool wxGrid
::MoveCursorUpBlock()
5038 m_currentCellCoords
!= wxGridNoCellCoords
&&
5039 m_currentCellCoords
.GetRow() > 0 )
5041 int row
= m_currentCellCoords
.GetRow();
5042 int col
= m_currentCellCoords
.GetCol();
5044 if ( m_table
->IsEmptyCell(row
, col
) )
5046 // starting in an empty cell: find the next block of
5052 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5055 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5057 // starting at the top of a block: find the next block
5063 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5068 // starting within a block: find the top of the block
5073 if ( m_table
->IsEmptyCell(row
, col
) )
5081 MakeCellVisible( row
, col
);
5082 SetCurrentCell( row
, col
);
5090 bool wxGrid
::MoveCursorDownBlock()
5093 m_currentCellCoords
!= wxGridNoCellCoords
&&
5094 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5096 int row
= m_currentCellCoords
.GetRow();
5097 int col
= m_currentCellCoords
.GetCol();
5099 if ( m_table
->IsEmptyCell(row
, col
) )
5101 // starting in an empty cell: find the next block of
5104 while ( row
< m_numRows
-1 )
5107 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5110 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5112 // starting at the bottom of a block: find the next block
5115 while ( row
< m_numRows
-1 )
5118 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5123 // starting within a block: find the bottom of the block
5125 while ( row
< m_numRows
-1 )
5128 if ( m_table
->IsEmptyCell(row
, col
) )
5136 MakeCellVisible( row
, col
);
5137 SetCurrentCell( row
, col
);
5145 bool wxGrid
::MoveCursorLeftBlock()
5148 m_currentCellCoords
!= wxGridNoCellCoords
&&
5149 m_currentCellCoords
.GetCol() > 0 )
5151 int row
= m_currentCellCoords
.GetRow();
5152 int col
= m_currentCellCoords
.GetCol();
5154 if ( m_table
->IsEmptyCell(row
, col
) )
5156 // starting in an empty cell: find the next block of
5162 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5165 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5167 // starting at the left of a block: find the next block
5173 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5178 // starting within a block: find the left of the block
5183 if ( m_table
->IsEmptyCell(row
, col
) )
5191 MakeCellVisible( row
, col
);
5192 SetCurrentCell( row
, col
);
5200 bool wxGrid
::MoveCursorRightBlock()
5203 m_currentCellCoords
!= wxGridNoCellCoords
&&
5204 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5206 int row
= m_currentCellCoords
.GetRow();
5207 int col
= m_currentCellCoords
.GetCol();
5209 if ( m_table
->IsEmptyCell(row
, col
) )
5211 // starting in an empty cell: find the next block of
5214 while ( col
< m_numCols
-1 )
5217 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5220 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5222 // starting at the right of a block: find the next block
5225 while ( col
< m_numCols
-1 )
5228 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5233 // starting within a block: find the right of the block
5235 while ( col
< m_numCols
-1 )
5238 if ( m_table
->IsEmptyCell(row
, col
) )
5246 MakeCellVisible( row
, col
);
5247 SetCurrentCell( row
, col
);
5258 // ------ Label values and formatting
5261 void wxGrid
::GetRowLabelAlignment( int *horiz
, int *vert
)
5263 *horiz
= m_rowLabelHorizAlign
;
5264 *vert
= m_rowLabelVertAlign
;
5267 void wxGrid
::GetColLabelAlignment( int *horiz
, int *vert
)
5269 *horiz
= m_colLabelHorizAlign
;
5270 *vert
= m_colLabelVertAlign
;
5273 wxString wxGrid
::GetRowLabelValue( int row
)
5277 return m_table
->GetRowLabelValue( row
);
5287 wxString wxGrid
::GetColLabelValue( int col
)
5291 return m_table
->GetColLabelValue( col
);
5302 void wxGrid
::SetRowLabelSize( int width
)
5304 width
= wxMax( width
, 0 );
5305 if ( width
!= m_rowLabelWidth
)
5309 m_rowLabelWin
->Show( FALSE
);
5310 m_cornerLabelWin
->Show( FALSE
);
5312 else if ( m_rowLabelWidth
== 0 )
5314 m_rowLabelWin
->Show( TRUE
);
5315 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5318 m_rowLabelWidth
= width
;
5325 void wxGrid
::SetColLabelSize( int height
)
5327 height
= wxMax( height
, 0 );
5328 if ( height
!= m_colLabelHeight
)
5332 m_colLabelWin
->Show( FALSE
);
5333 m_cornerLabelWin
->Show( FALSE
);
5335 else if ( m_colLabelHeight
== 0 )
5337 m_colLabelWin
->Show( TRUE
);
5338 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5341 m_colLabelHeight
= height
;
5348 void wxGrid
::SetLabelBackgroundColour( const wxColour
& colour
)
5350 if ( m_labelBackgroundColour
!= colour
)
5352 m_labelBackgroundColour
= colour
;
5353 m_rowLabelWin
->SetBackgroundColour( colour
);
5354 m_colLabelWin
->SetBackgroundColour( colour
);
5355 m_cornerLabelWin
->SetBackgroundColour( colour
);
5357 if ( !GetBatchCount() )
5359 m_rowLabelWin
->Refresh();
5360 m_colLabelWin
->Refresh();
5361 m_cornerLabelWin
->Refresh();
5366 void wxGrid
::SetLabelTextColour( const wxColour
& colour
)
5368 if ( m_labelTextColour
!= colour
)
5370 m_labelTextColour
= colour
;
5371 if ( !GetBatchCount() )
5373 m_rowLabelWin
->Refresh();
5374 m_colLabelWin
->Refresh();
5379 void wxGrid
::SetLabelFont( const wxFont
& font
)
5382 if ( !GetBatchCount() )
5384 m_rowLabelWin
->Refresh();
5385 m_colLabelWin
->Refresh();
5389 void wxGrid
::SetRowLabelAlignment( int horiz
, int vert
)
5391 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5393 m_rowLabelHorizAlign
= horiz
;
5396 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5398 m_rowLabelVertAlign
= vert
;
5401 if ( !GetBatchCount() )
5403 m_rowLabelWin
->Refresh();
5407 void wxGrid
::SetColLabelAlignment( int horiz
, int vert
)
5409 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5411 m_colLabelHorizAlign
= horiz
;
5414 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5416 m_colLabelVertAlign
= vert
;
5419 if ( !GetBatchCount() )
5421 m_colLabelWin
->Refresh();
5425 void wxGrid
::SetRowLabelValue( int row
, const wxString
& s
)
5429 m_table
->SetRowLabelValue( row
, s
);
5430 if ( !GetBatchCount() )
5432 wxRect rect
= CellToRect( row
, 0);
5433 if ( rect
.height
> 0 )
5435 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5437 rect
.width
= m_rowLabelWidth
;
5438 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5444 void wxGrid
::SetColLabelValue( int col
, const wxString
& s
)
5448 m_table
->SetColLabelValue( col
, s
);
5449 if ( !GetBatchCount() )
5451 wxRect rect
= CellToRect( 0, col
);
5452 if ( rect
.width
> 0 )
5454 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5456 rect
.height
= m_colLabelHeight
;
5457 m_colLabelWin
->Refresh( TRUE
, &rect
);
5463 void wxGrid
::SetGridLineColour( const wxColour
& colour
)
5465 if ( m_gridLineColour
!= colour
)
5467 m_gridLineColour
= colour
;
5469 wxClientDC
dc( m_gridWin
);
5471 DrawAllGridLines( dc
, wxRegion() );
5475 void wxGrid
::EnableGridLines( bool enable
)
5477 if ( enable
!= m_gridLinesEnabled
)
5479 m_gridLinesEnabled
= enable
;
5481 if ( !GetBatchCount() )
5485 wxClientDC
dc( m_gridWin
);
5487 DrawAllGridLines( dc
, wxRegion() );
5491 m_gridWin
->Refresh();
5498 int wxGrid
::GetDefaultRowSize()
5500 return m_defaultRowHeight
;
5503 int wxGrid
::GetRowSize( int row
)
5505 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5507 return m_rowHeights
[row
];
5510 int wxGrid
::GetDefaultColSize()
5512 return m_defaultColWidth
;
5515 int wxGrid
::GetColSize( int col
)
5517 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5519 return m_colWidths
[col
];
5522 // ============================================================================
5523 // access to the grid attributes: each of them has a default value in the grid
5524 // itself and may be overidden on a per-cell basis
5525 // ============================================================================
5527 // ----------------------------------------------------------------------------
5528 // setting default attributes
5529 // ----------------------------------------------------------------------------
5531 void wxGrid
::SetDefaultCellBackgroundColour( const wxColour
& col
)
5533 m_defaultCellAttr
->SetBackgroundColour(col
);
5536 void wxGrid
::SetDefaultCellTextColour( const wxColour
& col
)
5538 m_defaultCellAttr
->SetTextColour(col
);
5541 void wxGrid
::SetDefaultCellAlignment( int horiz
, int vert
)
5543 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5546 void wxGrid
::SetDefaultCellFont( const wxFont
& font
)
5548 m_defaultCellAttr
->SetFont(font
);
5551 void wxGrid
::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5553 m_defaultCellAttr
->SetRenderer(renderer
);
5556 void wxGrid
::SetDefaultEditor(wxGridCellEditor
*editor
)
5558 m_defaultCellAttr
->SetEditor(editor
);
5561 // ----------------------------------------------------------------------------
5562 // access to the default attrbiutes
5563 // ----------------------------------------------------------------------------
5565 wxColour wxGrid
::GetDefaultCellBackgroundColour()
5567 return m_defaultCellAttr
->GetBackgroundColour();
5570 wxColour wxGrid
::GetDefaultCellTextColour()
5572 return m_defaultCellAttr
->GetTextColour();
5575 wxFont wxGrid
::GetDefaultCellFont()
5577 return m_defaultCellAttr
->GetFont();
5580 void wxGrid
::GetDefaultCellAlignment( int *horiz
, int *vert
)
5582 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5585 wxGridCellRenderer
*wxGrid
::GetDefaultRenderer() const
5587 return m_defaultCellAttr
->GetRenderer();
5590 wxGridCellEditor
*wxGrid
::GetDefaultEditor() const
5592 return m_defaultCellAttr
->GetEditor();
5595 // ----------------------------------------------------------------------------
5596 // access to cell attributes
5597 // ----------------------------------------------------------------------------
5599 wxColour wxGrid
::GetCellBackgroundColour(int row
, int col
)
5601 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5602 wxColour colour
= attr
->GetBackgroundColour();
5607 wxColour wxGrid
::GetCellTextColour( int row
, int col
)
5609 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5610 wxColour colour
= attr
->GetTextColour();
5615 wxFont wxGrid
::GetCellFont( int row
, int col
)
5617 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5618 wxFont font
= attr
->GetFont();
5623 void wxGrid
::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5625 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5626 attr
->GetAlignment(horiz
, vert
);
5630 wxGridCellRenderer
* wxGrid
::GetCellRenderer(int row
, int col
)
5632 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5633 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5638 wxGridCellEditor
* wxGrid
::GetCellEditor(int row
, int col
)
5640 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5641 wxGridCellEditor
* editor
= attr
->GetEditor();
5646 bool wxGrid
::IsReadOnly(int row
, int col
) const
5648 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5649 bool isReadOnly
= attr
->IsReadOnly();
5654 // ----------------------------------------------------------------------------
5655 // attribute support: cache, automatic provider creation, ...
5656 // ----------------------------------------------------------------------------
5658 bool wxGrid
::CanHaveAttributes()
5665 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5666 // table is providing the attributes itself??? In which case
5667 // I don't think the grid should create a Provider object for the
5668 // table but the table should be smart enough to do that on its own.
5669 if ( !m_table
->GetAttrProvider() )
5671 // use the default attr provider by default
5672 // (another choice would be to just return FALSE thus forcing the user
5674 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5680 void wxGrid
::ClearAttrCache()
5682 if ( m_attrCache
.row
!= -1 )
5684 m_attrCache
.attr
->SafeDecRef();
5685 m_attrCache
.row
= -1;
5689 void wxGrid
::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5691 wxGrid
*self
= (wxGrid
*)this; // const_cast
5693 self
->ClearAttrCache();
5694 self
->m_attrCache
.row
= row
;
5695 self
->m_attrCache
.col
= col
;
5696 self
->m_attrCache
.attr
= attr
;
5700 bool wxGrid
::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5702 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5704 *attr
= m_attrCache
.attr
;
5705 (*attr
)->SafeIncRef();
5707 #ifdef DEBUG_ATTR_CACHE
5708 gs_nAttrCacheHits
++;
5715 #ifdef DEBUG_ATTR_CACHE
5716 gs_nAttrCacheMisses
++;
5722 wxGridCellAttr
*wxGrid
::GetCellAttr(int row
, int col
) const
5724 wxGridCellAttr
*attr
;
5725 if ( !LookupAttr(row
, col
, &attr
) )
5727 attr
= m_table ? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5728 CacheAttr(row
, col
, attr
);
5732 attr
->SetDefAttr(m_defaultCellAttr
);
5736 attr
= m_defaultCellAttr
;
5743 wxGridCellAttr
*wxGrid
::GetOrCreateCellAttr(int row
, int col
) const
5745 wxGridCellAttr
*attr
;
5746 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5748 wxASSERT_MSG( m_table
,
5749 _T("we may only be called if CanHaveAttributes() "
5750 "returned TRUE and then m_table should be !NULL") );
5752 attr
= m_table
->GetAttr(row
, col
);
5755 attr
= new wxGridCellAttr
;
5757 // artificially inc the ref count to match DecRef() in caller
5760 m_table
->SetAttr(attr
, row
, col
);
5763 CacheAttr(row
, col
, attr
);
5765 attr
->SetDefAttr(m_defaultCellAttr
);
5769 // ----------------------------------------------------------------------------
5770 // setting cell attributes: this is forwarded to the table
5771 // ----------------------------------------------------------------------------
5773 void wxGrid
::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5775 if ( CanHaveAttributes() )
5777 m_table
->SetRowAttr(attr
, row
);
5785 void wxGrid
::SetColAttr(int col
, wxGridCellAttr
*attr
)
5787 if ( CanHaveAttributes() )
5789 m_table
->SetColAttr(attr
, col
);
5797 void wxGrid
::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5799 if ( CanHaveAttributes() )
5801 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5802 attr
->SetBackgroundColour(colour
);
5807 void wxGrid
::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5809 if ( CanHaveAttributes() )
5811 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5812 attr
->SetTextColour(colour
);
5817 void wxGrid
::SetCellFont( int row
, int col
, const wxFont
& font
)
5819 if ( CanHaveAttributes() )
5821 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5822 attr
->SetFont(font
);
5827 void wxGrid
::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5829 if ( CanHaveAttributes() )
5831 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5832 attr
->SetAlignment(horiz
, vert
);
5837 void wxGrid
::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5839 if ( CanHaveAttributes() )
5841 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5842 attr
->SetRenderer(renderer
);
5847 void wxGrid
::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5849 if ( CanHaveAttributes() )
5851 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5852 attr
->SetEditor(editor
);
5857 void wxGrid
::SetReadOnly(int row
, int col
, bool isReadOnly
)
5859 if ( CanHaveAttributes() )
5861 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5862 attr
->SetReadOnly(isReadOnly
);
5867 // ----------------------------------------------------------------------------
5869 // ----------------------------------------------------------------------------
5871 void wxGrid
::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5873 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5875 if ( resizeExistingRows
)
5879 for ( row
= 0; row
< m_numRows
; row
++ )
5881 m_rowHeights
[row
] = m_defaultRowHeight
;
5882 bottom
+= m_defaultRowHeight
;
5883 m_rowBottoms
[row
] = bottom
;
5889 void wxGrid
::SetRowSize( int row
, int height
)
5891 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5895 int h
= wxMax( 0, height
);
5896 int diff
= h
- m_rowHeights
[row
];
5898 m_rowHeights
[row
] = h
;
5899 for ( i
= row
; i
< m_numRows
; i
++ )
5901 m_rowBottoms
[i
] += diff
;
5906 void wxGrid
::SetDefaultColSize( int width
, bool resizeExistingCols
)
5908 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5910 if ( resizeExistingCols
)
5914 for ( col
= 0; col
< m_numCols
; col
++ )
5916 m_colWidths
[col
] = m_defaultColWidth
;
5917 right
+= m_defaultColWidth
;
5918 m_colRights
[col
] = right
;
5924 void wxGrid
::SetColSize( int col
, int width
)
5926 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5930 int w
= wxMax( 0, width
);
5931 int diff
= w
- m_colWidths
[col
];
5932 m_colWidths
[col
] = w
;
5934 for ( i
= col
; i
< m_numCols
; i
++ )
5936 m_colRights
[i
] += diff
;
5943 // ------ cell value accessor functions
5946 void wxGrid
::SetCellValue( int row
, int col
, const wxString
& s
)
5950 m_table
->SetValue( row
, col
, s
.c_str() );
5951 if ( !GetBatchCount() )
5953 wxClientDC
dc( m_gridWin
);
5955 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5958 #if 0 // TODO: edit in place
5960 if ( m_currentCellCoords
.GetRow() == row
&&
5961 m_currentCellCoords
.GetCol() == col
)
5963 SetEditControlValue( s
);
5972 // ------ Block, row and col selection
5975 void wxGrid
::SelectRow( int row
, bool addToSelected
)
5979 if ( IsSelection() && addToSelected
)
5982 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5985 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5986 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5987 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5988 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5992 need_refresh
[0] = TRUE
;
5993 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5994 wxGridCellCoords ( oldTop
- 1,
5996 m_selectedTopLeft
.SetRow( row
);
6001 need_refresh
[1] = TRUE
;
6002 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6003 wxGridCellCoords ( oldBottom
,
6006 m_selectedTopLeft
.SetCol( 0 );
6009 if ( oldBottom
< row
)
6011 need_refresh
[2] = TRUE
;
6012 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6013 wxGridCellCoords ( row
,
6015 m_selectedBottomRight
.SetRow( row
);
6018 if ( oldRight
< m_numCols
- 1 )
6020 need_refresh
[3] = TRUE
;
6021 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6023 wxGridCellCoords ( oldBottom
,
6025 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6028 for (i
= 0; i
< 4; i
++ )
6029 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6030 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6034 r
= SelectionToDeviceRect();
6036 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6038 m_selectedTopLeft
.Set( row
, 0 );
6039 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6040 r
= SelectionToDeviceRect();
6041 m_gridWin
->Refresh( FALSE
, &r
);
6044 wxGridRangeSelectEvent
gridEvt( GetId(),
6045 wxEVT_GRID_RANGE_SELECT
,
6048 m_selectedBottomRight
);
6050 GetEventHandler()->ProcessEvent(gridEvt
);
6054 void wxGrid
::SelectCol( int col
, bool addToSelected
)
6056 if ( IsSelection() && addToSelected
)
6059 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
6062 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6063 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6064 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6065 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6067 if ( oldLeft
> col
)
6069 need_refresh
[0] = TRUE
;
6070 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6071 wxGridCellCoords ( m_numRows
- 1,
6073 m_selectedTopLeft
.SetCol( col
);
6078 need_refresh
[1] = TRUE
;
6079 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6080 wxGridCellCoords ( oldTop
- 1,
6082 m_selectedTopLeft
.SetRow( 0 );
6085 if ( oldRight
< col
)
6087 need_refresh
[2] = TRUE
;
6088 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6089 wxGridCellCoords ( m_numRows
- 1,
6091 m_selectedBottomRight
.SetCol( col
);
6094 if ( oldBottom
< m_numRows
- 1 )
6096 need_refresh
[3] = TRUE
;
6097 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6099 wxGridCellCoords ( m_numRows
- 1,
6101 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6104 for (i
= 0; i
< 4; i
++ )
6105 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6106 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6112 r
= SelectionToDeviceRect();
6114 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6116 m_selectedTopLeft
.Set( 0, col
);
6117 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6118 r
= SelectionToDeviceRect();
6119 m_gridWin
->Refresh( FALSE
, &r
);
6122 wxGridRangeSelectEvent
gridEvt( GetId(),
6123 wxEVT_GRID_RANGE_SELECT
,
6126 m_selectedBottomRight
);
6128 GetEventHandler()->ProcessEvent(gridEvt
);
6132 void wxGrid
::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6135 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6137 if ( topRow
> bottomRow
)
6144 if ( leftCol
> rightCol
)
6151 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6152 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6154 if ( m_selectedTopLeft
!= updateTopLeft
||
6155 m_selectedBottomRight
!= updateBottomRight
)
6157 // Compute two optimal update rectangles:
6158 // Either one rectangle is a real subset of the
6159 // other, or they are (almost) disjoint!
6161 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
6164 // Store intermediate values
6165 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6166 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6167 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6168 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6170 // Determine the outer/inner coordinates.
6171 if (oldLeft
> leftCol
)
6177 if (oldTop
> topRow
)
6183 if (oldRight
< rightCol
)
6186 oldRight
= rightCol
;
6189 if (oldBottom
< bottomRow
)
6192 oldBottom
= bottomRow
;
6196 // Now, either the stuff marked old is the outer
6197 // rectangle or we don't have a situation where one
6198 // is contained in the other.
6200 if ( oldLeft
< leftCol
)
6202 need_refresh
[0] = TRUE
;
6203 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6205 wxGridCellCoords ( oldBottom
,
6209 if ( oldTop
< topRow
)
6211 need_refresh
[1] = TRUE
;
6212 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6214 wxGridCellCoords ( topRow
- 1,
6218 if ( oldRight
> rightCol
)
6220 need_refresh
[2] = TRUE
;
6221 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6223 wxGridCellCoords ( oldBottom
,
6227 if ( oldBottom
> bottomRow
)
6229 need_refresh
[3] = TRUE
;
6230 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6232 wxGridCellCoords ( oldBottom
,
6238 m_selectedTopLeft
= updateTopLeft
;
6239 m_selectedBottomRight
= updateBottomRight
;
6241 // various Refresh() calls
6242 for (i
= 0; i
< 4; i
++ )
6243 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6244 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6247 // only generate an event if the block is not being selected by
6248 // dragging the mouse (in which case the event will be generated in
6249 // the mouse event handler)
6250 if ( !m_isDragging
)
6252 wxGridRangeSelectEvent
gridEvt( GetId(),
6253 wxEVT_GRID_RANGE_SELECT
,
6256 m_selectedBottomRight
);
6258 GetEventHandler()->ProcessEvent(gridEvt
);
6262 void wxGrid
::SelectAll()
6264 m_selectedTopLeft
.Set( 0, 0 );
6265 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6267 m_gridWin
->Refresh();
6271 void wxGrid
::ClearSelection()
6273 m_selectedTopLeft
= wxGridNoCellCoords
;
6274 m_selectedBottomRight
= wxGridNoCellCoords
;
6278 // This function returns the rectangle that encloses the given block
6279 // in device coords clipped to the client size of the grid window.
6281 wxRect wxGrid
::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6282 const wxGridCellCoords
&bottomRight
)
6284 wxRect
rect( wxGridNoCellRect
);
6287 cellRect
= CellToRect( topLeft
);
6288 if ( cellRect
!= wxGridNoCellRect
)
6294 rect
= wxRect( 0, 0, 0, 0 );
6297 cellRect
= CellToRect( bottomRight
);
6298 if ( cellRect
!= wxGridNoCellRect
)
6304 return wxGridNoCellRect
;
6307 // convert to scrolled coords
6309 int left
, top
, right
, bottom
;
6310 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6311 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6314 m_gridWin
->GetClientSize( &cw
, &ch
);
6316 rect
.SetLeft( wxMax(0, left
) );
6317 rect
.SetTop( wxMax(0, top
) );
6318 rect
.SetRight( wxMin(cw
, right
) );
6319 rect
.SetBottom( wxMin(ch
, bottom
) );
6327 // ------ Grid event classes
6330 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6332 wxGridEvent
::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6333 int row
, int col
, int x
, int y
,
6334 bool control
, bool shift
, bool alt
, bool meta
)
6335 : wxNotifyEvent( type
, id
)
6341 m_control
= control
;
6346 SetEventObject(obj
);
6350 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6352 wxGridSizeEvent
::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6353 int rowOrCol
, int x
, int y
,
6354 bool control
, bool shift
, bool alt
, bool meta
)
6355 : wxNotifyEvent( type
, id
)
6357 m_rowOrCol
= rowOrCol
;
6360 m_control
= control
;
6365 SetEventObject(obj
);
6369 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6371 wxGridRangeSelectEvent
::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6372 const wxGridCellCoords
& topLeft
,
6373 const wxGridCellCoords
& bottomRight
,
6374 bool control
, bool shift
, bool alt
, bool meta
)
6375 : wxNotifyEvent( type
, id
)
6377 m_topLeft
= topLeft
;
6378 m_bottomRight
= bottomRight
;
6379 m_control
= control
;
6384 SetEventObject(obj
);
6388 #endif // ifndef wxUSE_NEW_GRID