1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
59 struct wxGridCellWithAttr
61 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
62 : coords(row
, col
), attr(attr_
)
71 wxGridCellCoords coords
;
75 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
77 #include "wx/arrimpl.cpp"
79 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
80 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
89 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
90 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
91 const wxPoint
&pos
, const wxSize
&size
);
96 void OnPaint( wxPaintEvent
& event
);
97 void OnMouseEvent( wxMouseEvent
& event
);
98 void OnKeyDown( wxKeyEvent
& event
);
100 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
108 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
109 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
110 const wxPoint
&pos
, const wxSize
&size
);
115 void OnPaint( wxPaintEvent
&event
);
116 void OnMouseEvent( wxMouseEvent
& event
);
117 void OnKeyDown( wxKeyEvent
& event
);
119 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
120 DECLARE_EVENT_TABLE()
124 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
127 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
128 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnKeyDown( wxKeyEvent
& event
);
136 void OnPaint( wxPaintEvent
& event
);
138 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
139 DECLARE_EVENT_TABLE()
142 class WXDLLEXPORT wxGridWindow
: public wxPanel
147 m_owner
= (wxGrid
*)NULL
;
148 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
149 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
152 wxGridWindow( wxGrid
*parent
,
153 wxGridRowLabelWindow
*rowLblWin
,
154 wxGridColLabelWindow
*colLblWin
,
155 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
158 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
162 wxGridRowLabelWindow
*m_rowLabelWin
;
163 wxGridColLabelWindow
*m_colLabelWin
;
165 void OnPaint( wxPaintEvent
&event
);
166 void OnMouseEvent( wxMouseEvent
& event
);
167 void OnKeyDown( wxKeyEvent
& );
168 void OnEraseBackground( wxEraseEvent
& );
171 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
172 DECLARE_EVENT_TABLE()
177 class wxGridCellEditorEvtHandler
: public wxEvtHandler
180 wxGridCellEditorEvtHandler()
181 : m_grid(0), m_editor(0)
183 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
184 : m_grid(grid
), m_editor(editor
)
187 void OnKeyDown(wxKeyEvent
& event
);
188 void OnChar(wxKeyEvent
& event
);
192 wxGridCellEditor
* m_editor
;
193 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
194 DECLARE_EVENT_TABLE()
198 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
199 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
200 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
201 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
206 // ----------------------------------------------------------------------------
207 // the internal data representation used by wxGridCellAttrProvider
208 // ----------------------------------------------------------------------------
210 // this class stores attributes set for cells
211 class WXDLLEXPORT wxGridCellAttrData
214 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
215 wxGridCellAttr
*GetAttr(int row
, int col
) const;
216 void UpdateAttrRows( size_t pos
, int numRows
);
217 void UpdateAttrCols( size_t pos
, int numCols
);
220 // searches for the attr for given cell, returns wxNOT_FOUND if not found
221 int FindIndex(int row
, int col
) const;
223 wxGridCellWithAttrArray m_attrs
;
226 // this class stores attributes set for rows or columns
227 class WXDLLEXPORT wxGridRowOrColAttrData
230 // empty ctor to suppress warnings
231 wxGridRowOrColAttrData() { }
232 ~wxGridRowOrColAttrData();
234 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
235 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
236 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
239 wxArrayInt m_rowsOrCols
;
240 wxArrayAttrs m_attrs
;
243 // NB: this is just a wrapper around 3 objects: one which stores cell
244 // attributes, and 2 others for row/col ones
245 class WXDLLEXPORT wxGridCellAttrProviderData
248 wxGridCellAttrData m_cellAttrs
;
249 wxGridRowOrColAttrData m_rowAttrs
,
254 // ----------------------------------------------------------------------------
255 // data structures used for the data type registry
256 // ----------------------------------------------------------------------------
258 struct wxGridDataTypeInfo
{
259 wxGridDataTypeInfo(const wxString
& typeName
,
260 wxGridCellRenderer
* renderer
,
261 wxGridCellEditor
* editor
)
262 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
265 ~wxGridDataTypeInfo() { delete m_renderer
; delete m_editor
; }
268 wxGridCellRenderer
* m_renderer
;
269 wxGridCellEditor
* m_editor
;
273 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
276 class WXDLLEXPORT wxGridTypeRegistry
{
278 ~wxGridTypeRegistry();
279 void RegisterDataType(const wxString
& typeName
,
280 wxGridCellRenderer
* renderer
,
281 wxGridCellEditor
* editor
);
282 int FindDataType(const wxString
& typeName
);
283 wxGridCellRenderer
* GetRenderer(int index
);
284 wxGridCellEditor
* GetEditor(int index
);
287 wxGridDataTypeInfoArray m_typeinfo
;
293 // ----------------------------------------------------------------------------
294 // conditional compilation
295 // ----------------------------------------------------------------------------
297 #ifndef WXGRID_DRAW_LINES
298 #define WXGRID_DRAW_LINES 1
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 //#define DEBUG_ATTR_CACHE
306 #ifdef DEBUG_ATTR_CACHE
307 static size_t gs_nAttrCacheHits
= 0;
308 static size_t gs_nAttrCacheMisses
= 0;
309 #endif // DEBUG_ATTR_CACHE
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
316 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
319 // TODO: fixed so far - make configurable later (and also different for x/y)
320 static const size_t GRID_SCROLL_LINE
= 10;
322 // the size of hash tables used a bit everywhere (the max number of elements
323 // in these hash tables is the number of rows/columns)
324 static const int GRID_HASH_SIZE
= 100;
326 // ============================================================================
328 // ============================================================================
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 wxGridCellEditor::wxGridCellEditor()
340 wxGridCellEditor::~wxGridCellEditor()
345 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
346 wxWindowID
WXUNUSED(id
),
347 wxEvtHandler
* evtHandler
)
350 m_control
->PushEventHandler(evtHandler
);
353 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
354 wxGridCellAttr
*attr
)
356 // erase the background because we might not fill the cell
357 wxClientDC
dc(m_control
->GetParent());
358 dc
.SetPen(*wxTRANSPARENT_PEN
);
359 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
360 dc
.DrawRectangle(rectCell
);
362 // redraw the control we just painted over
363 m_control
->Refresh();
366 void wxGridCellEditor::Destroy()
370 m_control
->Destroy();
375 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
377 wxASSERT_MSG(m_control
,
378 wxT("The wxGridCellEditor must be Created first!"));
379 m_control
->Show(show
);
383 // set the colours/fonts if we have any
386 m_colFgOld
= m_control
->GetForegroundColour();
387 m_control
->SetForegroundColour(attr
->GetTextColour());
389 m_colBgOld
= m_control
->GetBackgroundColour();
390 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
392 m_fontOld
= m_control
->GetFont();
393 m_control
->SetFont(attr
->GetFont());
395 // can't do anything more in the base class version, the other
396 // attributes may only be used by the derived classes
401 // restore the standard colours fonts
402 if ( m_colFgOld
.Ok() )
404 m_control
->SetForegroundColour(m_colFgOld
);
405 m_colFgOld
= wxNullColour
;
408 if ( m_colBgOld
.Ok() )
410 m_control
->SetBackgroundColour(m_colBgOld
);
411 m_colBgOld
= wxNullColour
;
414 if ( m_fontOld
.Ok() )
416 m_control
->SetFont(m_fontOld
);
417 m_fontOld
= wxNullFont
;
422 void wxGridCellEditor::SetSize(const wxRect
& rect
)
424 wxASSERT_MSG(m_control
,
425 wxT("The wxGridCellEditor must be Created first!"));
426 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
429 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
435 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
440 void wxGridCellEditor::StartingClick()
444 // ----------------------------------------------------------------------------
445 // wxGridCellTextEditor
446 // ----------------------------------------------------------------------------
448 wxGridCellTextEditor::wxGridCellTextEditor()
452 void wxGridCellTextEditor::Create(wxWindow
* parent
,
454 wxEvtHandler
* evtHandler
)
456 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
457 wxDefaultPosition
, wxDefaultSize
458 #if defined(__WXMSW__)
459 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
463 wxGridCellEditor::Create(parent
, id
, evtHandler
);
466 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
467 wxGridCellAttr
* WXUNUSED(attr
))
469 // as we fill the entire client area, don't do anything here to minimize
473 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
475 wxRect
rect(rectOrig
);
477 // Make the edit control large enough to allow for internal
480 // TODO: remove this if the text ctrl sizing is improved esp. for
483 #if defined(__WXGTK__)
484 rect
.Inflate(rect
.x
? 1 : 0, rect
.y
? 1 : 0);
486 int extra
= rect
.x
&& rect
.y
? 2 : 1;
487 #if defined(__WXMOTIF__)
490 rect
.SetLeft( wxMax(0, rect
.x
- extra
) );
491 rect
.SetTop( wxMax(0, rect
.y
- extra
) );
492 rect
.SetRight( rect
.GetRight() + 2*extra
);
493 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
496 wxGridCellEditor::SetSize(rect
);
499 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
501 wxASSERT_MSG(m_control
,
502 wxT("The wxGridCellEditor must be Created first!"));
504 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
506 DoBeginEdit(m_startValue
);
509 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
511 Text()->SetValue(startValue
);
512 Text()->SetInsertionPointEnd();
516 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
519 wxASSERT_MSG(m_control
,
520 wxT("The wxGridCellEditor must be Created first!"));
522 bool changed
= FALSE
;
523 wxString value
= Text()->GetValue();
524 if (value
!= m_startValue
)
528 grid
->GetTable()->SetValue(row
, col
, value
);
530 m_startValue
= wxEmptyString
;
531 Text()->SetValue(m_startValue
);
537 void wxGridCellTextEditor::Reset()
539 wxASSERT_MSG(m_control
,
540 wxT("The wxGridCellEditor must be Created first!"));
542 DoReset(m_startValue
);
545 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
547 Text()->SetValue(startValue
);
548 Text()->SetInsertionPointEnd();
551 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
553 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
555 // insert the key in the control
556 long keycode
= event
.KeyCode();
557 if ( isprint(keycode
) )
559 // FIXME this is not going to work for non letters...
560 if ( !event
.ShiftDown() )
562 keycode
= tolower(keycode
);
565 Text()->AppendText((wxChar
)keycode
);
575 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
577 #if defined(__WXMOTIF__) || defined(__WXGTK__)
578 // wxMotif needs a little extra help...
579 int pos
= Text()->GetInsertionPoint();
580 wxString
s( Text()->GetValue() );
581 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
583 Text()->SetInsertionPoint( pos
);
585 // the other ports can handle a Return key press
591 // ----------------------------------------------------------------------------
592 // wxGridCellNumberEditor
593 // ----------------------------------------------------------------------------
595 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
601 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
603 wxEvtHandler
* evtHandler
)
607 // create a spin ctrl
608 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
609 wxDefaultPosition
, wxDefaultSize
,
613 wxGridCellEditor::Create(parent
, id
, evtHandler
);
617 // just a text control
618 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
621 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
622 #endif // wxUSE_VALIDATORS
626 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
628 // first get the value
629 wxGridTableBase
*table
= grid
->GetTable();
630 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
632 m_valueOld
= table
->GetValueAsLong(row
, col
);
636 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
643 Spin()->SetValue(m_valueOld
);
647 DoBeginEdit(GetString());
651 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
659 value
= Spin()->GetValue();
660 changed
= value
!= m_valueOld
;
664 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
669 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
675 void wxGridCellNumberEditor::Reset()
679 Spin()->SetValue(m_valueOld
);
683 DoReset(GetString());
687 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
691 long keycode
= event
.KeyCode();
692 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
694 wxGridCellTextEditor::StartingKey(event
);
704 // ----------------------------------------------------------------------------
705 // wxGridCellFloatEditor
706 // ----------------------------------------------------------------------------
708 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
710 wxEvtHandler
* evtHandler
)
712 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
715 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
716 #endif // wxUSE_VALIDATORS
719 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
721 // first get the value
722 wxGridTableBase
*table
= grid
->GetTable();
723 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
725 m_valueOld
= table
->GetValueAsDouble(row
, col
);
729 wxFAIL_MSG( _T("this cell doesn't have float value") );
734 DoBeginEdit(GetString());
737 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
741 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
743 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
753 void wxGridCellFloatEditor::Reset()
755 DoReset(GetString());
758 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
760 long keycode
= event
.KeyCode();
761 if ( isdigit(keycode
) ||
762 keycode
== '+' || keycode
== '-' || keycode
== '.' )
764 wxGridCellTextEditor::StartingKey(event
);
773 // ----------------------------------------------------------------------------
774 // wxGridCellBoolEditor
775 // ----------------------------------------------------------------------------
777 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
779 wxEvtHandler
* evtHandler
)
781 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
782 wxDefaultPosition
, wxDefaultSize
,
785 wxGridCellEditor::Create(parent
, id
, evtHandler
);
788 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
790 // position it in the centre of the rectangle (TODO: support alignment?)
792 m_control
->GetSize(&w
, &h
);
794 // the checkbox without label still has some space to the right in wxGTK,
795 // so shift it to the right
800 m_control
->Move(r
.x
+ r
.width
/2 - w
/2, r
.y
+ r
.height
/2 - h
/2);
803 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
805 m_control
->Show(show
);
809 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
810 CBox()->SetBackgroundColour(colBg
);
814 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
816 wxASSERT_MSG(m_control
,
817 wxT("The wxGridCellEditor must be Created first!"));
819 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
820 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
822 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
823 CBox()->SetValue(m_startValue
);
827 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
830 wxASSERT_MSG(m_control
,
831 wxT("The wxGridCellEditor must be Created first!"));
833 bool changed
= FALSE
;
834 bool value
= CBox()->GetValue();
835 if ( value
!= m_startValue
)
840 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
841 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
843 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
849 void wxGridCellBoolEditor::Reset()
851 wxASSERT_MSG(m_control
,
852 wxT("The wxGridCellEditor must be Created first!"));
854 CBox()->SetValue(m_startValue
);
857 void wxGridCellBoolEditor::StartingClick()
859 CBox()->SetValue(!CBox()->GetValue());
862 // ----------------------------------------------------------------------------
863 // wxGridCellChoiceEditor
864 // ----------------------------------------------------------------------------
866 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
867 const wxChar
* choices
[],
869 : m_allowOthers(allowOthers
)
871 m_choices
.Alloc(count
);
872 for ( size_t n
= 0; n
< count
; n
++ )
874 m_choices
.Add(choices
[n
]);
878 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
880 wxEvtHandler
* evtHandler
)
882 size_t count
= m_choices
.GetCount();
883 wxString
*choices
= new wxString
[count
];
884 for ( size_t n
= 0; n
< count
; n
++ )
886 choices
[n
] = m_choices
[n
];
889 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
890 wxDefaultPosition
, wxDefaultSize
,
892 m_allowOthers
? 0 : wxCB_READONLY
);
896 wxGridCellEditor::Create(parent
, id
, evtHandler
);
899 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
900 wxGridCellAttr
* WXUNUSED(attr
))
902 // as we fill the entire client area, don't do anything here to minimize
906 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
908 wxASSERT_MSG(m_control
,
909 wxT("The wxGridCellEditor must be Created first!"));
911 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
913 Combo()->SetValue(m_startValue
);
914 size_t count
= m_choices
.GetCount();
915 for (size_t i
=0; i
<count
; i
++)
917 if (m_startValue
== m_choices
[i
])
919 Combo()->SetSelection(i
);
923 Combo()->SetInsertionPointEnd();
927 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
930 wxString value
= Combo()->GetValue();
931 bool changed
= value
!= m_startValue
;
934 grid
->GetTable()->SetValue(row
, col
, value
);
936 m_startValue
= wxEmptyString
;
937 Combo()->SetValue(m_startValue
);
942 void wxGridCellChoiceEditor::Reset()
944 Combo()->SetValue(m_startValue
);
945 Combo()->SetInsertionPointEnd();
948 // ----------------------------------------------------------------------------
949 // wxGridCellEditorEvtHandler
950 // ----------------------------------------------------------------------------
952 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
954 switch ( event
.KeyCode() )
958 m_grid
->DisableCellEditControl();
962 event
.Skip( m_grid
->ProcessEvent( event
) );
966 if (!m_grid
->ProcessEvent(event
))
967 m_editor
->HandleReturn(event
);
976 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
978 switch ( event
.KeyCode() )
990 // ============================================================================
992 // ============================================================================
994 // ----------------------------------------------------------------------------
995 // wxGridCellRenderer
996 // ----------------------------------------------------------------------------
998 void wxGridCellRenderer::Draw(wxGrid
& grid
,
999 wxGridCellAttr
& attr
,
1005 dc
.SetBackgroundMode( wxSOLID
);
1009 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1013 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1016 dc
.SetPen( *wxTRANSPARENT_PEN
);
1017 dc
.DrawRectangle(rect
);
1020 wxGridCellRenderer::~wxGridCellRenderer()
1024 // ----------------------------------------------------------------------------
1025 // wxGridCellStringRenderer
1026 // ----------------------------------------------------------------------------
1028 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1029 wxGridCellAttr
& attr
,
1033 dc
.SetBackgroundMode( wxTRANSPARENT
);
1035 // TODO some special colours for attr.IsReadOnly() case?
1039 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1040 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1044 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1045 dc
.SetTextForeground( attr
.GetTextColour() );
1048 dc
.SetFont( attr
.GetFont() );
1051 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1053 const wxString
& text
)
1056 dc
.SetFont(attr
.GetFont());
1057 dc
.GetTextExtent(text
, &x
, &y
);
1059 return wxSize(x
, y
);
1062 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1063 wxGridCellAttr
& attr
,
1067 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1070 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1071 wxGridCellAttr
& attr
,
1073 const wxRect
& rectCell
,
1077 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1079 // now we only have to draw the text
1080 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1083 attr
.GetAlignment(&hAlign
, &vAlign
);
1085 wxRect rect
= rectCell
;
1088 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1089 rect
, hAlign
, vAlign
);
1092 // ----------------------------------------------------------------------------
1093 // wxGridCellNumberRenderer
1094 // ----------------------------------------------------------------------------
1096 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1098 wxGridTableBase
*table
= grid
.GetTable();
1100 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1102 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1106 text
= table
->GetValue(row
, col
);
1112 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1113 wxGridCellAttr
& attr
,
1115 const wxRect
& rectCell
,
1119 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1121 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1123 // draw the text right aligned by default
1125 attr
.GetAlignment(&hAlign
, &vAlign
);
1128 wxRect rect
= rectCell
;
1131 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1134 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1135 wxGridCellAttr
& attr
,
1139 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1142 // ----------------------------------------------------------------------------
1143 // wxGridCellFloatRenderer
1144 // ----------------------------------------------------------------------------
1146 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1149 SetPrecision(precision
);
1152 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1154 wxGridTableBase
*table
= grid
.GetTable();
1156 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1160 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1163 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1167 text
= table
->GetValue(row
, col
);
1173 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1174 wxGridCellAttr
& attr
,
1176 const wxRect
& rectCell
,
1180 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1182 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1184 // draw the text right aligned by default
1186 attr
.GetAlignment(&hAlign
, &vAlign
);
1189 wxRect rect
= rectCell
;
1192 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1195 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1196 wxGridCellAttr
& attr
,
1200 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1203 // ----------------------------------------------------------------------------
1204 // wxGridCellBoolRenderer
1205 // ----------------------------------------------------------------------------
1207 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1209 // between checkmark and box
1210 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1212 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1213 wxGridCellAttr
& WXUNUSED(attr
),
1218 // compute it only once (no locks for MT safeness in GUI thread...)
1219 if ( !ms_sizeCheckMark
.x
)
1221 // get checkbox size
1222 wxCoord checkSize
= 0;
1223 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1224 wxSize size
= checkbox
->GetBestSize();
1225 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1227 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1229 checkSize
-= size
.y
/ 2;
1234 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1237 return ms_sizeCheckMark
;
1240 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1241 wxGridCellAttr
& attr
,
1247 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1249 // draw a check mark in the centre (ignoring alignment - TODO)
1250 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1252 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1253 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1254 rectMark
.width
= size
.x
;
1255 rectMark
.height
= size
.y
;
1257 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1258 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1259 dc
.DrawRectangle(rectMark
);
1261 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1264 if (grid
.GetTable()->CanGetValueAs(row
, col
, wxT("bool")))
1265 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1267 value
= !!grid
.GetTable()->GetValue(row
, col
);
1271 dc
.SetTextForeground(attr
.GetTextColour());
1272 dc
.DrawCheckMark(rectMark
);
1276 // ----------------------------------------------------------------------------
1278 // ----------------------------------------------------------------------------
1280 const wxColour
& wxGridCellAttr::GetTextColour() const
1282 if (HasTextColour())
1286 else if (m_defGridAttr
!= this)
1288 return m_defGridAttr
->GetTextColour();
1292 wxFAIL_MSG(wxT("Missing default cell attribute"));
1293 return wxNullColour
;
1298 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1300 if (HasBackgroundColour())
1302 else if (m_defGridAttr
!= this)
1303 return m_defGridAttr
->GetBackgroundColour();
1306 wxFAIL_MSG(wxT("Missing default cell attribute"));
1307 return wxNullColour
;
1312 const wxFont
& wxGridCellAttr::GetFont() const
1316 else if (m_defGridAttr
!= this)
1317 return m_defGridAttr
->GetFont();
1320 wxFAIL_MSG(wxT("Missing default cell attribute"));
1326 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1330 if ( hAlign
) *hAlign
= m_hAlign
;
1331 if ( vAlign
) *vAlign
= m_vAlign
;
1333 else if (m_defGridAttr
!= this)
1334 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1337 wxFAIL_MSG(wxT("Missing default cell attribute"));
1342 // GetRenderer and GetEditor use a slightly different decision path about
1343 // which attribute to use. If a non-default attr object has one then it is
1344 // used, otherwise the default editor or renderer is fetched from the grid and
1345 // used. It should be the default for the data type of the cell. If it is
1346 // NULL (because the table has a type that the grid does not have in its
1347 // registry,) then the grid's default editor or renderer is used.
1349 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1351 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1352 return m_renderer
; // use local attribute
1354 wxGridCellRenderer
* renderer
= NULL
;
1355 if (grid
) // get renderer for the data type
1356 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1359 // if we still don't have one then use the grid default
1360 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1363 wxFAIL_MSG(wxT("Missing default cell attribute"));
1368 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1370 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1371 return m_editor
; // use local attribute
1373 wxGridCellEditor
* editor
= NULL
;
1374 if (grid
) // get renderer for the data type
1375 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1378 // if we still don't have one then use the grid default
1379 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1382 wxFAIL_MSG(wxT("Missing default cell attribute"));
1387 // ----------------------------------------------------------------------------
1388 // wxGridCellAttrData
1389 // ----------------------------------------------------------------------------
1391 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1393 int n
= FindIndex(row
, col
);
1394 if ( n
== wxNOT_FOUND
)
1396 // add the attribute
1397 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1403 // change the attribute
1404 m_attrs
[(size_t)n
].attr
= attr
;
1408 // remove this attribute
1409 m_attrs
.RemoveAt((size_t)n
);
1414 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1416 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1418 int n
= FindIndex(row
, col
);
1419 if ( n
!= wxNOT_FOUND
)
1421 attr
= m_attrs
[(size_t)n
].attr
;
1428 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1430 size_t count
= m_attrs
.GetCount();
1431 for ( size_t n
= 0; n
< count
; n
++ )
1433 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1434 wxCoord row
= coords
.GetRow();
1435 if ((size_t)row
>= pos
)
1439 // If rows inserted, include row counter where necessary
1440 coords
.SetRow(row
+ numRows
);
1442 else if (numRows
< 0)
1444 // If rows deleted ...
1445 if ((size_t)row
>= pos
- numRows
)
1447 // ...either decrement row counter (if row still exists)...
1448 coords
.SetRow(row
+ numRows
);
1452 // ...or remove the attribute
1453 m_attrs
.RemoveAt((size_t)n
);
1461 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1463 size_t count
= m_attrs
.GetCount();
1464 for ( size_t n
= 0; n
< count
; n
++ )
1466 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1467 wxCoord col
= coords
.GetCol();
1468 if ( (size_t)col
>= pos
)
1472 // If rows inserted, include row counter where necessary
1473 coords
.SetCol(col
+ numCols
);
1475 else if (numCols
< 0)
1477 // If rows deleted ...
1478 if ((size_t)col
>= pos
- numCols
)
1480 // ...either decrement row counter (if row still exists)...
1481 coords
.SetCol(col
+ numCols
);
1485 // ...or remove the attribute
1486 m_attrs
.RemoveAt((size_t)n
);
1494 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1496 size_t count
= m_attrs
.GetCount();
1497 for ( size_t n
= 0; n
< count
; n
++ )
1499 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1500 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1509 // ----------------------------------------------------------------------------
1510 // wxGridRowOrColAttrData
1511 // ----------------------------------------------------------------------------
1513 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1515 size_t count
= m_attrs
.Count();
1516 for ( size_t n
= 0; n
< count
; n
++ )
1518 m_attrs
[n
]->DecRef();
1522 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1524 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1526 int n
= m_rowsOrCols
.Index(rowOrCol
);
1527 if ( n
!= wxNOT_FOUND
)
1529 attr
= m_attrs
[(size_t)n
];
1536 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1538 int n
= m_rowsOrCols
.Index(rowOrCol
);
1539 if ( n
== wxNOT_FOUND
)
1541 // add the attribute
1542 m_rowsOrCols
.Add(rowOrCol
);
1549 // change the attribute
1550 m_attrs
[(size_t)n
] = attr
;
1554 // remove this attribute
1555 m_attrs
[(size_t)n
]->DecRef();
1556 m_rowsOrCols
.RemoveAt((size_t)n
);
1557 m_attrs
.RemoveAt((size_t)n
);
1562 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1564 size_t count
= m_attrs
.GetCount();
1565 for ( size_t n
= 0; n
< count
; n
++ )
1567 int & rowOrCol
= m_rowsOrCols
[n
];
1568 if ( (size_t)rowOrCol
>= pos
)
1570 if ( numRowsOrCols
> 0 )
1572 // If rows inserted, include row counter where necessary
1573 rowOrCol
+= numRowsOrCols
;
1575 else if ( numRowsOrCols
< 0)
1577 // If rows deleted, either decrement row counter (if row still exists)
1578 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1579 rowOrCol
+= numRowsOrCols
;
1582 m_rowsOrCols
.RemoveAt((size_t)n
);
1583 m_attrs
.RemoveAt((size_t)n
);
1591 // ----------------------------------------------------------------------------
1592 // wxGridCellAttrProvider
1593 // ----------------------------------------------------------------------------
1595 wxGridCellAttrProvider::wxGridCellAttrProvider()
1597 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1600 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1605 void wxGridCellAttrProvider::InitData()
1607 m_data
= new wxGridCellAttrProviderData
;
1610 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1612 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1615 // first look for the attribute of this specific cell
1616 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1620 // then look for the col attr (col attributes are more common than
1621 // the row ones, hence they have priority)
1622 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1627 // finally try the row attributes
1628 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1635 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1641 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1644 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1649 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1652 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1657 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1660 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1664 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1666 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1670 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1674 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1676 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1680 // ----------------------------------------------------------------------------
1681 // wxGridTypeRegistry
1682 // ----------------------------------------------------------------------------
1684 wxGridTypeRegistry::~wxGridTypeRegistry()
1686 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++)
1687 delete m_typeinfo
[i
];
1691 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1692 wxGridCellRenderer
* renderer
,
1693 wxGridCellEditor
* editor
)
1696 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1698 // is it already registered?
1699 if ((loc
= FindDataType(typeName
)) != -1) {
1700 delete m_typeinfo
[loc
];
1701 m_typeinfo
[loc
] = info
;
1704 m_typeinfo
.Add(info
);
1708 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1712 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1713 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1722 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1724 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1728 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1730 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1734 // ----------------------------------------------------------------------------
1736 // ----------------------------------------------------------------------------
1738 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1741 wxGridTableBase::wxGridTableBase()
1743 m_view
= (wxGrid
*) NULL
;
1744 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1747 wxGridTableBase::~wxGridTableBase()
1749 delete m_attrProvider
;
1752 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1754 delete m_attrProvider
;
1755 m_attrProvider
= attrProvider
;
1758 bool wxGridTableBase::CanHaveAttributes()
1760 if ( ! GetAttrProvider() )
1762 // use the default attr provider by default
1763 SetAttrProvider(new wxGridCellAttrProvider
);
1768 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1770 if ( m_attrProvider
)
1771 return m_attrProvider
->GetAttr(row
, col
);
1773 return (wxGridCellAttr
*)NULL
;
1776 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1778 if ( m_attrProvider
)
1780 m_attrProvider
->SetAttr(attr
, row
, col
);
1784 // as we take ownership of the pointer and don't store it, we must
1790 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1792 if ( m_attrProvider
)
1794 m_attrProvider
->SetRowAttr(attr
, row
);
1798 // as we take ownership of the pointer and don't store it, we must
1804 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1806 if ( m_attrProvider
)
1808 m_attrProvider
->SetColAttr(attr
, col
);
1812 // as we take ownership of the pointer and don't store it, we must
1818 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1820 if ( m_attrProvider
)
1822 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1826 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1828 if ( m_attrProvider
)
1830 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1834 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1836 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1837 "but your derived table class does not override this function") );
1842 bool wxGridTableBase::AppendRows( size_t numRows
)
1844 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1845 "but your derived table class does not override this function"));
1850 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1852 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1853 "but your derived table class does not override this function"));
1858 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1860 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1861 "but your derived table class does not override this function"));
1866 bool wxGridTableBase::AppendCols( size_t numCols
)
1868 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1869 "but your derived table class does not override this function"));
1874 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1876 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1877 "but your derived table class does not override this function"));
1883 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1886 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1887 // how much it makes sense to us geeks.
1891 wxString
wxGridTableBase::GetColLabelValue( int col
)
1893 // default col labels are:
1894 // cols 0 to 25 : A-Z
1895 // cols 26 to 675 : AA-ZZ
1900 for ( n
= 1; ; n
++ )
1902 s
+= (_T('A') + (wxChar
)( col%26
));
1904 if ( col
< 0 ) break;
1907 // reverse the string...
1909 for ( i
= 0; i
< n
; i
++ )
1918 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1920 return wxGRID_VALUE_STRING
;
1923 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1924 const wxString
& typeName
)
1926 return typeName
== wxGRID_VALUE_STRING
;
1929 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1931 return CanGetValueAs(row
, col
, typeName
);
1934 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1939 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1944 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1949 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1950 long WXUNUSED(value
) )
1954 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1955 double WXUNUSED(value
) )
1959 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1960 bool WXUNUSED(value
) )
1965 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1966 const wxString
& WXUNUSED(typeName
) )
1971 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1972 const wxString
& WXUNUSED(typeName
),
1973 void* WXUNUSED(value
) )
1978 //////////////////////////////////////////////////////////////////////
1980 // Message class for the grid table to send requests and notifications
1984 wxGridTableMessage::wxGridTableMessage()
1986 m_table
= (wxGridTableBase
*) NULL
;
1992 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1993 int commandInt1
, int commandInt2
)
1997 m_comInt1
= commandInt1
;
1998 m_comInt2
= commandInt2
;
2003 //////////////////////////////////////////////////////////////////////
2005 // A basic grid table for string data. An object of this class will
2006 // created by wxGrid if you don't specify an alternative table class.
2009 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2011 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2013 wxGridStringTable::wxGridStringTable()
2018 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2023 m_data
.Alloc( numRows
);
2026 sa
.Alloc( numCols
);
2027 for ( col
= 0; col
< numCols
; col
++ )
2029 sa
.Add( wxEmptyString
);
2032 for ( row
= 0; row
< numRows
; row
++ )
2038 wxGridStringTable::~wxGridStringTable()
2042 long wxGridStringTable::GetNumberRows()
2044 return m_data
.GetCount();
2047 long wxGridStringTable::GetNumberCols()
2049 if ( m_data
.GetCount() > 0 )
2050 return m_data
[0].GetCount();
2055 wxString
wxGridStringTable::GetValue( int row
, int col
)
2057 // TODO: bounds checking
2059 return m_data
[row
][col
];
2062 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2064 // TODO: bounds checking
2066 m_data
[row
][col
] = value
;
2069 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2071 // TODO: bounds checking
2073 return (m_data
[row
][col
] == wxEmptyString
);
2077 void wxGridStringTable::Clear()
2080 int numRows
, numCols
;
2082 numRows
= m_data
.GetCount();
2085 numCols
= m_data
[0].GetCount();
2087 for ( row
= 0; row
< numRows
; row
++ )
2089 for ( col
= 0; col
< numCols
; col
++ )
2091 m_data
[row
][col
] = wxEmptyString
;
2098 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2102 size_t curNumRows
= m_data
.GetCount();
2103 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2105 if ( pos
>= curNumRows
)
2107 return AppendRows( numRows
);
2111 sa
.Alloc( curNumCols
);
2112 for ( col
= 0; col
< curNumCols
; col
++ )
2114 sa
.Add( wxEmptyString
);
2117 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2119 m_data
.Insert( sa
, row
);
2121 UpdateAttrRows( pos
, numRows
);
2124 wxGridTableMessage
msg( this,
2125 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2129 GetView()->ProcessTableMessage( msg
);
2135 bool wxGridStringTable::AppendRows( size_t numRows
)
2139 size_t curNumRows
= m_data
.GetCount();
2140 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2143 if ( curNumCols
> 0 )
2145 sa
.Alloc( curNumCols
);
2146 for ( col
= 0; col
< curNumCols
; col
++ )
2148 sa
.Add( wxEmptyString
);
2152 for ( row
= 0; row
< numRows
; row
++ )
2159 wxGridTableMessage
msg( this,
2160 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2163 GetView()->ProcessTableMessage( msg
);
2169 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2173 size_t curNumRows
= m_data
.GetCount();
2175 if ( pos
>= curNumRows
)
2178 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2179 "Pos value is invalid for present table with %d rows",
2180 pos
, numRows
, curNumRows
);
2181 wxFAIL_MSG( wxT(errmsg
) );
2185 if ( numRows
> curNumRows
- pos
)
2187 numRows
= curNumRows
- pos
;
2190 if ( numRows
>= curNumRows
)
2192 m_data
.Empty(); // don't release memory just yet
2196 for ( n
= 0; n
< numRows
; n
++ )
2198 m_data
.Remove( pos
);
2201 UpdateAttrRows( pos
, -((int)numRows
) );
2204 wxGridTableMessage
msg( this,
2205 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2209 GetView()->ProcessTableMessage( msg
);
2215 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2219 size_t curNumRows
= m_data
.GetCount();
2220 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2222 if ( pos
>= curNumCols
)
2224 return AppendCols( numCols
);
2227 for ( row
= 0; row
< curNumRows
; row
++ )
2229 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2231 m_data
[row
].Insert( wxEmptyString
, col
);
2234 UpdateAttrCols( pos
, numCols
);
2237 wxGridTableMessage
msg( this,
2238 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2242 GetView()->ProcessTableMessage( msg
);
2248 bool wxGridStringTable::AppendCols( size_t numCols
)
2252 size_t curNumRows
= m_data
.GetCount();
2255 // TODO: something better than this ?
2257 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2258 "Call AppendRows() first") );
2262 for ( row
= 0; row
< curNumRows
; row
++ )
2264 for ( n
= 0; n
< numCols
; n
++ )
2266 m_data
[row
].Add( wxEmptyString
);
2272 wxGridTableMessage
msg( this,
2273 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2276 GetView()->ProcessTableMessage( msg
);
2282 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2286 size_t curNumRows
= m_data
.GetCount();
2287 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2289 if ( pos
>= curNumCols
)
2292 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2293 "Pos value is invalid for present table with %d cols",
2294 pos
, numCols
, curNumCols
);
2295 wxFAIL_MSG( wxT( errmsg
) );
2299 if ( numCols
> curNumCols
- pos
)
2301 numCols
= curNumCols
- pos
;
2304 for ( row
= 0; row
< curNumRows
; row
++ )
2306 if ( numCols
>= curNumCols
)
2308 m_data
[row
].Clear();
2312 for ( n
= 0; n
< numCols
; n
++ )
2314 m_data
[row
].Remove( pos
);
2318 UpdateAttrCols( pos
, -((int)numCols
) );
2321 wxGridTableMessage
msg( this,
2322 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2326 GetView()->ProcessTableMessage( msg
);
2332 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2334 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2336 // using default label
2338 return wxGridTableBase::GetRowLabelValue( row
);
2342 return m_rowLabels
[ row
];
2346 wxString
wxGridStringTable::GetColLabelValue( int col
)
2348 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2350 // using default label
2352 return wxGridTableBase::GetColLabelValue( col
);
2356 return m_colLabels
[ col
];
2360 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2362 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2364 int n
= m_rowLabels
.GetCount();
2366 for ( i
= n
; i
<= row
; i
++ )
2368 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2372 m_rowLabels
[row
] = value
;
2375 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2377 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2379 int n
= m_colLabels
.GetCount();
2381 for ( i
= n
; i
<= col
; i
++ )
2383 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2387 m_colLabels
[col
] = value
;
2392 //////////////////////////////////////////////////////////////////////
2393 //////////////////////////////////////////////////////////////////////
2395 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2397 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2398 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2399 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2400 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2403 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2405 const wxPoint
&pos
, const wxSize
&size
)
2406 : wxWindow( parent
, id
, pos
, size
)
2411 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2415 // NO - don't do this because it will set both the x and y origin
2416 // coords to match the parent scrolled window and we just want to
2417 // set the y coord - MB
2419 // m_owner->PrepareDC( dc );
2422 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2423 dc
.SetDeviceOrigin( 0, -y
);
2425 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2426 m_owner
->DrawRowLabels( dc
);
2430 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2432 m_owner
->ProcessRowLabelMouseEvent( event
);
2436 // This seems to be required for wxMotif otherwise the mouse
2437 // cursor must be in the cell edit control to get key events
2439 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2441 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2446 //////////////////////////////////////////////////////////////////////
2448 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2450 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2451 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2452 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2453 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2456 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2458 const wxPoint
&pos
, const wxSize
&size
)
2459 : wxWindow( parent
, id
, pos
, size
)
2464 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2468 // NO - don't do this because it will set both the x and y origin
2469 // coords to match the parent scrolled window and we just want to
2470 // set the x coord - MB
2472 // m_owner->PrepareDC( dc );
2475 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2476 dc
.SetDeviceOrigin( -x
, 0 );
2478 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2479 m_owner
->DrawColLabels( dc
);
2483 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2485 m_owner
->ProcessColLabelMouseEvent( event
);
2489 // This seems to be required for wxMotif otherwise the mouse
2490 // cursor must be in the cell edit control to get key events
2492 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2494 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2499 //////////////////////////////////////////////////////////////////////
2501 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2503 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2504 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2505 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2506 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2509 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2511 const wxPoint
&pos
, const wxSize
&size
)
2512 : wxWindow( parent
, id
, pos
, size
)
2517 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2521 int client_height
= 0;
2522 int client_width
= 0;
2523 GetClientSize( &client_width
, &client_height
);
2525 dc
.SetPen( *wxBLACK_PEN
);
2526 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2527 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2529 dc
.SetPen( *wxWHITE_PEN
);
2530 dc
.DrawLine( 0, 0, client_width
, 0 );
2531 dc
.DrawLine( 0, 0, 0, client_height
);
2535 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2537 m_owner
->ProcessCornerLabelMouseEvent( event
);
2541 // This seems to be required for wxMotif otherwise the mouse
2542 // cursor must be in the cell edit control to get key events
2544 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2546 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2551 //////////////////////////////////////////////////////////////////////
2553 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2555 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2556 EVT_PAINT( wxGridWindow::OnPaint
)
2557 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2558 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2559 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2562 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2563 wxGridRowLabelWindow
*rowLblWin
,
2564 wxGridColLabelWindow
*colLblWin
,
2565 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2566 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
2569 m_rowLabelWin
= rowLblWin
;
2570 m_colLabelWin
= colLblWin
;
2571 SetBackgroundColour( "WHITE" );
2575 wxGridWindow::~wxGridWindow()
2580 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2582 wxPaintDC
dc( this );
2583 m_owner
->PrepareDC( dc
);
2584 wxRegion reg
= GetUpdateRegion();
2585 m_owner
->CalcCellsExposed( reg
);
2586 m_owner
->DrawGridCellArea( dc
);
2587 m_owner
->DrawGridSpace( dc
);
2588 #if WXGRID_DRAW_LINES
2589 m_owner
->DrawAllGridLines( dc
, reg
);
2591 m_owner
->DrawHighlight( dc
);
2595 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2597 wxPanel::ScrollWindow( dx
, dy
, rect
);
2598 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2599 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2603 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2605 m_owner
->ProcessGridCellMouseEvent( event
);
2609 // This seems to be required for wxMotif otherwise the mouse
2610 // cursor must be in the cell edit control to get key events
2612 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2614 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2618 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2623 //////////////////////////////////////////////////////////////////////
2626 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2628 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2629 EVT_PAINT( wxGrid::OnPaint
)
2630 EVT_SIZE( wxGrid::OnSize
)
2631 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2632 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2635 wxGrid::wxGrid( wxWindow
*parent
,
2640 const wxString
& name
)
2641 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2642 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2651 m_defaultCellAttr
->SafeDecRef();
2653 #ifdef DEBUG_ATTR_CACHE
2654 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2655 wxPrintf(_T("wxGrid attribute cache statistics: "
2656 "total: %u, hits: %u (%u%%)\n"),
2657 total
, gs_nAttrCacheHits
,
2658 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2664 delete m_typeRegistry
;
2669 // ----- internal init and update functions
2672 void wxGrid::Create()
2674 m_created
= FALSE
; // set to TRUE by CreateGrid
2675 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2677 m_table
= (wxGridTableBase
*) NULL
;
2680 m_cellEditCtrlEnabled
= FALSE
;
2682 m_defaultCellAttr
= new wxGridCellAttr
;
2683 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2685 // Set default cell attributes
2686 m_defaultCellAttr
->SetFont(GetFont());
2687 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2688 m_defaultCellAttr
->SetTextColour(
2689 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2690 m_defaultCellAttr
->SetBackgroundColour(
2691 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2692 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2693 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2698 m_currentCellCoords
= wxGridNoCellCoords
;
2700 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2701 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2703 // data type registration: register all standard data types
2704 // TODO: may be allow the app to selectively disable some of them?
2705 m_typeRegistry
= new wxGridTypeRegistry
;
2706 RegisterDataType(wxGRID_VALUE_STRING
,
2707 new wxGridCellStringRenderer
,
2708 new wxGridCellTextEditor
);
2709 RegisterDataType(wxGRID_VALUE_BOOL
,
2710 new wxGridCellBoolRenderer
,
2711 new wxGridCellBoolEditor
);
2712 RegisterDataType(wxGRID_VALUE_NUMBER
,
2713 new wxGridCellNumberRenderer
,
2714 new wxGridCellNumberEditor
);
2716 // subwindow components that make up the wxGrid
2717 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2722 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2727 m_colLabelWin
= new wxGridColLabelWindow( this,
2732 m_gridWin
= new wxGridWindow( this,
2739 SetTargetWindow( m_gridWin
);
2743 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2747 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2752 m_numRows
= numRows
;
2753 m_numCols
= numCols
;
2755 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2756 m_table
->SetView( this );
2765 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2769 // RD: Actually, this should probably be allowed. I think it would be
2770 // nice to be able to switch multiple Tables in and out of a single
2771 // View at runtime. Is there anything in the implmentation that would
2774 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2779 m_numRows
= table
->GetNumberRows();
2780 m_numCols
= table
->GetNumberCols();
2783 m_table
->SetView( this );
2796 if ( m_numRows
<= 0 )
2797 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2799 if ( m_numCols
<= 0 )
2800 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2802 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2803 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2805 if ( m_rowLabelWin
)
2807 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2811 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2814 m_labelTextColour
= wxColour( _T("BLACK") );
2817 m_attrCache
.row
= -1;
2819 // TODO: something better than this ?
2821 m_labelFont
= this->GetFont();
2822 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2824 m_rowLabelHorizAlign
= wxLEFT
;
2825 m_rowLabelVertAlign
= wxCENTRE
;
2827 m_colLabelHorizAlign
= wxCENTRE
;
2828 m_colLabelVertAlign
= wxTOP
;
2830 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2831 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2833 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2834 m_defaultRowHeight
+= 8;
2836 m_defaultRowHeight
+= 4;
2839 m_gridLineColour
= wxColour( 128, 128, 255 );
2840 m_gridLinesEnabled
= TRUE
;
2842 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2843 m_winCapture
= (wxWindow
*)NULL
;
2844 m_canDragRowSize
= TRUE
;
2845 m_canDragColSize
= TRUE
;
2846 m_canDragGridSize
= TRUE
;
2848 m_dragRowOrCol
= -1;
2849 m_isDragging
= FALSE
;
2850 m_startDragPos
= wxDefaultPosition
;
2852 m_waitForSlowClick
= FALSE
;
2854 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2855 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2857 m_currentCellCoords
= wxGridNoCellCoords
;
2859 m_selectedTopLeft
= wxGridNoCellCoords
;
2860 m_selectedBottomRight
= wxGridNoCellCoords
;
2861 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2862 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2864 m_editable
= TRUE
; // default for whole grid
2866 m_inOnKeyDown
= FALSE
;
2870 // ----------------------------------------------------------------------------
2871 // the idea is to call these functions only when necessary because they create
2872 // quite big arrays which eat memory mostly unnecessary - in particular, if
2873 // default widths/heights are used for all rows/columns, we may not use these
2876 // with some extra code, it should be possible to only store the
2877 // widths/heights different from default ones but this will be done later...
2878 // ----------------------------------------------------------------------------
2880 void wxGrid::InitRowHeights()
2882 m_rowHeights
.Empty();
2883 m_rowBottoms
.Empty();
2885 m_rowHeights
.Alloc( m_numRows
);
2886 m_rowBottoms
.Alloc( m_numRows
);
2889 for ( int i
= 0; i
< m_numRows
; i
++ )
2891 m_rowHeights
.Add( m_defaultRowHeight
);
2892 rowBottom
+= m_defaultRowHeight
;
2893 m_rowBottoms
.Add( rowBottom
);
2897 void wxGrid::InitColWidths()
2899 m_colWidths
.Empty();
2900 m_colRights
.Empty();
2902 m_colWidths
.Alloc( m_numCols
);
2903 m_colRights
.Alloc( m_numCols
);
2905 for ( int i
= 0; i
< m_numCols
; i
++ )
2907 m_colWidths
.Add( m_defaultColWidth
);
2908 colRight
+= m_defaultColWidth
;
2909 m_colRights
.Add( colRight
);
2913 int wxGrid::GetColWidth(int col
) const
2915 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2918 int wxGrid::GetColLeft(int col
) const
2920 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2921 : m_colRights
[col
] - m_colWidths
[col
];
2924 int wxGrid::GetColRight(int col
) const
2926 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
2930 int wxGrid::GetRowHeight(int row
) const
2932 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2935 int wxGrid::GetRowTop(int row
) const
2937 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2938 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2941 int wxGrid::GetRowBottom(int row
) const
2943 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2944 : m_rowBottoms
[row
];
2947 void wxGrid::CalcDimensions()
2950 GetClientSize( &cw
, &ch
);
2952 if ( m_numRows
> 0 && m_numCols
> 0 )
2954 int right
= GetColRight( m_numCols
-1 ) + 50;
2955 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
2957 // TODO: restore the scroll position that we had before sizing
2960 GetViewStart( &x
, &y
);
2961 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2962 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2968 void wxGrid::CalcWindowSizes()
2971 GetClientSize( &cw
, &ch
);
2973 if ( m_cornerLabelWin
->IsShown() )
2974 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2976 if ( m_colLabelWin
->IsShown() )
2977 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2979 if ( m_rowLabelWin
->IsShown() )
2980 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2982 if ( m_gridWin
->IsShown() )
2983 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2987 // this is called when the grid table sends a message to say that it
2988 // has been redimensioned
2990 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2994 // if we were using the default widths/heights so far, we must change them
2996 if ( m_colWidths
.IsEmpty() )
3001 if ( m_rowHeights
.IsEmpty() )
3006 switch ( msg
.GetId() )
3008 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3010 size_t pos
= msg
.GetCommandInt();
3011 int numRows
= msg
.GetCommandInt2();
3012 for ( i
= 0; i
< numRows
; i
++ )
3014 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3015 m_rowBottoms
.Insert( 0, pos
);
3017 m_numRows
+= numRows
;
3020 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3022 for ( i
= pos
; i
< m_numRows
; i
++ )
3024 bottom
+= m_rowHeights
[i
];
3025 m_rowBottoms
[i
] = bottom
;
3031 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3033 int numRows
= msg
.GetCommandInt();
3034 for ( i
= 0; i
< numRows
; i
++ )
3036 m_rowHeights
.Add( m_defaultRowHeight
);
3037 m_rowBottoms
.Add( 0 );
3040 int oldNumRows
= m_numRows
;
3041 m_numRows
+= numRows
;
3044 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3046 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3048 bottom
+= m_rowHeights
[i
];
3049 m_rowBottoms
[i
] = bottom
;
3055 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3057 size_t pos
= msg
.GetCommandInt();
3058 int numRows
= msg
.GetCommandInt2();
3059 for ( i
= 0; i
< numRows
; i
++ )
3061 m_rowHeights
.Remove( pos
);
3062 m_rowBottoms
.Remove( pos
);
3064 m_numRows
-= numRows
;
3069 m_colWidths
.Clear();
3070 m_colRights
.Clear();
3071 m_currentCellCoords
= wxGridNoCellCoords
;
3075 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3076 m_currentCellCoords
.Set( 0, 0 );
3079 for ( i
= 0; i
< m_numRows
; i
++ )
3081 h
+= m_rowHeights
[i
];
3082 m_rowBottoms
[i
] = h
;
3090 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3092 size_t pos
= msg
.GetCommandInt();
3093 int numCols
= msg
.GetCommandInt2();
3094 for ( i
= 0; i
< numCols
; i
++ )
3096 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3097 m_colRights
.Insert( 0, pos
);
3099 m_numCols
+= numCols
;
3102 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3104 for ( i
= pos
; i
< m_numCols
; i
++ )
3106 right
+= m_colWidths
[i
];
3107 m_colRights
[i
] = right
;
3113 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3115 int numCols
= msg
.GetCommandInt();
3116 for ( i
= 0; i
< numCols
; i
++ )
3118 m_colWidths
.Add( m_defaultColWidth
);
3119 m_colRights
.Add( 0 );
3122 int oldNumCols
= m_numCols
;
3123 m_numCols
+= numCols
;
3126 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3128 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3130 right
+= m_colWidths
[i
];
3131 m_colRights
[i
] = right
;
3137 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3139 size_t pos
= msg
.GetCommandInt();
3140 int numCols
= msg
.GetCommandInt2();
3141 for ( i
= 0; i
< numCols
; i
++ )
3143 m_colWidths
.Remove( pos
);
3144 m_colRights
.Remove( pos
);
3146 m_numCols
-= numCols
;
3150 #if 0 // leave the row alone here so that AppendCols will work subsequently
3152 m_rowHeights
.Clear();
3153 m_rowBottoms
.Clear();
3155 m_currentCellCoords
= wxGridNoCellCoords
;
3159 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3160 m_currentCellCoords
.Set( 0, 0 );
3163 for ( i
= 0; i
< m_numCols
; i
++ )
3165 w
+= m_colWidths
[i
];
3178 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3180 wxRegionIterator
iter( reg
);
3183 m_rowLabelsExposed
.Empty();
3190 // TODO: remove this when we can...
3191 // There is a bug in wxMotif that gives garbage update
3192 // rectangles if you jump-scroll a long way by clicking the
3193 // scrollbar with middle button. This is a work-around
3195 #if defined(__WXMOTIF__)
3197 m_gridWin
->GetClientSize( &cw
, &ch
);
3198 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3199 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3202 // logical bounds of update region
3205 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3206 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3208 // find the row labels within these bounds
3211 for ( row
= 0; row
< m_numRows
; row
++ )
3213 if ( GetRowBottom(row
) < top
)
3216 if ( GetRowTop(row
) > bottom
)
3219 m_rowLabelsExposed
.Add( row
);
3227 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3229 wxRegionIterator
iter( reg
);
3232 m_colLabelsExposed
.Empty();
3239 // TODO: remove this when we can...
3240 // There is a bug in wxMotif that gives garbage update
3241 // rectangles if you jump-scroll a long way by clicking the
3242 // scrollbar with middle button. This is a work-around
3244 #if defined(__WXMOTIF__)
3246 m_gridWin
->GetClientSize( &cw
, &ch
);
3247 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3248 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3251 // logical bounds of update region
3254 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3255 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3257 // find the cells within these bounds
3260 for ( col
= 0; col
< m_numCols
; col
++ )
3262 if ( GetColRight(col
) < left
)
3265 if ( GetColLeft(col
) > right
)
3268 m_colLabelsExposed
.Add( col
);
3276 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3278 wxRegionIterator
iter( reg
);
3281 m_cellsExposed
.Empty();
3282 m_rowsExposed
.Empty();
3283 m_colsExposed
.Empty();
3285 int left
, top
, right
, bottom
;
3290 // TODO: remove this when we can...
3291 // There is a bug in wxMotif that gives garbage update
3292 // rectangles if you jump-scroll a long way by clicking the
3293 // scrollbar with middle button. This is a work-around
3295 #if defined(__WXMOTIF__)
3297 m_gridWin
->GetClientSize( &cw
, &ch
);
3298 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3299 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3300 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3301 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3304 // logical bounds of update region
3306 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3307 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3309 // find the cells within these bounds
3312 for ( row
= 0; row
< m_numRows
; row
++ )
3314 if ( GetRowBottom(row
) <= top
)
3317 if ( GetRowTop(row
) > bottom
)
3320 m_rowsExposed
.Add( row
);
3322 for ( col
= 0; col
< m_numCols
; col
++ )
3324 if ( GetColRight(col
) <= left
)
3327 if ( GetColLeft(col
) > right
)
3330 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3331 m_colsExposed
.Add( col
);
3332 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3341 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3344 wxPoint
pos( event
.GetPosition() );
3345 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3347 if ( event
.Dragging() )
3349 m_isDragging
= TRUE
;
3351 if ( event
.LeftIsDown() )
3353 switch( m_cursorMode
)
3355 case WXGRID_CURSOR_RESIZE_ROW
:
3357 int cw
, ch
, left
, dummy
;
3358 m_gridWin
->GetClientSize( &cw
, &ch
);
3359 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3361 wxClientDC
dc( m_gridWin
);
3363 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3364 dc
.SetLogicalFunction(wxINVERT
);
3365 if ( m_dragLastPos
>= 0 )
3367 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3369 dc
.DrawLine( left
, y
, left
+cw
, y
);
3374 case WXGRID_CURSOR_SELECT_ROW
:
3375 if ( (row
= YToRow( y
)) >= 0 &&
3376 !IsInSelection( row
, 0 ) )
3378 SelectRow( row
, TRUE
);
3381 // default label to suppress warnings about "enumeration value
3382 // 'xxx' not handled in switch
3390 m_isDragging
= FALSE
;
3393 // ------------ Entering or leaving the window
3395 if ( event
.Entering() || event
.Leaving() )
3397 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3401 // ------------ Left button pressed
3403 else if ( event
.LeftDown() )
3405 // don't send a label click event for a hit on the
3406 // edge of the row label - this is probably the user
3407 // wanting to resize the row
3409 if ( YToEdgeOfRow(y
) < 0 )
3413 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3415 SelectRow( row
, event
.ShiftDown() );
3416 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3421 // starting to drag-resize a row
3423 if ( CanDragRowSize() )
3424 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3429 // ------------ Left double click
3431 else if (event
.LeftDClick() )
3433 if ( YToEdgeOfRow(y
) < 0 )
3436 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3441 // ------------ Left button released
3443 else if ( event
.LeftUp() )
3445 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3447 DoEndDragResizeRow();
3449 // Note: we are ending the event *after* doing
3450 // default processing in this case
3452 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3455 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3460 // ------------ Right button down
3462 else if ( event
.RightDown() )
3465 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3467 // no default action at the moment
3472 // ------------ Right double click
3474 else if ( event
.RightDClick() )
3477 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3479 // no default action at the moment
3484 // ------------ No buttons down and mouse moving
3486 else if ( event
.Moving() )
3488 m_dragRowOrCol
= YToEdgeOfRow( y
);
3489 if ( m_dragRowOrCol
>= 0 )
3491 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3493 // don't capture the mouse yet
3494 if ( CanDragRowSize() )
3495 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3498 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3500 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3506 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3509 wxPoint
pos( event
.GetPosition() );
3510 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3512 if ( event
.Dragging() )
3514 m_isDragging
= TRUE
;
3516 if ( event
.LeftIsDown() )
3518 switch( m_cursorMode
)
3520 case WXGRID_CURSOR_RESIZE_COL
:
3522 int cw
, ch
, dummy
, top
;
3523 m_gridWin
->GetClientSize( &cw
, &ch
);
3524 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3526 wxClientDC
dc( m_gridWin
);
3529 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3530 GetColMinimalWidth(m_dragRowOrCol
));
3531 dc
.SetLogicalFunction(wxINVERT
);
3532 if ( m_dragLastPos
>= 0 )
3534 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3536 dc
.DrawLine( x
, top
, x
, top
+ch
);
3541 case WXGRID_CURSOR_SELECT_COL
:
3542 if ( (col
= XToCol( x
)) >= 0 &&
3543 !IsInSelection( 0, col
) )
3545 SelectCol( col
, TRUE
);
3548 // default label to suppress warnings about "enumeration value
3549 // 'xxx' not handled in switch
3557 m_isDragging
= FALSE
;
3560 // ------------ Entering or leaving the window
3562 if ( event
.Entering() || event
.Leaving() )
3564 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3568 // ------------ Left button pressed
3570 else if ( event
.LeftDown() )
3572 // don't send a label click event for a hit on the
3573 // edge of the col label - this is probably the user
3574 // wanting to resize the col
3576 if ( XToEdgeOfCol(x
) < 0 )
3580 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3582 SelectCol( col
, event
.ShiftDown() );
3583 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3588 // starting to drag-resize a col
3590 if ( CanDragColSize() )
3591 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3596 // ------------ Left double click
3598 if ( event
.LeftDClick() )
3600 if ( XToEdgeOfCol(x
) < 0 )
3603 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3608 // ------------ Left button released
3610 else if ( event
.LeftUp() )
3612 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3614 DoEndDragResizeCol();
3616 // Note: we are ending the event *after* doing
3617 // default processing in this case
3619 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3622 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3627 // ------------ Right button down
3629 else if ( event
.RightDown() )
3632 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3634 // no default action at the moment
3639 // ------------ Right double click
3641 else if ( event
.RightDClick() )
3644 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3646 // no default action at the moment
3651 // ------------ No buttons down and mouse moving
3653 else if ( event
.Moving() )
3655 m_dragRowOrCol
= XToEdgeOfCol( x
);
3656 if ( m_dragRowOrCol
>= 0 )
3658 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3660 // don't capture the cursor yet
3661 if ( CanDragColSize() )
3662 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3665 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3667 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3673 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3675 if ( event
.LeftDown() )
3677 // indicate corner label by having both row and
3680 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3686 else if ( event
.LeftDClick() )
3688 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3691 else if ( event
.RightDown() )
3693 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3695 // no default action at the moment
3699 else if ( event
.RightDClick() )
3701 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3703 // no default action at the moment
3708 void wxGrid::ChangeCursorMode(CursorMode mode
,
3713 static const wxChar
*cursorModes
[] =
3722 wxLogTrace(_T("grid"),
3723 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3724 win
== m_colLabelWin
? _T("colLabelWin")
3725 : win
? _T("rowLabelWin")
3727 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3728 #endif // __WXDEBUG__
3730 if ( mode
== m_cursorMode
)
3735 // by default use the grid itself
3741 m_winCapture
->ReleaseMouse();
3742 m_winCapture
= (wxWindow
*)NULL
;
3745 m_cursorMode
= mode
;
3747 switch ( m_cursorMode
)
3749 case WXGRID_CURSOR_RESIZE_ROW
:
3750 win
->SetCursor( m_rowResizeCursor
);
3753 case WXGRID_CURSOR_RESIZE_COL
:
3754 win
->SetCursor( m_colResizeCursor
);
3758 win
->SetCursor( *wxSTANDARD_CURSOR
);
3761 // we need to capture mouse when resizing
3762 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3763 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3765 if ( captureMouse
&& resize
)
3767 win
->CaptureMouse();
3772 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3775 wxPoint
pos( event
.GetPosition() );
3776 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3778 wxGridCellCoords coords
;
3779 XYToCell( x
, y
, coords
);
3781 if ( event
.Dragging() )
3783 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3785 // Don't start doing anything until the mouse has been drug at
3786 // least 3 pixels in any direction...
3789 if (m_startDragPos
== wxDefaultPosition
)
3791 m_startDragPos
= pos
;
3794 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3798 m_isDragging
= TRUE
;
3799 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3801 // Hide the edit control, so it
3802 // won't interfer with drag-shrinking.
3803 if ( IsCellEditControlEnabled() )
3804 HideCellEditControl();
3806 // Have we captured the mouse yet?
3809 m_winCapture
= m_gridWin
;
3810 m_winCapture
->CaptureMouse();
3813 if ( coords
!= wxGridNoCellCoords
)
3815 if ( !IsSelection() )
3817 SelectBlock( coords
, coords
);
3821 SelectBlock( m_currentCellCoords
, coords
);
3824 if (! IsVisible(coords
))
3826 MakeCellVisible(coords
);
3827 // TODO: need to introduce a delay or something here. The
3828 // scrolling is way to fast, at least on MSW.
3832 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3834 int cw
, ch
, left
, dummy
;
3835 m_gridWin
->GetClientSize( &cw
, &ch
);
3836 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3838 wxClientDC
dc( m_gridWin
);
3840 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3841 dc
.SetLogicalFunction(wxINVERT
);
3842 if ( m_dragLastPos
>= 0 )
3844 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3846 dc
.DrawLine( left
, y
, left
+cw
, y
);
3849 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3851 int cw
, ch
, dummy
, top
;
3852 m_gridWin
->GetClientSize( &cw
, &ch
);
3853 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3855 wxClientDC
dc( m_gridWin
);
3857 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3858 GetColMinimalWidth(m_dragRowOrCol
) );
3859 dc
.SetLogicalFunction(wxINVERT
);
3860 if ( m_dragLastPos
>= 0 )
3862 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3864 dc
.DrawLine( x
, top
, x
, top
+ch
);
3871 m_isDragging
= FALSE
;
3872 m_startDragPos
= wxDefaultPosition
;
3874 // if ( coords == wxGridNoCellCoords && m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3876 // ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3879 // if ( coords != wxGridNoCellCoords )
3881 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3882 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3885 if ( event
.Entering() || event
.Leaving() )
3887 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3888 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3893 // ------------ Left button pressed
3895 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
3897 DisableCellEditControl();
3898 if ( event
.ShiftDown() )
3900 SelectBlock( m_currentCellCoords
, coords
);
3902 else if ( XToEdgeOfCol(x
) < 0 &&
3903 YToEdgeOfRow(y
) < 0 )
3905 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3910 MakeCellVisible( coords
);
3912 // if this is the second click on this cell then start
3914 if ( m_waitForSlowClick
&&
3915 (coords
== m_currentCellCoords
) &&
3916 CanEnableCellControl())
3918 EnableCellEditControl();
3920 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3921 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
3924 m_waitForSlowClick
= FALSE
;
3928 SetCurrentCell( coords
);
3929 m_waitForSlowClick
= TRUE
;
3936 // ------------ Left double click
3938 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
3940 DisableCellEditControl();
3941 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3943 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3951 // ------------ Left button released
3953 else if ( event
.LeftUp() )
3955 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3957 if ( IsSelection() )
3961 m_winCapture
->ReleaseMouse();
3962 m_winCapture
= NULL
;
3964 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3967 // Show the edit control, if it has been hidden for
3969 ShowCellEditControl();
3971 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3973 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3974 DoEndDragResizeRow();
3976 // Note: we are ending the event *after* doing
3977 // default processing in this case
3979 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3981 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3983 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3984 DoEndDragResizeCol();
3986 // Note: we are ending the event *after* doing
3987 // default processing in this case
3989 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3996 // ------------ Right button down
3998 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4000 DisableCellEditControl();
4001 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4006 // no default action at the moment
4011 // ------------ Right double click
4013 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4015 DisableCellEditControl();
4016 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4021 // no default action at the moment
4025 // ------------ Moving and no button action
4027 else if ( event
.Moving() && !event
.IsButton() )
4029 int dragRow
= YToEdgeOfRow( y
);
4030 int dragCol
= XToEdgeOfCol( x
);
4032 // Dragging on the corner of a cell to resize in both
4033 // directions is not implemented yet...
4035 if ( dragRow
>= 0 && dragCol
>= 0 )
4037 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4043 m_dragRowOrCol
= dragRow
;
4045 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4047 if ( CanDragRowSize() && CanDragGridSize() )
4048 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4056 m_dragRowOrCol
= dragCol
;
4058 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4060 if ( CanDragColSize() && CanDragGridSize() )
4061 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4067 // Neither on a row or col edge
4069 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4071 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4077 void wxGrid::DoEndDragResizeRow()
4079 if ( m_dragLastPos
>= 0 )
4081 // erase the last line and resize the row
4083 int cw
, ch
, left
, dummy
;
4084 m_gridWin
->GetClientSize( &cw
, &ch
);
4085 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4087 wxClientDC
dc( m_gridWin
);
4089 dc
.SetLogicalFunction( wxINVERT
);
4090 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4091 HideCellEditControl();
4093 int rowTop
= GetRowTop(m_dragRowOrCol
);
4094 SetRowSize( m_dragRowOrCol
,
4095 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4097 if ( !GetBatchCount() )
4099 // Only needed to get the correct rect.y:
4100 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4102 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4103 rect
.width
= m_rowLabelWidth
;
4104 rect
.height
= ch
- rect
.y
;
4105 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4107 m_gridWin
->Refresh( FALSE
, &rect
);
4110 ShowCellEditControl();
4115 void wxGrid::DoEndDragResizeCol()
4117 if ( m_dragLastPos
>= 0 )
4119 // erase the last line and resize the col
4121 int cw
, ch
, dummy
, top
;
4122 m_gridWin
->GetClientSize( &cw
, &ch
);
4123 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4125 wxClientDC
dc( m_gridWin
);
4127 dc
.SetLogicalFunction( wxINVERT
);
4128 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4129 HideCellEditControl();
4131 int colLeft
= GetColLeft(m_dragRowOrCol
);
4132 SetColSize( m_dragRowOrCol
,
4133 wxMax( m_dragLastPos
- colLeft
,
4134 GetColMinimalWidth(m_dragRowOrCol
) ) );
4136 if ( !GetBatchCount() )
4138 // Only needed to get the correct rect.x:
4139 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4141 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4142 rect
.width
= cw
- rect
.x
;
4143 rect
.height
= m_colLabelHeight
;
4144 m_colLabelWin
->Refresh( TRUE
, &rect
);
4146 m_gridWin
->Refresh( FALSE
, &rect
);
4149 ShowCellEditControl();
4156 // ------ interaction with data model
4158 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4160 switch ( msg
.GetId() )
4162 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4163 return GetModelValues();
4165 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4166 return SetModelValues();
4168 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4169 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4170 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4171 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4172 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4173 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4174 return Redimension( msg
);
4183 // The behaviour of this function depends on the grid table class
4184 // Clear() function. For the default wxGridStringTable class the
4185 // behavious is to replace all cell contents with wxEmptyString but
4186 // not to change the number of rows or cols.
4188 void wxGrid::ClearGrid()
4192 if (IsCellEditControlEnabled())
4193 DisableCellEditControl();
4196 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4201 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4203 // TODO: something with updateLabels flag
4207 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4213 if (IsCellEditControlEnabled())
4214 DisableCellEditControl();
4216 bool ok
= m_table
->InsertRows( pos
, numRows
);
4218 // the table will have sent the results of the insert row
4219 // operation to this view object as a grid table message
4223 if ( m_numCols
== 0 )
4225 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4227 // TODO: perhaps instead of appending the default number of cols
4228 // we should remember what the last non-zero number of cols was ?
4232 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4234 // if we have just inserted cols into an empty grid the current
4235 // cell will be undefined...
4237 SetCurrentCell( 0, 0 );
4241 if ( !GetBatchCount() ) Refresh();
4253 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4255 // TODO: something with updateLabels flag
4259 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4263 if ( m_table
&& m_table
->AppendRows( numRows
) )
4265 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4267 // if we have just inserted cols into an empty grid the current
4268 // cell will be undefined...
4270 SetCurrentCell( 0, 0 );
4273 // the table will have sent the results of the append row
4274 // operation to this view object as a grid table message
4277 if ( !GetBatchCount() ) Refresh();
4287 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4289 // TODO: something with updateLabels flag
4293 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4299 if (IsCellEditControlEnabled())
4300 DisableCellEditControl();
4302 if (m_table
->DeleteRows( pos
, numRows
))
4305 // the table will have sent the results of the delete row
4306 // operation to this view object as a grid table message
4309 if ( !GetBatchCount() ) Refresh();
4317 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4319 // TODO: something with updateLabels flag
4323 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4329 if (IsCellEditControlEnabled())
4330 DisableCellEditControl();
4332 bool ok
= m_table
->InsertCols( pos
, numCols
);
4334 // the table will have sent the results of the insert col
4335 // operation to this view object as a grid table message
4339 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4341 // if we have just inserted cols into an empty grid the current
4342 // cell will be undefined...
4344 SetCurrentCell( 0, 0 );
4348 if ( !GetBatchCount() ) Refresh();
4360 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4362 // TODO: something with updateLabels flag
4366 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4370 if ( m_table
&& m_table
->AppendCols( numCols
) )
4372 // the table will have sent the results of the append col
4373 // operation to this view object as a grid table message
4375 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4377 // if we have just inserted cols into an empty grid the current
4378 // cell will be undefined...
4380 SetCurrentCell( 0, 0 );
4384 if ( !GetBatchCount() ) Refresh();
4394 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4396 // TODO: something with updateLabels flag
4400 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4406 if (IsCellEditControlEnabled())
4407 DisableCellEditControl();
4409 if ( m_table
->DeleteCols( pos
, numCols
) )
4411 // the table will have sent the results of the delete col
4412 // operation to this view object as a grid table message
4415 if ( !GetBatchCount() ) Refresh();
4425 // ----- event handlers
4428 // Generate a grid event based on a mouse event and
4429 // return the result of ProcessEvent()
4431 bool wxGrid::SendEvent( const wxEventType type
,
4433 wxMouseEvent
& mouseEv
)
4435 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4437 int rowOrCol
= (row
== -1 ? col
: row
);
4439 wxGridSizeEvent
gridEvt( GetId(),
4443 mouseEv
.GetX(), mouseEv
.GetY(),
4444 mouseEv
.ControlDown(),
4445 mouseEv
.ShiftDown(),
4447 mouseEv
.MetaDown() );
4449 return GetEventHandler()->ProcessEvent(gridEvt
);
4451 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4453 wxGridRangeSelectEvent
gridEvt( GetId(),
4457 m_selectedBottomRight
,
4458 mouseEv
.ControlDown(),
4459 mouseEv
.ShiftDown(),
4461 mouseEv
.MetaDown() );
4463 return GetEventHandler()->ProcessEvent(gridEvt
);
4467 wxGridEvent
gridEvt( GetId(),
4471 mouseEv
.GetX(), mouseEv
.GetY(),
4472 mouseEv
.ControlDown(),
4473 mouseEv
.ShiftDown(),
4475 mouseEv
.MetaDown() );
4477 return GetEventHandler()->ProcessEvent(gridEvt
);
4482 // Generate a grid event of specified type and return the result
4483 // of ProcessEvent().
4485 bool wxGrid::SendEvent( const wxEventType type
,
4488 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4490 int rowOrCol
= (row
== -1 ? col
: row
);
4492 wxGridSizeEvent
gridEvt( GetId(),
4497 return GetEventHandler()->ProcessEvent(gridEvt
);
4501 wxGridEvent
gridEvt( GetId(),
4506 return GetEventHandler()->ProcessEvent(gridEvt
);
4511 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4513 wxPaintDC
dc( this );
4515 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4516 m_numRows
&& m_numCols
)
4518 m_currentCellCoords
.Set(0, 0);
4519 ShowCellEditControl();
4526 // This is just here to make sure that CalcDimensions gets called when
4527 // the grid view is resized... then the size event is skipped to allow
4528 // the box sizers to handle everything
4530 void wxGrid::OnSize( wxSizeEvent
& event
)
4537 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4539 if ( m_inOnKeyDown
)
4541 // shouldn't be here - we are going round in circles...
4543 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4546 m_inOnKeyDown
= TRUE
;
4548 // propagate the event up and see if it gets processed
4550 wxWindow
*parent
= GetParent();
4551 wxKeyEvent
keyEvt( event
);
4552 keyEvt
.SetEventObject( parent
);
4554 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4557 // TODO: Should also support Shift-cursor keys for
4558 // extending the selection. Maybe add a flag to
4559 // MoveCursorXXX() and MoveCursorXXXBlock() and
4560 // just send event.ShiftDown().
4562 // try local handlers
4564 switch ( event
.KeyCode() )
4567 if ( event
.ControlDown() )
4569 MoveCursorUpBlock();
4578 if ( event
.ControlDown() )
4580 MoveCursorDownBlock();
4589 if ( event
.ControlDown() )
4591 MoveCursorLeftBlock();
4600 if ( event
.ControlDown() )
4602 MoveCursorRightBlock();
4611 if ( event
.ControlDown() )
4613 event
.Skip(); // to let the edit control have the return
4622 if (event
.ShiftDown())
4629 if ( event
.ControlDown() )
4631 MakeCellVisible( 0, 0 );
4632 SetCurrentCell( 0, 0 );
4641 if ( event
.ControlDown() )
4643 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4644 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4660 // We don't want these keys to trigger the edit control, any others?
4669 if ( !IsEditable() )
4674 // Otherwise fall through to default
4677 // now try the cell edit control
4679 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4681 EnableCellEditControl();
4682 int row
= m_currentCellCoords
.GetRow();
4683 int col
= m_currentCellCoords
.GetCol();
4684 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4685 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4690 // let others process char events for readonly cells
4697 m_inOnKeyDown
= FALSE
;
4701 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4705 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4707 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4709 // the event has been intercepted - do nothing
4714 m_currentCellCoords
!= wxGridNoCellCoords
)
4716 HideCellEditControl();
4717 DisableCellEditControl();
4719 // Clear the old current cell highlight
4720 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4722 // Otherwise refresh redraws the highlight!
4723 m_currentCellCoords
= coords
;
4725 m_gridWin
->Refresh( FALSE
, &r
);
4728 m_currentCellCoords
= coords
;
4732 wxClientDC
dc(m_gridWin
);
4735 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4736 DrawCellHighlight(dc
, attr
);
4739 if ( IsSelection() )
4741 wxRect
r( SelectionToDeviceRect() );
4743 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4750 // ------ functions to get/send data (see also public functions)
4753 bool wxGrid::GetModelValues()
4757 // all we need to do is repaint the grid
4759 m_gridWin
->Refresh();
4767 bool wxGrid::SetModelValues()
4773 for ( row
= 0; row
< m_numRows
; row
++ )
4775 for ( col
= 0; col
< m_numCols
; col
++ )
4777 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4789 // Note - this function only draws cells that are in the list of
4790 // exposed cells (usually set from the update region by
4791 // CalcExposedCells)
4793 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4795 if ( !m_numRows
|| !m_numCols
) return;
4798 size_t numCells
= m_cellsExposed
.GetCount();
4800 for ( i
= 0; i
< numCells
; i
++ )
4802 DrawCell( dc
, m_cellsExposed
[i
] );
4807 void wxGrid::DrawGridSpace( wxDC
& dc
)
4809 if ( m_numRows
&& m_numCols
)
4812 m_gridWin
->GetClientSize( &cw
, &ch
);
4815 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4817 if ( right
> GetColRight(m_numCols
-1) ||
4818 bottom
> GetRowBottom(m_numRows
-1) )
4821 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4823 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4824 dc
.SetPen( *wxTRANSPARENT_PEN
);
4826 if ( right
> GetColRight(m_numCols
-1) )
4827 dc
.DrawRectangle( GetColRight(m_numCols
-1)+1, top
,
4828 right
- GetColRight(m_numCols
-1), ch
);
4830 if ( bottom
> GetRowBottom(m_numRows
-1) )
4831 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1)+1,
4832 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4838 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4840 int row
= coords
.GetRow();
4841 int col
= coords
.GetCol();
4843 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4846 // we draw the cell border ourselves
4847 #if !WXGRID_DRAW_LINES
4848 if ( m_gridLinesEnabled
)
4849 DrawCellBorder( dc
, coords
);
4852 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4854 bool isCurrent
= coords
== m_currentCellCoords
;
4857 rect
.x
= GetColLeft(col
);
4858 rect
.y
= GetRowTop(row
);
4859 rect
.width
= GetColWidth(col
) - 1;
4860 rect
.height
= GetRowHeight(row
) - 1;
4862 // if the editor is shown, we should use it and not the renderer
4863 if ( isCurrent
&& IsCellEditControlEnabled() )
4865 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4869 // but all the rest is drawn by the cell renderer and hence may be
4871 attr
->GetRenderer(this, row
, col
)->
4872 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4879 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4881 int row
= m_currentCellCoords
.GetRow();
4882 int col
= m_currentCellCoords
.GetCol();
4884 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4888 rect
.x
= GetColLeft(col
);
4889 rect
.y
= GetRowTop(row
);
4890 rect
.width
= GetColWidth(col
) - 1;
4891 rect
.height
= GetRowHeight(row
) - 1;
4893 // hmmm... what could we do here to show that the cell is disabled?
4894 // for now, I just draw a thinner border than for the other ones, but
4895 // it doesn't look really good
4896 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4897 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4899 dc
.DrawRectangle(rect
);
4902 // VZ: my experiments with 3d borders...
4904 // how to properly set colours for arbitrary bg?
4905 wxCoord x1
= rect
.x
,
4907 x2
= rect
.x
+ rect
.width
-1,
4908 y2
= rect
.y
+ rect
.height
-1;
4910 dc
.SetPen(*wxWHITE_PEN
);
4911 dc
.DrawLine(x1
, y1
, x2
, y1
);
4912 dc
.DrawLine(x1
, y1
, x1
, y2
);
4914 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4915 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4917 dc
.SetPen(*wxBLACK_PEN
);
4918 dc
.DrawLine(x1
, y2
, x2
, y2
);
4919 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4924 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4926 int row
= coords
.GetRow();
4927 int col
= coords
.GetCol();
4928 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4931 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4933 // right hand border
4935 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
4936 GetColRight(col
), GetRowBottom(row
) );
4940 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
4941 GetColRight(col
), GetRowBottom(row
) );
4944 void wxGrid::DrawHighlight(wxDC
& dc
)
4946 if ( IsCellEditControlEnabled() )
4948 // don't show highlight when the edit control is shown
4952 // if the active cell was repainted, repaint its highlight too because it
4953 // might have been damaged by the grid lines
4954 size_t count
= m_cellsExposed
.GetCount();
4955 for ( size_t n
= 0; n
< count
; n
++ )
4957 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4959 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4960 DrawCellHighlight(dc
, attr
);
4968 // TODO: remove this ???
4969 // This is used to redraw all grid lines e.g. when the grid line colour
4972 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4974 if ( !m_gridLinesEnabled
||
4976 !m_numCols
) return;
4978 int top
, bottom
, left
, right
;
4984 m_gridWin
->GetClientSize(&cw
, &ch
);
4986 // virtual coords of visible area
4988 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4989 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4994 reg
.GetBox(x
, y
, w
, h
);
4995 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4996 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5000 m_gridWin
->GetClientSize(&cw
, &ch
);
5001 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5002 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5005 // avoid drawing grid lines past the last row and col
5007 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5008 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5010 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5012 // horizontal grid lines
5015 for ( i
= 0; i
< m_numRows
; i
++ )
5017 int bot
= GetRowBottom(i
) - 1;
5026 dc
.DrawLine( left
, bot
, right
, bot
);
5031 // vertical grid lines
5033 for ( i
= 0; i
< m_numCols
; i
++ )
5035 int colRight
= GetColRight(i
) - 1;
5036 if ( colRight
> right
)
5041 if ( colRight
>= left
)
5043 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5049 void wxGrid::DrawRowLabels( wxDC
& dc
)
5051 if ( !m_numRows
|| !m_numCols
) return;
5054 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5056 for ( i
= 0; i
< numLabels
; i
++ )
5058 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5063 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5065 if ( GetRowHeight(row
) <= 0 )
5068 int rowTop
= GetRowTop(row
),
5069 rowBottom
= GetRowBottom(row
) - 1;
5071 dc
.SetPen( *wxBLACK_PEN
);
5072 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5073 m_rowLabelWidth
-1, rowBottom
);
5075 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5077 dc
.SetPen( *wxWHITE_PEN
);
5078 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5079 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5081 dc
.SetBackgroundMode( wxTRANSPARENT
);
5082 dc
.SetTextForeground( GetLabelTextColour() );
5083 dc
.SetFont( GetLabelFont() );
5086 GetRowLabelAlignment( &hAlign
, &vAlign
);
5090 rect
.SetY( GetRowTop(row
) + 2 );
5091 rect
.SetWidth( m_rowLabelWidth
- 4 );
5092 rect
.SetHeight( GetRowHeight(row
) - 4 );
5093 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5097 void wxGrid::DrawColLabels( wxDC
& dc
)
5099 if ( !m_numRows
|| !m_numCols
) return;
5102 size_t numLabels
= m_colLabelsExposed
.GetCount();
5104 for ( i
= 0; i
< numLabels
; i
++ )
5106 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5111 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5113 if ( GetColWidth(col
) <= 0 )
5116 int colLeft
= GetColLeft(col
),
5117 colRight
= GetColRight(col
) - 1;
5119 dc
.SetPen( *wxBLACK_PEN
);
5120 dc
.DrawLine( colRight
, 0,
5121 colRight
, m_colLabelHeight
-1 );
5123 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5124 colRight
, m_colLabelHeight
-1 );
5126 dc
.SetPen( *wxWHITE_PEN
);
5127 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5128 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5130 dc
.SetBackgroundMode( wxTRANSPARENT
);
5131 dc
.SetTextForeground( GetLabelTextColour() );
5132 dc
.SetFont( GetLabelFont() );
5134 dc
.SetBackgroundMode( wxTRANSPARENT
);
5135 dc
.SetTextForeground( GetLabelTextColour() );
5136 dc
.SetFont( GetLabelFont() );
5139 GetColLabelAlignment( &hAlign
, &vAlign
);
5142 rect
.SetX( colLeft
+ 2 );
5144 rect
.SetWidth( GetColWidth(col
) - 4 );
5145 rect
.SetHeight( m_colLabelHeight
- 4 );
5146 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5150 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5151 const wxString
& value
,
5156 long textWidth
, textHeight
;
5157 long lineWidth
, lineHeight
;
5158 wxArrayString lines
;
5160 dc
.SetClippingRegion( rect
);
5161 StringToLines( value
, lines
);
5162 if ( lines
.GetCount() )
5164 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5165 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5168 switch ( horizAlign
)
5171 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5175 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5184 switch ( vertAlign
)
5187 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5191 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5200 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5202 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5207 dc
.DestroyClippingRegion();
5211 // Split multi line text up into an array of strings. Any existing
5212 // contents of the string array are preserved.
5214 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5218 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5219 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5221 while ( startPos
< (int)tVal
.Length() )
5223 pos
= tVal
.Mid(startPos
).Find( eol
);
5228 else if ( pos
== 0 )
5230 lines
.Add( wxEmptyString
);
5234 lines
.Add( value
.Mid(startPos
, pos
) );
5238 if ( startPos
< (int)value
.Length() )
5240 lines
.Add( value
.Mid( startPos
) );
5245 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5246 wxArrayString
& lines
,
5247 long *width
, long *height
)
5254 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5256 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5257 w
= wxMax( w
, lineW
);
5267 // ------ Edit control functions
5271 void wxGrid::EnableEditing( bool edit
)
5273 // TODO: improve this ?
5275 if ( edit
!= m_editable
)
5279 // FIXME IMHO this won't disable the edit control if edit == FALSE
5280 // because of the check in the beginning of
5281 // EnableCellEditControl() just below (VZ)
5282 EnableCellEditControl(m_editable
);
5287 void wxGrid::EnableCellEditControl( bool enable
)
5292 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5293 SetCurrentCell( 0, 0 );
5295 if ( enable
!= m_cellEditCtrlEnabled
)
5297 // TODO allow the app to Veto() this event?
5298 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5302 // this should be checked by the caller!
5303 wxASSERT_MSG( CanEnableCellControl(),
5304 _T("can't enable editing for this cell!") );
5306 // do it before ShowCellEditControl()
5307 m_cellEditCtrlEnabled
= enable
;
5309 ShowCellEditControl();
5313 HideCellEditControl();
5314 SaveEditControlValue();
5316 // do it after HideCellEditControl()
5317 m_cellEditCtrlEnabled
= enable
;
5322 bool wxGrid::IsCurrentCellReadOnly() const
5325 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5326 bool readonly
= attr
->IsReadOnly();
5332 bool wxGrid::CanEnableCellControl() const
5334 return m_editable
&& !IsCurrentCellReadOnly();
5337 bool wxGrid::IsCellEditControlEnabled() const
5339 // the cell edit control might be disable for all cells or just for the
5340 // current one if it's read only
5341 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5344 void wxGrid::ShowCellEditControl()
5346 if ( IsCellEditControlEnabled() )
5348 if ( !IsVisible( m_currentCellCoords
) )
5354 wxRect rect
= CellToRect( m_currentCellCoords
);
5355 int row
= m_currentCellCoords
.GetRow();
5356 int col
= m_currentCellCoords
.GetCol();
5358 // convert to scrolled coords
5360 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5362 // done in PaintBackground()
5364 // erase the highlight and the cell contents because the editor
5365 // might not cover the entire cell
5366 wxClientDC
dc( m_gridWin
);
5368 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5369 dc
.SetPen(*wxTRANSPARENT_PEN
);
5370 dc
.DrawRectangle(rect
);
5373 // cell is shifted by one pixel
5377 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5378 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5379 if ( !editor
->IsCreated() )
5381 editor
->Create(m_gridWin
, -1,
5382 new wxGridCellEditorEvtHandler(this, editor
));
5385 editor
->SetSize( rect
);
5387 editor
->Show( TRUE
, attr
);
5388 editor
->BeginEdit(row
, col
, this);
5395 void wxGrid::HideCellEditControl()
5397 if ( IsCellEditControlEnabled() )
5399 int row
= m_currentCellCoords
.GetRow();
5400 int col
= m_currentCellCoords
.GetCol();
5402 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5403 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5405 m_gridWin
->SetFocus();
5410 void wxGrid::SaveEditControlValue()
5412 if ( IsCellEditControlEnabled() )
5414 int row
= m_currentCellCoords
.GetRow();
5415 int col
= m_currentCellCoords
.GetCol();
5417 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5418 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5419 bool changed
= editor
->EndEdit(row
, col
, this);
5425 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5426 m_currentCellCoords
.GetRow(),
5427 m_currentCellCoords
.GetCol() );
5434 // ------ Grid location functions
5435 // Note that all of these functions work with the logical coordinates of
5436 // grid cells and labels so you will need to convert from device
5437 // coordinates for mouse events etc.
5440 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5442 int row
= YToRow(y
);
5443 int col
= XToCol(x
);
5445 if ( row
== -1 || col
== -1 )
5447 coords
= wxGridNoCellCoords
;
5451 coords
.Set( row
, col
);
5456 int wxGrid::YToRow( int y
)
5460 for ( i
= 0; i
< m_numRows
; i
++ )
5462 if ( y
< GetRowBottom(i
) )
5470 int wxGrid::XToCol( int x
)
5474 for ( i
= 0; i
< m_numCols
; i
++ )
5476 if ( x
< GetColRight(i
) )
5484 // return the row number that that the y coord is near the edge of, or
5485 // -1 if not near an edge
5487 int wxGrid::YToEdgeOfRow( int y
)
5491 for ( i
= 0; i
< m_numRows
; i
++ )
5493 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5495 d
= abs( y
- GetRowBottom(i
) );
5496 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5505 // return the col number that that the x coord is near the edge of, or
5506 // -1 if not near an edge
5508 int wxGrid::XToEdgeOfCol( int x
)
5512 for ( i
= 0; i
< m_numCols
; i
++ )
5514 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5516 d
= abs( x
- GetColRight(i
) );
5517 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5526 wxRect
wxGrid::CellToRect( int row
, int col
)
5528 wxRect
rect( -1, -1, -1, -1 );
5530 if ( row
>= 0 && row
< m_numRows
&&
5531 col
>= 0 && col
< m_numCols
)
5533 rect
.x
= GetColLeft(col
);
5534 rect
.y
= GetRowTop(row
);
5535 rect
.width
= GetColWidth(col
);
5536 rect
.height
= GetRowHeight(row
);
5543 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5545 // get the cell rectangle in logical coords
5547 wxRect
r( CellToRect( row
, col
) );
5549 // convert to device coords
5551 int left
, top
, right
, bottom
;
5552 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5553 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5555 // check against the client area of the grid window
5558 m_gridWin
->GetClientSize( &cw
, &ch
);
5560 if ( wholeCellVisible
)
5562 // is the cell wholly visible ?
5564 return ( left
>= 0 && right
<= cw
&&
5565 top
>= 0 && bottom
<= ch
);
5569 // is the cell partly visible ?
5571 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5572 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5577 // make the specified cell location visible by doing a minimal amount
5580 void wxGrid::MakeCellVisible( int row
, int col
)
5583 int xpos
= -1, ypos
= -1;
5585 if ( row
>= 0 && row
< m_numRows
&&
5586 col
>= 0 && col
< m_numCols
)
5588 // get the cell rectangle in logical coords
5590 wxRect
r( CellToRect( row
, col
) );
5592 // convert to device coords
5594 int left
, top
, right
, bottom
;
5595 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5596 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5599 m_gridWin
->GetClientSize( &cw
, &ch
);
5605 else if ( bottom
> ch
)
5607 int h
= r
.GetHeight();
5609 for ( i
= row
-1; i
>= 0; i
-- )
5611 int rowHeight
= GetRowHeight(i
);
5612 if ( h
+ rowHeight
> ch
)
5619 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5620 // have rounding errors (this is important, because if we do, we
5621 // might not scroll at all and some cells won't be redrawn)
5622 ypos
+= GRID_SCROLL_LINE
/ 2;
5629 else if ( right
> cw
)
5631 int w
= r
.GetWidth();
5633 for ( i
= col
-1; i
>= 0; i
-- )
5635 int colWidth
= GetColWidth(i
);
5636 if ( w
+ colWidth
> cw
)
5643 // see comment for ypos above
5644 xpos
+= GRID_SCROLL_LINE
/ 2;
5647 if ( xpos
!= -1 || ypos
!= -1 )
5649 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5650 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5651 Scroll( xpos
, ypos
);
5659 // ------ Grid cursor movement functions
5662 bool wxGrid::MoveCursorUp()
5664 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5665 m_currentCellCoords
.GetRow() > 0 )
5667 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5668 m_currentCellCoords
.GetCol() );
5670 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5671 m_currentCellCoords
.GetCol() );
5680 bool wxGrid::MoveCursorDown()
5682 // TODO: allow for scrolling
5684 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5685 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5687 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5688 m_currentCellCoords
.GetCol() );
5690 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5691 m_currentCellCoords
.GetCol() );
5700 bool wxGrid::MoveCursorLeft()
5702 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5703 m_currentCellCoords
.GetCol() > 0 )
5705 MakeCellVisible( m_currentCellCoords
.GetRow(),
5706 m_currentCellCoords
.GetCol() - 1 );
5708 SetCurrentCell( m_currentCellCoords
.GetRow(),
5709 m_currentCellCoords
.GetCol() - 1 );
5718 bool wxGrid::MoveCursorRight()
5720 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5721 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5723 MakeCellVisible( m_currentCellCoords
.GetRow(),
5724 m_currentCellCoords
.GetCol() + 1 );
5726 SetCurrentCell( m_currentCellCoords
.GetRow(),
5727 m_currentCellCoords
.GetCol() + 1 );
5736 bool wxGrid::MovePageUp()
5738 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5740 int row
= m_currentCellCoords
.GetRow();
5744 m_gridWin
->GetClientSize( &cw
, &ch
);
5746 int y
= GetRowTop(row
);
5747 int newRow
= YToRow( y
- ch
+ 1 );
5752 else if ( newRow
== row
)
5757 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5758 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5766 bool wxGrid::MovePageDown()
5768 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5770 int row
= m_currentCellCoords
.GetRow();
5771 if ( row
< m_numRows
)
5774 m_gridWin
->GetClientSize( &cw
, &ch
);
5776 int y
= GetRowTop(row
);
5777 int newRow
= YToRow( y
+ ch
);
5780 newRow
= m_numRows
- 1;
5782 else if ( newRow
== row
)
5787 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5788 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5796 bool wxGrid::MoveCursorUpBlock()
5799 m_currentCellCoords
!= wxGridNoCellCoords
&&
5800 m_currentCellCoords
.GetRow() > 0 )
5802 int row
= m_currentCellCoords
.GetRow();
5803 int col
= m_currentCellCoords
.GetCol();
5805 if ( m_table
->IsEmptyCell(row
, col
) )
5807 // starting in an empty cell: find the next block of
5813 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5816 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5818 // starting at the top of a block: find the next block
5824 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5829 // starting within a block: find the top of the block
5834 if ( m_table
->IsEmptyCell(row
, col
) )
5842 MakeCellVisible( row
, col
);
5843 SetCurrentCell( row
, col
);
5851 bool wxGrid::MoveCursorDownBlock()
5854 m_currentCellCoords
!= wxGridNoCellCoords
&&
5855 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5857 int row
= m_currentCellCoords
.GetRow();
5858 int col
= m_currentCellCoords
.GetCol();
5860 if ( m_table
->IsEmptyCell(row
, col
) )
5862 // starting in an empty cell: find the next block of
5865 while ( row
< m_numRows
-1 )
5868 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5871 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5873 // starting at the bottom of a block: find the next block
5876 while ( row
< m_numRows
-1 )
5879 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5884 // starting within a block: find the bottom of the block
5886 while ( row
< m_numRows
-1 )
5889 if ( m_table
->IsEmptyCell(row
, col
) )
5897 MakeCellVisible( row
, col
);
5898 SetCurrentCell( row
, col
);
5906 bool wxGrid::MoveCursorLeftBlock()
5909 m_currentCellCoords
!= wxGridNoCellCoords
&&
5910 m_currentCellCoords
.GetCol() > 0 )
5912 int row
= m_currentCellCoords
.GetRow();
5913 int col
= m_currentCellCoords
.GetCol();
5915 if ( m_table
->IsEmptyCell(row
, col
) )
5917 // starting in an empty cell: find the next block of
5923 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5926 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5928 // starting at the left of a block: find the next block
5934 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5939 // starting within a block: find the left of the block
5944 if ( m_table
->IsEmptyCell(row
, col
) )
5952 MakeCellVisible( row
, col
);
5953 SetCurrentCell( row
, col
);
5961 bool wxGrid::MoveCursorRightBlock()
5964 m_currentCellCoords
!= wxGridNoCellCoords
&&
5965 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5967 int row
= m_currentCellCoords
.GetRow();
5968 int col
= m_currentCellCoords
.GetCol();
5970 if ( m_table
->IsEmptyCell(row
, col
) )
5972 // starting in an empty cell: find the next block of
5975 while ( col
< m_numCols
-1 )
5978 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5981 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5983 // starting at the right of a block: find the next block
5986 while ( col
< m_numCols
-1 )
5989 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5994 // starting within a block: find the right of the block
5996 while ( col
< m_numCols
-1 )
5999 if ( m_table
->IsEmptyCell(row
, col
) )
6007 MakeCellVisible( row
, col
);
6008 SetCurrentCell( row
, col
);
6019 // ------ Label values and formatting
6022 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6024 *horiz
= m_rowLabelHorizAlign
;
6025 *vert
= m_rowLabelVertAlign
;
6028 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6030 *horiz
= m_colLabelHorizAlign
;
6031 *vert
= m_colLabelVertAlign
;
6034 wxString
wxGrid::GetRowLabelValue( int row
)
6038 return m_table
->GetRowLabelValue( row
);
6048 wxString
wxGrid::GetColLabelValue( int col
)
6052 return m_table
->GetColLabelValue( col
);
6063 void wxGrid::SetRowLabelSize( int width
)
6065 width
= wxMax( width
, 0 );
6066 if ( width
!= m_rowLabelWidth
)
6070 m_rowLabelWin
->Show( FALSE
);
6071 m_cornerLabelWin
->Show( FALSE
);
6073 else if ( m_rowLabelWidth
== 0 )
6075 m_rowLabelWin
->Show( TRUE
);
6076 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6079 m_rowLabelWidth
= width
;
6086 void wxGrid::SetColLabelSize( int height
)
6088 height
= wxMax( height
, 0 );
6089 if ( height
!= m_colLabelHeight
)
6093 m_colLabelWin
->Show( FALSE
);
6094 m_cornerLabelWin
->Show( FALSE
);
6096 else if ( m_colLabelHeight
== 0 )
6098 m_colLabelWin
->Show( TRUE
);
6099 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6102 m_colLabelHeight
= height
;
6109 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6111 if ( m_labelBackgroundColour
!= colour
)
6113 m_labelBackgroundColour
= colour
;
6114 m_rowLabelWin
->SetBackgroundColour( colour
);
6115 m_colLabelWin
->SetBackgroundColour( colour
);
6116 m_cornerLabelWin
->SetBackgroundColour( colour
);
6118 if ( !GetBatchCount() )
6120 m_rowLabelWin
->Refresh();
6121 m_colLabelWin
->Refresh();
6122 m_cornerLabelWin
->Refresh();
6127 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6129 if ( m_labelTextColour
!= colour
)
6131 m_labelTextColour
= colour
;
6132 if ( !GetBatchCount() )
6134 m_rowLabelWin
->Refresh();
6135 m_colLabelWin
->Refresh();
6140 void wxGrid::SetLabelFont( const wxFont
& font
)
6143 if ( !GetBatchCount() )
6145 m_rowLabelWin
->Refresh();
6146 m_colLabelWin
->Refresh();
6150 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6152 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6154 m_rowLabelHorizAlign
= horiz
;
6157 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6159 m_rowLabelVertAlign
= vert
;
6162 if ( !GetBatchCount() )
6164 m_rowLabelWin
->Refresh();
6168 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6170 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6172 m_colLabelHorizAlign
= horiz
;
6175 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6177 m_colLabelVertAlign
= vert
;
6180 if ( !GetBatchCount() )
6182 m_colLabelWin
->Refresh();
6186 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6190 m_table
->SetRowLabelValue( row
, s
);
6191 if ( !GetBatchCount() )
6193 wxRect rect
= CellToRect( row
, 0);
6194 if ( rect
.height
> 0 )
6196 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6198 rect
.width
= m_rowLabelWidth
;
6199 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6205 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6209 m_table
->SetColLabelValue( col
, s
);
6210 if ( !GetBatchCount() )
6212 wxRect rect
= CellToRect( 0, col
);
6213 if ( rect
.width
> 0 )
6215 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6217 rect
.height
= m_colLabelHeight
;
6218 m_colLabelWin
->Refresh( TRUE
, &rect
);
6224 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6226 if ( m_gridLineColour
!= colour
)
6228 m_gridLineColour
= colour
;
6230 wxClientDC
dc( m_gridWin
);
6232 DrawAllGridLines( dc
, wxRegion() );
6236 void wxGrid::EnableGridLines( bool enable
)
6238 if ( enable
!= m_gridLinesEnabled
)
6240 m_gridLinesEnabled
= enable
;
6242 if ( !GetBatchCount() )
6246 wxClientDC
dc( m_gridWin
);
6248 DrawAllGridLines( dc
, wxRegion() );
6252 m_gridWin
->Refresh();
6259 int wxGrid::GetDefaultRowSize()
6261 return m_defaultRowHeight
;
6264 int wxGrid::GetRowSize( int row
)
6266 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6268 return GetRowHeight(row
);
6271 int wxGrid::GetDefaultColSize()
6273 return m_defaultColWidth
;
6276 int wxGrid::GetColSize( int col
)
6278 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6280 return GetColWidth(col
);
6283 // ============================================================================
6284 // access to the grid attributes: each of them has a default value in the grid
6285 // itself and may be overidden on a per-cell basis
6286 // ============================================================================
6288 // ----------------------------------------------------------------------------
6289 // setting default attributes
6290 // ----------------------------------------------------------------------------
6292 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6294 m_defaultCellAttr
->SetBackgroundColour(col
);
6296 m_gridWin
->SetBackgroundColour(col
);
6300 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6302 m_defaultCellAttr
->SetTextColour(col
);
6305 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6307 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6310 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6312 m_defaultCellAttr
->SetFont(font
);
6315 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6317 m_defaultCellAttr
->SetRenderer(renderer
);
6320 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6322 m_defaultCellAttr
->SetEditor(editor
);
6325 // ----------------------------------------------------------------------------
6326 // access to the default attrbiutes
6327 // ----------------------------------------------------------------------------
6329 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6331 return m_defaultCellAttr
->GetBackgroundColour();
6334 wxColour
wxGrid::GetDefaultCellTextColour()
6336 return m_defaultCellAttr
->GetTextColour();
6339 wxFont
wxGrid::GetDefaultCellFont()
6341 return m_defaultCellAttr
->GetFont();
6344 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6346 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6349 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6351 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6354 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6356 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6359 // ----------------------------------------------------------------------------
6360 // access to cell attributes
6361 // ----------------------------------------------------------------------------
6363 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6365 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6366 wxColour colour
= attr
->GetBackgroundColour();
6371 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6373 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6374 wxColour colour
= attr
->GetTextColour();
6379 wxFont
wxGrid::GetCellFont( int row
, int col
)
6381 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6382 wxFont font
= attr
->GetFont();
6387 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6389 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6390 attr
->GetAlignment(horiz
, vert
);
6394 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6396 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6397 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6402 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6404 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6405 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6410 bool wxGrid::IsReadOnly(int row
, int col
) const
6412 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6413 bool isReadOnly
= attr
->IsReadOnly();
6418 // ----------------------------------------------------------------------------
6419 // attribute support: cache, automatic provider creation, ...
6420 // ----------------------------------------------------------------------------
6422 bool wxGrid::CanHaveAttributes()
6429 return m_table
->CanHaveAttributes();
6432 void wxGrid::ClearAttrCache()
6434 if ( m_attrCache
.row
!= -1 )
6436 m_attrCache
.attr
->SafeDecRef();
6437 m_attrCache
.row
= -1;
6441 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6443 wxGrid
*self
= (wxGrid
*)this; // const_cast
6445 self
->ClearAttrCache();
6446 self
->m_attrCache
.row
= row
;
6447 self
->m_attrCache
.col
= col
;
6448 self
->m_attrCache
.attr
= attr
;
6452 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6454 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6456 *attr
= m_attrCache
.attr
;
6457 (*attr
)->SafeIncRef();
6459 #ifdef DEBUG_ATTR_CACHE
6460 gs_nAttrCacheHits
++;
6467 #ifdef DEBUG_ATTR_CACHE
6468 gs_nAttrCacheMisses
++;
6474 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6476 wxGridCellAttr
*attr
;
6477 if ( !LookupAttr(row
, col
, &attr
) )
6479 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6480 CacheAttr(row
, col
, attr
);
6484 attr
->SetDefAttr(m_defaultCellAttr
);
6488 attr
= m_defaultCellAttr
;
6495 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6497 wxGridCellAttr
*attr
;
6498 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6500 wxASSERT_MSG( m_table
,
6501 _T("we may only be called if CanHaveAttributes() "
6502 "returned TRUE and then m_table should be !NULL") );
6504 attr
= m_table
->GetAttr(row
, col
);
6507 attr
= new wxGridCellAttr
;
6509 // artificially inc the ref count to match DecRef() in caller
6512 m_table
->SetAttr(attr
, row
, col
);
6515 CacheAttr(row
, col
, attr
);
6517 attr
->SetDefAttr(m_defaultCellAttr
);
6521 // ----------------------------------------------------------------------------
6522 // setting cell attributes: this is forwarded to the table
6523 // ----------------------------------------------------------------------------
6525 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6527 if ( CanHaveAttributes() )
6529 m_table
->SetRowAttr(attr
, row
);
6537 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6539 if ( CanHaveAttributes() )
6541 m_table
->SetColAttr(attr
, col
);
6549 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6551 if ( CanHaveAttributes() )
6553 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6554 attr
->SetBackgroundColour(colour
);
6559 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6561 if ( CanHaveAttributes() )
6563 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6564 attr
->SetTextColour(colour
);
6569 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6571 if ( CanHaveAttributes() )
6573 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6574 attr
->SetFont(font
);
6579 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6581 if ( CanHaveAttributes() )
6583 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6584 attr
->SetAlignment(horiz
, vert
);
6589 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6591 if ( CanHaveAttributes() )
6593 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6594 attr
->SetRenderer(renderer
);
6599 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6601 if ( CanHaveAttributes() )
6603 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6604 attr
->SetEditor(editor
);
6609 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6611 if ( CanHaveAttributes() )
6613 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6614 attr
->SetReadOnly(isReadOnly
);
6619 // ----------------------------------------------------------------------------
6620 // Data type registration
6621 // ----------------------------------------------------------------------------
6623 void wxGrid::RegisterDataType(const wxString
& typeName
,
6624 wxGridCellRenderer
* renderer
,
6625 wxGridCellEditor
* editor
)
6627 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6631 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6633 wxString typeName
= m_table
->GetTypeName(row
, col
);
6634 return GetDefaultEditorForType(typeName
);
6637 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6639 wxString typeName
= m_table
->GetTypeName(row
, col
);
6640 return GetDefaultRendererForType(typeName
);
6644 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6646 int index
= m_typeRegistry
->FindDataType(typeName
);
6648 // Should we force the failure here or let it fallback to string handling???
6649 // wxFAIL_MSG(wxT("Unknown data type name"));
6652 return m_typeRegistry
->GetEditor(index
);
6656 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6658 int index
= m_typeRegistry
->FindDataType(typeName
);
6660 // Should we force the failure here or let it fallback to string handling???
6661 // wxFAIL_MSG(wxT("Unknown data type name"));
6664 return m_typeRegistry
->GetRenderer(index
);
6668 // ----------------------------------------------------------------------------
6670 // ----------------------------------------------------------------------------
6672 void wxGrid::EnableDragRowSize( bool enable
)
6674 m_canDragRowSize
= enable
;
6678 void wxGrid::EnableDragColSize( bool enable
)
6680 m_canDragColSize
= enable
;
6683 void wxGrid::EnableDragGridSize( bool enable
)
6685 m_canDragGridSize
= enable
;
6689 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6691 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6693 if ( resizeExistingRows
)
6701 void wxGrid::SetRowSize( int row
, int height
)
6703 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6705 if ( m_rowHeights
.IsEmpty() )
6707 // need to really create the array
6711 int h
= wxMax( 0, height
);
6712 int diff
= h
- m_rowHeights
[row
];
6714 m_rowHeights
[row
] = h
;
6716 for ( i
= row
; i
< m_numRows
; i
++ )
6718 m_rowBottoms
[i
] += diff
;
6723 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6725 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6727 if ( resizeExistingCols
)
6735 void wxGrid::SetColSize( int col
, int width
)
6737 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6739 // should we check that it's bigger than GetColMinimalWidth(col) here?
6741 if ( m_colWidths
.IsEmpty() )
6743 // need to really create the array
6747 int w
= wxMax( 0, width
);
6748 int diff
= w
- m_colWidths
[col
];
6749 m_colWidths
[col
] = w
;
6752 for ( i
= col
; i
< m_numCols
; i
++ )
6754 m_colRights
[i
] += diff
;
6760 void wxGrid::SetColMinimalWidth( int col
, int width
)
6762 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6765 int wxGrid::GetColMinimalWidth(int col
) const
6767 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6768 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6771 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6773 wxClientDC
dc(m_gridWin
);
6775 wxCoord width
, widthMax
= 0;
6776 for ( int row
= 0; row
< m_numRows
; row
++ )
6778 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6779 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6782 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6783 if ( width
> widthMax
)
6792 // now also compare with the column label width
6793 dc
.SetFont( GetLabelFont() );
6794 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6795 if ( width
> widthMax
)
6802 // empty column - give default width (notice that if widthMax is less
6803 // than default width but != 0, it's ok)
6804 widthMax
= m_defaultColWidth
;
6808 // leave some space around text
6812 SetColSize(col
, widthMax
);
6815 SetColMinimalWidth(col
, widthMax
);
6819 void wxGrid::AutoSizeColumns( bool setAsMin
)
6821 for ( int col
= 0; col
< m_numCols
; col
++ )
6823 AutoSizeColumn(col
, setAsMin
);
6828 // ------ cell value accessor functions
6831 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6835 m_table
->SetValue( row
, col
, s
.c_str() );
6836 if ( !GetBatchCount() )
6838 wxClientDC
dc( m_gridWin
);
6840 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6843 if ( m_currentCellCoords
.GetRow() == row
&&
6844 m_currentCellCoords
.GetCol() == col
&&
6845 IsCellEditControlEnabled())
6847 HideCellEditControl();
6848 ShowCellEditControl(); // will reread data from table
6855 // ------ Block, row and col selection
6858 void wxGrid::SelectRow( int row
, bool addToSelected
)
6862 if ( IsSelection() && addToSelected
)
6865 bool need_refresh
[4];
6869 need_refresh
[3] = FALSE
;
6873 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6874 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6875 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6876 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6880 need_refresh
[0] = TRUE
;
6881 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6882 wxGridCellCoords ( oldTop
- 1,
6884 m_selectedTopLeft
.SetRow( row
);
6889 need_refresh
[1] = TRUE
;
6890 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6891 wxGridCellCoords ( oldBottom
,
6894 m_selectedTopLeft
.SetCol( 0 );
6897 if ( oldBottom
< row
)
6899 need_refresh
[2] = TRUE
;
6900 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6901 wxGridCellCoords ( row
,
6903 m_selectedBottomRight
.SetRow( row
);
6906 if ( oldRight
< m_numCols
- 1 )
6908 need_refresh
[3] = TRUE
;
6909 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6911 wxGridCellCoords ( oldBottom
,
6913 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6916 for (i
= 0; i
< 4; i
++ )
6917 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6918 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6922 r
= SelectionToDeviceRect();
6924 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6926 m_selectedTopLeft
.Set( row
, 0 );
6927 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6928 r
= SelectionToDeviceRect();
6929 m_gridWin
->Refresh( FALSE
, &r
);
6932 wxGridRangeSelectEvent
gridEvt( GetId(),
6933 wxEVT_GRID_RANGE_SELECT
,
6936 m_selectedBottomRight
);
6938 GetEventHandler()->ProcessEvent(gridEvt
);
6942 void wxGrid::SelectCol( int col
, bool addToSelected
)
6944 if ( IsSelection() && addToSelected
)
6947 bool need_refresh
[4];
6951 need_refresh
[3] = FALSE
;
6954 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6955 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6956 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6957 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6959 if ( oldLeft
> col
)
6961 need_refresh
[0] = TRUE
;
6962 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6963 wxGridCellCoords ( m_numRows
- 1,
6965 m_selectedTopLeft
.SetCol( col
);
6970 need_refresh
[1] = TRUE
;
6971 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6972 wxGridCellCoords ( oldTop
- 1,
6974 m_selectedTopLeft
.SetRow( 0 );
6977 if ( oldRight
< col
)
6979 need_refresh
[2] = TRUE
;
6980 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6981 wxGridCellCoords ( m_numRows
- 1,
6983 m_selectedBottomRight
.SetCol( col
);
6986 if ( oldBottom
< m_numRows
- 1 )
6988 need_refresh
[3] = TRUE
;
6989 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6991 wxGridCellCoords ( m_numRows
- 1,
6993 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6996 for (i
= 0; i
< 4; i
++ )
6997 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6998 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7004 r
= SelectionToDeviceRect();
7006 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7008 m_selectedTopLeft
.Set( 0, col
);
7009 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7010 r
= SelectionToDeviceRect();
7011 m_gridWin
->Refresh( FALSE
, &r
);
7014 wxGridRangeSelectEvent
gridEvt( GetId(),
7015 wxEVT_GRID_RANGE_SELECT
,
7018 m_selectedBottomRight
);
7020 GetEventHandler()->ProcessEvent(gridEvt
);
7024 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7027 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7029 if ( topRow
> bottomRow
)
7036 if ( leftCol
> rightCol
)
7043 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7044 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7046 if ( m_selectedTopLeft
!= updateTopLeft
||
7047 m_selectedBottomRight
!= updateBottomRight
)
7049 // Compute two optimal update rectangles:
7050 // Either one rectangle is a real subset of the
7051 // other, or they are (almost) disjoint!
7053 bool need_refresh
[4];
7057 need_refresh
[3] = FALSE
;
7060 // Store intermediate values
7061 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7062 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7063 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7064 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7066 // Determine the outer/inner coordinates.
7067 if (oldLeft
> leftCol
)
7073 if (oldTop
> topRow
)
7079 if (oldRight
< rightCol
)
7082 oldRight
= rightCol
;
7085 if (oldBottom
< bottomRow
)
7088 oldBottom
= bottomRow
;
7092 // Now, either the stuff marked old is the outer
7093 // rectangle or we don't have a situation where one
7094 // is contained in the other.
7096 if ( oldLeft
< leftCol
)
7098 need_refresh
[0] = TRUE
;
7099 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7101 wxGridCellCoords ( oldBottom
,
7105 if ( oldTop
< topRow
)
7107 need_refresh
[1] = TRUE
;
7108 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7110 wxGridCellCoords ( topRow
- 1,
7114 if ( oldRight
> rightCol
)
7116 need_refresh
[2] = TRUE
;
7117 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7119 wxGridCellCoords ( oldBottom
,
7123 if ( oldBottom
> bottomRow
)
7125 need_refresh
[3] = TRUE
;
7126 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7128 wxGridCellCoords ( oldBottom
,
7134 m_selectedTopLeft
= updateTopLeft
;
7135 m_selectedBottomRight
= updateBottomRight
;
7137 // various Refresh() calls
7138 for (i
= 0; i
< 4; i
++ )
7139 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7140 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7143 // only generate an event if the block is not being selected by
7144 // dragging the mouse (in which case the event will be generated in
7145 // the mouse event handler)
7146 if ( !m_isDragging
)
7148 wxGridRangeSelectEvent
gridEvt( GetId(),
7149 wxEVT_GRID_RANGE_SELECT
,
7152 m_selectedBottomRight
);
7154 GetEventHandler()->ProcessEvent(gridEvt
);
7158 void wxGrid::SelectAll()
7160 m_selectedTopLeft
.Set( 0, 0 );
7161 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7163 m_gridWin
->Refresh();
7167 void wxGrid::ClearSelection()
7169 m_selectedTopLeft
= wxGridNoCellCoords
;
7170 m_selectedBottomRight
= wxGridNoCellCoords
;
7174 // This function returns the rectangle that encloses the given block
7175 // in device coords clipped to the client size of the grid window.
7177 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7178 const wxGridCellCoords
&bottomRight
)
7180 wxRect
rect( wxGridNoCellRect
);
7183 cellRect
= CellToRect( topLeft
);
7184 if ( cellRect
!= wxGridNoCellRect
)
7190 rect
= wxRect( 0, 0, 0, 0 );
7193 cellRect
= CellToRect( bottomRight
);
7194 if ( cellRect
!= wxGridNoCellRect
)
7200 return wxGridNoCellRect
;
7203 // convert to scrolled coords
7205 int left
, top
, right
, bottom
;
7206 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7207 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7210 m_gridWin
->GetClientSize( &cw
, &ch
);
7212 rect
.SetLeft( wxMax(0, left
) );
7213 rect
.SetTop( wxMax(0, top
) );
7214 rect
.SetRight( wxMin(cw
, right
) );
7215 rect
.SetBottom( wxMin(ch
, bottom
) );
7223 // ------ Grid event classes
7226 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7228 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7229 int row
, int col
, int x
, int y
,
7230 bool control
, bool shift
, bool alt
, bool meta
)
7231 : wxNotifyEvent( type
, id
)
7237 m_control
= control
;
7242 SetEventObject(obj
);
7246 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7248 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7249 int rowOrCol
, int x
, int y
,
7250 bool control
, bool shift
, bool alt
, bool meta
)
7251 : wxNotifyEvent( type
, id
)
7253 m_rowOrCol
= rowOrCol
;
7256 m_control
= control
;
7261 SetEventObject(obj
);
7265 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7267 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7268 const wxGridCellCoords
& topLeft
,
7269 const wxGridCellCoords
& bottomRight
,
7270 bool control
, bool shift
, bool alt
, bool meta
)
7271 : wxNotifyEvent( type
, id
)
7273 m_topLeft
= topLeft
;
7274 m_bottomRight
= bottomRight
;
7275 m_control
= control
;
7280 SetEventObject(obj
);
7284 #endif // ifndef wxUSE_NEW_GRID