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
;
4961 m_gridWin
->GetClientSize(&cw
, &ch
);
4963 // virtual coords of visible area
4965 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4966 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4971 reg
.GetBox(x
, y
, w
, h
);
4972 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4973 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4976 // avoid drawing grid lines past the last row and col
4978 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
4979 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
4981 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4983 // horizontal grid lines
4986 for ( i
= 0; i
< m_numRows
; i
++ )
4988 int bot
= GetRowBottom(i
) - 1;
4997 dc
.DrawLine( left
, bot
, right
, bot
);
5002 // vertical grid lines
5004 for ( i
= 0; i
< m_numCols
; i
++ )
5006 int colRight
= GetColRight(i
) - 1;
5007 if ( colRight
> right
)
5012 if ( colRight
>= left
)
5014 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5020 void wxGrid::DrawRowLabels( wxDC
& dc
)
5022 if ( !m_numRows
|| !m_numCols
) return;
5025 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5027 for ( i
= 0; i
< numLabels
; i
++ )
5029 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5034 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5036 if ( GetRowHeight(row
) <= 0 )
5039 int rowTop
= GetRowTop(row
),
5040 rowBottom
= GetRowBottom(row
) - 1;
5042 dc
.SetPen( *wxBLACK_PEN
);
5043 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5044 m_rowLabelWidth
-1, rowBottom
);
5046 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5048 dc
.SetPen( *wxWHITE_PEN
);
5049 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5050 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5052 dc
.SetBackgroundMode( wxTRANSPARENT
);
5053 dc
.SetTextForeground( GetLabelTextColour() );
5054 dc
.SetFont( GetLabelFont() );
5057 GetRowLabelAlignment( &hAlign
, &vAlign
);
5061 rect
.SetY( GetRowTop(row
) + 2 );
5062 rect
.SetWidth( m_rowLabelWidth
- 4 );
5063 rect
.SetHeight( GetRowHeight(row
) - 4 );
5064 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5068 void wxGrid::DrawColLabels( wxDC
& dc
)
5070 if ( !m_numRows
|| !m_numCols
) return;
5073 size_t numLabels
= m_colLabelsExposed
.GetCount();
5075 for ( i
= 0; i
< numLabels
; i
++ )
5077 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5082 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5084 if ( GetColWidth(col
) <= 0 )
5087 int colLeft
= GetColLeft(col
),
5088 colRight
= GetColRight(col
) - 1;
5090 dc
.SetPen( *wxBLACK_PEN
);
5091 dc
.DrawLine( colRight
, 0,
5092 colRight
, m_colLabelHeight
-1 );
5094 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5095 colRight
, m_colLabelHeight
-1 );
5097 dc
.SetPen( *wxWHITE_PEN
);
5098 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5099 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5101 dc
.SetBackgroundMode( wxTRANSPARENT
);
5102 dc
.SetTextForeground( GetLabelTextColour() );
5103 dc
.SetFont( GetLabelFont() );
5105 dc
.SetBackgroundMode( wxTRANSPARENT
);
5106 dc
.SetTextForeground( GetLabelTextColour() );
5107 dc
.SetFont( GetLabelFont() );
5110 GetColLabelAlignment( &hAlign
, &vAlign
);
5113 rect
.SetX( colLeft
+ 2 );
5115 rect
.SetWidth( GetColWidth(col
) - 4 );
5116 rect
.SetHeight( m_colLabelHeight
- 4 );
5117 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5121 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5122 const wxString
& value
,
5127 long textWidth
, textHeight
;
5128 long lineWidth
, lineHeight
;
5129 wxArrayString lines
;
5131 dc
.SetClippingRegion( rect
);
5132 StringToLines( value
, lines
);
5133 if ( lines
.GetCount() )
5135 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5136 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5139 switch ( horizAlign
)
5142 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5146 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5155 switch ( vertAlign
)
5158 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5162 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5171 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5173 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5178 dc
.DestroyClippingRegion();
5182 // Split multi line text up into an array of strings. Any existing
5183 // contents of the string array are preserved.
5185 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5189 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5190 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5192 while ( startPos
< (int)tVal
.Length() )
5194 pos
= tVal
.Mid(startPos
).Find( eol
);
5199 else if ( pos
== 0 )
5201 lines
.Add( wxEmptyString
);
5205 lines
.Add( value
.Mid(startPos
, pos
) );
5209 if ( startPos
< (int)value
.Length() )
5211 lines
.Add( value
.Mid( startPos
) );
5216 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5217 wxArrayString
& lines
,
5218 long *width
, long *height
)
5225 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5227 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5228 w
= wxMax( w
, lineW
);
5238 // ------ Edit control functions
5242 void wxGrid::EnableEditing( bool edit
)
5244 // TODO: improve this ?
5246 if ( edit
!= m_editable
)
5250 // FIXME IMHO this won't disable the edit control if edit == FALSE
5251 // because of the check in the beginning of
5252 // EnableCellEditControl() just below (VZ)
5253 EnableCellEditControl(m_editable
);
5258 void wxGrid::EnableCellEditControl( bool enable
)
5263 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5264 SetCurrentCell( 0, 0 );
5266 if ( enable
!= m_cellEditCtrlEnabled
)
5268 // TODO allow the app to Veto() this event?
5269 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5273 // this should be checked by the caller!
5274 wxASSERT_MSG( CanEnableCellControl(),
5275 _T("can't enable editing for this cell!") );
5277 // do it before ShowCellEditControl()
5278 m_cellEditCtrlEnabled
= enable
;
5280 SetEditControlValue();
5281 ShowCellEditControl();
5285 HideCellEditControl();
5286 SaveEditControlValue();
5288 // do it after HideCellEditControl()
5289 m_cellEditCtrlEnabled
= enable
;
5294 bool wxGrid::IsCurrentCellReadOnly() const
5297 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5298 bool readonly
= attr
->IsReadOnly();
5304 bool wxGrid::CanEnableCellControl() const
5306 return m_editable
&& !IsCurrentCellReadOnly();
5309 bool wxGrid::IsCellEditControlEnabled() const
5311 // the cell edit control might be disable for all cells or just for the
5312 // current one if it's read only
5313 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5316 void wxGrid::ShowCellEditControl()
5318 if ( IsCellEditControlEnabled() )
5320 if ( !IsVisible( m_currentCellCoords
) )
5326 wxRect rect
= CellToRect( m_currentCellCoords
);
5327 int row
= m_currentCellCoords
.GetRow();
5328 int col
= m_currentCellCoords
.GetCol();
5330 // convert to scrolled coords
5332 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5334 // done in PaintBackground()
5336 // erase the highlight and the cell contents because the editor
5337 // might not cover the entire cell
5338 wxClientDC
dc( m_gridWin
);
5340 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5341 dc
.SetPen(*wxTRANSPARENT_PEN
);
5342 dc
.DrawRectangle(rect
);
5345 // cell is shifted by one pixel
5349 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5350 wxGridCellEditor
* editor
= attr
->GetEditor(GetDefaultEditorForCell(row
, col
));
5351 if ( !editor
->IsCreated() )
5353 editor
->Create(m_gridWin
, -1,
5354 new wxGridCellEditorEvtHandler(this, editor
));
5357 editor
->SetSize( rect
);
5359 editor
->Show( TRUE
, attr
);
5360 editor
->BeginEdit(row
, col
, this);
5367 void wxGrid::HideCellEditControl()
5369 if ( IsCellEditControlEnabled() )
5371 int row
= m_currentCellCoords
.GetRow();
5372 int col
= m_currentCellCoords
.GetCol();
5374 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5375 attr
->GetEditor(GetDefaultEditorForCell(row
, col
))->Show( FALSE
);
5377 m_gridWin
->SetFocus();
5382 void wxGrid::SetEditControlValue( const wxString
& value
)
5384 // RD: The new Editors get the value from the table themselves now. This
5385 // method can probably be removed...
5389 void wxGrid::SaveEditControlValue()
5391 if ( IsCellEditControlEnabled() )
5393 int row
= m_currentCellCoords
.GetRow();
5394 int col
= m_currentCellCoords
.GetCol();
5396 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5397 wxGridCellEditor
* editor
= attr
->GetEditor(GetDefaultEditorForCell(row
, col
));
5398 bool changed
= editor
->EndEdit(row
, col
, TRUE
, this);
5404 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5405 m_currentCellCoords
.GetRow(),
5406 m_currentCellCoords
.GetCol() );
5413 // ------ Grid location functions
5414 // Note that all of these functions work with the logical coordinates of
5415 // grid cells and labels so you will need to convert from device
5416 // coordinates for mouse events etc.
5419 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5421 int row
= YToRow(y
);
5422 int col
= XToCol(x
);
5424 if ( row
== -1 || col
== -1 )
5426 coords
= wxGridNoCellCoords
;
5430 coords
.Set( row
, col
);
5435 int wxGrid::YToRow( int y
)
5439 for ( i
= 0; i
< m_numRows
; i
++ )
5441 if ( y
< GetRowBottom(i
) )
5449 int wxGrid::XToCol( int x
)
5453 for ( i
= 0; i
< m_numCols
; i
++ )
5455 if ( x
< GetColRight(i
) )
5463 // return the row number that that the y coord is near the edge of, or
5464 // -1 if not near an edge
5466 int wxGrid::YToEdgeOfRow( int y
)
5470 for ( i
= 0; i
< m_numRows
; i
++ )
5472 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5474 d
= abs( y
- GetRowBottom(i
) );
5475 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5484 // return the col number that that the x coord is near the edge of, or
5485 // -1 if not near an edge
5487 int wxGrid::XToEdgeOfCol( int x
)
5491 for ( i
= 0; i
< m_numCols
; i
++ )
5493 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5495 d
= abs( x
- GetColRight(i
) );
5496 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5505 wxRect
wxGrid::CellToRect( int row
, int col
)
5507 wxRect
rect( -1, -1, -1, -1 );
5509 if ( row
>= 0 && row
< m_numRows
&&
5510 col
>= 0 && col
< m_numCols
)
5512 rect
.x
= GetColLeft(col
);
5513 rect
.y
= GetRowTop(row
);
5514 rect
.width
= GetColWidth(col
);
5515 rect
.height
= GetRowHeight(row
);
5522 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5524 // get the cell rectangle in logical coords
5526 wxRect
r( CellToRect( row
, col
) );
5528 // convert to device coords
5530 int left
, top
, right
, bottom
;
5531 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5532 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5534 // check against the client area of the grid window
5537 m_gridWin
->GetClientSize( &cw
, &ch
);
5539 if ( wholeCellVisible
)
5541 // is the cell wholly visible ?
5543 return ( left
>= 0 && right
<= cw
&&
5544 top
>= 0 && bottom
<= ch
);
5548 // is the cell partly visible ?
5550 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5551 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5556 // make the specified cell location visible by doing a minimal amount
5559 void wxGrid::MakeCellVisible( int row
, int col
)
5562 int xpos
= -1, ypos
= -1;
5564 if ( row
>= 0 && row
< m_numRows
&&
5565 col
>= 0 && col
< m_numCols
)
5567 // get the cell rectangle in logical coords
5569 wxRect
r( CellToRect( row
, col
) );
5571 // convert to device coords
5573 int left
, top
, right
, bottom
;
5574 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5575 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5578 m_gridWin
->GetClientSize( &cw
, &ch
);
5584 else if ( bottom
> ch
)
5586 int h
= r
.GetHeight();
5588 for ( i
= row
-1; i
>= 0; i
-- )
5590 int rowHeight
= GetRowHeight(i
);
5591 if ( h
+ rowHeight
> ch
)
5598 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5599 // have rounding errors (this is important, because if we do, we
5600 // might not scroll at all and some cells won't be redrawn)
5601 ypos
+= GRID_SCROLL_LINE
/ 2;
5608 else if ( right
> cw
)
5610 int w
= r
.GetWidth();
5612 for ( i
= col
-1; i
>= 0; i
-- )
5614 int colWidth
= GetColWidth(i
);
5615 if ( w
+ colWidth
> cw
)
5622 // see comment for ypos above
5623 xpos
+= GRID_SCROLL_LINE
/ 2;
5626 if ( xpos
!= -1 || ypos
!= -1 )
5628 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5629 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5630 Scroll( xpos
, ypos
);
5638 // ------ Grid cursor movement functions
5641 bool wxGrid::MoveCursorUp()
5643 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5644 m_currentCellCoords
.GetRow() > 0 )
5646 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5647 m_currentCellCoords
.GetCol() );
5649 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5650 m_currentCellCoords
.GetCol() );
5659 bool wxGrid::MoveCursorDown()
5661 // TODO: allow for scrolling
5663 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5664 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5666 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5667 m_currentCellCoords
.GetCol() );
5669 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5670 m_currentCellCoords
.GetCol() );
5679 bool wxGrid::MoveCursorLeft()
5681 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5682 m_currentCellCoords
.GetCol() > 0 )
5684 MakeCellVisible( m_currentCellCoords
.GetRow(),
5685 m_currentCellCoords
.GetCol() - 1 );
5687 SetCurrentCell( m_currentCellCoords
.GetRow(),
5688 m_currentCellCoords
.GetCol() - 1 );
5697 bool wxGrid::MoveCursorRight()
5699 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5700 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5702 MakeCellVisible( m_currentCellCoords
.GetRow(),
5703 m_currentCellCoords
.GetCol() + 1 );
5705 SetCurrentCell( m_currentCellCoords
.GetRow(),
5706 m_currentCellCoords
.GetCol() + 1 );
5715 bool wxGrid::MovePageUp()
5717 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5719 int row
= m_currentCellCoords
.GetRow();
5723 m_gridWin
->GetClientSize( &cw
, &ch
);
5725 int y
= GetRowTop(row
);
5726 int newRow
= YToRow( y
- ch
+ 1 );
5731 else if ( newRow
== row
)
5736 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5737 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5745 bool wxGrid::MovePageDown()
5747 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5749 int row
= m_currentCellCoords
.GetRow();
5750 if ( row
< m_numRows
)
5753 m_gridWin
->GetClientSize( &cw
, &ch
);
5755 int y
= GetRowTop(row
);
5756 int newRow
= YToRow( y
+ ch
);
5759 newRow
= m_numRows
- 1;
5761 else if ( newRow
== row
)
5766 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5767 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5775 bool wxGrid::MoveCursorUpBlock()
5778 m_currentCellCoords
!= wxGridNoCellCoords
&&
5779 m_currentCellCoords
.GetRow() > 0 )
5781 int row
= m_currentCellCoords
.GetRow();
5782 int col
= m_currentCellCoords
.GetCol();
5784 if ( m_table
->IsEmptyCell(row
, col
) )
5786 // starting in an empty cell: find the next block of
5792 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5795 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5797 // starting at the top of a block: find the next block
5803 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5808 // starting within a block: find the top of the block
5813 if ( m_table
->IsEmptyCell(row
, col
) )
5821 MakeCellVisible( row
, col
);
5822 SetCurrentCell( row
, col
);
5830 bool wxGrid::MoveCursorDownBlock()
5833 m_currentCellCoords
!= wxGridNoCellCoords
&&
5834 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5836 int row
= m_currentCellCoords
.GetRow();
5837 int col
= m_currentCellCoords
.GetCol();
5839 if ( m_table
->IsEmptyCell(row
, col
) )
5841 // starting in an empty cell: find the next block of
5844 while ( row
< m_numRows
-1 )
5847 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5850 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5852 // starting at the bottom of a block: find the next block
5855 while ( row
< m_numRows
-1 )
5858 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5863 // starting within a block: find the bottom of the block
5865 while ( row
< m_numRows
-1 )
5868 if ( m_table
->IsEmptyCell(row
, col
) )
5876 MakeCellVisible( row
, col
);
5877 SetCurrentCell( row
, col
);
5885 bool wxGrid::MoveCursorLeftBlock()
5888 m_currentCellCoords
!= wxGridNoCellCoords
&&
5889 m_currentCellCoords
.GetCol() > 0 )
5891 int row
= m_currentCellCoords
.GetRow();
5892 int col
= m_currentCellCoords
.GetCol();
5894 if ( m_table
->IsEmptyCell(row
, col
) )
5896 // starting in an empty cell: find the next block of
5902 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5905 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5907 // starting at the left of a block: find the next block
5913 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5918 // starting within a block: find the left of the block
5923 if ( m_table
->IsEmptyCell(row
, col
) )
5931 MakeCellVisible( row
, col
);
5932 SetCurrentCell( row
, col
);
5940 bool wxGrid::MoveCursorRightBlock()
5943 m_currentCellCoords
!= wxGridNoCellCoords
&&
5944 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5946 int row
= m_currentCellCoords
.GetRow();
5947 int col
= m_currentCellCoords
.GetCol();
5949 if ( m_table
->IsEmptyCell(row
, col
) )
5951 // starting in an empty cell: find the next block of
5954 while ( col
< m_numCols
-1 )
5957 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5960 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5962 // starting at the right of a block: find the next block
5965 while ( col
< m_numCols
-1 )
5968 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5973 // starting within a block: find the right of the block
5975 while ( col
< m_numCols
-1 )
5978 if ( m_table
->IsEmptyCell(row
, col
) )
5986 MakeCellVisible( row
, col
);
5987 SetCurrentCell( row
, col
);
5998 // ------ Label values and formatting
6001 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6003 *horiz
= m_rowLabelHorizAlign
;
6004 *vert
= m_rowLabelVertAlign
;
6007 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6009 *horiz
= m_colLabelHorizAlign
;
6010 *vert
= m_colLabelVertAlign
;
6013 wxString
wxGrid::GetRowLabelValue( int row
)
6017 return m_table
->GetRowLabelValue( row
);
6027 wxString
wxGrid::GetColLabelValue( int col
)
6031 return m_table
->GetColLabelValue( col
);
6042 void wxGrid::SetRowLabelSize( int width
)
6044 width
= wxMax( width
, 0 );
6045 if ( width
!= m_rowLabelWidth
)
6049 m_rowLabelWin
->Show( FALSE
);
6050 m_cornerLabelWin
->Show( FALSE
);
6052 else if ( m_rowLabelWidth
== 0 )
6054 m_rowLabelWin
->Show( TRUE
);
6055 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6058 m_rowLabelWidth
= width
;
6065 void wxGrid::SetColLabelSize( int height
)
6067 height
= wxMax( height
, 0 );
6068 if ( height
!= m_colLabelHeight
)
6072 m_colLabelWin
->Show( FALSE
);
6073 m_cornerLabelWin
->Show( FALSE
);
6075 else if ( m_colLabelHeight
== 0 )
6077 m_colLabelWin
->Show( TRUE
);
6078 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6081 m_colLabelHeight
= height
;
6088 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6090 if ( m_labelBackgroundColour
!= colour
)
6092 m_labelBackgroundColour
= colour
;
6093 m_rowLabelWin
->SetBackgroundColour( colour
);
6094 m_colLabelWin
->SetBackgroundColour( colour
);
6095 m_cornerLabelWin
->SetBackgroundColour( colour
);
6097 if ( !GetBatchCount() )
6099 m_rowLabelWin
->Refresh();
6100 m_colLabelWin
->Refresh();
6101 m_cornerLabelWin
->Refresh();
6106 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6108 if ( m_labelTextColour
!= colour
)
6110 m_labelTextColour
= colour
;
6111 if ( !GetBatchCount() )
6113 m_rowLabelWin
->Refresh();
6114 m_colLabelWin
->Refresh();
6119 void wxGrid::SetLabelFont( const wxFont
& font
)
6122 if ( !GetBatchCount() )
6124 m_rowLabelWin
->Refresh();
6125 m_colLabelWin
->Refresh();
6129 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6131 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6133 m_rowLabelHorizAlign
= horiz
;
6136 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6138 m_rowLabelVertAlign
= vert
;
6141 if ( !GetBatchCount() )
6143 m_rowLabelWin
->Refresh();
6147 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6149 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6151 m_colLabelHorizAlign
= horiz
;
6154 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6156 m_colLabelVertAlign
= vert
;
6159 if ( !GetBatchCount() )
6161 m_colLabelWin
->Refresh();
6165 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6169 m_table
->SetRowLabelValue( row
, s
);
6170 if ( !GetBatchCount() )
6172 wxRect rect
= CellToRect( row
, 0);
6173 if ( rect
.height
> 0 )
6175 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6177 rect
.width
= m_rowLabelWidth
;
6178 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6184 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6188 m_table
->SetColLabelValue( col
, s
);
6189 if ( !GetBatchCount() )
6191 wxRect rect
= CellToRect( 0, col
);
6192 if ( rect
.width
> 0 )
6194 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6196 rect
.height
= m_colLabelHeight
;
6197 m_colLabelWin
->Refresh( TRUE
, &rect
);
6203 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6205 if ( m_gridLineColour
!= colour
)
6207 m_gridLineColour
= colour
;
6209 wxClientDC
dc( m_gridWin
);
6211 DrawAllGridLines( dc
, wxRegion() );
6215 void wxGrid::EnableGridLines( bool enable
)
6217 if ( enable
!= m_gridLinesEnabled
)
6219 m_gridLinesEnabled
= enable
;
6221 if ( !GetBatchCount() )
6225 wxClientDC
dc( m_gridWin
);
6227 DrawAllGridLines( dc
, wxRegion() );
6231 m_gridWin
->Refresh();
6238 int wxGrid::GetDefaultRowSize()
6240 return m_defaultRowHeight
;
6243 int wxGrid::GetRowSize( int row
)
6245 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6247 return GetRowHeight(row
);
6250 int wxGrid::GetDefaultColSize()
6252 return m_defaultColWidth
;
6255 int wxGrid::GetColSize( int col
)
6257 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6259 return GetColWidth(col
);
6262 // ============================================================================
6263 // access to the grid attributes: each of them has a default value in the grid
6264 // itself and may be overidden on a per-cell basis
6265 // ============================================================================
6267 // ----------------------------------------------------------------------------
6268 // setting default attributes
6269 // ----------------------------------------------------------------------------
6271 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6273 m_defaultCellAttr
->SetBackgroundColour(col
);
6275 m_gridWin
->SetBackgroundColour(col
);
6279 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6281 m_defaultCellAttr
->SetTextColour(col
);
6284 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6286 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6289 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6291 m_defaultCellAttr
->SetFont(font
);
6294 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6296 m_defaultCellAttr
->SetRenderer(renderer
);
6299 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6301 m_defaultCellAttr
->SetEditor(editor
);
6304 // ----------------------------------------------------------------------------
6305 // access to the default attrbiutes
6306 // ----------------------------------------------------------------------------
6308 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6310 return m_defaultCellAttr
->GetBackgroundColour();
6313 wxColour
wxGrid::GetDefaultCellTextColour()
6315 return m_defaultCellAttr
->GetTextColour();
6318 wxFont
wxGrid::GetDefaultCellFont()
6320 return m_defaultCellAttr
->GetFont();
6323 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6325 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6328 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6330 return m_defaultCellAttr
->GetRenderer(NULL
);
6333 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6335 return m_defaultCellAttr
->GetEditor(NULL
);
6338 // ----------------------------------------------------------------------------
6339 // access to cell attributes
6340 // ----------------------------------------------------------------------------
6342 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6344 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6345 wxColour colour
= attr
->GetBackgroundColour();
6350 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6352 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6353 wxColour colour
= attr
->GetTextColour();
6358 wxFont
wxGrid::GetCellFont( int row
, int col
)
6360 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6361 wxFont font
= attr
->GetFont();
6366 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6368 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6369 attr
->GetAlignment(horiz
, vert
);
6373 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6375 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6376 wxGridCellRenderer
* renderer
= attr
->GetRenderer(GetDefaultRendererForCell(row
,col
));
6381 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6383 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6384 wxGridCellEditor
* editor
= attr
->GetEditor(GetDefaultEditorForCell(row
, col
));
6389 bool wxGrid::IsReadOnly(int row
, int col
) const
6391 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6392 bool isReadOnly
= attr
->IsReadOnly();
6397 // ----------------------------------------------------------------------------
6398 // attribute support: cache, automatic provider creation, ...
6399 // ----------------------------------------------------------------------------
6401 bool wxGrid::CanHaveAttributes()
6408 return m_table
->CanHaveAttributes();
6411 void wxGrid::ClearAttrCache()
6413 if ( m_attrCache
.row
!= -1 )
6415 m_attrCache
.attr
->SafeDecRef();
6416 m_attrCache
.row
= -1;
6420 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6422 wxGrid
*self
= (wxGrid
*)this; // const_cast
6424 self
->ClearAttrCache();
6425 self
->m_attrCache
.row
= row
;
6426 self
->m_attrCache
.col
= col
;
6427 self
->m_attrCache
.attr
= attr
;
6431 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6433 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6435 *attr
= m_attrCache
.attr
;
6436 (*attr
)->SafeIncRef();
6438 #ifdef DEBUG_ATTR_CACHE
6439 gs_nAttrCacheHits
++;
6446 #ifdef DEBUG_ATTR_CACHE
6447 gs_nAttrCacheMisses
++;
6453 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6455 wxGridCellAttr
*attr
;
6456 if ( !LookupAttr(row
, col
, &attr
) )
6458 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6459 CacheAttr(row
, col
, attr
);
6463 attr
->SetDefAttr(m_defaultCellAttr
);
6467 attr
= m_defaultCellAttr
;
6474 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6476 wxGridCellAttr
*attr
;
6477 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6479 wxASSERT_MSG( m_table
,
6480 _T("we may only be called if CanHaveAttributes() "
6481 "returned TRUE and then m_table should be !NULL") );
6483 attr
= m_table
->GetAttr(row
, col
);
6486 attr
= new wxGridCellAttr
;
6488 // artificially inc the ref count to match DecRef() in caller
6491 m_table
->SetAttr(attr
, row
, col
);
6494 CacheAttr(row
, col
, attr
);
6496 attr
->SetDefAttr(m_defaultCellAttr
);
6500 // ----------------------------------------------------------------------------
6501 // setting cell attributes: this is forwarded to the table
6502 // ----------------------------------------------------------------------------
6504 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6506 if ( CanHaveAttributes() )
6508 m_table
->SetRowAttr(attr
, row
);
6516 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6518 if ( CanHaveAttributes() )
6520 m_table
->SetColAttr(attr
, col
);
6528 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6530 if ( CanHaveAttributes() )
6532 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6533 attr
->SetBackgroundColour(colour
);
6538 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6540 if ( CanHaveAttributes() )
6542 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6543 attr
->SetTextColour(colour
);
6548 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6550 if ( CanHaveAttributes() )
6552 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6553 attr
->SetFont(font
);
6558 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6560 if ( CanHaveAttributes() )
6562 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6563 attr
->SetAlignment(horiz
, vert
);
6568 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6570 if ( CanHaveAttributes() )
6572 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6573 attr
->SetRenderer(renderer
);
6578 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6580 if ( CanHaveAttributes() )
6582 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6583 attr
->SetEditor(editor
);
6588 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6590 if ( CanHaveAttributes() )
6592 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6593 attr
->SetReadOnly(isReadOnly
);
6598 // ----------------------------------------------------------------------------
6599 // Data type registration
6600 // ----------------------------------------------------------------------------
6602 void wxGrid::RegisterDataType(const wxString
& typeName
,
6603 wxGridCellRenderer
* renderer
,
6604 wxGridCellEditor
* editor
)
6606 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6610 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6612 wxString typeName
= m_table
->GetTypeName(row
, col
);
6613 return GetDefaultEditorForType(typeName
);
6616 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6618 wxString typeName
= m_table
->GetTypeName(row
, col
);
6619 return GetDefaultRendererForType(typeName
);
6623 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6625 int index
= m_typeRegistry
->FindDataType(typeName
);
6627 // Should we force the failure here or let it fallback to string handling???
6628 // wxFAIL_MSG(wxT("Unknown data type name"));
6631 return m_typeRegistry
->GetEditor(index
);
6635 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6637 int index
= m_typeRegistry
->FindDataType(typeName
);
6639 // Should we force the failure here or let it fallback to string handling???
6640 // wxFAIL_MSG(wxT("Unknown data type name"));
6643 return m_typeRegistry
->GetRenderer(index
);
6647 // ----------------------------------------------------------------------------
6649 // ----------------------------------------------------------------------------
6651 void wxGrid::EnableDragRowSize( bool enable
)
6653 m_canDragRowSize
= enable
;
6657 void wxGrid::EnableDragColSize( bool enable
)
6659 m_canDragColSize
= enable
;
6663 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6665 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6667 if ( resizeExistingRows
)
6675 void wxGrid::SetRowSize( int row
, int height
)
6677 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6679 if ( m_rowHeights
.IsEmpty() )
6681 // need to really create the array
6685 int h
= wxMax( 0, height
);
6686 int diff
= h
- m_rowHeights
[row
];
6688 m_rowHeights
[row
] = h
;
6690 for ( i
= row
; i
< m_numRows
; i
++ )
6692 m_rowBottoms
[i
] += diff
;
6697 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6699 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6701 if ( resizeExistingCols
)
6709 void wxGrid::SetColSize( int col
, int width
)
6711 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6713 // should we check that it's bigger than GetColMinimalWidth(col) here?
6715 if ( m_colWidths
.IsEmpty() )
6717 // need to really create the array
6721 int w
= wxMax( 0, width
);
6722 int diff
= w
- m_colWidths
[col
];
6723 m_colWidths
[col
] = w
;
6726 for ( i
= col
; i
< m_numCols
; i
++ )
6728 m_colRights
[i
] += diff
;
6734 void wxGrid::SetColMinimalWidth( int col
, int width
)
6736 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6739 int wxGrid::GetColMinimalWidth(int col
) const
6741 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6742 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6745 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6747 wxClientDC
dc(m_gridWin
);
6749 wxCoord width
, widthMax
= 0;
6750 for ( int row
= 0; row
< m_numRows
; row
++ )
6752 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6753 wxGridCellRenderer
* renderer
= attr
->GetRenderer(GetDefaultRendererForCell(row
,col
));
6756 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6757 if ( width
> widthMax
)
6766 // now also compare with the column label width
6767 dc
.SetFont( GetLabelFont() );
6768 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6769 if ( width
> widthMax
)
6776 // empty column - give default width (notice that if widthMax is less
6777 // than default width but != 0, it's ok)
6778 widthMax
= m_defaultColWidth
;
6782 // leave some space around text
6786 SetColSize(col
, widthMax
);
6789 SetColMinimalWidth(col
, widthMax
);
6793 void wxGrid::AutoSizeColumns( bool setAsMin
)
6795 for ( int col
= 0; col
< m_numCols
; col
++ )
6797 AutoSizeColumn(col
, setAsMin
);
6802 // ------ cell value accessor functions
6805 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6809 m_table
->SetValue( row
, col
, s
.c_str() );
6810 if ( !GetBatchCount() )
6812 wxClientDC
dc( m_gridWin
);
6814 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6817 #if 0 // TODO: edit in place
6819 if ( m_currentCellCoords
.GetRow() == row
&&
6820 m_currentCellCoords
.GetCol() == col
)
6822 SetEditControlValue( s
);
6831 // ------ Block, row and col selection
6834 void wxGrid::SelectRow( int row
, bool addToSelected
)
6838 if ( IsSelection() && addToSelected
)
6841 bool need_refresh
[4];
6845 need_refresh
[3] = FALSE
;
6849 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6850 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6851 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6852 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6856 need_refresh
[0] = TRUE
;
6857 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6858 wxGridCellCoords ( oldTop
- 1,
6860 m_selectedTopLeft
.SetRow( row
);
6865 need_refresh
[1] = TRUE
;
6866 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6867 wxGridCellCoords ( oldBottom
,
6870 m_selectedTopLeft
.SetCol( 0 );
6873 if ( oldBottom
< row
)
6875 need_refresh
[2] = TRUE
;
6876 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6877 wxGridCellCoords ( row
,
6879 m_selectedBottomRight
.SetRow( row
);
6882 if ( oldRight
< m_numCols
- 1 )
6884 need_refresh
[3] = TRUE
;
6885 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6887 wxGridCellCoords ( oldBottom
,
6889 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6892 for (i
= 0; i
< 4; i
++ )
6893 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6894 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6898 r
= SelectionToDeviceRect();
6900 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6902 m_selectedTopLeft
.Set( row
, 0 );
6903 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6904 r
= SelectionToDeviceRect();
6905 m_gridWin
->Refresh( FALSE
, &r
);
6908 wxGridRangeSelectEvent
gridEvt( GetId(),
6909 wxEVT_GRID_RANGE_SELECT
,
6912 m_selectedBottomRight
);
6914 GetEventHandler()->ProcessEvent(gridEvt
);
6918 void wxGrid::SelectCol( int col
, bool addToSelected
)
6920 if ( IsSelection() && addToSelected
)
6923 bool need_refresh
[4];
6927 need_refresh
[3] = FALSE
;
6930 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6931 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6932 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6933 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6935 if ( oldLeft
> col
)
6937 need_refresh
[0] = TRUE
;
6938 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6939 wxGridCellCoords ( m_numRows
- 1,
6941 m_selectedTopLeft
.SetCol( col
);
6946 need_refresh
[1] = TRUE
;
6947 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6948 wxGridCellCoords ( oldTop
- 1,
6950 m_selectedTopLeft
.SetRow( 0 );
6953 if ( oldRight
< col
)
6955 need_refresh
[2] = TRUE
;
6956 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6957 wxGridCellCoords ( m_numRows
- 1,
6959 m_selectedBottomRight
.SetCol( col
);
6962 if ( oldBottom
< m_numRows
- 1 )
6964 need_refresh
[3] = TRUE
;
6965 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6967 wxGridCellCoords ( m_numRows
- 1,
6969 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6972 for (i
= 0; i
< 4; i
++ )
6973 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6974 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6980 r
= SelectionToDeviceRect();
6982 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6984 m_selectedTopLeft
.Set( 0, col
);
6985 m_selectedBottomRight
.Set( m_numRows
-1, col
);
6986 r
= SelectionToDeviceRect();
6987 m_gridWin
->Refresh( FALSE
, &r
);
6990 wxGridRangeSelectEvent
gridEvt( GetId(),
6991 wxEVT_GRID_RANGE_SELECT
,
6994 m_selectedBottomRight
);
6996 GetEventHandler()->ProcessEvent(gridEvt
);
7000 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7003 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7005 if ( topRow
> bottomRow
)
7012 if ( leftCol
> rightCol
)
7019 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7020 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7022 if ( m_selectedTopLeft
!= updateTopLeft
||
7023 m_selectedBottomRight
!= updateBottomRight
)
7025 // Compute two optimal update rectangles:
7026 // Either one rectangle is a real subset of the
7027 // other, or they are (almost) disjoint!
7029 bool need_refresh
[4];
7033 need_refresh
[3] = FALSE
;
7036 // Store intermediate values
7037 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7038 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7039 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7040 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7042 // Determine the outer/inner coordinates.
7043 if (oldLeft
> leftCol
)
7049 if (oldTop
> topRow
)
7055 if (oldRight
< rightCol
)
7058 oldRight
= rightCol
;
7061 if (oldBottom
< bottomRow
)
7064 oldBottom
= bottomRow
;
7068 // Now, either the stuff marked old is the outer
7069 // rectangle or we don't have a situation where one
7070 // is contained in the other.
7072 if ( oldLeft
< leftCol
)
7074 need_refresh
[0] = TRUE
;
7075 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7077 wxGridCellCoords ( oldBottom
,
7081 if ( oldTop
< topRow
)
7083 need_refresh
[1] = TRUE
;
7084 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7086 wxGridCellCoords ( topRow
- 1,
7090 if ( oldRight
> rightCol
)
7092 need_refresh
[2] = TRUE
;
7093 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7095 wxGridCellCoords ( oldBottom
,
7099 if ( oldBottom
> bottomRow
)
7101 need_refresh
[3] = TRUE
;
7102 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7104 wxGridCellCoords ( oldBottom
,
7110 m_selectedTopLeft
= updateTopLeft
;
7111 m_selectedBottomRight
= updateBottomRight
;
7113 // various Refresh() calls
7114 for (i
= 0; i
< 4; i
++ )
7115 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7116 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7119 // only generate an event if the block is not being selected by
7120 // dragging the mouse (in which case the event will be generated in
7121 // the mouse event handler)
7122 if ( !m_isDragging
)
7124 wxGridRangeSelectEvent
gridEvt( GetId(),
7125 wxEVT_GRID_RANGE_SELECT
,
7128 m_selectedBottomRight
);
7130 GetEventHandler()->ProcessEvent(gridEvt
);
7134 void wxGrid::SelectAll()
7136 m_selectedTopLeft
.Set( 0, 0 );
7137 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7139 m_gridWin
->Refresh();
7143 void wxGrid::ClearSelection()
7145 m_selectedTopLeft
= wxGridNoCellCoords
;
7146 m_selectedBottomRight
= wxGridNoCellCoords
;
7150 // This function returns the rectangle that encloses the given block
7151 // in device coords clipped to the client size of the grid window.
7153 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7154 const wxGridCellCoords
&bottomRight
)
7156 wxRect
rect( wxGridNoCellRect
);
7159 cellRect
= CellToRect( topLeft
);
7160 if ( cellRect
!= wxGridNoCellRect
)
7166 rect
= wxRect( 0, 0, 0, 0 );
7169 cellRect
= CellToRect( bottomRight
);
7170 if ( cellRect
!= wxGridNoCellRect
)
7176 return wxGridNoCellRect
;
7179 // convert to scrolled coords
7181 int left
, top
, right
, bottom
;
7182 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7183 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7186 m_gridWin
->GetClientSize( &cw
, &ch
);
7188 rect
.SetLeft( wxMax(0, left
) );
7189 rect
.SetTop( wxMax(0, top
) );
7190 rect
.SetRight( wxMin(cw
, right
) );
7191 rect
.SetBottom( wxMin(ch
, bottom
) );
7199 // ------ Grid event classes
7202 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7204 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7205 int row
, int col
, int x
, int y
,
7206 bool control
, bool shift
, bool alt
, bool meta
)
7207 : wxNotifyEvent( type
, id
)
7213 m_control
= control
;
7218 SetEventObject(obj
);
7222 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7224 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7225 int rowOrCol
, int x
, int y
,
7226 bool control
, bool shift
, bool alt
, bool meta
)
7227 : wxNotifyEvent( type
, id
)
7229 m_rowOrCol
= rowOrCol
;
7232 m_control
= control
;
7237 SetEventObject(obj
);
7241 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7243 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7244 const wxGridCellCoords
& topLeft
,
7245 const wxGridCellCoords
& bottomRight
,
7246 bool control
, bool shift
, bool alt
, bool meta
)
7247 : wxNotifyEvent( type
, id
)
7249 m_topLeft
= topLeft
;
7250 m_bottomRight
= bottomRight
;
7251 m_control
= control
;
7256 SetEventObject(obj
);
7260 #endif // ifndef wxUSE_NEW_GRID