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"
44 // this include needs to be outside precomp for BCC
45 #include "wx/textfile.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
56 struct wxGridCellWithAttr
58 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
59 : coords(row
, col
), attr(attr_
)
68 wxGridCellCoords coords
;
72 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
74 #include "wx/arrimpl.cpp"
76 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
77 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
86 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
87 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
88 const wxPoint
&pos
, const wxSize
&size
);
93 void OnPaint( wxPaintEvent
& event
);
94 void OnMouseEvent( wxMouseEvent
& event
);
95 void OnKeyDown( wxKeyEvent
& event
);
97 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
102 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
105 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
106 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
107 const wxPoint
&pos
, const wxSize
&size
);
112 void OnPaint( wxPaintEvent
&event
);
113 void OnMouseEvent( wxMouseEvent
& event
);
114 void OnKeyDown( wxKeyEvent
& event
);
116 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
117 DECLARE_EVENT_TABLE()
121 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
124 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
125 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
126 const wxPoint
&pos
, const wxSize
&size
);
131 void OnMouseEvent( wxMouseEvent
& event
);
132 void OnKeyDown( wxKeyEvent
& event
);
133 void OnPaint( wxPaintEvent
& event
);
135 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
136 DECLARE_EVENT_TABLE()
139 class WXDLLEXPORT wxGridWindow
: public wxPanel
144 m_owner
= (wxGrid
*)NULL
;
145 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
146 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
149 wxGridWindow( wxGrid
*parent
,
150 wxGridRowLabelWindow
*rowLblWin
,
151 wxGridColLabelWindow
*colLblWin
,
152 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
155 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
159 wxGridRowLabelWindow
*m_rowLabelWin
;
160 wxGridColLabelWindow
*m_colLabelWin
;
162 void OnPaint( wxPaintEvent
&event
);
163 void OnMouseEvent( wxMouseEvent
& event
);
164 void OnKeyDown( wxKeyEvent
& );
165 void OnEraseBackground( wxEraseEvent
& );
168 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
169 DECLARE_EVENT_TABLE()
174 class wxGridCellEditorEvtHandler
: public wxEvtHandler
177 wxGridCellEditorEvtHandler()
178 : m_grid(0), m_editor(0)
180 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
181 : m_grid(grid
), m_editor(editor
)
184 void OnKeyDown(wxKeyEvent
& event
);
188 wxGridCellEditor
* m_editor
;
189 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
190 DECLARE_EVENT_TABLE()
194 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
195 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
196 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
201 // ----------------------------------------------------------------------------
202 // the internal data representation used by wxGridCellAttrProvider
203 // ----------------------------------------------------------------------------
205 // this class stores attributes set for cells
206 class WXDLLEXPORT wxGridCellAttrData
209 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
210 wxGridCellAttr
*GetAttr(int row
, int col
) const;
211 void UpdateAttrRows( size_t pos
, int numRows
);
212 void UpdateAttrCols( size_t pos
, int numCols
);
215 // searches for the attr for given cell, returns wxNOT_FOUND if not found
216 int FindIndex(int row
, int col
) const;
218 wxGridCellWithAttrArray m_attrs
;
221 // this class stores attributes set for rows or columns
222 class WXDLLEXPORT wxGridRowOrColAttrData
225 ~wxGridRowOrColAttrData();
227 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
228 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
229 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
232 wxArrayInt m_rowsOrCols
;
233 wxArrayAttrs m_attrs
;
236 // NB: this is just a wrapper around 3 objects: one which stores cell
237 // attributes, and 2 others for row/col ones
238 class WXDLLEXPORT wxGridCellAttrProviderData
241 wxGridCellAttrData m_cellAttrs
;
242 wxGridRowOrColAttrData m_rowAttrs
,
246 // ----------------------------------------------------------------------------
247 // conditional compilation
248 // ----------------------------------------------------------------------------
250 #ifndef WXGRID_DRAW_LINES
251 #define WXGRID_DRAW_LINES 1
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 //#define DEBUG_ATTR_CACHE
259 #ifdef DEBUG_ATTR_CACHE
260 static size_t gs_nAttrCacheHits
= 0;
261 static size_t gs_nAttrCacheMisses
= 0;
262 #endif // DEBUG_ATTR_CACHE
264 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
265 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
268 // TODO: fixed so far - make configurable later (and also different for x/y)
269 static const size_t GRID_SCROLL_LINE
= 10;
271 // ============================================================================
273 // ============================================================================
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 wxGridCellEditor::wxGridCellEditor()
286 wxGridCellEditor::~wxGridCellEditor()
292 void wxGridCellEditor::Destroy()
295 m_control
->Destroy();
300 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
302 wxASSERT_MSG(m_control
,
303 wxT("The wxGridCellEditor must be Created first!"));
304 m_control
->Show(show
);
308 // set the colours/fonts if we have any
311 if ( attr
->HasTextColour() )
313 m_colFgOld
= m_control
->GetForegroundColour();
314 m_control
->SetForegroundColour(attr
->GetTextColour());
317 if ( attr
->HasBackgroundColour() )
319 m_colBgOld
= m_control
->GetBackgroundColour();
320 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
323 if ( attr
->HasFont() )
325 m_fontOld
= m_control
->GetFont();
326 m_control
->SetFont(attr
->GetFont());
329 // can't do anything more in the base class version, the other
330 // attributes may only be used by the derived classes
335 // restore the standard colours fonts
336 if ( m_colFgOld
.Ok() )
338 m_control
->SetForegroundColour(m_colFgOld
);
339 m_colFgOld
= wxNullColour
;
342 if ( m_colBgOld
.Ok() )
344 m_control
->SetBackgroundColour(m_colBgOld
);
345 m_colBgOld
= wxNullColour
;
348 if ( m_fontOld
.Ok() )
350 m_control
->SetFont(m_fontOld
);
351 m_fontOld
= wxNullFont
;
356 void wxGridCellEditor::SetSize(const wxRect
& rect
)
358 wxASSERT_MSG(m_control
,
359 wxT("The wxGridCellEditor must be Created first!"));
360 m_control
->SetSize(rect
);
363 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
369 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
375 wxGridCellTextEditor::wxGridCellTextEditor()
379 void wxGridCellTextEditor::Create(wxWindow
* parent
,
381 wxEvtHandler
* evtHandler
)
383 m_control
= new wxTextCtrl(parent
, -1, "",
384 wxDefaultPosition
, wxDefaultSize
385 #if defined(__WXMSW__)
386 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
391 m_control
->PushEventHandler(evtHandler
);
395 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
397 wxASSERT_MSG(m_control
,
398 wxT("The wxGridCellEditor must be Created first!"));
400 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
401 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
402 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
403 ((wxTextCtrl
*)m_control
)->SetFocus();
408 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
411 wxASSERT_MSG(m_control
,
412 wxT("The wxGridCellEditor must be Created first!"));
414 bool changed
= FALSE
;
415 wxString value
= ((wxTextCtrl
*)m_control
)->GetValue();
416 if (value
!= m_startValue
)
420 grid
->GetTable()->SetValue(row
, col
, value
);
422 m_startValue
= wxEmptyString
;
423 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
429 void wxGridCellTextEditor::Reset()
431 wxASSERT_MSG(m_control
,
432 wxT("The wxGridCellEditor must be Created first!"));
434 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
435 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
439 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
441 wxASSERT_MSG(m_control
,
442 wxT("The wxGridCellEditor must be Created first!"));
444 int code
= event
.KeyCode();
445 if (code
>= 32 && code
< 255) {
446 wxString
st((char)code
);
447 if (! event
.ShiftDown())
449 ((wxTextCtrl
*)m_control
)->AppendText(st
);
454 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
456 #if defined(__WXMOTIF__) || defined(__WXGTK__)
457 // wxMotif needs a little extra help...
458 int pos
= ((wxTextCtrl
*)m_control
)->GetInsertionPoint();
459 wxString
s( ((wxTextCtrl
*)m_control
)->GetValue() );
460 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
461 ((wxTextCtrl
*)m_control
)->SetValue(s
);
462 ((wxTextCtrl
*)m_control
)->SetInsertionPoint( pos
);
464 // the other ports can handle a Return key press
471 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
473 switch ( event
.KeyCode() )
477 m_grid
->EnableCellEditControl(FALSE
);
481 event
.Skip( m_grid
->ProcessEvent( event
) );
485 if (!m_grid
->ProcessEvent(event
))
486 m_editor
->HandleReturn(event
);
495 // ----------------------------------------------------------------------------
496 // wxGridCellRenderer
497 // ----------------------------------------------------------------------------
499 void wxGridCellRenderer::Draw(wxGrid
& grid
,
500 wxGridCellAttr
& attr
,
506 dc
.SetBackgroundMode( wxSOLID
);
510 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
514 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
517 dc
.SetPen( *wxTRANSPARENT_PEN
);
518 dc
.DrawRectangle(rect
);
521 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
522 wxGridCellAttr
& attr
,
524 const wxRect
& rectCell
,
528 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
530 // now we only have to draw the text
531 dc
.SetBackgroundMode( wxTRANSPARENT
);
535 dc
.SetTextBackground( grid
.GetSelectionBackground() );
536 dc
.SetTextForeground( grid
.GetSelectionForeground() );
540 dc
.SetTextBackground( attr
.GetBackgroundColour() );
541 dc
.SetTextForeground( attr
.GetTextColour() );
543 dc
.SetFont( attr
.GetFont() );
546 attr
.GetAlignment(&hAlign
, &vAlign
);
548 wxRect rect
= rectCell
;
554 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
555 rect
, hAlign
, vAlign
);
558 // ----------------------------------------------------------------------------
560 // ----------------------------------------------------------------------------
562 const wxColour
& wxGridCellAttr::GetTextColour() const
566 else if (m_defGridAttr
!= this)
567 return m_defGridAttr
->GetTextColour();
569 wxFAIL_MSG(wxT("Missing default cell attribute"));
575 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
577 if (HasBackgroundColour())
579 else if (m_defGridAttr
!= this)
580 return m_defGridAttr
->GetBackgroundColour();
582 wxFAIL_MSG(wxT("Missing default cell attribute"));
588 const wxFont
& wxGridCellAttr::GetFont() const
592 else if (m_defGridAttr
!= this)
593 return m_defGridAttr
->GetFont();
595 wxFAIL_MSG(wxT("Missing default cell attribute"));
601 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
603 if (HasAlignment()) {
604 if ( hAlign
) *hAlign
= m_hAlign
;
605 if ( vAlign
) *vAlign
= m_vAlign
;
607 else if (m_defGridAttr
!= this)
608 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
610 wxFAIL_MSG(wxT("Missing default cell attribute"));
615 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
619 else if (m_defGridAttr
!= this)
620 return m_defGridAttr
->GetRenderer();
622 wxFAIL_MSG(wxT("Missing default cell attribute"));
627 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
631 else if (m_defGridAttr
!= this)
632 return m_defGridAttr
->GetEditor();
634 wxFAIL_MSG(wxT("Missing default cell attribute"));
639 // ----------------------------------------------------------------------------
640 // wxGridCellAttrData
641 // ----------------------------------------------------------------------------
643 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
645 int n
= FindIndex(row
, col
);
646 if ( n
== wxNOT_FOUND
)
649 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
655 // change the attribute
656 m_attrs
[(size_t)n
].attr
= attr
;
660 // remove this attribute
661 m_attrs
.RemoveAt((size_t)n
);
666 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
668 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
670 int n
= FindIndex(row
, col
);
671 if ( n
!= wxNOT_FOUND
)
673 attr
= m_attrs
[(size_t)n
].attr
;
680 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
682 size_t count
= m_attrs
.GetCount();
683 for ( size_t n
= 0; n
< count
; n
++ )
685 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
686 wxCoord row
= coords
.GetRow();
687 if ((size_t)row
>= pos
)
691 // If rows inserted, include row counter where necessary
692 coords
.SetRow(row
+ numRows
);
694 else if (numRows
< 0)
696 // If rows deleted ...
697 if ((size_t)row
>= pos
- numRows
)
699 // ...either decrement row counter (if row still exists)...
700 coords
.SetRow(row
+ numRows
);
704 // ...or remove the attribute
705 m_attrs
.RemoveAt((size_t)n
);
713 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
715 size_t count
= m_attrs
.GetCount();
716 for ( size_t n
= 0; n
< count
; n
++ )
718 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
719 wxCoord col
= coords
.GetCol();
720 if ( (size_t)col
>= pos
)
724 // If rows inserted, include row counter where necessary
725 coords
.SetCol(col
+ numCols
);
727 else if (numCols
< 0)
729 // If rows deleted ...
730 if ((size_t)col
>= pos
- numCols
)
732 // ...either decrement row counter (if row still exists)...
733 coords
.SetCol(col
+ numCols
);
737 // ...or remove the attribute
738 m_attrs
.RemoveAt((size_t)n
);
746 int wxGridCellAttrData::FindIndex(int row
, int col
) const
748 size_t count
= m_attrs
.GetCount();
749 for ( size_t n
= 0; n
< count
; n
++ )
751 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
752 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
761 // ----------------------------------------------------------------------------
762 // wxGridRowOrColAttrData
763 // ----------------------------------------------------------------------------
765 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
767 size_t count
= m_attrs
.Count();
768 for ( size_t n
= 0; n
< count
; n
++ )
770 m_attrs
[n
]->DecRef();
774 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
776 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
778 int n
= m_rowsOrCols
.Index(rowOrCol
);
779 if ( n
!= wxNOT_FOUND
)
781 attr
= m_attrs
[(size_t)n
];
788 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
790 int n
= m_rowsOrCols
.Index(rowOrCol
);
791 if ( n
== wxNOT_FOUND
)
794 m_rowsOrCols
.Add(rowOrCol
);
801 // change the attribute
802 m_attrs
[(size_t)n
] = attr
;
806 // remove this attribute
807 m_attrs
[(size_t)n
]->DecRef();
808 m_rowsOrCols
.RemoveAt((size_t)n
);
809 m_attrs
.RemoveAt((size_t)n
);
814 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
816 size_t count
= m_attrs
.GetCount();
817 for ( size_t n
= 0; n
< count
; n
++ )
819 int & rowOrCol
= m_rowsOrCols
[n
];
820 if ( (size_t)rowOrCol
>= pos
)
822 if ( numRowsOrCols
> 0 )
824 // If rows inserted, include row counter where necessary
825 rowOrCol
+= numRowsOrCols
;
827 else if ( numRowsOrCols
< 0)
829 // If rows deleted, either decrement row counter (if row still exists)
830 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
831 rowOrCol
+= numRowsOrCols
;
834 m_rowsOrCols
.RemoveAt((size_t)n
);
835 m_attrs
.RemoveAt((size_t)n
);
843 // ----------------------------------------------------------------------------
844 // wxGridCellAttrProvider
845 // ----------------------------------------------------------------------------
847 wxGridCellAttrProvider::wxGridCellAttrProvider()
849 m_data
= (wxGridCellAttrProviderData
*)NULL
;
852 wxGridCellAttrProvider::~wxGridCellAttrProvider()
857 void wxGridCellAttrProvider::InitData()
859 m_data
= new wxGridCellAttrProviderData
;
862 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
864 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
867 // first look for the attribute of this specific cell
868 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
872 // then look for the col attr (col attributes are more common than
873 // the row ones, hence they have priority)
874 attr
= m_data
->m_colAttrs
.GetAttr(col
);
879 // finally try the row attributes
880 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
887 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
893 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
896 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
901 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
904 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
909 m_data
->m_colAttrs
.SetAttr(attr
, col
);
912 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
916 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
918 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
922 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
926 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
928 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
932 // ----------------------------------------------------------------------------
934 // ----------------------------------------------------------------------------
936 //////////////////////////////////////////////////////////////////////
938 // Abstract base class for grid data (the model)
940 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
943 wxGridTableBase::wxGridTableBase()
945 m_view
= (wxGrid
*) NULL
;
946 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
949 wxGridTableBase::~wxGridTableBase()
951 delete m_attrProvider
;
954 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
956 delete m_attrProvider
;
957 m_attrProvider
= attrProvider
;
960 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
962 if ( m_attrProvider
)
963 return m_attrProvider
->GetAttr(row
, col
);
965 return (wxGridCellAttr
*)NULL
;
968 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
970 if ( m_attrProvider
)
972 m_attrProvider
->SetAttr(attr
, row
, col
);
976 // as we take ownership of the pointer and don't store it, we must
982 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
984 if ( m_attrProvider
)
986 m_attrProvider
->SetRowAttr(attr
, row
);
990 // as we take ownership of the pointer and don't store it, we must
996 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
998 if ( m_attrProvider
)
1000 m_attrProvider
->SetColAttr(attr
, col
);
1004 // as we take ownership of the pointer and don't store it, we must
1010 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1012 if ( m_attrProvider
)
1014 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1018 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1020 if ( m_attrProvider
)
1022 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1026 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1028 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1029 "but your derived table class does not override this function") );
1034 bool wxGridTableBase::AppendRows( size_t numRows
)
1036 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1037 "but your derived table class does not override this function"));
1042 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1044 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1045 "but your derived table class does not override this function"));
1050 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1052 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1053 "but your derived table class does not override this function"));
1058 bool wxGridTableBase::AppendCols( size_t numCols
)
1060 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1061 "but your derived table class does not override this function"));
1066 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1068 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1069 "but your derived table class does not override this function"));
1075 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1082 wxString
wxGridTableBase::GetColLabelValue( int col
)
1084 // default col labels are:
1085 // cols 0 to 25 : A-Z
1086 // cols 26 to 675 : AA-ZZ
1091 for ( n
= 1; ; n
++ )
1093 s
+= (_T('A') + (wxChar
)( col%26
));
1095 if ( col
< 0 ) break;
1098 // reverse the string...
1100 for ( i
= 0; i
< n
; i
++ )
1110 //////////////////////////////////////////////////////////////////////
1112 // Message class for the grid table to send requests and notifications
1116 wxGridTableMessage::wxGridTableMessage()
1118 m_table
= (wxGridTableBase
*) NULL
;
1124 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1125 int commandInt1
, int commandInt2
)
1129 m_comInt1
= commandInt1
;
1130 m_comInt2
= commandInt2
;
1135 //////////////////////////////////////////////////////////////////////
1137 // A basic grid table for string data. An object of this class will
1138 // created by wxGrid if you don't specify an alternative table class.
1141 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1143 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1145 wxGridStringTable::wxGridStringTable()
1150 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1155 m_data
.Alloc( numRows
);
1158 sa
.Alloc( numCols
);
1159 for ( col
= 0; col
< numCols
; col
++ )
1161 sa
.Add( wxEmptyString
);
1164 for ( row
= 0; row
< numRows
; row
++ )
1170 wxGridStringTable::~wxGridStringTable()
1174 long wxGridStringTable::GetNumberRows()
1176 return m_data
.GetCount();
1179 long wxGridStringTable::GetNumberCols()
1181 if ( m_data
.GetCount() > 0 )
1182 return m_data
[0].GetCount();
1187 wxString
wxGridStringTable::GetValue( int row
, int col
)
1189 // TODO: bounds checking
1191 return m_data
[row
][col
];
1194 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1196 // TODO: bounds checking
1198 m_data
[row
][col
] = s
;
1201 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1203 // TODO: bounds checking
1205 return (m_data
[row
][col
] == wxEmptyString
);
1209 void wxGridStringTable::Clear()
1212 int numRows
, numCols
;
1214 numRows
= m_data
.GetCount();
1217 numCols
= m_data
[0].GetCount();
1219 for ( row
= 0; row
< numRows
; row
++ )
1221 for ( col
= 0; col
< numCols
; col
++ )
1223 m_data
[row
][col
] = wxEmptyString
;
1230 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1234 size_t curNumRows
= m_data
.GetCount();
1235 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1237 if ( pos
>= curNumRows
)
1239 return AppendRows( numRows
);
1243 sa
.Alloc( curNumCols
);
1244 for ( col
= 0; col
< curNumCols
; col
++ )
1246 sa
.Add( wxEmptyString
);
1249 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1251 m_data
.Insert( sa
, row
);
1253 UpdateAttrRows( pos
, numRows
);
1256 wxGridTableMessage
msg( this,
1257 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1261 GetView()->ProcessTableMessage( msg
);
1267 bool wxGridStringTable::AppendRows( size_t numRows
)
1271 size_t curNumRows
= m_data
.GetCount();
1272 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1275 if ( curNumCols
> 0 )
1277 sa
.Alloc( curNumCols
);
1278 for ( col
= 0; col
< curNumCols
; col
++ )
1280 sa
.Add( wxEmptyString
);
1284 for ( row
= 0; row
< numRows
; row
++ )
1291 wxGridTableMessage
msg( this,
1292 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1295 GetView()->ProcessTableMessage( msg
);
1301 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1305 size_t curNumRows
= m_data
.GetCount();
1307 if ( pos
>= curNumRows
)
1310 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1311 "Pos value is invalid for present table with %d rows",
1312 pos
, numRows
, curNumRows
);
1313 wxFAIL_MSG( wxT(errmsg
) );
1317 if ( numRows
> curNumRows
- pos
)
1319 numRows
= curNumRows
- pos
;
1322 if ( numRows
>= curNumRows
)
1324 m_data
.Empty(); // don't release memory just yet
1328 for ( n
= 0; n
< numRows
; n
++ )
1330 m_data
.Remove( pos
);
1333 UpdateAttrRows( pos
, -((int)numRows
) );
1336 wxGridTableMessage
msg( this,
1337 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1341 GetView()->ProcessTableMessage( msg
);
1347 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1351 size_t curNumRows
= m_data
.GetCount();
1352 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1354 if ( pos
>= curNumCols
)
1356 return AppendCols( numCols
);
1359 for ( row
= 0; row
< curNumRows
; row
++ )
1361 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1363 m_data
[row
].Insert( wxEmptyString
, col
);
1366 UpdateAttrCols( pos
, numCols
);
1369 wxGridTableMessage
msg( this,
1370 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1374 GetView()->ProcessTableMessage( msg
);
1380 bool wxGridStringTable::AppendCols( size_t numCols
)
1384 size_t curNumRows
= m_data
.GetCount();
1387 // TODO: something better than this ?
1389 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1390 "Call AppendRows() first") );
1394 for ( row
= 0; row
< curNumRows
; row
++ )
1396 for ( n
= 0; n
< numCols
; n
++ )
1398 m_data
[row
].Add( wxEmptyString
);
1404 wxGridTableMessage
msg( this,
1405 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1408 GetView()->ProcessTableMessage( msg
);
1414 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1418 size_t curNumRows
= m_data
.GetCount();
1419 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1421 if ( pos
>= curNumCols
)
1424 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1425 "Pos value is invalid for present table with %d cols",
1426 pos
, numCols
, curNumCols
);
1427 wxFAIL_MSG( wxT( errmsg
) );
1431 if ( numCols
> curNumCols
- pos
)
1433 numCols
= curNumCols
- pos
;
1436 for ( row
= 0; row
< curNumRows
; row
++ )
1438 if ( numCols
>= curNumCols
)
1440 m_data
[row
].Clear();
1444 for ( n
= 0; n
< numCols
; n
++ )
1446 m_data
[row
].Remove( pos
);
1450 UpdateAttrCols( pos
, -((int)numCols
) );
1453 wxGridTableMessage
msg( this,
1454 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1458 GetView()->ProcessTableMessage( msg
);
1464 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1466 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1468 // using default label
1470 return wxGridTableBase::GetRowLabelValue( row
);
1474 return m_rowLabels
[ row
];
1478 wxString
wxGridStringTable::GetColLabelValue( int col
)
1480 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1482 // using default label
1484 return wxGridTableBase::GetColLabelValue( col
);
1488 return m_colLabels
[ col
];
1492 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1494 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1496 int n
= m_rowLabels
.GetCount();
1498 for ( i
= n
; i
<= row
; i
++ )
1500 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1504 m_rowLabels
[row
] = value
;
1507 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1509 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1511 int n
= m_colLabels
.GetCount();
1513 for ( i
= n
; i
<= col
; i
++ )
1515 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1519 m_colLabels
[col
] = value
;
1524 //////////////////////////////////////////////////////////////////////
1525 //////////////////////////////////////////////////////////////////////
1527 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1529 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1530 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1531 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1532 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1535 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1537 const wxPoint
&pos
, const wxSize
&size
)
1538 : wxWindow( parent
, id
, pos
, size
)
1543 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1547 // NO - don't do this because it will set both the x and y origin
1548 // coords to match the parent scrolled window and we just want to
1549 // set the y coord - MB
1551 // m_owner->PrepareDC( dc );
1554 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1555 dc
.SetDeviceOrigin( 0, -y
);
1557 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1558 m_owner
->DrawRowLabels( dc
);
1562 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1564 m_owner
->ProcessRowLabelMouseEvent( event
);
1568 // This seems to be required for wxMotif otherwise the mouse
1569 // cursor must be in the cell edit control to get key events
1571 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1573 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1578 //////////////////////////////////////////////////////////////////////
1580 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1582 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1583 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1584 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1585 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1588 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1590 const wxPoint
&pos
, const wxSize
&size
)
1591 : wxWindow( parent
, id
, pos
, size
)
1596 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1600 // NO - don't do this because it will set both the x and y origin
1601 // coords to match the parent scrolled window and we just want to
1602 // set the x coord - MB
1604 // m_owner->PrepareDC( dc );
1607 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1608 dc
.SetDeviceOrigin( -x
, 0 );
1610 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1611 m_owner
->DrawColLabels( dc
);
1615 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1617 m_owner
->ProcessColLabelMouseEvent( event
);
1621 // This seems to be required for wxMotif otherwise the mouse
1622 // cursor must be in the cell edit control to get key events
1624 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1626 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1631 //////////////////////////////////////////////////////////////////////
1633 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1635 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1636 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1637 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1638 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1641 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1643 const wxPoint
&pos
, const wxSize
&size
)
1644 : wxWindow( parent
, id
, pos
, size
)
1649 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1653 int client_height
= 0;
1654 int client_width
= 0;
1655 GetClientSize( &client_width
, &client_height
);
1657 dc
.SetPen( *wxBLACK_PEN
);
1658 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1659 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1661 dc
.SetPen( *wxWHITE_PEN
);
1662 dc
.DrawLine( 0, 0, client_width
, 0 );
1663 dc
.DrawLine( 0, 0, 0, client_height
);
1667 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1669 m_owner
->ProcessCornerLabelMouseEvent( event
);
1673 // This seems to be required for wxMotif otherwise the mouse
1674 // cursor must be in the cell edit control to get key events
1676 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1678 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1683 //////////////////////////////////////////////////////////////////////
1685 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1687 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1688 EVT_PAINT( wxGridWindow::OnPaint
)
1689 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1690 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1691 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1694 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1695 wxGridRowLabelWindow
*rowLblWin
,
1696 wxGridColLabelWindow
*colLblWin
,
1697 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1698 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1701 m_rowLabelWin
= rowLblWin
;
1702 m_colLabelWin
= colLblWin
;
1703 SetBackgroundColour( "WHITE" );
1707 wxGridWindow::~wxGridWindow()
1712 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1714 wxPaintDC
dc( this );
1715 m_owner
->PrepareDC( dc
);
1716 wxRegion reg
= GetUpdateRegion();
1717 m_owner
->CalcCellsExposed( reg
);
1718 m_owner
->DrawGridCellArea( dc
);
1719 #if WXGRID_DRAW_LINES
1720 m_owner
->DrawAllGridLines( dc
, reg
);
1725 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1727 wxPanel::ScrollWindow( dx
, dy
, rect
);
1728 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1729 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1733 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1735 m_owner
->ProcessGridCellMouseEvent( event
);
1739 // This seems to be required for wxMotif otherwise the mouse
1740 // cursor must be in the cell edit control to get key events
1742 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1744 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1747 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1753 //////////////////////////////////////////////////////////////////////
1756 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1758 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1759 EVT_PAINT( wxGrid::OnPaint
)
1760 EVT_SIZE( wxGrid::OnSize
)
1761 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1762 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1765 wxGrid::wxGrid( wxWindow
*parent
,
1770 const wxString
& name
)
1771 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1780 m_defaultCellAttr
->SafeDecRef();
1782 #ifdef DEBUG_ATTR_CACHE
1783 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1784 wxPrintf(_T("wxGrid attribute cache statistics: "
1785 "total: %u, hits: %u (%u%%)\n"),
1786 total
, gs_nAttrCacheHits
,
1787 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1796 // ----- internal init and update functions
1799 void wxGrid::Create()
1801 m_created
= FALSE
; // set to TRUE by CreateGrid
1802 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
1804 m_table
= (wxGridTableBase
*) NULL
;
1807 m_cellEditCtrlEnabled
= FALSE
;
1809 m_defaultCellAttr
= new wxGridCellAttr
;
1810 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1811 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1816 m_currentCellCoords
= wxGridNoCellCoords
;
1818 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1819 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1821 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1826 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1831 m_colLabelWin
= new wxGridColLabelWindow( this,
1836 m_gridWin
= new wxGridWindow( this,
1843 SetTargetWindow( m_gridWin
);
1847 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1851 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1856 m_numRows
= numRows
;
1857 m_numCols
= numCols
;
1859 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1860 m_table
->SetView( this );
1869 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1873 // RD: Actually, this should probably be allowed. I think it would be
1874 // nice to be able to switch multiple Tables in and out of a single
1875 // View at runtime. Is there anything in the implmentation that would
1878 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1883 m_numRows
= table
->GetNumberRows();
1884 m_numCols
= table
->GetNumberCols();
1887 m_table
->SetView( this );
1902 if ( m_numRows
<= 0 )
1903 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1905 if ( m_numCols
<= 0 )
1906 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1908 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1909 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1911 if ( m_rowLabelWin
)
1913 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1917 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1920 m_labelTextColour
= wxColour( _T("BLACK") );
1923 m_attrCache
.row
= -1;
1925 // TODO: something better than this ?
1927 m_labelFont
= this->GetFont();
1928 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1930 m_rowLabelHorizAlign
= wxLEFT
;
1931 m_rowLabelVertAlign
= wxCENTRE
;
1933 m_colLabelHorizAlign
= wxCENTRE
;
1934 m_colLabelVertAlign
= wxTOP
;
1936 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1937 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1939 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1940 m_defaultRowHeight
+= 8;
1942 m_defaultRowHeight
+= 4;
1945 m_rowHeights
.Alloc( m_numRows
);
1946 m_rowBottoms
.Alloc( m_numRows
);
1948 for ( i
= 0; i
< m_numRows
; i
++ )
1950 m_rowHeights
.Add( m_defaultRowHeight
);
1951 rowBottom
+= m_defaultRowHeight
;
1952 m_rowBottoms
.Add( rowBottom
);
1955 m_colWidths
.Alloc( m_numCols
);
1956 m_colRights
.Alloc( m_numCols
);
1958 for ( i
= 0; i
< m_numCols
; i
++ )
1960 m_colWidths
.Add( m_defaultColWidth
);
1961 colRight
+= m_defaultColWidth
;
1962 m_colRights
.Add( colRight
);
1965 // Set default cell attributes
1966 m_defaultCellAttr
->SetFont(GetFont());
1967 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1968 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1969 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1970 m_defaultCellAttr
->SetTextColour(
1971 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1972 m_defaultCellAttr
->SetBackgroundColour(
1973 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1976 m_gridLineColour
= wxColour( 128, 128, 255 );
1977 m_gridLinesEnabled
= TRUE
;
1979 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1980 m_winCapture
= (wxWindow
*)NULL
;
1982 m_dragRowOrCol
= -1;
1983 m_isDragging
= FALSE
;
1984 m_startDragPos
= wxDefaultPosition
;
1986 m_waitForSlowClick
= FALSE
;
1988 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1989 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1991 m_currentCellCoords
= wxGridNoCellCoords
;
1993 m_selectedTopLeft
= wxGridNoCellCoords
;
1994 m_selectedBottomRight
= wxGridNoCellCoords
;
1995 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
1996 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1998 m_editable
= TRUE
; // default for whole grid
2000 m_inOnKeyDown
= FALSE
;
2006 void wxGrid::CalcDimensions()
2009 GetClientSize( &cw
, &ch
);
2011 if ( m_numRows
> 0 && m_numCols
> 0 )
2013 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2014 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2016 // TODO: restore the scroll position that we had before sizing
2019 GetViewStart( &x
, &y
);
2020 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2021 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2027 void wxGrid::CalcWindowSizes()
2030 GetClientSize( &cw
, &ch
);
2032 if ( m_cornerLabelWin
->IsShown() )
2033 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2035 if ( m_colLabelWin
->IsShown() )
2036 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2038 if ( m_rowLabelWin
->IsShown() )
2039 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2041 if ( m_gridWin
->IsShown() )
2042 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2046 // this is called when the grid table sends a message to say that it
2047 // has been redimensioned
2049 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2053 switch ( msg
.GetId() )
2055 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2057 size_t pos
= msg
.GetCommandInt();
2058 int numRows
= msg
.GetCommandInt2();
2059 for ( i
= 0; i
< numRows
; i
++ )
2061 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2062 m_rowBottoms
.Insert( 0, pos
);
2064 m_numRows
+= numRows
;
2067 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2069 for ( i
= pos
; i
< m_numRows
; i
++ )
2071 bottom
+= m_rowHeights
[i
];
2072 m_rowBottoms
[i
] = bottom
;
2078 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2080 int numRows
= msg
.GetCommandInt();
2081 for ( i
= 0; i
< numRows
; i
++ )
2083 m_rowHeights
.Add( m_defaultRowHeight
);
2084 m_rowBottoms
.Add( 0 );
2087 int oldNumRows
= m_numRows
;
2088 m_numRows
+= numRows
;
2091 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2093 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2095 bottom
+= m_rowHeights
[i
];
2096 m_rowBottoms
[i
] = bottom
;
2102 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2104 size_t pos
= msg
.GetCommandInt();
2105 int numRows
= msg
.GetCommandInt2();
2106 for ( i
= 0; i
< numRows
; i
++ )
2108 m_rowHeights
.Remove( pos
);
2109 m_rowBottoms
.Remove( pos
);
2111 m_numRows
-= numRows
;
2116 m_colWidths
.Clear();
2117 m_colRights
.Clear();
2118 m_currentCellCoords
= wxGridNoCellCoords
;
2122 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2123 m_currentCellCoords
.Set( 0, 0 );
2126 for ( i
= 0; i
< m_numRows
; i
++ )
2128 h
+= m_rowHeights
[i
];
2129 m_rowBottoms
[i
] = h
;
2137 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2139 size_t pos
= msg
.GetCommandInt();
2140 int numCols
= msg
.GetCommandInt2();
2141 for ( i
= 0; i
< numCols
; i
++ )
2143 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2144 m_colRights
.Insert( 0, pos
);
2146 m_numCols
+= numCols
;
2149 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2151 for ( i
= pos
; i
< m_numCols
; i
++ )
2153 right
+= m_colWidths
[i
];
2154 m_colRights
[i
] = right
;
2160 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2162 int numCols
= msg
.GetCommandInt();
2163 for ( i
= 0; i
< numCols
; i
++ )
2165 m_colWidths
.Add( m_defaultColWidth
);
2166 m_colRights
.Add( 0 );
2169 int oldNumCols
= m_numCols
;
2170 m_numCols
+= numCols
;
2173 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2175 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2177 right
+= m_colWidths
[i
];
2178 m_colRights
[i
] = right
;
2184 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2186 size_t pos
= msg
.GetCommandInt();
2187 int numCols
= msg
.GetCommandInt2();
2188 for ( i
= 0; i
< numCols
; i
++ )
2190 m_colWidths
.Remove( pos
);
2191 m_colRights
.Remove( pos
);
2193 m_numCols
-= numCols
;
2197 #if 0 // leave the row alone here so that AppendCols will work subsequently
2199 m_rowHeights
.Clear();
2200 m_rowBottoms
.Clear();
2202 m_currentCellCoords
= wxGridNoCellCoords
;
2206 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2207 m_currentCellCoords
.Set( 0, 0 );
2210 for ( i
= 0; i
< m_numCols
; i
++ )
2212 w
+= m_colWidths
[i
];
2225 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2227 wxRegionIterator
iter( reg
);
2230 m_rowLabelsExposed
.Empty();
2237 // TODO: remove this when we can...
2238 // There is a bug in wxMotif that gives garbage update
2239 // rectangles if you jump-scroll a long way by clicking the
2240 // scrollbar with middle button. This is a work-around
2242 #if defined(__WXMOTIF__)
2244 m_gridWin
->GetClientSize( &cw
, &ch
);
2245 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2246 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2249 // logical bounds of update region
2252 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2253 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2255 // find the row labels within these bounds
2259 for ( row
= 0; row
< m_numRows
; row
++ )
2261 if ( m_rowBottoms
[row
] < top
) continue;
2263 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2264 if ( rowTop
> bottom
) break;
2266 m_rowLabelsExposed
.Add( row
);
2274 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2276 wxRegionIterator
iter( reg
);
2279 m_colLabelsExposed
.Empty();
2286 // TODO: remove this when we can...
2287 // There is a bug in wxMotif that gives garbage update
2288 // rectangles if you jump-scroll a long way by clicking the
2289 // scrollbar with middle button. This is a work-around
2291 #if defined(__WXMOTIF__)
2293 m_gridWin
->GetClientSize( &cw
, &ch
);
2294 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2295 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2298 // logical bounds of update region
2301 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2302 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2304 // find the cells within these bounds
2308 for ( col
= 0; col
< m_numCols
; col
++ )
2310 if ( m_colRights
[col
] < left
) continue;
2312 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2313 if ( colLeft
> right
) break;
2315 m_colLabelsExposed
.Add( col
);
2323 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2325 wxRegionIterator
iter( reg
);
2328 m_cellsExposed
.Empty();
2329 m_rowsExposed
.Empty();
2330 m_colsExposed
.Empty();
2332 int left
, top
, right
, bottom
;
2337 // TODO: remove this when we can...
2338 // There is a bug in wxMotif that gives garbage update
2339 // rectangles if you jump-scroll a long way by clicking the
2340 // scrollbar with middle button. This is a work-around
2342 #if defined(__WXMOTIF__)
2344 m_gridWin
->GetClientSize( &cw
, &ch
);
2345 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2346 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2347 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2348 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2351 // logical bounds of update region
2353 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2354 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2356 // find the cells within these bounds
2359 int colLeft
, rowTop
;
2360 for ( row
= 0; row
< m_numRows
; row
++ )
2362 if ( m_rowBottoms
[row
] <= top
) continue;
2364 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2365 if ( rowTop
> bottom
) break;
2367 m_rowsExposed
.Add( row
);
2369 for ( col
= 0; col
< m_numCols
; col
++ )
2371 if ( m_colRights
[col
] <= left
) continue;
2373 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2374 if ( colLeft
> right
) break;
2376 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2377 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2386 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2389 wxPoint
pos( event
.GetPosition() );
2390 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2392 if ( event
.Dragging() )
2394 m_isDragging
= TRUE
;
2396 if ( event
.LeftIsDown() )
2398 switch( m_cursorMode
)
2400 case WXGRID_CURSOR_RESIZE_ROW
:
2402 int cw
, ch
, left
, dummy
;
2403 m_gridWin
->GetClientSize( &cw
, &ch
);
2404 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2406 wxClientDC
dc( m_gridWin
);
2409 m_rowBottoms
[m_dragRowOrCol
] -
2410 m_rowHeights
[m_dragRowOrCol
] +
2411 WXGRID_MIN_ROW_HEIGHT
);
2412 dc
.SetLogicalFunction(wxINVERT
);
2413 if ( m_dragLastPos
>= 0 )
2415 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2417 dc
.DrawLine( left
, y
, left
+cw
, y
);
2422 case WXGRID_CURSOR_SELECT_ROW
:
2423 if ( (row
= YToRow( y
)) >= 0 &&
2424 !IsInSelection( row
, 0 ) )
2426 SelectRow( row
, TRUE
);
2429 // default label to suppress warnings about "enumeration value
2430 // 'xxx' not handled in switch
2438 m_isDragging
= FALSE
;
2441 // ------------ Entering or leaving the window
2443 if ( event
.Entering() || event
.Leaving() )
2445 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2449 // ------------ Left button pressed
2451 else if ( event
.LeftDown() )
2453 // don't send a label click event for a hit on the
2454 // edge of the row label - this is probably the user
2455 // wanting to resize the row
2457 if ( YToEdgeOfRow(y
) < 0 )
2461 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2463 SelectRow( row
, event
.ShiftDown() );
2464 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2469 // starting to drag-resize a row
2471 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2476 // ------------ Left double click
2478 else if (event
.LeftDClick() )
2480 if ( YToEdgeOfRow(y
) < 0 )
2483 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2488 // ------------ Left button released
2490 else if ( event
.LeftUp() )
2492 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2494 DoEndDragResizeRow();
2496 // Note: we are ending the event *after* doing
2497 // default processing in this case
2499 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2502 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2507 // ------------ Right button down
2509 else if ( event
.RightDown() )
2512 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2514 // no default action at the moment
2519 // ------------ Right double click
2521 else if ( event
.RightDClick() )
2524 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2526 // no default action at the moment
2531 // ------------ No buttons down and mouse moving
2533 else if ( event
.Moving() )
2535 m_dragRowOrCol
= YToEdgeOfRow( y
);
2536 if ( m_dragRowOrCol
>= 0 )
2538 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2540 // don't capture the mouse yet
2541 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2544 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2546 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2552 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2555 wxPoint
pos( event
.GetPosition() );
2556 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2558 if ( event
.Dragging() )
2560 m_isDragging
= TRUE
;
2562 if ( event
.LeftIsDown() )
2564 switch( m_cursorMode
)
2566 case WXGRID_CURSOR_RESIZE_COL
:
2568 int cw
, ch
, dummy
, top
;
2569 m_gridWin
->GetClientSize( &cw
, &ch
);
2570 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2572 wxClientDC
dc( m_gridWin
);
2575 m_colRights
[m_dragRowOrCol
] -
2576 m_colWidths
[m_dragRowOrCol
] +
2577 WXGRID_MIN_COL_WIDTH
);
2578 dc
.SetLogicalFunction(wxINVERT
);
2579 if ( m_dragLastPos
>= 0 )
2581 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2583 dc
.DrawLine( x
, top
, x
, top
+ch
);
2588 case WXGRID_CURSOR_SELECT_COL
:
2589 if ( (col
= XToCol( x
)) >= 0 &&
2590 !IsInSelection( 0, col
) )
2592 SelectCol( col
, TRUE
);
2595 // default label to suppress warnings about "enumeration value
2596 // 'xxx' not handled in switch
2604 m_isDragging
= FALSE
;
2607 // ------------ Entering or leaving the window
2609 if ( event
.Entering() || event
.Leaving() )
2611 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2615 // ------------ Left button pressed
2617 else if ( event
.LeftDown() )
2619 // don't send a label click event for a hit on the
2620 // edge of the col label - this is probably the user
2621 // wanting to resize the col
2623 if ( XToEdgeOfCol(x
) < 0 )
2627 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2629 SelectCol( col
, event
.ShiftDown() );
2630 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2635 // starting to drag-resize a col
2637 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2642 // ------------ Left double click
2644 if ( event
.LeftDClick() )
2646 if ( XToEdgeOfCol(x
) < 0 )
2649 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2654 // ------------ Left button released
2656 else if ( event
.LeftUp() )
2658 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2660 DoEndDragResizeCol();
2662 // Note: we are ending the event *after* doing
2663 // default processing in this case
2665 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2668 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2673 // ------------ Right button down
2675 else if ( event
.RightDown() )
2678 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2680 // no default action at the moment
2685 // ------------ Right double click
2687 else if ( event
.RightDClick() )
2690 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2692 // no default action at the moment
2697 // ------------ No buttons down and mouse moving
2699 else if ( event
.Moving() )
2701 m_dragRowOrCol
= XToEdgeOfCol( x
);
2702 if ( m_dragRowOrCol
>= 0 )
2704 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2706 // don't capture the cursor yet
2707 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2710 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2712 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2718 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2720 if ( event
.LeftDown() )
2722 // indicate corner label by having both row and
2725 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2731 else if ( event
.LeftDClick() )
2733 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2736 else if ( event
.RightDown() )
2738 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2740 // no default action at the moment
2744 else if ( event
.RightDClick() )
2746 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2748 // no default action at the moment
2753 void wxGrid::ChangeCursorMode(CursorMode mode
,
2758 static const wxChar
*cursorModes
[] =
2767 wxLogTrace(_T("grid"),
2768 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2769 win
== m_colLabelWin
? _T("colLabelWin")
2770 : win
? _T("rowLabelWin")
2772 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2773 #endif // __WXDEBUG__
2775 if ( mode
== m_cursorMode
)
2780 // by default use the grid itself
2786 m_winCapture
->ReleaseMouse();
2787 m_winCapture
= (wxWindow
*)NULL
;
2790 m_cursorMode
= mode
;
2792 switch ( m_cursorMode
)
2794 case WXGRID_CURSOR_RESIZE_ROW
:
2795 win
->SetCursor( m_rowResizeCursor
);
2798 case WXGRID_CURSOR_RESIZE_COL
:
2799 win
->SetCursor( m_colResizeCursor
);
2803 win
->SetCursor( *wxSTANDARD_CURSOR
);
2806 // we need to capture mouse when resizing
2807 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2808 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2810 if ( captureMouse
&& resize
)
2812 win
->CaptureMouse();
2817 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2820 wxPoint
pos( event
.GetPosition() );
2821 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2823 wxGridCellCoords coords
;
2824 XYToCell( x
, y
, coords
);
2826 if ( event
.Dragging() )
2828 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
2830 // Don't start doing anything until the mouse has been drug at
2831 // least 3 pixels in any direction...
2832 if (! m_isDragging
) {
2833 if (m_startDragPos
== wxDefaultPosition
) {
2834 m_startDragPos
= pos
;
2837 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
2841 m_isDragging
= TRUE
;
2842 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2844 // Hide the edit control, so it
2845 // won't interfer with drag-shrinking.
2846 if ( IsCellEditControlEnabled() )
2847 HideCellEditControl();
2849 // Have we captured the mouse yet?
2850 if (! m_winCapture
) {
2851 m_winCapture
= m_gridWin
;
2852 m_winCapture
->CaptureMouse();
2855 if ( coords
!= wxGridNoCellCoords
)
2857 if ( !IsSelection() )
2859 SelectBlock( coords
, coords
);
2863 SelectBlock( m_currentCellCoords
, coords
);
2866 if (! IsVisible(coords
)) {
2867 MakeCellVisible(coords
);
2868 // TODO: need to introduce a delay or something here. The
2869 // scrolling is way to fast, at least on MSW.
2873 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2875 int cw
, ch
, left
, dummy
;
2876 m_gridWin
->GetClientSize( &cw
, &ch
);
2877 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2879 wxClientDC
dc( m_gridWin
);
2882 m_rowBottoms
[m_dragRowOrCol
] -
2883 m_rowHeights
[m_dragRowOrCol
] +
2884 WXGRID_MIN_ROW_HEIGHT
);
2885 dc
.SetLogicalFunction(wxINVERT
);
2886 if ( m_dragLastPos
>= 0 )
2888 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2890 dc
.DrawLine( left
, y
, left
+cw
, y
);
2893 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2895 int cw
, ch
, dummy
, top
;
2896 m_gridWin
->GetClientSize( &cw
, &ch
);
2897 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2899 wxClientDC
dc( m_gridWin
);
2902 m_colRights
[m_dragRowOrCol
] -
2903 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
2904 dc
.SetLogicalFunction(wxINVERT
);
2905 if ( m_dragLastPos
>= 0 )
2907 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2909 dc
.DrawLine( x
, top
, x
, top
+ch
);
2916 m_isDragging
= FALSE
;
2917 m_startDragPos
= wxDefaultPosition
;
2920 if ( coords
!= wxGridNoCellCoords
)
2922 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2923 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2926 if ( event
.Entering() || event
.Leaving() )
2928 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2929 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2934 // ------------ Left button pressed
2936 if ( event
.LeftDown() )
2938 EnableCellEditControl( FALSE
);
2939 if ( event
.ShiftDown() )
2941 SelectBlock( m_currentCellCoords
, coords
);
2943 else if ( XToEdgeOfCol(x
) < 0 &&
2944 YToEdgeOfRow(y
) < 0 )
2946 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2951 MakeCellVisible( coords
);
2953 // if this is the second click on this cell then start
2955 if (m_waitForSlowClick
&& coords
== m_currentCellCoords
) {
2956 EnableCellEditControl(TRUE
);
2957 // VZ: this is done by the call above, so why do it
2958 // again? please remove this line if it's ok
2959 //ShowCellEditControl();
2960 m_waitForSlowClick
= FALSE
;
2963 SetCurrentCell( coords
);
2964 m_waitForSlowClick
= TRUE
;
2971 // ------------ Left double click
2973 else if ( event
.LeftDClick() )
2975 EnableCellEditControl( FALSE
);
2976 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2978 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2986 // ------------ Left button released
2988 else if ( event
.LeftUp() )
2990 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2992 if ( IsSelection() )
2995 m_winCapture
->ReleaseMouse();
2996 m_winCapture
= NULL
;
2998 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
3001 // Show the edit control, if it has been hidden for
3003 ShowCellEditControl();
3005 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3007 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3008 DoEndDragResizeRow();
3010 // Note: we are ending the event *after* doing
3011 // default processing in this case
3013 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3015 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3017 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3018 DoEndDragResizeCol();
3020 // Note: we are ending the event *after* doing
3021 // default processing in this case
3023 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3030 // ------------ Right button down
3032 else if ( event
.RightDown() )
3034 EnableCellEditControl( FALSE
);
3035 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
3040 // no default action at the moment
3045 // ------------ Right double click
3047 else if ( event
.RightDClick() )
3049 EnableCellEditControl( FALSE
);
3050 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
3055 // no default action at the moment
3059 // ------------ Moving and no button action
3061 else if ( event
.Moving() && !event
.IsButton() )
3063 int dragRow
= YToEdgeOfRow( y
);
3064 int dragCol
= XToEdgeOfCol( x
);
3066 // Dragging on the corner of a cell to resize in both
3067 // directions is not implemented yet...
3069 if ( dragRow
>= 0 && dragCol
>= 0 )
3071 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3077 m_dragRowOrCol
= dragRow
;
3079 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3081 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3089 m_dragRowOrCol
= dragCol
;
3091 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3093 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3099 // Neither on a row or col edge
3101 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3103 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3110 void wxGrid::DoEndDragResizeRow()
3112 if ( m_dragLastPos
>= 0 )
3114 // erase the last line and resize the row
3116 int cw
, ch
, left
, dummy
;
3117 m_gridWin
->GetClientSize( &cw
, &ch
);
3118 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3120 wxClientDC
dc( m_gridWin
);
3122 dc
.SetLogicalFunction( wxINVERT
);
3123 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3124 HideCellEditControl();
3126 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3127 SetRowSize( m_dragRowOrCol
,
3128 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3130 if ( !GetBatchCount() )
3132 // Only needed to get the correct rect.y:
3133 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3135 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3136 rect
.width
= m_rowLabelWidth
;
3137 rect
.height
= ch
- rect
.y
;
3138 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3140 m_gridWin
->Refresh( FALSE
, &rect
);
3143 ShowCellEditControl();
3148 void wxGrid::DoEndDragResizeCol()
3150 if ( m_dragLastPos
>= 0 )
3152 // erase the last line and resize the col
3154 int cw
, ch
, dummy
, top
;
3155 m_gridWin
->GetClientSize( &cw
, &ch
);
3156 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3158 wxClientDC
dc( m_gridWin
);
3160 dc
.SetLogicalFunction( wxINVERT
);
3161 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3162 HideCellEditControl();
3164 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3165 SetColSize( m_dragRowOrCol
,
3166 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3168 if ( !GetBatchCount() )
3170 // Only needed to get the correct rect.x:
3171 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3173 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3174 rect
.width
= cw
- rect
.x
;
3175 rect
.height
= m_colLabelHeight
;
3176 m_colLabelWin
->Refresh( TRUE
, &rect
);
3178 m_gridWin
->Refresh( FALSE
, &rect
);
3181 ShowCellEditControl();
3188 // ------ interaction with data model
3190 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3192 switch ( msg
.GetId() )
3194 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3195 return GetModelValues();
3197 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3198 return SetModelValues();
3200 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3201 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3202 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3203 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3204 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3205 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3206 return Redimension( msg
);
3215 // The behaviour of this function depends on the grid table class
3216 // Clear() function. For the default wxGridStringTable class the
3217 // behavious is to replace all cell contents with wxEmptyString but
3218 // not to change the number of rows or cols.
3220 void wxGrid::ClearGrid()
3225 SetEditControlValue();
3226 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3231 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3233 // TODO: something with updateLabels flag
3237 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3243 if (IsCellEditControlEnabled())
3244 EnableCellEditControl(FALSE
);
3246 bool ok
= m_table
->InsertRows( pos
, numRows
);
3248 // the table will have sent the results of the insert row
3249 // operation to this view object as a grid table message
3253 if ( m_numCols
== 0 )
3255 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3257 // TODO: perhaps instead of appending the default number of cols
3258 // we should remember what the last non-zero number of cols was ?
3262 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3264 // if we have just inserted cols into an empty grid the current
3265 // cell will be undefined...
3267 SetCurrentCell( 0, 0 );
3271 if ( !GetBatchCount() ) Refresh();
3274 SetEditControlValue();
3284 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3286 // TODO: something with updateLabels flag
3290 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3294 if ( m_table
&& m_table
->AppendRows( numRows
) )
3296 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3298 // if we have just inserted cols into an empty grid the current
3299 // cell will be undefined...
3301 SetCurrentCell( 0, 0 );
3304 // the table will have sent the results of the append row
3305 // operation to this view object as a grid table message
3308 if ( !GetBatchCount() ) Refresh();
3318 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3320 // TODO: something with updateLabels flag
3324 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3330 if (IsCellEditControlEnabled())
3331 EnableCellEditControl(FALSE
);
3333 if (m_table
->DeleteRows( pos
, numRows
))
3336 // the table will have sent the results of the delete row
3337 // operation to this view object as a grid table message
3340 if ( !GetBatchCount() ) Refresh();
3348 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3350 // TODO: something with updateLabels flag
3354 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3360 if (IsCellEditControlEnabled())
3361 EnableCellEditControl(FALSE
);
3363 bool ok
= m_table
->InsertCols( pos
, numCols
);
3365 // the table will have sent the results of the insert col
3366 // operation to this view object as a grid table message
3370 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3372 // if we have just inserted cols into an empty grid the current
3373 // cell will be undefined...
3375 SetCurrentCell( 0, 0 );
3379 if ( !GetBatchCount() ) Refresh();
3382 SetEditControlValue();
3392 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3394 // TODO: something with updateLabels flag
3398 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3402 if ( m_table
&& m_table
->AppendCols( numCols
) )
3404 // the table will have sent the results of the append col
3405 // operation to this view object as a grid table message
3407 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3409 // if we have just inserted cols into an empty grid the current
3410 // cell will be undefined...
3412 SetCurrentCell( 0, 0 );
3416 if ( !GetBatchCount() ) Refresh();
3426 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3428 // TODO: something with updateLabels flag
3432 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3438 if (IsCellEditControlEnabled())
3439 EnableCellEditControl(FALSE
);
3441 if ( m_table
->DeleteCols( pos
, numCols
) )
3443 // the table will have sent the results of the delete col
3444 // operation to this view object as a grid table message
3447 if ( !GetBatchCount() ) Refresh();
3457 // ----- event handlers
3460 // Generate a grid event based on a mouse event and
3461 // return the result of ProcessEvent()
3463 bool wxGrid::SendEvent( const wxEventType type
,
3465 wxMouseEvent
& mouseEv
)
3467 if ( type
== EVT_GRID_ROW_SIZE
||
3468 type
== EVT_GRID_COL_SIZE
)
3470 int rowOrCol
= (row
== -1 ? col
: row
);
3472 wxGridSizeEvent
gridEvt( GetId(),
3476 mouseEv
.GetX(), mouseEv
.GetY(),
3477 mouseEv
.ControlDown(),
3478 mouseEv
.ShiftDown(),
3480 mouseEv
.MetaDown() );
3482 return GetEventHandler()->ProcessEvent(gridEvt
);
3484 else if ( type
== EVT_GRID_RANGE_SELECT
)
3486 wxGridRangeSelectEvent
gridEvt( GetId(),
3490 m_selectedBottomRight
,
3491 mouseEv
.ControlDown(),
3492 mouseEv
.ShiftDown(),
3494 mouseEv
.MetaDown() );
3496 return GetEventHandler()->ProcessEvent(gridEvt
);
3500 wxGridEvent
gridEvt( GetId(),
3504 mouseEv
.GetX(), mouseEv
.GetY(),
3505 mouseEv
.ControlDown(),
3506 mouseEv
.ShiftDown(),
3508 mouseEv
.MetaDown() );
3510 return GetEventHandler()->ProcessEvent(gridEvt
);
3515 // Generate a grid event of specified type and return the result
3516 // of ProcessEvent().
3518 bool wxGrid::SendEvent( const wxEventType type
,
3521 if ( type
== EVT_GRID_ROW_SIZE
||
3522 type
== EVT_GRID_COL_SIZE
)
3524 int rowOrCol
= (row
== -1 ? col
: row
);
3526 wxGridSizeEvent
gridEvt( GetId(),
3531 return GetEventHandler()->ProcessEvent(gridEvt
);
3535 wxGridEvent
gridEvt( GetId(),
3540 return GetEventHandler()->ProcessEvent(gridEvt
);
3545 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3547 wxPaintDC
dc( this );
3549 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3550 m_numRows
&& m_numCols
)
3552 m_currentCellCoords
.Set(0, 0);
3553 SetEditControlValue();
3554 ShowCellEditControl();
3561 // This is just here to make sure that CalcDimensions gets called when
3562 // the grid view is resized... then the size event is skipped to allow
3563 // the box sizers to handle everything
3565 void wxGrid::OnSize( wxSizeEvent
& event
)
3572 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3574 if ( m_inOnKeyDown
)
3576 // shouldn't be here - we are going round in circles...
3578 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3581 m_inOnKeyDown
= TRUE
;
3583 // propagate the event up and see if it gets processed
3585 wxWindow
*parent
= GetParent();
3586 wxKeyEvent
keyEvt( event
);
3587 keyEvt
.SetEventObject( parent
);
3589 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3592 // TODO: Should also support Shift-cursor keys for
3593 // extending the selection. Maybe add a flag to
3594 // MoveCursorXXX() and MoveCursorXXXBlock() and
3595 // just send event.ShiftDown().
3597 // try local handlers
3599 switch ( event
.KeyCode() )
3602 if ( event
.ControlDown() )
3604 MoveCursorUpBlock();
3613 if ( event
.ControlDown() )
3615 MoveCursorDownBlock();
3624 if ( event
.ControlDown() )
3626 MoveCursorLeftBlock();
3635 if ( event
.ControlDown() )
3637 MoveCursorRightBlock();
3646 if ( event
.ControlDown() )
3648 event
.Skip(); // to let the edit control have the return
3657 if (event
.ShiftDown())
3664 if ( event
.ControlDown() )
3666 MakeCellVisible( 0, 0 );
3667 SetCurrentCell( 0, 0 );
3676 if ( event
.ControlDown() )
3678 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3679 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3695 // We don't want these keys to trigger the edit control, any others?
3704 if ( !IsEditable() )
3709 // Otherwise fall through to default
3712 // now try the cell edit control
3714 if ( !IsCellEditControlEnabled() )
3715 EnableCellEditControl( TRUE
);
3716 if (IsCellEditControlEnabled()) {
3717 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3718 attr
->GetEditor()->StartingKey(event
);
3725 m_inOnKeyDown
= FALSE
;
3729 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3735 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3737 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3739 // the event has been intercepted - do nothing
3744 m_currentCellCoords
!= wxGridNoCellCoords
)
3746 HideCellEditControl();
3747 SaveEditControlValue();
3748 EnableCellEditControl(FALSE
);
3750 // Clear the old current cell highlight
3751 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3753 // Otherwise refresh redraws the highlight!
3754 m_currentCellCoords
= coords
;
3756 m_gridWin
->Refresh( FALSE
, &r
);
3759 m_currentCellCoords
= coords
;
3761 SetEditControlValue();
3765 wxClientDC
dc(m_gridWin
);
3767 DrawCellHighlight(dc
);
3769 if ( IsSelection() )
3771 wxRect
r( SelectionToDeviceRect() );
3773 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3780 // ------ functions to get/send data (see also public functions)
3783 bool wxGrid::GetModelValues()
3787 // all we need to do is repaint the grid
3789 m_gridWin
->Refresh();
3797 bool wxGrid::SetModelValues()
3803 for ( row
= 0; row
< m_numRows
; row
++ )
3805 for ( col
= 0; col
< m_numCols
; col
++ )
3807 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3819 // Note - this function only draws cells that are in the list of
3820 // exposed cells (usually set from the update region by
3821 // CalcExposedCells)
3823 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3825 if ( !m_numRows
|| !m_numCols
) return;
3828 size_t numCells
= m_cellsExposed
.GetCount();
3830 for ( i
= 0; i
< numCells
; i
++ )
3832 DrawCell( dc
, m_cellsExposed
[i
] );
3837 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3839 int row
= coords
.GetRow();
3840 int col
= coords
.GetCol();
3842 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3845 // we draw the cell border ourselves
3846 #if !WXGRID_DRAW_LINES
3847 if ( m_gridLinesEnabled
)
3848 DrawCellBorder( dc
, coords
);
3851 // but all the rest is drawn by the cell renderer and hence may be
3854 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3855 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3856 rect
.width
= m_colWidths
[col
]-1;
3857 rect
.height
= m_rowHeights
[row
]-1;
3859 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3860 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3863 if (m_currentCellCoords
== coords
)
3864 DrawCellHighlight(dc
);
3868 void wxGrid::DrawCellHighlight( wxDC
& dc
)
3870 int row
= m_currentCellCoords
.GetRow();
3871 int col
= m_currentCellCoords
.GetCol();
3873 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3877 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3878 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3879 rect
.width
= m_colWidths
[col
] - 1;
3880 rect
.height
= m_rowHeights
[row
] - 1;
3882 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
3883 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3885 dc
.DrawRectangle(rect
);
3888 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3890 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3891 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3893 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3894 int row
= coords
.GetRow();
3895 int col
= coords
.GetCol();
3897 // right hand border
3899 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3900 m_colRights
[col
], m_rowBottoms
[row
] );
3904 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3905 m_colRights
[col
], m_rowBottoms
[row
] );
3909 // TODO: remove this ???
3910 // This is used to redraw all grid lines e.g. when the grid line colour
3913 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3915 if ( !m_gridLinesEnabled
||
3917 !m_numCols
) return;
3919 int top
, bottom
, left
, right
;
3923 m_gridWin
->GetClientSize(&cw
, &ch
);
3925 // virtual coords of visible area
3927 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3928 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3932 reg
.GetBox(x
, y
, w
, h
);
3933 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3934 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3937 // avoid drawing grid lines past the last row and col
3939 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3940 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3942 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3944 // horizontal grid lines
3947 for ( i
= 0; i
< m_numRows
; i
++ )
3949 if ( m_rowBottoms
[i
]-1 > bottom
)
3953 else if ( m_rowBottoms
[i
]-1 >= top
)
3955 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
3960 // vertical grid lines
3962 for ( i
= 0; i
< m_numCols
; i
++ )
3964 if ( m_colRights
[i
]-1 > right
)
3968 else if ( m_colRights
[i
]-1 >= left
)
3970 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
3976 void wxGrid::DrawRowLabels( wxDC
& dc
)
3978 if ( !m_numRows
|| !m_numCols
) return;
3981 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3983 for ( i
= 0; i
< numLabels
; i
++ )
3985 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3990 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3992 if ( m_rowHeights
[row
] <= 0 ) return;
3994 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3996 dc
.SetPen( *wxBLACK_PEN
);
3997 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3998 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4000 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4001 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4003 dc
.SetPen( *wxWHITE_PEN
);
4004 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4005 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4007 dc
.SetBackgroundMode( wxTRANSPARENT
);
4008 dc
.SetTextForeground( GetLabelTextColour() );
4009 dc
.SetFont( GetLabelFont() );
4012 GetRowLabelAlignment( &hAlign
, &vAlign
);
4016 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4017 rect
.SetWidth( m_rowLabelWidth
- 4 );
4018 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4019 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4023 void wxGrid::DrawColLabels( wxDC
& dc
)
4025 if ( !m_numRows
|| !m_numCols
) return;
4028 size_t numLabels
= m_colLabelsExposed
.GetCount();
4030 for ( i
= 0; i
< numLabels
; i
++ )
4032 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4037 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4039 if ( m_colWidths
[col
] <= 0 ) return;
4041 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4043 dc
.SetPen( *wxBLACK_PEN
);
4044 dc
.DrawLine( m_colRights
[col
]-1, 0,
4045 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4047 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4048 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4050 dc
.SetPen( *wxWHITE_PEN
);
4051 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4052 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4054 dc
.SetBackgroundMode( wxTRANSPARENT
);
4055 dc
.SetTextForeground( GetLabelTextColour() );
4056 dc
.SetFont( GetLabelFont() );
4058 dc
.SetBackgroundMode( wxTRANSPARENT
);
4059 dc
.SetTextForeground( GetLabelTextColour() );
4060 dc
.SetFont( GetLabelFont() );
4063 GetColLabelAlignment( &hAlign
, &vAlign
);
4066 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4068 rect
.SetWidth( m_colWidths
[col
] - 4 );
4069 rect
.SetHeight( m_colLabelHeight
- 4 );
4070 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4074 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4075 const wxString
& value
,
4080 long textWidth
, textHeight
;
4081 long lineWidth
, lineHeight
;
4082 wxArrayString lines
;
4084 dc
.SetClippingRegion( rect
);
4085 StringToLines( value
, lines
);
4086 if ( lines
.GetCount() )
4088 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4089 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4092 switch ( horizAlign
)
4095 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4099 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4108 switch ( vertAlign
)
4111 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4115 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4124 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4126 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4131 dc
.DestroyClippingRegion();
4135 // Split multi line text up into an array of strings. Any existing
4136 // contents of the string array are preserved.
4138 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4142 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4143 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4145 while ( startPos
< (int)tVal
.Length() )
4147 pos
= tVal
.Mid(startPos
).Find( eol
);
4152 else if ( pos
== 0 )
4154 lines
.Add( wxEmptyString
);
4158 lines
.Add( value
.Mid(startPos
, pos
) );
4162 if ( startPos
< (int)value
.Length() )
4164 lines
.Add( value
.Mid( startPos
) );
4169 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4170 wxArrayString
& lines
,
4171 long *width
, long *height
)
4178 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4180 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4181 w
= wxMax( w
, lineW
);
4191 // ------ Edit control functions
4195 void wxGrid::EnableEditing( bool edit
)
4197 // TODO: improve this ?
4199 if ( edit
!= m_editable
)
4203 EnableCellEditControl(m_editable
);
4208 void wxGrid::EnableCellEditControl( bool enable
)
4213 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4214 SetCurrentCell( 0, 0 );
4216 if ( enable
!= m_cellEditCtrlEnabled
)
4220 m_cellEditCtrlEnabled
= enable
;
4221 SetEditControlValue();
4222 ShowCellEditControl();
4226 HideCellEditControl();
4227 SaveEditControlValue();
4228 m_cellEditCtrlEnabled
= enable
;
4234 void wxGrid::ShowCellEditControl()
4236 if ( IsCellEditControlEnabled() )
4238 if ( !IsVisible( m_currentCellCoords
) )
4244 wxRect rect
= CellToRect( m_currentCellCoords
);
4245 int row
= m_currentCellCoords
.GetRow();
4246 int col
= m_currentCellCoords
.GetCol();
4248 // convert to scrolled coords
4250 int left
, top
, right
, bottom
;
4251 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4252 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4253 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4255 m_gridWin
->GetClientSize( &cw
, &ch
);
4257 // Make the edit control large enough to allow for internal
4260 // TODO: remove this if the text ctrl sizing is improved esp. for
4264 #if defined(__WXMOTIF__)
4265 if ( row
== 0 || col
== 0 )
4274 if ( row
== 0 || col
== 0 )
4284 #if defined(__WXGTK__)
4287 if (left
!= 0) left_diff
++;
4288 if (top
!= 0) top_diff
++;
4289 rect
.SetLeft( left
+ left_diff
);
4290 rect
.SetTop( top
+ top_diff
);
4291 rect
.SetRight( rect
.GetRight() - left_diff
);
4292 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4294 rect
.SetLeft( wxMax(0, left
- extra
) );
4295 rect
.SetTop( wxMax(0, top
- extra
) );
4296 rect
.SetRight( rect
.GetRight() + 2*extra
);
4297 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4300 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4301 wxGridCellEditor
* editor
= attr
->GetEditor();
4302 if ( !editor
->IsCreated() )
4304 editor
->Create(m_gridWin
, -1,
4305 new wxGridCellEditorEvtHandler(this, editor
));
4308 editor
->SetSize( rect
);
4309 editor
->Show( TRUE
, attr
);
4310 editor
->BeginEdit(row
, col
, this);
4317 void wxGrid::HideCellEditControl()
4319 if ( IsCellEditControlEnabled() )
4321 int row
= m_currentCellCoords
.GetRow();
4322 int col
= m_currentCellCoords
.GetCol();
4324 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4325 attr
->GetEditor()->Show( FALSE
);
4327 m_gridWin
->SetFocus();
4332 void wxGrid::SetEditControlValue( const wxString
& value
)
4334 // RD: The new Editors get the value from the table themselves now. This
4335 // method can probably be removed...
4339 void wxGrid::SaveEditControlValue()
4341 if ( IsCellEditControlEnabled() )
4343 int row
= m_currentCellCoords
.GetRow();
4344 int col
= m_currentCellCoords
.GetCol();
4346 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4347 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4353 SendEvent( EVT_GRID_CELL_CHANGE
,
4354 m_currentCellCoords
.GetRow(),
4355 m_currentCellCoords
.GetCol() );
4362 // ------ Grid location functions
4363 // Note that all of these functions work with the logical coordinates of
4364 // grid cells and labels so you will need to convert from device
4365 // coordinates for mouse events etc.
4368 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4370 int row
= YToRow(y
);
4371 int col
= XToCol(x
);
4373 if ( row
== -1 || col
== -1 )
4375 coords
= wxGridNoCellCoords
;
4379 coords
.Set( row
, col
);
4384 int wxGrid::YToRow( int y
)
4388 for ( i
= 0; i
< m_numRows
; i
++ )
4390 if ( y
< m_rowBottoms
[i
] ) return i
;
4393 return m_numRows
; //-1;
4397 int wxGrid::XToCol( int x
)
4401 for ( i
= 0; i
< m_numCols
; i
++ )
4403 if ( x
< m_colRights
[i
] ) return i
;
4406 return m_numCols
; //-1;
4410 // return the row number that that the y coord is near the edge of, or
4411 // -1 if not near an edge
4413 int wxGrid::YToEdgeOfRow( int y
)
4417 for ( i
= 0; i
< m_numRows
; i
++ )
4419 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4421 d
= abs( y
- m_rowBottoms
[i
] );
4423 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4432 // return the col number that that the x coord is near the edge of, or
4433 // -1 if not near an edge
4435 int wxGrid::XToEdgeOfCol( int x
)
4439 for ( i
= 0; i
< m_numCols
; i
++ )
4441 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4443 d
= abs( x
- m_colRights
[i
] );
4445 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4454 wxRect
wxGrid::CellToRect( int row
, int col
)
4456 wxRect
rect( -1, -1, -1, -1 );
4458 if ( row
>= 0 && row
< m_numRows
&&
4459 col
>= 0 && col
< m_numCols
)
4461 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4462 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4463 rect
.width
= m_colWidths
[col
];
4464 rect
.height
= m_rowHeights
[ row
];
4471 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4473 // get the cell rectangle in logical coords
4475 wxRect
r( CellToRect( row
, col
) );
4477 // convert to device coords
4479 int left
, top
, right
, bottom
;
4480 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4481 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4483 // check against the client area of the grid window
4486 m_gridWin
->GetClientSize( &cw
, &ch
);
4488 if ( wholeCellVisible
)
4490 // is the cell wholly visible ?
4492 return ( left
>= 0 && right
<= cw
&&
4493 top
>= 0 && bottom
<= ch
);
4497 // is the cell partly visible ?
4499 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4500 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4505 // make the specified cell location visible by doing a minimal amount
4508 void wxGrid::MakeCellVisible( int row
, int col
)
4511 int xpos
= -1, ypos
= -1;
4513 if ( row
>= 0 && row
< m_numRows
&&
4514 col
>= 0 && col
< m_numCols
)
4516 // get the cell rectangle in logical coords
4518 wxRect
r( CellToRect( row
, col
) );
4520 // convert to device coords
4522 int left
, top
, right
, bottom
;
4523 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4524 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4527 m_gridWin
->GetClientSize( &cw
, &ch
);
4533 else if ( bottom
> ch
)
4535 int h
= r
.GetHeight();
4537 for ( i
= row
-1; i
>= 0; i
-- )
4539 if ( h
+ m_rowHeights
[i
] > ch
) break;
4541 h
+= m_rowHeights
[i
];
4542 ypos
-= m_rowHeights
[i
];
4545 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4546 // have rounding errors (this is important, because if we do, we
4547 // might not scroll at all and some cells won't be redrawn)
4548 ypos
+= GRID_SCROLL_LINE
/ 2;
4555 else if ( right
> cw
)
4557 int w
= r
.GetWidth();
4559 for ( i
= col
-1; i
>= 0; i
-- )
4561 if ( w
+ m_colWidths
[i
] > cw
) break;
4563 w
+= m_colWidths
[i
];
4564 xpos
-= m_colWidths
[i
];
4567 // see comment for ypos above
4568 xpos
+= GRID_SCROLL_LINE
/ 2;
4571 if ( xpos
!= -1 || ypos
!= -1 )
4573 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4574 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4575 Scroll( xpos
, ypos
);
4583 // ------ Grid cursor movement functions
4586 bool wxGrid::MoveCursorUp()
4588 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4589 m_currentCellCoords
.GetRow() > 0 )
4591 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4592 m_currentCellCoords
.GetCol() );
4594 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4595 m_currentCellCoords
.GetCol() );
4604 bool wxGrid::MoveCursorDown()
4606 // TODO: allow for scrolling
4608 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4609 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4611 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4612 m_currentCellCoords
.GetCol() );
4614 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4615 m_currentCellCoords
.GetCol() );
4624 bool wxGrid::MoveCursorLeft()
4626 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4627 m_currentCellCoords
.GetCol() > 0 )
4629 MakeCellVisible( m_currentCellCoords
.GetRow(),
4630 m_currentCellCoords
.GetCol() - 1 );
4632 SetCurrentCell( m_currentCellCoords
.GetRow(),
4633 m_currentCellCoords
.GetCol() - 1 );
4642 bool wxGrid::MoveCursorRight()
4644 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4645 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4647 MakeCellVisible( m_currentCellCoords
.GetRow(),
4648 m_currentCellCoords
.GetCol() + 1 );
4650 SetCurrentCell( m_currentCellCoords
.GetRow(),
4651 m_currentCellCoords
.GetCol() + 1 );
4660 bool wxGrid::MovePageUp()
4662 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4664 int row
= m_currentCellCoords
.GetRow();
4668 m_gridWin
->GetClientSize( &cw
, &ch
);
4670 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4671 int newRow
= YToRow( y
- ch
+ 1 );
4676 else if ( newRow
== row
)
4681 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4682 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4690 bool wxGrid::MovePageDown()
4692 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4694 int row
= m_currentCellCoords
.GetRow();
4695 if ( row
< m_numRows
)
4698 m_gridWin
->GetClientSize( &cw
, &ch
);
4700 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4701 int newRow
= YToRow( y
+ ch
);
4704 newRow
= m_numRows
- 1;
4706 else if ( newRow
== row
)
4711 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4712 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4720 bool wxGrid::MoveCursorUpBlock()
4723 m_currentCellCoords
!= wxGridNoCellCoords
&&
4724 m_currentCellCoords
.GetRow() > 0 )
4726 int row
= m_currentCellCoords
.GetRow();
4727 int col
= m_currentCellCoords
.GetCol();
4729 if ( m_table
->IsEmptyCell(row
, col
) )
4731 // starting in an empty cell: find the next block of
4737 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4740 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4742 // starting at the top of a block: find the next block
4748 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4753 // starting within a block: find the top of the block
4758 if ( m_table
->IsEmptyCell(row
, col
) )
4766 MakeCellVisible( row
, col
);
4767 SetCurrentCell( row
, col
);
4775 bool wxGrid::MoveCursorDownBlock()
4778 m_currentCellCoords
!= wxGridNoCellCoords
&&
4779 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4781 int row
= m_currentCellCoords
.GetRow();
4782 int col
= m_currentCellCoords
.GetCol();
4784 if ( m_table
->IsEmptyCell(row
, col
) )
4786 // starting in an empty cell: find the next block of
4789 while ( row
< m_numRows
-1 )
4792 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4795 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4797 // starting at the bottom of a block: find the next block
4800 while ( row
< m_numRows
-1 )
4803 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4808 // starting within a block: find the bottom of the block
4810 while ( row
< m_numRows
-1 )
4813 if ( m_table
->IsEmptyCell(row
, col
) )
4821 MakeCellVisible( row
, col
);
4822 SetCurrentCell( row
, col
);
4830 bool wxGrid::MoveCursorLeftBlock()
4833 m_currentCellCoords
!= wxGridNoCellCoords
&&
4834 m_currentCellCoords
.GetCol() > 0 )
4836 int row
= m_currentCellCoords
.GetRow();
4837 int col
= m_currentCellCoords
.GetCol();
4839 if ( m_table
->IsEmptyCell(row
, col
) )
4841 // starting in an empty cell: find the next block of
4847 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4850 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4852 // starting at the left of a block: find the next block
4858 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4863 // starting within a block: find the left of the block
4868 if ( m_table
->IsEmptyCell(row
, col
) )
4876 MakeCellVisible( row
, col
);
4877 SetCurrentCell( row
, col
);
4885 bool wxGrid::MoveCursorRightBlock()
4888 m_currentCellCoords
!= wxGridNoCellCoords
&&
4889 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4891 int row
= m_currentCellCoords
.GetRow();
4892 int col
= m_currentCellCoords
.GetCol();
4894 if ( m_table
->IsEmptyCell(row
, col
) )
4896 // starting in an empty cell: find the next block of
4899 while ( col
< m_numCols
-1 )
4902 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4905 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4907 // starting at the right of a block: find the next block
4910 while ( col
< m_numCols
-1 )
4913 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4918 // starting within a block: find the right of the block
4920 while ( col
< m_numCols
-1 )
4923 if ( m_table
->IsEmptyCell(row
, col
) )
4931 MakeCellVisible( row
, col
);
4932 SetCurrentCell( row
, col
);
4943 // ------ Label values and formatting
4946 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4948 *horiz
= m_rowLabelHorizAlign
;
4949 *vert
= m_rowLabelVertAlign
;
4952 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4954 *horiz
= m_colLabelHorizAlign
;
4955 *vert
= m_colLabelVertAlign
;
4958 wxString
wxGrid::GetRowLabelValue( int row
)
4962 return m_table
->GetRowLabelValue( row
);
4972 wxString
wxGrid::GetColLabelValue( int col
)
4976 return m_table
->GetColLabelValue( col
);
4987 void wxGrid::SetRowLabelSize( int width
)
4989 width
= wxMax( width
, 0 );
4990 if ( width
!= m_rowLabelWidth
)
4994 m_rowLabelWin
->Show( FALSE
);
4995 m_cornerLabelWin
->Show( FALSE
);
4997 else if ( m_rowLabelWidth
== 0 )
4999 m_rowLabelWin
->Show( TRUE
);
5000 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5003 m_rowLabelWidth
= width
;
5010 void wxGrid::SetColLabelSize( int height
)
5012 height
= wxMax( height
, 0 );
5013 if ( height
!= m_colLabelHeight
)
5017 m_colLabelWin
->Show( FALSE
);
5018 m_cornerLabelWin
->Show( FALSE
);
5020 else if ( m_colLabelHeight
== 0 )
5022 m_colLabelWin
->Show( TRUE
);
5023 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5026 m_colLabelHeight
= height
;
5033 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5035 if ( m_labelBackgroundColour
!= colour
)
5037 m_labelBackgroundColour
= colour
;
5038 m_rowLabelWin
->SetBackgroundColour( colour
);
5039 m_colLabelWin
->SetBackgroundColour( colour
);
5040 m_cornerLabelWin
->SetBackgroundColour( colour
);
5042 if ( !GetBatchCount() )
5044 m_rowLabelWin
->Refresh();
5045 m_colLabelWin
->Refresh();
5046 m_cornerLabelWin
->Refresh();
5051 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5053 if ( m_labelTextColour
!= colour
)
5055 m_labelTextColour
= colour
;
5056 if ( !GetBatchCount() )
5058 m_rowLabelWin
->Refresh();
5059 m_colLabelWin
->Refresh();
5064 void wxGrid::SetLabelFont( const wxFont
& font
)
5067 if ( !GetBatchCount() )
5069 m_rowLabelWin
->Refresh();
5070 m_colLabelWin
->Refresh();
5074 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5076 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5078 m_rowLabelHorizAlign
= horiz
;
5081 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5083 m_rowLabelVertAlign
= vert
;
5086 if ( !GetBatchCount() )
5088 m_rowLabelWin
->Refresh();
5092 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5094 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5096 m_colLabelHorizAlign
= horiz
;
5099 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5101 m_colLabelVertAlign
= vert
;
5104 if ( !GetBatchCount() )
5106 m_colLabelWin
->Refresh();
5110 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5114 m_table
->SetRowLabelValue( row
, s
);
5115 if ( !GetBatchCount() )
5117 wxRect rect
= CellToRect( row
, 0);
5118 if ( rect
.height
> 0 )
5120 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5122 rect
.width
= m_rowLabelWidth
;
5123 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5129 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5133 m_table
->SetColLabelValue( col
, s
);
5134 if ( !GetBatchCount() )
5136 wxRect rect
= CellToRect( 0, col
);
5137 if ( rect
.width
> 0 )
5139 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5141 rect
.height
= m_colLabelHeight
;
5142 m_colLabelWin
->Refresh( TRUE
, &rect
);
5148 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5150 if ( m_gridLineColour
!= colour
)
5152 m_gridLineColour
= colour
;
5154 wxClientDC
dc( m_gridWin
);
5156 DrawAllGridLines( dc
, wxRegion() );
5160 void wxGrid::EnableGridLines( bool enable
)
5162 if ( enable
!= m_gridLinesEnabled
)
5164 m_gridLinesEnabled
= enable
;
5166 if ( !GetBatchCount() )
5170 wxClientDC
dc( m_gridWin
);
5172 DrawAllGridLines( dc
, wxRegion() );
5176 m_gridWin
->Refresh();
5183 int wxGrid::GetDefaultRowSize()
5185 return m_defaultRowHeight
;
5188 int wxGrid::GetRowSize( int row
)
5190 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5192 return m_rowHeights
[row
];
5195 int wxGrid::GetDefaultColSize()
5197 return m_defaultColWidth
;
5200 int wxGrid::GetColSize( int col
)
5202 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5204 return m_colWidths
[col
];
5207 // ============================================================================
5208 // access to the grid attributes: each of them has a default value in the grid
5209 // itself and may be overidden on a per-cell basis
5210 // ============================================================================
5212 // ----------------------------------------------------------------------------
5213 // setting default attributes
5214 // ----------------------------------------------------------------------------
5216 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5218 m_defaultCellAttr
->SetBackgroundColour(col
);
5221 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5223 m_defaultCellAttr
->SetTextColour(col
);
5226 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5228 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5231 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5233 m_defaultCellAttr
->SetFont(font
);
5236 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5238 m_defaultCellAttr
->SetRenderer(renderer
);
5241 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5243 m_defaultCellAttr
->SetEditor(editor
);
5246 // ----------------------------------------------------------------------------
5247 // access to the default attrbiutes
5248 // ----------------------------------------------------------------------------
5250 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5252 return m_defaultCellAttr
->GetBackgroundColour();
5255 wxColour
wxGrid::GetDefaultCellTextColour()
5257 return m_defaultCellAttr
->GetTextColour();
5260 wxFont
wxGrid::GetDefaultCellFont()
5262 return m_defaultCellAttr
->GetFont();
5265 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5267 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5270 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5272 return m_defaultCellAttr
->GetRenderer();
5275 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5277 return m_defaultCellAttr
->GetEditor();
5280 // ----------------------------------------------------------------------------
5281 // access to cell attributes
5282 // ----------------------------------------------------------------------------
5284 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5286 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5287 wxColour colour
= attr
->GetBackgroundColour();
5292 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5294 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5295 wxColour colour
= attr
->GetTextColour();
5300 wxFont
wxGrid::GetCellFont( int row
, int col
)
5302 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5303 wxFont font
= attr
->GetFont();
5308 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5310 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5311 attr
->GetAlignment(horiz
, vert
);
5315 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5317 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5318 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5323 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5325 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5326 wxGridCellEditor
* editor
= attr
->GetEditor();
5331 // ----------------------------------------------------------------------------
5332 // attribute support: cache, automatic provider creation, ...
5333 // ----------------------------------------------------------------------------
5335 bool wxGrid::CanHaveAttributes()
5342 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5343 // table is providing the attributes itself??? In which case
5344 // I don't think the grid should create a Provider object for the
5345 // table but the table should be smart enough to do that on its own.
5346 if ( !m_table
->GetAttrProvider() )
5348 // use the default attr provider by default
5349 // (another choice would be to just return FALSE thus forcing the user
5351 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5357 void wxGrid::ClearAttrCache()
5359 if ( m_attrCache
.row
!= -1 )
5361 m_attrCache
.attr
->SafeDecRef();
5362 m_attrCache
.row
= -1;
5366 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5368 wxGrid
*self
= (wxGrid
*)this; // const_cast
5370 self
->ClearAttrCache();
5371 self
->m_attrCache
.row
= row
;
5372 self
->m_attrCache
.col
= col
;
5373 self
->m_attrCache
.attr
= attr
;
5377 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5379 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5381 *attr
= m_attrCache
.attr
;
5382 (*attr
)->SafeIncRef();
5384 #ifdef DEBUG_ATTR_CACHE
5385 gs_nAttrCacheHits
++;
5392 #ifdef DEBUG_ATTR_CACHE
5393 gs_nAttrCacheMisses
++;
5399 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5401 wxGridCellAttr
*attr
;
5402 if ( !LookupAttr(row
, col
, &attr
) )
5404 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5405 CacheAttr(row
, col
, attr
);
5408 attr
->SetDefAttr(m_defaultCellAttr
);
5410 attr
= m_defaultCellAttr
;
5417 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5419 wxGridCellAttr
*attr
;
5420 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5422 wxASSERT_MSG( m_table
,
5423 _T("we may only be called if CanHaveAttributes() "
5424 "returned TRUE and then m_table should be !NULL") );
5426 attr
= m_table
->GetAttr(row
, col
);
5429 attr
= new wxGridCellAttr
;
5431 // artificially inc the ref count to match DecRef() in caller
5434 m_table
->SetAttr(attr
, row
, col
);
5437 CacheAttr(row
, col
, attr
);
5439 attr
->SetDefAttr(m_defaultCellAttr
);
5443 // ----------------------------------------------------------------------------
5444 // setting cell attributes: this is forwarded to the table
5445 // ----------------------------------------------------------------------------
5447 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5449 if ( CanHaveAttributes() )
5451 m_table
->SetRowAttr(attr
, row
);
5459 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5461 if ( CanHaveAttributes() )
5463 m_table
->SetColAttr(attr
, col
);
5471 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5473 if ( CanHaveAttributes() )
5475 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5476 attr
->SetBackgroundColour(colour
);
5481 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5483 if ( CanHaveAttributes() )
5485 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5486 attr
->SetTextColour(colour
);
5491 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5493 if ( CanHaveAttributes() )
5495 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5496 attr
->SetFont(font
);
5501 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5503 if ( CanHaveAttributes() )
5505 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5506 attr
->SetAlignment(horiz
, vert
);
5511 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5513 if ( CanHaveAttributes() )
5515 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5516 attr
->SetRenderer(renderer
);
5521 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5523 if ( CanHaveAttributes() )
5525 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5526 attr
->SetEditor(editor
);
5531 // ----------------------------------------------------------------------------
5533 // ----------------------------------------------------------------------------
5535 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5537 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5539 if ( resizeExistingRows
)
5543 for ( row
= 0; row
< m_numRows
; row
++ )
5545 m_rowHeights
[row
] = m_defaultRowHeight
;
5546 bottom
+= m_defaultRowHeight
;
5547 m_rowBottoms
[row
] = bottom
;
5553 void wxGrid::SetRowSize( int row
, int height
)
5555 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5559 int h
= wxMax( 0, height
);
5560 int diff
= h
- m_rowHeights
[row
];
5562 m_rowHeights
[row
] = h
;
5563 for ( i
= row
; i
< m_numRows
; i
++ )
5565 m_rowBottoms
[i
] += diff
;
5570 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5572 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5574 if ( resizeExistingCols
)
5578 for ( col
= 0; col
< m_numCols
; col
++ )
5580 m_colWidths
[col
] = m_defaultColWidth
;
5581 right
+= m_defaultColWidth
;
5582 m_colRights
[col
] = right
;
5588 void wxGrid::SetColSize( int col
, int width
)
5590 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5594 int w
= wxMax( 0, width
);
5595 int diff
= w
- m_colWidths
[col
];
5596 m_colWidths
[col
] = w
;
5598 for ( i
= col
; i
< m_numCols
; i
++ )
5600 m_colRights
[i
] += diff
;
5607 // ------ cell value accessor functions
5610 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5614 m_table
->SetValue( row
, col
, s
.c_str() );
5615 if ( !GetBatchCount() )
5617 wxClientDC
dc( m_gridWin
);
5619 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5622 #if 0 // TODO: edit in place
5624 if ( m_currentCellCoords
.GetRow() == row
&&
5625 m_currentCellCoords
.GetCol() == col
)
5627 SetEditControlValue( s
);
5636 // ------ Block, row and col selection
5639 void wxGrid::SelectRow( int row
, bool addToSelected
)
5643 if ( IsSelection() && addToSelected
)
5646 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5649 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5650 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5651 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5652 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5656 need_refresh
[0] = TRUE
;
5657 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5658 wxGridCellCoords ( oldTop
- 1,
5660 m_selectedTopLeft
.SetRow( row
);
5665 need_refresh
[1] = TRUE
;
5666 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5667 wxGridCellCoords ( oldBottom
,
5670 m_selectedTopLeft
.SetCol( 0 );
5673 if ( oldBottom
< row
)
5675 need_refresh
[2] = TRUE
;
5676 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5677 wxGridCellCoords ( row
,
5679 m_selectedBottomRight
.SetRow( row
);
5682 if ( oldRight
< m_numCols
- 1 )
5684 need_refresh
[3] = TRUE
;
5685 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5687 wxGridCellCoords ( oldBottom
,
5689 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5692 for (i
= 0; i
< 4; i
++ )
5693 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5694 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5698 r
= SelectionToDeviceRect();
5700 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5702 m_selectedTopLeft
.Set( row
, 0 );
5703 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5704 r
= SelectionToDeviceRect();
5705 m_gridWin
->Refresh( FALSE
, &r
);
5708 wxGridRangeSelectEvent
gridEvt( GetId(),
5709 EVT_GRID_RANGE_SELECT
,
5712 m_selectedBottomRight
);
5714 GetEventHandler()->ProcessEvent(gridEvt
);
5718 void wxGrid::SelectCol( int col
, bool addToSelected
)
5720 if ( IsSelection() && addToSelected
)
5723 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5726 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5727 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5728 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5729 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5731 if ( oldLeft
> col
)
5733 need_refresh
[0] = TRUE
;
5734 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5735 wxGridCellCoords ( m_numRows
- 1,
5737 m_selectedTopLeft
.SetCol( col
);
5742 need_refresh
[1] = TRUE
;
5743 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5744 wxGridCellCoords ( oldTop
- 1,
5746 m_selectedTopLeft
.SetRow( 0 );
5749 if ( oldRight
< col
)
5751 need_refresh
[2] = TRUE
;
5752 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5753 wxGridCellCoords ( m_numRows
- 1,
5755 m_selectedBottomRight
.SetCol( col
);
5758 if ( oldBottom
< m_numRows
- 1 )
5760 need_refresh
[3] = TRUE
;
5761 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5763 wxGridCellCoords ( m_numRows
- 1,
5765 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5768 for (i
= 0; i
< 4; i
++ )
5769 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5770 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5776 r
= SelectionToDeviceRect();
5778 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5780 m_selectedTopLeft
.Set( 0, col
);
5781 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5782 r
= SelectionToDeviceRect();
5783 m_gridWin
->Refresh( FALSE
, &r
);
5786 wxGridRangeSelectEvent
gridEvt( GetId(),
5787 EVT_GRID_RANGE_SELECT
,
5790 m_selectedBottomRight
);
5792 GetEventHandler()->ProcessEvent(gridEvt
);
5796 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5799 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5801 if ( topRow
> bottomRow
)
5808 if ( leftCol
> rightCol
)
5815 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5816 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5818 if ( m_selectedTopLeft
!= updateTopLeft
||
5819 m_selectedBottomRight
!= updateBottomRight
)
5821 // Compute two optimal update rectangles:
5822 // Either one rectangle is a real subset of the
5823 // other, or they are (almost) disjoint!
5825 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5828 // Store intermediate values
5829 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5830 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5831 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5832 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5834 // Determine the outer/inner coordinates.
5835 if (oldLeft
> leftCol
)
5841 if (oldTop
> topRow
)
5847 if (oldRight
< rightCol
)
5850 oldRight
= rightCol
;
5853 if (oldBottom
< bottomRow
)
5856 oldBottom
= bottomRow
;
5860 // Now, either the stuff marked old is the outer
5861 // rectangle or we don't have a situation where one
5862 // is contained in the other.
5864 if ( oldLeft
< leftCol
)
5866 need_refresh
[0] = TRUE
;
5867 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5869 wxGridCellCoords ( oldBottom
,
5873 if ( oldTop
< topRow
)
5875 need_refresh
[1] = TRUE
;
5876 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5878 wxGridCellCoords ( topRow
- 1,
5882 if ( oldRight
> rightCol
)
5884 need_refresh
[2] = TRUE
;
5885 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5887 wxGridCellCoords ( oldBottom
,
5891 if ( oldBottom
> bottomRow
)
5893 need_refresh
[3] = TRUE
;
5894 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5896 wxGridCellCoords ( oldBottom
,
5902 m_selectedTopLeft
= updateTopLeft
;
5903 m_selectedBottomRight
= updateBottomRight
;
5905 // various Refresh() calls
5906 for (i
= 0; i
< 4; i
++ )
5907 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5908 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5911 // only generate an event if the block is not being selected by
5912 // dragging the mouse (in which case the event will be generated in
5913 // the mouse event handler)
5914 if ( !m_isDragging
)
5916 wxGridRangeSelectEvent
gridEvt( GetId(),
5917 EVT_GRID_RANGE_SELECT
,
5920 m_selectedBottomRight
);
5922 GetEventHandler()->ProcessEvent(gridEvt
);
5926 void wxGrid::SelectAll()
5928 m_selectedTopLeft
.Set( 0, 0 );
5929 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5931 m_gridWin
->Refresh();
5935 void wxGrid::ClearSelection()
5937 m_selectedTopLeft
= wxGridNoCellCoords
;
5938 m_selectedBottomRight
= wxGridNoCellCoords
;
5942 // This function returns the rectangle that encloses the given block
5943 // in device coords clipped to the client size of the grid window.
5945 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5946 const wxGridCellCoords
&bottomRight
)
5948 wxRect
rect( wxGridNoCellRect
);
5951 cellRect
= CellToRect( topLeft
);
5952 if ( cellRect
!= wxGridNoCellRect
)
5958 rect
= wxRect( 0, 0, 0, 0 );
5961 cellRect
= CellToRect( bottomRight
);
5962 if ( cellRect
!= wxGridNoCellRect
)
5968 return wxGridNoCellRect
;
5971 // convert to scrolled coords
5973 int left
, top
, right
, bottom
;
5974 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5975 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5978 m_gridWin
->GetClientSize( &cw
, &ch
);
5980 rect
.SetLeft( wxMax(0, left
) );
5981 rect
.SetTop( wxMax(0, top
) );
5982 rect
.SetRight( wxMin(cw
, right
) );
5983 rect
.SetBottom( wxMin(ch
, bottom
) );
5991 // ------ Grid event classes
5994 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5996 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5997 int row
, int col
, int x
, int y
,
5998 bool control
, bool shift
, bool alt
, bool meta
)
5999 : wxNotifyEvent( type
, id
)
6005 m_control
= control
;
6010 SetEventObject(obj
);
6014 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6016 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6017 int rowOrCol
, int x
, int y
,
6018 bool control
, bool shift
, bool alt
, bool meta
)
6019 : wxNotifyEvent( type
, id
)
6021 m_rowOrCol
= rowOrCol
;
6024 m_control
= control
;
6029 SetEventObject(obj
);
6033 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6035 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6036 const wxGridCellCoords
& topLeft
,
6037 const wxGridCellCoords
& bottomRight
,
6038 bool control
, bool shift
, bool alt
, bool meta
)
6039 : wxNotifyEvent( type
, id
)
6041 m_topLeft
= topLeft
;
6042 m_bottomRight
= bottomRight
;
6043 m_control
= control
;
6048 SetEventObject(obj
);
6052 #endif // ifndef wxUSE_NEW_GRID