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
);
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
, bool saveValue
,
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
, bool saveValue
,
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
);
703 // ----------------------------------------------------------------------------
704 // wxGridCellFloatEditor
705 // ----------------------------------------------------------------------------
707 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
709 wxEvtHandler
* evtHandler
)
711 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
714 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
715 #endif // wxUSE_VALIDATORS
718 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
720 // first get the value
721 wxGridTableBase
*table
= grid
->GetTable();
722 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
724 m_valueOld
= table
->GetValueAsDouble(row
, col
);
728 wxFAIL_MSG( _T("this cell doesn't have float value") );
733 DoBeginEdit(GetString());
736 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, bool saveValue
,
740 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
742 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
752 void wxGridCellFloatEditor::Reset()
754 DoReset(GetString());
757 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
759 long keycode
= event
.KeyCode();
760 if ( isdigit(keycode
) ||
761 keycode
== '+' || keycode
== '-' || keycode
== '.' )
763 wxGridCellTextEditor::StartingKey(event
);
772 // ----------------------------------------------------------------------------
773 // wxGridCellBoolEditor
774 // ----------------------------------------------------------------------------
776 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
778 wxEvtHandler
* evtHandler
)
780 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
781 wxDefaultPosition
, wxDefaultSize
,
784 wxGridCellEditor::Create(parent
, id
, evtHandler
);
787 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
789 // position it in the centre of the rectangle (TODO: support alignment?)
791 m_control
->GetSize(&w
, &h
);
793 // the checkbox without label still has some space to the right in wxGTK,
794 // so shift it to the right
799 m_control
->Move(r
.x
+ r
.width
/2 - w
/2, r
.y
+ r
.height
/2 - h
/2);
802 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
804 m_control
->Show(show
);
808 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
809 CBox()->SetBackgroundColour(colBg
);
813 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
815 wxASSERT_MSG(m_control
,
816 wxT("The wxGridCellEditor must be Created first!"));
818 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxT("bool")))
819 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
821 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
822 CBox()->SetValue(m_startValue
);
826 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
, wxT("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 Combo()->SetInsertionPointEnd();
918 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
922 wxString value
= Combo()->GetValue();
923 bool changed
= value
!= m_startValue
;
926 grid
->GetTable()->SetValue(row
, col
, value
);
928 m_startValue
= wxEmptyString
;
929 Combo()->SetValue(m_startValue
);
934 void wxGridCellChoiceEditor::Reset()
936 Combo()->SetValue(m_startValue
);
937 Combo()->SetInsertionPointEnd();
940 // ----------------------------------------------------------------------------
941 // wxGridCellEditorEvtHandler
942 // ----------------------------------------------------------------------------
944 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
946 switch ( event
.KeyCode() )
950 m_grid
->DisableCellEditControl();
954 event
.Skip( m_grid
->ProcessEvent( event
) );
958 if (!m_grid
->ProcessEvent(event
))
959 m_editor
->HandleReturn(event
);
968 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
970 switch ( event
.KeyCode() )
982 // ============================================================================
984 // ============================================================================
986 // ----------------------------------------------------------------------------
987 // wxGridCellRenderer
988 // ----------------------------------------------------------------------------
990 void wxGridCellRenderer::Draw(wxGrid
& grid
,
991 wxGridCellAttr
& attr
,
997 dc
.SetBackgroundMode( wxSOLID
);
1001 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1005 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1008 dc
.SetPen( *wxTRANSPARENT_PEN
);
1009 dc
.DrawRectangle(rect
);
1012 wxGridCellRenderer::~wxGridCellRenderer()
1016 // ----------------------------------------------------------------------------
1017 // wxGridCellStringRenderer
1018 // ----------------------------------------------------------------------------
1020 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1021 wxGridCellAttr
& attr
,
1025 dc
.SetBackgroundMode( wxTRANSPARENT
);
1027 // TODO some special colours for attr.IsReadOnly() case?
1031 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1032 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1036 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1037 dc
.SetTextForeground( attr
.GetTextColour() );
1040 dc
.SetFont( attr
.GetFont() );
1043 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1045 const wxString
& text
)
1048 dc
.SetFont(attr
.GetFont());
1049 dc
.GetTextExtent(text
, &x
, &y
);
1051 return wxSize(x
, y
);
1054 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1055 wxGridCellAttr
& attr
,
1059 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1062 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1063 wxGridCellAttr
& attr
,
1065 const wxRect
& rectCell
,
1069 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1071 // now we only have to draw the text
1072 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1075 attr
.GetAlignment(&hAlign
, &vAlign
);
1077 wxRect rect
= rectCell
;
1080 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1081 rect
, hAlign
, vAlign
);
1084 // ----------------------------------------------------------------------------
1085 // wxGridCellNumberRenderer
1086 // ----------------------------------------------------------------------------
1088 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1090 wxGridTableBase
*table
= grid
.GetTable();
1092 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1094 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1096 //else: leave the string empty or put 0 into it?
1101 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1102 wxGridCellAttr
& attr
,
1104 const wxRect
& rectCell
,
1108 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1110 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1112 // draw the text right aligned by default
1114 attr
.GetAlignment(&hAlign
, &vAlign
);
1117 wxRect rect
= rectCell
;
1120 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1123 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1124 wxGridCellAttr
& attr
,
1128 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1131 // ----------------------------------------------------------------------------
1132 // wxGridCellFloatRenderer
1133 // ----------------------------------------------------------------------------
1135 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1138 SetPrecision(precision
);
1141 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1143 wxGridTableBase
*table
= grid
.GetTable();
1145 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1149 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1152 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1154 //else: leave the string empty or put 0 into it?
1159 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1160 wxGridCellAttr
& attr
,
1162 const wxRect
& rectCell
,
1166 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1168 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1170 // draw the text right aligned by default
1172 attr
.GetAlignment(&hAlign
, &vAlign
);
1175 wxRect rect
= rectCell
;
1178 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1181 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1182 wxGridCellAttr
& attr
,
1186 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1189 // ----------------------------------------------------------------------------
1190 // wxGridCellBoolRenderer
1191 // ----------------------------------------------------------------------------
1193 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1195 // between checkmark and box
1196 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1198 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1199 wxGridCellAttr
& WXUNUSED(attr
),
1204 // compute it only once (no locks for MT safeness in GUI thread...)
1205 if ( !ms_sizeCheckMark
.x
)
1207 // get checkbox size
1208 wxCoord checkSize
= 0;
1209 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1210 wxSize size
= checkbox
->GetBestSize();
1211 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1213 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1215 checkSize
-= size
.y
/ 2;
1220 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1223 return ms_sizeCheckMark
;
1226 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1227 wxGridCellAttr
& attr
,
1233 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1235 // draw a check mark in the centre (ignoring alignment - TODO)
1236 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1238 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1239 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1240 rectMark
.width
= size
.x
;
1241 rectMark
.height
= size
.y
;
1243 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1244 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1245 dc
.DrawRectangle(rectMark
);
1247 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1250 if (grid
.GetTable()->CanGetValueAs(row
, col
, wxT("bool")))
1251 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1253 value
= !!grid
.GetTable()->GetValue(row
, col
);
1257 dc
.SetTextForeground(attr
.GetTextColour());
1258 dc
.DrawCheckMark(rectMark
);
1262 // ----------------------------------------------------------------------------
1264 // ----------------------------------------------------------------------------
1266 const wxColour
& wxGridCellAttr::GetTextColour() const
1268 if (HasTextColour())
1272 else if (m_defGridAttr
!= this)
1274 return m_defGridAttr
->GetTextColour();
1278 wxFAIL_MSG(wxT("Missing default cell attribute"));
1279 return wxNullColour
;
1284 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1286 if (HasBackgroundColour())
1288 else if (m_defGridAttr
!= this)
1289 return m_defGridAttr
->GetBackgroundColour();
1292 wxFAIL_MSG(wxT("Missing default cell attribute"));
1293 return wxNullColour
;
1298 const wxFont
& wxGridCellAttr::GetFont() const
1302 else if (m_defGridAttr
!= this)
1303 return m_defGridAttr
->GetFont();
1306 wxFAIL_MSG(wxT("Missing default cell attribute"));
1312 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1316 if ( hAlign
) *hAlign
= m_hAlign
;
1317 if ( vAlign
) *vAlign
= m_vAlign
;
1319 else if (m_defGridAttr
!= this)
1320 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1323 wxFAIL_MSG(wxT("Missing default cell attribute"));
1328 // GetRenderer and GetEditor use a slightly different decision path about
1329 // which to use. If a non-default attr object has one then it is used,
1330 // otherwise the default editor or renderer passed in is used. It should be
1331 // the default for the data type of the cell. If it is NULL (because the
1332 // table has a type that the grid does not have in its registry,) then the
1333 // grid's default editor or renderer is used.
1335 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGridCellRenderer
* def
) const
1337 if ((m_defGridAttr
!= this || def
== NULL
) && HasRenderer())
1341 else if (m_defGridAttr
!= this)
1342 return m_defGridAttr
->GetRenderer(NULL
);
1345 wxFAIL_MSG(wxT("Missing default cell attribute"));
1350 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGridCellEditor
* def
) const
1352 if ((m_defGridAttr
!= this || def
== NULL
) && HasEditor())
1356 else if (m_defGridAttr
!= this)
1357 return m_defGridAttr
->GetEditor(NULL
);
1360 wxFAIL_MSG(wxT("Missing default cell attribute"));
1365 // ----------------------------------------------------------------------------
1366 // wxGridCellAttrData
1367 // ----------------------------------------------------------------------------
1369 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1371 int n
= FindIndex(row
, col
);
1372 if ( n
== wxNOT_FOUND
)
1374 // add the attribute
1375 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1381 // change the attribute
1382 m_attrs
[(size_t)n
].attr
= attr
;
1386 // remove this attribute
1387 m_attrs
.RemoveAt((size_t)n
);
1392 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1394 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1396 int n
= FindIndex(row
, col
);
1397 if ( n
!= wxNOT_FOUND
)
1399 attr
= m_attrs
[(size_t)n
].attr
;
1406 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1408 size_t count
= m_attrs
.GetCount();
1409 for ( size_t n
= 0; n
< count
; n
++ )
1411 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1412 wxCoord row
= coords
.GetRow();
1413 if ((size_t)row
>= pos
)
1417 // If rows inserted, include row counter where necessary
1418 coords
.SetRow(row
+ numRows
);
1420 else if (numRows
< 0)
1422 // If rows deleted ...
1423 if ((size_t)row
>= pos
- numRows
)
1425 // ...either decrement row counter (if row still exists)...
1426 coords
.SetRow(row
+ numRows
);
1430 // ...or remove the attribute
1431 m_attrs
.RemoveAt((size_t)n
);
1439 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1441 size_t count
= m_attrs
.GetCount();
1442 for ( size_t n
= 0; n
< count
; n
++ )
1444 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1445 wxCoord col
= coords
.GetCol();
1446 if ( (size_t)col
>= pos
)
1450 // If rows inserted, include row counter where necessary
1451 coords
.SetCol(col
+ numCols
);
1453 else if (numCols
< 0)
1455 // If rows deleted ...
1456 if ((size_t)col
>= pos
- numCols
)
1458 // ...either decrement row counter (if row still exists)...
1459 coords
.SetCol(col
+ numCols
);
1463 // ...or remove the attribute
1464 m_attrs
.RemoveAt((size_t)n
);
1472 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1474 size_t count
= m_attrs
.GetCount();
1475 for ( size_t n
= 0; n
< count
; n
++ )
1477 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1478 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1487 // ----------------------------------------------------------------------------
1488 // wxGridRowOrColAttrData
1489 // ----------------------------------------------------------------------------
1491 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1493 size_t count
= m_attrs
.Count();
1494 for ( size_t n
= 0; n
< count
; n
++ )
1496 m_attrs
[n
]->DecRef();
1500 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1502 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1504 int n
= m_rowsOrCols
.Index(rowOrCol
);
1505 if ( n
!= wxNOT_FOUND
)
1507 attr
= m_attrs
[(size_t)n
];
1514 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1516 int n
= m_rowsOrCols
.Index(rowOrCol
);
1517 if ( n
== wxNOT_FOUND
)
1519 // add the attribute
1520 m_rowsOrCols
.Add(rowOrCol
);
1527 // change the attribute
1528 m_attrs
[(size_t)n
] = attr
;
1532 // remove this attribute
1533 m_attrs
[(size_t)n
]->DecRef();
1534 m_rowsOrCols
.RemoveAt((size_t)n
);
1535 m_attrs
.RemoveAt((size_t)n
);
1540 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1542 size_t count
= m_attrs
.GetCount();
1543 for ( size_t n
= 0; n
< count
; n
++ )
1545 int & rowOrCol
= m_rowsOrCols
[n
];
1546 if ( (size_t)rowOrCol
>= pos
)
1548 if ( numRowsOrCols
> 0 )
1550 // If rows inserted, include row counter where necessary
1551 rowOrCol
+= numRowsOrCols
;
1553 else if ( numRowsOrCols
< 0)
1555 // If rows deleted, either decrement row counter (if row still exists)
1556 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1557 rowOrCol
+= numRowsOrCols
;
1560 m_rowsOrCols
.RemoveAt((size_t)n
);
1561 m_attrs
.RemoveAt((size_t)n
);
1569 // ----------------------------------------------------------------------------
1570 // wxGridCellAttrProvider
1571 // ----------------------------------------------------------------------------
1573 wxGridCellAttrProvider::wxGridCellAttrProvider()
1575 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1578 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1583 void wxGridCellAttrProvider::InitData()
1585 m_data
= new wxGridCellAttrProviderData
;
1588 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1590 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1593 // first look for the attribute of this specific cell
1594 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1598 // then look for the col attr (col attributes are more common than
1599 // the row ones, hence they have priority)
1600 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1605 // finally try the row attributes
1606 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1613 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1619 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1622 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1627 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1630 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1635 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1638 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1642 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1644 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1648 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1652 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1654 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1658 // ----------------------------------------------------------------------------
1659 // wxGridTypeRegistry
1660 // ----------------------------------------------------------------------------
1662 wxGridTypeRegistry::~wxGridTypeRegistry()
1664 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++)
1665 delete m_typeinfo
[i
];
1669 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1670 wxGridCellRenderer
* renderer
,
1671 wxGridCellEditor
* editor
)
1674 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1676 // is it already registered?
1677 if ((loc
= FindDataType(typeName
)) != -1) {
1678 delete m_typeinfo
[loc
];
1679 m_typeinfo
[loc
] = info
;
1682 m_typeinfo
.Add(info
);
1686 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1690 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1691 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1700 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1702 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1706 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1708 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1712 // ----------------------------------------------------------------------------
1714 // ----------------------------------------------------------------------------
1716 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1719 wxGridTableBase::wxGridTableBase()
1721 m_view
= (wxGrid
*) NULL
;
1722 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1725 wxGridTableBase::~wxGridTableBase()
1727 delete m_attrProvider
;
1730 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1732 delete m_attrProvider
;
1733 m_attrProvider
= attrProvider
;
1736 bool wxGridTableBase::CanHaveAttributes()
1738 if ( ! GetAttrProvider() )
1740 // use the default attr provider by default
1741 SetAttrProvider(new wxGridCellAttrProvider
);
1746 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1748 if ( m_attrProvider
)
1749 return m_attrProvider
->GetAttr(row
, col
);
1751 return (wxGridCellAttr
*)NULL
;
1754 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1756 if ( m_attrProvider
)
1758 m_attrProvider
->SetAttr(attr
, row
, col
);
1762 // as we take ownership of the pointer and don't store it, we must
1768 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1770 if ( m_attrProvider
)
1772 m_attrProvider
->SetRowAttr(attr
, row
);
1776 // as we take ownership of the pointer and don't store it, we must
1782 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1784 if ( m_attrProvider
)
1786 m_attrProvider
->SetColAttr(attr
, col
);
1790 // as we take ownership of the pointer and don't store it, we must
1796 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1798 if ( m_attrProvider
)
1800 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1804 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1806 if ( m_attrProvider
)
1808 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1812 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1814 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1815 "but your derived table class does not override this function") );
1820 bool wxGridTableBase::AppendRows( size_t numRows
)
1822 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1823 "but your derived table class does not override this function"));
1828 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1830 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1831 "but your derived table class does not override this function"));
1836 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1838 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1839 "but your derived table class does not override this function"));
1844 bool wxGridTableBase::AppendCols( size_t numCols
)
1846 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1847 "but your derived table class does not override this function"));
1852 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1854 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1855 "but your derived table class does not override this function"));
1861 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1864 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1865 // how much it makes sense to us geeks.
1869 wxString
wxGridTableBase::GetColLabelValue( int col
)
1871 // default col labels are:
1872 // cols 0 to 25 : A-Z
1873 // cols 26 to 675 : AA-ZZ
1878 for ( n
= 1; ; n
++ )
1880 s
+= (_T('A') + (wxChar
)( col%26
));
1882 if ( col
< 0 ) break;
1885 // reverse the string...
1887 for ( i
= 0; i
< n
; i
++ )
1896 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1898 return wxGRID_VALUE_STRING
;
1901 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1902 const wxString
& typeName
)
1904 return typeName
== wxGRID_VALUE_STRING
;
1907 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1909 return CanGetValueAs(row
, col
, typeName
);
1912 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1917 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1922 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1927 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1928 long WXUNUSED(value
) )
1932 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1933 double WXUNUSED(value
) )
1937 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1938 bool WXUNUSED(value
) )
1943 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1944 const wxString
& WXUNUSED(typeName
) )
1949 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1950 const wxString
& WXUNUSED(typeName
),
1951 void* WXUNUSED(value
) )
1956 //////////////////////////////////////////////////////////////////////
1958 // Message class for the grid table to send requests and notifications
1962 wxGridTableMessage::wxGridTableMessage()
1964 m_table
= (wxGridTableBase
*) NULL
;
1970 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1971 int commandInt1
, int commandInt2
)
1975 m_comInt1
= commandInt1
;
1976 m_comInt2
= commandInt2
;
1981 //////////////////////////////////////////////////////////////////////
1983 // A basic grid table for string data. An object of this class will
1984 // created by wxGrid if you don't specify an alternative table class.
1987 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1989 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1991 wxGridStringTable::wxGridStringTable()
1996 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2001 m_data
.Alloc( numRows
);
2004 sa
.Alloc( numCols
);
2005 for ( col
= 0; col
< numCols
; col
++ )
2007 sa
.Add( wxEmptyString
);
2010 for ( row
= 0; row
< numRows
; row
++ )
2016 wxGridStringTable::~wxGridStringTable()
2020 long wxGridStringTable::GetNumberRows()
2022 return m_data
.GetCount();
2025 long wxGridStringTable::GetNumberCols()
2027 if ( m_data
.GetCount() > 0 )
2028 return m_data
[0].GetCount();
2033 wxString
wxGridStringTable::GetValue( int row
, int col
)
2035 // TODO: bounds checking
2037 return m_data
[row
][col
];
2040 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2042 // TODO: bounds checking
2044 m_data
[row
][col
] = value
;
2047 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2049 // TODO: bounds checking
2051 return (m_data
[row
][col
] == wxEmptyString
);
2055 void wxGridStringTable::Clear()
2058 int numRows
, numCols
;
2060 numRows
= m_data
.GetCount();
2063 numCols
= m_data
[0].GetCount();
2065 for ( row
= 0; row
< numRows
; row
++ )
2067 for ( col
= 0; col
< numCols
; col
++ )
2069 m_data
[row
][col
] = wxEmptyString
;
2076 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2080 size_t curNumRows
= m_data
.GetCount();
2081 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2083 if ( pos
>= curNumRows
)
2085 return AppendRows( numRows
);
2089 sa
.Alloc( curNumCols
);
2090 for ( col
= 0; col
< curNumCols
; col
++ )
2092 sa
.Add( wxEmptyString
);
2095 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2097 m_data
.Insert( sa
, row
);
2099 UpdateAttrRows( pos
, numRows
);
2102 wxGridTableMessage
msg( this,
2103 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2107 GetView()->ProcessTableMessage( msg
);
2113 bool wxGridStringTable::AppendRows( size_t numRows
)
2117 size_t curNumRows
= m_data
.GetCount();
2118 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2121 if ( curNumCols
> 0 )
2123 sa
.Alloc( curNumCols
);
2124 for ( col
= 0; col
< curNumCols
; col
++ )
2126 sa
.Add( wxEmptyString
);
2130 for ( row
= 0; row
< numRows
; row
++ )
2137 wxGridTableMessage
msg( this,
2138 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2141 GetView()->ProcessTableMessage( msg
);
2147 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2151 size_t curNumRows
= m_data
.GetCount();
2153 if ( pos
>= curNumRows
)
2156 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2157 "Pos value is invalid for present table with %d rows",
2158 pos
, numRows
, curNumRows
);
2159 wxFAIL_MSG( wxT(errmsg
) );
2163 if ( numRows
> curNumRows
- pos
)
2165 numRows
= curNumRows
- pos
;
2168 if ( numRows
>= curNumRows
)
2170 m_data
.Empty(); // don't release memory just yet
2174 for ( n
= 0; n
< numRows
; n
++ )
2176 m_data
.Remove( pos
);
2179 UpdateAttrRows( pos
, -((int)numRows
) );
2182 wxGridTableMessage
msg( this,
2183 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2187 GetView()->ProcessTableMessage( msg
);
2193 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2197 size_t curNumRows
= m_data
.GetCount();
2198 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2200 if ( pos
>= curNumCols
)
2202 return AppendCols( numCols
);
2205 for ( row
= 0; row
< curNumRows
; row
++ )
2207 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2209 m_data
[row
].Insert( wxEmptyString
, col
);
2212 UpdateAttrCols( pos
, numCols
);
2215 wxGridTableMessage
msg( this,
2216 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2220 GetView()->ProcessTableMessage( msg
);
2226 bool wxGridStringTable::AppendCols( size_t numCols
)
2230 size_t curNumRows
= m_data
.GetCount();
2233 // TODO: something better than this ?
2235 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2236 "Call AppendRows() first") );
2240 for ( row
= 0; row
< curNumRows
; row
++ )
2242 for ( n
= 0; n
< numCols
; n
++ )
2244 m_data
[row
].Add( wxEmptyString
);
2250 wxGridTableMessage
msg( this,
2251 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2254 GetView()->ProcessTableMessage( msg
);
2260 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2264 size_t curNumRows
= m_data
.GetCount();
2265 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2267 if ( pos
>= curNumCols
)
2270 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2271 "Pos value is invalid for present table with %d cols",
2272 pos
, numCols
, curNumCols
);
2273 wxFAIL_MSG( wxT( errmsg
) );
2277 if ( numCols
> curNumCols
- pos
)
2279 numCols
= curNumCols
- pos
;
2282 for ( row
= 0; row
< curNumRows
; row
++ )
2284 if ( numCols
>= curNumCols
)
2286 m_data
[row
].Clear();
2290 for ( n
= 0; n
< numCols
; n
++ )
2292 m_data
[row
].Remove( pos
);
2296 UpdateAttrCols( pos
, -((int)numCols
) );
2299 wxGridTableMessage
msg( this,
2300 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2304 GetView()->ProcessTableMessage( msg
);
2310 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2312 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2314 // using default label
2316 return wxGridTableBase::GetRowLabelValue( row
);
2320 return m_rowLabels
[ row
];
2324 wxString
wxGridStringTable::GetColLabelValue( int col
)
2326 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2328 // using default label
2330 return wxGridTableBase::GetColLabelValue( col
);
2334 return m_colLabels
[ col
];
2338 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2340 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2342 int n
= m_rowLabels
.GetCount();
2344 for ( i
= n
; i
<= row
; i
++ )
2346 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2350 m_rowLabels
[row
] = value
;
2353 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2355 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2357 int n
= m_colLabels
.GetCount();
2359 for ( i
= n
; i
<= col
; i
++ )
2361 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2365 m_colLabels
[col
] = value
;
2370 //////////////////////////////////////////////////////////////////////
2371 //////////////////////////////////////////////////////////////////////
2373 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2375 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2376 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2377 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2378 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2381 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2383 const wxPoint
&pos
, const wxSize
&size
)
2384 : wxWindow( parent
, id
, pos
, size
)
2389 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2393 // NO - don't do this because it will set both the x and y origin
2394 // coords to match the parent scrolled window and we just want to
2395 // set the y coord - MB
2397 // m_owner->PrepareDC( dc );
2400 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2401 dc
.SetDeviceOrigin( 0, -y
);
2403 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2404 m_owner
->DrawRowLabels( dc
);
2408 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2410 m_owner
->ProcessRowLabelMouseEvent( event
);
2414 // This seems to be required for wxMotif otherwise the mouse
2415 // cursor must be in the cell edit control to get key events
2417 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2419 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2424 //////////////////////////////////////////////////////////////////////
2426 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2428 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2429 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2430 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2431 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2434 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2436 const wxPoint
&pos
, const wxSize
&size
)
2437 : wxWindow( parent
, id
, pos
, size
)
2442 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2446 // NO - don't do this because it will set both the x and y origin
2447 // coords to match the parent scrolled window and we just want to
2448 // set the x coord - MB
2450 // m_owner->PrepareDC( dc );
2453 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2454 dc
.SetDeviceOrigin( -x
, 0 );
2456 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2457 m_owner
->DrawColLabels( dc
);
2461 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2463 m_owner
->ProcessColLabelMouseEvent( event
);
2467 // This seems to be required for wxMotif otherwise the mouse
2468 // cursor must be in the cell edit control to get key events
2470 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2472 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2477 //////////////////////////////////////////////////////////////////////
2479 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2481 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2482 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2483 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2484 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2487 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2489 const wxPoint
&pos
, const wxSize
&size
)
2490 : wxWindow( parent
, id
, pos
, size
)
2495 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2499 int client_height
= 0;
2500 int client_width
= 0;
2501 GetClientSize( &client_width
, &client_height
);
2503 dc
.SetPen( *wxBLACK_PEN
);
2504 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2505 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2507 dc
.SetPen( *wxWHITE_PEN
);
2508 dc
.DrawLine( 0, 0, client_width
, 0 );
2509 dc
.DrawLine( 0, 0, 0, client_height
);
2513 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2515 m_owner
->ProcessCornerLabelMouseEvent( event
);
2519 // This seems to be required for wxMotif otherwise the mouse
2520 // cursor must be in the cell edit control to get key events
2522 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2524 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2529 //////////////////////////////////////////////////////////////////////
2531 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2533 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2534 EVT_PAINT( wxGridWindow::OnPaint
)
2535 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2536 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2537 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2540 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2541 wxGridRowLabelWindow
*rowLblWin
,
2542 wxGridColLabelWindow
*colLblWin
,
2543 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2544 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
2547 m_rowLabelWin
= rowLblWin
;
2548 m_colLabelWin
= colLblWin
;
2549 SetBackgroundColour( "WHITE" );
2553 wxGridWindow::~wxGridWindow()
2558 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2560 wxPaintDC
dc( this );
2561 m_owner
->PrepareDC( dc
);
2562 wxRegion reg
= GetUpdateRegion();
2563 m_owner
->CalcCellsExposed( reg
);
2564 m_owner
->DrawGridCellArea( dc
);
2565 #if WXGRID_DRAW_LINES
2566 m_owner
->DrawAllGridLines( dc
, reg
);
2568 m_owner
->DrawHighlight( dc
);
2572 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2574 wxPanel::ScrollWindow( dx
, dy
, rect
);
2575 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2576 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2580 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2582 m_owner
->ProcessGridCellMouseEvent( event
);
2586 // This seems to be required for wxMotif otherwise the mouse
2587 // cursor must be in the cell edit control to get key events
2589 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2591 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2594 // We are trapping erase background events to reduce flicker under MSW
2595 // and GTK but this can leave junk in the space beyond the last row and
2596 // col. So here we paint these spaces if they are visible.
2598 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2601 GetClientSize( &cw
, &ch
);
2604 m_owner
->CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2607 rightRect
= m_owner
->CellToRect( 0, m_owner
->GetNumberCols()-1 );
2610 bottomRect
= m_owner
->CellToRect( m_owner
->GetNumberRows()-1, 0 );
2612 if ( right
> rightRect
.GetRight() || bottom
> bottomRect
.GetBottom() )
2615 m_owner
->CalcUnscrolledPosition( 0, 0, &left
, &top
);
2617 wxClientDC
dc( this );
2618 m_owner
->PrepareDC( dc
);
2619 dc
.SetBrush( wxBrush(m_owner
->GetDefaultCellBackgroundColour(), wxSOLID
) );
2620 dc
.SetPen( *wxTRANSPARENT_PEN
);
2622 if ( right
> rightRect
.GetRight() )
2623 dc
.DrawRectangle( rightRect
.GetRight()+1, top
, right
- rightRect
.GetRight(), ch
);
2625 if ( bottom
> bottomRect
.GetBottom() )
2626 dc
.DrawRectangle( left
, bottomRect
.GetBottom()+1, cw
, bottom
- bottomRect
.GetBottom() );
2631 //////////////////////////////////////////////////////////////////////
2634 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2636 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2637 EVT_PAINT( wxGrid::OnPaint
)
2638 EVT_SIZE( wxGrid::OnSize
)
2639 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2640 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2643 wxGrid::wxGrid( wxWindow
*parent
,
2648 const wxString
& name
)
2649 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2650 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2659 m_defaultCellAttr
->SafeDecRef();
2661 #ifdef DEBUG_ATTR_CACHE
2662 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2663 wxPrintf(_T("wxGrid attribute cache statistics: "
2664 "total: %u, hits: %u (%u%%)\n"),
2665 total
, gs_nAttrCacheHits
,
2666 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2672 delete m_typeRegistry
;
2677 // ----- internal init and update functions
2680 void wxGrid::Create()
2682 m_created
= FALSE
; // set to TRUE by CreateGrid
2683 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2685 m_table
= (wxGridTableBase
*) NULL
;
2688 m_cellEditCtrlEnabled
= FALSE
;
2690 m_defaultCellAttr
= new wxGridCellAttr
;
2691 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2693 // Set default cell attributes
2694 m_defaultCellAttr
->SetFont(GetFont());
2695 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2696 m_defaultCellAttr
->SetTextColour(
2697 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2698 m_defaultCellAttr
->SetBackgroundColour(
2699 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2700 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2701 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2706 m_currentCellCoords
= wxGridNoCellCoords
;
2708 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2709 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2711 // data type registration: register all standard data types
2712 // TODO: may be allow the app to selectively disable some of them?
2713 m_typeRegistry
= new wxGridTypeRegistry
;
2714 RegisterDataType(wxGRID_VALUE_STRING
,
2715 new wxGridCellStringRenderer
,
2716 new wxGridCellTextEditor
);
2717 RegisterDataType(wxGRID_VALUE_BOOL
,
2718 new wxGridCellBoolRenderer
,
2719 new wxGridCellBoolEditor
);
2720 RegisterDataType(wxGRID_VALUE_NUMBER
,
2721 new wxGridCellNumberRenderer
,
2722 new wxGridCellNumberEditor
);
2724 // subwindow components that make up the wxGrid
2725 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2730 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2735 m_colLabelWin
= new wxGridColLabelWindow( this,
2740 m_gridWin
= new wxGridWindow( this,
2747 SetTargetWindow( m_gridWin
);
2751 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2755 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2760 m_numRows
= numRows
;
2761 m_numCols
= numCols
;
2763 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2764 m_table
->SetView( this );
2773 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2777 // RD: Actually, this should probably be allowed. I think it would be
2778 // nice to be able to switch multiple Tables in and out of a single
2779 // View at runtime. Is there anything in the implmentation that would
2782 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2787 m_numRows
= table
->GetNumberRows();
2788 m_numCols
= table
->GetNumberCols();
2791 m_table
->SetView( this );
2804 if ( m_numRows
<= 0 )
2805 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2807 if ( m_numCols
<= 0 )
2808 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2810 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2811 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2813 if ( m_rowLabelWin
)
2815 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2819 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2822 m_labelTextColour
= wxColour( _T("BLACK") );
2825 m_attrCache
.row
= -1;
2827 // TODO: something better than this ?
2829 m_labelFont
= this->GetFont();
2830 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2832 m_rowLabelHorizAlign
= wxLEFT
;
2833 m_rowLabelVertAlign
= wxCENTRE
;
2835 m_colLabelHorizAlign
= wxCENTRE
;
2836 m_colLabelVertAlign
= wxTOP
;
2838 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2839 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2841 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2842 m_defaultRowHeight
+= 8;
2844 m_defaultRowHeight
+= 4;
2847 m_gridLineColour
= wxColour( 128, 128, 255 );
2848 m_gridLinesEnabled
= TRUE
;
2850 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2851 m_winCapture
= (wxWindow
*)NULL
;
2852 m_canDragRowSize
= TRUE
;
2853 m_canDragColSize
= TRUE
;
2855 m_dragRowOrCol
= -1;
2856 m_isDragging
= FALSE
;
2857 m_startDragPos
= wxDefaultPosition
;
2859 m_waitForSlowClick
= FALSE
;
2861 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2862 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2864 m_currentCellCoords
= wxGridNoCellCoords
;
2866 m_selectedTopLeft
= wxGridNoCellCoords
;
2867 m_selectedBottomRight
= wxGridNoCellCoords
;
2868 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2869 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2871 m_editable
= TRUE
; // default for whole grid
2873 m_inOnKeyDown
= FALSE
;
2877 // ----------------------------------------------------------------------------
2878 // the idea is to call these functions only when necessary because they create
2879 // quite big arrays which eat memory mostly unnecessary - in particular, if
2880 // default widths/heights are used for all rows/columns, we may not use these
2883 // with some extra code, it should be possible to only store the
2884 // widths/heights different from default ones but this will be done later...
2885 // ----------------------------------------------------------------------------
2887 void wxGrid::InitRowHeights()
2889 m_rowHeights
.Empty();
2890 m_rowBottoms
.Empty();
2892 m_rowHeights
.Alloc( m_numRows
);
2893 m_rowBottoms
.Alloc( m_numRows
);
2896 for ( int i
= 0; i
< m_numRows
; i
++ )
2898 m_rowHeights
.Add( m_defaultRowHeight
);
2899 rowBottom
+= m_defaultRowHeight
;
2900 m_rowBottoms
.Add( rowBottom
);
2904 void wxGrid::InitColWidths()
2906 m_colWidths
.Empty();
2907 m_colRights
.Empty();
2909 m_colWidths
.Alloc( m_numCols
);
2910 m_colRights
.Alloc( m_numCols
);
2912 for ( int i
= 0; i
< m_numCols
; i
++ )
2914 m_colWidths
.Add( m_defaultColWidth
);
2915 colRight
+= m_defaultColWidth
;
2916 m_colRights
.Add( colRight
);
2920 int wxGrid::GetColWidth(int col
) const
2922 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2925 int wxGrid::GetColLeft(int col
) const
2927 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2928 : m_colRights
[col
] - m_colWidths
[col
];
2931 int wxGrid::GetColRight(int col
) const
2933 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
2937 int wxGrid::GetRowHeight(int row
) const
2939 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2942 int wxGrid::GetRowTop(int row
) const
2944 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2945 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2948 int wxGrid::GetRowBottom(int row
) const
2950 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2951 : m_rowBottoms
[row
];
2954 void wxGrid::CalcDimensions()
2957 GetClientSize( &cw
, &ch
);
2959 if ( m_numRows
> 0 && m_numCols
> 0 )
2961 int right
= GetColRight( m_numCols
-1 ) + 50;
2962 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
2964 // TODO: restore the scroll position that we had before sizing
2967 GetViewStart( &x
, &y
);
2968 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2969 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2975 void wxGrid::CalcWindowSizes()
2978 GetClientSize( &cw
, &ch
);
2980 if ( m_cornerLabelWin
->IsShown() )
2981 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2983 if ( m_colLabelWin
->IsShown() )
2984 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2986 if ( m_rowLabelWin
->IsShown() )
2987 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2989 if ( m_gridWin
->IsShown() )
2990 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2994 // this is called when the grid table sends a message to say that it
2995 // has been redimensioned
2997 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3001 // if we were using the default widths/heights so far, we must change them
3003 if ( m_colWidths
.IsEmpty() )
3008 if ( m_rowHeights
.IsEmpty() )
3013 switch ( msg
.GetId() )
3015 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3017 size_t pos
= msg
.GetCommandInt();
3018 int numRows
= msg
.GetCommandInt2();
3019 for ( i
= 0; i
< numRows
; i
++ )
3021 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3022 m_rowBottoms
.Insert( 0, pos
);
3024 m_numRows
+= numRows
;
3027 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3029 for ( i
= pos
; i
< m_numRows
; i
++ )
3031 bottom
+= m_rowHeights
[i
];
3032 m_rowBottoms
[i
] = bottom
;
3038 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3040 int numRows
= msg
.GetCommandInt();
3041 for ( i
= 0; i
< numRows
; i
++ )
3043 m_rowHeights
.Add( m_defaultRowHeight
);
3044 m_rowBottoms
.Add( 0 );
3047 int oldNumRows
= m_numRows
;
3048 m_numRows
+= numRows
;
3051 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3053 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3055 bottom
+= m_rowHeights
[i
];
3056 m_rowBottoms
[i
] = bottom
;
3062 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3064 size_t pos
= msg
.GetCommandInt();
3065 int numRows
= msg
.GetCommandInt2();
3066 for ( i
= 0; i
< numRows
; i
++ )
3068 m_rowHeights
.Remove( pos
);
3069 m_rowBottoms
.Remove( pos
);
3071 m_numRows
-= numRows
;
3076 m_colWidths
.Clear();
3077 m_colRights
.Clear();
3078 m_currentCellCoords
= wxGridNoCellCoords
;
3082 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3083 m_currentCellCoords
.Set( 0, 0 );
3086 for ( i
= 0; i
< m_numRows
; i
++ )
3088 h
+= m_rowHeights
[i
];
3089 m_rowBottoms
[i
] = h
;
3097 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3099 size_t pos
= msg
.GetCommandInt();
3100 int numCols
= msg
.GetCommandInt2();
3101 for ( i
= 0; i
< numCols
; i
++ )
3103 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3104 m_colRights
.Insert( 0, pos
);
3106 m_numCols
+= numCols
;
3109 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3111 for ( i
= pos
; i
< m_numCols
; i
++ )
3113 right
+= m_colWidths
[i
];
3114 m_colRights
[i
] = right
;
3120 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3122 int numCols
= msg
.GetCommandInt();
3123 for ( i
= 0; i
< numCols
; i
++ )
3125 m_colWidths
.Add( m_defaultColWidth
);
3126 m_colRights
.Add( 0 );
3129 int oldNumCols
= m_numCols
;
3130 m_numCols
+= numCols
;
3133 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3135 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3137 right
+= m_colWidths
[i
];
3138 m_colRights
[i
] = right
;
3144 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3146 size_t pos
= msg
.GetCommandInt();
3147 int numCols
= msg
.GetCommandInt2();
3148 for ( i
= 0; i
< numCols
; i
++ )
3150 m_colWidths
.Remove( pos
);
3151 m_colRights
.Remove( pos
);
3153 m_numCols
-= numCols
;
3157 #if 0 // leave the row alone here so that AppendCols will work subsequently
3159 m_rowHeights
.Clear();
3160 m_rowBottoms
.Clear();
3162 m_currentCellCoords
= wxGridNoCellCoords
;
3166 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3167 m_currentCellCoords
.Set( 0, 0 );
3170 for ( i
= 0; i
< m_numCols
; i
++ )
3172 w
+= m_colWidths
[i
];
3185 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3187 wxRegionIterator
iter( reg
);
3190 m_rowLabelsExposed
.Empty();
3197 // TODO: remove this when we can...
3198 // There is a bug in wxMotif that gives garbage update
3199 // rectangles if you jump-scroll a long way by clicking the
3200 // scrollbar with middle button. This is a work-around
3202 #if defined(__WXMOTIF__)
3204 m_gridWin
->GetClientSize( &cw
, &ch
);
3205 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3206 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3209 // logical bounds of update region
3212 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3213 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3215 // find the row labels within these bounds
3218 for ( row
= 0; row
< m_numRows
; row
++ )
3220 if ( GetRowBottom(row
) < top
)
3223 if ( GetRowTop(row
) > bottom
)
3226 m_rowLabelsExposed
.Add( row
);
3234 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3236 wxRegionIterator
iter( reg
);
3239 m_colLabelsExposed
.Empty();
3246 // TODO: remove this when we can...
3247 // There is a bug in wxMotif that gives garbage update
3248 // rectangles if you jump-scroll a long way by clicking the
3249 // scrollbar with middle button. This is a work-around
3251 #if defined(__WXMOTIF__)
3253 m_gridWin
->GetClientSize( &cw
, &ch
);
3254 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3255 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3258 // logical bounds of update region
3261 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3262 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3264 // find the cells within these bounds
3267 for ( col
= 0; col
< m_numCols
; col
++ )
3269 if ( GetColRight(col
) < left
)
3272 if ( GetColLeft(col
) > right
)
3275 m_colLabelsExposed
.Add( col
);
3283 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3285 wxRegionIterator
iter( reg
);
3288 m_cellsExposed
.Empty();
3289 m_rowsExposed
.Empty();
3290 m_colsExposed
.Empty();
3292 int left
, top
, right
, bottom
;
3297 // TODO: remove this when we can...
3298 // There is a bug in wxMotif that gives garbage update
3299 // rectangles if you jump-scroll a long way by clicking the
3300 // scrollbar with middle button. This is a work-around
3302 #if defined(__WXMOTIF__)
3304 m_gridWin
->GetClientSize( &cw
, &ch
);
3305 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3306 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3307 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3308 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3311 // logical bounds of update region
3313 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3314 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3316 // find the cells within these bounds
3319 for ( row
= 0; row
< m_numRows
; row
++ )
3321 if ( GetRowBottom(row
) <= top
)
3324 if ( GetRowTop(row
) > bottom
)
3327 m_rowsExposed
.Add( row
);
3329 for ( col
= 0; col
< m_numCols
; col
++ )
3331 if ( GetColRight(col
) <= left
)
3334 if ( GetColLeft(col
) > right
)
3337 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3338 m_colsExposed
.Add( col
);
3339 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3348 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3351 wxPoint
pos( event
.GetPosition() );
3352 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3354 if ( event
.Dragging() )
3356 m_isDragging
= TRUE
;
3358 if ( event
.LeftIsDown() )
3360 switch( m_cursorMode
)
3362 case WXGRID_CURSOR_RESIZE_ROW
:
3364 int cw
, ch
, left
, dummy
;
3365 m_gridWin
->GetClientSize( &cw
, &ch
);
3366 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3368 wxClientDC
dc( m_gridWin
);
3370 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3371 dc
.SetLogicalFunction(wxINVERT
);
3372 if ( m_dragLastPos
>= 0 )
3374 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3376 dc
.DrawLine( left
, y
, left
+cw
, y
);
3381 case WXGRID_CURSOR_SELECT_ROW
:
3382 if ( (row
= YToRow( y
)) >= 0 &&
3383 !IsInSelection( row
, 0 ) )
3385 SelectRow( row
, TRUE
);
3388 // default label to suppress warnings about "enumeration value
3389 // 'xxx' not handled in switch
3397 m_isDragging
= FALSE
;
3400 // ------------ Entering or leaving the window
3402 if ( event
.Entering() || event
.Leaving() )
3404 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3408 // ------------ Left button pressed
3410 else if ( event
.LeftDown() )
3412 // don't send a label click event for a hit on the
3413 // edge of the row label - this is probably the user
3414 // wanting to resize the row
3416 if ( YToEdgeOfRow(y
) < 0 )
3420 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3422 SelectRow( row
, event
.ShiftDown() );
3423 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3428 // starting to drag-resize a row
3430 if ( CanDragRowSize() )
3431 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3436 // ------------ Left double click
3438 else if (event
.LeftDClick() )
3440 if ( YToEdgeOfRow(y
) < 0 )
3443 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3448 // ------------ Left button released
3450 else if ( event
.LeftUp() )
3452 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3454 DoEndDragResizeRow();
3456 // Note: we are ending the event *after* doing
3457 // default processing in this case
3459 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3462 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3467 // ------------ Right button down
3469 else if ( event
.RightDown() )
3472 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3474 // no default action at the moment
3479 // ------------ Right double click
3481 else if ( event
.RightDClick() )
3484 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3486 // no default action at the moment
3491 // ------------ No buttons down and mouse moving
3493 else if ( event
.Moving() )
3495 m_dragRowOrCol
= YToEdgeOfRow( y
);
3496 if ( m_dragRowOrCol
>= 0 )
3498 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3500 // don't capture the mouse yet
3501 if ( CanDragRowSize() )
3502 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3505 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3507 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3513 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3516 wxPoint
pos( event
.GetPosition() );
3517 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3519 if ( event
.Dragging() )
3521 m_isDragging
= TRUE
;
3523 if ( event
.LeftIsDown() )
3525 switch( m_cursorMode
)
3527 case WXGRID_CURSOR_RESIZE_COL
:
3529 int cw
, ch
, dummy
, top
;
3530 m_gridWin
->GetClientSize( &cw
, &ch
);
3531 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3533 wxClientDC
dc( m_gridWin
);
3536 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3537 GetColMinimalWidth(m_dragRowOrCol
));
3538 dc
.SetLogicalFunction(wxINVERT
);
3539 if ( m_dragLastPos
>= 0 )
3541 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3543 dc
.DrawLine( x
, top
, x
, top
+ch
);
3548 case WXGRID_CURSOR_SELECT_COL
:
3549 if ( (col
= XToCol( x
)) >= 0 &&
3550 !IsInSelection( 0, col
) )
3552 SelectCol( col
, TRUE
);
3555 // default label to suppress warnings about "enumeration value
3556 // 'xxx' not handled in switch
3564 m_isDragging
= FALSE
;
3567 // ------------ Entering or leaving the window
3569 if ( event
.Entering() || event
.Leaving() )
3571 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3575 // ------------ Left button pressed
3577 else if ( event
.LeftDown() )
3579 // don't send a label click event for a hit on the
3580 // edge of the col label - this is probably the user
3581 // wanting to resize the col
3583 if ( XToEdgeOfCol(x
) < 0 )
3587 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3589 SelectCol( col
, event
.ShiftDown() );
3590 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3595 // starting to drag-resize a col
3597 if ( CanDragColSize() )
3598 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3603 // ------------ Left double click
3605 if ( event
.LeftDClick() )
3607 if ( XToEdgeOfCol(x
) < 0 )
3610 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3615 // ------------ Left button released
3617 else if ( event
.LeftUp() )
3619 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3621 DoEndDragResizeCol();
3623 // Note: we are ending the event *after* doing
3624 // default processing in this case
3626 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3629 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3634 // ------------ Right button down
3636 else if ( event
.RightDown() )
3639 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3641 // no default action at the moment
3646 // ------------ Right double click
3648 else if ( event
.RightDClick() )
3651 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3653 // no default action at the moment
3658 // ------------ No buttons down and mouse moving
3660 else if ( event
.Moving() )
3662 m_dragRowOrCol
= XToEdgeOfCol( x
);
3663 if ( m_dragRowOrCol
>= 0 )
3665 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3667 // don't capture the cursor yet
3668 if ( CanDragColSize() )
3669 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3672 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3674 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3680 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3682 if ( event
.LeftDown() )
3684 // indicate corner label by having both row and
3687 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3693 else if ( event
.LeftDClick() )
3695 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3698 else if ( event
.RightDown() )
3700 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3702 // no default action at the moment
3706 else if ( event
.RightDClick() )
3708 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3710 // no default action at the moment
3715 void wxGrid::ChangeCursorMode(CursorMode mode
,
3720 static const wxChar
*cursorModes
[] =
3729 wxLogTrace(_T("grid"),
3730 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3731 win
== m_colLabelWin
? _T("colLabelWin")
3732 : win
? _T("rowLabelWin")
3734 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3735 #endif // __WXDEBUG__
3737 if ( mode
== m_cursorMode
)
3742 // by default use the grid itself
3748 m_winCapture
->ReleaseMouse();
3749 m_winCapture
= (wxWindow
*)NULL
;
3752 m_cursorMode
= mode
;
3754 switch ( m_cursorMode
)
3756 case WXGRID_CURSOR_RESIZE_ROW
:
3757 win
->SetCursor( m_rowResizeCursor
);
3760 case WXGRID_CURSOR_RESIZE_COL
:
3761 win
->SetCursor( m_colResizeCursor
);
3765 win
->SetCursor( *wxSTANDARD_CURSOR
);
3768 // we need to capture mouse when resizing
3769 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3770 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3772 if ( captureMouse
&& resize
)
3774 win
->CaptureMouse();
3779 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3782 wxPoint
pos( event
.GetPosition() );
3783 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3785 wxGridCellCoords coords
;
3786 XYToCell( x
, y
, coords
);
3788 if ( event
.Dragging() )
3790 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3792 // Don't start doing anything until the mouse has been drug at
3793 // least 3 pixels in any direction...
3796 if (m_startDragPos
== wxDefaultPosition
)
3798 m_startDragPos
= pos
;
3801 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3805 m_isDragging
= TRUE
;
3806 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3808 // Hide the edit control, so it
3809 // won't interfer with drag-shrinking.
3810 if ( IsCellEditControlEnabled() )
3811 HideCellEditControl();
3813 // Have we captured the mouse yet?
3816 m_winCapture
= m_gridWin
;
3817 m_winCapture
->CaptureMouse();
3820 if ( coords
!= wxGridNoCellCoords
)
3822 if ( !IsSelection() )
3824 SelectBlock( coords
, coords
);
3828 SelectBlock( m_currentCellCoords
, coords
);
3831 if (! IsVisible(coords
))
3833 MakeCellVisible(coords
);
3834 // TODO: need to introduce a delay or something here. The
3835 // scrolling is way to fast, at least on MSW.
3839 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3841 int cw
, ch
, left
, dummy
;
3842 m_gridWin
->GetClientSize( &cw
, &ch
);
3843 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3845 wxClientDC
dc( m_gridWin
);
3847 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3848 dc
.SetLogicalFunction(wxINVERT
);
3849 if ( m_dragLastPos
>= 0 )
3851 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3853 dc
.DrawLine( left
, y
, left
+cw
, y
);
3856 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3858 int cw
, ch
, dummy
, top
;
3859 m_gridWin
->GetClientSize( &cw
, &ch
);
3860 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3862 wxClientDC
dc( m_gridWin
);
3864 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3865 GetColMinimalWidth(m_dragRowOrCol
) );
3866 dc
.SetLogicalFunction(wxINVERT
);
3867 if ( m_dragLastPos
>= 0 )
3869 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3871 dc
.DrawLine( x
, top
, x
, top
+ch
);
3878 m_isDragging
= FALSE
;
3879 m_startDragPos
= wxDefaultPosition
;
3882 if ( coords
!= wxGridNoCellCoords
)
3884 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3885 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3888 if ( event
.Entering() || event
.Leaving() )
3890 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3891 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3896 // ------------ Left button pressed
3898 if ( event
.LeftDown() )
3900 DisableCellEditControl();
3901 if ( event
.ShiftDown() )
3903 SelectBlock( m_currentCellCoords
, coords
);
3905 else if ( XToEdgeOfCol(x
) < 0 &&
3906 YToEdgeOfRow(y
) < 0 )
3908 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3913 MakeCellVisible( coords
);
3915 // if this is the second click on this cell then start
3917 if ( m_waitForSlowClick
&&
3918 (coords
== m_currentCellCoords
) &&
3919 CanEnableCellControl())
3921 EnableCellEditControl();
3923 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3924 attr
->GetEditor(GetDefaultEditorForCell(coords
.GetRow(), coords
.GetCol()))->StartingClick();
3927 m_waitForSlowClick
= FALSE
;
3931 SetCurrentCell( coords
);
3932 m_waitForSlowClick
= TRUE
;
3939 // ------------ Left double click
3941 else if ( event
.LeftDClick() )
3943 DisableCellEditControl();
3944 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3946 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3954 // ------------ Left button released
3956 else if ( event
.LeftUp() )
3958 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3960 if ( IsSelection() )
3964 m_winCapture
->ReleaseMouse();
3965 m_winCapture
= NULL
;
3967 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3970 // Show the edit control, if it has been hidden for
3972 ShowCellEditControl();
3974 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3976 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3977 DoEndDragResizeRow();
3979 // Note: we are ending the event *after* doing
3980 // default processing in this case
3982 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3984 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3986 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3987 DoEndDragResizeCol();
3989 // Note: we are ending the event *after* doing
3990 // default processing in this case
3992 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3999 // ------------ Right button down
4001 else if ( event
.RightDown() )
4003 DisableCellEditControl();
4004 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4009 // no default action at the moment
4014 // ------------ Right double click
4016 else if ( event
.RightDClick() )
4018 DisableCellEditControl();
4019 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4024 // no default action at the moment
4028 // ------------ Moving and no button action
4030 else if ( event
.Moving() && !event
.IsButton() )
4032 int dragRow
= YToEdgeOfRow( y
);
4033 int dragCol
= XToEdgeOfCol( x
);
4035 // Dragging on the corner of a cell to resize in both
4036 // directions is not implemented yet...
4038 if ( dragRow
>= 0 && dragCol
>= 0 )
4040 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4046 m_dragRowOrCol
= dragRow
;
4048 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4050 if ( CanDragRowSize() )
4051 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4059 m_dragRowOrCol
= dragCol
;
4061 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4063 if ( CanDragColSize() )
4064 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4070 // Neither on a row or col edge
4072 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4074 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4081 void wxGrid::DoEndDragResizeRow()
4083 if ( m_dragLastPos
>= 0 )
4085 // erase the last line and resize the row
4087 int cw
, ch
, left
, dummy
;
4088 m_gridWin
->GetClientSize( &cw
, &ch
);
4089 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4091 wxClientDC
dc( m_gridWin
);
4093 dc
.SetLogicalFunction( wxINVERT
);
4094 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4095 HideCellEditControl();
4097 int rowTop
= GetRowTop(m_dragRowOrCol
);
4098 SetRowSize( m_dragRowOrCol
,
4099 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4101 if ( !GetBatchCount() )
4103 // Only needed to get the correct rect.y:
4104 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4106 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4107 rect
.width
= m_rowLabelWidth
;
4108 rect
.height
= ch
- rect
.y
;
4109 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4111 m_gridWin
->Refresh( FALSE
, &rect
);
4114 ShowCellEditControl();
4119 void wxGrid::DoEndDragResizeCol()
4121 if ( m_dragLastPos
>= 0 )
4123 // erase the last line and resize the col
4125 int cw
, ch
, dummy
, top
;
4126 m_gridWin
->GetClientSize( &cw
, &ch
);
4127 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4129 wxClientDC
dc( m_gridWin
);
4131 dc
.SetLogicalFunction( wxINVERT
);
4132 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4133 HideCellEditControl();
4135 int colLeft
= GetColLeft(m_dragRowOrCol
);
4136 SetColSize( m_dragRowOrCol
,
4137 wxMax( m_dragLastPos
- colLeft
,
4138 GetColMinimalWidth(m_dragRowOrCol
) ) );
4140 if ( !GetBatchCount() )
4142 // Only needed to get the correct rect.x:
4143 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4145 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4146 rect
.width
= cw
- rect
.x
;
4147 rect
.height
= m_colLabelHeight
;
4148 m_colLabelWin
->Refresh( TRUE
, &rect
);
4150 m_gridWin
->Refresh( FALSE
, &rect
);
4153 ShowCellEditControl();
4160 // ------ interaction with data model
4162 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4164 switch ( msg
.GetId() )
4166 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4167 return GetModelValues();
4169 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4170 return SetModelValues();
4172 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4173 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4174 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4175 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4176 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4177 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4178 return Redimension( msg
);
4187 // The behaviour of this function depends on the grid table class
4188 // Clear() function. For the default wxGridStringTable class the
4189 // behavious is to replace all cell contents with wxEmptyString but
4190 // not to change the number of rows or cols.
4192 void wxGrid::ClearGrid()
4197 SetEditControlValue();
4198 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4203 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4205 // TODO: something with updateLabels flag
4209 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4215 if (IsCellEditControlEnabled())
4216 DisableCellEditControl();
4218 bool ok
= m_table
->InsertRows( pos
, numRows
);
4220 // the table will have sent the results of the insert row
4221 // operation to this view object as a grid table message
4225 if ( m_numCols
== 0 )
4227 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4229 // TODO: perhaps instead of appending the default number of cols
4230 // we should remember what the last non-zero number of cols was ?
4234 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4236 // if we have just inserted cols into an empty grid the current
4237 // cell will be undefined...
4239 SetCurrentCell( 0, 0 );
4243 if ( !GetBatchCount() ) Refresh();
4246 SetEditControlValue();
4256 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4258 // TODO: something with updateLabels flag
4262 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4266 if ( m_table
&& m_table
->AppendRows( numRows
) )
4268 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4270 // if we have just inserted cols into an empty grid the current
4271 // cell will be undefined...
4273 SetCurrentCell( 0, 0 );
4276 // the table will have sent the results of the append row
4277 // operation to this view object as a grid table message
4280 if ( !GetBatchCount() ) Refresh();
4290 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4292 // TODO: something with updateLabels flag
4296 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4302 if (IsCellEditControlEnabled())
4303 DisableCellEditControl();
4305 if (m_table
->DeleteRows( pos
, numRows
))
4308 // the table will have sent the results of the delete row
4309 // operation to this view object as a grid table message
4312 if ( !GetBatchCount() ) Refresh();
4320 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4322 // TODO: something with updateLabels flag
4326 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4332 if (IsCellEditControlEnabled())
4333 DisableCellEditControl();
4335 bool ok
= m_table
->InsertCols( pos
, numCols
);
4337 // the table will have sent the results of the insert col
4338 // operation to this view object as a grid table message
4342 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4344 // if we have just inserted cols into an empty grid the current
4345 // cell will be undefined...
4347 SetCurrentCell( 0, 0 );
4351 if ( !GetBatchCount() ) Refresh();
4354 SetEditControlValue();
4364 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4366 // TODO: something with updateLabels flag
4370 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4374 if ( m_table
&& m_table
->AppendCols( numCols
) )
4376 // the table will have sent the results of the append col
4377 // operation to this view object as a grid table message
4379 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4381 // if we have just inserted cols into an empty grid the current
4382 // cell will be undefined...
4384 SetCurrentCell( 0, 0 );
4388 if ( !GetBatchCount() ) Refresh();
4398 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4400 // TODO: something with updateLabels flag
4404 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4410 if (IsCellEditControlEnabled())
4411 DisableCellEditControl();
4413 if ( m_table
->DeleteCols( pos
, numCols
) )
4415 // the table will have sent the results of the delete col
4416 // operation to this view object as a grid table message
4419 if ( !GetBatchCount() ) Refresh();
4429 // ----- event handlers
4432 // Generate a grid event based on a mouse event and
4433 // return the result of ProcessEvent()
4435 bool wxGrid::SendEvent( const wxEventType type
,
4437 wxMouseEvent
& mouseEv
)
4439 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4441 int rowOrCol
= (row
== -1 ? col
: row
);
4443 wxGridSizeEvent
gridEvt( GetId(),
4447 mouseEv
.GetX(), mouseEv
.GetY(),
4448 mouseEv
.ControlDown(),
4449 mouseEv
.ShiftDown(),
4451 mouseEv
.MetaDown() );
4453 return GetEventHandler()->ProcessEvent(gridEvt
);
4455 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4457 wxGridRangeSelectEvent
gridEvt( GetId(),
4461 m_selectedBottomRight
,
4462 mouseEv
.ControlDown(),
4463 mouseEv
.ShiftDown(),
4465 mouseEv
.MetaDown() );
4467 return GetEventHandler()->ProcessEvent(gridEvt
);
4471 wxGridEvent
gridEvt( GetId(),
4475 mouseEv
.GetX(), mouseEv
.GetY(),
4476 mouseEv
.ControlDown(),
4477 mouseEv
.ShiftDown(),
4479 mouseEv
.MetaDown() );
4481 return GetEventHandler()->ProcessEvent(gridEvt
);
4486 // Generate a grid event of specified type and return the result
4487 // of ProcessEvent().
4489 bool wxGrid::SendEvent( const wxEventType type
,
4492 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4494 int rowOrCol
= (row
== -1 ? col
: row
);
4496 wxGridSizeEvent
gridEvt( GetId(),
4501 return GetEventHandler()->ProcessEvent(gridEvt
);
4505 wxGridEvent
gridEvt( GetId(),
4510 return GetEventHandler()->ProcessEvent(gridEvt
);
4515 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4517 wxPaintDC
dc( this );
4519 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4520 m_numRows
&& m_numCols
)
4522 m_currentCellCoords
.Set(0, 0);
4523 SetEditControlValue();
4524 ShowCellEditControl();
4531 // This is just here to make sure that CalcDimensions gets called when
4532 // the grid view is resized... then the size event is skipped to allow
4533 // the box sizers to handle everything
4535 void wxGrid::OnSize( wxSizeEvent
& event
)
4542 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4544 if ( m_inOnKeyDown
)
4546 // shouldn't be here - we are going round in circles...
4548 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4551 m_inOnKeyDown
= TRUE
;
4553 // propagate the event up and see if it gets processed
4555 wxWindow
*parent
= GetParent();
4556 wxKeyEvent
keyEvt( event
);
4557 keyEvt
.SetEventObject( parent
);
4559 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4562 // TODO: Should also support Shift-cursor keys for
4563 // extending the selection. Maybe add a flag to
4564 // MoveCursorXXX() and MoveCursorXXXBlock() and
4565 // just send event.ShiftDown().
4567 // try local handlers
4569 switch ( event
.KeyCode() )
4572 if ( event
.ControlDown() )
4574 MoveCursorUpBlock();
4583 if ( event
.ControlDown() )
4585 MoveCursorDownBlock();
4594 if ( event
.ControlDown() )
4596 MoveCursorLeftBlock();
4605 if ( event
.ControlDown() )
4607 MoveCursorRightBlock();
4616 if ( event
.ControlDown() )
4618 event
.Skip(); // to let the edit control have the return
4627 if (event
.ShiftDown())
4634 if ( event
.ControlDown() )
4636 MakeCellVisible( 0, 0 );
4637 SetCurrentCell( 0, 0 );
4646 if ( event
.ControlDown() )
4648 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4649 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4665 // We don't want these keys to trigger the edit control, any others?
4674 if ( !IsEditable() )
4679 // Otherwise fall through to default
4682 // now try the cell edit control
4684 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4686 EnableCellEditControl();
4687 int row
= m_currentCellCoords
.GetRow();
4688 int col
= m_currentCellCoords
.GetCol();
4689 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4690 attr
->GetEditor(GetDefaultEditorForCell(row
, col
))->StartingKey(event
);
4695 // let others process char events for readonly cells
4702 m_inOnKeyDown
= FALSE
;
4706 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4710 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4712 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4714 // the event has been intercepted - do nothing
4719 m_currentCellCoords
!= wxGridNoCellCoords
)
4721 HideCellEditControl();
4722 SaveEditControlValue();
4723 DisableCellEditControl();
4725 // Clear the old current cell highlight
4726 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4728 // Otherwise refresh redraws the highlight!
4729 m_currentCellCoords
= coords
;
4731 m_gridWin
->Refresh( FALSE
, &r
);
4734 m_currentCellCoords
= coords
;
4736 SetEditControlValue();
4740 wxClientDC
dc(m_gridWin
);
4743 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4744 DrawCellHighlight(dc
, attr
);
4747 if ( IsSelection() )
4749 wxRect
r( SelectionToDeviceRect() );
4751 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4758 // ------ functions to get/send data (see also public functions)
4761 bool wxGrid::GetModelValues()
4765 // all we need to do is repaint the grid
4767 m_gridWin
->Refresh();
4775 bool wxGrid::SetModelValues()
4781 for ( row
= 0; row
< m_numRows
; row
++ )
4783 for ( col
= 0; col
< m_numCols
; col
++ )
4785 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4797 // Note - this function only draws cells that are in the list of
4798 // exposed cells (usually set from the update region by
4799 // CalcExposedCells)
4801 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4803 if ( !m_numRows
|| !m_numCols
) return;
4806 size_t numCells
= m_cellsExposed
.GetCount();
4808 for ( i
= 0; i
< numCells
; i
++ )
4810 DrawCell( dc
, m_cellsExposed
[i
] );
4815 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4817 int row
= coords
.GetRow();
4818 int col
= coords
.GetCol();
4820 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4823 // we draw the cell border ourselves
4824 #if !WXGRID_DRAW_LINES
4825 if ( m_gridLinesEnabled
)
4826 DrawCellBorder( dc
, coords
);
4829 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4831 bool isCurrent
= coords
== m_currentCellCoords
;
4834 rect
.x
= GetColLeft(col
);
4835 rect
.y
= GetRowTop(row
);
4836 rect
.width
= GetColWidth(col
) - 1;
4837 rect
.height
= GetRowHeight(row
) - 1;
4839 // if the editor is shown, we should use it and not the renderer
4840 if ( isCurrent
&& IsCellEditControlEnabled() )
4842 attr
->GetEditor(GetDefaultEditorForCell(row
, col
))->
4843 PaintBackground(rect
, attr
);
4847 // but all the rest is drawn by the cell renderer and hence may be
4849 attr
->GetRenderer(GetDefaultRendererForCell(row
,col
))->
4850 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4857 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4859 int row
= m_currentCellCoords
.GetRow();
4860 int col
= m_currentCellCoords
.GetCol();
4862 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4866 rect
.x
= GetColLeft(col
);
4867 rect
.y
= GetRowTop(row
);
4868 rect
.width
= GetColWidth(col
) - 1;
4869 rect
.height
= GetRowHeight(row
) - 1;
4871 // hmmm... what could we do here to show that the cell is disabled?
4872 // for now, I just draw a thinner border than for the other ones, but
4873 // it doesn't look really good
4874 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4875 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4877 dc
.DrawRectangle(rect
);
4880 // VZ: my experiments with 3d borders...
4882 // how to properly set colours for arbitrary bg?
4883 wxCoord x1
= rect
.x
,
4885 x2
= rect
.x
+ rect
.width
-1,
4886 y2
= rect
.y
+ rect
.height
-1;
4888 dc
.SetPen(*wxWHITE_PEN
);
4889 dc
.DrawLine(x1
, y1
, x2
, y1
);
4890 dc
.DrawLine(x1
, y1
, x1
, y2
);
4892 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4893 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4895 dc
.SetPen(*wxBLACK_PEN
);
4896 dc
.DrawLine(x1
, y2
, x2
, y2
);
4897 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4902 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4904 int row
= coords
.GetRow();
4905 int col
= coords
.GetCol();
4906 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4909 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4911 // right hand border
4913 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
4914 GetColRight(col
), GetRowBottom(row
) );
4918 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
4919 GetColRight(col
), GetRowBottom(row
) );
4922 void wxGrid::DrawHighlight(wxDC
& dc
)
4924 if ( IsCellEditControlEnabled() )
4926 // don't show highlight when the edit control is shown
4930 // if the active cell was repainted, repaint its highlight too because it
4931 // might have been damaged by the grid lines
4932 size_t count
= m_cellsExposed
.GetCount();
4933 for ( size_t n
= 0; n
< count
; n
++ )
4935 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4937 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4938 DrawCellHighlight(dc
, attr
);
4946 // TODO: remove this ???
4947 // This is used to redraw all grid lines e.g. when the grid line colour
4950 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4952 if ( !m_gridLinesEnabled
||
4954 !m_numCols
) return;
4956 int top
, bottom
, left
, right
;
4962 m_gridWin
->GetClientSize(&cw
, &ch
);
4964 // virtual coords of visible area
4966 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4967 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4972 reg
.GetBox(x
, y
, w
, h
);
4973 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4974 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4978 m_gridWin
->GetClientSize(&cw
, &ch
);
4979 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4980 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4983 // avoid drawing grid lines past the last row and col
4985 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
4986 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
4988 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4990 // horizontal grid lines
4993 for ( i
= 0; i
< m_numRows
; i
++ )
4995 int bot
= GetRowBottom(i
) - 1;
5004 dc
.DrawLine( left
, bot
, right
, bot
);
5009 // vertical grid lines
5011 for ( i
= 0; i
< m_numCols
; i
++ )
5013 int colRight
= GetColRight(i
) - 1;
5014 if ( colRight
> right
)
5019 if ( colRight
>= left
)
5021 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5027 void wxGrid::DrawRowLabels( wxDC
& dc
)
5029 if ( !m_numRows
|| !m_numCols
) return;
5032 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5034 for ( i
= 0; i
< numLabels
; i
++ )
5036 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5041 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5043 if ( GetRowHeight(row
) <= 0 )
5046 int rowTop
= GetRowTop(row
),
5047 rowBottom
= GetRowBottom(row
) - 1;
5049 dc
.SetPen( *wxBLACK_PEN
);
5050 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5051 m_rowLabelWidth
-1, rowBottom
);
5053 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5055 dc
.SetPen( *wxWHITE_PEN
);
5056 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5057 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5059 dc
.SetBackgroundMode( wxTRANSPARENT
);
5060 dc
.SetTextForeground( GetLabelTextColour() );
5061 dc
.SetFont( GetLabelFont() );
5064 GetRowLabelAlignment( &hAlign
, &vAlign
);
5068 rect
.SetY( GetRowTop(row
) + 2 );
5069 rect
.SetWidth( m_rowLabelWidth
- 4 );
5070 rect
.SetHeight( GetRowHeight(row
) - 4 );
5071 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5075 void wxGrid::DrawColLabels( wxDC
& dc
)
5077 if ( !m_numRows
|| !m_numCols
) return;
5080 size_t numLabels
= m_colLabelsExposed
.GetCount();
5082 for ( i
= 0; i
< numLabels
; i
++ )
5084 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5089 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5091 if ( GetColWidth(col
) <= 0 )
5094 int colLeft
= GetColLeft(col
),
5095 colRight
= GetColRight(col
) - 1;
5097 dc
.SetPen( *wxBLACK_PEN
);
5098 dc
.DrawLine( colRight
, 0,
5099 colRight
, m_colLabelHeight
-1 );
5101 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5102 colRight
, m_colLabelHeight
-1 );
5104 dc
.SetPen( *wxWHITE_PEN
);
5105 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5106 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5108 dc
.SetBackgroundMode( wxTRANSPARENT
);
5109 dc
.SetTextForeground( GetLabelTextColour() );
5110 dc
.SetFont( GetLabelFont() );
5112 dc
.SetBackgroundMode( wxTRANSPARENT
);
5113 dc
.SetTextForeground( GetLabelTextColour() );
5114 dc
.SetFont( GetLabelFont() );
5117 GetColLabelAlignment( &hAlign
, &vAlign
);
5120 rect
.SetX( colLeft
+ 2 );
5122 rect
.SetWidth( GetColWidth(col
) - 4 );
5123 rect
.SetHeight( m_colLabelHeight
- 4 );
5124 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5128 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5129 const wxString
& value
,
5134 long textWidth
, textHeight
;
5135 long lineWidth
, lineHeight
;
5136 wxArrayString lines
;
5138 dc
.SetClippingRegion( rect
);
5139 StringToLines( value
, lines
);
5140 if ( lines
.GetCount() )
5142 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5143 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5146 switch ( horizAlign
)
5149 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5153 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5162 switch ( vertAlign
)
5165 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5169 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5178 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5180 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5185 dc
.DestroyClippingRegion();
5189 // Split multi line text up into an array of strings. Any existing
5190 // contents of the string array are preserved.
5192 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5196 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5197 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5199 while ( startPos
< (int)tVal
.Length() )
5201 pos
= tVal
.Mid(startPos
).Find( eol
);
5206 else if ( pos
== 0 )
5208 lines
.Add( wxEmptyString
);
5212 lines
.Add( value
.Mid(startPos
, pos
) );
5216 if ( startPos
< (int)value
.Length() )
5218 lines
.Add( value
.Mid( startPos
) );
5223 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5224 wxArrayString
& lines
,
5225 long *width
, long *height
)
5232 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5234 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5235 w
= wxMax( w
, lineW
);
5245 // ------ Edit control functions
5249 void wxGrid::EnableEditing( bool edit
)
5251 // TODO: improve this ?
5253 if ( edit
!= m_editable
)
5257 // FIXME IMHO this won't disable the edit control if edit == FALSE
5258 // because of the check in the beginning of
5259 // EnableCellEditControl() just below (VZ)
5260 EnableCellEditControl(m_editable
);
5265 void wxGrid::EnableCellEditControl( bool enable
)
5270 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5271 SetCurrentCell( 0, 0 );
5273 if ( enable
!= m_cellEditCtrlEnabled
)
5275 // TODO allow the app to Veto() this event?
5276 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5280 // this should be checked by the caller!
5281 wxASSERT_MSG( CanEnableCellControl(),
5282 _T("can't enable editing for this cell!") );
5284 // do it before ShowCellEditControl()
5285 m_cellEditCtrlEnabled
= enable
;
5287 SetEditControlValue();
5288 ShowCellEditControl();
5292 HideCellEditControl();
5293 SaveEditControlValue();
5295 // do it after HideCellEditControl()
5296 m_cellEditCtrlEnabled
= enable
;
5301 bool wxGrid::IsCurrentCellReadOnly() const
5304 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5305 bool readonly
= attr
->IsReadOnly();
5311 bool wxGrid::CanEnableCellControl() const
5313 return m_editable
&& !IsCurrentCellReadOnly();
5316 bool wxGrid::IsCellEditControlEnabled() const
5318 // the cell edit control might be disable for all cells or just for the
5319 // current one if it's read only
5320 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5323 void wxGrid::ShowCellEditControl()
5325 if ( IsCellEditControlEnabled() )
5327 if ( !IsVisible( m_currentCellCoords
) )
5333 wxRect rect
= CellToRect( m_currentCellCoords
);
5334 int row
= m_currentCellCoords
.GetRow();
5335 int col
= m_currentCellCoords
.GetCol();
5337 // convert to scrolled coords
5339 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5341 // done in PaintBackground()
5343 // erase the highlight and the cell contents because the editor
5344 // might not cover the entire cell
5345 wxClientDC
dc( m_gridWin
);
5347 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5348 dc
.SetPen(*wxTRANSPARENT_PEN
);
5349 dc
.DrawRectangle(rect
);
5352 // cell is shifted by one pixel
5356 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5357 wxGridCellEditor
* editor
= attr
->GetEditor(GetDefaultEditorForCell(row
, col
));
5358 if ( !editor
->IsCreated() )
5360 editor
->Create(m_gridWin
, -1,
5361 new wxGridCellEditorEvtHandler(this, editor
));
5364 editor
->SetSize( rect
);
5366 editor
->Show( TRUE
, attr
);
5367 editor
->BeginEdit(row
, col
, this);
5374 void wxGrid::HideCellEditControl()
5376 if ( IsCellEditControlEnabled() )
5378 int row
= m_currentCellCoords
.GetRow();
5379 int col
= m_currentCellCoords
.GetCol();
5381 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5382 attr
->GetEditor(GetDefaultEditorForCell(row
, col
))->Show( FALSE
);
5384 m_gridWin
->SetFocus();
5389 void wxGrid::SetEditControlValue( const wxString
& value
)
5391 // RD: The new Editors get the value from the table themselves now. This
5392 // method can probably be removed...
5396 void wxGrid::SaveEditControlValue()
5398 if ( IsCellEditControlEnabled() )
5400 int row
= m_currentCellCoords
.GetRow();
5401 int col
= m_currentCellCoords
.GetCol();
5403 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5404 wxGridCellEditor
* editor
= attr
->GetEditor(GetDefaultEditorForCell(row
, col
));
5405 bool changed
= editor
->EndEdit(row
, col
, TRUE
, this);
5411 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5412 m_currentCellCoords
.GetRow(),
5413 m_currentCellCoords
.GetCol() );
5420 // ------ Grid location functions
5421 // Note that all of these functions work with the logical coordinates of
5422 // grid cells and labels so you will need to convert from device
5423 // coordinates for mouse events etc.
5426 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5428 int row
= YToRow(y
);
5429 int col
= XToCol(x
);
5431 if ( row
== -1 || col
== -1 )
5433 coords
= wxGridNoCellCoords
;
5437 coords
.Set( row
, col
);
5442 int wxGrid::YToRow( int y
)
5446 for ( i
= 0; i
< m_numRows
; i
++ )
5448 if ( y
< GetRowBottom(i
) )
5456 int wxGrid::XToCol( int x
)
5460 for ( i
= 0; i
< m_numCols
; i
++ )
5462 if ( x
< GetColRight(i
) )
5470 // return the row number that that the y coord is near the edge of, or
5471 // -1 if not near an edge
5473 int wxGrid::YToEdgeOfRow( int y
)
5477 for ( i
= 0; i
< m_numRows
; i
++ )
5479 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5481 d
= abs( y
- GetRowBottom(i
) );
5482 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5491 // return the col number that that the x coord is near the edge of, or
5492 // -1 if not near an edge
5494 int wxGrid::XToEdgeOfCol( int x
)
5498 for ( i
= 0; i
< m_numCols
; i
++ )
5500 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5502 d
= abs( x
- GetColRight(i
) );
5503 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5512 wxRect
wxGrid::CellToRect( int row
, int col
)
5514 wxRect
rect( -1, -1, -1, -1 );
5516 if ( row
>= 0 && row
< m_numRows
&&
5517 col
>= 0 && col
< m_numCols
)
5519 rect
.x
= GetColLeft(col
);
5520 rect
.y
= GetRowTop(row
);
5521 rect
.width
= GetColWidth(col
);
5522 rect
.height
= GetRowHeight(row
);
5529 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5531 // get the cell rectangle in logical coords
5533 wxRect
r( CellToRect( row
, col
) );
5535 // convert to device coords
5537 int left
, top
, right
, bottom
;
5538 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5539 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5541 // check against the client area of the grid window
5544 m_gridWin
->GetClientSize( &cw
, &ch
);
5546 if ( wholeCellVisible
)
5548 // is the cell wholly visible ?
5550 return ( left
>= 0 && right
<= cw
&&
5551 top
>= 0 && bottom
<= ch
);
5555 // is the cell partly visible ?
5557 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5558 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5563 // make the specified cell location visible by doing a minimal amount
5566 void wxGrid::MakeCellVisible( int row
, int col
)
5569 int xpos
= -1, ypos
= -1;
5571 if ( row
>= 0 && row
< m_numRows
&&
5572 col
>= 0 && col
< m_numCols
)
5574 // get the cell rectangle in logical coords
5576 wxRect
r( CellToRect( row
, col
) );
5578 // convert to device coords
5580 int left
, top
, right
, bottom
;
5581 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5582 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5585 m_gridWin
->GetClientSize( &cw
, &ch
);
5591 else if ( bottom
> ch
)
5593 int h
= r
.GetHeight();
5595 for ( i
= row
-1; i
>= 0; i
-- )
5597 int rowHeight
= GetRowHeight(i
);
5598 if ( h
+ rowHeight
> ch
)
5605 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5606 // have rounding errors (this is important, because if we do, we
5607 // might not scroll at all and some cells won't be redrawn)
5608 ypos
+= GRID_SCROLL_LINE
/ 2;
5615 else if ( right
> cw
)
5617 int w
= r
.GetWidth();
5619 for ( i
= col
-1; i
>= 0; i
-- )
5621 int colWidth
= GetColWidth(i
);
5622 if ( w
+ colWidth
> cw
)
5629 // see comment for ypos above
5630 xpos
+= GRID_SCROLL_LINE
/ 2;
5633 if ( xpos
!= -1 || ypos
!= -1 )
5635 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5636 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5637 Scroll( xpos
, ypos
);
5645 // ------ Grid cursor movement functions
5648 bool wxGrid::MoveCursorUp()
5650 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5651 m_currentCellCoords
.GetRow() > 0 )
5653 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5654 m_currentCellCoords
.GetCol() );
5656 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5657 m_currentCellCoords
.GetCol() );
5666 bool wxGrid::MoveCursorDown()
5668 // TODO: allow for scrolling
5670 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5671 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5673 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5674 m_currentCellCoords
.GetCol() );
5676 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5677 m_currentCellCoords
.GetCol() );
5686 bool wxGrid::MoveCursorLeft()
5688 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5689 m_currentCellCoords
.GetCol() > 0 )
5691 MakeCellVisible( m_currentCellCoords
.GetRow(),
5692 m_currentCellCoords
.GetCol() - 1 );
5694 SetCurrentCell( m_currentCellCoords
.GetRow(),
5695 m_currentCellCoords
.GetCol() - 1 );
5704 bool wxGrid::MoveCursorRight()
5706 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5707 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5709 MakeCellVisible( m_currentCellCoords
.GetRow(),
5710 m_currentCellCoords
.GetCol() + 1 );
5712 SetCurrentCell( m_currentCellCoords
.GetRow(),
5713 m_currentCellCoords
.GetCol() + 1 );
5722 bool wxGrid::MovePageUp()
5724 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5726 int row
= m_currentCellCoords
.GetRow();
5730 m_gridWin
->GetClientSize( &cw
, &ch
);
5732 int y
= GetRowTop(row
);
5733 int newRow
= YToRow( y
- ch
+ 1 );
5738 else if ( newRow
== row
)
5743 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5744 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5752 bool wxGrid::MovePageDown()
5754 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5756 int row
= m_currentCellCoords
.GetRow();
5757 if ( row
< m_numRows
)
5760 m_gridWin
->GetClientSize( &cw
, &ch
);
5762 int y
= GetRowTop(row
);
5763 int newRow
= YToRow( y
+ ch
);
5766 newRow
= m_numRows
- 1;
5768 else if ( newRow
== row
)
5773 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5774 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5782 bool wxGrid::MoveCursorUpBlock()
5785 m_currentCellCoords
!= wxGridNoCellCoords
&&
5786 m_currentCellCoords
.GetRow() > 0 )
5788 int row
= m_currentCellCoords
.GetRow();
5789 int col
= m_currentCellCoords
.GetCol();
5791 if ( m_table
->IsEmptyCell(row
, col
) )
5793 // starting in an empty cell: find the next block of
5799 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5802 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5804 // starting at the top of a block: find the next block
5810 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5815 // starting within a block: find the top of the block
5820 if ( m_table
->IsEmptyCell(row
, col
) )
5828 MakeCellVisible( row
, col
);
5829 SetCurrentCell( row
, col
);
5837 bool wxGrid::MoveCursorDownBlock()
5840 m_currentCellCoords
!= wxGridNoCellCoords
&&
5841 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5843 int row
= m_currentCellCoords
.GetRow();
5844 int col
= m_currentCellCoords
.GetCol();
5846 if ( m_table
->IsEmptyCell(row
, col
) )
5848 // starting in an empty cell: find the next block of
5851 while ( row
< m_numRows
-1 )
5854 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5857 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5859 // starting at the bottom of a block: find the next block
5862 while ( row
< m_numRows
-1 )
5865 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5870 // starting within a block: find the bottom of the block
5872 while ( row
< m_numRows
-1 )
5875 if ( m_table
->IsEmptyCell(row
, col
) )
5883 MakeCellVisible( row
, col
);
5884 SetCurrentCell( row
, col
);
5892 bool wxGrid::MoveCursorLeftBlock()
5895 m_currentCellCoords
!= wxGridNoCellCoords
&&
5896 m_currentCellCoords
.GetCol() > 0 )
5898 int row
= m_currentCellCoords
.GetRow();
5899 int col
= m_currentCellCoords
.GetCol();
5901 if ( m_table
->IsEmptyCell(row
, col
) )
5903 // starting in an empty cell: find the next block of
5909 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5912 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5914 // starting at the left of a block: find the next block
5920 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5925 // starting within a block: find the left of the block
5930 if ( m_table
->IsEmptyCell(row
, col
) )
5938 MakeCellVisible( row
, col
);
5939 SetCurrentCell( row
, col
);
5947 bool wxGrid::MoveCursorRightBlock()
5950 m_currentCellCoords
!= wxGridNoCellCoords
&&
5951 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5953 int row
= m_currentCellCoords
.GetRow();
5954 int col
= m_currentCellCoords
.GetCol();
5956 if ( m_table
->IsEmptyCell(row
, col
) )
5958 // starting in an empty cell: find the next block of
5961 while ( col
< m_numCols
-1 )
5964 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5967 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5969 // starting at the right of a block: find the next block
5972 while ( col
< m_numCols
-1 )
5975 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5980 // starting within a block: find the right of the block
5982 while ( col
< m_numCols
-1 )
5985 if ( m_table
->IsEmptyCell(row
, col
) )
5993 MakeCellVisible( row
, col
);
5994 SetCurrentCell( row
, col
);
6005 // ------ Label values and formatting
6008 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6010 *horiz
= m_rowLabelHorizAlign
;
6011 *vert
= m_rowLabelVertAlign
;
6014 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6016 *horiz
= m_colLabelHorizAlign
;
6017 *vert
= m_colLabelVertAlign
;
6020 wxString
wxGrid::GetRowLabelValue( int row
)
6024 return m_table
->GetRowLabelValue( row
);
6034 wxString
wxGrid::GetColLabelValue( int col
)
6038 return m_table
->GetColLabelValue( col
);
6049 void wxGrid::SetRowLabelSize( int width
)
6051 width
= wxMax( width
, 0 );
6052 if ( width
!= m_rowLabelWidth
)
6056 m_rowLabelWin
->Show( FALSE
);
6057 m_cornerLabelWin
->Show( FALSE
);
6059 else if ( m_rowLabelWidth
== 0 )
6061 m_rowLabelWin
->Show( TRUE
);
6062 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6065 m_rowLabelWidth
= width
;
6072 void wxGrid::SetColLabelSize( int height
)
6074 height
= wxMax( height
, 0 );
6075 if ( height
!= m_colLabelHeight
)
6079 m_colLabelWin
->Show( FALSE
);
6080 m_cornerLabelWin
->Show( FALSE
);
6082 else if ( m_colLabelHeight
== 0 )
6084 m_colLabelWin
->Show( TRUE
);
6085 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6088 m_colLabelHeight
= height
;
6095 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6097 if ( m_labelBackgroundColour
!= colour
)
6099 m_labelBackgroundColour
= colour
;
6100 m_rowLabelWin
->SetBackgroundColour( colour
);
6101 m_colLabelWin
->SetBackgroundColour( colour
);
6102 m_cornerLabelWin
->SetBackgroundColour( colour
);
6104 if ( !GetBatchCount() )
6106 m_rowLabelWin
->Refresh();
6107 m_colLabelWin
->Refresh();
6108 m_cornerLabelWin
->Refresh();
6113 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6115 if ( m_labelTextColour
!= colour
)
6117 m_labelTextColour
= colour
;
6118 if ( !GetBatchCount() )
6120 m_rowLabelWin
->Refresh();
6121 m_colLabelWin
->Refresh();
6126 void wxGrid::SetLabelFont( const wxFont
& font
)
6129 if ( !GetBatchCount() )
6131 m_rowLabelWin
->Refresh();
6132 m_colLabelWin
->Refresh();
6136 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6138 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6140 m_rowLabelHorizAlign
= horiz
;
6143 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6145 m_rowLabelVertAlign
= vert
;
6148 if ( !GetBatchCount() )
6150 m_rowLabelWin
->Refresh();
6154 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6156 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6158 m_colLabelHorizAlign
= horiz
;
6161 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6163 m_colLabelVertAlign
= vert
;
6166 if ( !GetBatchCount() )
6168 m_colLabelWin
->Refresh();
6172 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6176 m_table
->SetRowLabelValue( row
, s
);
6177 if ( !GetBatchCount() )
6179 wxRect rect
= CellToRect( row
, 0);
6180 if ( rect
.height
> 0 )
6182 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6184 rect
.width
= m_rowLabelWidth
;
6185 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6191 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6195 m_table
->SetColLabelValue( col
, s
);
6196 if ( !GetBatchCount() )
6198 wxRect rect
= CellToRect( 0, col
);
6199 if ( rect
.width
> 0 )
6201 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6203 rect
.height
= m_colLabelHeight
;
6204 m_colLabelWin
->Refresh( TRUE
, &rect
);
6210 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6212 if ( m_gridLineColour
!= colour
)
6214 m_gridLineColour
= colour
;
6216 wxClientDC
dc( m_gridWin
);
6218 DrawAllGridLines( dc
, wxRegion() );
6222 void wxGrid::EnableGridLines( bool enable
)
6224 if ( enable
!= m_gridLinesEnabled
)
6226 m_gridLinesEnabled
= enable
;
6228 if ( !GetBatchCount() )
6232 wxClientDC
dc( m_gridWin
);
6234 DrawAllGridLines( dc
, wxRegion() );
6238 m_gridWin
->Refresh();
6245 int wxGrid::GetDefaultRowSize()
6247 return m_defaultRowHeight
;
6250 int wxGrid::GetRowSize( int row
)
6252 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6254 return GetRowHeight(row
);
6257 int wxGrid::GetDefaultColSize()
6259 return m_defaultColWidth
;
6262 int wxGrid::GetColSize( int col
)
6264 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6266 return GetColWidth(col
);
6269 // ============================================================================
6270 // access to the grid attributes: each of them has a default value in the grid
6271 // itself and may be overidden on a per-cell basis
6272 // ============================================================================
6274 // ----------------------------------------------------------------------------
6275 // setting default attributes
6276 // ----------------------------------------------------------------------------
6278 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6280 m_defaultCellAttr
->SetBackgroundColour(col
);
6282 m_gridWin
->SetBackgroundColour(col
);
6286 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6288 m_defaultCellAttr
->SetTextColour(col
);
6291 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6293 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6296 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6298 m_defaultCellAttr
->SetFont(font
);
6301 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6303 m_defaultCellAttr
->SetRenderer(renderer
);
6306 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6308 m_defaultCellAttr
->SetEditor(editor
);
6311 // ----------------------------------------------------------------------------
6312 // access to the default attrbiutes
6313 // ----------------------------------------------------------------------------
6315 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6317 return m_defaultCellAttr
->GetBackgroundColour();
6320 wxColour
wxGrid::GetDefaultCellTextColour()
6322 return m_defaultCellAttr
->GetTextColour();
6325 wxFont
wxGrid::GetDefaultCellFont()
6327 return m_defaultCellAttr
->GetFont();
6330 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6332 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6335 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6337 return m_defaultCellAttr
->GetRenderer(NULL
);
6340 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6342 return m_defaultCellAttr
->GetEditor(NULL
);
6345 // ----------------------------------------------------------------------------
6346 // access to cell attributes
6347 // ----------------------------------------------------------------------------
6349 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6351 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6352 wxColour colour
= attr
->GetBackgroundColour();
6357 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6359 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6360 wxColour colour
= attr
->GetTextColour();
6365 wxFont
wxGrid::GetCellFont( int row
, int col
)
6367 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6368 wxFont font
= attr
->GetFont();
6373 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6375 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6376 attr
->GetAlignment(horiz
, vert
);
6380 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6382 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6383 wxGridCellRenderer
* renderer
= attr
->GetRenderer(GetDefaultRendererForCell(row
,col
));
6388 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6390 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6391 wxGridCellEditor
* editor
= attr
->GetEditor(GetDefaultEditorForCell(row
, col
));
6396 bool wxGrid::IsReadOnly(int row
, int col
) const
6398 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6399 bool isReadOnly
= attr
->IsReadOnly();
6404 // ----------------------------------------------------------------------------
6405 // attribute support: cache, automatic provider creation, ...
6406 // ----------------------------------------------------------------------------
6408 bool wxGrid::CanHaveAttributes()
6415 return m_table
->CanHaveAttributes();
6418 void wxGrid::ClearAttrCache()
6420 if ( m_attrCache
.row
!= -1 )
6422 m_attrCache
.attr
->SafeDecRef();
6423 m_attrCache
.row
= -1;
6427 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6429 wxGrid
*self
= (wxGrid
*)this; // const_cast
6431 self
->ClearAttrCache();
6432 self
->m_attrCache
.row
= row
;
6433 self
->m_attrCache
.col
= col
;
6434 self
->m_attrCache
.attr
= attr
;
6438 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6440 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6442 *attr
= m_attrCache
.attr
;
6443 (*attr
)->SafeIncRef();
6445 #ifdef DEBUG_ATTR_CACHE
6446 gs_nAttrCacheHits
++;
6453 #ifdef DEBUG_ATTR_CACHE
6454 gs_nAttrCacheMisses
++;
6460 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6462 wxGridCellAttr
*attr
;
6463 if ( !LookupAttr(row
, col
, &attr
) )
6465 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6466 CacheAttr(row
, col
, attr
);
6470 attr
->SetDefAttr(m_defaultCellAttr
);
6474 attr
= m_defaultCellAttr
;
6481 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6483 wxGridCellAttr
*attr
;
6484 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6486 wxASSERT_MSG( m_table
,
6487 _T("we may only be called if CanHaveAttributes() "
6488 "returned TRUE and then m_table should be !NULL") );
6490 attr
= m_table
->GetAttr(row
, col
);
6493 attr
= new wxGridCellAttr
;
6495 // artificially inc the ref count to match DecRef() in caller
6498 m_table
->SetAttr(attr
, row
, col
);
6501 CacheAttr(row
, col
, attr
);
6503 attr
->SetDefAttr(m_defaultCellAttr
);
6507 // ----------------------------------------------------------------------------
6508 // setting cell attributes: this is forwarded to the table
6509 // ----------------------------------------------------------------------------
6511 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6513 if ( CanHaveAttributes() )
6515 m_table
->SetRowAttr(attr
, row
);
6523 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6525 if ( CanHaveAttributes() )
6527 m_table
->SetColAttr(attr
, col
);
6535 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6537 if ( CanHaveAttributes() )
6539 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6540 attr
->SetBackgroundColour(colour
);
6545 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6547 if ( CanHaveAttributes() )
6549 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6550 attr
->SetTextColour(colour
);
6555 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6557 if ( CanHaveAttributes() )
6559 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6560 attr
->SetFont(font
);
6565 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6567 if ( CanHaveAttributes() )
6569 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6570 attr
->SetAlignment(horiz
, vert
);
6575 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6577 if ( CanHaveAttributes() )
6579 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6580 attr
->SetRenderer(renderer
);
6585 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6587 if ( CanHaveAttributes() )
6589 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6590 attr
->SetEditor(editor
);
6595 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6597 if ( CanHaveAttributes() )
6599 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6600 attr
->SetReadOnly(isReadOnly
);
6605 // ----------------------------------------------------------------------------
6606 // Data type registration
6607 // ----------------------------------------------------------------------------
6609 void wxGrid::RegisterDataType(const wxString
& typeName
,
6610 wxGridCellRenderer
* renderer
,
6611 wxGridCellEditor
* editor
)
6613 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6617 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6619 wxString typeName
= m_table
->GetTypeName(row
, col
);
6620 return GetDefaultEditorForType(typeName
);
6623 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6625 wxString typeName
= m_table
->GetTypeName(row
, col
);
6626 return GetDefaultRendererForType(typeName
);
6630 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6632 int index
= m_typeRegistry
->FindDataType(typeName
);
6634 // Should we force the failure here or let it fallback to string handling???
6635 // wxFAIL_MSG(wxT("Unknown data type name"));
6638 return m_typeRegistry
->GetEditor(index
);
6642 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6644 int index
= m_typeRegistry
->FindDataType(typeName
);
6646 // Should we force the failure here or let it fallback to string handling???
6647 // wxFAIL_MSG(wxT("Unknown data type name"));
6650 return m_typeRegistry
->GetRenderer(index
);
6654 // ----------------------------------------------------------------------------
6656 // ----------------------------------------------------------------------------
6658 void wxGrid::EnableDragRowSize( bool enable
)
6660 m_canDragRowSize
= enable
;
6664 void wxGrid::EnableDragColSize( bool enable
)
6666 m_canDragColSize
= enable
;
6670 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6672 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6674 if ( resizeExistingRows
)
6682 void wxGrid::SetRowSize( int row
, int height
)
6684 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6686 if ( m_rowHeights
.IsEmpty() )
6688 // need to really create the array
6692 int h
= wxMax( 0, height
);
6693 int diff
= h
- m_rowHeights
[row
];
6695 m_rowHeights
[row
] = h
;
6697 for ( i
= row
; i
< m_numRows
; i
++ )
6699 m_rowBottoms
[i
] += diff
;
6704 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6706 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6708 if ( resizeExistingCols
)
6716 void wxGrid::SetColSize( int col
, int width
)
6718 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6720 // should we check that it's bigger than GetColMinimalWidth(col) here?
6722 if ( m_colWidths
.IsEmpty() )
6724 // need to really create the array
6728 int w
= wxMax( 0, width
);
6729 int diff
= w
- m_colWidths
[col
];
6730 m_colWidths
[col
] = w
;
6733 for ( i
= col
; i
< m_numCols
; i
++ )
6735 m_colRights
[i
] += diff
;
6741 void wxGrid::SetColMinimalWidth( int col
, int width
)
6743 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6746 int wxGrid::GetColMinimalWidth(int col
) const
6748 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6749 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6752 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6754 wxClientDC
dc(m_gridWin
);
6756 wxCoord width
, widthMax
= 0;
6757 for ( int row
= 0; row
< m_numRows
; row
++ )
6759 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6760 wxGridCellRenderer
* renderer
= attr
->GetRenderer(GetDefaultRendererForCell(row
,col
));
6763 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6764 if ( width
> widthMax
)
6773 // now also compare with the column label width
6774 dc
.SetFont( GetLabelFont() );
6775 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6776 if ( width
> widthMax
)
6783 // empty column - give default width (notice that if widthMax is less
6784 // than default width but != 0, it's ok)
6785 widthMax
= m_defaultColWidth
;
6789 // leave some space around text
6793 SetColSize(col
, widthMax
);
6796 SetColMinimalWidth(col
, widthMax
);
6800 void wxGrid::AutoSizeColumns( bool setAsMin
)
6802 for ( int col
= 0; col
< m_numCols
; col
++ )
6804 AutoSizeColumn(col
, setAsMin
);
6809 // ------ cell value accessor functions
6812 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6816 m_table
->SetValue( row
, col
, s
.c_str() );
6817 if ( !GetBatchCount() )
6819 wxClientDC
dc( m_gridWin
);
6821 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6824 #if 0 // TODO: edit in place
6826 if ( m_currentCellCoords
.GetRow() == row
&&
6827 m_currentCellCoords
.GetCol() == col
)
6829 SetEditControlValue( s
);
6838 // ------ Block, row and col selection
6841 void wxGrid::SelectRow( int row
, bool addToSelected
)
6845 if ( IsSelection() && addToSelected
)
6848 bool need_refresh
[4];
6852 need_refresh
[3] = FALSE
;
6856 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6857 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6858 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6859 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6863 need_refresh
[0] = TRUE
;
6864 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6865 wxGridCellCoords ( oldTop
- 1,
6867 m_selectedTopLeft
.SetRow( row
);
6872 need_refresh
[1] = TRUE
;
6873 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6874 wxGridCellCoords ( oldBottom
,
6877 m_selectedTopLeft
.SetCol( 0 );
6880 if ( oldBottom
< row
)
6882 need_refresh
[2] = TRUE
;
6883 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6884 wxGridCellCoords ( row
,
6886 m_selectedBottomRight
.SetRow( row
);
6889 if ( oldRight
< m_numCols
- 1 )
6891 need_refresh
[3] = TRUE
;
6892 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6894 wxGridCellCoords ( oldBottom
,
6896 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6899 for (i
= 0; i
< 4; i
++ )
6900 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6901 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6905 r
= SelectionToDeviceRect();
6907 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6909 m_selectedTopLeft
.Set( row
, 0 );
6910 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6911 r
= SelectionToDeviceRect();
6912 m_gridWin
->Refresh( FALSE
, &r
);
6915 wxGridRangeSelectEvent
gridEvt( GetId(),
6916 wxEVT_GRID_RANGE_SELECT
,
6919 m_selectedBottomRight
);
6921 GetEventHandler()->ProcessEvent(gridEvt
);
6925 void wxGrid::SelectCol( int col
, bool addToSelected
)
6927 if ( IsSelection() && addToSelected
)
6930 bool need_refresh
[4];
6934 need_refresh
[3] = FALSE
;
6937 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6938 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6939 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6940 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6942 if ( oldLeft
> col
)
6944 need_refresh
[0] = TRUE
;
6945 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6946 wxGridCellCoords ( m_numRows
- 1,
6948 m_selectedTopLeft
.SetCol( col
);
6953 need_refresh
[1] = TRUE
;
6954 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6955 wxGridCellCoords ( oldTop
- 1,
6957 m_selectedTopLeft
.SetRow( 0 );
6960 if ( oldRight
< col
)
6962 need_refresh
[2] = TRUE
;
6963 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6964 wxGridCellCoords ( m_numRows
- 1,
6966 m_selectedBottomRight
.SetCol( col
);
6969 if ( oldBottom
< m_numRows
- 1 )
6971 need_refresh
[3] = TRUE
;
6972 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6974 wxGridCellCoords ( m_numRows
- 1,
6976 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6979 for (i
= 0; i
< 4; i
++ )
6980 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6981 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6987 r
= SelectionToDeviceRect();
6989 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6991 m_selectedTopLeft
.Set( 0, col
);
6992 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6993 r
= SelectionToDeviceRect();
6994 m_gridWin
->Refresh( FALSE
, &r
);
6997 wxGridRangeSelectEvent
gridEvt( GetId(),
6998 wxEVT_GRID_RANGE_SELECT
,
7001 m_selectedBottomRight
);
7003 GetEventHandler()->ProcessEvent(gridEvt
);
7007 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7010 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7012 if ( topRow
> bottomRow
)
7019 if ( leftCol
> rightCol
)
7026 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7027 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7029 if ( m_selectedTopLeft
!= updateTopLeft
||
7030 m_selectedBottomRight
!= updateBottomRight
)
7032 // Compute two optimal update rectangles:
7033 // Either one rectangle is a real subset of the
7034 // other, or they are (almost) disjoint!
7036 bool need_refresh
[4];
7040 need_refresh
[3] = FALSE
;
7043 // Store intermediate values
7044 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7045 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7046 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7047 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7049 // Determine the outer/inner coordinates.
7050 if (oldLeft
> leftCol
)
7056 if (oldTop
> topRow
)
7062 if (oldRight
< rightCol
)
7065 oldRight
= rightCol
;
7068 if (oldBottom
< bottomRow
)
7071 oldBottom
= bottomRow
;
7075 // Now, either the stuff marked old is the outer
7076 // rectangle or we don't have a situation where one
7077 // is contained in the other.
7079 if ( oldLeft
< leftCol
)
7081 need_refresh
[0] = TRUE
;
7082 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7084 wxGridCellCoords ( oldBottom
,
7088 if ( oldTop
< topRow
)
7090 need_refresh
[1] = TRUE
;
7091 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7093 wxGridCellCoords ( topRow
- 1,
7097 if ( oldRight
> rightCol
)
7099 need_refresh
[2] = TRUE
;
7100 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7102 wxGridCellCoords ( oldBottom
,
7106 if ( oldBottom
> bottomRow
)
7108 need_refresh
[3] = TRUE
;
7109 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7111 wxGridCellCoords ( oldBottom
,
7117 m_selectedTopLeft
= updateTopLeft
;
7118 m_selectedBottomRight
= updateBottomRight
;
7120 // various Refresh() calls
7121 for (i
= 0; i
< 4; i
++ )
7122 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7123 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7126 // only generate an event if the block is not being selected by
7127 // dragging the mouse (in which case the event will be generated in
7128 // the mouse event handler)
7129 if ( !m_isDragging
)
7131 wxGridRangeSelectEvent
gridEvt( GetId(),
7132 wxEVT_GRID_RANGE_SELECT
,
7135 m_selectedBottomRight
);
7137 GetEventHandler()->ProcessEvent(gridEvt
);
7141 void wxGrid::SelectAll()
7143 m_selectedTopLeft
.Set( 0, 0 );
7144 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7146 m_gridWin
->Refresh();
7150 void wxGrid::ClearSelection()
7152 m_selectedTopLeft
= wxGridNoCellCoords
;
7153 m_selectedBottomRight
= wxGridNoCellCoords
;
7157 // This function returns the rectangle that encloses the given block
7158 // in device coords clipped to the client size of the grid window.
7160 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7161 const wxGridCellCoords
&bottomRight
)
7163 wxRect
rect( wxGridNoCellRect
);
7166 cellRect
= CellToRect( topLeft
);
7167 if ( cellRect
!= wxGridNoCellRect
)
7173 rect
= wxRect( 0, 0, 0, 0 );
7176 cellRect
= CellToRect( bottomRight
);
7177 if ( cellRect
!= wxGridNoCellRect
)
7183 return wxGridNoCellRect
;
7186 // convert to scrolled coords
7188 int left
, top
, right
, bottom
;
7189 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7190 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7193 m_gridWin
->GetClientSize( &cw
, &ch
);
7195 rect
.SetLeft( wxMax(0, left
) );
7196 rect
.SetTop( wxMax(0, top
) );
7197 rect
.SetRight( wxMin(cw
, right
) );
7198 rect
.SetBottom( wxMin(ch
, bottom
) );
7206 // ------ Grid event classes
7209 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7211 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7212 int row
, int col
, int x
, int y
,
7213 bool control
, bool shift
, bool alt
, bool meta
)
7214 : wxNotifyEvent( type
, id
)
7220 m_control
= control
;
7225 SetEventObject(obj
);
7229 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7231 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7232 int rowOrCol
, int x
, int y
,
7233 bool control
, bool shift
, bool alt
, bool meta
)
7234 : wxNotifyEvent( type
, id
)
7236 m_rowOrCol
= rowOrCol
;
7239 m_control
= control
;
7244 SetEventObject(obj
);
7248 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7250 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7251 const wxGridCellCoords
& topLeft
,
7252 const wxGridCellCoords
& bottomRight
,
7253 bool control
, bool shift
, bool alt
, bool meta
)
7254 : wxNotifyEvent( type
, id
)
7256 m_topLeft
= topLeft
;
7257 m_bottomRight
= bottomRight
;
7258 m_control
= control
;
7263 SetEventObject(obj
);
7267 #endif // ifndef wxUSE_NEW_GRID