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;
213 // searches for the attr for given cell, returns wxNOT_FOUND if not found
214 int FindIndex(int row
, int col
) const;
216 wxGridCellWithAttrArray m_attrs
;
219 // this class stores attributes set for rows or columns
220 class WXDLLEXPORT wxGridRowOrColAttrData
223 ~wxGridRowOrColAttrData();
225 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
226 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
229 wxArrayInt m_rowsOrCols
;
230 wxArrayAttrs m_attrs
;
233 // NB: this is just a wrapper around 3 objects: one which stores cell
234 // attributes, and 2 others for row/col ones
235 class WXDLLEXPORT wxGridCellAttrProviderData
238 wxGridCellAttrData m_cellAttrs
;
239 wxGridRowOrColAttrData m_rowAttrs
,
243 // ----------------------------------------------------------------------------
244 // conditional compilation
245 // ----------------------------------------------------------------------------
247 #ifndef WXGRID_DRAW_LINES
248 #define WXGRID_DRAW_LINES 1
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 //#define DEBUG_ATTR_CACHE
256 #ifdef DEBUG_ATTR_CACHE
257 static size_t gs_nAttrCacheHits
= 0;
258 static size_t gs_nAttrCacheMisses
= 0;
259 #endif // DEBUG_ATTR_CACHE
261 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
262 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
265 // TODO: fixed so far - make configurable later (and also different for x/y)
266 static const size_t GRID_SCROLL_LINE
= 10;
268 // ============================================================================
270 // ============================================================================
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 wxGridCellEditor::wxGridCellEditor()
283 wxGridCellEditor::~wxGridCellEditor()
289 void wxGridCellEditor::Destroy()
292 m_control
->Destroy();
297 void wxGridCellEditor::Show(bool show
)
299 wxASSERT_MSG(m_control
,
300 wxT("The wxGridCellEditor must be Created first!"));
301 m_control
->Show(show
);
304 void wxGridCellEditor::SetSize(const wxRect
& rect
)
306 wxASSERT_MSG(m_control
,
307 wxT("The wxGridCellEditor must be Created first!"));
308 m_control
->SetSize(rect
);
311 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
317 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
323 wxGridCellTextEditor::wxGridCellTextEditor()
327 void wxGridCellTextEditor::Create(wxWindow
* parent
,
329 wxEvtHandler
* evtHandler
)
331 m_control
= new wxTextCtrl(parent
, -1, "",
332 wxDefaultPosition
, wxDefaultSize
333 #if defined(__WXMSW__)
334 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
339 m_control
->PushEventHandler(evtHandler
);
343 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
,
344 wxGridCellAttr
* attr
)
346 wxASSERT_MSG(m_control
,
347 wxT("The wxGridCellEditor must be Created first!"));
349 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
350 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
351 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
352 ((wxTextCtrl
*)m_control
)->SetFocus();
354 // ??? Should we use attr and try to set colours and font?
359 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
360 wxGrid
* grid
, wxGridCellAttr
* attr
)
362 wxASSERT_MSG(m_control
,
363 wxT("The wxGridCellEditor must be Created first!"));
365 bool changed
= FALSE
;
366 wxString value
= ((wxTextCtrl
*)m_control
)->GetValue();
367 if (value
!= m_startValue
)
371 grid
->GetTable()->SetValue(row
, col
, value
);
374 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
380 void wxGridCellTextEditor::Reset()
382 wxASSERT_MSG(m_control
,
383 wxT("The wxGridCellEditor must be Created first!"));
385 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
386 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
390 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
392 wxASSERT_MSG(m_control
,
393 wxT("The wxGridCellEditor must be Created first!"));
395 int code
= event
.KeyCode();
396 if (code
>= 32 && code
< 255) {
397 wxString
st((char)code
);
398 if (! event
.ShiftDown())
400 ((wxTextCtrl
*)m_control
)->AppendText(st
);
405 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
407 #if defined(__WXMOTIF__) || defined(__WXGTK__)
408 // wxMotif needs a little extra help...
409 int pos
= ((wxTextCtrl
*)m_control
)->GetInsertionPoint();
410 wxString
s( ((wxTextCtrl
*)m_control
)->GetValue() );
411 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
412 ((wxTextCtrl
*)m_control
)->SetValue(s
);
413 ((wxTextCtrl
*)m_control
)->SetInsertionPoint( pos
);
415 // the other ports can handle a Return key press
422 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
424 switch ( event
.KeyCode() )
428 m_grid
->EnableCellEditControl(FALSE
);
440 // // send the event to the parent grid, skipping the
441 // // event if nothing happens
443 // event.Skip( m_grid->ProcessEvent( event ) );
448 if (!m_grid
->ProcessEvent(event
))
449 m_editor
->HandleReturn(event
);
458 // ----------------------------------------------------------------------------
459 // wxGridCellRenderer
460 // ----------------------------------------------------------------------------
462 void wxGridCellRenderer::Draw(wxGrid
& grid
,
463 wxGridCellAttr
& attr
,
469 dc
.SetBackgroundMode( wxSOLID
);
473 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
477 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
480 dc
.SetPen( *wxTRANSPARENT_PEN
);
481 dc
.DrawRectangle(rect
);
484 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
485 wxGridCellAttr
& attr
,
487 const wxRect
& rectCell
,
491 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
493 // now we only have to draw the text
494 dc
.SetBackgroundMode( wxTRANSPARENT
);
498 dc
.SetTextBackground( grid
.GetSelectionBackground() );
499 dc
.SetTextForeground( grid
.GetSelectionForeground() );
503 dc
.SetTextBackground( attr
.GetBackgroundColour() );
504 dc
.SetTextForeground( attr
.GetTextColour() );
506 dc
.SetFont( attr
.GetFont() );
509 attr
.GetAlignment(&hAlign
, &vAlign
);
511 wxRect rect
= rectCell
;
517 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
518 rect
, hAlign
, vAlign
);
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
525 const wxColour
& wxGridCellAttr::GetTextColour() const
529 else if (m_defGridAttr
!= this)
530 return m_defGridAttr
->GetTextColour();
532 wxFAIL_MSG(wxT("Missing default cell attribute"));
538 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
540 if (HasBackgroundColour())
542 else if (m_defGridAttr
!= this)
543 return m_defGridAttr
->GetBackgroundColour();
545 wxFAIL_MSG(wxT("Missing default cell attribute"));
551 const wxFont
& wxGridCellAttr::GetFont() const
555 else if (m_defGridAttr
!= this)
556 return m_defGridAttr
->GetFont();
558 wxFAIL_MSG(wxT("Missing default cell attribute"));
564 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
566 if (HasAlignment()) {
567 if ( hAlign
) *hAlign
= m_hAlign
;
568 if ( vAlign
) *vAlign
= m_vAlign
;
570 else if (m_defGridAttr
!= this)
571 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
573 wxFAIL_MSG(wxT("Missing default cell attribute"));
578 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
582 else if (m_defGridAttr
!= this)
583 return m_defGridAttr
->GetRenderer();
585 wxFAIL_MSG(wxT("Missing default cell attribute"));
590 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
594 else if (m_defGridAttr
!= this)
595 return m_defGridAttr
->GetEditor();
597 wxFAIL_MSG(wxT("Missing default cell attribute"));
602 // ----------------------------------------------------------------------------
603 // wxGridCellAttrData
604 // ----------------------------------------------------------------------------
606 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
608 int n
= FindIndex(row
, col
);
609 if ( n
== wxNOT_FOUND
)
612 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
618 // change the attribute
619 m_attrs
[(size_t)n
].attr
= attr
;
623 // remove this attribute
624 m_attrs
.RemoveAt((size_t)n
);
629 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
631 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
633 int n
= FindIndex(row
, col
);
634 if ( n
!= wxNOT_FOUND
)
636 attr
= m_attrs
[(size_t)n
].attr
;
643 int wxGridCellAttrData::FindIndex(int row
, int col
) const
645 size_t count
= m_attrs
.GetCount();
646 for ( size_t n
= 0; n
< count
; n
++ )
648 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
649 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
658 // ----------------------------------------------------------------------------
659 // wxGridRowOrColAttrData
660 // ----------------------------------------------------------------------------
662 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
664 size_t count
= m_attrs
.Count();
665 for ( size_t n
= 0; n
< count
; n
++ )
667 m_attrs
[n
]->DecRef();
671 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
673 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
675 int n
= m_rowsOrCols
.Index(rowOrCol
);
676 if ( n
!= wxNOT_FOUND
)
678 attr
= m_attrs
[(size_t)n
];
685 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
687 int n
= m_rowsOrCols
.Index(rowOrCol
);
688 if ( n
== wxNOT_FOUND
)
691 m_rowsOrCols
.Add(rowOrCol
);
698 // change the attribute
699 m_attrs
[(size_t)n
] = attr
;
703 // remove this attribute
704 m_attrs
[(size_t)n
]->DecRef();
705 m_rowsOrCols
.RemoveAt((size_t)n
);
706 m_attrs
.RemoveAt((size_t)n
);
711 // ----------------------------------------------------------------------------
712 // wxGridCellAttrProvider
713 // ----------------------------------------------------------------------------
715 wxGridCellAttrProvider::wxGridCellAttrProvider()
717 m_data
= (wxGridCellAttrProviderData
*)NULL
;
720 wxGridCellAttrProvider::~wxGridCellAttrProvider()
725 void wxGridCellAttrProvider::InitData()
727 m_data
= new wxGridCellAttrProviderData
;
730 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
732 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
735 // first look for the attribute of this specific cell
736 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
740 // then look for the col attr (col attributes are more common than
741 // the row ones, hence they have priority)
742 attr
= m_data
->m_colAttrs
.GetAttr(col
);
747 // finally try the row attributes
748 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
755 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
761 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
764 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
769 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
772 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
777 m_data
->m_colAttrs
.SetAttr(attr
, col
);
780 // ----------------------------------------------------------------------------
782 // ----------------------------------------------------------------------------
784 //////////////////////////////////////////////////////////////////////
786 // Abstract base class for grid data (the model)
788 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
791 wxGridTableBase::wxGridTableBase()
793 m_view
= (wxGrid
*) NULL
;
794 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
797 wxGridTableBase::~wxGridTableBase()
799 delete m_attrProvider
;
802 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
804 delete m_attrProvider
;
805 m_attrProvider
= attrProvider
;
808 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
810 if ( m_attrProvider
)
811 return m_attrProvider
->GetAttr(row
, col
);
813 return (wxGridCellAttr
*)NULL
;
816 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
818 if ( m_attrProvider
)
820 m_attrProvider
->SetAttr(attr
, row
, col
);
824 // as we take ownership of the pointer and don't store it, we must
830 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
832 if ( m_attrProvider
)
834 m_attrProvider
->SetRowAttr(attr
, row
);
838 // as we take ownership of the pointer and don't store it, we must
844 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
846 if ( m_attrProvider
)
848 m_attrProvider
->SetColAttr(attr
, col
);
852 // as we take ownership of the pointer and don't store it, we must
858 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
860 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
861 "but your derived table class does not override this function") );
866 bool wxGridTableBase::AppendRows( size_t numRows
)
868 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
869 "but your derived table class does not override this function"));
874 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
876 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
877 "but your derived table class does not override this function"));
882 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
884 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
885 "but your derived table class does not override this function"));
890 bool wxGridTableBase::AppendCols( size_t numCols
)
892 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
893 "but your derived table class does not override this function"));
898 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
900 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
901 "but your derived table class does not override this function"));
907 wxString
wxGridTableBase::GetRowLabelValue( int row
)
914 wxString
wxGridTableBase::GetColLabelValue( int col
)
916 // default col labels are:
917 // cols 0 to 25 : A-Z
918 // cols 26 to 675 : AA-ZZ
925 s
+= (_T('A') + (wxChar
)( col%26
));
927 if ( col
< 0 ) break;
930 // reverse the string...
932 for ( i
= 0; i
< n
; i
++ )
942 //////////////////////////////////////////////////////////////////////
944 // Message class for the grid table to send requests and notifications
948 wxGridTableMessage::wxGridTableMessage()
950 m_table
= (wxGridTableBase
*) NULL
;
956 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
957 int commandInt1
, int commandInt2
)
961 m_comInt1
= commandInt1
;
962 m_comInt2
= commandInt2
;
967 //////////////////////////////////////////////////////////////////////
969 // A basic grid table for string data. An object of this class will
970 // created by wxGrid if you don't specify an alternative table class.
973 WX_DEFINE_OBJARRAY(wxGridStringArray
)
975 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
977 wxGridStringTable::wxGridStringTable()
982 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
987 m_data
.Alloc( numRows
);
991 for ( col
= 0; col
< numCols
; col
++ )
993 sa
.Add( wxEmptyString
);
996 for ( row
= 0; row
< numRows
; row
++ )
1002 wxGridStringTable::~wxGridStringTable()
1006 long wxGridStringTable::GetNumberRows()
1008 return m_data
.GetCount();
1011 long wxGridStringTable::GetNumberCols()
1013 if ( m_data
.GetCount() > 0 )
1014 return m_data
[0].GetCount();
1019 wxString
wxGridStringTable::GetValue( int row
, int col
)
1021 // TODO: bounds checking
1023 return m_data
[row
][col
];
1026 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1028 // TODO: bounds checking
1030 m_data
[row
][col
] = s
;
1033 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1035 // TODO: bounds checking
1037 return (m_data
[row
][col
] == wxEmptyString
);
1041 void wxGridStringTable::Clear()
1044 int numRows
, numCols
;
1046 numRows
= m_data
.GetCount();
1049 numCols
= m_data
[0].GetCount();
1051 for ( row
= 0; row
< numRows
; row
++ )
1053 for ( col
= 0; col
< numCols
; col
++ )
1055 m_data
[row
][col
] = wxEmptyString
;
1062 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1066 size_t curNumRows
= m_data
.GetCount();
1067 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1069 if ( pos
>= curNumRows
)
1071 return AppendRows( numRows
);
1075 sa
.Alloc( curNumCols
);
1076 for ( col
= 0; col
< curNumCols
; col
++ )
1078 sa
.Add( wxEmptyString
);
1081 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1083 m_data
.Insert( sa
, row
);
1088 wxGridTableMessage
msg( this,
1089 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1093 GetView()->ProcessTableMessage( msg
);
1099 bool wxGridStringTable::AppendRows( size_t numRows
)
1103 size_t curNumRows
= m_data
.GetCount();
1104 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1107 if ( curNumCols
> 0 )
1109 sa
.Alloc( curNumCols
);
1110 for ( col
= 0; col
< curNumCols
; col
++ )
1112 sa
.Add( wxEmptyString
);
1116 for ( row
= 0; row
< numRows
; row
++ )
1123 wxGridTableMessage
msg( this,
1124 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1127 GetView()->ProcessTableMessage( msg
);
1133 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1137 size_t curNumRows
= m_data
.GetCount();
1139 if ( pos
>= curNumRows
)
1142 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1143 "Pos value is invalid for present table with %d rows",
1144 pos
, numRows
, curNumRows
);
1145 wxFAIL_MSG( wxT(errmsg
) );
1149 if ( numRows
> curNumRows
- pos
)
1151 numRows
= curNumRows
- pos
;
1154 if ( numRows
>= curNumRows
)
1156 m_data
.Empty(); // don't release memory just yet
1160 for ( n
= 0; n
< numRows
; n
++ )
1162 m_data
.Remove( pos
);
1168 wxGridTableMessage
msg( this,
1169 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1173 GetView()->ProcessTableMessage( msg
);
1179 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1183 size_t curNumRows
= m_data
.GetCount();
1184 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1186 if ( pos
>= curNumCols
)
1188 return AppendCols( numCols
);
1191 for ( row
= 0; row
< curNumRows
; row
++ )
1193 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1195 m_data
[row
].Insert( wxEmptyString
, col
);
1201 wxGridTableMessage
msg( this,
1202 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1206 GetView()->ProcessTableMessage( msg
);
1212 bool wxGridStringTable::AppendCols( size_t numCols
)
1216 size_t curNumRows
= m_data
.GetCount();
1219 // TODO: something better than this ?
1221 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1222 "Call AppendRows() first") );
1226 for ( row
= 0; row
< curNumRows
; row
++ )
1228 for ( n
= 0; n
< numCols
; n
++ )
1230 m_data
[row
].Add( wxEmptyString
);
1236 wxGridTableMessage
msg( this,
1237 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1240 GetView()->ProcessTableMessage( msg
);
1246 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1250 size_t curNumRows
= m_data
.GetCount();
1251 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1253 if ( pos
>= curNumCols
)
1256 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1257 "Pos value is invalid for present table with %d cols",
1258 pos
, numCols
, curNumCols
);
1259 wxFAIL_MSG( wxT( errmsg
) );
1263 if ( numCols
> curNumCols
- pos
)
1265 numCols
= curNumCols
- pos
;
1268 for ( row
= 0; row
< curNumRows
; row
++ )
1270 if ( numCols
>= curNumCols
)
1272 m_data
[row
].Clear();
1276 for ( n
= 0; n
< numCols
; n
++ )
1278 m_data
[row
].Remove( pos
);
1285 wxGridTableMessage
msg( this,
1286 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1290 GetView()->ProcessTableMessage( msg
);
1296 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1298 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1300 // using default label
1302 return wxGridTableBase::GetRowLabelValue( row
);
1306 return m_rowLabels
[ row
];
1310 wxString
wxGridStringTable::GetColLabelValue( int col
)
1312 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1314 // using default label
1316 return wxGridTableBase::GetColLabelValue( col
);
1320 return m_colLabels
[ col
];
1324 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1326 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1328 int n
= m_rowLabels
.GetCount();
1330 for ( i
= n
; i
<= row
; i
++ )
1332 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1336 m_rowLabels
[row
] = value
;
1339 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1341 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1343 int n
= m_colLabels
.GetCount();
1345 for ( i
= n
; i
<= col
; i
++ )
1347 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1351 m_colLabels
[col
] = value
;
1356 //////////////////////////////////////////////////////////////////////
1357 //////////////////////////////////////////////////////////////////////
1359 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1361 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1362 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1363 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1364 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1367 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1369 const wxPoint
&pos
, const wxSize
&size
)
1370 : wxWindow( parent
, id
, pos
, size
)
1375 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1379 // NO - don't do this because it will set both the x and y origin
1380 // coords to match the parent scrolled window and we just want to
1381 // set the y coord - MB
1383 // m_owner->PrepareDC( dc );
1386 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1387 dc
.SetDeviceOrigin( 0, -y
);
1389 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1390 m_owner
->DrawRowLabels( dc
);
1394 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1396 m_owner
->ProcessRowLabelMouseEvent( event
);
1400 // This seems to be required for wxMotif otherwise the mouse
1401 // cursor must be in the cell edit control to get key events
1403 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1405 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1410 //////////////////////////////////////////////////////////////////////
1412 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1414 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1415 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1416 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1417 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1420 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1422 const wxPoint
&pos
, const wxSize
&size
)
1423 : wxWindow( parent
, id
, pos
, size
)
1428 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1432 // NO - don't do this because it will set both the x and y origin
1433 // coords to match the parent scrolled window and we just want to
1434 // set the x coord - MB
1436 // m_owner->PrepareDC( dc );
1439 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1440 dc
.SetDeviceOrigin( -x
, 0 );
1442 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1443 m_owner
->DrawColLabels( dc
);
1447 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1449 m_owner
->ProcessColLabelMouseEvent( event
);
1453 // This seems to be required for wxMotif otherwise the mouse
1454 // cursor must be in the cell edit control to get key events
1456 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1458 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1463 //////////////////////////////////////////////////////////////////////
1465 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1467 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1468 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1469 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1470 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1473 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1475 const wxPoint
&pos
, const wxSize
&size
)
1476 : wxWindow( parent
, id
, pos
, size
)
1481 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1485 int client_height
= 0;
1486 int client_width
= 0;
1487 GetClientSize( &client_width
, &client_height
);
1489 dc
.SetPen( *wxBLACK_PEN
);
1490 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1491 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1493 dc
.SetPen( *wxWHITE_PEN
);
1494 dc
.DrawLine( 0, 0, client_width
, 0 );
1495 dc
.DrawLine( 0, 0, 0, client_height
);
1499 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1501 m_owner
->ProcessCornerLabelMouseEvent( event
);
1505 // This seems to be required for wxMotif otherwise the mouse
1506 // cursor must be in the cell edit control to get key events
1508 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1510 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1515 //////////////////////////////////////////////////////////////////////
1517 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1519 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1520 EVT_PAINT( wxGridWindow::OnPaint
)
1521 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1522 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1523 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1526 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1527 wxGridRowLabelWindow
*rowLblWin
,
1528 wxGridColLabelWindow
*colLblWin
,
1529 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1530 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1533 m_rowLabelWin
= rowLblWin
;
1534 m_colLabelWin
= colLblWin
;
1535 SetBackgroundColour( "WHITE" );
1539 wxGridWindow::~wxGridWindow()
1544 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1546 wxPaintDC
dc( this );
1547 m_owner
->PrepareDC( dc
);
1548 wxRegion reg
= GetUpdateRegion();
1549 m_owner
->CalcCellsExposed( reg
);
1550 m_owner
->DrawGridCellArea( dc
);
1551 #if WXGRID_DRAW_LINES
1552 m_owner
->DrawAllGridLines( dc
, reg
);
1557 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1559 wxPanel::ScrollWindow( dx
, dy
, rect
);
1560 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1561 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1565 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1567 m_owner
->ProcessGridCellMouseEvent( event
);
1571 // This seems to be required for wxMotif otherwise the mouse
1572 // cursor must be in the cell edit control to get key events
1574 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1576 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1579 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1585 //////////////////////////////////////////////////////////////////////
1588 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1590 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1591 EVT_PAINT( wxGrid::OnPaint
)
1592 EVT_SIZE( wxGrid::OnSize
)
1593 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1594 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1597 wxGrid::wxGrid( wxWindow
*parent
,
1602 const wxString
& name
)
1603 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1612 m_defaultCellAttr
->SafeDecRef();
1614 #ifdef DEBUG_ATTR_CACHE
1615 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1616 wxPrintf(_T("wxGrid attribute cache statistics: "
1617 "total: %u, hits: %u (%u%%)\n"),
1618 total
, gs_nAttrCacheHits
,
1619 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1628 // ----- internal init and update functions
1631 void wxGrid::Create()
1633 m_created
= FALSE
; // set to TRUE by CreateGrid
1634 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
1636 m_table
= (wxGridTableBase
*) NULL
;
1639 m_cellEditCtrlEnabled
= FALSE
;
1641 m_defaultCellAttr
= new wxGridCellAttr
;
1642 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1643 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1648 m_currentCellCoords
= wxGridNoCellCoords
;
1650 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1651 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1653 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1658 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1663 m_colLabelWin
= new wxGridColLabelWindow( this,
1668 m_gridWin
= new wxGridWindow( this,
1675 SetTargetWindow( m_gridWin
);
1679 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1683 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1688 m_numRows
= numRows
;
1689 m_numCols
= numCols
;
1691 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1692 m_table
->SetView( this );
1701 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1705 // RD: Actually, this should probably be allowed. I think it would be
1706 // nice to be able to switch multiple Tables in and out of a single
1707 // View at runtime. Is there anything in the implmentation that would
1710 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1715 m_numRows
= table
->GetNumberRows();
1716 m_numCols
= table
->GetNumberCols();
1719 m_table
->SetView( this );
1734 if ( m_numRows
<= 0 )
1735 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1737 if ( m_numCols
<= 0 )
1738 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1740 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1741 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1743 if ( m_rowLabelWin
)
1745 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1749 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1752 m_labelTextColour
= wxColour( _T("BLACK") );
1755 m_attrCache
.row
= -1;
1757 // TODO: something better than this ?
1759 m_labelFont
= this->GetFont();
1760 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1762 m_rowLabelHorizAlign
= wxLEFT
;
1763 m_rowLabelVertAlign
= wxCENTRE
;
1765 m_colLabelHorizAlign
= wxCENTRE
;
1766 m_colLabelVertAlign
= wxTOP
;
1768 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1769 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1771 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1772 m_defaultRowHeight
+= 8;
1774 m_defaultRowHeight
+= 4;
1777 m_rowHeights
.Alloc( m_numRows
);
1778 m_rowBottoms
.Alloc( m_numRows
);
1780 for ( i
= 0; i
< m_numRows
; i
++ )
1782 m_rowHeights
.Add( m_defaultRowHeight
);
1783 rowBottom
+= m_defaultRowHeight
;
1784 m_rowBottoms
.Add( rowBottom
);
1787 m_colWidths
.Alloc( m_numCols
);
1788 m_colRights
.Alloc( m_numCols
);
1790 for ( i
= 0; i
< m_numCols
; i
++ )
1792 m_colWidths
.Add( m_defaultColWidth
);
1793 colRight
+= m_defaultColWidth
;
1794 m_colRights
.Add( colRight
);
1797 // Set default cell attributes
1798 m_defaultCellAttr
->SetFont(GetFont());
1799 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1800 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1801 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1802 m_defaultCellAttr
->SetTextColour(
1803 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1804 m_defaultCellAttr
->SetBackgroundColour(
1805 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1808 m_gridLineColour
= wxColour( 128, 128, 255 );
1809 m_gridLinesEnabled
= TRUE
;
1811 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1812 m_winCapture
= (wxWindow
*)NULL
;
1814 m_dragRowOrCol
= -1;
1815 m_isDragging
= FALSE
;
1816 m_startDragPos
= wxDefaultPosition
;
1818 m_waitForSlowClick
= FALSE
;
1820 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1821 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1823 m_currentCellCoords
= wxGridNoCellCoords
;
1825 m_selectedTopLeft
= wxGridNoCellCoords
;
1826 m_selectedBottomRight
= wxGridNoCellCoords
;
1827 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
1828 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1830 m_editable
= TRUE
; // default for whole grid
1832 m_inOnKeyDown
= FALSE
;
1838 void wxGrid::CalcDimensions()
1841 GetClientSize( &cw
, &ch
);
1843 if ( m_numRows
> 0 && m_numCols
> 0 )
1845 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1846 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1848 // TODO: restore the scroll position that we had before sizing
1851 GetViewStart( &x
, &y
);
1852 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1853 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1859 void wxGrid::CalcWindowSizes()
1862 GetClientSize( &cw
, &ch
);
1864 if ( m_cornerLabelWin
->IsShown() )
1865 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1867 if ( m_colLabelWin
->IsShown() )
1868 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1870 if ( m_rowLabelWin
->IsShown() )
1871 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1873 if ( m_gridWin
->IsShown() )
1874 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1878 // this is called when the grid table sends a message to say that it
1879 // has been redimensioned
1881 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1885 switch ( msg
.GetId() )
1887 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1889 size_t pos
= msg
.GetCommandInt();
1890 int numRows
= msg
.GetCommandInt2();
1891 for ( i
= 0; i
< numRows
; i
++ )
1893 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1894 m_rowBottoms
.Insert( 0, pos
);
1896 m_numRows
+= numRows
;
1899 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1901 for ( i
= pos
; i
< m_numRows
; i
++ )
1903 bottom
+= m_rowHeights
[i
];
1904 m_rowBottoms
[i
] = bottom
;
1910 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1912 int numRows
= msg
.GetCommandInt();
1913 for ( i
= 0; i
< numRows
; i
++ )
1915 m_rowHeights
.Add( m_defaultRowHeight
);
1916 m_rowBottoms
.Add( 0 );
1919 int oldNumRows
= m_numRows
;
1920 m_numRows
+= numRows
;
1923 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1925 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1927 bottom
+= m_rowHeights
[i
];
1928 m_rowBottoms
[i
] = bottom
;
1934 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1936 size_t pos
= msg
.GetCommandInt();
1937 int numRows
= msg
.GetCommandInt2();
1938 for ( i
= 0; i
< numRows
; i
++ )
1940 m_rowHeights
.Remove( pos
);
1941 m_rowBottoms
.Remove( pos
);
1943 m_numRows
-= numRows
;
1948 m_colWidths
.Clear();
1949 m_colRights
.Clear();
1950 m_currentCellCoords
= wxGridNoCellCoords
;
1954 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1955 m_currentCellCoords
.Set( 0, 0 );
1958 for ( i
= 0; i
< m_numRows
; i
++ )
1960 h
+= m_rowHeights
[i
];
1961 m_rowBottoms
[i
] = h
;
1969 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1971 size_t pos
= msg
.GetCommandInt();
1972 int numCols
= msg
.GetCommandInt2();
1973 for ( i
= 0; i
< numCols
; i
++ )
1975 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1976 m_colRights
.Insert( 0, pos
);
1978 m_numCols
+= numCols
;
1981 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1983 for ( i
= pos
; i
< m_numCols
; i
++ )
1985 right
+= m_colWidths
[i
];
1986 m_colRights
[i
] = right
;
1992 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1994 int numCols
= msg
.GetCommandInt();
1995 for ( i
= 0; i
< numCols
; i
++ )
1997 m_colWidths
.Add( m_defaultColWidth
);
1998 m_colRights
.Add( 0 );
2001 int oldNumCols
= m_numCols
;
2002 m_numCols
+= numCols
;
2005 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2007 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2009 right
+= m_colWidths
[i
];
2010 m_colRights
[i
] = right
;
2016 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2018 size_t pos
= msg
.GetCommandInt();
2019 int numCols
= msg
.GetCommandInt2();
2020 for ( i
= 0; i
< numCols
; i
++ )
2022 m_colWidths
.Remove( pos
);
2023 m_colRights
.Remove( pos
);
2025 m_numCols
-= numCols
;
2029 #if 0 // leave the row alone here so that AppendCols will work subsequently
2031 m_rowHeights
.Clear();
2032 m_rowBottoms
.Clear();
2034 m_currentCellCoords
= wxGridNoCellCoords
;
2038 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2039 m_currentCellCoords
.Set( 0, 0 );
2042 for ( i
= 0; i
< m_numCols
; i
++ )
2044 w
+= m_colWidths
[i
];
2057 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2059 wxRegionIterator
iter( reg
);
2062 m_rowLabelsExposed
.Empty();
2069 // TODO: remove this when we can...
2070 // There is a bug in wxMotif that gives garbage update
2071 // rectangles if you jump-scroll a long way by clicking the
2072 // scrollbar with middle button. This is a work-around
2074 #if defined(__WXMOTIF__)
2076 m_gridWin
->GetClientSize( &cw
, &ch
);
2077 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2078 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2081 // logical bounds of update region
2084 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2085 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2087 // find the row labels within these bounds
2091 for ( row
= 0; row
< m_numRows
; row
++ )
2093 if ( m_rowBottoms
[row
] < top
) continue;
2095 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2096 if ( rowTop
> bottom
) break;
2098 m_rowLabelsExposed
.Add( row
);
2106 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2108 wxRegionIterator
iter( reg
);
2111 m_colLabelsExposed
.Empty();
2118 // TODO: remove this when we can...
2119 // There is a bug in wxMotif that gives garbage update
2120 // rectangles if you jump-scroll a long way by clicking the
2121 // scrollbar with middle button. This is a work-around
2123 #if defined(__WXMOTIF__)
2125 m_gridWin
->GetClientSize( &cw
, &ch
);
2126 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2127 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2130 // logical bounds of update region
2133 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2134 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2136 // find the cells within these bounds
2140 for ( col
= 0; col
< m_numCols
; col
++ )
2142 if ( m_colRights
[col
] < left
) continue;
2144 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2145 if ( colLeft
> right
) break;
2147 m_colLabelsExposed
.Add( col
);
2155 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2157 wxRegionIterator
iter( reg
);
2160 m_cellsExposed
.Empty();
2161 m_rowsExposed
.Empty();
2162 m_colsExposed
.Empty();
2164 int left
, top
, right
, bottom
;
2169 // TODO: remove this when we can...
2170 // There is a bug in wxMotif that gives garbage update
2171 // rectangles if you jump-scroll a long way by clicking the
2172 // scrollbar with middle button. This is a work-around
2174 #if defined(__WXMOTIF__)
2176 m_gridWin
->GetClientSize( &cw
, &ch
);
2177 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2178 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2179 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2180 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2183 // logical bounds of update region
2185 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2186 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2188 // find the cells within these bounds
2191 int colLeft
, rowTop
;
2192 for ( row
= 0; row
< m_numRows
; row
++ )
2194 if ( m_rowBottoms
[row
] <= top
) continue;
2196 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2197 if ( rowTop
> bottom
) break;
2199 m_rowsExposed
.Add( row
);
2201 for ( col
= 0; col
< m_numCols
; col
++ )
2203 if ( m_colRights
[col
] <= left
) continue;
2205 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2206 if ( colLeft
> right
) break;
2208 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2209 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2218 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2221 wxPoint
pos( event
.GetPosition() );
2222 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2224 if ( event
.Dragging() )
2226 m_isDragging
= TRUE
;
2228 if ( event
.LeftIsDown() )
2230 switch( m_cursorMode
)
2232 case WXGRID_CURSOR_RESIZE_ROW
:
2234 int cw
, ch
, left
, dummy
;
2235 m_gridWin
->GetClientSize( &cw
, &ch
);
2236 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2238 wxClientDC
dc( m_gridWin
);
2241 m_rowBottoms
[m_dragRowOrCol
] -
2242 m_rowHeights
[m_dragRowOrCol
] +
2243 WXGRID_MIN_ROW_HEIGHT
);
2244 dc
.SetLogicalFunction(wxINVERT
);
2245 if ( m_dragLastPos
>= 0 )
2247 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2249 dc
.DrawLine( left
, y
, left
+cw
, y
);
2254 case WXGRID_CURSOR_SELECT_ROW
:
2255 if ( (row
= YToRow( y
)) >= 0 &&
2256 !IsInSelection( row
, 0 ) )
2258 SelectRow( row
, TRUE
);
2261 // default label to suppress warnings about "enumeration value
2262 // 'xxx' not handled in switch
2270 m_isDragging
= FALSE
;
2273 // ------------ Entering or leaving the window
2275 if ( event
.Entering() || event
.Leaving() )
2277 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2281 // ------------ Left button pressed
2283 else if ( event
.LeftDown() )
2285 // don't send a label click event for a hit on the
2286 // edge of the row label - this is probably the user
2287 // wanting to resize the row
2289 if ( YToEdgeOfRow(y
) < 0 )
2293 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2295 SelectRow( row
, event
.ShiftDown() );
2296 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2301 // starting to drag-resize a row
2303 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2308 // ------------ Left double click
2310 else if (event
.LeftDClick() )
2312 if ( YToEdgeOfRow(y
) < 0 )
2315 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2320 // ------------ Left button released
2322 else if ( event
.LeftUp() )
2324 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2326 DoEndDragResizeRow();
2328 // Note: we are ending the event *after* doing
2329 // default processing in this case
2331 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2334 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2339 // ------------ Right button down
2341 else if ( event
.RightDown() )
2344 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2346 // no default action at the moment
2351 // ------------ Right double click
2353 else if ( event
.RightDClick() )
2356 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2358 // no default action at the moment
2363 // ------------ No buttons down and mouse moving
2365 else if ( event
.Moving() )
2367 m_dragRowOrCol
= YToEdgeOfRow( y
);
2368 if ( m_dragRowOrCol
>= 0 )
2370 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2372 // don't capture the mouse yet
2373 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2376 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2378 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2384 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2387 wxPoint
pos( event
.GetPosition() );
2388 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2390 if ( event
.Dragging() )
2392 m_isDragging
= TRUE
;
2394 if ( event
.LeftIsDown() )
2396 switch( m_cursorMode
)
2398 case WXGRID_CURSOR_RESIZE_COL
:
2400 int cw
, ch
, dummy
, top
;
2401 m_gridWin
->GetClientSize( &cw
, &ch
);
2402 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2404 wxClientDC
dc( m_gridWin
);
2407 m_colRights
[m_dragRowOrCol
] -
2408 m_colWidths
[m_dragRowOrCol
] +
2409 WXGRID_MIN_COL_WIDTH
);
2410 dc
.SetLogicalFunction(wxINVERT
);
2411 if ( m_dragLastPos
>= 0 )
2413 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2415 dc
.DrawLine( x
, top
, x
, top
+ch
);
2420 case WXGRID_CURSOR_SELECT_COL
:
2421 if ( (col
= XToCol( x
)) >= 0 &&
2422 !IsInSelection( 0, col
) )
2424 SelectCol( col
, TRUE
);
2427 // default label to suppress warnings about "enumeration value
2428 // 'xxx' not handled in switch
2436 m_isDragging
= FALSE
;
2439 // ------------ Entering or leaving the window
2441 if ( event
.Entering() || event
.Leaving() )
2443 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2447 // ------------ Left button pressed
2449 else if ( event
.LeftDown() )
2451 // don't send a label click event for a hit on the
2452 // edge of the col label - this is probably the user
2453 // wanting to resize the col
2455 if ( XToEdgeOfCol(x
) < 0 )
2459 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2461 SelectCol( col
, event
.ShiftDown() );
2462 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2467 // starting to drag-resize a col
2469 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2474 // ------------ Left double click
2476 if ( event
.LeftDClick() )
2478 if ( XToEdgeOfCol(x
) < 0 )
2481 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2486 // ------------ Left button released
2488 else if ( event
.LeftUp() )
2490 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2492 DoEndDragResizeCol();
2494 // Note: we are ending the event *after* doing
2495 // default processing in this case
2497 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2500 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2505 // ------------ Right button down
2507 else if ( event
.RightDown() )
2510 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2512 // no default action at the moment
2517 // ------------ Right double click
2519 else if ( event
.RightDClick() )
2522 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2524 // no default action at the moment
2529 // ------------ No buttons down and mouse moving
2531 else if ( event
.Moving() )
2533 m_dragRowOrCol
= XToEdgeOfCol( x
);
2534 if ( m_dragRowOrCol
>= 0 )
2536 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2538 // don't capture the cursor yet
2539 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2542 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2544 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2550 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2552 if ( event
.LeftDown() )
2554 // indicate corner label by having both row and
2557 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2563 else if ( event
.LeftDClick() )
2565 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2568 else if ( event
.RightDown() )
2570 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2572 // no default action at the moment
2576 else if ( event
.RightDClick() )
2578 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2580 // no default action at the moment
2585 void wxGrid::ChangeCursorMode(CursorMode mode
,
2590 static const wxChar
*cursorModes
[] =
2599 wxLogTrace(_T("grid"),
2600 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2601 win
== m_colLabelWin
? _T("colLabelWin")
2602 : win
? _T("rowLabelWin")
2604 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2605 #endif // __WXDEBUG__
2607 if ( mode
== m_cursorMode
)
2612 // by default use the grid itself
2618 m_winCapture
->ReleaseMouse();
2619 m_winCapture
= (wxWindow
*)NULL
;
2622 m_cursorMode
= mode
;
2624 switch ( m_cursorMode
)
2626 case WXGRID_CURSOR_RESIZE_ROW
:
2627 win
->SetCursor( m_rowResizeCursor
);
2630 case WXGRID_CURSOR_RESIZE_COL
:
2631 win
->SetCursor( m_colResizeCursor
);
2635 win
->SetCursor( *wxSTANDARD_CURSOR
);
2638 // we need to capture mouse when resizing
2639 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2640 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2642 if ( captureMouse
&& resize
)
2644 win
->CaptureMouse();
2649 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2652 wxPoint
pos( event
.GetPosition() );
2653 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2655 wxGridCellCoords coords
;
2656 XYToCell( x
, y
, coords
);
2658 if ( event
.Dragging() )
2660 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
2662 // Don't start doing anything until the mouse has been drug at
2663 // least 3 pixels in any direction...
2664 if (! m_isDragging
) {
2665 if (m_startDragPos
== wxDefaultPosition
) {
2666 m_startDragPos
= pos
;
2669 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
2673 m_isDragging
= TRUE
;
2674 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2676 // Hide the edit control, so it
2677 // won't interfer with drag-shrinking.
2678 if ( IsCellEditControlEnabled() )
2679 HideCellEditControl();
2681 // Have we captured the mouse yet?
2682 if (! m_winCapture
) {
2683 m_winCapture
= m_gridWin
;
2684 m_winCapture
->CaptureMouse();
2687 if ( coords
!= wxGridNoCellCoords
)
2689 if ( !IsSelection() )
2691 SelectBlock( coords
, coords
);
2695 SelectBlock( m_currentCellCoords
, coords
);
2698 if (! IsVisible(coords
)) {
2699 MakeCellVisible(coords
);
2700 // TODO: need to introduce a delay or something here. The
2701 // scrolling is way to fast, at least on MSW.
2705 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2707 int cw
, ch
, left
, dummy
;
2708 m_gridWin
->GetClientSize( &cw
, &ch
);
2709 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2711 wxClientDC
dc( m_gridWin
);
2714 m_rowBottoms
[m_dragRowOrCol
] -
2715 m_rowHeights
[m_dragRowOrCol
] +
2716 WXGRID_MIN_ROW_HEIGHT
);
2717 dc
.SetLogicalFunction(wxINVERT
);
2718 if ( m_dragLastPos
>= 0 )
2720 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2722 dc
.DrawLine( left
, y
, left
+cw
, y
);
2725 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2727 int cw
, ch
, dummy
, top
;
2728 m_gridWin
->GetClientSize( &cw
, &ch
);
2729 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2731 wxClientDC
dc( m_gridWin
);
2734 m_colRights
[m_dragRowOrCol
] -
2735 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
2736 dc
.SetLogicalFunction(wxINVERT
);
2737 if ( m_dragLastPos
>= 0 )
2739 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2741 dc
.DrawLine( x
, top
, x
, top
+ch
);
2748 m_isDragging
= FALSE
;
2749 m_startDragPos
= wxDefaultPosition
;
2752 if ( coords
!= wxGridNoCellCoords
)
2754 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2755 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2758 if ( event
.Entering() || event
.Leaving() )
2760 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2761 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2766 // ------------ Left button pressed
2768 if ( event
.LeftDown() )
2770 EnableCellEditControl( FALSE
);
2771 if ( event
.ShiftDown() )
2773 SelectBlock( m_currentCellCoords
, coords
);
2775 else if ( XToEdgeOfCol(x
) < 0 &&
2776 YToEdgeOfRow(y
) < 0 )
2778 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2783 MakeCellVisible( coords
);
2785 // if this is the second click on this cell then start
2787 if (m_waitForSlowClick
&& coords
== m_currentCellCoords
) {
2788 EnableCellEditControl(TRUE
);
2789 ShowCellEditControl();
2790 m_waitForSlowClick
= FALSE
;
2793 SetCurrentCell( coords
);
2794 m_waitForSlowClick
= TRUE
;
2801 // ------------ Left double click
2803 else if ( event
.LeftDClick() )
2805 EnableCellEditControl( FALSE
);
2806 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2808 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2816 // ------------ Left button released
2818 else if ( event
.LeftUp() )
2820 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2822 if ( IsSelection() )
2825 m_winCapture
->ReleaseMouse();
2826 m_winCapture
= NULL
;
2828 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2831 // Show the edit control, if it has
2832 // been hidden for drag-shrinking.
2833 if ( IsCellEditControlEnabled() )
2834 ShowCellEditControl();
2836 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2838 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2839 DoEndDragResizeRow();
2841 // Note: we are ending the event *after* doing
2842 // default processing in this case
2844 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2846 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2848 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2849 DoEndDragResizeCol();
2851 // Note: we are ending the event *after* doing
2852 // default processing in this case
2854 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2861 // ------------ Right button down
2863 else if ( event
.RightDown() )
2865 EnableCellEditControl( FALSE
);
2866 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2871 // no default action at the moment
2876 // ------------ Right double click
2878 else if ( event
.RightDClick() )
2880 EnableCellEditControl( FALSE
);
2881 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2886 // no default action at the moment
2890 // ------------ Moving and no button action
2892 else if ( event
.Moving() && !event
.IsButton() )
2894 int dragRow
= YToEdgeOfRow( y
);
2895 int dragCol
= XToEdgeOfCol( x
);
2897 // Dragging on the corner of a cell to resize in both
2898 // directions is not implemented yet...
2900 if ( dragRow
>= 0 && dragCol
>= 0 )
2902 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2908 m_dragRowOrCol
= dragRow
;
2910 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2912 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
2920 m_dragRowOrCol
= dragCol
;
2922 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2924 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
2930 // Neither on a row or col edge
2932 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2934 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2941 void wxGrid::DoEndDragResizeRow()
2943 if ( m_dragLastPos
>= 0 )
2945 // erase the last line and resize the row
2947 int cw
, ch
, left
, dummy
;
2948 m_gridWin
->GetClientSize( &cw
, &ch
);
2949 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2951 wxClientDC
dc( m_gridWin
);
2953 dc
.SetLogicalFunction( wxINVERT
);
2954 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2955 HideCellEditControl();
2957 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
2958 SetRowSize( m_dragRowOrCol
,
2959 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
2961 if ( !GetBatchCount() )
2963 // Only needed to get the correct rect.y:
2964 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
2966 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
2967 rect
.width
= m_rowLabelWidth
;
2968 rect
.height
= ch
- rect
.y
;
2969 m_rowLabelWin
->Refresh( TRUE
, &rect
);
2971 m_gridWin
->Refresh( FALSE
, &rect
);
2974 ShowCellEditControl();
2979 void wxGrid::DoEndDragResizeCol()
2981 if ( m_dragLastPos
>= 0 )
2983 // erase the last line and resize the col
2985 int cw
, ch
, dummy
, top
;
2986 m_gridWin
->GetClientSize( &cw
, &ch
);
2987 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2989 wxClientDC
dc( m_gridWin
);
2991 dc
.SetLogicalFunction( wxINVERT
);
2992 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2993 HideCellEditControl();
2995 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
2996 SetColSize( m_dragRowOrCol
,
2997 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
2999 if ( !GetBatchCount() )
3001 // Only needed to get the correct rect.x:
3002 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3004 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3005 rect
.width
= cw
- rect
.x
;
3006 rect
.height
= m_colLabelHeight
;
3007 m_colLabelWin
->Refresh( TRUE
, &rect
);
3009 m_gridWin
->Refresh( FALSE
, &rect
);
3012 ShowCellEditControl();
3019 // ------ interaction with data model
3021 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3023 switch ( msg
.GetId() )
3025 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3026 return GetModelValues();
3028 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3029 return SetModelValues();
3031 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3032 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3033 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3034 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3035 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3036 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3037 return Redimension( msg
);
3046 // The behaviour of this function depends on the grid table class
3047 // Clear() function. For the default wxGridStringTable class the
3048 // behavious is to replace all cell contents with wxEmptyString but
3049 // not to change the number of rows or cols.
3051 void wxGrid::ClearGrid()
3056 SetEditControlValue();
3057 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3062 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3064 // TODO: something with updateLabels flag
3068 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3074 bool ok
= m_table
->InsertRows( pos
, numRows
);
3076 // the table will have sent the results of the insert row
3077 // operation to this view object as a grid table message
3081 if ( m_numCols
== 0 )
3083 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3085 // TODO: perhaps instead of appending the default number of cols
3086 // we should remember what the last non-zero number of cols was ?
3090 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3092 // if we have just inserted cols into an empty grid the current
3093 // cell will be undefined...
3095 SetCurrentCell( 0, 0 );
3099 if ( !GetBatchCount() ) Refresh();
3102 SetEditControlValue();
3112 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3114 // TODO: something with updateLabels flag
3118 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3122 if ( m_table
&& m_table
->AppendRows( numRows
) )
3124 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3126 // if we have just inserted cols into an empty grid the current
3127 // cell will be undefined...
3129 SetCurrentCell( 0, 0 );
3132 // the table will have sent the results of the append row
3133 // operation to this view object as a grid table message
3136 if ( !GetBatchCount() ) Refresh();
3146 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3148 // TODO: something with updateLabels flag
3152 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3156 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3158 // the table will have sent the results of the delete row
3159 // operation to this view object as a grid table message
3161 if ( m_numRows
> 0 )
3162 SetEditControlValue();
3164 HideCellEditControl();
3167 if ( !GetBatchCount() ) Refresh();
3177 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3179 // TODO: something with updateLabels flag
3183 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3189 HideCellEditControl();
3190 bool ok
= m_table
->InsertCols( pos
, numCols
);
3192 // the table will have sent the results of the insert col
3193 // operation to this view object as a grid table message
3197 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3199 // if we have just inserted cols into an empty grid the current
3200 // cell will be undefined...
3202 SetCurrentCell( 0, 0 );
3206 if ( !GetBatchCount() ) Refresh();
3209 SetEditControlValue();
3219 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3221 // TODO: something with updateLabels flag
3225 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3229 if ( m_table
&& m_table
->AppendCols( numCols
) )
3231 // the table will have sent the results of the append col
3232 // operation to this view object as a grid table message
3234 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3236 // if we have just inserted cols into an empty grid the current
3237 // cell will be undefined...
3239 SetCurrentCell( 0, 0 );
3243 if ( !GetBatchCount() ) Refresh();
3253 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3255 // TODO: something with updateLabels flag
3259 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3263 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3265 // the table will have sent the results of the delete col
3266 // operation to this view object as a grid table message
3268 if ( m_numCols
> 0 )
3269 SetEditControlValue();
3271 HideCellEditControl();
3274 if ( !GetBatchCount() ) Refresh();
3286 // ----- event handlers
3289 // Generate a grid event based on a mouse event and
3290 // return the result of ProcessEvent()
3292 bool wxGrid::SendEvent( const wxEventType type
,
3294 wxMouseEvent
& mouseEv
)
3296 if ( type
== EVT_GRID_ROW_SIZE
||
3297 type
== EVT_GRID_COL_SIZE
)
3299 int rowOrCol
= (row
== -1 ? col
: row
);
3301 wxGridSizeEvent
gridEvt( GetId(),
3305 mouseEv
.GetX(), mouseEv
.GetY(),
3306 mouseEv
.ControlDown(),
3307 mouseEv
.ShiftDown(),
3309 mouseEv
.MetaDown() );
3311 return GetEventHandler()->ProcessEvent(gridEvt
);
3313 else if ( type
== EVT_GRID_RANGE_SELECT
)
3315 wxGridRangeSelectEvent
gridEvt( GetId(),
3319 m_selectedBottomRight
,
3320 mouseEv
.ControlDown(),
3321 mouseEv
.ShiftDown(),
3323 mouseEv
.MetaDown() );
3325 return GetEventHandler()->ProcessEvent(gridEvt
);
3329 wxGridEvent
gridEvt( GetId(),
3333 mouseEv
.GetX(), mouseEv
.GetY(),
3334 mouseEv
.ControlDown(),
3335 mouseEv
.ShiftDown(),
3337 mouseEv
.MetaDown() );
3339 return GetEventHandler()->ProcessEvent(gridEvt
);
3344 // Generate a grid event of specified type and return the result
3345 // of ProcessEvent().
3347 bool wxGrid::SendEvent( const wxEventType type
,
3350 if ( type
== EVT_GRID_ROW_SIZE
||
3351 type
== EVT_GRID_COL_SIZE
)
3353 int rowOrCol
= (row
== -1 ? col
: row
);
3355 wxGridSizeEvent
gridEvt( GetId(),
3360 return GetEventHandler()->ProcessEvent(gridEvt
);
3364 wxGridEvent
gridEvt( GetId(),
3369 return GetEventHandler()->ProcessEvent(gridEvt
);
3374 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3376 wxPaintDC
dc( this );
3378 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3379 m_numRows
&& m_numCols
)
3381 m_currentCellCoords
.Set(0, 0);
3382 SetEditControlValue();
3383 ShowCellEditControl();
3390 // This is just here to make sure that CalcDimensions gets called when
3391 // the grid view is resized... then the size event is skipped to allow
3392 // the box sizers to handle everything
3394 void wxGrid::OnSize( wxSizeEvent
& event
)
3401 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3403 if ( m_inOnKeyDown
)
3405 // shouldn't be here - we are going round in circles...
3407 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3410 m_inOnKeyDown
= TRUE
;
3412 // propagate the event up and see if it gets processed
3414 wxWindow
*parent
= GetParent();
3415 wxKeyEvent
keyEvt( event
);
3416 keyEvt
.SetEventObject( parent
);
3418 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3420 // try local handlers
3422 switch ( event
.KeyCode() )
3425 if ( event
.ControlDown() )
3427 MoveCursorUpBlock();
3436 if ( event
.ControlDown() )
3438 MoveCursorDownBlock();
3447 if ( event
.ControlDown() )
3449 MoveCursorLeftBlock();
3458 if ( event
.ControlDown() )
3460 MoveCursorRightBlock();
3469 if ( event
.ControlDown() )
3471 event
.Skip(); // to let the edit control have the return
3480 if (event
.ShiftDown())
3487 if ( event
.ControlDown() )
3489 MakeCellVisible( 0, 0 );
3490 SetCurrentCell( 0, 0 );
3499 if ( event
.ControlDown() )
3501 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3502 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3518 // We don't want these keys to trigger the edit control, any others?
3527 if ( !IsEditable() )
3532 // Otherwise fall through to default
3535 // now try the cell edit control
3537 if ( !IsCellEditControlEnabled() )
3538 EnableCellEditControl( TRUE
);
3539 if (IsCellEditControlEnabled()) {
3540 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3541 attr
->GetEditor()->StartingKey(event
);
3548 m_inOnKeyDown
= FALSE
;
3552 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3558 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3560 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3562 // the event has been intercepted - do nothing
3567 m_currentCellCoords
!= wxGridNoCellCoords
)
3569 HideCellEditControl();
3570 SaveEditControlValue();
3571 EnableCellEditControl(FALSE
);
3573 // Clear the old current cell highlight
3574 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3575 m_currentCellCoords
= coords
; // Otherwise refresh redraws the highlight!
3576 m_gridWin
->Refresh( FALSE
, &r
);
3579 m_currentCellCoords
= coords
;
3581 SetEditControlValue();
3585 wxClientDC
dc(m_gridWin
);
3587 DrawCellHighlight(dc
);
3589 if ( IsSelection() )
3591 wxRect
r( SelectionToDeviceRect() );
3593 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3600 // ------ functions to get/send data (see also public functions)
3603 bool wxGrid::GetModelValues()
3607 // all we need to do is repaint the grid
3609 m_gridWin
->Refresh();
3617 bool wxGrid::SetModelValues()
3623 for ( row
= 0; row
< m_numRows
; row
++ )
3625 for ( col
= 0; col
< m_numCols
; col
++ )
3627 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3639 // Note - this function only draws cells that are in the list of
3640 // exposed cells (usually set from the update region by
3641 // CalcExposedCells)
3643 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3645 if ( !m_numRows
|| !m_numCols
) return;
3648 size_t numCells
= m_cellsExposed
.GetCount();
3650 for ( i
= 0; i
< numCells
; i
++ )
3652 DrawCell( dc
, m_cellsExposed
[i
] );
3657 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3659 int row
= coords
.GetRow();
3660 int col
= coords
.GetCol();
3662 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3665 // we draw the cell border ourselves
3666 #if !WXGRID_DRAW_LINES
3667 if ( m_gridLinesEnabled
)
3668 DrawCellBorder( dc
, coords
);
3671 // but all the rest is drawn by the cell renderer and hence may be
3674 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3675 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3676 rect
.width
= m_colWidths
[col
];
3677 rect
.height
= m_rowHeights
[row
];
3679 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3680 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3683 if (m_currentCellCoords
== coords
)
3684 DrawCellHighlight(dc
);
3688 void wxGrid::DrawCellHighlight( wxDC
& dc
)
3690 int row
= m_currentCellCoords
.GetRow();
3691 int col
= m_currentCellCoords
.GetCol();
3693 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3697 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3698 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3699 rect
.width
= m_colWidths
[col
] - 1;
3700 rect
.height
= m_rowHeights
[row
] - 1;
3702 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
3703 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3705 dc
.DrawRectangle(rect
);
3708 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3710 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3711 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3713 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3714 int row
= coords
.GetRow();
3715 int col
= coords
.GetCol();
3717 // right hand border
3719 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3720 m_colRights
[col
], m_rowBottoms
[row
] );
3724 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3725 m_colRights
[col
], m_rowBottoms
[row
] );
3729 // TODO: remove this ???
3730 // This is used to redraw all grid lines e.g. when the grid line colour
3733 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3735 if ( !m_gridLinesEnabled
||
3737 !m_numCols
) return;
3739 int top
, bottom
, left
, right
;
3743 m_gridWin
->GetClientSize(&cw
, &ch
);
3745 // virtual coords of visible area
3747 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3748 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3752 reg
.GetBox(x
, y
, w
, h
);
3753 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3754 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3757 // avoid drawing grid lines past the last row and col
3759 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3760 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3762 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3764 // horizontal grid lines
3767 for ( i
= 0; i
< m_numRows
; i
++ )
3769 if ( m_rowBottoms
[i
]-1 > bottom
)
3773 else if ( m_rowBottoms
[i
]-1 >= top
)
3775 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
3780 // vertical grid lines
3782 for ( i
= 0; i
< m_numCols
; i
++ )
3784 if ( m_colRights
[i
]-1 > right
)
3788 else if ( m_colRights
[i
]-1 >= left
)
3790 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
3796 void wxGrid::DrawRowLabels( wxDC
& dc
)
3798 if ( !m_numRows
|| !m_numCols
) return;
3801 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3803 for ( i
= 0; i
< numLabels
; i
++ )
3805 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3810 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3812 if ( m_rowHeights
[row
] <= 0 ) return;
3814 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3816 dc
.SetPen( *wxBLACK_PEN
);
3817 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3818 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3820 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3821 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3823 dc
.SetPen( *wxWHITE_PEN
);
3824 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3825 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3827 dc
.SetBackgroundMode( wxTRANSPARENT
);
3828 dc
.SetTextForeground( GetLabelTextColour() );
3829 dc
.SetFont( GetLabelFont() );
3832 GetRowLabelAlignment( &hAlign
, &vAlign
);
3836 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3837 rect
.SetWidth( m_rowLabelWidth
- 4 );
3838 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3839 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3843 void wxGrid::DrawColLabels( wxDC
& dc
)
3845 if ( !m_numRows
|| !m_numCols
) return;
3848 size_t numLabels
= m_colLabelsExposed
.GetCount();
3850 for ( i
= 0; i
< numLabels
; i
++ )
3852 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3857 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3859 if ( m_colWidths
[col
] <= 0 ) return;
3861 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3863 dc
.SetPen( *wxBLACK_PEN
);
3864 dc
.DrawLine( m_colRights
[col
]-1, 0,
3865 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3867 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3868 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3870 dc
.SetPen( *wxWHITE_PEN
);
3871 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3872 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3874 dc
.SetBackgroundMode( wxTRANSPARENT
);
3875 dc
.SetTextForeground( GetLabelTextColour() );
3876 dc
.SetFont( GetLabelFont() );
3878 dc
.SetBackgroundMode( wxTRANSPARENT
);
3879 dc
.SetTextForeground( GetLabelTextColour() );
3880 dc
.SetFont( GetLabelFont() );
3883 GetColLabelAlignment( &hAlign
, &vAlign
);
3886 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3888 rect
.SetWidth( m_colWidths
[col
] - 4 );
3889 rect
.SetHeight( m_colLabelHeight
- 4 );
3890 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3894 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3895 const wxString
& value
,
3900 long textWidth
, textHeight
;
3901 long lineWidth
, lineHeight
;
3902 wxArrayString lines
;
3904 dc
.SetClippingRegion( rect
);
3905 StringToLines( value
, lines
);
3906 if ( lines
.GetCount() )
3908 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3909 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3912 switch ( horizAlign
)
3915 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3919 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3928 switch ( vertAlign
)
3931 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3935 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3944 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3946 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3951 dc
.DestroyClippingRegion();
3955 // Split multi line text up into an array of strings. Any existing
3956 // contents of the string array are preserved.
3958 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3962 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
3963 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
3965 while ( startPos
< (int)tVal
.Length() )
3967 pos
= tVal
.Mid(startPos
).Find( eol
);
3972 else if ( pos
== 0 )
3974 lines
.Add( wxEmptyString
);
3978 lines
.Add( value
.Mid(startPos
, pos
) );
3982 if ( startPos
< (int)value
.Length() )
3984 lines
.Add( value
.Mid( startPos
) );
3989 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3990 wxArrayString
& lines
,
3991 long *width
, long *height
)
3998 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4000 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4001 w
= wxMax( w
, lineW
);
4011 // ------ Edit control functions
4015 void wxGrid::EnableEditing( bool edit
)
4017 // TODO: improve this ?
4019 if ( edit
!= m_editable
)
4023 EnableCellEditControl(m_editable
);
4028 void wxGrid::EnableCellEditControl( bool enable
)
4033 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4034 SetCurrentCell( 0, 0 );
4036 if ( enable
!= m_cellEditCtrlEnabled
)
4040 m_cellEditCtrlEnabled
= enable
;
4041 SetEditControlValue();
4042 ShowCellEditControl();
4046 HideCellEditControl();
4047 SaveEditControlValue();
4048 m_cellEditCtrlEnabled
= enable
;
4054 void wxGrid::ShowCellEditControl()
4056 if ( IsCellEditControlEnabled() )
4058 if ( !IsVisible( m_currentCellCoords
) )
4064 wxRect rect
= CellToRect( m_currentCellCoords
);
4065 int row
= m_currentCellCoords
.GetRow();
4066 int col
= m_currentCellCoords
.GetCol();
4068 // convert to scrolled coords
4070 int left
, top
, right
, bottom
;
4071 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4072 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4073 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4075 m_gridWin
->GetClientSize( &cw
, &ch
);
4077 // Make the edit control large enough to allow for internal margins
4078 // TODO: remove this if the text ctrl sizing is improved esp. for unix
4081 #if defined(__WXMOTIF__)
4082 if ( row
== 0 || col
== 0 )
4091 if ( row
== 0 || col
== 0 )
4101 #if defined(__WXGTK__)
4104 if (left
!= 0) left_diff
++;
4105 if (top
!= 0) top_diff
++;
4106 rect
.SetLeft( left
+ left_diff
);
4107 rect
.SetTop( top
+ top_diff
);
4108 rect
.SetRight( rect
.GetRight() - left_diff
);
4109 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4111 rect
.SetLeft( wxMax(0, left
- extra
) );
4112 rect
.SetTop( wxMax(0, top
- extra
) );
4113 rect
.SetRight( rect
.GetRight() + 2*extra
);
4114 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4117 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4118 wxGridCellEditor
* editor
= attr
->GetEditor();
4119 if (! editor
->IsCreated()) {
4120 editor
->Create(m_gridWin
, -1,
4121 new wxGridCellEditorEvtHandler(this, editor
));
4124 editor
->SetSize( rect
);
4125 editor
->Show( TRUE
);
4126 editor
->BeginEdit(row
, col
, this, attr
);
4133 void wxGrid::HideCellEditControl()
4135 if ( IsCellEditControlEnabled() )
4137 int row
= m_currentCellCoords
.GetRow();
4138 int col
= m_currentCellCoords
.GetCol();
4140 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4141 attr
->GetEditor()->Show( FALSE
);
4143 m_gridWin
->SetFocus();
4148 void wxGrid::SetEditControlValue( const wxString
& value
)
4150 // RD: The new Editors get the value from the table themselves now. This
4151 // method can probably be removed...
4155 void wxGrid::SaveEditControlValue()
4157 if (IsCellEditControlEnabled()) {
4158 int row
= m_currentCellCoords
.GetRow();
4159 int col
= m_currentCellCoords
.GetCol();
4161 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4162 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this, attr
);
4167 SendEvent( EVT_GRID_CELL_CHANGE
,
4168 m_currentCellCoords
.GetRow(),
4169 m_currentCellCoords
.GetCol() );
4176 // ------ Grid location functions
4177 // Note that all of these functions work with the logical coordinates of
4178 // grid cells and labels so you will need to convert from device
4179 // coordinates for mouse events etc.
4182 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4184 int row
= YToRow(y
);
4185 int col
= XToCol(x
);
4187 if ( row
== -1 || col
== -1 )
4189 coords
= wxGridNoCellCoords
;
4193 coords
.Set( row
, col
);
4198 int wxGrid::YToRow( int y
)
4202 for ( i
= 0; i
< m_numRows
; i
++ )
4204 if ( y
< m_rowBottoms
[i
] ) return i
;
4207 return m_numRows
; //-1;
4211 int wxGrid::XToCol( int x
)
4215 for ( i
= 0; i
< m_numCols
; i
++ )
4217 if ( x
< m_colRights
[i
] ) return i
;
4220 return m_numCols
; //-1;
4224 // return the row number that that the y coord is near the edge of, or
4225 // -1 if not near an edge
4227 int wxGrid::YToEdgeOfRow( int y
)
4231 for ( i
= 0; i
< m_numRows
; i
++ )
4233 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4235 d
= abs( y
- m_rowBottoms
[i
] );
4237 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4246 // return the col number that that the x coord is near the edge of, or
4247 // -1 if not near an edge
4249 int wxGrid::XToEdgeOfCol( int x
)
4253 for ( i
= 0; i
< m_numCols
; i
++ )
4255 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4257 d
= abs( x
- m_colRights
[i
] );
4259 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4268 wxRect
wxGrid::CellToRect( int row
, int col
)
4270 wxRect
rect( -1, -1, -1, -1 );
4272 if ( row
>= 0 && row
< m_numRows
&&
4273 col
>= 0 && col
< m_numCols
)
4275 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4276 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4277 rect
.width
= m_colWidths
[col
];
4278 rect
.height
= m_rowHeights
[ row
];
4285 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4287 // get the cell rectangle in logical coords
4289 wxRect
r( CellToRect( row
, col
) );
4291 // convert to device coords
4293 int left
, top
, right
, bottom
;
4294 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4295 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4297 // check against the client area of the grid window
4300 m_gridWin
->GetClientSize( &cw
, &ch
);
4302 if ( wholeCellVisible
)
4304 // is the cell wholly visible ?
4306 return ( left
>= 0 && right
<= cw
&&
4307 top
>= 0 && bottom
<= ch
);
4311 // is the cell partly visible ?
4313 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4314 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4319 // make the specified cell location visible by doing a minimal amount
4322 void wxGrid::MakeCellVisible( int row
, int col
)
4325 int xpos
= -1, ypos
= -1;
4327 if ( row
>= 0 && row
< m_numRows
&&
4328 col
>= 0 && col
< m_numCols
)
4330 // get the cell rectangle in logical coords
4332 wxRect
r( CellToRect( row
, col
) );
4334 // convert to device coords
4336 int left
, top
, right
, bottom
;
4337 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4338 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4341 m_gridWin
->GetClientSize( &cw
, &ch
);
4347 else if ( bottom
> ch
)
4349 int h
= r
.GetHeight();
4351 for ( i
= row
-1; i
>= 0; i
-- )
4353 if ( h
+ m_rowHeights
[i
] > ch
) break;
4355 h
+= m_rowHeights
[i
];
4356 ypos
-= m_rowHeights
[i
];
4359 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4360 // have rounding errors (this is important, because if we do, we
4361 // might not scroll at all and some cells won't be redrawn)
4362 ypos
+= GRID_SCROLL_LINE
/ 2;
4369 else if ( right
> cw
)
4371 int w
= r
.GetWidth();
4373 for ( i
= col
-1; i
>= 0; i
-- )
4375 if ( w
+ m_colWidths
[i
] > cw
) break;
4377 w
+= m_colWidths
[i
];
4378 xpos
-= m_colWidths
[i
];
4381 // see comment for ypos above
4382 xpos
+= GRID_SCROLL_LINE
/ 2;
4385 if ( xpos
!= -1 || ypos
!= -1 )
4387 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4388 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4389 Scroll( xpos
, ypos
);
4397 // ------ Grid cursor movement functions
4400 bool wxGrid::MoveCursorUp()
4402 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4403 m_currentCellCoords
.GetRow() > 0 )
4405 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4406 m_currentCellCoords
.GetCol() );
4408 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4409 m_currentCellCoords
.GetCol() );
4418 bool wxGrid::MoveCursorDown()
4420 // TODO: allow for scrolling
4422 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4423 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4425 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4426 m_currentCellCoords
.GetCol() );
4428 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4429 m_currentCellCoords
.GetCol() );
4438 bool wxGrid::MoveCursorLeft()
4440 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4441 m_currentCellCoords
.GetCol() > 0 )
4443 MakeCellVisible( m_currentCellCoords
.GetRow(),
4444 m_currentCellCoords
.GetCol() - 1 );
4446 SetCurrentCell( m_currentCellCoords
.GetRow(),
4447 m_currentCellCoords
.GetCol() - 1 );
4456 bool wxGrid::MoveCursorRight()
4458 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4459 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4461 MakeCellVisible( m_currentCellCoords
.GetRow(),
4462 m_currentCellCoords
.GetCol() + 1 );
4464 SetCurrentCell( m_currentCellCoords
.GetRow(),
4465 m_currentCellCoords
.GetCol() + 1 );
4474 bool wxGrid::MovePageUp()
4476 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4478 int row
= m_currentCellCoords
.GetRow();
4482 m_gridWin
->GetClientSize( &cw
, &ch
);
4484 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4485 int newRow
= YToRow( y
- ch
+ 1 );
4490 else if ( newRow
== row
)
4495 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4496 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4504 bool wxGrid::MovePageDown()
4506 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4508 int row
= m_currentCellCoords
.GetRow();
4509 if ( row
< m_numRows
)
4512 m_gridWin
->GetClientSize( &cw
, &ch
);
4514 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4515 int newRow
= YToRow( y
+ ch
);
4518 newRow
= m_numRows
- 1;
4520 else if ( newRow
== row
)
4525 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4526 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4534 bool wxGrid::MoveCursorUpBlock()
4537 m_currentCellCoords
!= wxGridNoCellCoords
&&
4538 m_currentCellCoords
.GetRow() > 0 )
4540 int row
= m_currentCellCoords
.GetRow();
4541 int col
= m_currentCellCoords
.GetCol();
4543 if ( m_table
->IsEmptyCell(row
, col
) )
4545 // starting in an empty cell: find the next block of
4551 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4554 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4556 // starting at the top of a block: find the next block
4562 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4567 // starting within a block: find the top of the block
4572 if ( m_table
->IsEmptyCell(row
, col
) )
4580 MakeCellVisible( row
, col
);
4581 SetCurrentCell( row
, col
);
4589 bool wxGrid::MoveCursorDownBlock()
4592 m_currentCellCoords
!= wxGridNoCellCoords
&&
4593 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4595 int row
= m_currentCellCoords
.GetRow();
4596 int col
= m_currentCellCoords
.GetCol();
4598 if ( m_table
->IsEmptyCell(row
, col
) )
4600 // starting in an empty cell: find the next block of
4603 while ( row
< m_numRows
-1 )
4606 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4609 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4611 // starting at the bottom of a block: find the next block
4614 while ( row
< m_numRows
-1 )
4617 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4622 // starting within a block: find the bottom of the block
4624 while ( row
< m_numRows
-1 )
4627 if ( m_table
->IsEmptyCell(row
, col
) )
4635 MakeCellVisible( row
, col
);
4636 SetCurrentCell( row
, col
);
4644 bool wxGrid::MoveCursorLeftBlock()
4647 m_currentCellCoords
!= wxGridNoCellCoords
&&
4648 m_currentCellCoords
.GetCol() > 0 )
4650 int row
= m_currentCellCoords
.GetRow();
4651 int col
= m_currentCellCoords
.GetCol();
4653 if ( m_table
->IsEmptyCell(row
, col
) )
4655 // starting in an empty cell: find the next block of
4661 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4664 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4666 // starting at the left of a block: find the next block
4672 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4677 // starting within a block: find the left of the block
4682 if ( m_table
->IsEmptyCell(row
, col
) )
4690 MakeCellVisible( row
, col
);
4691 SetCurrentCell( row
, col
);
4699 bool wxGrid::MoveCursorRightBlock()
4702 m_currentCellCoords
!= wxGridNoCellCoords
&&
4703 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4705 int row
= m_currentCellCoords
.GetRow();
4706 int col
= m_currentCellCoords
.GetCol();
4708 if ( m_table
->IsEmptyCell(row
, col
) )
4710 // starting in an empty cell: find the next block of
4713 while ( col
< m_numCols
-1 )
4716 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4719 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4721 // starting at the right of a block: find the next block
4724 while ( col
< m_numCols
-1 )
4727 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4732 // starting within a block: find the right of the block
4734 while ( col
< m_numCols
-1 )
4737 if ( m_table
->IsEmptyCell(row
, col
) )
4745 MakeCellVisible( row
, col
);
4746 SetCurrentCell( row
, col
);
4757 // ------ Label values and formatting
4760 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4762 *horiz
= m_rowLabelHorizAlign
;
4763 *vert
= m_rowLabelVertAlign
;
4766 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4768 *horiz
= m_colLabelHorizAlign
;
4769 *vert
= m_colLabelVertAlign
;
4772 wxString
wxGrid::GetRowLabelValue( int row
)
4776 return m_table
->GetRowLabelValue( row
);
4786 wxString
wxGrid::GetColLabelValue( int col
)
4790 return m_table
->GetColLabelValue( col
);
4801 void wxGrid::SetRowLabelSize( int width
)
4803 width
= wxMax( width
, 0 );
4804 if ( width
!= m_rowLabelWidth
)
4808 m_rowLabelWin
->Show( FALSE
);
4809 m_cornerLabelWin
->Show( FALSE
);
4811 else if ( m_rowLabelWidth
== 0 )
4813 m_rowLabelWin
->Show( TRUE
);
4814 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4817 m_rowLabelWidth
= width
;
4824 void wxGrid::SetColLabelSize( int height
)
4826 height
= wxMax( height
, 0 );
4827 if ( height
!= m_colLabelHeight
)
4831 m_colLabelWin
->Show( FALSE
);
4832 m_cornerLabelWin
->Show( FALSE
);
4834 else if ( m_colLabelHeight
== 0 )
4836 m_colLabelWin
->Show( TRUE
);
4837 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4840 m_colLabelHeight
= height
;
4847 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4849 if ( m_labelBackgroundColour
!= colour
)
4851 m_labelBackgroundColour
= colour
;
4852 m_rowLabelWin
->SetBackgroundColour( colour
);
4853 m_colLabelWin
->SetBackgroundColour( colour
);
4854 m_cornerLabelWin
->SetBackgroundColour( colour
);
4856 if ( !GetBatchCount() )
4858 m_rowLabelWin
->Refresh();
4859 m_colLabelWin
->Refresh();
4860 m_cornerLabelWin
->Refresh();
4865 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4867 if ( m_labelTextColour
!= colour
)
4869 m_labelTextColour
= colour
;
4870 if ( !GetBatchCount() )
4872 m_rowLabelWin
->Refresh();
4873 m_colLabelWin
->Refresh();
4878 void wxGrid::SetLabelFont( const wxFont
& font
)
4881 if ( !GetBatchCount() )
4883 m_rowLabelWin
->Refresh();
4884 m_colLabelWin
->Refresh();
4888 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4890 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4892 m_rowLabelHorizAlign
= horiz
;
4895 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4897 m_rowLabelVertAlign
= vert
;
4900 if ( !GetBatchCount() )
4902 m_rowLabelWin
->Refresh();
4906 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4908 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4910 m_colLabelHorizAlign
= horiz
;
4913 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4915 m_colLabelVertAlign
= vert
;
4918 if ( !GetBatchCount() )
4920 m_colLabelWin
->Refresh();
4924 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4928 m_table
->SetRowLabelValue( row
, s
);
4929 if ( !GetBatchCount() )
4931 wxRect rect
= CellToRect( row
, 0);
4932 if ( rect
.height
> 0 )
4934 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4936 rect
.width
= m_rowLabelWidth
;
4937 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4943 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4947 m_table
->SetColLabelValue( col
, s
);
4948 if ( !GetBatchCount() )
4950 wxRect rect
= CellToRect( 0, col
);
4951 if ( rect
.width
> 0 )
4953 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4955 rect
.height
= m_colLabelHeight
;
4956 m_colLabelWin
->Refresh( TRUE
, &rect
);
4962 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4964 if ( m_gridLineColour
!= colour
)
4966 m_gridLineColour
= colour
;
4968 wxClientDC
dc( m_gridWin
);
4970 DrawAllGridLines( dc
, wxRegion() );
4974 void wxGrid::EnableGridLines( bool enable
)
4976 if ( enable
!= m_gridLinesEnabled
)
4978 m_gridLinesEnabled
= enable
;
4980 if ( !GetBatchCount() )
4984 wxClientDC
dc( m_gridWin
);
4986 DrawAllGridLines( dc
, wxRegion() );
4990 m_gridWin
->Refresh();
4997 int wxGrid::GetDefaultRowSize()
4999 return m_defaultRowHeight
;
5002 int wxGrid::GetRowSize( int row
)
5004 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5006 return m_rowHeights
[row
];
5009 int wxGrid::GetDefaultColSize()
5011 return m_defaultColWidth
;
5014 int wxGrid::GetColSize( int col
)
5016 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5018 return m_colWidths
[col
];
5021 // ============================================================================
5022 // access to the grid attributes: each of them has a default value in the grid
5023 // itself and may be overidden on a per-cell basis
5024 // ============================================================================
5026 // ----------------------------------------------------------------------------
5027 // setting default attributes
5028 // ----------------------------------------------------------------------------
5030 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5032 m_defaultCellAttr
->SetBackgroundColour(col
);
5035 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5037 m_defaultCellAttr
->SetTextColour(col
);
5040 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5042 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5045 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5047 m_defaultCellAttr
->SetFont(font
);
5050 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5052 m_defaultCellAttr
->SetRenderer(renderer
);
5055 // ----------------------------------------------------------------------------
5056 // access to the default attrbiutes
5057 // ----------------------------------------------------------------------------
5059 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5061 return m_defaultCellAttr
->GetBackgroundColour();
5064 wxColour
wxGrid::GetDefaultCellTextColour()
5066 return m_defaultCellAttr
->GetTextColour();
5069 wxFont
wxGrid::GetDefaultCellFont()
5071 return m_defaultCellAttr
->GetFont();
5074 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5076 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5079 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5081 return m_defaultCellAttr
->GetRenderer();
5084 // ----------------------------------------------------------------------------
5085 // access to cell attributes
5086 // ----------------------------------------------------------------------------
5088 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5090 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5091 wxColour colour
= attr
->GetBackgroundColour();
5096 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5098 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5099 wxColour colour
= attr
->GetTextColour();
5104 wxFont
wxGrid::GetCellFont( int row
, int col
)
5106 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5107 wxFont font
= attr
->GetFont();
5112 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5114 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5115 attr
->GetAlignment(horiz
, vert
);
5119 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5121 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5122 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5127 // ----------------------------------------------------------------------------
5128 // attribute support: cache, automatic provider creation, ...
5129 // ----------------------------------------------------------------------------
5131 bool wxGrid::CanHaveAttributes()
5138 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5139 // table is providing the attributes itself??? In which case
5140 // I don't think the grid should create a Provider object for the
5141 // table but the table should be smart enough to do that on its own.
5142 if ( !m_table
->GetAttrProvider() )
5144 // use the default attr provider by default
5145 // (another choice would be to just return FALSE thus forcing the user
5147 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5153 void wxGrid::ClearAttrCache()
5155 if ( m_attrCache
.row
!= -1 )
5157 m_attrCache
.attr
->SafeDecRef();
5158 m_attrCache
.row
= -1;
5162 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5164 wxGrid
*self
= (wxGrid
*)this; // const_cast
5166 self
->ClearAttrCache();
5167 self
->m_attrCache
.row
= row
;
5168 self
->m_attrCache
.col
= col
;
5169 self
->m_attrCache
.attr
= attr
;
5173 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5175 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5177 *attr
= m_attrCache
.attr
;
5178 (*attr
)->SafeIncRef();
5180 #ifdef DEBUG_ATTR_CACHE
5181 gs_nAttrCacheHits
++;
5188 #ifdef DEBUG_ATTR_CACHE
5189 gs_nAttrCacheMisses
++;
5195 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5197 wxGridCellAttr
*attr
;
5198 if ( !LookupAttr(row
, col
, &attr
) )
5200 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5201 CacheAttr(row
, col
, attr
);
5204 attr
->SetDefAttr(m_defaultCellAttr
);
5206 attr
= m_defaultCellAttr
;
5213 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5215 wxGridCellAttr
*attr
;
5216 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5218 wxASSERT_MSG( m_table
,
5219 _T("we may only be called if CanHaveAttributes() "
5220 "returned TRUE and then m_table should be !NULL") );
5222 attr
= m_table
->GetAttr(row
, col
);
5225 attr
= new wxGridCellAttr
;
5227 // artificially inc the ref count to match DecRef() in caller
5230 m_table
->SetAttr(attr
, row
, col
);
5233 CacheAttr(row
, col
, attr
);
5235 attr
->SetDefAttr(m_defaultCellAttr
);
5239 // ----------------------------------------------------------------------------
5240 // setting cell attributes: this is forwarded to the table
5241 // ----------------------------------------------------------------------------
5243 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5245 if ( CanHaveAttributes() )
5247 m_table
->SetRowAttr(attr
, row
);
5255 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5257 if ( CanHaveAttributes() )
5259 m_table
->SetColAttr(attr
, col
);
5267 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5269 if ( CanHaveAttributes() )
5271 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5272 attr
->SetBackgroundColour(colour
);
5277 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5279 if ( CanHaveAttributes() )
5281 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5282 attr
->SetTextColour(colour
);
5287 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5289 if ( CanHaveAttributes() )
5291 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5292 attr
->SetFont(font
);
5297 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5299 if ( CanHaveAttributes() )
5301 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5302 attr
->SetAlignment(horiz
, vert
);
5307 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5309 if ( CanHaveAttributes() )
5311 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5312 attr
->SetRenderer(renderer
);
5317 // ----------------------------------------------------------------------------
5319 // ----------------------------------------------------------------------------
5321 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5323 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5325 if ( resizeExistingRows
)
5329 for ( row
= 0; row
< m_numRows
; row
++ )
5331 m_rowHeights
[row
] = m_defaultRowHeight
;
5332 bottom
+= m_defaultRowHeight
;
5333 m_rowBottoms
[row
] = bottom
;
5339 void wxGrid::SetRowSize( int row
, int height
)
5341 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5345 int h
= wxMax( 0, height
);
5346 int diff
= h
- m_rowHeights
[row
];
5348 m_rowHeights
[row
] = h
;
5349 for ( i
= row
; i
< m_numRows
; i
++ )
5351 m_rowBottoms
[i
] += diff
;
5356 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5358 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5360 if ( resizeExistingCols
)
5364 for ( col
= 0; col
< m_numCols
; col
++ )
5366 m_colWidths
[col
] = m_defaultColWidth
;
5367 right
+= m_defaultColWidth
;
5368 m_colRights
[col
] = right
;
5374 void wxGrid::SetColSize( int col
, int width
)
5376 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5380 int w
= wxMax( 0, width
);
5381 int diff
= w
- m_colWidths
[col
];
5382 m_colWidths
[col
] = w
;
5384 for ( i
= col
; i
< m_numCols
; i
++ )
5386 m_colRights
[i
] += diff
;
5393 // ------ cell value accessor functions
5396 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5400 m_table
->SetValue( row
, col
, s
.c_str() );
5401 if ( !GetBatchCount() )
5403 wxClientDC
dc( m_gridWin
);
5405 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5408 #if 0 // TODO: edit in place
5410 if ( m_currentCellCoords
.GetRow() == row
&&
5411 m_currentCellCoords
.GetCol() == col
)
5413 SetEditControlValue( s
);
5422 // ------ Block, row and col selection
5425 void wxGrid::SelectRow( int row
, bool addToSelected
)
5429 if ( IsSelection() && addToSelected
)
5432 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5435 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5436 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5437 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5438 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5442 need_refresh
[0] = TRUE
;
5443 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5444 wxGridCellCoords ( oldTop
- 1,
5446 m_selectedTopLeft
.SetRow( row
);
5451 need_refresh
[1] = TRUE
;
5452 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5453 wxGridCellCoords ( oldBottom
,
5456 m_selectedTopLeft
.SetCol( 0 );
5459 if ( oldBottom
< row
)
5461 need_refresh
[2] = TRUE
;
5462 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5463 wxGridCellCoords ( row
,
5465 m_selectedBottomRight
.SetRow( row
);
5468 if ( oldRight
< m_numCols
- 1 )
5470 need_refresh
[3] = TRUE
;
5471 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5473 wxGridCellCoords ( oldBottom
,
5475 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5478 for (i
= 0; i
< 4; i
++ )
5479 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5480 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5484 r
= SelectionToDeviceRect();
5486 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5488 m_selectedTopLeft
.Set( row
, 0 );
5489 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5490 r
= SelectionToDeviceRect();
5491 m_gridWin
->Refresh( FALSE
, &r
);
5494 wxGridRangeSelectEvent
gridEvt( GetId(),
5495 EVT_GRID_RANGE_SELECT
,
5498 m_selectedBottomRight
);
5500 GetEventHandler()->ProcessEvent(gridEvt
);
5504 void wxGrid::SelectCol( int col
, bool addToSelected
)
5506 if ( IsSelection() && addToSelected
)
5509 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5512 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5513 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5514 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5515 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5517 if ( oldLeft
> col
)
5519 need_refresh
[0] = TRUE
;
5520 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5521 wxGridCellCoords ( m_numRows
- 1,
5523 m_selectedTopLeft
.SetCol( col
);
5528 need_refresh
[1] = TRUE
;
5529 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5530 wxGridCellCoords ( oldTop
- 1,
5532 m_selectedTopLeft
.SetRow( 0 );
5535 if ( oldRight
< col
)
5537 need_refresh
[2] = TRUE
;
5538 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5539 wxGridCellCoords ( m_numRows
- 1,
5541 m_selectedBottomRight
.SetCol( col
);
5544 if ( oldBottom
< m_numRows
- 1 )
5546 need_refresh
[3] = TRUE
;
5547 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5549 wxGridCellCoords ( m_numRows
- 1,
5551 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5554 for (i
= 0; i
< 4; i
++ )
5555 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5556 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5562 r
= SelectionToDeviceRect();
5564 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5566 m_selectedTopLeft
.Set( 0, col
);
5567 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5568 r
= SelectionToDeviceRect();
5569 m_gridWin
->Refresh( FALSE
, &r
);
5572 wxGridRangeSelectEvent
gridEvt( GetId(),
5573 EVT_GRID_RANGE_SELECT
,
5576 m_selectedBottomRight
);
5578 GetEventHandler()->ProcessEvent(gridEvt
);
5582 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5585 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5587 if ( topRow
> bottomRow
)
5594 if ( leftCol
> rightCol
)
5601 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5602 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5604 if ( m_selectedTopLeft
!= updateTopLeft
||
5605 m_selectedBottomRight
!= updateBottomRight
)
5607 // Compute two optimal update rectangles:
5608 // Either one rectangle is a real subset of the
5609 // other, or they are (almost) disjoint!
5611 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5614 // Store intermediate values
5615 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5616 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5617 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5618 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5620 // Determine the outer/inner coordinates.
5621 if (oldLeft
> leftCol
)
5627 if (oldTop
> topRow
)
5633 if (oldRight
< rightCol
)
5636 oldRight
= rightCol
;
5639 if (oldBottom
< bottomRow
)
5642 oldBottom
= bottomRow
;
5646 // Now, either the stuff marked old is the outer
5647 // rectangle or we don't have a situation where one
5648 // is contained in the other.
5650 if ( oldLeft
< leftCol
)
5652 need_refresh
[0] = TRUE
;
5653 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5655 wxGridCellCoords ( oldBottom
,
5659 if ( oldTop
< topRow
)
5661 need_refresh
[1] = TRUE
;
5662 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5664 wxGridCellCoords ( topRow
- 1,
5668 if ( oldRight
> rightCol
)
5670 need_refresh
[2] = TRUE
;
5671 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5673 wxGridCellCoords ( oldBottom
,
5677 if ( oldBottom
> bottomRow
)
5679 need_refresh
[3] = TRUE
;
5680 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5682 wxGridCellCoords ( oldBottom
,
5688 m_selectedTopLeft
= updateTopLeft
;
5689 m_selectedBottomRight
= updateBottomRight
;
5691 // various Refresh() calls
5692 for (i
= 0; i
< 4; i
++ )
5693 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5694 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5697 // only generate an event if the block is not being selected by
5698 // dragging the mouse (in which case the event will be generated in
5699 // the mouse event handler)
5700 if ( !m_isDragging
)
5702 wxGridRangeSelectEvent
gridEvt( GetId(),
5703 EVT_GRID_RANGE_SELECT
,
5706 m_selectedBottomRight
);
5708 GetEventHandler()->ProcessEvent(gridEvt
);
5712 void wxGrid::SelectAll()
5714 m_selectedTopLeft
.Set( 0, 0 );
5715 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5717 m_gridWin
->Refresh();
5721 void wxGrid::ClearSelection()
5723 m_selectedTopLeft
= wxGridNoCellCoords
;
5724 m_selectedBottomRight
= wxGridNoCellCoords
;
5728 // This function returns the rectangle that encloses the given block
5729 // in device coords clipped to the client size of the grid window.
5731 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5732 const wxGridCellCoords
&bottomRight
)
5734 wxRect
rect( wxGridNoCellRect
);
5737 cellRect
= CellToRect( topLeft
);
5738 if ( cellRect
!= wxGridNoCellRect
)
5744 rect
= wxRect( 0, 0, 0, 0 );
5747 cellRect
= CellToRect( bottomRight
);
5748 if ( cellRect
!= wxGridNoCellRect
)
5754 return wxGridNoCellRect
;
5757 // convert to scrolled coords
5759 int left
, top
, right
, bottom
;
5760 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5761 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5764 m_gridWin
->GetClientSize( &cw
, &ch
);
5766 rect
.SetLeft( wxMax(0, left
) );
5767 rect
.SetTop( wxMax(0, top
) );
5768 rect
.SetRight( wxMin(cw
, right
) );
5769 rect
.SetBottom( wxMin(ch
, bottom
) );
5777 // ------ Grid event classes
5780 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5782 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5783 int row
, int col
, int x
, int y
,
5784 bool control
, bool shift
, bool alt
, bool meta
)
5785 : wxNotifyEvent( type
, id
)
5791 m_control
= control
;
5796 SetEventObject(obj
);
5800 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5802 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5803 int rowOrCol
, int x
, int y
,
5804 bool control
, bool shift
, bool alt
, bool meta
)
5805 : wxNotifyEvent( type
, id
)
5807 m_rowOrCol
= rowOrCol
;
5810 m_control
= control
;
5815 SetEventObject(obj
);
5819 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5821 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5822 const wxGridCellCoords
& topLeft
,
5823 const wxGridCellCoords
& bottomRight
,
5824 bool control
, bool shift
, bool alt
, bool meta
)
5825 : wxNotifyEvent( type
, id
)
5827 m_topLeft
= topLeft
;
5828 m_bottomRight
= bottomRight
;
5829 m_control
= control
;
5834 SetEventObject(obj
);
5838 #endif // ifndef wxUSE_NEW_GRID