1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
59 struct wxGridCellWithAttr
61 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
62 : coords(row
, col
), attr(attr_
)
71 wxGridCellCoords coords
;
75 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
77 #include "wx/arrimpl.cpp"
79 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
80 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
89 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
90 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
91 const wxPoint
&pos
, const wxSize
&size
);
96 void OnPaint( wxPaintEvent
& event
);
97 void OnMouseEvent( wxMouseEvent
& event
);
98 void OnKeyDown( wxKeyEvent
& event
);
100 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
108 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
109 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
110 const wxPoint
&pos
, const wxSize
&size
);
115 void OnPaint( wxPaintEvent
&event
);
116 void OnMouseEvent( wxMouseEvent
& event
);
117 void OnKeyDown( wxKeyEvent
& event
);
119 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
120 DECLARE_EVENT_TABLE()
124 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
127 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
128 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnKeyDown( wxKeyEvent
& event
);
136 void OnPaint( wxPaintEvent
& event
);
138 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
139 DECLARE_EVENT_TABLE()
142 class WXDLLEXPORT wxGridWindow
: public wxPanel
147 m_owner
= (wxGrid
*)NULL
;
148 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
149 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
152 wxGridWindow( wxGrid
*parent
,
153 wxGridRowLabelWindow
*rowLblWin
,
154 wxGridColLabelWindow
*colLblWin
,
155 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
158 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
162 wxGridRowLabelWindow
*m_rowLabelWin
;
163 wxGridColLabelWindow
*m_colLabelWin
;
165 void OnPaint( wxPaintEvent
&event
);
166 void OnMouseEvent( wxMouseEvent
& event
);
167 void OnKeyDown( wxKeyEvent
& );
168 void OnEraseBackground( wxEraseEvent
& );
171 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
172 DECLARE_EVENT_TABLE()
177 class wxGridCellEditorEvtHandler
: public wxEvtHandler
180 wxGridCellEditorEvtHandler()
181 : m_grid(0), m_editor(0)
183 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
184 : m_grid(grid
), m_editor(editor
)
187 void OnKeyDown(wxKeyEvent
& event
);
188 void OnChar(wxKeyEvent
& event
);
192 wxGridCellEditor
* m_editor
;
193 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
194 DECLARE_EVENT_TABLE()
198 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
199 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
200 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
201 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
206 // ----------------------------------------------------------------------------
207 // the internal data representation used by wxGridCellAttrProvider
208 // ----------------------------------------------------------------------------
210 // this class stores attributes set for cells
211 class WXDLLEXPORT wxGridCellAttrData
214 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
215 wxGridCellAttr
*GetAttr(int row
, int col
) const;
216 void UpdateAttrRows( size_t pos
, int numRows
);
217 void UpdateAttrCols( size_t pos
, int numCols
);
220 // searches for the attr for given cell, returns wxNOT_FOUND if not found
221 int FindIndex(int row
, int col
) const;
223 wxGridCellWithAttrArray m_attrs
;
226 // this class stores attributes set for rows or columns
227 class WXDLLEXPORT wxGridRowOrColAttrData
230 // empty ctor to suppress warnings
231 wxGridRowOrColAttrData() { }
232 ~wxGridRowOrColAttrData();
234 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
235 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
236 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
239 wxArrayInt m_rowsOrCols
;
240 wxArrayAttrs m_attrs
;
243 // NB: this is just a wrapper around 3 objects: one which stores cell
244 // attributes, and 2 others for row/col ones
245 class WXDLLEXPORT wxGridCellAttrProviderData
248 wxGridCellAttrData m_cellAttrs
;
249 wxGridRowOrColAttrData m_rowAttrs
,
254 // ----------------------------------------------------------------------------
255 // data structures used for the data type registry
256 // ----------------------------------------------------------------------------
258 struct wxGridDataTypeInfo
{
259 wxGridDataTypeInfo(const wxString
& typeName
,
260 wxGridCellRenderer
* renderer
,
261 wxGridCellEditor
* editor
)
262 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
265 ~wxGridDataTypeInfo() { delete m_renderer
; delete m_editor
; }
268 wxGridCellRenderer
* m_renderer
;
269 wxGridCellEditor
* m_editor
;
273 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
276 class WXDLLEXPORT wxGridTypeRegistry
{
278 ~wxGridTypeRegistry();
279 void RegisterDataType(const wxString
& typeName
,
280 wxGridCellRenderer
* renderer
,
281 wxGridCellEditor
* editor
);
282 int FindDataType(const wxString
& typeName
);
283 wxGridCellRenderer
* GetRenderer(int index
);
284 wxGridCellEditor
* GetEditor(int index
);
287 wxGridDataTypeInfoArray m_typeinfo
;
293 // ----------------------------------------------------------------------------
294 // conditional compilation
295 // ----------------------------------------------------------------------------
297 #ifndef WXGRID_DRAW_LINES
298 #define WXGRID_DRAW_LINES 1
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 //#define DEBUG_ATTR_CACHE
306 #ifdef DEBUG_ATTR_CACHE
307 static size_t gs_nAttrCacheHits
= 0;
308 static size_t gs_nAttrCacheMisses
= 0;
309 #endif // DEBUG_ATTR_CACHE
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
316 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
319 // TODO: fixed so far - make configurable later (and also different for x/y)
320 static const size_t GRID_SCROLL_LINE
= 10;
322 // the size of hash tables used a bit everywhere (the max number of elements
323 // in these hash tables is the number of rows/columns)
324 static const int GRID_HASH_SIZE
= 100;
326 // ============================================================================
328 // ============================================================================
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 wxGridCellEditor::wxGridCellEditor()
340 wxGridCellEditor::~wxGridCellEditor()
345 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
346 wxWindowID
WXUNUSED(id
),
347 wxEvtHandler
* evtHandler
)
350 m_control
->PushEventHandler(evtHandler
);
353 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
354 wxGridCellAttr
*attr
)
356 // erase the background because we might not fill the cell
357 wxClientDC
dc(m_control
->GetParent());
358 dc
.SetPen(*wxTRANSPARENT_PEN
);
359 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
360 dc
.DrawRectangle(rectCell
);
362 // redraw the control we just painted over
363 m_control
->Refresh();
366 void wxGridCellEditor::Destroy()
370 m_control
->Destroy();
375 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
377 wxASSERT_MSG(m_control
,
378 wxT("The wxGridCellEditor must be Created first!"));
379 m_control
->Show(show
);
383 // set the colours/fonts if we have any
386 m_colFgOld
= m_control
->GetForegroundColour();
387 m_control
->SetForegroundColour(attr
->GetTextColour());
389 m_colBgOld
= m_control
->GetBackgroundColour();
390 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
392 m_fontOld
= m_control
->GetFont();
393 m_control
->SetFont(attr
->GetFont());
395 // can't do anything more in the base class version, the other
396 // attributes may only be used by the derived classes
401 // restore the standard colours fonts
402 if ( m_colFgOld
.Ok() )
404 m_control
->SetForegroundColour(m_colFgOld
);
405 m_colFgOld
= wxNullColour
;
408 if ( m_colBgOld
.Ok() )
410 m_control
->SetBackgroundColour(m_colBgOld
);
411 m_colBgOld
= wxNullColour
;
414 if ( m_fontOld
.Ok() )
416 m_control
->SetFont(m_fontOld
);
417 m_fontOld
= wxNullFont
;
422 void wxGridCellEditor::SetSize(const wxRect
& rect
)
424 wxASSERT_MSG(m_control
,
425 wxT("The wxGridCellEditor must be Created first!"));
426 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
429 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
435 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
440 void wxGridCellEditor::StartingClick()
444 // ----------------------------------------------------------------------------
445 // wxGridCellTextEditor
446 // ----------------------------------------------------------------------------
448 wxGridCellTextEditor::wxGridCellTextEditor()
452 void wxGridCellTextEditor::Create(wxWindow
* parent
,
454 wxEvtHandler
* evtHandler
)
456 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
457 wxDefaultPosition
, wxDefaultSize
458 #if defined(__WXMSW__)
459 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
463 wxGridCellEditor::Create(parent
, id
, evtHandler
);
466 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
467 wxGridCellAttr
* WXUNUSED(attr
))
469 // as we fill the entire client area, don't do anything here to minimize
473 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
475 wxRect
rect(rectOrig
);
477 // Make the edit control large enough to allow for internal
480 // TODO: remove this if the text ctrl sizing is improved esp. for
483 #if defined(__WXGTK__)
484 rect
.Inflate(rect
.x
? 1 : 0, rect
.y
? 1 : 0);
486 int extra
= rect
.x
&& rect
.y
? 2 : 1;
487 #if defined(__WXMOTIF__)
490 rect
.SetLeft( wxMax(0, rect
.x
- extra
) );
491 rect
.SetTop( wxMax(0, rect
.y
- extra
) );
492 rect
.SetRight( rect
.GetRight() + 2*extra
);
493 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
496 wxGridCellEditor::SetSize(rect
);
499 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
501 wxASSERT_MSG(m_control
,
502 wxT("The wxGridCellEditor must be Created first!"));
504 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
506 DoBeginEdit(m_startValue
);
509 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
511 Text()->SetValue(startValue
);
512 Text()->SetInsertionPointEnd();
516 bool wxGridCellTextEditor::EndEdit(int row
, int col
, 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
, wxGRID_VALUE_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
, wxGRID_VALUE_BOOL
))
841 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
843 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
849 void wxGridCellBoolEditor::Reset()
851 wxASSERT_MSG(m_control
,
852 wxT("The wxGridCellEditor must be Created first!"));
854 CBox()->SetValue(m_startValue
);
857 void wxGridCellBoolEditor::StartingClick()
859 CBox()->SetValue(!CBox()->GetValue());
862 // ----------------------------------------------------------------------------
863 // wxGridCellChoiceEditor
864 // ----------------------------------------------------------------------------
866 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
867 const wxChar
* choices
[],
869 : m_allowOthers(allowOthers
)
871 m_choices
.Alloc(count
);
872 for ( size_t n
= 0; n
< count
; n
++ )
874 m_choices
.Add(choices
[n
]);
878 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
880 wxEvtHandler
* evtHandler
)
882 size_t count
= m_choices
.GetCount();
883 wxString
*choices
= new wxString
[count
];
884 for ( size_t n
= 0; n
< count
; n
++ )
886 choices
[n
] = m_choices
[n
];
889 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
890 wxDefaultPosition
, wxDefaultSize
,
892 m_allowOthers
? 0 : wxCB_READONLY
);
896 wxGridCellEditor::Create(parent
, id
, evtHandler
);
899 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
900 wxGridCellAttr
* WXUNUSED(attr
))
902 // as we fill the entire client area, don't do anything here to minimize
906 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
908 wxASSERT_MSG(m_control
,
909 wxT("The wxGridCellEditor must be Created first!"));
911 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
913 Combo()->SetValue(m_startValue
);
914 size_t count
= m_choices
.GetCount();
915 for (size_t i
=0; i
<count
; i
++)
917 if (m_startValue
== m_choices
[i
])
919 Combo()->SetSelection(i
);
923 Combo()->SetInsertionPointEnd();
927 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
931 wxString value
= Combo()->GetValue();
932 bool changed
= value
!= m_startValue
;
935 grid
->GetTable()->SetValue(row
, col
, value
);
937 m_startValue
= wxEmptyString
;
938 Combo()->SetValue(m_startValue
);
943 void wxGridCellChoiceEditor::Reset()
945 Combo()->SetValue(m_startValue
);
946 Combo()->SetInsertionPointEnd();
949 // ----------------------------------------------------------------------------
950 // wxGridCellEditorEvtHandler
951 // ----------------------------------------------------------------------------
953 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
955 switch ( event
.KeyCode() )
959 m_grid
->DisableCellEditControl();
963 event
.Skip( m_grid
->ProcessEvent( event
) );
967 if (!m_grid
->ProcessEvent(event
))
968 m_editor
->HandleReturn(event
);
977 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
979 switch ( event
.KeyCode() )
991 // ============================================================================
993 // ============================================================================
995 // ----------------------------------------------------------------------------
996 // wxGridCellRenderer
997 // ----------------------------------------------------------------------------
999 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1000 wxGridCellAttr
& attr
,
1006 dc
.SetBackgroundMode( wxSOLID
);
1010 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1014 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1017 dc
.SetPen( *wxTRANSPARENT_PEN
);
1018 dc
.DrawRectangle(rect
);
1021 wxGridCellRenderer::~wxGridCellRenderer()
1025 // ----------------------------------------------------------------------------
1026 // wxGridCellStringRenderer
1027 // ----------------------------------------------------------------------------
1029 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1030 wxGridCellAttr
& attr
,
1034 dc
.SetBackgroundMode( wxTRANSPARENT
);
1036 // TODO some special colours for attr.IsReadOnly() case?
1040 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1041 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1045 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1046 dc
.SetTextForeground( attr
.GetTextColour() );
1049 dc
.SetFont( attr
.GetFont() );
1052 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1054 const wxString
& text
)
1057 dc
.SetFont(attr
.GetFont());
1058 dc
.GetTextExtent(text
, &x
, &y
);
1060 return wxSize(x
, y
);
1063 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1064 wxGridCellAttr
& attr
,
1068 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1071 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1072 wxGridCellAttr
& attr
,
1074 const wxRect
& rectCell
,
1078 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1080 // now we only have to draw the text
1081 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1084 attr
.GetAlignment(&hAlign
, &vAlign
);
1086 wxRect rect
= rectCell
;
1089 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1090 rect
, hAlign
, vAlign
);
1093 // ----------------------------------------------------------------------------
1094 // wxGridCellNumberRenderer
1095 // ----------------------------------------------------------------------------
1097 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1099 wxGridTableBase
*table
= grid
.GetTable();
1101 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1103 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1105 //else: leave the string empty or put 0 into it?
1110 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1111 wxGridCellAttr
& attr
,
1113 const wxRect
& rectCell
,
1117 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1119 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1121 // draw the text right aligned by default
1123 attr
.GetAlignment(&hAlign
, &vAlign
);
1126 wxRect rect
= rectCell
;
1129 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1132 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1133 wxGridCellAttr
& attr
,
1137 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1140 // ----------------------------------------------------------------------------
1141 // wxGridCellFloatRenderer
1142 // ----------------------------------------------------------------------------
1144 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1147 SetPrecision(precision
);
1150 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1152 wxGridTableBase
*table
= grid
.GetTable();
1154 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1158 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1161 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1163 //else: leave the string empty or put 0 into it?
1168 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1169 wxGridCellAttr
& attr
,
1171 const wxRect
& rectCell
,
1175 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1177 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1179 // draw the text right aligned by default
1181 attr
.GetAlignment(&hAlign
, &vAlign
);
1184 wxRect rect
= rectCell
;
1187 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1190 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1191 wxGridCellAttr
& attr
,
1195 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1198 // ----------------------------------------------------------------------------
1199 // wxGridCellBoolRenderer
1200 // ----------------------------------------------------------------------------
1202 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1204 // between checkmark and box
1205 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1207 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1208 wxGridCellAttr
& WXUNUSED(attr
),
1213 // compute it only once (no locks for MT safeness in GUI thread...)
1214 if ( !ms_sizeCheckMark
.x
)
1216 // get checkbox size
1217 wxCoord checkSize
= 0;
1218 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1219 wxSize size
= checkbox
->GetBestSize();
1220 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1222 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1224 checkSize
-= size
.y
/ 2;
1229 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1232 return ms_sizeCheckMark
;
1235 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1236 wxGridCellAttr
& attr
,
1242 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1244 // draw a check mark in the centre (ignoring alignment - TODO)
1245 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1247 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1248 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1249 rectMark
.width
= size
.x
;
1250 rectMark
.height
= size
.y
;
1252 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1253 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1254 dc
.DrawRectangle(rectMark
);
1256 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1259 if (grid
.GetTable()->CanGetValueAs(row
, col
, wxT("bool")))
1260 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1262 value
= !!grid
.GetTable()->GetValue(row
, col
);
1266 dc
.SetTextForeground(attr
.GetTextColour());
1267 dc
.DrawCheckMark(rectMark
);
1271 // ----------------------------------------------------------------------------
1273 // ----------------------------------------------------------------------------
1275 const wxColour
& wxGridCellAttr::GetTextColour() const
1277 if (HasTextColour())
1281 else if (m_defGridAttr
!= this)
1283 return m_defGridAttr
->GetTextColour();
1287 wxFAIL_MSG(wxT("Missing default cell attribute"));
1288 return wxNullColour
;
1293 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1295 if (HasBackgroundColour())
1297 else if (m_defGridAttr
!= this)
1298 return m_defGridAttr
->GetBackgroundColour();
1301 wxFAIL_MSG(wxT("Missing default cell attribute"));
1302 return wxNullColour
;
1307 const wxFont
& wxGridCellAttr::GetFont() const
1311 else if (m_defGridAttr
!= this)
1312 return m_defGridAttr
->GetFont();
1315 wxFAIL_MSG(wxT("Missing default cell attribute"));
1321 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1325 if ( hAlign
) *hAlign
= m_hAlign
;
1326 if ( vAlign
) *vAlign
= m_vAlign
;
1328 else if (m_defGridAttr
!= this)
1329 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1332 wxFAIL_MSG(wxT("Missing default cell attribute"));
1337 // GetRenderer and GetEditor use a slightly different decision path about
1338 // which attribute to use. If a non-default attr object has one then it is
1339 // used, otherwise the default editor or renderer is fetched from the grid and
1340 // used. It should be the default for the data type of the cell. If it is
1341 // NULL (because the table has a type that the grid does not have in its
1342 // registry,) then the grid's default editor or renderer is used.
1344 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1346 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1347 return m_renderer
; // use local attribute
1349 wxGridCellRenderer
* renderer
= NULL
;
1350 if (grid
) // get renderer for the data type
1351 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1354 // if we still don't have one then use the grid default
1355 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1358 wxFAIL_MSG(wxT("Missing default cell attribute"));
1363 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1365 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1366 return m_editor
; // use local attribute
1368 wxGridCellEditor
* editor
= NULL
;
1369 if (grid
) // get renderer for the data type
1370 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1373 // if we still don't have one then use the grid default
1374 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1377 wxFAIL_MSG(wxT("Missing default cell attribute"));
1382 // ----------------------------------------------------------------------------
1383 // wxGridCellAttrData
1384 // ----------------------------------------------------------------------------
1386 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1388 int n
= FindIndex(row
, col
);
1389 if ( n
== wxNOT_FOUND
)
1391 // add the attribute
1392 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1398 // change the attribute
1399 m_attrs
[(size_t)n
].attr
= attr
;
1403 // remove this attribute
1404 m_attrs
.RemoveAt((size_t)n
);
1409 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1411 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1413 int n
= FindIndex(row
, col
);
1414 if ( n
!= wxNOT_FOUND
)
1416 attr
= m_attrs
[(size_t)n
].attr
;
1423 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1425 size_t count
= m_attrs
.GetCount();
1426 for ( size_t n
= 0; n
< count
; n
++ )
1428 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1429 wxCoord row
= coords
.GetRow();
1430 if ((size_t)row
>= pos
)
1434 // If rows inserted, include row counter where necessary
1435 coords
.SetRow(row
+ numRows
);
1437 else if (numRows
< 0)
1439 // If rows deleted ...
1440 if ((size_t)row
>= pos
- numRows
)
1442 // ...either decrement row counter (if row still exists)...
1443 coords
.SetRow(row
+ numRows
);
1447 // ...or remove the attribute
1448 m_attrs
.RemoveAt((size_t)n
);
1456 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1458 size_t count
= m_attrs
.GetCount();
1459 for ( size_t n
= 0; n
< count
; n
++ )
1461 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1462 wxCoord col
= coords
.GetCol();
1463 if ( (size_t)col
>= pos
)
1467 // If rows inserted, include row counter where necessary
1468 coords
.SetCol(col
+ numCols
);
1470 else if (numCols
< 0)
1472 // If rows deleted ...
1473 if ((size_t)col
>= pos
- numCols
)
1475 // ...either decrement row counter (if row still exists)...
1476 coords
.SetCol(col
+ numCols
);
1480 // ...or remove the attribute
1481 m_attrs
.RemoveAt((size_t)n
);
1489 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1491 size_t count
= m_attrs
.GetCount();
1492 for ( size_t n
= 0; n
< count
; n
++ )
1494 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1495 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1504 // ----------------------------------------------------------------------------
1505 // wxGridRowOrColAttrData
1506 // ----------------------------------------------------------------------------
1508 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1510 size_t count
= m_attrs
.Count();
1511 for ( size_t n
= 0; n
< count
; n
++ )
1513 m_attrs
[n
]->DecRef();
1517 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1519 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1521 int n
= m_rowsOrCols
.Index(rowOrCol
);
1522 if ( n
!= wxNOT_FOUND
)
1524 attr
= m_attrs
[(size_t)n
];
1531 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1533 int n
= m_rowsOrCols
.Index(rowOrCol
);
1534 if ( n
== wxNOT_FOUND
)
1536 // add the attribute
1537 m_rowsOrCols
.Add(rowOrCol
);
1544 // change the attribute
1545 m_attrs
[(size_t)n
] = attr
;
1549 // remove this attribute
1550 m_attrs
[(size_t)n
]->DecRef();
1551 m_rowsOrCols
.RemoveAt((size_t)n
);
1552 m_attrs
.RemoveAt((size_t)n
);
1557 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1559 size_t count
= m_attrs
.GetCount();
1560 for ( size_t n
= 0; n
< count
; n
++ )
1562 int & rowOrCol
= m_rowsOrCols
[n
];
1563 if ( (size_t)rowOrCol
>= pos
)
1565 if ( numRowsOrCols
> 0 )
1567 // If rows inserted, include row counter where necessary
1568 rowOrCol
+= numRowsOrCols
;
1570 else if ( numRowsOrCols
< 0)
1572 // If rows deleted, either decrement row counter (if row still exists)
1573 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1574 rowOrCol
+= numRowsOrCols
;
1577 m_rowsOrCols
.RemoveAt((size_t)n
);
1578 m_attrs
.RemoveAt((size_t)n
);
1586 // ----------------------------------------------------------------------------
1587 // wxGridCellAttrProvider
1588 // ----------------------------------------------------------------------------
1590 wxGridCellAttrProvider::wxGridCellAttrProvider()
1592 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1595 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1600 void wxGridCellAttrProvider::InitData()
1602 m_data
= new wxGridCellAttrProviderData
;
1605 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1607 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1610 // first look for the attribute of this specific cell
1611 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1615 // then look for the col attr (col attributes are more common than
1616 // the row ones, hence they have priority)
1617 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1622 // finally try the row attributes
1623 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1630 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1636 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1639 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1644 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1647 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1652 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1655 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1659 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1661 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1665 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1669 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1671 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1675 // ----------------------------------------------------------------------------
1676 // wxGridTypeRegistry
1677 // ----------------------------------------------------------------------------
1679 wxGridTypeRegistry::~wxGridTypeRegistry()
1681 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++)
1682 delete m_typeinfo
[i
];
1686 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1687 wxGridCellRenderer
* renderer
,
1688 wxGridCellEditor
* editor
)
1691 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1693 // is it already registered?
1694 if ((loc
= FindDataType(typeName
)) != -1) {
1695 delete m_typeinfo
[loc
];
1696 m_typeinfo
[loc
] = info
;
1699 m_typeinfo
.Add(info
);
1703 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1707 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1708 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1717 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1719 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1723 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1725 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1729 // ----------------------------------------------------------------------------
1731 // ----------------------------------------------------------------------------
1733 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1736 wxGridTableBase::wxGridTableBase()
1738 m_view
= (wxGrid
*) NULL
;
1739 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1742 wxGridTableBase::~wxGridTableBase()
1744 delete m_attrProvider
;
1747 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1749 delete m_attrProvider
;
1750 m_attrProvider
= attrProvider
;
1753 bool wxGridTableBase::CanHaveAttributes()
1755 if ( ! GetAttrProvider() )
1757 // use the default attr provider by default
1758 SetAttrProvider(new wxGridCellAttrProvider
);
1763 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1765 if ( m_attrProvider
)
1766 return m_attrProvider
->GetAttr(row
, col
);
1768 return (wxGridCellAttr
*)NULL
;
1771 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1773 if ( m_attrProvider
)
1775 m_attrProvider
->SetAttr(attr
, row
, col
);
1779 // as we take ownership of the pointer and don't store it, we must
1785 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1787 if ( m_attrProvider
)
1789 m_attrProvider
->SetRowAttr(attr
, row
);
1793 // as we take ownership of the pointer and don't store it, we must
1799 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1801 if ( m_attrProvider
)
1803 m_attrProvider
->SetColAttr(attr
, col
);
1807 // as we take ownership of the pointer and don't store it, we must
1813 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1815 if ( m_attrProvider
)
1817 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1821 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1823 if ( m_attrProvider
)
1825 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1829 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1831 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1832 "but your derived table class does not override this function") );
1837 bool wxGridTableBase::AppendRows( size_t numRows
)
1839 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1840 "but your derived table class does not override this function"));
1845 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1847 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1848 "but your derived table class does not override this function"));
1853 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1855 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1856 "but your derived table class does not override this function"));
1861 bool wxGridTableBase::AppendCols( size_t numCols
)
1863 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1864 "but your derived table class does not override this function"));
1869 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1871 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1872 "but your derived table class does not override this function"));
1878 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1881 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1882 // how much it makes sense to us geeks.
1886 wxString
wxGridTableBase::GetColLabelValue( int col
)
1888 // default col labels are:
1889 // cols 0 to 25 : A-Z
1890 // cols 26 to 675 : AA-ZZ
1895 for ( n
= 1; ; n
++ )
1897 s
+= (_T('A') + (wxChar
)( col%26
));
1899 if ( col
< 0 ) break;
1902 // reverse the string...
1904 for ( i
= 0; i
< n
; i
++ )
1913 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1915 return wxGRID_VALUE_STRING
;
1918 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1919 const wxString
& typeName
)
1921 return typeName
== wxGRID_VALUE_STRING
;
1924 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1926 return CanGetValueAs(row
, col
, typeName
);
1929 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1934 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1939 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1944 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1945 long WXUNUSED(value
) )
1949 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1950 double WXUNUSED(value
) )
1954 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1955 bool WXUNUSED(value
) )
1960 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1961 const wxString
& WXUNUSED(typeName
) )
1966 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1967 const wxString
& WXUNUSED(typeName
),
1968 void* WXUNUSED(value
) )
1973 //////////////////////////////////////////////////////////////////////
1975 // Message class for the grid table to send requests and notifications
1979 wxGridTableMessage::wxGridTableMessage()
1981 m_table
= (wxGridTableBase
*) NULL
;
1987 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1988 int commandInt1
, int commandInt2
)
1992 m_comInt1
= commandInt1
;
1993 m_comInt2
= commandInt2
;
1998 //////////////////////////////////////////////////////////////////////
2000 // A basic grid table for string data. An object of this class will
2001 // created by wxGrid if you don't specify an alternative table class.
2004 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2006 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2008 wxGridStringTable::wxGridStringTable()
2013 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2018 m_data
.Alloc( numRows
);
2021 sa
.Alloc( numCols
);
2022 for ( col
= 0; col
< numCols
; col
++ )
2024 sa
.Add( wxEmptyString
);
2027 for ( row
= 0; row
< numRows
; row
++ )
2033 wxGridStringTable::~wxGridStringTable()
2037 long wxGridStringTable::GetNumberRows()
2039 return m_data
.GetCount();
2042 long wxGridStringTable::GetNumberCols()
2044 if ( m_data
.GetCount() > 0 )
2045 return m_data
[0].GetCount();
2050 wxString
wxGridStringTable::GetValue( int row
, int col
)
2052 // TODO: bounds checking
2054 return m_data
[row
][col
];
2057 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2059 // TODO: bounds checking
2061 m_data
[row
][col
] = value
;
2064 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2066 // TODO: bounds checking
2068 return (m_data
[row
][col
] == wxEmptyString
);
2072 void wxGridStringTable::Clear()
2075 int numRows
, numCols
;
2077 numRows
= m_data
.GetCount();
2080 numCols
= m_data
[0].GetCount();
2082 for ( row
= 0; row
< numRows
; row
++ )
2084 for ( col
= 0; col
< numCols
; col
++ )
2086 m_data
[row
][col
] = wxEmptyString
;
2093 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2097 size_t curNumRows
= m_data
.GetCount();
2098 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2100 if ( pos
>= curNumRows
)
2102 return AppendRows( numRows
);
2106 sa
.Alloc( curNumCols
);
2107 for ( col
= 0; col
< curNumCols
; col
++ )
2109 sa
.Add( wxEmptyString
);
2112 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2114 m_data
.Insert( sa
, row
);
2116 UpdateAttrRows( pos
, numRows
);
2119 wxGridTableMessage
msg( this,
2120 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2124 GetView()->ProcessTableMessage( msg
);
2130 bool wxGridStringTable::AppendRows( size_t numRows
)
2134 size_t curNumRows
= m_data
.GetCount();
2135 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2138 if ( curNumCols
> 0 )
2140 sa
.Alloc( curNumCols
);
2141 for ( col
= 0; col
< curNumCols
; col
++ )
2143 sa
.Add( wxEmptyString
);
2147 for ( row
= 0; row
< numRows
; row
++ )
2154 wxGridTableMessage
msg( this,
2155 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2158 GetView()->ProcessTableMessage( msg
);
2164 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2168 size_t curNumRows
= m_data
.GetCount();
2170 if ( pos
>= curNumRows
)
2173 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2174 "Pos value is invalid for present table with %d rows",
2175 pos
, numRows
, curNumRows
);
2176 wxFAIL_MSG( wxT(errmsg
) );
2180 if ( numRows
> curNumRows
- pos
)
2182 numRows
= curNumRows
- pos
;
2185 if ( numRows
>= curNumRows
)
2187 m_data
.Empty(); // don't release memory just yet
2191 for ( n
= 0; n
< numRows
; n
++ )
2193 m_data
.Remove( pos
);
2196 UpdateAttrRows( pos
, -((int)numRows
) );
2199 wxGridTableMessage
msg( this,
2200 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2204 GetView()->ProcessTableMessage( msg
);
2210 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2214 size_t curNumRows
= m_data
.GetCount();
2215 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2217 if ( pos
>= curNumCols
)
2219 return AppendCols( numCols
);
2222 for ( row
= 0; row
< curNumRows
; row
++ )
2224 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2226 m_data
[row
].Insert( wxEmptyString
, col
);
2229 UpdateAttrCols( pos
, numCols
);
2232 wxGridTableMessage
msg( this,
2233 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2237 GetView()->ProcessTableMessage( msg
);
2243 bool wxGridStringTable::AppendCols( size_t numCols
)
2247 size_t curNumRows
= m_data
.GetCount();
2250 // TODO: something better than this ?
2252 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2253 "Call AppendRows() first") );
2257 for ( row
= 0; row
< curNumRows
; row
++ )
2259 for ( n
= 0; n
< numCols
; n
++ )
2261 m_data
[row
].Add( wxEmptyString
);
2267 wxGridTableMessage
msg( this,
2268 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2271 GetView()->ProcessTableMessage( msg
);
2277 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2281 size_t curNumRows
= m_data
.GetCount();
2282 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2284 if ( pos
>= curNumCols
)
2287 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2288 "Pos value is invalid for present table with %d cols",
2289 pos
, numCols
, curNumCols
);
2290 wxFAIL_MSG( wxT( errmsg
) );
2294 if ( numCols
> curNumCols
- pos
)
2296 numCols
= curNumCols
- pos
;
2299 for ( row
= 0; row
< curNumRows
; row
++ )
2301 if ( numCols
>= curNumCols
)
2303 m_data
[row
].Clear();
2307 for ( n
= 0; n
< numCols
; n
++ )
2309 m_data
[row
].Remove( pos
);
2313 UpdateAttrCols( pos
, -((int)numCols
) );
2316 wxGridTableMessage
msg( this,
2317 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2321 GetView()->ProcessTableMessage( msg
);
2327 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2329 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2331 // using default label
2333 return wxGridTableBase::GetRowLabelValue( row
);
2337 return m_rowLabels
[ row
];
2341 wxString
wxGridStringTable::GetColLabelValue( int col
)
2343 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2345 // using default label
2347 return wxGridTableBase::GetColLabelValue( col
);
2351 return m_colLabels
[ col
];
2355 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2357 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2359 int n
= m_rowLabels
.GetCount();
2361 for ( i
= n
; i
<= row
; i
++ )
2363 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2367 m_rowLabels
[row
] = value
;
2370 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2372 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2374 int n
= m_colLabels
.GetCount();
2376 for ( i
= n
; i
<= col
; i
++ )
2378 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2382 m_colLabels
[col
] = value
;
2387 //////////////////////////////////////////////////////////////////////
2388 //////////////////////////////////////////////////////////////////////
2390 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2392 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2393 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2394 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2395 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2398 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2400 const wxPoint
&pos
, const wxSize
&size
)
2401 : wxWindow( parent
, id
, pos
, size
)
2406 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2410 // NO - don't do this because it will set both the x and y origin
2411 // coords to match the parent scrolled window and we just want to
2412 // set the y coord - MB
2414 // m_owner->PrepareDC( dc );
2417 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2418 dc
.SetDeviceOrigin( 0, -y
);
2420 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2421 m_owner
->DrawRowLabels( dc
);
2425 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2427 m_owner
->ProcessRowLabelMouseEvent( event
);
2431 // This seems to be required for wxMotif otherwise the mouse
2432 // cursor must be in the cell edit control to get key events
2434 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2436 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2441 //////////////////////////////////////////////////////////////////////
2443 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2445 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2446 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2447 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2448 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2451 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2453 const wxPoint
&pos
, const wxSize
&size
)
2454 : wxWindow( parent
, id
, pos
, size
)
2459 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2463 // NO - don't do this because it will set both the x and y origin
2464 // coords to match the parent scrolled window and we just want to
2465 // set the x coord - MB
2467 // m_owner->PrepareDC( dc );
2470 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2471 dc
.SetDeviceOrigin( -x
, 0 );
2473 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2474 m_owner
->DrawColLabels( dc
);
2478 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2480 m_owner
->ProcessColLabelMouseEvent( event
);
2484 // This seems to be required for wxMotif otherwise the mouse
2485 // cursor must be in the cell edit control to get key events
2487 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2489 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2494 //////////////////////////////////////////////////////////////////////
2496 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2498 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2499 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2500 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2501 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2504 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2506 const wxPoint
&pos
, const wxSize
&size
)
2507 : wxWindow( parent
, id
, pos
, size
)
2512 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2516 int client_height
= 0;
2517 int client_width
= 0;
2518 GetClientSize( &client_width
, &client_height
);
2520 dc
.SetPen( *wxBLACK_PEN
);
2521 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2522 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2524 dc
.SetPen( *wxWHITE_PEN
);
2525 dc
.DrawLine( 0, 0, client_width
, 0 );
2526 dc
.DrawLine( 0, 0, 0, client_height
);
2530 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2532 m_owner
->ProcessCornerLabelMouseEvent( event
);
2536 // This seems to be required for wxMotif otherwise the mouse
2537 // cursor must be in the cell edit control to get key events
2539 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2541 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2546 //////////////////////////////////////////////////////////////////////
2548 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2550 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2551 EVT_PAINT( wxGridWindow::OnPaint
)
2552 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2553 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2554 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2557 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2558 wxGridRowLabelWindow
*rowLblWin
,
2559 wxGridColLabelWindow
*colLblWin
,
2560 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2561 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
2564 m_rowLabelWin
= rowLblWin
;
2565 m_colLabelWin
= colLblWin
;
2566 SetBackgroundColour( "WHITE" );
2570 wxGridWindow::~wxGridWindow()
2575 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2577 wxPaintDC
dc( this );
2578 m_owner
->PrepareDC( dc
);
2579 wxRegion reg
= GetUpdateRegion();
2580 m_owner
->CalcCellsExposed( reg
);
2581 m_owner
->DrawGridCellArea( dc
);
2582 #if WXGRID_DRAW_LINES
2583 m_owner
->DrawAllGridLines( dc
, reg
);
2585 m_owner
->DrawHighlight( dc
);
2589 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2591 wxPanel::ScrollWindow( dx
, dy
, rect
);
2592 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2593 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2597 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2599 m_owner
->ProcessGridCellMouseEvent( event
);
2603 // This seems to be required for wxMotif otherwise the mouse
2604 // cursor must be in the cell edit control to get key events
2606 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2608 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2611 // We are trapping erase background events to reduce flicker under MSW
2612 // and GTK but this can leave junk in the space beyond the last row and
2613 // col. So here we paint these spaces if they are visible.
2615 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2618 GetClientSize( &cw
, &ch
);
2621 m_owner
->CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2624 rightRect
= m_owner
->CellToRect( 0, m_owner
->GetNumberCols()-1 );
2627 bottomRect
= m_owner
->CellToRect( m_owner
->GetNumberRows()-1, 0 );
2629 if ( right
> rightRect
.GetRight() || bottom
> bottomRect
.GetBottom() )
2632 m_owner
->CalcUnscrolledPosition( 0, 0, &left
, &top
);
2634 wxClientDC
dc( this );
2635 m_owner
->PrepareDC( dc
);
2636 dc
.SetBrush( wxBrush(m_owner
->GetDefaultCellBackgroundColour(), wxSOLID
) );
2637 dc
.SetPen( *wxTRANSPARENT_PEN
);
2639 if ( right
> rightRect
.GetRight() )
2640 dc
.DrawRectangle( rightRect
.GetRight()+1, top
, right
- rightRect
.GetRight(), ch
);
2642 if ( bottom
> bottomRect
.GetBottom() )
2643 dc
.DrawRectangle( left
, bottomRect
.GetBottom()+1, cw
, bottom
- bottomRect
.GetBottom() );
2648 //////////////////////////////////////////////////////////////////////
2651 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2653 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2654 EVT_PAINT( wxGrid::OnPaint
)
2655 EVT_SIZE( wxGrid::OnSize
)
2656 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2657 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2660 wxGrid::wxGrid( wxWindow
*parent
,
2665 const wxString
& name
)
2666 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2667 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2676 m_defaultCellAttr
->SafeDecRef();
2678 #ifdef DEBUG_ATTR_CACHE
2679 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2680 wxPrintf(_T("wxGrid attribute cache statistics: "
2681 "total: %u, hits: %u (%u%%)\n"),
2682 total
, gs_nAttrCacheHits
,
2683 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2689 delete m_typeRegistry
;
2694 // ----- internal init and update functions
2697 void wxGrid::Create()
2699 m_created
= FALSE
; // set to TRUE by CreateGrid
2700 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2702 m_table
= (wxGridTableBase
*) NULL
;
2705 m_cellEditCtrlEnabled
= FALSE
;
2707 m_defaultCellAttr
= new wxGridCellAttr
;
2708 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2710 // Set default cell attributes
2711 m_defaultCellAttr
->SetFont(GetFont());
2712 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2713 m_defaultCellAttr
->SetTextColour(
2714 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2715 m_defaultCellAttr
->SetBackgroundColour(
2716 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2717 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2718 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2723 m_currentCellCoords
= wxGridNoCellCoords
;
2725 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2726 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2728 // data type registration: register all standard data types
2729 // TODO: may be allow the app to selectively disable some of them?
2730 m_typeRegistry
= new wxGridTypeRegistry
;
2731 RegisterDataType(wxGRID_VALUE_STRING
,
2732 new wxGridCellStringRenderer
,
2733 new wxGridCellTextEditor
);
2734 RegisterDataType(wxGRID_VALUE_BOOL
,
2735 new wxGridCellBoolRenderer
,
2736 new wxGridCellBoolEditor
);
2737 RegisterDataType(wxGRID_VALUE_NUMBER
,
2738 new wxGridCellNumberRenderer
,
2739 new wxGridCellNumberEditor
);
2741 // subwindow components that make up the wxGrid
2742 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2747 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2752 m_colLabelWin
= new wxGridColLabelWindow( this,
2757 m_gridWin
= new wxGridWindow( this,
2764 SetTargetWindow( m_gridWin
);
2768 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2772 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2777 m_numRows
= numRows
;
2778 m_numCols
= numCols
;
2780 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2781 m_table
->SetView( this );
2790 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2794 // RD: Actually, this should probably be allowed. I think it would be
2795 // nice to be able to switch multiple Tables in and out of a single
2796 // View at runtime. Is there anything in the implmentation that would
2799 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2804 m_numRows
= table
->GetNumberRows();
2805 m_numCols
= table
->GetNumberCols();
2808 m_table
->SetView( this );
2821 if ( m_numRows
<= 0 )
2822 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2824 if ( m_numCols
<= 0 )
2825 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2827 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2828 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2830 if ( m_rowLabelWin
)
2832 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2836 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2839 m_labelTextColour
= wxColour( _T("BLACK") );
2842 m_attrCache
.row
= -1;
2844 // TODO: something better than this ?
2846 m_labelFont
= this->GetFont();
2847 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2849 m_rowLabelHorizAlign
= wxLEFT
;
2850 m_rowLabelVertAlign
= wxCENTRE
;
2852 m_colLabelHorizAlign
= wxCENTRE
;
2853 m_colLabelVertAlign
= wxTOP
;
2855 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2856 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2858 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2859 m_defaultRowHeight
+= 8;
2861 m_defaultRowHeight
+= 4;
2864 m_gridLineColour
= wxColour( 128, 128, 255 );
2865 m_gridLinesEnabled
= TRUE
;
2867 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2868 m_winCapture
= (wxWindow
*)NULL
;
2869 m_canDragRowSize
= TRUE
;
2870 m_canDragColSize
= TRUE
;
2872 m_dragRowOrCol
= -1;
2873 m_isDragging
= FALSE
;
2874 m_startDragPos
= wxDefaultPosition
;
2876 m_waitForSlowClick
= FALSE
;
2878 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2879 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2881 m_currentCellCoords
= wxGridNoCellCoords
;
2883 m_selectedTopLeft
= wxGridNoCellCoords
;
2884 m_selectedBottomRight
= wxGridNoCellCoords
;
2885 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2886 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2888 m_editable
= TRUE
; // default for whole grid
2890 m_inOnKeyDown
= FALSE
;
2894 // ----------------------------------------------------------------------------
2895 // the idea is to call these functions only when necessary because they create
2896 // quite big arrays which eat memory mostly unnecessary - in particular, if
2897 // default widths/heights are used for all rows/columns, we may not use these
2900 // with some extra code, it should be possible to only store the
2901 // widths/heights different from default ones but this will be done later...
2902 // ----------------------------------------------------------------------------
2904 void wxGrid::InitRowHeights()
2906 m_rowHeights
.Empty();
2907 m_rowBottoms
.Empty();
2909 m_rowHeights
.Alloc( m_numRows
);
2910 m_rowBottoms
.Alloc( m_numRows
);
2913 for ( int i
= 0; i
< m_numRows
; i
++ )
2915 m_rowHeights
.Add( m_defaultRowHeight
);
2916 rowBottom
+= m_defaultRowHeight
;
2917 m_rowBottoms
.Add( rowBottom
);
2921 void wxGrid::InitColWidths()
2923 m_colWidths
.Empty();
2924 m_colRights
.Empty();
2926 m_colWidths
.Alloc( m_numCols
);
2927 m_colRights
.Alloc( m_numCols
);
2929 for ( int i
= 0; i
< m_numCols
; i
++ )
2931 m_colWidths
.Add( m_defaultColWidth
);
2932 colRight
+= m_defaultColWidth
;
2933 m_colRights
.Add( colRight
);
2937 int wxGrid::GetColWidth(int col
) const
2939 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2942 int wxGrid::GetColLeft(int col
) const
2944 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2945 : m_colRights
[col
] - m_colWidths
[col
];
2948 int wxGrid::GetColRight(int col
) const
2950 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
2954 int wxGrid::GetRowHeight(int row
) const
2956 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2959 int wxGrid::GetRowTop(int row
) const
2961 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2962 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2965 int wxGrid::GetRowBottom(int row
) const
2967 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2968 : m_rowBottoms
[row
];
2971 void wxGrid::CalcDimensions()
2974 GetClientSize( &cw
, &ch
);
2976 if ( m_numRows
> 0 && m_numCols
> 0 )
2978 int right
= GetColRight( m_numCols
-1 ) + 50;
2979 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
2981 // TODO: restore the scroll position that we had before sizing
2984 GetViewStart( &x
, &y
);
2985 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2986 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2992 void wxGrid::CalcWindowSizes()
2995 GetClientSize( &cw
, &ch
);
2997 if ( m_cornerLabelWin
->IsShown() )
2998 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3000 if ( m_colLabelWin
->IsShown() )
3001 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3003 if ( m_rowLabelWin
->IsShown() )
3004 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3006 if ( m_gridWin
->IsShown() )
3007 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3011 // this is called when the grid table sends a message to say that it
3012 // has been redimensioned
3014 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3018 // if we were using the default widths/heights so far, we must change them
3020 if ( m_colWidths
.IsEmpty() )
3025 if ( m_rowHeights
.IsEmpty() )
3030 switch ( msg
.GetId() )
3032 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3034 size_t pos
= msg
.GetCommandInt();
3035 int numRows
= msg
.GetCommandInt2();
3036 for ( i
= 0; i
< numRows
; i
++ )
3038 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3039 m_rowBottoms
.Insert( 0, pos
);
3041 m_numRows
+= numRows
;
3044 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3046 for ( i
= pos
; i
< m_numRows
; i
++ )
3048 bottom
+= m_rowHeights
[i
];
3049 m_rowBottoms
[i
] = bottom
;
3055 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3057 int numRows
= msg
.GetCommandInt();
3058 for ( i
= 0; i
< numRows
; i
++ )
3060 m_rowHeights
.Add( m_defaultRowHeight
);
3061 m_rowBottoms
.Add( 0 );
3064 int oldNumRows
= m_numRows
;
3065 m_numRows
+= numRows
;
3068 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3070 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3072 bottom
+= m_rowHeights
[i
];
3073 m_rowBottoms
[i
] = bottom
;
3079 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3081 size_t pos
= msg
.GetCommandInt();
3082 int numRows
= msg
.GetCommandInt2();
3083 for ( i
= 0; i
< numRows
; i
++ )
3085 m_rowHeights
.Remove( pos
);
3086 m_rowBottoms
.Remove( pos
);
3088 m_numRows
-= numRows
;
3093 m_colWidths
.Clear();
3094 m_colRights
.Clear();
3095 m_currentCellCoords
= wxGridNoCellCoords
;
3099 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3100 m_currentCellCoords
.Set( 0, 0 );
3103 for ( i
= 0; i
< m_numRows
; i
++ )
3105 h
+= m_rowHeights
[i
];
3106 m_rowBottoms
[i
] = h
;
3114 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3116 size_t pos
= msg
.GetCommandInt();
3117 int numCols
= msg
.GetCommandInt2();
3118 for ( i
= 0; i
< numCols
; i
++ )
3120 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3121 m_colRights
.Insert( 0, pos
);
3123 m_numCols
+= numCols
;
3126 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3128 for ( i
= pos
; i
< m_numCols
; i
++ )
3130 right
+= m_colWidths
[i
];
3131 m_colRights
[i
] = right
;
3137 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3139 int numCols
= msg
.GetCommandInt();
3140 for ( i
= 0; i
< numCols
; i
++ )
3142 m_colWidths
.Add( m_defaultColWidth
);
3143 m_colRights
.Add( 0 );
3146 int oldNumCols
= m_numCols
;
3147 m_numCols
+= numCols
;
3150 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3152 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3154 right
+= m_colWidths
[i
];
3155 m_colRights
[i
] = right
;
3161 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3163 size_t pos
= msg
.GetCommandInt();
3164 int numCols
= msg
.GetCommandInt2();
3165 for ( i
= 0; i
< numCols
; i
++ )
3167 m_colWidths
.Remove( pos
);
3168 m_colRights
.Remove( pos
);
3170 m_numCols
-= numCols
;
3174 #if 0 // leave the row alone here so that AppendCols will work subsequently
3176 m_rowHeights
.Clear();
3177 m_rowBottoms
.Clear();
3179 m_currentCellCoords
= wxGridNoCellCoords
;
3183 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3184 m_currentCellCoords
.Set( 0, 0 );
3187 for ( i
= 0; i
< m_numCols
; i
++ )
3189 w
+= m_colWidths
[i
];
3202 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3204 wxRegionIterator
iter( reg
);
3207 m_rowLabelsExposed
.Empty();
3214 // TODO: remove this when we can...
3215 // There is a bug in wxMotif that gives garbage update
3216 // rectangles if you jump-scroll a long way by clicking the
3217 // scrollbar with middle button. This is a work-around
3219 #if defined(__WXMOTIF__)
3221 m_gridWin
->GetClientSize( &cw
, &ch
);
3222 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3223 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3226 // logical bounds of update region
3229 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3230 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3232 // find the row labels within these bounds
3235 for ( row
= 0; row
< m_numRows
; row
++ )
3237 if ( GetRowBottom(row
) < top
)
3240 if ( GetRowTop(row
) > bottom
)
3243 m_rowLabelsExposed
.Add( row
);
3251 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3253 wxRegionIterator
iter( reg
);
3256 m_colLabelsExposed
.Empty();
3263 // TODO: remove this when we can...
3264 // There is a bug in wxMotif that gives garbage update
3265 // rectangles if you jump-scroll a long way by clicking the
3266 // scrollbar with middle button. This is a work-around
3268 #if defined(__WXMOTIF__)
3270 m_gridWin
->GetClientSize( &cw
, &ch
);
3271 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3272 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3275 // logical bounds of update region
3278 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3279 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3281 // find the cells within these bounds
3284 for ( col
= 0; col
< m_numCols
; col
++ )
3286 if ( GetColRight(col
) < left
)
3289 if ( GetColLeft(col
) > right
)
3292 m_colLabelsExposed
.Add( col
);
3300 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3302 wxRegionIterator
iter( reg
);
3305 m_cellsExposed
.Empty();
3306 m_rowsExposed
.Empty();
3307 m_colsExposed
.Empty();
3309 int left
, top
, right
, bottom
;
3314 // TODO: remove this when we can...
3315 // There is a bug in wxMotif that gives garbage update
3316 // rectangles if you jump-scroll a long way by clicking the
3317 // scrollbar with middle button. This is a work-around
3319 #if defined(__WXMOTIF__)
3321 m_gridWin
->GetClientSize( &cw
, &ch
);
3322 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3323 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3324 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3325 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3328 // logical bounds of update region
3330 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3331 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3333 // find the cells within these bounds
3336 for ( row
= 0; row
< m_numRows
; row
++ )
3338 if ( GetRowBottom(row
) <= top
)
3341 if ( GetRowTop(row
) > bottom
)
3344 m_rowsExposed
.Add( row
);
3346 for ( col
= 0; col
< m_numCols
; col
++ )
3348 if ( GetColRight(col
) <= left
)
3351 if ( GetColLeft(col
) > right
)
3354 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3355 m_colsExposed
.Add( col
);
3356 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3365 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3368 wxPoint
pos( event
.GetPosition() );
3369 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3371 if ( event
.Dragging() )
3373 m_isDragging
= TRUE
;
3375 if ( event
.LeftIsDown() )
3377 switch( m_cursorMode
)
3379 case WXGRID_CURSOR_RESIZE_ROW
:
3381 int cw
, ch
, left
, dummy
;
3382 m_gridWin
->GetClientSize( &cw
, &ch
);
3383 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3385 wxClientDC
dc( m_gridWin
);
3387 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3388 dc
.SetLogicalFunction(wxINVERT
);
3389 if ( m_dragLastPos
>= 0 )
3391 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3393 dc
.DrawLine( left
, y
, left
+cw
, y
);
3398 case WXGRID_CURSOR_SELECT_ROW
:
3399 if ( (row
= YToRow( y
)) >= 0 &&
3400 !IsInSelection( row
, 0 ) )
3402 SelectRow( row
, TRUE
);
3405 // default label to suppress warnings about "enumeration value
3406 // 'xxx' not handled in switch
3414 m_isDragging
= FALSE
;
3417 // ------------ Entering or leaving the window
3419 if ( event
.Entering() || event
.Leaving() )
3421 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3425 // ------------ Left button pressed
3427 else if ( event
.LeftDown() )
3429 // don't send a label click event for a hit on the
3430 // edge of the row label - this is probably the user
3431 // wanting to resize the row
3433 if ( YToEdgeOfRow(y
) < 0 )
3437 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3439 SelectRow( row
, event
.ShiftDown() );
3440 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3445 // starting to drag-resize a row
3447 if ( CanDragRowSize() )
3448 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3453 // ------------ Left double click
3455 else if (event
.LeftDClick() )
3457 if ( YToEdgeOfRow(y
) < 0 )
3460 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3465 // ------------ Left button released
3467 else if ( event
.LeftUp() )
3469 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3471 DoEndDragResizeRow();
3473 // Note: we are ending the event *after* doing
3474 // default processing in this case
3476 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3479 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3484 // ------------ Right button down
3486 else if ( event
.RightDown() )
3489 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3491 // no default action at the moment
3496 // ------------ Right double click
3498 else if ( event
.RightDClick() )
3501 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3503 // no default action at the moment
3508 // ------------ No buttons down and mouse moving
3510 else if ( event
.Moving() )
3512 m_dragRowOrCol
= YToEdgeOfRow( y
);
3513 if ( m_dragRowOrCol
>= 0 )
3515 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3517 // don't capture the mouse yet
3518 if ( CanDragRowSize() )
3519 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3522 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3524 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3530 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3533 wxPoint
pos( event
.GetPosition() );
3534 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3536 if ( event
.Dragging() )
3538 m_isDragging
= TRUE
;
3540 if ( event
.LeftIsDown() )
3542 switch( m_cursorMode
)
3544 case WXGRID_CURSOR_RESIZE_COL
:
3546 int cw
, ch
, dummy
, top
;
3547 m_gridWin
->GetClientSize( &cw
, &ch
);
3548 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3550 wxClientDC
dc( m_gridWin
);
3553 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3554 GetColMinimalWidth(m_dragRowOrCol
));
3555 dc
.SetLogicalFunction(wxINVERT
);
3556 if ( m_dragLastPos
>= 0 )
3558 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3560 dc
.DrawLine( x
, top
, x
, top
+ch
);
3565 case WXGRID_CURSOR_SELECT_COL
:
3566 if ( (col
= XToCol( x
)) >= 0 &&
3567 !IsInSelection( 0, col
) )
3569 SelectCol( col
, TRUE
);
3572 // default label to suppress warnings about "enumeration value
3573 // 'xxx' not handled in switch
3581 m_isDragging
= FALSE
;
3584 // ------------ Entering or leaving the window
3586 if ( event
.Entering() || event
.Leaving() )
3588 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3592 // ------------ Left button pressed
3594 else if ( event
.LeftDown() )
3596 // don't send a label click event for a hit on the
3597 // edge of the col label - this is probably the user
3598 // wanting to resize the col
3600 if ( XToEdgeOfCol(x
) < 0 )
3604 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3606 SelectCol( col
, event
.ShiftDown() );
3607 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3612 // starting to drag-resize a col
3614 if ( CanDragColSize() )
3615 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3620 // ------------ Left double click
3622 if ( event
.LeftDClick() )
3624 if ( XToEdgeOfCol(x
) < 0 )
3627 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3632 // ------------ Left button released
3634 else if ( event
.LeftUp() )
3636 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3638 DoEndDragResizeCol();
3640 // Note: we are ending the event *after* doing
3641 // default processing in this case
3643 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3646 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3651 // ------------ Right button down
3653 else if ( event
.RightDown() )
3656 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3658 // no default action at the moment
3663 // ------------ Right double click
3665 else if ( event
.RightDClick() )
3668 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3670 // no default action at the moment
3675 // ------------ No buttons down and mouse moving
3677 else if ( event
.Moving() )
3679 m_dragRowOrCol
= XToEdgeOfCol( x
);
3680 if ( m_dragRowOrCol
>= 0 )
3682 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3684 // don't capture the cursor yet
3685 if ( CanDragColSize() )
3686 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3689 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3691 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3697 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3699 if ( event
.LeftDown() )
3701 // indicate corner label by having both row and
3704 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3710 else if ( event
.LeftDClick() )
3712 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3715 else if ( event
.RightDown() )
3717 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3719 // no default action at the moment
3723 else if ( event
.RightDClick() )
3725 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3727 // no default action at the moment
3732 void wxGrid::ChangeCursorMode(CursorMode mode
,
3737 static const wxChar
*cursorModes
[] =
3746 wxLogTrace(_T("grid"),
3747 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3748 win
== m_colLabelWin
? _T("colLabelWin")
3749 : win
? _T("rowLabelWin")
3751 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3752 #endif // __WXDEBUG__
3754 if ( mode
== m_cursorMode
)
3759 // by default use the grid itself
3765 m_winCapture
->ReleaseMouse();
3766 m_winCapture
= (wxWindow
*)NULL
;
3769 m_cursorMode
= mode
;
3771 switch ( m_cursorMode
)
3773 case WXGRID_CURSOR_RESIZE_ROW
:
3774 win
->SetCursor( m_rowResizeCursor
);
3777 case WXGRID_CURSOR_RESIZE_COL
:
3778 win
->SetCursor( m_colResizeCursor
);
3782 win
->SetCursor( *wxSTANDARD_CURSOR
);
3785 // we need to capture mouse when resizing
3786 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3787 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3789 if ( captureMouse
&& resize
)
3791 win
->CaptureMouse();
3796 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3799 wxPoint
pos( event
.GetPosition() );
3800 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3802 wxGridCellCoords coords
;
3803 XYToCell( x
, y
, coords
);
3805 if ( event
.Dragging() )
3807 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3809 // Don't start doing anything until the mouse has been drug at
3810 // least 3 pixels in any direction...
3813 if (m_startDragPos
== wxDefaultPosition
)
3815 m_startDragPos
= pos
;
3818 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3822 m_isDragging
= TRUE
;
3823 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3825 // Hide the edit control, so it
3826 // won't interfer with drag-shrinking.
3827 if ( IsCellEditControlEnabled() )
3828 HideCellEditControl();
3830 // Have we captured the mouse yet?
3833 m_winCapture
= m_gridWin
;
3834 m_winCapture
->CaptureMouse();
3837 if ( coords
!= wxGridNoCellCoords
)
3839 if ( !IsSelection() )
3841 SelectBlock( coords
, coords
);
3845 SelectBlock( m_currentCellCoords
, coords
);
3848 if (! IsVisible(coords
))
3850 MakeCellVisible(coords
);
3851 // TODO: need to introduce a delay or something here. The
3852 // scrolling is way to fast, at least on MSW.
3856 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3858 int cw
, ch
, left
, dummy
;
3859 m_gridWin
->GetClientSize( &cw
, &ch
);
3860 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3862 wxClientDC
dc( m_gridWin
);
3864 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3865 dc
.SetLogicalFunction(wxINVERT
);
3866 if ( m_dragLastPos
>= 0 )
3868 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3870 dc
.DrawLine( left
, y
, left
+cw
, y
);
3873 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3875 int cw
, ch
, dummy
, top
;
3876 m_gridWin
->GetClientSize( &cw
, &ch
);
3877 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3879 wxClientDC
dc( m_gridWin
);
3881 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3882 GetColMinimalWidth(m_dragRowOrCol
) );
3883 dc
.SetLogicalFunction(wxINVERT
);
3884 if ( m_dragLastPos
>= 0 )
3886 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3888 dc
.DrawLine( x
, top
, x
, top
+ch
);
3895 m_isDragging
= FALSE
;
3896 m_startDragPos
= wxDefaultPosition
;
3899 if ( coords
!= wxGridNoCellCoords
)
3901 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3902 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3905 if ( event
.Entering() || event
.Leaving() )
3907 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3908 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3913 // ------------ Left button pressed
3915 if ( event
.LeftDown() )
3917 DisableCellEditControl();
3918 if ( event
.ShiftDown() )
3920 SelectBlock( m_currentCellCoords
, coords
);
3922 else if ( XToEdgeOfCol(x
) < 0 &&
3923 YToEdgeOfRow(y
) < 0 )
3925 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3930 MakeCellVisible( coords
);
3932 // if this is the second click on this cell then start
3934 if ( m_waitForSlowClick
&&
3935 (coords
== m_currentCellCoords
) &&
3936 CanEnableCellControl())
3938 EnableCellEditControl();
3940 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3941 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
3944 m_waitForSlowClick
= FALSE
;
3948 SetCurrentCell( coords
);
3949 m_waitForSlowClick
= TRUE
;
3956 // ------------ Left double click
3958 else if ( event
.LeftDClick() )
3960 DisableCellEditControl();
3961 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3963 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3971 // ------------ Left button released
3973 else if ( event
.LeftUp() )
3975 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3977 if ( IsSelection() )
3981 m_winCapture
->ReleaseMouse();
3982 m_winCapture
= NULL
;
3984 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3987 // Show the edit control, if it has been hidden for
3989 ShowCellEditControl();
3991 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3993 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3994 DoEndDragResizeRow();
3996 // Note: we are ending the event *after* doing
3997 // default processing in this case
3999 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4001 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4003 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4004 DoEndDragResizeCol();
4006 // Note: we are ending the event *after* doing
4007 // default processing in this case
4009 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4016 // ------------ Right button down
4018 else if ( event
.RightDown() )
4020 DisableCellEditControl();
4021 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4026 // no default action at the moment
4031 // ------------ Right double click
4033 else if ( event
.RightDClick() )
4035 DisableCellEditControl();
4036 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4041 // no default action at the moment
4045 // ------------ Moving and no button action
4047 else if ( event
.Moving() && !event
.IsButton() )
4049 int dragRow
= YToEdgeOfRow( y
);
4050 int dragCol
= XToEdgeOfCol( x
);
4052 // Dragging on the corner of a cell to resize in both
4053 // directions is not implemented yet...
4055 if ( dragRow
>= 0 && dragCol
>= 0 )
4057 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4063 m_dragRowOrCol
= dragRow
;
4065 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4067 if ( CanDragRowSize() )
4068 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4076 m_dragRowOrCol
= dragCol
;
4078 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4080 if ( CanDragColSize() )
4081 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4087 // Neither on a row or col edge
4089 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4091 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4098 void wxGrid::DoEndDragResizeRow()
4100 if ( m_dragLastPos
>= 0 )
4102 // erase the last line and resize the row
4104 int cw
, ch
, left
, dummy
;
4105 m_gridWin
->GetClientSize( &cw
, &ch
);
4106 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4108 wxClientDC
dc( m_gridWin
);
4110 dc
.SetLogicalFunction( wxINVERT
);
4111 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4112 HideCellEditControl();
4114 int rowTop
= GetRowTop(m_dragRowOrCol
);
4115 SetRowSize( m_dragRowOrCol
,
4116 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4118 if ( !GetBatchCount() )
4120 // Only needed to get the correct rect.y:
4121 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4123 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4124 rect
.width
= m_rowLabelWidth
;
4125 rect
.height
= ch
- rect
.y
;
4126 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4128 m_gridWin
->Refresh( FALSE
, &rect
);
4131 ShowCellEditControl();
4136 void wxGrid::DoEndDragResizeCol()
4138 if ( m_dragLastPos
>= 0 )
4140 // erase the last line and resize the col
4142 int cw
, ch
, dummy
, top
;
4143 m_gridWin
->GetClientSize( &cw
, &ch
);
4144 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4146 wxClientDC
dc( m_gridWin
);
4148 dc
.SetLogicalFunction( wxINVERT
);
4149 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4150 HideCellEditControl();
4152 int colLeft
= GetColLeft(m_dragRowOrCol
);
4153 SetColSize( m_dragRowOrCol
,
4154 wxMax( m_dragLastPos
- colLeft
,
4155 GetColMinimalWidth(m_dragRowOrCol
) ) );
4157 if ( !GetBatchCount() )
4159 // Only needed to get the correct rect.x:
4160 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4162 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4163 rect
.width
= cw
- rect
.x
;
4164 rect
.height
= m_colLabelHeight
;
4165 m_colLabelWin
->Refresh( TRUE
, &rect
);
4167 m_gridWin
->Refresh( FALSE
, &rect
);
4170 ShowCellEditControl();
4177 // ------ interaction with data model
4179 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4181 switch ( msg
.GetId() )
4183 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4184 return GetModelValues();
4186 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4187 return SetModelValues();
4189 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4190 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4191 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4192 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4193 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4194 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4195 return Redimension( msg
);
4204 // The behaviour of this function depends on the grid table class
4205 // Clear() function. For the default wxGridStringTable class the
4206 // behavious is to replace all cell contents with wxEmptyString but
4207 // not to change the number of rows or cols.
4209 void wxGrid::ClearGrid()
4214 SetEditControlValue();
4215 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4220 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4222 // TODO: something with updateLabels flag
4226 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4232 if (IsCellEditControlEnabled())
4233 DisableCellEditControl();
4235 bool ok
= m_table
->InsertRows( pos
, numRows
);
4237 // the table will have sent the results of the insert row
4238 // operation to this view object as a grid table message
4242 if ( m_numCols
== 0 )
4244 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4246 // TODO: perhaps instead of appending the default number of cols
4247 // we should remember what the last non-zero number of cols was ?
4251 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4253 // if we have just inserted cols into an empty grid the current
4254 // cell will be undefined...
4256 SetCurrentCell( 0, 0 );
4260 if ( !GetBatchCount() ) Refresh();
4263 SetEditControlValue();
4273 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4275 // TODO: something with updateLabels flag
4279 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4283 if ( m_table
&& m_table
->AppendRows( numRows
) )
4285 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4287 // if we have just inserted cols into an empty grid the current
4288 // cell will be undefined...
4290 SetCurrentCell( 0, 0 );
4293 // the table will have sent the results of the append row
4294 // operation to this view object as a grid table message
4297 if ( !GetBatchCount() ) Refresh();
4307 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4309 // TODO: something with updateLabels flag
4313 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4319 if (IsCellEditControlEnabled())
4320 DisableCellEditControl();
4322 if (m_table
->DeleteRows( pos
, numRows
))
4325 // the table will have sent the results of the delete row
4326 // operation to this view object as a grid table message
4329 if ( !GetBatchCount() ) Refresh();
4337 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4339 // TODO: something with updateLabels flag
4343 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4349 if (IsCellEditControlEnabled())
4350 DisableCellEditControl();
4352 bool ok
= m_table
->InsertCols( pos
, numCols
);
4354 // the table will have sent the results of the insert col
4355 // operation to this view object as a grid table message
4359 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4361 // if we have just inserted cols into an empty grid the current
4362 // cell will be undefined...
4364 SetCurrentCell( 0, 0 );
4368 if ( !GetBatchCount() ) Refresh();
4371 SetEditControlValue();
4381 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4383 // TODO: something with updateLabels flag
4387 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4391 if ( m_table
&& m_table
->AppendCols( numCols
) )
4393 // the table will have sent the results of the append col
4394 // operation to this view object as a grid table message
4396 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4398 // if we have just inserted cols into an empty grid the current
4399 // cell will be undefined...
4401 SetCurrentCell( 0, 0 );
4405 if ( !GetBatchCount() ) Refresh();
4415 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4417 // TODO: something with updateLabels flag
4421 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4427 if (IsCellEditControlEnabled())
4428 DisableCellEditControl();
4430 if ( m_table
->DeleteCols( pos
, numCols
) )
4432 // the table will have sent the results of the delete col
4433 // operation to this view object as a grid table message
4436 if ( !GetBatchCount() ) Refresh();
4446 // ----- event handlers
4449 // Generate a grid event based on a mouse event and
4450 // return the result of ProcessEvent()
4452 bool wxGrid::SendEvent( const wxEventType type
,
4454 wxMouseEvent
& mouseEv
)
4456 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4458 int rowOrCol
= (row
== -1 ? col
: row
);
4460 wxGridSizeEvent
gridEvt( GetId(),
4464 mouseEv
.GetX(), mouseEv
.GetY(),
4465 mouseEv
.ControlDown(),
4466 mouseEv
.ShiftDown(),
4468 mouseEv
.MetaDown() );
4470 return GetEventHandler()->ProcessEvent(gridEvt
);
4472 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4474 wxGridRangeSelectEvent
gridEvt( GetId(),
4478 m_selectedBottomRight
,
4479 mouseEv
.ControlDown(),
4480 mouseEv
.ShiftDown(),
4482 mouseEv
.MetaDown() );
4484 return GetEventHandler()->ProcessEvent(gridEvt
);
4488 wxGridEvent
gridEvt( GetId(),
4492 mouseEv
.GetX(), mouseEv
.GetY(),
4493 mouseEv
.ControlDown(),
4494 mouseEv
.ShiftDown(),
4496 mouseEv
.MetaDown() );
4498 return GetEventHandler()->ProcessEvent(gridEvt
);
4503 // Generate a grid event of specified type and return the result
4504 // of ProcessEvent().
4506 bool wxGrid::SendEvent( const wxEventType type
,
4509 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4511 int rowOrCol
= (row
== -1 ? col
: row
);
4513 wxGridSizeEvent
gridEvt( GetId(),
4518 return GetEventHandler()->ProcessEvent(gridEvt
);
4522 wxGridEvent
gridEvt( GetId(),
4527 return GetEventHandler()->ProcessEvent(gridEvt
);
4532 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4534 wxPaintDC
dc( this );
4536 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4537 m_numRows
&& m_numCols
)
4539 m_currentCellCoords
.Set(0, 0);
4540 SetEditControlValue();
4541 ShowCellEditControl();
4548 // This is just here to make sure that CalcDimensions gets called when
4549 // the grid view is resized... then the size event is skipped to allow
4550 // the box sizers to handle everything
4552 void wxGrid::OnSize( wxSizeEvent
& event
)
4559 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4561 if ( m_inOnKeyDown
)
4563 // shouldn't be here - we are going round in circles...
4565 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4568 m_inOnKeyDown
= TRUE
;
4570 // propagate the event up and see if it gets processed
4572 wxWindow
*parent
= GetParent();
4573 wxKeyEvent
keyEvt( event
);
4574 keyEvt
.SetEventObject( parent
);
4576 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4579 // TODO: Should also support Shift-cursor keys for
4580 // extending the selection. Maybe add a flag to
4581 // MoveCursorXXX() and MoveCursorXXXBlock() and
4582 // just send event.ShiftDown().
4584 // try local handlers
4586 switch ( event
.KeyCode() )
4589 if ( event
.ControlDown() )
4591 MoveCursorUpBlock();
4600 if ( event
.ControlDown() )
4602 MoveCursorDownBlock();
4611 if ( event
.ControlDown() )
4613 MoveCursorLeftBlock();
4622 if ( event
.ControlDown() )
4624 MoveCursorRightBlock();
4633 if ( event
.ControlDown() )
4635 event
.Skip(); // to let the edit control have the return
4644 if (event
.ShiftDown())
4651 if ( event
.ControlDown() )
4653 MakeCellVisible( 0, 0 );
4654 SetCurrentCell( 0, 0 );
4663 if ( event
.ControlDown() )
4665 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4666 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4682 // We don't want these keys to trigger the edit control, any others?
4691 if ( !IsEditable() )
4696 // Otherwise fall through to default
4699 // now try the cell edit control
4701 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4703 EnableCellEditControl();
4704 int row
= m_currentCellCoords
.GetRow();
4705 int col
= m_currentCellCoords
.GetCol();
4706 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4707 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4712 // let others process char events for readonly cells
4719 m_inOnKeyDown
= FALSE
;
4723 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4727 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4729 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4731 // the event has been intercepted - do nothing
4736 m_currentCellCoords
!= wxGridNoCellCoords
)
4738 HideCellEditControl();
4739 // RD: Does disabling this cause any problems? It's called again
4740 // in DisableCellEditControl...
4741 // SaveEditControlValue();
4742 DisableCellEditControl();
4744 // Clear the old current cell highlight
4745 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4747 // Otherwise refresh redraws the highlight!
4748 m_currentCellCoords
= coords
;
4750 m_gridWin
->Refresh( FALSE
, &r
);
4753 m_currentCellCoords
= coords
;
4755 SetEditControlValue();
4759 wxClientDC
dc(m_gridWin
);
4762 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4763 DrawCellHighlight(dc
, attr
);
4766 if ( IsSelection() )
4768 wxRect
r( SelectionToDeviceRect() );
4770 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4777 // ------ functions to get/send data (see also public functions)
4780 bool wxGrid::GetModelValues()
4784 // all we need to do is repaint the grid
4786 m_gridWin
->Refresh();
4794 bool wxGrid::SetModelValues()
4800 for ( row
= 0; row
< m_numRows
; row
++ )
4802 for ( col
= 0; col
< m_numCols
; col
++ )
4804 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4816 // Note - this function only draws cells that are in the list of
4817 // exposed cells (usually set from the update region by
4818 // CalcExposedCells)
4820 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4822 if ( !m_numRows
|| !m_numCols
) return;
4825 size_t numCells
= m_cellsExposed
.GetCount();
4827 for ( i
= 0; i
< numCells
; i
++ )
4829 DrawCell( dc
, m_cellsExposed
[i
] );
4834 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4836 int row
= coords
.GetRow();
4837 int col
= coords
.GetCol();
4839 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4842 // we draw the cell border ourselves
4843 #if !WXGRID_DRAW_LINES
4844 if ( m_gridLinesEnabled
)
4845 DrawCellBorder( dc
, coords
);
4848 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4850 bool isCurrent
= coords
== m_currentCellCoords
;
4853 rect
.x
= GetColLeft(col
);
4854 rect
.y
= GetRowTop(row
);
4855 rect
.width
= GetColWidth(col
) - 1;
4856 rect
.height
= GetRowHeight(row
) - 1;
4858 // if the editor is shown, we should use it and not the renderer
4859 if ( isCurrent
&& IsCellEditControlEnabled() )
4861 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4865 // but all the rest is drawn by the cell renderer and hence may be
4867 attr
->GetRenderer(this, row
, col
)->
4868 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4875 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4877 int row
= m_currentCellCoords
.GetRow();
4878 int col
= m_currentCellCoords
.GetCol();
4880 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4884 rect
.x
= GetColLeft(col
);
4885 rect
.y
= GetRowTop(row
);
4886 rect
.width
= GetColWidth(col
) - 1;
4887 rect
.height
= GetRowHeight(row
) - 1;
4889 // hmmm... what could we do here to show that the cell is disabled?
4890 // for now, I just draw a thinner border than for the other ones, but
4891 // it doesn't look really good
4892 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4893 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4895 dc
.DrawRectangle(rect
);
4898 // VZ: my experiments with 3d borders...
4900 // how to properly set colours for arbitrary bg?
4901 wxCoord x1
= rect
.x
,
4903 x2
= rect
.x
+ rect
.width
-1,
4904 y2
= rect
.y
+ rect
.height
-1;
4906 dc
.SetPen(*wxWHITE_PEN
);
4907 dc
.DrawLine(x1
, y1
, x2
, y1
);
4908 dc
.DrawLine(x1
, y1
, x1
, y2
);
4910 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4911 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4913 dc
.SetPen(*wxBLACK_PEN
);
4914 dc
.DrawLine(x1
, y2
, x2
, y2
);
4915 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4920 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4922 int row
= coords
.GetRow();
4923 int col
= coords
.GetCol();
4924 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4927 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4929 // right hand border
4931 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
4932 GetColRight(col
), GetRowBottom(row
) );
4936 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
4937 GetColRight(col
), GetRowBottom(row
) );
4940 void wxGrid::DrawHighlight(wxDC
& dc
)
4942 if ( IsCellEditControlEnabled() )
4944 // don't show highlight when the edit control is shown
4948 // if the active cell was repainted, repaint its highlight too because it
4949 // might have been damaged by the grid lines
4950 size_t count
= m_cellsExposed
.GetCount();
4951 for ( size_t n
= 0; n
< count
; n
++ )
4953 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4955 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4956 DrawCellHighlight(dc
, attr
);
4964 // TODO: remove this ???
4965 // This is used to redraw all grid lines e.g. when the grid line colour
4968 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4970 if ( !m_gridLinesEnabled
||
4972 !m_numCols
) return;
4974 int top
, bottom
, left
, right
;
4980 m_gridWin
->GetClientSize(&cw
, &ch
);
4982 // virtual coords of visible area
4984 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4985 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4990 reg
.GetBox(x
, y
, w
, h
);
4991 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4992 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4996 m_gridWin
->GetClientSize(&cw
, &ch
);
4997 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4998 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5001 // avoid drawing grid lines past the last row and col
5003 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5004 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5006 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5008 // horizontal grid lines
5011 for ( i
= 0; i
< m_numRows
; i
++ )
5013 int bot
= GetRowBottom(i
) - 1;
5022 dc
.DrawLine( left
, bot
, right
, bot
);
5027 // vertical grid lines
5029 for ( i
= 0; i
< m_numCols
; i
++ )
5031 int colRight
= GetColRight(i
) - 1;
5032 if ( colRight
> right
)
5037 if ( colRight
>= left
)
5039 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5045 void wxGrid::DrawRowLabels( wxDC
& dc
)
5047 if ( !m_numRows
|| !m_numCols
) return;
5050 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5052 for ( i
= 0; i
< numLabels
; i
++ )
5054 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5059 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5061 if ( GetRowHeight(row
) <= 0 )
5064 int rowTop
= GetRowTop(row
),
5065 rowBottom
= GetRowBottom(row
) - 1;
5067 dc
.SetPen( *wxBLACK_PEN
);
5068 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5069 m_rowLabelWidth
-1, rowBottom
);
5071 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5073 dc
.SetPen( *wxWHITE_PEN
);
5074 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5075 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5077 dc
.SetBackgroundMode( wxTRANSPARENT
);
5078 dc
.SetTextForeground( GetLabelTextColour() );
5079 dc
.SetFont( GetLabelFont() );
5082 GetRowLabelAlignment( &hAlign
, &vAlign
);
5086 rect
.SetY( GetRowTop(row
) + 2 );
5087 rect
.SetWidth( m_rowLabelWidth
- 4 );
5088 rect
.SetHeight( GetRowHeight(row
) - 4 );
5089 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5093 void wxGrid::DrawColLabels( wxDC
& dc
)
5095 if ( !m_numRows
|| !m_numCols
) return;
5098 size_t numLabels
= m_colLabelsExposed
.GetCount();
5100 for ( i
= 0; i
< numLabels
; i
++ )
5102 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5107 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5109 if ( GetColWidth(col
) <= 0 )
5112 int colLeft
= GetColLeft(col
),
5113 colRight
= GetColRight(col
) - 1;
5115 dc
.SetPen( *wxBLACK_PEN
);
5116 dc
.DrawLine( colRight
, 0,
5117 colRight
, m_colLabelHeight
-1 );
5119 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5120 colRight
, m_colLabelHeight
-1 );
5122 dc
.SetPen( *wxWHITE_PEN
);
5123 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5124 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5126 dc
.SetBackgroundMode( wxTRANSPARENT
);
5127 dc
.SetTextForeground( GetLabelTextColour() );
5128 dc
.SetFont( GetLabelFont() );
5130 dc
.SetBackgroundMode( wxTRANSPARENT
);
5131 dc
.SetTextForeground( GetLabelTextColour() );
5132 dc
.SetFont( GetLabelFont() );
5135 GetColLabelAlignment( &hAlign
, &vAlign
);
5138 rect
.SetX( colLeft
+ 2 );
5140 rect
.SetWidth( GetColWidth(col
) - 4 );
5141 rect
.SetHeight( m_colLabelHeight
- 4 );
5142 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5146 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5147 const wxString
& value
,
5152 long textWidth
, textHeight
;
5153 long lineWidth
, lineHeight
;
5154 wxArrayString lines
;
5156 dc
.SetClippingRegion( rect
);
5157 StringToLines( value
, lines
);
5158 if ( lines
.GetCount() )
5160 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5161 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5164 switch ( horizAlign
)
5167 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5171 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5180 switch ( vertAlign
)
5183 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5187 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5196 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5198 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5203 dc
.DestroyClippingRegion();
5207 // Split multi line text up into an array of strings. Any existing
5208 // contents of the string array are preserved.
5210 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5214 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5215 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5217 while ( startPos
< (int)tVal
.Length() )
5219 pos
= tVal
.Mid(startPos
).Find( eol
);
5224 else if ( pos
== 0 )
5226 lines
.Add( wxEmptyString
);
5230 lines
.Add( value
.Mid(startPos
, pos
) );
5234 if ( startPos
< (int)value
.Length() )
5236 lines
.Add( value
.Mid( startPos
) );
5241 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5242 wxArrayString
& lines
,
5243 long *width
, long *height
)
5250 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5252 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5253 w
= wxMax( w
, lineW
);
5263 // ------ Edit control functions
5267 void wxGrid::EnableEditing( bool edit
)
5269 // TODO: improve this ?
5271 if ( edit
!= m_editable
)
5275 // FIXME IMHO this won't disable the edit control if edit == FALSE
5276 // because of the check in the beginning of
5277 // EnableCellEditControl() just below (VZ)
5278 EnableCellEditControl(m_editable
);
5283 void wxGrid::EnableCellEditControl( bool enable
)
5288 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5289 SetCurrentCell( 0, 0 );
5291 if ( enable
!= m_cellEditCtrlEnabled
)
5293 // TODO allow the app to Veto() this event?
5294 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5298 // this should be checked by the caller!
5299 wxASSERT_MSG( CanEnableCellControl(),
5300 _T("can't enable editing for this cell!") );
5302 // do it before ShowCellEditControl()
5303 m_cellEditCtrlEnabled
= enable
;
5305 SetEditControlValue();
5306 ShowCellEditControl();
5310 HideCellEditControl();
5311 SaveEditControlValue();
5313 // do it after HideCellEditControl()
5314 m_cellEditCtrlEnabled
= enable
;
5319 bool wxGrid::IsCurrentCellReadOnly() const
5322 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5323 bool readonly
= attr
->IsReadOnly();
5329 bool wxGrid::CanEnableCellControl() const
5331 return m_editable
&& !IsCurrentCellReadOnly();
5334 bool wxGrid::IsCellEditControlEnabled() const
5336 // the cell edit control might be disable for all cells or just for the
5337 // current one if it's read only
5338 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5341 void wxGrid::ShowCellEditControl()
5343 if ( IsCellEditControlEnabled() )
5345 if ( !IsVisible( m_currentCellCoords
) )
5351 wxRect rect
= CellToRect( m_currentCellCoords
);
5352 int row
= m_currentCellCoords
.GetRow();
5353 int col
= m_currentCellCoords
.GetCol();
5355 // convert to scrolled coords
5357 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5359 // done in PaintBackground()
5361 // erase the highlight and the cell contents because the editor
5362 // might not cover the entire cell
5363 wxClientDC
dc( m_gridWin
);
5365 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5366 dc
.SetPen(*wxTRANSPARENT_PEN
);
5367 dc
.DrawRectangle(rect
);
5370 // cell is shifted by one pixel
5374 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5375 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5376 if ( !editor
->IsCreated() )
5378 editor
->Create(m_gridWin
, -1,
5379 new wxGridCellEditorEvtHandler(this, editor
));
5382 editor
->SetSize( rect
);
5384 editor
->Show( TRUE
, attr
);
5385 editor
->BeginEdit(row
, col
, this);
5392 void wxGrid::HideCellEditControl()
5394 if ( IsCellEditControlEnabled() )
5396 int row
= m_currentCellCoords
.GetRow();
5397 int col
= m_currentCellCoords
.GetCol();
5399 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5400 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5402 m_gridWin
->SetFocus();
5407 void wxGrid::SetEditControlValue( const wxString
& value
)
5409 // RD: The new Editors get the value from the table themselves now. This
5410 // method can probably be removed...
5414 void wxGrid::SaveEditControlValue()
5416 if ( IsCellEditControlEnabled() )
5418 int row
= m_currentCellCoords
.GetRow();
5419 int col
= m_currentCellCoords
.GetCol();
5421 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5422 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5423 bool changed
= editor
->EndEdit(row
, col
, TRUE
, this);
5429 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5430 m_currentCellCoords
.GetRow(),
5431 m_currentCellCoords
.GetCol() );
5438 // ------ Grid location functions
5439 // Note that all of these functions work with the logical coordinates of
5440 // grid cells and labels so you will need to convert from device
5441 // coordinates for mouse events etc.
5444 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5446 int row
= YToRow(y
);
5447 int col
= XToCol(x
);
5449 if ( row
== -1 || col
== -1 )
5451 coords
= wxGridNoCellCoords
;
5455 coords
.Set( row
, col
);
5460 int wxGrid::YToRow( int y
)
5464 for ( i
= 0; i
< m_numRows
; i
++ )
5466 if ( y
< GetRowBottom(i
) )
5474 int wxGrid::XToCol( int x
)
5478 for ( i
= 0; i
< m_numCols
; i
++ )
5480 if ( x
< GetColRight(i
) )
5488 // return the row number that that the y coord is near the edge of, or
5489 // -1 if not near an edge
5491 int wxGrid::YToEdgeOfRow( int y
)
5495 for ( i
= 0; i
< m_numRows
; i
++ )
5497 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5499 d
= abs( y
- GetRowBottom(i
) );
5500 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5509 // return the col number that that the x coord is near the edge of, or
5510 // -1 if not near an edge
5512 int wxGrid::XToEdgeOfCol( int x
)
5516 for ( i
= 0; i
< m_numCols
; i
++ )
5518 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5520 d
= abs( x
- GetColRight(i
) );
5521 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5530 wxRect
wxGrid::CellToRect( int row
, int col
)
5532 wxRect
rect( -1, -1, -1, -1 );
5534 if ( row
>= 0 && row
< m_numRows
&&
5535 col
>= 0 && col
< m_numCols
)
5537 rect
.x
= GetColLeft(col
);
5538 rect
.y
= GetRowTop(row
);
5539 rect
.width
= GetColWidth(col
);
5540 rect
.height
= GetRowHeight(row
);
5547 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5549 // get the cell rectangle in logical coords
5551 wxRect
r( CellToRect( row
, col
) );
5553 // convert to device coords
5555 int left
, top
, right
, bottom
;
5556 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5557 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5559 // check against the client area of the grid window
5562 m_gridWin
->GetClientSize( &cw
, &ch
);
5564 if ( wholeCellVisible
)
5566 // is the cell wholly visible ?
5568 return ( left
>= 0 && right
<= cw
&&
5569 top
>= 0 && bottom
<= ch
);
5573 // is the cell partly visible ?
5575 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5576 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5581 // make the specified cell location visible by doing a minimal amount
5584 void wxGrid::MakeCellVisible( int row
, int col
)
5587 int xpos
= -1, ypos
= -1;
5589 if ( row
>= 0 && row
< m_numRows
&&
5590 col
>= 0 && col
< m_numCols
)
5592 // get the cell rectangle in logical coords
5594 wxRect
r( CellToRect( row
, col
) );
5596 // convert to device coords
5598 int left
, top
, right
, bottom
;
5599 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5600 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5603 m_gridWin
->GetClientSize( &cw
, &ch
);
5609 else if ( bottom
> ch
)
5611 int h
= r
.GetHeight();
5613 for ( i
= row
-1; i
>= 0; i
-- )
5615 int rowHeight
= GetRowHeight(i
);
5616 if ( h
+ rowHeight
> ch
)
5623 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5624 // have rounding errors (this is important, because if we do, we
5625 // might not scroll at all and some cells won't be redrawn)
5626 ypos
+= GRID_SCROLL_LINE
/ 2;
5633 else if ( right
> cw
)
5635 int w
= r
.GetWidth();
5637 for ( i
= col
-1; i
>= 0; i
-- )
5639 int colWidth
= GetColWidth(i
);
5640 if ( w
+ colWidth
> cw
)
5647 // see comment for ypos above
5648 xpos
+= GRID_SCROLL_LINE
/ 2;
5651 if ( xpos
!= -1 || ypos
!= -1 )
5653 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5654 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5655 Scroll( xpos
, ypos
);
5663 // ------ Grid cursor movement functions
5666 bool wxGrid::MoveCursorUp()
5668 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5669 m_currentCellCoords
.GetRow() > 0 )
5671 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5672 m_currentCellCoords
.GetCol() );
5674 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5675 m_currentCellCoords
.GetCol() );
5684 bool wxGrid::MoveCursorDown()
5686 // TODO: allow for scrolling
5688 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5689 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5691 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5692 m_currentCellCoords
.GetCol() );
5694 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5695 m_currentCellCoords
.GetCol() );
5704 bool wxGrid::MoveCursorLeft()
5706 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5707 m_currentCellCoords
.GetCol() > 0 )
5709 MakeCellVisible( m_currentCellCoords
.GetRow(),
5710 m_currentCellCoords
.GetCol() - 1 );
5712 SetCurrentCell( m_currentCellCoords
.GetRow(),
5713 m_currentCellCoords
.GetCol() - 1 );
5722 bool wxGrid::MoveCursorRight()
5724 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5725 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5727 MakeCellVisible( m_currentCellCoords
.GetRow(),
5728 m_currentCellCoords
.GetCol() + 1 );
5730 SetCurrentCell( m_currentCellCoords
.GetRow(),
5731 m_currentCellCoords
.GetCol() + 1 );
5740 bool wxGrid::MovePageUp()
5742 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5744 int row
= m_currentCellCoords
.GetRow();
5748 m_gridWin
->GetClientSize( &cw
, &ch
);
5750 int y
= GetRowTop(row
);
5751 int newRow
= YToRow( y
- ch
+ 1 );
5756 else if ( newRow
== row
)
5761 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5762 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5770 bool wxGrid::MovePageDown()
5772 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5774 int row
= m_currentCellCoords
.GetRow();
5775 if ( row
< m_numRows
)
5778 m_gridWin
->GetClientSize( &cw
, &ch
);
5780 int y
= GetRowTop(row
);
5781 int newRow
= YToRow( y
+ ch
);
5784 newRow
= m_numRows
- 1;
5786 else if ( newRow
== row
)
5791 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5792 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5800 bool wxGrid::MoveCursorUpBlock()
5803 m_currentCellCoords
!= wxGridNoCellCoords
&&
5804 m_currentCellCoords
.GetRow() > 0 )
5806 int row
= m_currentCellCoords
.GetRow();
5807 int col
= m_currentCellCoords
.GetCol();
5809 if ( m_table
->IsEmptyCell(row
, col
) )
5811 // starting in an empty cell: find the next block of
5817 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5820 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5822 // starting at the top of a block: find the next block
5828 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5833 // starting within a block: find the top of the block
5838 if ( m_table
->IsEmptyCell(row
, col
) )
5846 MakeCellVisible( row
, col
);
5847 SetCurrentCell( row
, col
);
5855 bool wxGrid::MoveCursorDownBlock()
5858 m_currentCellCoords
!= wxGridNoCellCoords
&&
5859 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5861 int row
= m_currentCellCoords
.GetRow();
5862 int col
= m_currentCellCoords
.GetCol();
5864 if ( m_table
->IsEmptyCell(row
, col
) )
5866 // starting in an empty cell: find the next block of
5869 while ( row
< m_numRows
-1 )
5872 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5875 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5877 // starting at the bottom of a block: find the next block
5880 while ( row
< m_numRows
-1 )
5883 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5888 // starting within a block: find the bottom of the block
5890 while ( row
< m_numRows
-1 )
5893 if ( m_table
->IsEmptyCell(row
, col
) )
5901 MakeCellVisible( row
, col
);
5902 SetCurrentCell( row
, col
);
5910 bool wxGrid::MoveCursorLeftBlock()
5913 m_currentCellCoords
!= wxGridNoCellCoords
&&
5914 m_currentCellCoords
.GetCol() > 0 )
5916 int row
= m_currentCellCoords
.GetRow();
5917 int col
= m_currentCellCoords
.GetCol();
5919 if ( m_table
->IsEmptyCell(row
, col
) )
5921 // starting in an empty cell: find the next block of
5927 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5930 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5932 // starting at the left of a block: find the next block
5938 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5943 // starting within a block: find the left of the block
5948 if ( m_table
->IsEmptyCell(row
, col
) )
5956 MakeCellVisible( row
, col
);
5957 SetCurrentCell( row
, col
);
5965 bool wxGrid::MoveCursorRightBlock()
5968 m_currentCellCoords
!= wxGridNoCellCoords
&&
5969 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5971 int row
= m_currentCellCoords
.GetRow();
5972 int col
= m_currentCellCoords
.GetCol();
5974 if ( m_table
->IsEmptyCell(row
, col
) )
5976 // starting in an empty cell: find the next block of
5979 while ( col
< m_numCols
-1 )
5982 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5985 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5987 // starting at the right of a block: find the next block
5990 while ( col
< m_numCols
-1 )
5993 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5998 // starting within a block: find the right of the block
6000 while ( col
< m_numCols
-1 )
6003 if ( m_table
->IsEmptyCell(row
, col
) )
6011 MakeCellVisible( row
, col
);
6012 SetCurrentCell( row
, col
);
6023 // ------ Label values and formatting
6026 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6028 *horiz
= m_rowLabelHorizAlign
;
6029 *vert
= m_rowLabelVertAlign
;
6032 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6034 *horiz
= m_colLabelHorizAlign
;
6035 *vert
= m_colLabelVertAlign
;
6038 wxString
wxGrid::GetRowLabelValue( int row
)
6042 return m_table
->GetRowLabelValue( row
);
6052 wxString
wxGrid::GetColLabelValue( int col
)
6056 return m_table
->GetColLabelValue( col
);
6067 void wxGrid::SetRowLabelSize( int width
)
6069 width
= wxMax( width
, 0 );
6070 if ( width
!= m_rowLabelWidth
)
6074 m_rowLabelWin
->Show( FALSE
);
6075 m_cornerLabelWin
->Show( FALSE
);
6077 else if ( m_rowLabelWidth
== 0 )
6079 m_rowLabelWin
->Show( TRUE
);
6080 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6083 m_rowLabelWidth
= width
;
6090 void wxGrid::SetColLabelSize( int height
)
6092 height
= wxMax( height
, 0 );
6093 if ( height
!= m_colLabelHeight
)
6097 m_colLabelWin
->Show( FALSE
);
6098 m_cornerLabelWin
->Show( FALSE
);
6100 else if ( m_colLabelHeight
== 0 )
6102 m_colLabelWin
->Show( TRUE
);
6103 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6106 m_colLabelHeight
= height
;
6113 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6115 if ( m_labelBackgroundColour
!= colour
)
6117 m_labelBackgroundColour
= colour
;
6118 m_rowLabelWin
->SetBackgroundColour( colour
);
6119 m_colLabelWin
->SetBackgroundColour( colour
);
6120 m_cornerLabelWin
->SetBackgroundColour( colour
);
6122 if ( !GetBatchCount() )
6124 m_rowLabelWin
->Refresh();
6125 m_colLabelWin
->Refresh();
6126 m_cornerLabelWin
->Refresh();
6131 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6133 if ( m_labelTextColour
!= colour
)
6135 m_labelTextColour
= colour
;
6136 if ( !GetBatchCount() )
6138 m_rowLabelWin
->Refresh();
6139 m_colLabelWin
->Refresh();
6144 void wxGrid::SetLabelFont( const wxFont
& font
)
6147 if ( !GetBatchCount() )
6149 m_rowLabelWin
->Refresh();
6150 m_colLabelWin
->Refresh();
6154 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6156 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6158 m_rowLabelHorizAlign
= horiz
;
6161 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6163 m_rowLabelVertAlign
= vert
;
6166 if ( !GetBatchCount() )
6168 m_rowLabelWin
->Refresh();
6172 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6174 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6176 m_colLabelHorizAlign
= horiz
;
6179 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6181 m_colLabelVertAlign
= vert
;
6184 if ( !GetBatchCount() )
6186 m_colLabelWin
->Refresh();
6190 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6194 m_table
->SetRowLabelValue( row
, s
);
6195 if ( !GetBatchCount() )
6197 wxRect rect
= CellToRect( row
, 0);
6198 if ( rect
.height
> 0 )
6200 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6202 rect
.width
= m_rowLabelWidth
;
6203 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6209 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6213 m_table
->SetColLabelValue( col
, s
);
6214 if ( !GetBatchCount() )
6216 wxRect rect
= CellToRect( 0, col
);
6217 if ( rect
.width
> 0 )
6219 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6221 rect
.height
= m_colLabelHeight
;
6222 m_colLabelWin
->Refresh( TRUE
, &rect
);
6228 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6230 if ( m_gridLineColour
!= colour
)
6232 m_gridLineColour
= colour
;
6234 wxClientDC
dc( m_gridWin
);
6236 DrawAllGridLines( dc
, wxRegion() );
6240 void wxGrid::EnableGridLines( bool enable
)
6242 if ( enable
!= m_gridLinesEnabled
)
6244 m_gridLinesEnabled
= enable
;
6246 if ( !GetBatchCount() )
6250 wxClientDC
dc( m_gridWin
);
6252 DrawAllGridLines( dc
, wxRegion() );
6256 m_gridWin
->Refresh();
6263 int wxGrid::GetDefaultRowSize()
6265 return m_defaultRowHeight
;
6268 int wxGrid::GetRowSize( int row
)
6270 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6272 return GetRowHeight(row
);
6275 int wxGrid::GetDefaultColSize()
6277 return m_defaultColWidth
;
6280 int wxGrid::GetColSize( int col
)
6282 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6284 return GetColWidth(col
);
6287 // ============================================================================
6288 // access to the grid attributes: each of them has a default value in the grid
6289 // itself and may be overidden on a per-cell basis
6290 // ============================================================================
6292 // ----------------------------------------------------------------------------
6293 // setting default attributes
6294 // ----------------------------------------------------------------------------
6296 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6298 m_defaultCellAttr
->SetBackgroundColour(col
);
6300 m_gridWin
->SetBackgroundColour(col
);
6304 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6306 m_defaultCellAttr
->SetTextColour(col
);
6309 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6311 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6314 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6316 m_defaultCellAttr
->SetFont(font
);
6319 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6321 m_defaultCellAttr
->SetRenderer(renderer
);
6324 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6326 m_defaultCellAttr
->SetEditor(editor
);
6329 // ----------------------------------------------------------------------------
6330 // access to the default attrbiutes
6331 // ----------------------------------------------------------------------------
6333 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6335 return m_defaultCellAttr
->GetBackgroundColour();
6338 wxColour
wxGrid::GetDefaultCellTextColour()
6340 return m_defaultCellAttr
->GetTextColour();
6343 wxFont
wxGrid::GetDefaultCellFont()
6345 return m_defaultCellAttr
->GetFont();
6348 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6350 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6353 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6355 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6358 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6360 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6363 // ----------------------------------------------------------------------------
6364 // access to cell attributes
6365 // ----------------------------------------------------------------------------
6367 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6369 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6370 wxColour colour
= attr
->GetBackgroundColour();
6375 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6377 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6378 wxColour colour
= attr
->GetTextColour();
6383 wxFont
wxGrid::GetCellFont( int row
, int col
)
6385 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6386 wxFont font
= attr
->GetFont();
6391 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6393 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6394 attr
->GetAlignment(horiz
, vert
);
6398 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6400 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6401 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6406 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6408 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6409 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6414 bool wxGrid::IsReadOnly(int row
, int col
) const
6416 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6417 bool isReadOnly
= attr
->IsReadOnly();
6422 // ----------------------------------------------------------------------------
6423 // attribute support: cache, automatic provider creation, ...
6424 // ----------------------------------------------------------------------------
6426 bool wxGrid::CanHaveAttributes()
6433 return m_table
->CanHaveAttributes();
6436 void wxGrid::ClearAttrCache()
6438 if ( m_attrCache
.row
!= -1 )
6440 m_attrCache
.attr
->SafeDecRef();
6441 m_attrCache
.row
= -1;
6445 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6447 wxGrid
*self
= (wxGrid
*)this; // const_cast
6449 self
->ClearAttrCache();
6450 self
->m_attrCache
.row
= row
;
6451 self
->m_attrCache
.col
= col
;
6452 self
->m_attrCache
.attr
= attr
;
6456 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6458 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6460 *attr
= m_attrCache
.attr
;
6461 (*attr
)->SafeIncRef();
6463 #ifdef DEBUG_ATTR_CACHE
6464 gs_nAttrCacheHits
++;
6471 #ifdef DEBUG_ATTR_CACHE
6472 gs_nAttrCacheMisses
++;
6478 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6480 wxGridCellAttr
*attr
;
6481 if ( !LookupAttr(row
, col
, &attr
) )
6483 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6484 CacheAttr(row
, col
, attr
);
6488 attr
->SetDefAttr(m_defaultCellAttr
);
6492 attr
= m_defaultCellAttr
;
6499 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6501 wxGridCellAttr
*attr
;
6502 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6504 wxASSERT_MSG( m_table
,
6505 _T("we may only be called if CanHaveAttributes() "
6506 "returned TRUE and then m_table should be !NULL") );
6508 attr
= m_table
->GetAttr(row
, col
);
6511 attr
= new wxGridCellAttr
;
6513 // artificially inc the ref count to match DecRef() in caller
6516 m_table
->SetAttr(attr
, row
, col
);
6519 CacheAttr(row
, col
, attr
);
6521 attr
->SetDefAttr(m_defaultCellAttr
);
6525 // ----------------------------------------------------------------------------
6526 // setting cell attributes: this is forwarded to the table
6527 // ----------------------------------------------------------------------------
6529 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6531 if ( CanHaveAttributes() )
6533 m_table
->SetRowAttr(attr
, row
);
6541 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6543 if ( CanHaveAttributes() )
6545 m_table
->SetColAttr(attr
, col
);
6553 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6555 if ( CanHaveAttributes() )
6557 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6558 attr
->SetBackgroundColour(colour
);
6563 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6565 if ( CanHaveAttributes() )
6567 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6568 attr
->SetTextColour(colour
);
6573 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6575 if ( CanHaveAttributes() )
6577 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6578 attr
->SetFont(font
);
6583 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6585 if ( CanHaveAttributes() )
6587 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6588 attr
->SetAlignment(horiz
, vert
);
6593 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6595 if ( CanHaveAttributes() )
6597 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6598 attr
->SetRenderer(renderer
);
6603 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6605 if ( CanHaveAttributes() )
6607 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6608 attr
->SetEditor(editor
);
6613 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6615 if ( CanHaveAttributes() )
6617 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6618 attr
->SetReadOnly(isReadOnly
);
6623 // ----------------------------------------------------------------------------
6624 // Data type registration
6625 // ----------------------------------------------------------------------------
6627 void wxGrid::RegisterDataType(const wxString
& typeName
,
6628 wxGridCellRenderer
* renderer
,
6629 wxGridCellEditor
* editor
)
6631 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6635 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6637 wxString typeName
= m_table
->GetTypeName(row
, col
);
6638 return GetDefaultEditorForType(typeName
);
6641 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6643 wxString typeName
= m_table
->GetTypeName(row
, col
);
6644 return GetDefaultRendererForType(typeName
);
6648 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6650 int index
= m_typeRegistry
->FindDataType(typeName
);
6652 // Should we force the failure here or let it fallback to string handling???
6653 // wxFAIL_MSG(wxT("Unknown data type name"));
6656 return m_typeRegistry
->GetEditor(index
);
6660 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6662 int index
= m_typeRegistry
->FindDataType(typeName
);
6664 // Should we force the failure here or let it fallback to string handling???
6665 // wxFAIL_MSG(wxT("Unknown data type name"));
6668 return m_typeRegistry
->GetRenderer(index
);
6672 // ----------------------------------------------------------------------------
6674 // ----------------------------------------------------------------------------
6676 void wxGrid::EnableDragRowSize( bool enable
)
6678 m_canDragRowSize
= enable
;
6682 void wxGrid::EnableDragColSize( bool enable
)
6684 m_canDragColSize
= enable
;
6688 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6690 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6692 if ( resizeExistingRows
)
6700 void wxGrid::SetRowSize( int row
, int height
)
6702 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6704 if ( m_rowHeights
.IsEmpty() )
6706 // need to really create the array
6710 int h
= wxMax( 0, height
);
6711 int diff
= h
- m_rowHeights
[row
];
6713 m_rowHeights
[row
] = h
;
6715 for ( i
= row
; i
< m_numRows
; i
++ )
6717 m_rowBottoms
[i
] += diff
;
6722 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6724 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6726 if ( resizeExistingCols
)
6734 void wxGrid::SetColSize( int col
, int width
)
6736 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6738 // should we check that it's bigger than GetColMinimalWidth(col) here?
6740 if ( m_colWidths
.IsEmpty() )
6742 // need to really create the array
6746 int w
= wxMax( 0, width
);
6747 int diff
= w
- m_colWidths
[col
];
6748 m_colWidths
[col
] = w
;
6751 for ( i
= col
; i
< m_numCols
; i
++ )
6753 m_colRights
[i
] += diff
;
6759 void wxGrid::SetColMinimalWidth( int col
, int width
)
6761 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6764 int wxGrid::GetColMinimalWidth(int col
) const
6766 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6767 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6770 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6772 wxClientDC
dc(m_gridWin
);
6774 wxCoord width
, widthMax
= 0;
6775 for ( int row
= 0; row
< m_numRows
; row
++ )
6777 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6778 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6781 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6782 if ( width
> widthMax
)
6791 // now also compare with the column label width
6792 dc
.SetFont( GetLabelFont() );
6793 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6794 if ( width
> widthMax
)
6801 // empty column - give default width (notice that if widthMax is less
6802 // than default width but != 0, it's ok)
6803 widthMax
= m_defaultColWidth
;
6807 // leave some space around text
6811 SetColSize(col
, widthMax
);
6814 SetColMinimalWidth(col
, widthMax
);
6818 void wxGrid::AutoSizeColumns( bool setAsMin
)
6820 for ( int col
= 0; col
< m_numCols
; col
++ )
6822 AutoSizeColumn(col
, setAsMin
);
6827 // ------ cell value accessor functions
6830 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6834 m_table
->SetValue( row
, col
, s
.c_str() );
6835 if ( !GetBatchCount() )
6837 wxClientDC
dc( m_gridWin
);
6839 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6842 #if 0 // TODO: edit in place
6844 if ( m_currentCellCoords
.GetRow() == row
&&
6845 m_currentCellCoords
.GetCol() == col
)
6847 SetEditControlValue( s
);
6856 // ------ Block, row and col selection
6859 void wxGrid::SelectRow( int row
, bool addToSelected
)
6863 if ( IsSelection() && addToSelected
)
6866 bool need_refresh
[4];
6870 need_refresh
[3] = FALSE
;
6874 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6875 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6876 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6877 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6881 need_refresh
[0] = TRUE
;
6882 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6883 wxGridCellCoords ( oldTop
- 1,
6885 m_selectedTopLeft
.SetRow( row
);
6890 need_refresh
[1] = TRUE
;
6891 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6892 wxGridCellCoords ( oldBottom
,
6895 m_selectedTopLeft
.SetCol( 0 );
6898 if ( oldBottom
< row
)
6900 need_refresh
[2] = TRUE
;
6901 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6902 wxGridCellCoords ( row
,
6904 m_selectedBottomRight
.SetRow( row
);
6907 if ( oldRight
< m_numCols
- 1 )
6909 need_refresh
[3] = TRUE
;
6910 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6912 wxGridCellCoords ( oldBottom
,
6914 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6917 for (i
= 0; i
< 4; i
++ )
6918 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6919 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6923 r
= SelectionToDeviceRect();
6925 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6927 m_selectedTopLeft
.Set( row
, 0 );
6928 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6929 r
= SelectionToDeviceRect();
6930 m_gridWin
->Refresh( FALSE
, &r
);
6933 wxGridRangeSelectEvent
gridEvt( GetId(),
6934 wxEVT_GRID_RANGE_SELECT
,
6937 m_selectedBottomRight
);
6939 GetEventHandler()->ProcessEvent(gridEvt
);
6943 void wxGrid::SelectCol( int col
, bool addToSelected
)
6945 if ( IsSelection() && addToSelected
)
6948 bool need_refresh
[4];
6952 need_refresh
[3] = FALSE
;
6955 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6956 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6957 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6958 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6960 if ( oldLeft
> col
)
6962 need_refresh
[0] = TRUE
;
6963 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6964 wxGridCellCoords ( m_numRows
- 1,
6966 m_selectedTopLeft
.SetCol( col
);
6971 need_refresh
[1] = TRUE
;
6972 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6973 wxGridCellCoords ( oldTop
- 1,
6975 m_selectedTopLeft
.SetRow( 0 );
6978 if ( oldRight
< col
)
6980 need_refresh
[2] = TRUE
;
6981 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6982 wxGridCellCoords ( m_numRows
- 1,
6984 m_selectedBottomRight
.SetCol( col
);
6987 if ( oldBottom
< m_numRows
- 1 )
6989 need_refresh
[3] = TRUE
;
6990 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6992 wxGridCellCoords ( m_numRows
- 1,
6994 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6997 for (i
= 0; i
< 4; i
++ )
6998 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6999 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7005 r
= SelectionToDeviceRect();
7007 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7009 m_selectedTopLeft
.Set( 0, col
);
7010 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7011 r
= SelectionToDeviceRect();
7012 m_gridWin
->Refresh( FALSE
, &r
);
7015 wxGridRangeSelectEvent
gridEvt( GetId(),
7016 wxEVT_GRID_RANGE_SELECT
,
7019 m_selectedBottomRight
);
7021 GetEventHandler()->ProcessEvent(gridEvt
);
7025 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7028 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7030 if ( topRow
> bottomRow
)
7037 if ( leftCol
> rightCol
)
7044 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7045 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7047 if ( m_selectedTopLeft
!= updateTopLeft
||
7048 m_selectedBottomRight
!= updateBottomRight
)
7050 // Compute two optimal update rectangles:
7051 // Either one rectangle is a real subset of the
7052 // other, or they are (almost) disjoint!
7054 bool need_refresh
[4];
7058 need_refresh
[3] = FALSE
;
7061 // Store intermediate values
7062 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7063 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7064 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7065 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7067 // Determine the outer/inner coordinates.
7068 if (oldLeft
> leftCol
)
7074 if (oldTop
> topRow
)
7080 if (oldRight
< rightCol
)
7083 oldRight
= rightCol
;
7086 if (oldBottom
< bottomRow
)
7089 oldBottom
= bottomRow
;
7093 // Now, either the stuff marked old is the outer
7094 // rectangle or we don't have a situation where one
7095 // is contained in the other.
7097 if ( oldLeft
< leftCol
)
7099 need_refresh
[0] = TRUE
;
7100 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7102 wxGridCellCoords ( oldBottom
,
7106 if ( oldTop
< topRow
)
7108 need_refresh
[1] = TRUE
;
7109 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7111 wxGridCellCoords ( topRow
- 1,
7115 if ( oldRight
> rightCol
)
7117 need_refresh
[2] = TRUE
;
7118 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7120 wxGridCellCoords ( oldBottom
,
7124 if ( oldBottom
> bottomRow
)
7126 need_refresh
[3] = TRUE
;
7127 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7129 wxGridCellCoords ( oldBottom
,
7135 m_selectedTopLeft
= updateTopLeft
;
7136 m_selectedBottomRight
= updateBottomRight
;
7138 // various Refresh() calls
7139 for (i
= 0; i
< 4; i
++ )
7140 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7141 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7144 // only generate an event if the block is not being selected by
7145 // dragging the mouse (in which case the event will be generated in
7146 // the mouse event handler)
7147 if ( !m_isDragging
)
7149 wxGridRangeSelectEvent
gridEvt( GetId(),
7150 wxEVT_GRID_RANGE_SELECT
,
7153 m_selectedBottomRight
);
7155 GetEventHandler()->ProcessEvent(gridEvt
);
7159 void wxGrid::SelectAll()
7161 m_selectedTopLeft
.Set( 0, 0 );
7162 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7164 m_gridWin
->Refresh();
7168 void wxGrid::ClearSelection()
7170 m_selectedTopLeft
= wxGridNoCellCoords
;
7171 m_selectedBottomRight
= wxGridNoCellCoords
;
7175 // This function returns the rectangle that encloses the given block
7176 // in device coords clipped to the client size of the grid window.
7178 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7179 const wxGridCellCoords
&bottomRight
)
7181 wxRect
rect( wxGridNoCellRect
);
7184 cellRect
= CellToRect( topLeft
);
7185 if ( cellRect
!= wxGridNoCellRect
)
7191 rect
= wxRect( 0, 0, 0, 0 );
7194 cellRect
= CellToRect( bottomRight
);
7195 if ( cellRect
!= wxGridNoCellRect
)
7201 return wxGridNoCellRect
;
7204 // convert to scrolled coords
7206 int left
, top
, right
, bottom
;
7207 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7208 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7211 m_gridWin
->GetClientSize( &cw
, &ch
);
7213 rect
.SetLeft( wxMax(0, left
) );
7214 rect
.SetTop( wxMax(0, top
) );
7215 rect
.SetRight( wxMin(cw
, right
) );
7216 rect
.SetBottom( wxMin(ch
, bottom
) );
7224 // ------ Grid event classes
7227 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7229 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7230 int row
, int col
, int x
, int y
,
7231 bool control
, bool shift
, bool alt
, bool meta
)
7232 : wxNotifyEvent( type
, id
)
7238 m_control
= control
;
7243 SetEventObject(obj
);
7247 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7249 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7250 int rowOrCol
, int x
, int y
,
7251 bool control
, bool shift
, bool alt
, bool meta
)
7252 : wxNotifyEvent( type
, id
)
7254 m_rowOrCol
= rowOrCol
;
7257 m_control
= control
;
7262 SetEventObject(obj
);
7266 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7268 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7269 const wxGridCellCoords
& topLeft
,
7270 const wxGridCellCoords
& bottomRight
,
7271 bool control
, bool shift
, bool alt
, bool meta
)
7272 : wxNotifyEvent( type
, id
)
7274 m_topLeft
= topLeft
;
7275 m_bottomRight
= bottomRight
;
7276 m_control
= control
;
7281 SetEventObject(obj
);
7285 #endif // ifndef wxUSE_NEW_GRID