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 bool ok
= m_table
->InsertRows( pos
, numRows
);
3245 // the table will have sent the results of the insert row
3246 // operation to this view object as a grid table message
3250 if ( m_numCols
== 0 )
3252 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3254 // TODO: perhaps instead of appending the default number of cols
3255 // we should remember what the last non-zero number of cols was ?
3259 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3261 // if we have just inserted cols into an empty grid the current
3262 // cell will be undefined...
3264 SetCurrentCell( 0, 0 );
3268 if ( !GetBatchCount() ) Refresh();
3271 SetEditControlValue();
3281 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3283 // TODO: something with updateLabels flag
3287 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3291 if ( m_table
&& m_table
->AppendRows( numRows
) )
3293 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3295 // if we have just inserted cols into an empty grid the current
3296 // cell will be undefined...
3298 SetCurrentCell( 0, 0 );
3301 // the table will have sent the results of the append row
3302 // operation to this view object as a grid table message
3305 if ( !GetBatchCount() ) Refresh();
3315 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3317 // TODO: something with updateLabels flag
3321 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3325 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3327 // the table will have sent the results of the delete row
3328 // operation to this view object as a grid table message
3330 if ( m_numRows
> 0 )
3331 SetEditControlValue();
3333 HideCellEditControl();
3336 if ( !GetBatchCount() ) Refresh();
3346 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3348 // TODO: something with updateLabels flag
3352 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3358 HideCellEditControl();
3359 bool ok
= m_table
->InsertCols( pos
, numCols
);
3361 // the table will have sent the results of the insert col
3362 // operation to this view object as a grid table message
3366 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3368 // if we have just inserted cols into an empty grid the current
3369 // cell will be undefined...
3371 SetCurrentCell( 0, 0 );
3375 if ( !GetBatchCount() ) Refresh();
3378 SetEditControlValue();
3388 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3390 // TODO: something with updateLabels flag
3394 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3398 if ( m_table
&& m_table
->AppendCols( numCols
) )
3400 // the table will have sent the results of the append col
3401 // operation to this view object as a grid table message
3403 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3405 // if we have just inserted cols into an empty grid the current
3406 // cell will be undefined...
3408 SetCurrentCell( 0, 0 );
3412 if ( !GetBatchCount() ) Refresh();
3422 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3424 // TODO: something with updateLabels flag
3428 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3432 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3434 // the table will have sent the results of the delete col
3435 // operation to this view object as a grid table message
3437 if ( m_numCols
> 0 )
3438 SetEditControlValue();
3440 HideCellEditControl();
3443 if ( !GetBatchCount() ) Refresh();
3455 // ----- event handlers
3458 // Generate a grid event based on a mouse event and
3459 // return the result of ProcessEvent()
3461 bool wxGrid::SendEvent( const wxEventType type
,
3463 wxMouseEvent
& mouseEv
)
3465 if ( type
== EVT_GRID_ROW_SIZE
||
3466 type
== EVT_GRID_COL_SIZE
)
3468 int rowOrCol
= (row
== -1 ? col
: row
);
3470 wxGridSizeEvent
gridEvt( GetId(),
3474 mouseEv
.GetX(), mouseEv
.GetY(),
3475 mouseEv
.ControlDown(),
3476 mouseEv
.ShiftDown(),
3478 mouseEv
.MetaDown() );
3480 return GetEventHandler()->ProcessEvent(gridEvt
);
3482 else if ( type
== EVT_GRID_RANGE_SELECT
)
3484 wxGridRangeSelectEvent
gridEvt( GetId(),
3488 m_selectedBottomRight
,
3489 mouseEv
.ControlDown(),
3490 mouseEv
.ShiftDown(),
3492 mouseEv
.MetaDown() );
3494 return GetEventHandler()->ProcessEvent(gridEvt
);
3498 wxGridEvent
gridEvt( GetId(),
3502 mouseEv
.GetX(), mouseEv
.GetY(),
3503 mouseEv
.ControlDown(),
3504 mouseEv
.ShiftDown(),
3506 mouseEv
.MetaDown() );
3508 return GetEventHandler()->ProcessEvent(gridEvt
);
3513 // Generate a grid event of specified type and return the result
3514 // of ProcessEvent().
3516 bool wxGrid::SendEvent( const wxEventType type
,
3519 if ( type
== EVT_GRID_ROW_SIZE
||
3520 type
== EVT_GRID_COL_SIZE
)
3522 int rowOrCol
= (row
== -1 ? col
: row
);
3524 wxGridSizeEvent
gridEvt( GetId(),
3529 return GetEventHandler()->ProcessEvent(gridEvt
);
3533 wxGridEvent
gridEvt( GetId(),
3538 return GetEventHandler()->ProcessEvent(gridEvt
);
3543 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3545 wxPaintDC
dc( this );
3547 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3548 m_numRows
&& m_numCols
)
3550 m_currentCellCoords
.Set(0, 0);
3551 SetEditControlValue();
3552 ShowCellEditControl();
3559 // This is just here to make sure that CalcDimensions gets called when
3560 // the grid view is resized... then the size event is skipped to allow
3561 // the box sizers to handle everything
3563 void wxGrid::OnSize( wxSizeEvent
& event
)
3570 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3572 if ( m_inOnKeyDown
)
3574 // shouldn't be here - we are going round in circles...
3576 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3579 m_inOnKeyDown
= TRUE
;
3581 // propagate the event up and see if it gets processed
3583 wxWindow
*parent
= GetParent();
3584 wxKeyEvent
keyEvt( event
);
3585 keyEvt
.SetEventObject( parent
);
3587 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3590 // TODO: Should also support Shift-cursor keys for
3591 // extending the selection. Maybe add a flag to
3592 // MoveCursorXXX() and MoveCursorXXXBlock() and
3593 // just send event.ShiftDown().
3595 // try local handlers
3597 switch ( event
.KeyCode() )
3600 if ( event
.ControlDown() )
3602 MoveCursorUpBlock();
3611 if ( event
.ControlDown() )
3613 MoveCursorDownBlock();
3622 if ( event
.ControlDown() )
3624 MoveCursorLeftBlock();
3633 if ( event
.ControlDown() )
3635 MoveCursorRightBlock();
3644 if ( event
.ControlDown() )
3646 event
.Skip(); // to let the edit control have the return
3655 if (event
.ShiftDown())
3662 if ( event
.ControlDown() )
3664 MakeCellVisible( 0, 0 );
3665 SetCurrentCell( 0, 0 );
3674 if ( event
.ControlDown() )
3676 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3677 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3693 // We don't want these keys to trigger the edit control, any others?
3702 if ( !IsEditable() )
3707 // Otherwise fall through to default
3710 // now try the cell edit control
3712 if ( !IsCellEditControlEnabled() )
3713 EnableCellEditControl( TRUE
);
3714 if (IsCellEditControlEnabled()) {
3715 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3716 attr
->GetEditor()->StartingKey(event
);
3723 m_inOnKeyDown
= FALSE
;
3727 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3733 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3735 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3737 // the event has been intercepted - do nothing
3742 m_currentCellCoords
!= wxGridNoCellCoords
)
3744 HideCellEditControl();
3745 SaveEditControlValue();
3746 EnableCellEditControl(FALSE
);
3748 // Clear the old current cell highlight
3749 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3751 // Otherwise refresh redraws the highlight!
3752 m_currentCellCoords
= coords
;
3754 m_gridWin
->Refresh( FALSE
, &r
);
3757 m_currentCellCoords
= coords
;
3759 SetEditControlValue();
3763 wxClientDC
dc(m_gridWin
);
3765 DrawCellHighlight(dc
);
3767 if ( IsSelection() )
3769 wxRect
r( SelectionToDeviceRect() );
3771 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3778 // ------ functions to get/send data (see also public functions)
3781 bool wxGrid::GetModelValues()
3785 // all we need to do is repaint the grid
3787 m_gridWin
->Refresh();
3795 bool wxGrid::SetModelValues()
3801 for ( row
= 0; row
< m_numRows
; row
++ )
3803 for ( col
= 0; col
< m_numCols
; col
++ )
3805 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3817 // Note - this function only draws cells that are in the list of
3818 // exposed cells (usually set from the update region by
3819 // CalcExposedCells)
3821 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3823 if ( !m_numRows
|| !m_numCols
) return;
3826 size_t numCells
= m_cellsExposed
.GetCount();
3828 for ( i
= 0; i
< numCells
; i
++ )
3830 DrawCell( dc
, m_cellsExposed
[i
] );
3835 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3837 int row
= coords
.GetRow();
3838 int col
= coords
.GetCol();
3840 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3843 // we draw the cell border ourselves
3844 #if !WXGRID_DRAW_LINES
3845 if ( m_gridLinesEnabled
)
3846 DrawCellBorder( dc
, coords
);
3849 // but all the rest is drawn by the cell renderer and hence may be
3852 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3853 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3854 rect
.width
= m_colWidths
[col
]-1;
3855 rect
.height
= m_rowHeights
[row
]-1;
3857 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3858 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3861 if (m_currentCellCoords
== coords
)
3862 DrawCellHighlight(dc
);
3866 void wxGrid::DrawCellHighlight( wxDC
& dc
)
3868 int row
= m_currentCellCoords
.GetRow();
3869 int col
= m_currentCellCoords
.GetCol();
3871 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3875 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3876 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3877 rect
.width
= m_colWidths
[col
] - 1;
3878 rect
.height
= m_rowHeights
[row
] - 1;
3880 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
3881 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3883 dc
.DrawRectangle(rect
);
3886 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3888 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3889 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3891 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3892 int row
= coords
.GetRow();
3893 int col
= coords
.GetCol();
3895 // right hand border
3897 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3898 m_colRights
[col
], m_rowBottoms
[row
] );
3902 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3903 m_colRights
[col
], m_rowBottoms
[row
] );
3907 // TODO: remove this ???
3908 // This is used to redraw all grid lines e.g. when the grid line colour
3911 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3913 if ( !m_gridLinesEnabled
||
3915 !m_numCols
) return;
3917 int top
, bottom
, left
, right
;
3921 m_gridWin
->GetClientSize(&cw
, &ch
);
3923 // virtual coords of visible area
3925 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3926 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3930 reg
.GetBox(x
, y
, w
, h
);
3931 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3932 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3935 // avoid drawing grid lines past the last row and col
3937 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3938 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3940 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3942 // horizontal grid lines
3945 for ( i
= 0; i
< m_numRows
; i
++ )
3947 if ( m_rowBottoms
[i
]-1 > bottom
)
3951 else if ( m_rowBottoms
[i
]-1 >= top
)
3953 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
3958 // vertical grid lines
3960 for ( i
= 0; i
< m_numCols
; i
++ )
3962 if ( m_colRights
[i
]-1 > right
)
3966 else if ( m_colRights
[i
]-1 >= left
)
3968 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
3974 void wxGrid::DrawRowLabels( wxDC
& dc
)
3976 if ( !m_numRows
|| !m_numCols
) return;
3979 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3981 for ( i
= 0; i
< numLabels
; i
++ )
3983 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3988 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3990 if ( m_rowHeights
[row
] <= 0 ) return;
3992 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3994 dc
.SetPen( *wxBLACK_PEN
);
3995 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3996 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3998 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3999 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4001 dc
.SetPen( *wxWHITE_PEN
);
4002 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4003 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4005 dc
.SetBackgroundMode( wxTRANSPARENT
);
4006 dc
.SetTextForeground( GetLabelTextColour() );
4007 dc
.SetFont( GetLabelFont() );
4010 GetRowLabelAlignment( &hAlign
, &vAlign
);
4014 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4015 rect
.SetWidth( m_rowLabelWidth
- 4 );
4016 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4017 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4021 void wxGrid::DrawColLabels( wxDC
& dc
)
4023 if ( !m_numRows
|| !m_numCols
) return;
4026 size_t numLabels
= m_colLabelsExposed
.GetCount();
4028 for ( i
= 0; i
< numLabels
; i
++ )
4030 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4035 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4037 if ( m_colWidths
[col
] <= 0 ) return;
4039 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4041 dc
.SetPen( *wxBLACK_PEN
);
4042 dc
.DrawLine( m_colRights
[col
]-1, 0,
4043 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4045 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4046 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4048 dc
.SetPen( *wxWHITE_PEN
);
4049 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4050 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4052 dc
.SetBackgroundMode( wxTRANSPARENT
);
4053 dc
.SetTextForeground( GetLabelTextColour() );
4054 dc
.SetFont( GetLabelFont() );
4056 dc
.SetBackgroundMode( wxTRANSPARENT
);
4057 dc
.SetTextForeground( GetLabelTextColour() );
4058 dc
.SetFont( GetLabelFont() );
4061 GetColLabelAlignment( &hAlign
, &vAlign
);
4064 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4066 rect
.SetWidth( m_colWidths
[col
] - 4 );
4067 rect
.SetHeight( m_colLabelHeight
- 4 );
4068 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4072 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4073 const wxString
& value
,
4078 long textWidth
, textHeight
;
4079 long lineWidth
, lineHeight
;
4080 wxArrayString lines
;
4082 dc
.SetClippingRegion( rect
);
4083 StringToLines( value
, lines
);
4084 if ( lines
.GetCount() )
4086 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4087 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4090 switch ( horizAlign
)
4093 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4097 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4106 switch ( vertAlign
)
4109 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4113 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4122 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4124 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4129 dc
.DestroyClippingRegion();
4133 // Split multi line text up into an array of strings. Any existing
4134 // contents of the string array are preserved.
4136 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4140 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4141 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4143 while ( startPos
< (int)tVal
.Length() )
4145 pos
= tVal
.Mid(startPos
).Find( eol
);
4150 else if ( pos
== 0 )
4152 lines
.Add( wxEmptyString
);
4156 lines
.Add( value
.Mid(startPos
, pos
) );
4160 if ( startPos
< (int)value
.Length() )
4162 lines
.Add( value
.Mid( startPos
) );
4167 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4168 wxArrayString
& lines
,
4169 long *width
, long *height
)
4176 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4178 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4179 w
= wxMax( w
, lineW
);
4189 // ------ Edit control functions
4193 void wxGrid::EnableEditing( bool edit
)
4195 // TODO: improve this ?
4197 if ( edit
!= m_editable
)
4201 EnableCellEditControl(m_editable
);
4206 void wxGrid::EnableCellEditControl( bool enable
)
4211 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4212 SetCurrentCell( 0, 0 );
4214 if ( enable
!= m_cellEditCtrlEnabled
)
4218 m_cellEditCtrlEnabled
= enable
;
4219 SetEditControlValue();
4220 ShowCellEditControl();
4224 HideCellEditControl();
4225 SaveEditControlValue();
4226 m_cellEditCtrlEnabled
= enable
;
4232 void wxGrid::ShowCellEditControl()
4234 if ( IsCellEditControlEnabled() )
4236 if ( !IsVisible( m_currentCellCoords
) )
4242 wxRect rect
= CellToRect( m_currentCellCoords
);
4243 int row
= m_currentCellCoords
.GetRow();
4244 int col
= m_currentCellCoords
.GetCol();
4246 // convert to scrolled coords
4248 int left
, top
, right
, bottom
;
4249 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4250 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4251 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4253 m_gridWin
->GetClientSize( &cw
, &ch
);
4255 // Make the edit control large enough to allow for internal
4258 // TODO: remove this if the text ctrl sizing is improved esp. for
4262 #if defined(__WXMOTIF__)
4263 if ( row
== 0 || col
== 0 )
4272 if ( row
== 0 || col
== 0 )
4282 #if defined(__WXGTK__)
4285 if (left
!= 0) left_diff
++;
4286 if (top
!= 0) top_diff
++;
4287 rect
.SetLeft( left
+ left_diff
);
4288 rect
.SetTop( top
+ top_diff
);
4289 rect
.SetRight( rect
.GetRight() - left_diff
);
4290 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4292 rect
.SetLeft( wxMax(0, left
- extra
) );
4293 rect
.SetTop( wxMax(0, top
- extra
) );
4294 rect
.SetRight( rect
.GetRight() + 2*extra
);
4295 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4298 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4299 wxGridCellEditor
* editor
= attr
->GetEditor();
4300 if ( !editor
->IsCreated() )
4302 editor
->Create(m_gridWin
, -1,
4303 new wxGridCellEditorEvtHandler(this, editor
));
4306 editor
->SetSize( rect
);
4307 editor
->Show( TRUE
, attr
);
4308 editor
->BeginEdit(row
, col
, this);
4315 void wxGrid::HideCellEditControl()
4317 if ( IsCellEditControlEnabled() )
4319 int row
= m_currentCellCoords
.GetRow();
4320 int col
= m_currentCellCoords
.GetCol();
4322 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4323 attr
->GetEditor()->Show( FALSE
);
4325 m_gridWin
->SetFocus();
4330 void wxGrid::SetEditControlValue( const wxString
& value
)
4332 // RD: The new Editors get the value from the table themselves now. This
4333 // method can probably be removed...
4337 void wxGrid::SaveEditControlValue()
4339 if ( IsCellEditControlEnabled() )
4341 int row
= m_currentCellCoords
.GetRow();
4342 int col
= m_currentCellCoords
.GetCol();
4344 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4345 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4351 SendEvent( EVT_GRID_CELL_CHANGE
,
4352 m_currentCellCoords
.GetRow(),
4353 m_currentCellCoords
.GetCol() );
4360 // ------ Grid location functions
4361 // Note that all of these functions work with the logical coordinates of
4362 // grid cells and labels so you will need to convert from device
4363 // coordinates for mouse events etc.
4366 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4368 int row
= YToRow(y
);
4369 int col
= XToCol(x
);
4371 if ( row
== -1 || col
== -1 )
4373 coords
= wxGridNoCellCoords
;
4377 coords
.Set( row
, col
);
4382 int wxGrid::YToRow( int y
)
4386 for ( i
= 0; i
< m_numRows
; i
++ )
4388 if ( y
< m_rowBottoms
[i
] ) return i
;
4391 return m_numRows
; //-1;
4395 int wxGrid::XToCol( int x
)
4399 for ( i
= 0; i
< m_numCols
; i
++ )
4401 if ( x
< m_colRights
[i
] ) return i
;
4404 return m_numCols
; //-1;
4408 // return the row number that that the y coord is near the edge of, or
4409 // -1 if not near an edge
4411 int wxGrid::YToEdgeOfRow( int y
)
4415 for ( i
= 0; i
< m_numRows
; i
++ )
4417 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4419 d
= abs( y
- m_rowBottoms
[i
] );
4421 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4430 // return the col number that that the x coord is near the edge of, or
4431 // -1 if not near an edge
4433 int wxGrid::XToEdgeOfCol( int x
)
4437 for ( i
= 0; i
< m_numCols
; i
++ )
4439 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4441 d
= abs( x
- m_colRights
[i
] );
4443 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4452 wxRect
wxGrid::CellToRect( int row
, int col
)
4454 wxRect
rect( -1, -1, -1, -1 );
4456 if ( row
>= 0 && row
< m_numRows
&&
4457 col
>= 0 && col
< m_numCols
)
4459 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4460 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4461 rect
.width
= m_colWidths
[col
];
4462 rect
.height
= m_rowHeights
[ row
];
4469 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4471 // get the cell rectangle in logical coords
4473 wxRect
r( CellToRect( row
, col
) );
4475 // convert to device coords
4477 int left
, top
, right
, bottom
;
4478 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4479 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4481 // check against the client area of the grid window
4484 m_gridWin
->GetClientSize( &cw
, &ch
);
4486 if ( wholeCellVisible
)
4488 // is the cell wholly visible ?
4490 return ( left
>= 0 && right
<= cw
&&
4491 top
>= 0 && bottom
<= ch
);
4495 // is the cell partly visible ?
4497 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4498 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4503 // make the specified cell location visible by doing a minimal amount
4506 void wxGrid::MakeCellVisible( int row
, int col
)
4509 int xpos
= -1, ypos
= -1;
4511 if ( row
>= 0 && row
< m_numRows
&&
4512 col
>= 0 && col
< m_numCols
)
4514 // get the cell rectangle in logical coords
4516 wxRect
r( CellToRect( row
, col
) );
4518 // convert to device coords
4520 int left
, top
, right
, bottom
;
4521 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4522 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4525 m_gridWin
->GetClientSize( &cw
, &ch
);
4531 else if ( bottom
> ch
)
4533 int h
= r
.GetHeight();
4535 for ( i
= row
-1; i
>= 0; i
-- )
4537 if ( h
+ m_rowHeights
[i
] > ch
) break;
4539 h
+= m_rowHeights
[i
];
4540 ypos
-= m_rowHeights
[i
];
4543 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4544 // have rounding errors (this is important, because if we do, we
4545 // might not scroll at all and some cells won't be redrawn)
4546 ypos
+= GRID_SCROLL_LINE
/ 2;
4553 else if ( right
> cw
)
4555 int w
= r
.GetWidth();
4557 for ( i
= col
-1; i
>= 0; i
-- )
4559 if ( w
+ m_colWidths
[i
] > cw
) break;
4561 w
+= m_colWidths
[i
];
4562 xpos
-= m_colWidths
[i
];
4565 // see comment for ypos above
4566 xpos
+= GRID_SCROLL_LINE
/ 2;
4569 if ( xpos
!= -1 || ypos
!= -1 )
4571 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4572 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4573 Scroll( xpos
, ypos
);
4581 // ------ Grid cursor movement functions
4584 bool wxGrid::MoveCursorUp()
4586 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4587 m_currentCellCoords
.GetRow() > 0 )
4589 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4590 m_currentCellCoords
.GetCol() );
4592 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4593 m_currentCellCoords
.GetCol() );
4602 bool wxGrid::MoveCursorDown()
4604 // TODO: allow for scrolling
4606 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4607 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4609 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4610 m_currentCellCoords
.GetCol() );
4612 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4613 m_currentCellCoords
.GetCol() );
4622 bool wxGrid::MoveCursorLeft()
4624 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4625 m_currentCellCoords
.GetCol() > 0 )
4627 MakeCellVisible( m_currentCellCoords
.GetRow(),
4628 m_currentCellCoords
.GetCol() - 1 );
4630 SetCurrentCell( m_currentCellCoords
.GetRow(),
4631 m_currentCellCoords
.GetCol() - 1 );
4640 bool wxGrid::MoveCursorRight()
4642 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4643 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4645 MakeCellVisible( m_currentCellCoords
.GetRow(),
4646 m_currentCellCoords
.GetCol() + 1 );
4648 SetCurrentCell( m_currentCellCoords
.GetRow(),
4649 m_currentCellCoords
.GetCol() + 1 );
4658 bool wxGrid::MovePageUp()
4660 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4662 int row
= m_currentCellCoords
.GetRow();
4666 m_gridWin
->GetClientSize( &cw
, &ch
);
4668 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4669 int newRow
= YToRow( y
- ch
+ 1 );
4674 else if ( newRow
== row
)
4679 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4680 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4688 bool wxGrid::MovePageDown()
4690 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4692 int row
= m_currentCellCoords
.GetRow();
4693 if ( row
< m_numRows
)
4696 m_gridWin
->GetClientSize( &cw
, &ch
);
4698 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4699 int newRow
= YToRow( y
+ ch
);
4702 newRow
= m_numRows
- 1;
4704 else if ( newRow
== row
)
4709 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4710 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4718 bool wxGrid::MoveCursorUpBlock()
4721 m_currentCellCoords
!= wxGridNoCellCoords
&&
4722 m_currentCellCoords
.GetRow() > 0 )
4724 int row
= m_currentCellCoords
.GetRow();
4725 int col
= m_currentCellCoords
.GetCol();
4727 if ( m_table
->IsEmptyCell(row
, col
) )
4729 // starting in an empty cell: find the next block of
4735 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4738 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4740 // starting at the top of a block: find the next block
4746 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4751 // starting within a block: find the top of the block
4756 if ( m_table
->IsEmptyCell(row
, col
) )
4764 MakeCellVisible( row
, col
);
4765 SetCurrentCell( row
, col
);
4773 bool wxGrid::MoveCursorDownBlock()
4776 m_currentCellCoords
!= wxGridNoCellCoords
&&
4777 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4779 int row
= m_currentCellCoords
.GetRow();
4780 int col
= m_currentCellCoords
.GetCol();
4782 if ( m_table
->IsEmptyCell(row
, col
) )
4784 // starting in an empty cell: find the next block of
4787 while ( row
< m_numRows
-1 )
4790 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4793 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4795 // starting at the bottom of a block: find the next block
4798 while ( row
< m_numRows
-1 )
4801 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4806 // starting within a block: find the bottom of the block
4808 while ( row
< m_numRows
-1 )
4811 if ( m_table
->IsEmptyCell(row
, col
) )
4819 MakeCellVisible( row
, col
);
4820 SetCurrentCell( row
, col
);
4828 bool wxGrid::MoveCursorLeftBlock()
4831 m_currentCellCoords
!= wxGridNoCellCoords
&&
4832 m_currentCellCoords
.GetCol() > 0 )
4834 int row
= m_currentCellCoords
.GetRow();
4835 int col
= m_currentCellCoords
.GetCol();
4837 if ( m_table
->IsEmptyCell(row
, col
) )
4839 // starting in an empty cell: find the next block of
4845 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4848 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4850 // starting at the left of a block: find the next block
4856 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4861 // starting within a block: find the left of the block
4866 if ( m_table
->IsEmptyCell(row
, col
) )
4874 MakeCellVisible( row
, col
);
4875 SetCurrentCell( row
, col
);
4883 bool wxGrid::MoveCursorRightBlock()
4886 m_currentCellCoords
!= wxGridNoCellCoords
&&
4887 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4889 int row
= m_currentCellCoords
.GetRow();
4890 int col
= m_currentCellCoords
.GetCol();
4892 if ( m_table
->IsEmptyCell(row
, col
) )
4894 // starting in an empty cell: find the next block of
4897 while ( col
< m_numCols
-1 )
4900 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4903 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4905 // starting at the right of a block: find the next block
4908 while ( col
< m_numCols
-1 )
4911 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4916 // starting within a block: find the right of the block
4918 while ( col
< m_numCols
-1 )
4921 if ( m_table
->IsEmptyCell(row
, col
) )
4929 MakeCellVisible( row
, col
);
4930 SetCurrentCell( row
, col
);
4941 // ------ Label values and formatting
4944 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4946 *horiz
= m_rowLabelHorizAlign
;
4947 *vert
= m_rowLabelVertAlign
;
4950 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4952 *horiz
= m_colLabelHorizAlign
;
4953 *vert
= m_colLabelVertAlign
;
4956 wxString
wxGrid::GetRowLabelValue( int row
)
4960 return m_table
->GetRowLabelValue( row
);
4970 wxString
wxGrid::GetColLabelValue( int col
)
4974 return m_table
->GetColLabelValue( col
);
4985 void wxGrid::SetRowLabelSize( int width
)
4987 width
= wxMax( width
, 0 );
4988 if ( width
!= m_rowLabelWidth
)
4992 m_rowLabelWin
->Show( FALSE
);
4993 m_cornerLabelWin
->Show( FALSE
);
4995 else if ( m_rowLabelWidth
== 0 )
4997 m_rowLabelWin
->Show( TRUE
);
4998 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5001 m_rowLabelWidth
= width
;
5008 void wxGrid::SetColLabelSize( int height
)
5010 height
= wxMax( height
, 0 );
5011 if ( height
!= m_colLabelHeight
)
5015 m_colLabelWin
->Show( FALSE
);
5016 m_cornerLabelWin
->Show( FALSE
);
5018 else if ( m_colLabelHeight
== 0 )
5020 m_colLabelWin
->Show( TRUE
);
5021 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5024 m_colLabelHeight
= height
;
5031 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5033 if ( m_labelBackgroundColour
!= colour
)
5035 m_labelBackgroundColour
= colour
;
5036 m_rowLabelWin
->SetBackgroundColour( colour
);
5037 m_colLabelWin
->SetBackgroundColour( colour
);
5038 m_cornerLabelWin
->SetBackgroundColour( colour
);
5040 if ( !GetBatchCount() )
5042 m_rowLabelWin
->Refresh();
5043 m_colLabelWin
->Refresh();
5044 m_cornerLabelWin
->Refresh();
5049 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5051 if ( m_labelTextColour
!= colour
)
5053 m_labelTextColour
= colour
;
5054 if ( !GetBatchCount() )
5056 m_rowLabelWin
->Refresh();
5057 m_colLabelWin
->Refresh();
5062 void wxGrid::SetLabelFont( const wxFont
& font
)
5065 if ( !GetBatchCount() )
5067 m_rowLabelWin
->Refresh();
5068 m_colLabelWin
->Refresh();
5072 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5074 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5076 m_rowLabelHorizAlign
= horiz
;
5079 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5081 m_rowLabelVertAlign
= vert
;
5084 if ( !GetBatchCount() )
5086 m_rowLabelWin
->Refresh();
5090 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5092 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5094 m_colLabelHorizAlign
= horiz
;
5097 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5099 m_colLabelVertAlign
= vert
;
5102 if ( !GetBatchCount() )
5104 m_colLabelWin
->Refresh();
5108 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5112 m_table
->SetRowLabelValue( row
, s
);
5113 if ( !GetBatchCount() )
5115 wxRect rect
= CellToRect( row
, 0);
5116 if ( rect
.height
> 0 )
5118 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5120 rect
.width
= m_rowLabelWidth
;
5121 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5127 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5131 m_table
->SetColLabelValue( col
, s
);
5132 if ( !GetBatchCount() )
5134 wxRect rect
= CellToRect( 0, col
);
5135 if ( rect
.width
> 0 )
5137 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5139 rect
.height
= m_colLabelHeight
;
5140 m_colLabelWin
->Refresh( TRUE
, &rect
);
5146 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5148 if ( m_gridLineColour
!= colour
)
5150 m_gridLineColour
= colour
;
5152 wxClientDC
dc( m_gridWin
);
5154 DrawAllGridLines( dc
, wxRegion() );
5158 void wxGrid::EnableGridLines( bool enable
)
5160 if ( enable
!= m_gridLinesEnabled
)
5162 m_gridLinesEnabled
= enable
;
5164 if ( !GetBatchCount() )
5168 wxClientDC
dc( m_gridWin
);
5170 DrawAllGridLines( dc
, wxRegion() );
5174 m_gridWin
->Refresh();
5181 int wxGrid::GetDefaultRowSize()
5183 return m_defaultRowHeight
;
5186 int wxGrid::GetRowSize( int row
)
5188 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5190 return m_rowHeights
[row
];
5193 int wxGrid::GetDefaultColSize()
5195 return m_defaultColWidth
;
5198 int wxGrid::GetColSize( int col
)
5200 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5202 return m_colWidths
[col
];
5205 // ============================================================================
5206 // access to the grid attributes: each of them has a default value in the grid
5207 // itself and may be overidden on a per-cell basis
5208 // ============================================================================
5210 // ----------------------------------------------------------------------------
5211 // setting default attributes
5212 // ----------------------------------------------------------------------------
5214 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5216 m_defaultCellAttr
->SetBackgroundColour(col
);
5219 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5221 m_defaultCellAttr
->SetTextColour(col
);
5224 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5226 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5229 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5231 m_defaultCellAttr
->SetFont(font
);
5234 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5236 m_defaultCellAttr
->SetRenderer(renderer
);
5239 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5241 m_defaultCellAttr
->SetEditor(editor
);
5244 // ----------------------------------------------------------------------------
5245 // access to the default attrbiutes
5246 // ----------------------------------------------------------------------------
5248 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5250 return m_defaultCellAttr
->GetBackgroundColour();
5253 wxColour
wxGrid::GetDefaultCellTextColour()
5255 return m_defaultCellAttr
->GetTextColour();
5258 wxFont
wxGrid::GetDefaultCellFont()
5260 return m_defaultCellAttr
->GetFont();
5263 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5265 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5268 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5270 return m_defaultCellAttr
->GetRenderer();
5273 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5275 return m_defaultCellAttr
->GetEditor();
5278 // ----------------------------------------------------------------------------
5279 // access to cell attributes
5280 // ----------------------------------------------------------------------------
5282 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5284 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5285 wxColour colour
= attr
->GetBackgroundColour();
5290 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5292 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5293 wxColour colour
= attr
->GetTextColour();
5298 wxFont
wxGrid::GetCellFont( int row
, int col
)
5300 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5301 wxFont font
= attr
->GetFont();
5306 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5308 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5309 attr
->GetAlignment(horiz
, vert
);
5313 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5315 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5316 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5321 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5323 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5324 wxGridCellEditor
* editor
= attr
->GetEditor();
5329 // ----------------------------------------------------------------------------
5330 // attribute support: cache, automatic provider creation, ...
5331 // ----------------------------------------------------------------------------
5333 bool wxGrid::CanHaveAttributes()
5340 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5341 // table is providing the attributes itself??? In which case
5342 // I don't think the grid should create a Provider object for the
5343 // table but the table should be smart enough to do that on its own.
5344 if ( !m_table
->GetAttrProvider() )
5346 // use the default attr provider by default
5347 // (another choice would be to just return FALSE thus forcing the user
5349 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5355 void wxGrid::ClearAttrCache()
5357 if ( m_attrCache
.row
!= -1 )
5359 m_attrCache
.attr
->SafeDecRef();
5360 m_attrCache
.row
= -1;
5364 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5366 wxGrid
*self
= (wxGrid
*)this; // const_cast
5368 self
->ClearAttrCache();
5369 self
->m_attrCache
.row
= row
;
5370 self
->m_attrCache
.col
= col
;
5371 self
->m_attrCache
.attr
= attr
;
5375 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5377 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5379 *attr
= m_attrCache
.attr
;
5380 (*attr
)->SafeIncRef();
5382 #ifdef DEBUG_ATTR_CACHE
5383 gs_nAttrCacheHits
++;
5390 #ifdef DEBUG_ATTR_CACHE
5391 gs_nAttrCacheMisses
++;
5397 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5399 wxGridCellAttr
*attr
;
5400 if ( !LookupAttr(row
, col
, &attr
) )
5402 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5403 CacheAttr(row
, col
, attr
);
5406 attr
->SetDefAttr(m_defaultCellAttr
);
5408 attr
= m_defaultCellAttr
;
5415 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5417 wxGridCellAttr
*attr
;
5418 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5420 wxASSERT_MSG( m_table
,
5421 _T("we may only be called if CanHaveAttributes() "
5422 "returned TRUE and then m_table should be !NULL") );
5424 attr
= m_table
->GetAttr(row
, col
);
5427 attr
= new wxGridCellAttr
;
5429 // artificially inc the ref count to match DecRef() in caller
5432 m_table
->SetAttr(attr
, row
, col
);
5435 CacheAttr(row
, col
, attr
);
5437 attr
->SetDefAttr(m_defaultCellAttr
);
5441 // ----------------------------------------------------------------------------
5442 // setting cell attributes: this is forwarded to the table
5443 // ----------------------------------------------------------------------------
5445 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5447 if ( CanHaveAttributes() )
5449 m_table
->SetRowAttr(attr
, row
);
5457 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5459 if ( CanHaveAttributes() )
5461 m_table
->SetColAttr(attr
, col
);
5469 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5471 if ( CanHaveAttributes() )
5473 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5474 attr
->SetBackgroundColour(colour
);
5479 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5481 if ( CanHaveAttributes() )
5483 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5484 attr
->SetTextColour(colour
);
5489 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5491 if ( CanHaveAttributes() )
5493 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5494 attr
->SetFont(font
);
5499 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5501 if ( CanHaveAttributes() )
5503 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5504 attr
->SetAlignment(horiz
, vert
);
5509 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5511 if ( CanHaveAttributes() )
5513 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5514 attr
->SetRenderer(renderer
);
5519 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5521 if ( CanHaveAttributes() )
5523 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5524 attr
->SetEditor(editor
);
5529 // ----------------------------------------------------------------------------
5531 // ----------------------------------------------------------------------------
5533 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5535 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5537 if ( resizeExistingRows
)
5541 for ( row
= 0; row
< m_numRows
; row
++ )
5543 m_rowHeights
[row
] = m_defaultRowHeight
;
5544 bottom
+= m_defaultRowHeight
;
5545 m_rowBottoms
[row
] = bottom
;
5551 void wxGrid::SetRowSize( int row
, int height
)
5553 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5557 int h
= wxMax( 0, height
);
5558 int diff
= h
- m_rowHeights
[row
];
5560 m_rowHeights
[row
] = h
;
5561 for ( i
= row
; i
< m_numRows
; i
++ )
5563 m_rowBottoms
[i
] += diff
;
5568 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5570 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5572 if ( resizeExistingCols
)
5576 for ( col
= 0; col
< m_numCols
; col
++ )
5578 m_colWidths
[col
] = m_defaultColWidth
;
5579 right
+= m_defaultColWidth
;
5580 m_colRights
[col
] = right
;
5586 void wxGrid::SetColSize( int col
, int width
)
5588 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5592 int w
= wxMax( 0, width
);
5593 int diff
= w
- m_colWidths
[col
];
5594 m_colWidths
[col
] = w
;
5596 for ( i
= col
; i
< m_numCols
; i
++ )
5598 m_colRights
[i
] += diff
;
5605 // ------ cell value accessor functions
5608 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5612 m_table
->SetValue( row
, col
, s
.c_str() );
5613 if ( !GetBatchCount() )
5615 wxClientDC
dc( m_gridWin
);
5617 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5620 #if 0 // TODO: edit in place
5622 if ( m_currentCellCoords
.GetRow() == row
&&
5623 m_currentCellCoords
.GetCol() == col
)
5625 SetEditControlValue( s
);
5634 // ------ Block, row and col selection
5637 void wxGrid::SelectRow( int row
, bool addToSelected
)
5641 if ( IsSelection() && addToSelected
)
5644 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5647 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5648 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5649 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5650 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5654 need_refresh
[0] = TRUE
;
5655 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5656 wxGridCellCoords ( oldTop
- 1,
5658 m_selectedTopLeft
.SetRow( row
);
5663 need_refresh
[1] = TRUE
;
5664 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5665 wxGridCellCoords ( oldBottom
,
5668 m_selectedTopLeft
.SetCol( 0 );
5671 if ( oldBottom
< row
)
5673 need_refresh
[2] = TRUE
;
5674 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5675 wxGridCellCoords ( row
,
5677 m_selectedBottomRight
.SetRow( row
);
5680 if ( oldRight
< m_numCols
- 1 )
5682 need_refresh
[3] = TRUE
;
5683 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5685 wxGridCellCoords ( oldBottom
,
5687 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5690 for (i
= 0; i
< 4; i
++ )
5691 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5692 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5696 r
= SelectionToDeviceRect();
5698 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5700 m_selectedTopLeft
.Set( row
, 0 );
5701 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5702 r
= SelectionToDeviceRect();
5703 m_gridWin
->Refresh( FALSE
, &r
);
5706 wxGridRangeSelectEvent
gridEvt( GetId(),
5707 EVT_GRID_RANGE_SELECT
,
5710 m_selectedBottomRight
);
5712 GetEventHandler()->ProcessEvent(gridEvt
);
5716 void wxGrid::SelectCol( int col
, bool addToSelected
)
5718 if ( IsSelection() && addToSelected
)
5721 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5724 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5725 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5726 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5727 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5729 if ( oldLeft
> col
)
5731 need_refresh
[0] = TRUE
;
5732 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5733 wxGridCellCoords ( m_numRows
- 1,
5735 m_selectedTopLeft
.SetCol( col
);
5740 need_refresh
[1] = TRUE
;
5741 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5742 wxGridCellCoords ( oldTop
- 1,
5744 m_selectedTopLeft
.SetRow( 0 );
5747 if ( oldRight
< col
)
5749 need_refresh
[2] = TRUE
;
5750 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5751 wxGridCellCoords ( m_numRows
- 1,
5753 m_selectedBottomRight
.SetCol( col
);
5756 if ( oldBottom
< m_numRows
- 1 )
5758 need_refresh
[3] = TRUE
;
5759 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5761 wxGridCellCoords ( m_numRows
- 1,
5763 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5766 for (i
= 0; i
< 4; i
++ )
5767 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5768 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5774 r
= SelectionToDeviceRect();
5776 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5778 m_selectedTopLeft
.Set( 0, col
);
5779 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5780 r
= SelectionToDeviceRect();
5781 m_gridWin
->Refresh( FALSE
, &r
);
5784 wxGridRangeSelectEvent
gridEvt( GetId(),
5785 EVT_GRID_RANGE_SELECT
,
5788 m_selectedBottomRight
);
5790 GetEventHandler()->ProcessEvent(gridEvt
);
5794 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5797 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5799 if ( topRow
> bottomRow
)
5806 if ( leftCol
> rightCol
)
5813 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5814 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5816 if ( m_selectedTopLeft
!= updateTopLeft
||
5817 m_selectedBottomRight
!= updateBottomRight
)
5819 // Compute two optimal update rectangles:
5820 // Either one rectangle is a real subset of the
5821 // other, or they are (almost) disjoint!
5823 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5826 // Store intermediate values
5827 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5828 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5829 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5830 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5832 // Determine the outer/inner coordinates.
5833 if (oldLeft
> leftCol
)
5839 if (oldTop
> topRow
)
5845 if (oldRight
< rightCol
)
5848 oldRight
= rightCol
;
5851 if (oldBottom
< bottomRow
)
5854 oldBottom
= bottomRow
;
5858 // Now, either the stuff marked old is the outer
5859 // rectangle or we don't have a situation where one
5860 // is contained in the other.
5862 if ( oldLeft
< leftCol
)
5864 need_refresh
[0] = TRUE
;
5865 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5867 wxGridCellCoords ( oldBottom
,
5871 if ( oldTop
< topRow
)
5873 need_refresh
[1] = TRUE
;
5874 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5876 wxGridCellCoords ( topRow
- 1,
5880 if ( oldRight
> rightCol
)
5882 need_refresh
[2] = TRUE
;
5883 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5885 wxGridCellCoords ( oldBottom
,
5889 if ( oldBottom
> bottomRow
)
5891 need_refresh
[3] = TRUE
;
5892 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5894 wxGridCellCoords ( oldBottom
,
5900 m_selectedTopLeft
= updateTopLeft
;
5901 m_selectedBottomRight
= updateBottomRight
;
5903 // various Refresh() calls
5904 for (i
= 0; i
< 4; i
++ )
5905 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5906 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5909 // only generate an event if the block is not being selected by
5910 // dragging the mouse (in which case the event will be generated in
5911 // the mouse event handler)
5912 if ( !m_isDragging
)
5914 wxGridRangeSelectEvent
gridEvt( GetId(),
5915 EVT_GRID_RANGE_SELECT
,
5918 m_selectedBottomRight
);
5920 GetEventHandler()->ProcessEvent(gridEvt
);
5924 void wxGrid::SelectAll()
5926 m_selectedTopLeft
.Set( 0, 0 );
5927 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5929 m_gridWin
->Refresh();
5933 void wxGrid::ClearSelection()
5935 m_selectedTopLeft
= wxGridNoCellCoords
;
5936 m_selectedBottomRight
= wxGridNoCellCoords
;
5940 // This function returns the rectangle that encloses the given block
5941 // in device coords clipped to the client size of the grid window.
5943 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5944 const wxGridCellCoords
&bottomRight
)
5946 wxRect
rect( wxGridNoCellRect
);
5949 cellRect
= CellToRect( topLeft
);
5950 if ( cellRect
!= wxGridNoCellRect
)
5956 rect
= wxRect( 0, 0, 0, 0 );
5959 cellRect
= CellToRect( bottomRight
);
5960 if ( cellRect
!= wxGridNoCellRect
)
5966 return wxGridNoCellRect
;
5969 // convert to scrolled coords
5971 int left
, top
, right
, bottom
;
5972 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5973 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5976 m_gridWin
->GetClientSize( &cw
, &ch
);
5978 rect
.SetLeft( wxMax(0, left
) );
5979 rect
.SetTop( wxMax(0, top
) );
5980 rect
.SetRight( wxMin(cw
, right
) );
5981 rect
.SetBottom( wxMin(ch
, bottom
) );
5989 // ------ Grid event classes
5992 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5994 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5995 int row
, int col
, int x
, int y
,
5996 bool control
, bool shift
, bool alt
, bool meta
)
5997 : wxNotifyEvent( type
, id
)
6003 m_control
= control
;
6008 SetEventObject(obj
);
6012 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6014 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6015 int rowOrCol
, int x
, int y
,
6016 bool control
, bool shift
, bool alt
, bool meta
)
6017 : wxNotifyEvent( type
, id
)
6019 m_rowOrCol
= rowOrCol
;
6022 m_control
= control
;
6027 SetEventObject(obj
);
6031 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6033 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6034 const wxGridCellCoords
& topLeft
,
6035 const wxGridCellCoords
& bottomRight
,
6036 bool control
, bool shift
, bool alt
, bool meta
)
6037 : wxNotifyEvent( type
, id
)
6039 m_topLeft
= topLeft
;
6040 m_bottomRight
= bottomRight
;
6041 m_control
= control
;
6046 SetEventObject(obj
);
6050 #endif // ifndef wxUSE_NEW_GRID