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 m_owner
->DrawGridSpace( dc
);
2583 #if WXGRID_DRAW_LINES
2584 m_owner
->DrawAllGridLines( dc
, reg
);
2586 m_owner
->DrawHighlight( dc
);
2590 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2592 wxPanel::ScrollWindow( dx
, dy
, rect
);
2593 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2594 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2598 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2600 m_owner
->ProcessGridCellMouseEvent( event
);
2604 // This seems to be required for wxMotif otherwise the mouse
2605 // cursor must be in the cell edit control to get key events
2607 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2609 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2613 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2618 //////////////////////////////////////////////////////////////////////
2621 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2623 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2624 EVT_PAINT( wxGrid::OnPaint
)
2625 EVT_SIZE( wxGrid::OnSize
)
2626 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2627 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2630 wxGrid::wxGrid( wxWindow
*parent
,
2635 const wxString
& name
)
2636 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2637 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2646 m_defaultCellAttr
->SafeDecRef();
2648 #ifdef DEBUG_ATTR_CACHE
2649 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2650 wxPrintf(_T("wxGrid attribute cache statistics: "
2651 "total: %u, hits: %u (%u%%)\n"),
2652 total
, gs_nAttrCacheHits
,
2653 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2659 delete m_typeRegistry
;
2664 // ----- internal init and update functions
2667 void wxGrid::Create()
2669 m_created
= FALSE
; // set to TRUE by CreateGrid
2670 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2672 m_table
= (wxGridTableBase
*) NULL
;
2675 m_cellEditCtrlEnabled
= FALSE
;
2677 m_defaultCellAttr
= new wxGridCellAttr
;
2678 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2680 // Set default cell attributes
2681 m_defaultCellAttr
->SetFont(GetFont());
2682 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2683 m_defaultCellAttr
->SetTextColour(
2684 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2685 m_defaultCellAttr
->SetBackgroundColour(
2686 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2687 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2688 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2693 m_currentCellCoords
= wxGridNoCellCoords
;
2695 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2696 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2698 // data type registration: register all standard data types
2699 // TODO: may be allow the app to selectively disable some of them?
2700 m_typeRegistry
= new wxGridTypeRegistry
;
2701 RegisterDataType(wxGRID_VALUE_STRING
,
2702 new wxGridCellStringRenderer
,
2703 new wxGridCellTextEditor
);
2704 RegisterDataType(wxGRID_VALUE_BOOL
,
2705 new wxGridCellBoolRenderer
,
2706 new wxGridCellBoolEditor
);
2707 RegisterDataType(wxGRID_VALUE_NUMBER
,
2708 new wxGridCellNumberRenderer
,
2709 new wxGridCellNumberEditor
);
2711 // subwindow components that make up the wxGrid
2712 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2717 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2722 m_colLabelWin
= new wxGridColLabelWindow( this,
2727 m_gridWin
= new wxGridWindow( this,
2734 SetTargetWindow( m_gridWin
);
2738 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2742 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2747 m_numRows
= numRows
;
2748 m_numCols
= numCols
;
2750 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2751 m_table
->SetView( this );
2760 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2764 // RD: Actually, this should probably be allowed. I think it would be
2765 // nice to be able to switch multiple Tables in and out of a single
2766 // View at runtime. Is there anything in the implmentation that would
2769 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2774 m_numRows
= table
->GetNumberRows();
2775 m_numCols
= table
->GetNumberCols();
2778 m_table
->SetView( this );
2791 if ( m_numRows
<= 0 )
2792 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2794 if ( m_numCols
<= 0 )
2795 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2797 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2798 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2800 if ( m_rowLabelWin
)
2802 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2806 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2809 m_labelTextColour
= wxColour( _T("BLACK") );
2812 m_attrCache
.row
= -1;
2814 // TODO: something better than this ?
2816 m_labelFont
= this->GetFont();
2817 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2819 m_rowLabelHorizAlign
= wxLEFT
;
2820 m_rowLabelVertAlign
= wxCENTRE
;
2822 m_colLabelHorizAlign
= wxCENTRE
;
2823 m_colLabelVertAlign
= wxTOP
;
2825 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2826 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2828 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2829 m_defaultRowHeight
+= 8;
2831 m_defaultRowHeight
+= 4;
2834 m_gridLineColour
= wxColour( 128, 128, 255 );
2835 m_gridLinesEnabled
= TRUE
;
2837 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2838 m_winCapture
= (wxWindow
*)NULL
;
2839 m_canDragRowSize
= TRUE
;
2840 m_canDragColSize
= TRUE
;
2841 m_canDragGridSize
= TRUE
;
2843 m_dragRowOrCol
= -1;
2844 m_isDragging
= FALSE
;
2845 m_startDragPos
= wxDefaultPosition
;
2847 m_waitForSlowClick
= FALSE
;
2849 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2850 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2852 m_currentCellCoords
= wxGridNoCellCoords
;
2854 m_selectedTopLeft
= wxGridNoCellCoords
;
2855 m_selectedBottomRight
= wxGridNoCellCoords
;
2856 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2857 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2859 m_editable
= TRUE
; // default for whole grid
2861 m_inOnKeyDown
= FALSE
;
2865 // ----------------------------------------------------------------------------
2866 // the idea is to call these functions only when necessary because they create
2867 // quite big arrays which eat memory mostly unnecessary - in particular, if
2868 // default widths/heights are used for all rows/columns, we may not use these
2871 // with some extra code, it should be possible to only store the
2872 // widths/heights different from default ones but this will be done later...
2873 // ----------------------------------------------------------------------------
2875 void wxGrid::InitRowHeights()
2877 m_rowHeights
.Empty();
2878 m_rowBottoms
.Empty();
2880 m_rowHeights
.Alloc( m_numRows
);
2881 m_rowBottoms
.Alloc( m_numRows
);
2884 for ( int i
= 0; i
< m_numRows
; i
++ )
2886 m_rowHeights
.Add( m_defaultRowHeight
);
2887 rowBottom
+= m_defaultRowHeight
;
2888 m_rowBottoms
.Add( rowBottom
);
2892 void wxGrid::InitColWidths()
2894 m_colWidths
.Empty();
2895 m_colRights
.Empty();
2897 m_colWidths
.Alloc( m_numCols
);
2898 m_colRights
.Alloc( m_numCols
);
2900 for ( int i
= 0; i
< m_numCols
; i
++ )
2902 m_colWidths
.Add( m_defaultColWidth
);
2903 colRight
+= m_defaultColWidth
;
2904 m_colRights
.Add( colRight
);
2908 int wxGrid::GetColWidth(int col
) const
2910 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2913 int wxGrid::GetColLeft(int col
) const
2915 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2916 : m_colRights
[col
] - m_colWidths
[col
];
2919 int wxGrid::GetColRight(int col
) const
2921 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
2925 int wxGrid::GetRowHeight(int row
) const
2927 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2930 int wxGrid::GetRowTop(int row
) const
2932 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2933 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2936 int wxGrid::GetRowBottom(int row
) const
2938 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2939 : m_rowBottoms
[row
];
2942 void wxGrid::CalcDimensions()
2945 GetClientSize( &cw
, &ch
);
2947 if ( m_numRows
> 0 && m_numCols
> 0 )
2949 int right
= GetColRight( m_numCols
-1 ) + 50;
2950 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
2952 // TODO: restore the scroll position that we had before sizing
2955 GetViewStart( &x
, &y
);
2956 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2957 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2963 void wxGrid::CalcWindowSizes()
2966 GetClientSize( &cw
, &ch
);
2968 if ( m_cornerLabelWin
->IsShown() )
2969 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2971 if ( m_colLabelWin
->IsShown() )
2972 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2974 if ( m_rowLabelWin
->IsShown() )
2975 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2977 if ( m_gridWin
->IsShown() )
2978 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2982 // this is called when the grid table sends a message to say that it
2983 // has been redimensioned
2985 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2989 // if we were using the default widths/heights so far, we must change them
2991 if ( m_colWidths
.IsEmpty() )
2996 if ( m_rowHeights
.IsEmpty() )
3001 switch ( msg
.GetId() )
3003 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3005 size_t pos
= msg
.GetCommandInt();
3006 int numRows
= msg
.GetCommandInt2();
3007 for ( i
= 0; i
< numRows
; i
++ )
3009 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3010 m_rowBottoms
.Insert( 0, pos
);
3012 m_numRows
+= numRows
;
3015 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3017 for ( i
= pos
; i
< m_numRows
; i
++ )
3019 bottom
+= m_rowHeights
[i
];
3020 m_rowBottoms
[i
] = bottom
;
3026 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3028 int numRows
= msg
.GetCommandInt();
3029 for ( i
= 0; i
< numRows
; i
++ )
3031 m_rowHeights
.Add( m_defaultRowHeight
);
3032 m_rowBottoms
.Add( 0 );
3035 int oldNumRows
= m_numRows
;
3036 m_numRows
+= numRows
;
3039 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3041 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3043 bottom
+= m_rowHeights
[i
];
3044 m_rowBottoms
[i
] = bottom
;
3050 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3052 size_t pos
= msg
.GetCommandInt();
3053 int numRows
= msg
.GetCommandInt2();
3054 for ( i
= 0; i
< numRows
; i
++ )
3056 m_rowHeights
.Remove( pos
);
3057 m_rowBottoms
.Remove( pos
);
3059 m_numRows
-= numRows
;
3064 m_colWidths
.Clear();
3065 m_colRights
.Clear();
3066 m_currentCellCoords
= wxGridNoCellCoords
;
3070 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3071 m_currentCellCoords
.Set( 0, 0 );
3074 for ( i
= 0; i
< m_numRows
; i
++ )
3076 h
+= m_rowHeights
[i
];
3077 m_rowBottoms
[i
] = h
;
3085 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3087 size_t pos
= msg
.GetCommandInt();
3088 int numCols
= msg
.GetCommandInt2();
3089 for ( i
= 0; i
< numCols
; i
++ )
3091 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3092 m_colRights
.Insert( 0, pos
);
3094 m_numCols
+= numCols
;
3097 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3099 for ( i
= pos
; i
< m_numCols
; i
++ )
3101 right
+= m_colWidths
[i
];
3102 m_colRights
[i
] = right
;
3108 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3110 int numCols
= msg
.GetCommandInt();
3111 for ( i
= 0; i
< numCols
; i
++ )
3113 m_colWidths
.Add( m_defaultColWidth
);
3114 m_colRights
.Add( 0 );
3117 int oldNumCols
= m_numCols
;
3118 m_numCols
+= numCols
;
3121 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3123 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3125 right
+= m_colWidths
[i
];
3126 m_colRights
[i
] = right
;
3132 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3134 size_t pos
= msg
.GetCommandInt();
3135 int numCols
= msg
.GetCommandInt2();
3136 for ( i
= 0; i
< numCols
; i
++ )
3138 m_colWidths
.Remove( pos
);
3139 m_colRights
.Remove( pos
);
3141 m_numCols
-= numCols
;
3145 #if 0 // leave the row alone here so that AppendCols will work subsequently
3147 m_rowHeights
.Clear();
3148 m_rowBottoms
.Clear();
3150 m_currentCellCoords
= wxGridNoCellCoords
;
3154 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3155 m_currentCellCoords
.Set( 0, 0 );
3158 for ( i
= 0; i
< m_numCols
; i
++ )
3160 w
+= m_colWidths
[i
];
3173 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3175 wxRegionIterator
iter( reg
);
3178 m_rowLabelsExposed
.Empty();
3185 // TODO: remove this when we can...
3186 // There is a bug in wxMotif that gives garbage update
3187 // rectangles if you jump-scroll a long way by clicking the
3188 // scrollbar with middle button. This is a work-around
3190 #if defined(__WXMOTIF__)
3192 m_gridWin
->GetClientSize( &cw
, &ch
);
3193 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3194 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3197 // logical bounds of update region
3200 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3201 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3203 // find the row labels within these bounds
3206 for ( row
= 0; row
< m_numRows
; row
++ )
3208 if ( GetRowBottom(row
) < top
)
3211 if ( GetRowTop(row
) > bottom
)
3214 m_rowLabelsExposed
.Add( row
);
3222 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3224 wxRegionIterator
iter( reg
);
3227 m_colLabelsExposed
.Empty();
3234 // TODO: remove this when we can...
3235 // There is a bug in wxMotif that gives garbage update
3236 // rectangles if you jump-scroll a long way by clicking the
3237 // scrollbar with middle button. This is a work-around
3239 #if defined(__WXMOTIF__)
3241 m_gridWin
->GetClientSize( &cw
, &ch
);
3242 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3243 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3246 // logical bounds of update region
3249 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3250 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3252 // find the cells within these bounds
3255 for ( col
= 0; col
< m_numCols
; col
++ )
3257 if ( GetColRight(col
) < left
)
3260 if ( GetColLeft(col
) > right
)
3263 m_colLabelsExposed
.Add( col
);
3271 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3273 wxRegionIterator
iter( reg
);
3276 m_cellsExposed
.Empty();
3277 m_rowsExposed
.Empty();
3278 m_colsExposed
.Empty();
3280 int left
, top
, right
, bottom
;
3285 // TODO: remove this when we can...
3286 // There is a bug in wxMotif that gives garbage update
3287 // rectangles if you jump-scroll a long way by clicking the
3288 // scrollbar with middle button. This is a work-around
3290 #if defined(__WXMOTIF__)
3292 m_gridWin
->GetClientSize( &cw
, &ch
);
3293 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3294 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3295 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3296 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3299 // logical bounds of update region
3301 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3302 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3304 // find the cells within these bounds
3307 for ( row
= 0; row
< m_numRows
; row
++ )
3309 if ( GetRowBottom(row
) <= top
)
3312 if ( GetRowTop(row
) > bottom
)
3315 m_rowsExposed
.Add( row
);
3317 for ( col
= 0; col
< m_numCols
; col
++ )
3319 if ( GetColRight(col
) <= left
)
3322 if ( GetColLeft(col
) > right
)
3325 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3326 m_colsExposed
.Add( col
);
3327 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3336 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3339 wxPoint
pos( event
.GetPosition() );
3340 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3342 if ( event
.Dragging() )
3344 m_isDragging
= TRUE
;
3346 if ( event
.LeftIsDown() )
3348 switch( m_cursorMode
)
3350 case WXGRID_CURSOR_RESIZE_ROW
:
3352 int cw
, ch
, left
, dummy
;
3353 m_gridWin
->GetClientSize( &cw
, &ch
);
3354 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3356 wxClientDC
dc( m_gridWin
);
3358 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3359 dc
.SetLogicalFunction(wxINVERT
);
3360 if ( m_dragLastPos
>= 0 )
3362 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3364 dc
.DrawLine( left
, y
, left
+cw
, y
);
3369 case WXGRID_CURSOR_SELECT_ROW
:
3370 if ( (row
= YToRow( y
)) >= 0 &&
3371 !IsInSelection( row
, 0 ) )
3373 SelectRow( row
, TRUE
);
3376 // default label to suppress warnings about "enumeration value
3377 // 'xxx' not handled in switch
3385 m_isDragging
= FALSE
;
3388 // ------------ Entering or leaving the window
3390 if ( event
.Entering() || event
.Leaving() )
3392 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3396 // ------------ Left button pressed
3398 else if ( event
.LeftDown() )
3400 // don't send a label click event for a hit on the
3401 // edge of the row label - this is probably the user
3402 // wanting to resize the row
3404 if ( YToEdgeOfRow(y
) < 0 )
3408 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3410 SelectRow( row
, event
.ShiftDown() );
3411 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3416 // starting to drag-resize a row
3418 if ( CanDragRowSize() )
3419 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3424 // ------------ Left double click
3426 else if (event
.LeftDClick() )
3428 if ( YToEdgeOfRow(y
) < 0 )
3431 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3436 // ------------ Left button released
3438 else if ( event
.LeftUp() )
3440 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3442 DoEndDragResizeRow();
3444 // Note: we are ending the event *after* doing
3445 // default processing in this case
3447 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3450 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3455 // ------------ Right button down
3457 else if ( event
.RightDown() )
3460 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3462 // no default action at the moment
3467 // ------------ Right double click
3469 else if ( event
.RightDClick() )
3472 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3474 // no default action at the moment
3479 // ------------ No buttons down and mouse moving
3481 else if ( event
.Moving() )
3483 m_dragRowOrCol
= YToEdgeOfRow( y
);
3484 if ( m_dragRowOrCol
>= 0 )
3486 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3488 // don't capture the mouse yet
3489 if ( CanDragRowSize() )
3490 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3493 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3495 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3501 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3504 wxPoint
pos( event
.GetPosition() );
3505 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3507 if ( event
.Dragging() )
3509 m_isDragging
= TRUE
;
3511 if ( event
.LeftIsDown() )
3513 switch( m_cursorMode
)
3515 case WXGRID_CURSOR_RESIZE_COL
:
3517 int cw
, ch
, dummy
, top
;
3518 m_gridWin
->GetClientSize( &cw
, &ch
);
3519 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3521 wxClientDC
dc( m_gridWin
);
3524 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3525 GetColMinimalWidth(m_dragRowOrCol
));
3526 dc
.SetLogicalFunction(wxINVERT
);
3527 if ( m_dragLastPos
>= 0 )
3529 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3531 dc
.DrawLine( x
, top
, x
, top
+ch
);
3536 case WXGRID_CURSOR_SELECT_COL
:
3537 if ( (col
= XToCol( x
)) >= 0 &&
3538 !IsInSelection( 0, col
) )
3540 SelectCol( col
, TRUE
);
3543 // default label to suppress warnings about "enumeration value
3544 // 'xxx' not handled in switch
3552 m_isDragging
= FALSE
;
3555 // ------------ Entering or leaving the window
3557 if ( event
.Entering() || event
.Leaving() )
3559 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3563 // ------------ Left button pressed
3565 else if ( event
.LeftDown() )
3567 // don't send a label click event for a hit on the
3568 // edge of the col label - this is probably the user
3569 // wanting to resize the col
3571 if ( XToEdgeOfCol(x
) < 0 )
3575 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3577 SelectCol( col
, event
.ShiftDown() );
3578 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3583 // starting to drag-resize a col
3585 if ( CanDragColSize() )
3586 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3591 // ------------ Left double click
3593 if ( event
.LeftDClick() )
3595 if ( XToEdgeOfCol(x
) < 0 )
3598 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3603 // ------------ Left button released
3605 else if ( event
.LeftUp() )
3607 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3609 DoEndDragResizeCol();
3611 // Note: we are ending the event *after* doing
3612 // default processing in this case
3614 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3617 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3622 // ------------ Right button down
3624 else if ( event
.RightDown() )
3627 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3629 // no default action at the moment
3634 // ------------ Right double click
3636 else if ( event
.RightDClick() )
3639 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3641 // no default action at the moment
3646 // ------------ No buttons down and mouse moving
3648 else if ( event
.Moving() )
3650 m_dragRowOrCol
= XToEdgeOfCol( x
);
3651 if ( m_dragRowOrCol
>= 0 )
3653 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3655 // don't capture the cursor yet
3656 if ( CanDragColSize() )
3657 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3660 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3662 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3668 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3670 if ( event
.LeftDown() )
3672 // indicate corner label by having both row and
3675 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3681 else if ( event
.LeftDClick() )
3683 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3686 else if ( event
.RightDown() )
3688 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3690 // no default action at the moment
3694 else if ( event
.RightDClick() )
3696 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3698 // no default action at the moment
3703 void wxGrid::ChangeCursorMode(CursorMode mode
,
3708 static const wxChar
*cursorModes
[] =
3717 wxLogTrace(_T("grid"),
3718 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3719 win
== m_colLabelWin
? _T("colLabelWin")
3720 : win
? _T("rowLabelWin")
3722 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3723 #endif // __WXDEBUG__
3725 if ( mode
== m_cursorMode
)
3730 // by default use the grid itself
3736 m_winCapture
->ReleaseMouse();
3737 m_winCapture
= (wxWindow
*)NULL
;
3740 m_cursorMode
= mode
;
3742 switch ( m_cursorMode
)
3744 case WXGRID_CURSOR_RESIZE_ROW
:
3745 win
->SetCursor( m_rowResizeCursor
);
3748 case WXGRID_CURSOR_RESIZE_COL
:
3749 win
->SetCursor( m_colResizeCursor
);
3753 win
->SetCursor( *wxSTANDARD_CURSOR
);
3756 // we need to capture mouse when resizing
3757 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3758 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3760 if ( captureMouse
&& resize
)
3762 win
->CaptureMouse();
3767 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3770 wxPoint
pos( event
.GetPosition() );
3771 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3773 wxGridCellCoords coords
;
3774 XYToCell( x
, y
, coords
);
3776 if ( event
.Dragging() )
3778 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3780 // Don't start doing anything until the mouse has been drug at
3781 // least 3 pixels in any direction...
3784 if (m_startDragPos
== wxDefaultPosition
)
3786 m_startDragPos
= pos
;
3789 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3793 m_isDragging
= TRUE
;
3794 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3796 // Hide the edit control, so it
3797 // won't interfer with drag-shrinking.
3798 if ( IsCellEditControlEnabled() )
3799 HideCellEditControl();
3801 // Have we captured the mouse yet?
3804 m_winCapture
= m_gridWin
;
3805 m_winCapture
->CaptureMouse();
3808 if ( coords
!= wxGridNoCellCoords
)
3810 if ( !IsSelection() )
3812 SelectBlock( coords
, coords
);
3816 SelectBlock( m_currentCellCoords
, coords
);
3819 if (! IsVisible(coords
))
3821 MakeCellVisible(coords
);
3822 // TODO: need to introduce a delay or something here. The
3823 // scrolling is way to fast, at least on MSW.
3827 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3829 int cw
, ch
, left
, dummy
;
3830 m_gridWin
->GetClientSize( &cw
, &ch
);
3831 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3833 wxClientDC
dc( m_gridWin
);
3835 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3836 dc
.SetLogicalFunction(wxINVERT
);
3837 if ( m_dragLastPos
>= 0 )
3839 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3841 dc
.DrawLine( left
, y
, left
+cw
, y
);
3844 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3846 int cw
, ch
, dummy
, top
;
3847 m_gridWin
->GetClientSize( &cw
, &ch
);
3848 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3850 wxClientDC
dc( m_gridWin
);
3852 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3853 GetColMinimalWidth(m_dragRowOrCol
) );
3854 dc
.SetLogicalFunction(wxINVERT
);
3855 if ( m_dragLastPos
>= 0 )
3857 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3859 dc
.DrawLine( x
, top
, x
, top
+ch
);
3866 m_isDragging
= FALSE
;
3867 m_startDragPos
= wxDefaultPosition
;
3869 // if ( coords == wxGridNoCellCoords && m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3871 // ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3874 // if ( coords != wxGridNoCellCoords )
3876 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3877 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3880 if ( event
.Entering() || event
.Leaving() )
3882 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3883 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3888 // ------------ Left button pressed
3890 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
3892 DisableCellEditControl();
3893 if ( event
.ShiftDown() )
3895 SelectBlock( m_currentCellCoords
, coords
);
3897 else if ( XToEdgeOfCol(x
) < 0 &&
3898 YToEdgeOfRow(y
) < 0 )
3900 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3905 MakeCellVisible( coords
);
3907 // if this is the second click on this cell then start
3909 if ( m_waitForSlowClick
&&
3910 (coords
== m_currentCellCoords
) &&
3911 CanEnableCellControl())
3913 EnableCellEditControl();
3915 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3916 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
3919 m_waitForSlowClick
= FALSE
;
3923 SetCurrentCell( coords
);
3924 m_waitForSlowClick
= TRUE
;
3931 // ------------ Left double click
3933 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
3935 DisableCellEditControl();
3936 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3938 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3946 // ------------ Left button released
3948 else if ( event
.LeftUp() )
3950 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3952 if ( IsSelection() )
3956 m_winCapture
->ReleaseMouse();
3957 m_winCapture
= NULL
;
3959 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3962 // Show the edit control, if it has been hidden for
3964 ShowCellEditControl();
3966 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3968 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3969 DoEndDragResizeRow();
3971 // Note: we are ending the event *after* doing
3972 // default processing in this case
3974 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3976 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3978 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3979 DoEndDragResizeCol();
3981 // Note: we are ending the event *after* doing
3982 // default processing in this case
3984 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3991 // ------------ Right button down
3993 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
3995 DisableCellEditControl();
3996 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4001 // no default action at the moment
4006 // ------------ Right double click
4008 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4010 DisableCellEditControl();
4011 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4016 // no default action at the moment
4020 // ------------ Moving and no button action
4022 else if ( event
.Moving() && !event
.IsButton() )
4024 int dragRow
= YToEdgeOfRow( y
);
4025 int dragCol
= XToEdgeOfCol( x
);
4027 // Dragging on the corner of a cell to resize in both
4028 // directions is not implemented yet...
4030 if ( dragRow
>= 0 && dragCol
>= 0 )
4032 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4038 m_dragRowOrCol
= dragRow
;
4040 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4042 if ( CanDragRowSize() && CanDragGridSize() )
4043 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4051 m_dragRowOrCol
= dragCol
;
4053 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4055 if ( CanDragColSize() && CanDragGridSize() )
4056 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4062 // Neither on a row or col edge
4064 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4066 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4072 void wxGrid::DoEndDragResizeRow()
4074 if ( m_dragLastPos
>= 0 )
4076 // erase the last line and resize the row
4078 int cw
, ch
, left
, dummy
;
4079 m_gridWin
->GetClientSize( &cw
, &ch
);
4080 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4082 wxClientDC
dc( m_gridWin
);
4084 dc
.SetLogicalFunction( wxINVERT
);
4085 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4086 HideCellEditControl();
4088 int rowTop
= GetRowTop(m_dragRowOrCol
);
4089 SetRowSize( m_dragRowOrCol
,
4090 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4092 if ( !GetBatchCount() )
4094 // Only needed to get the correct rect.y:
4095 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4097 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4098 rect
.width
= m_rowLabelWidth
;
4099 rect
.height
= ch
- rect
.y
;
4100 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4102 m_gridWin
->Refresh( FALSE
, &rect
);
4105 ShowCellEditControl();
4110 void wxGrid::DoEndDragResizeCol()
4112 if ( m_dragLastPos
>= 0 )
4114 // erase the last line and resize the col
4116 int cw
, ch
, dummy
, top
;
4117 m_gridWin
->GetClientSize( &cw
, &ch
);
4118 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4120 wxClientDC
dc( m_gridWin
);
4122 dc
.SetLogicalFunction( wxINVERT
);
4123 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4124 HideCellEditControl();
4126 int colLeft
= GetColLeft(m_dragRowOrCol
);
4127 SetColSize( m_dragRowOrCol
,
4128 wxMax( m_dragLastPos
- colLeft
,
4129 GetColMinimalWidth(m_dragRowOrCol
) ) );
4131 if ( !GetBatchCount() )
4133 // Only needed to get the correct rect.x:
4134 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4136 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4137 rect
.width
= cw
- rect
.x
;
4138 rect
.height
= m_colLabelHeight
;
4139 m_colLabelWin
->Refresh( TRUE
, &rect
);
4141 m_gridWin
->Refresh( FALSE
, &rect
);
4144 ShowCellEditControl();
4151 // ------ interaction with data model
4153 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4155 switch ( msg
.GetId() )
4157 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4158 return GetModelValues();
4160 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4161 return SetModelValues();
4163 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4164 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4165 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4166 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4167 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4168 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4169 return Redimension( msg
);
4178 // The behaviour of this function depends on the grid table class
4179 // Clear() function. For the default wxGridStringTable class the
4180 // behavious is to replace all cell contents with wxEmptyString but
4181 // not to change the number of rows or cols.
4183 void wxGrid::ClearGrid()
4187 if (IsCellEditControlEnabled())
4188 DisableCellEditControl();
4191 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4196 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4198 // TODO: something with updateLabels flag
4202 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4208 if (IsCellEditControlEnabled())
4209 DisableCellEditControl();
4211 bool ok
= m_table
->InsertRows( pos
, numRows
);
4213 // the table will have sent the results of the insert row
4214 // operation to this view object as a grid table message
4218 if ( m_numCols
== 0 )
4220 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4222 // TODO: perhaps instead of appending the default number of cols
4223 // we should remember what the last non-zero number of cols was ?
4227 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4229 // if we have just inserted cols into an empty grid the current
4230 // cell will be undefined...
4232 SetCurrentCell( 0, 0 );
4236 if ( !GetBatchCount() ) Refresh();
4248 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4250 // TODO: something with updateLabels flag
4254 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4258 if ( m_table
&& m_table
->AppendRows( numRows
) )
4260 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4262 // if we have just inserted cols into an empty grid the current
4263 // cell will be undefined...
4265 SetCurrentCell( 0, 0 );
4268 // the table will have sent the results of the append row
4269 // operation to this view object as a grid table message
4272 if ( !GetBatchCount() ) Refresh();
4282 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4284 // TODO: something with updateLabels flag
4288 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4294 if (IsCellEditControlEnabled())
4295 DisableCellEditControl();
4297 if (m_table
->DeleteRows( pos
, numRows
))
4300 // the table will have sent the results of the delete row
4301 // operation to this view object as a grid table message
4304 if ( !GetBatchCount() ) Refresh();
4312 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4314 // TODO: something with updateLabels flag
4318 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4324 if (IsCellEditControlEnabled())
4325 DisableCellEditControl();
4327 bool ok
= m_table
->InsertCols( pos
, numCols
);
4329 // the table will have sent the results of the insert col
4330 // operation to this view object as a grid table message
4334 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4336 // if we have just inserted cols into an empty grid the current
4337 // cell will be undefined...
4339 SetCurrentCell( 0, 0 );
4343 if ( !GetBatchCount() ) Refresh();
4355 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4357 // TODO: something with updateLabels flag
4361 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4365 if ( m_table
&& m_table
->AppendCols( numCols
) )
4367 // the table will have sent the results of the append col
4368 // operation to this view object as a grid table message
4370 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4372 // if we have just inserted cols into an empty grid the current
4373 // cell will be undefined...
4375 SetCurrentCell( 0, 0 );
4379 if ( !GetBatchCount() ) Refresh();
4389 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4391 // TODO: something with updateLabels flag
4395 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4401 if (IsCellEditControlEnabled())
4402 DisableCellEditControl();
4404 if ( m_table
->DeleteCols( pos
, numCols
) )
4406 // the table will have sent the results of the delete col
4407 // operation to this view object as a grid table message
4410 if ( !GetBatchCount() ) Refresh();
4420 // ----- event handlers
4423 // Generate a grid event based on a mouse event and
4424 // return the result of ProcessEvent()
4426 bool wxGrid::SendEvent( const wxEventType type
,
4428 wxMouseEvent
& mouseEv
)
4430 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4432 int rowOrCol
= (row
== -1 ? col
: row
);
4434 wxGridSizeEvent
gridEvt( GetId(),
4438 mouseEv
.GetX(), mouseEv
.GetY(),
4439 mouseEv
.ControlDown(),
4440 mouseEv
.ShiftDown(),
4442 mouseEv
.MetaDown() );
4444 return GetEventHandler()->ProcessEvent(gridEvt
);
4446 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4448 wxGridRangeSelectEvent
gridEvt( GetId(),
4452 m_selectedBottomRight
,
4453 mouseEv
.ControlDown(),
4454 mouseEv
.ShiftDown(),
4456 mouseEv
.MetaDown() );
4458 return GetEventHandler()->ProcessEvent(gridEvt
);
4462 wxGridEvent
gridEvt( GetId(),
4466 mouseEv
.GetX(), mouseEv
.GetY(),
4467 mouseEv
.ControlDown(),
4468 mouseEv
.ShiftDown(),
4470 mouseEv
.MetaDown() );
4472 return GetEventHandler()->ProcessEvent(gridEvt
);
4477 // Generate a grid event of specified type and return the result
4478 // of ProcessEvent().
4480 bool wxGrid::SendEvent( const wxEventType type
,
4483 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4485 int rowOrCol
= (row
== -1 ? col
: row
);
4487 wxGridSizeEvent
gridEvt( GetId(),
4492 return GetEventHandler()->ProcessEvent(gridEvt
);
4496 wxGridEvent
gridEvt( GetId(),
4501 return GetEventHandler()->ProcessEvent(gridEvt
);
4506 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4508 wxPaintDC
dc( this );
4510 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4511 m_numRows
&& m_numCols
)
4513 m_currentCellCoords
.Set(0, 0);
4514 ShowCellEditControl();
4521 // This is just here to make sure that CalcDimensions gets called when
4522 // the grid view is resized... then the size event is skipped to allow
4523 // the box sizers to handle everything
4525 void wxGrid::OnSize( wxSizeEvent
& event
)
4532 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4534 if ( m_inOnKeyDown
)
4536 // shouldn't be here - we are going round in circles...
4538 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4541 m_inOnKeyDown
= TRUE
;
4543 // propagate the event up and see if it gets processed
4545 wxWindow
*parent
= GetParent();
4546 wxKeyEvent
keyEvt( event
);
4547 keyEvt
.SetEventObject( parent
);
4549 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4552 // TODO: Should also support Shift-cursor keys for
4553 // extending the selection. Maybe add a flag to
4554 // MoveCursorXXX() and MoveCursorXXXBlock() and
4555 // just send event.ShiftDown().
4557 // try local handlers
4559 switch ( event
.KeyCode() )
4562 if ( event
.ControlDown() )
4564 MoveCursorUpBlock();
4573 if ( event
.ControlDown() )
4575 MoveCursorDownBlock();
4584 if ( event
.ControlDown() )
4586 MoveCursorLeftBlock();
4595 if ( event
.ControlDown() )
4597 MoveCursorRightBlock();
4606 if ( event
.ControlDown() )
4608 event
.Skip(); // to let the edit control have the return
4617 if (event
.ShiftDown())
4624 if ( event
.ControlDown() )
4626 MakeCellVisible( 0, 0 );
4627 SetCurrentCell( 0, 0 );
4636 if ( event
.ControlDown() )
4638 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4639 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4655 // We don't want these keys to trigger the edit control, any others?
4664 if ( !IsEditable() )
4669 // Otherwise fall through to default
4672 // now try the cell edit control
4674 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4676 EnableCellEditControl();
4677 int row
= m_currentCellCoords
.GetRow();
4678 int col
= m_currentCellCoords
.GetCol();
4679 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4680 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4685 // let others process char events for readonly cells
4692 m_inOnKeyDown
= FALSE
;
4696 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4700 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4702 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4704 // the event has been intercepted - do nothing
4709 m_currentCellCoords
!= wxGridNoCellCoords
)
4711 HideCellEditControl();
4712 DisableCellEditControl();
4714 // Clear the old current cell highlight
4715 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4717 // Otherwise refresh redraws the highlight!
4718 m_currentCellCoords
= coords
;
4720 m_gridWin
->Refresh( FALSE
, &r
);
4723 m_currentCellCoords
= coords
;
4727 wxClientDC
dc(m_gridWin
);
4730 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4731 DrawCellHighlight(dc
, attr
);
4734 if ( IsSelection() )
4736 wxRect
r( SelectionToDeviceRect() );
4738 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4745 // ------ functions to get/send data (see also public functions)
4748 bool wxGrid::GetModelValues()
4752 // all we need to do is repaint the grid
4754 m_gridWin
->Refresh();
4762 bool wxGrid::SetModelValues()
4768 for ( row
= 0; row
< m_numRows
; row
++ )
4770 for ( col
= 0; col
< m_numCols
; col
++ )
4772 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4784 // Note - this function only draws cells that are in the list of
4785 // exposed cells (usually set from the update region by
4786 // CalcExposedCells)
4788 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4790 if ( !m_numRows
|| !m_numCols
) return;
4793 size_t numCells
= m_cellsExposed
.GetCount();
4795 for ( i
= 0; i
< numCells
; i
++ )
4797 DrawCell( dc
, m_cellsExposed
[i
] );
4802 void wxGrid::DrawGridSpace( wxDC
& dc
)
4804 if ( m_numRows
&& m_numCols
)
4807 m_gridWin
->GetClientSize( &cw
, &ch
);
4810 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4812 if ( right
> GetColRight(m_numCols
-1) ||
4813 bottom
> GetRowBottom(m_numRows
-1) )
4816 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4818 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4819 dc
.SetPen( *wxTRANSPARENT_PEN
);
4821 if ( right
> GetColRight(m_numCols
-1) )
4822 dc
.DrawRectangle( GetColRight(m_numCols
-1)+1, top
,
4823 right
- GetColRight(m_numCols
-1), ch
);
4825 if ( bottom
> GetRowBottom(m_numRows
-1) )
4826 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1)+1,
4827 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4833 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4835 int row
= coords
.GetRow();
4836 int col
= coords
.GetCol();
4838 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4841 // we draw the cell border ourselves
4842 #if !WXGRID_DRAW_LINES
4843 if ( m_gridLinesEnabled
)
4844 DrawCellBorder( dc
, coords
);
4847 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4849 bool isCurrent
= coords
== m_currentCellCoords
;
4852 rect
.x
= GetColLeft(col
);
4853 rect
.y
= GetRowTop(row
);
4854 rect
.width
= GetColWidth(col
) - 1;
4855 rect
.height
= GetRowHeight(row
) - 1;
4857 // if the editor is shown, we should use it and not the renderer
4858 if ( isCurrent
&& IsCellEditControlEnabled() )
4860 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4864 // but all the rest is drawn by the cell renderer and hence may be
4866 attr
->GetRenderer(this, row
, col
)->
4867 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4874 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4876 int row
= m_currentCellCoords
.GetRow();
4877 int col
= m_currentCellCoords
.GetCol();
4879 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4883 rect
.x
= GetColLeft(col
);
4884 rect
.y
= GetRowTop(row
);
4885 rect
.width
= GetColWidth(col
) - 1;
4886 rect
.height
= GetRowHeight(row
) - 1;
4888 // hmmm... what could we do here to show that the cell is disabled?
4889 // for now, I just draw a thinner border than for the other ones, but
4890 // it doesn't look really good
4891 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4892 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4894 dc
.DrawRectangle(rect
);
4897 // VZ: my experiments with 3d borders...
4899 // how to properly set colours for arbitrary bg?
4900 wxCoord x1
= rect
.x
,
4902 x2
= rect
.x
+ rect
.width
-1,
4903 y2
= rect
.y
+ rect
.height
-1;
4905 dc
.SetPen(*wxWHITE_PEN
);
4906 dc
.DrawLine(x1
, y1
, x2
, y1
);
4907 dc
.DrawLine(x1
, y1
, x1
, y2
);
4909 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4910 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4912 dc
.SetPen(*wxBLACK_PEN
);
4913 dc
.DrawLine(x1
, y2
, x2
, y2
);
4914 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4919 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4921 int row
= coords
.GetRow();
4922 int col
= coords
.GetCol();
4923 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4926 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4928 // right hand border
4930 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
4931 GetColRight(col
), GetRowBottom(row
) );
4935 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
4936 GetColRight(col
), GetRowBottom(row
) );
4939 void wxGrid::DrawHighlight(wxDC
& dc
)
4941 if ( IsCellEditControlEnabled() )
4943 // don't show highlight when the edit control is shown
4947 // if the active cell was repainted, repaint its highlight too because it
4948 // might have been damaged by the grid lines
4949 size_t count
= m_cellsExposed
.GetCount();
4950 for ( size_t n
= 0; n
< count
; n
++ )
4952 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4954 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4955 DrawCellHighlight(dc
, attr
);
4963 // TODO: remove this ???
4964 // This is used to redraw all grid lines e.g. when the grid line colour
4967 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4969 if ( !m_gridLinesEnabled
||
4971 !m_numCols
) return;
4973 int top
, bottom
, left
, right
;
4979 m_gridWin
->GetClientSize(&cw
, &ch
);
4981 // virtual coords of visible area
4983 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4984 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4989 reg
.GetBox(x
, y
, w
, h
);
4990 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4991 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4995 m_gridWin
->GetClientSize(&cw
, &ch
);
4996 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4997 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5000 // avoid drawing grid lines past the last row and col
5002 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5003 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5005 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5007 // horizontal grid lines
5010 for ( i
= 0; i
< m_numRows
; i
++ )
5012 int bot
= GetRowBottom(i
) - 1;
5021 dc
.DrawLine( left
, bot
, right
, bot
);
5026 // vertical grid lines
5028 for ( i
= 0; i
< m_numCols
; i
++ )
5030 int colRight
= GetColRight(i
) - 1;
5031 if ( colRight
> right
)
5036 if ( colRight
>= left
)
5038 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5044 void wxGrid::DrawRowLabels( wxDC
& dc
)
5046 if ( !m_numRows
|| !m_numCols
) return;
5049 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5051 for ( i
= 0; i
< numLabels
; i
++ )
5053 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5058 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5060 if ( GetRowHeight(row
) <= 0 )
5063 int rowTop
= GetRowTop(row
),
5064 rowBottom
= GetRowBottom(row
) - 1;
5066 dc
.SetPen( *wxBLACK_PEN
);
5067 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5068 m_rowLabelWidth
-1, rowBottom
);
5070 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5072 dc
.SetPen( *wxWHITE_PEN
);
5073 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5074 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5076 dc
.SetBackgroundMode( wxTRANSPARENT
);
5077 dc
.SetTextForeground( GetLabelTextColour() );
5078 dc
.SetFont( GetLabelFont() );
5081 GetRowLabelAlignment( &hAlign
, &vAlign
);
5085 rect
.SetY( GetRowTop(row
) + 2 );
5086 rect
.SetWidth( m_rowLabelWidth
- 4 );
5087 rect
.SetHeight( GetRowHeight(row
) - 4 );
5088 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5092 void wxGrid::DrawColLabels( wxDC
& dc
)
5094 if ( !m_numRows
|| !m_numCols
) return;
5097 size_t numLabels
= m_colLabelsExposed
.GetCount();
5099 for ( i
= 0; i
< numLabels
; i
++ )
5101 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5106 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5108 if ( GetColWidth(col
) <= 0 )
5111 int colLeft
= GetColLeft(col
),
5112 colRight
= GetColRight(col
) - 1;
5114 dc
.SetPen( *wxBLACK_PEN
);
5115 dc
.DrawLine( colRight
, 0,
5116 colRight
, m_colLabelHeight
-1 );
5118 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5119 colRight
, m_colLabelHeight
-1 );
5121 dc
.SetPen( *wxWHITE_PEN
);
5122 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5123 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5125 dc
.SetBackgroundMode( wxTRANSPARENT
);
5126 dc
.SetTextForeground( GetLabelTextColour() );
5127 dc
.SetFont( GetLabelFont() );
5129 dc
.SetBackgroundMode( wxTRANSPARENT
);
5130 dc
.SetTextForeground( GetLabelTextColour() );
5131 dc
.SetFont( GetLabelFont() );
5134 GetColLabelAlignment( &hAlign
, &vAlign
);
5137 rect
.SetX( colLeft
+ 2 );
5139 rect
.SetWidth( GetColWidth(col
) - 4 );
5140 rect
.SetHeight( m_colLabelHeight
- 4 );
5141 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5145 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5146 const wxString
& value
,
5151 long textWidth
, textHeight
;
5152 long lineWidth
, lineHeight
;
5153 wxArrayString lines
;
5155 dc
.SetClippingRegion( rect
);
5156 StringToLines( value
, lines
);
5157 if ( lines
.GetCount() )
5159 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5160 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5163 switch ( horizAlign
)
5166 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5170 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5179 switch ( vertAlign
)
5182 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5186 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5195 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5197 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5202 dc
.DestroyClippingRegion();
5206 // Split multi line text up into an array of strings. Any existing
5207 // contents of the string array are preserved.
5209 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5213 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5214 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5216 while ( startPos
< (int)tVal
.Length() )
5218 pos
= tVal
.Mid(startPos
).Find( eol
);
5223 else if ( pos
== 0 )
5225 lines
.Add( wxEmptyString
);
5229 lines
.Add( value
.Mid(startPos
, pos
) );
5233 if ( startPos
< (int)value
.Length() )
5235 lines
.Add( value
.Mid( startPos
) );
5240 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5241 wxArrayString
& lines
,
5242 long *width
, long *height
)
5249 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5251 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5252 w
= wxMax( w
, lineW
);
5262 // ------ Edit control functions
5266 void wxGrid::EnableEditing( bool edit
)
5268 // TODO: improve this ?
5270 if ( edit
!= m_editable
)
5274 // FIXME IMHO this won't disable the edit control if edit == FALSE
5275 // because of the check in the beginning of
5276 // EnableCellEditControl() just below (VZ)
5277 EnableCellEditControl(m_editable
);
5282 void wxGrid::EnableCellEditControl( bool enable
)
5287 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5288 SetCurrentCell( 0, 0 );
5290 if ( enable
!= m_cellEditCtrlEnabled
)
5292 // TODO allow the app to Veto() this event?
5293 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5297 // this should be checked by the caller!
5298 wxASSERT_MSG( CanEnableCellControl(),
5299 _T("can't enable editing for this cell!") );
5301 // do it before ShowCellEditControl()
5302 m_cellEditCtrlEnabled
= enable
;
5304 ShowCellEditControl();
5308 HideCellEditControl();
5309 SaveEditControlValue();
5311 // do it after HideCellEditControl()
5312 m_cellEditCtrlEnabled
= enable
;
5317 bool wxGrid::IsCurrentCellReadOnly() const
5320 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5321 bool readonly
= attr
->IsReadOnly();
5327 bool wxGrid::CanEnableCellControl() const
5329 return m_editable
&& !IsCurrentCellReadOnly();
5332 bool wxGrid::IsCellEditControlEnabled() const
5334 // the cell edit control might be disable for all cells or just for the
5335 // current one if it's read only
5336 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5339 void wxGrid::ShowCellEditControl()
5341 if ( IsCellEditControlEnabled() )
5343 if ( !IsVisible( m_currentCellCoords
) )
5349 wxRect rect
= CellToRect( m_currentCellCoords
);
5350 int row
= m_currentCellCoords
.GetRow();
5351 int col
= m_currentCellCoords
.GetCol();
5353 // convert to scrolled coords
5355 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5357 // done in PaintBackground()
5359 // erase the highlight and the cell contents because the editor
5360 // might not cover the entire cell
5361 wxClientDC
dc( m_gridWin
);
5363 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5364 dc
.SetPen(*wxTRANSPARENT_PEN
);
5365 dc
.DrawRectangle(rect
);
5368 // cell is shifted by one pixel
5372 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5373 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5374 if ( !editor
->IsCreated() )
5376 editor
->Create(m_gridWin
, -1,
5377 new wxGridCellEditorEvtHandler(this, editor
));
5380 editor
->SetSize( rect
);
5382 editor
->Show( TRUE
, attr
);
5383 editor
->BeginEdit(row
, col
, this);
5390 void wxGrid::HideCellEditControl()
5392 if ( IsCellEditControlEnabled() )
5394 int row
= m_currentCellCoords
.GetRow();
5395 int col
= m_currentCellCoords
.GetCol();
5397 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5398 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5400 m_gridWin
->SetFocus();
5405 void wxGrid::SaveEditControlValue()
5407 if ( IsCellEditControlEnabled() )
5409 int row
= m_currentCellCoords
.GetRow();
5410 int col
= m_currentCellCoords
.GetCol();
5412 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5413 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5414 bool changed
= editor
->EndEdit(row
, col
, TRUE
, this);
5420 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5421 m_currentCellCoords
.GetRow(),
5422 m_currentCellCoords
.GetCol() );
5429 // ------ Grid location functions
5430 // Note that all of these functions work with the logical coordinates of
5431 // grid cells and labels so you will need to convert from device
5432 // coordinates for mouse events etc.
5435 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5437 int row
= YToRow(y
);
5438 int col
= XToCol(x
);
5440 if ( row
== -1 || col
== -1 )
5442 coords
= wxGridNoCellCoords
;
5446 coords
.Set( row
, col
);
5451 int wxGrid::YToRow( int y
)
5455 for ( i
= 0; i
< m_numRows
; i
++ )
5457 if ( y
< GetRowBottom(i
) )
5465 int wxGrid::XToCol( int x
)
5469 for ( i
= 0; i
< m_numCols
; i
++ )
5471 if ( x
< GetColRight(i
) )
5479 // return the row number that that the y coord is near the edge of, or
5480 // -1 if not near an edge
5482 int wxGrid::YToEdgeOfRow( int y
)
5486 for ( i
= 0; i
< m_numRows
; i
++ )
5488 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5490 d
= abs( y
- GetRowBottom(i
) );
5491 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5500 // return the col number that that the x coord is near the edge of, or
5501 // -1 if not near an edge
5503 int wxGrid::XToEdgeOfCol( int x
)
5507 for ( i
= 0; i
< m_numCols
; i
++ )
5509 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5511 d
= abs( x
- GetColRight(i
) );
5512 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5521 wxRect
wxGrid::CellToRect( int row
, int col
)
5523 wxRect
rect( -1, -1, -1, -1 );
5525 if ( row
>= 0 && row
< m_numRows
&&
5526 col
>= 0 && col
< m_numCols
)
5528 rect
.x
= GetColLeft(col
);
5529 rect
.y
= GetRowTop(row
);
5530 rect
.width
= GetColWidth(col
);
5531 rect
.height
= GetRowHeight(row
);
5538 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5540 // get the cell rectangle in logical coords
5542 wxRect
r( CellToRect( row
, col
) );
5544 // convert to device coords
5546 int left
, top
, right
, bottom
;
5547 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5548 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5550 // check against the client area of the grid window
5553 m_gridWin
->GetClientSize( &cw
, &ch
);
5555 if ( wholeCellVisible
)
5557 // is the cell wholly visible ?
5559 return ( left
>= 0 && right
<= cw
&&
5560 top
>= 0 && bottom
<= ch
);
5564 // is the cell partly visible ?
5566 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5567 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5572 // make the specified cell location visible by doing a minimal amount
5575 void wxGrid::MakeCellVisible( int row
, int col
)
5578 int xpos
= -1, ypos
= -1;
5580 if ( row
>= 0 && row
< m_numRows
&&
5581 col
>= 0 && col
< m_numCols
)
5583 // get the cell rectangle in logical coords
5585 wxRect
r( CellToRect( row
, col
) );
5587 // convert to device coords
5589 int left
, top
, right
, bottom
;
5590 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5591 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5594 m_gridWin
->GetClientSize( &cw
, &ch
);
5600 else if ( bottom
> ch
)
5602 int h
= r
.GetHeight();
5604 for ( i
= row
-1; i
>= 0; i
-- )
5606 int rowHeight
= GetRowHeight(i
);
5607 if ( h
+ rowHeight
> ch
)
5614 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5615 // have rounding errors (this is important, because if we do, we
5616 // might not scroll at all and some cells won't be redrawn)
5617 ypos
+= GRID_SCROLL_LINE
/ 2;
5624 else if ( right
> cw
)
5626 int w
= r
.GetWidth();
5628 for ( i
= col
-1; i
>= 0; i
-- )
5630 int colWidth
= GetColWidth(i
);
5631 if ( w
+ colWidth
> cw
)
5638 // see comment for ypos above
5639 xpos
+= GRID_SCROLL_LINE
/ 2;
5642 if ( xpos
!= -1 || ypos
!= -1 )
5644 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5645 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5646 Scroll( xpos
, ypos
);
5654 // ------ Grid cursor movement functions
5657 bool wxGrid::MoveCursorUp()
5659 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5660 m_currentCellCoords
.GetRow() > 0 )
5662 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5663 m_currentCellCoords
.GetCol() );
5665 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5666 m_currentCellCoords
.GetCol() );
5675 bool wxGrid::MoveCursorDown()
5677 // TODO: allow for scrolling
5679 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5680 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5682 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5683 m_currentCellCoords
.GetCol() );
5685 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5686 m_currentCellCoords
.GetCol() );
5695 bool wxGrid::MoveCursorLeft()
5697 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5698 m_currentCellCoords
.GetCol() > 0 )
5700 MakeCellVisible( m_currentCellCoords
.GetRow(),
5701 m_currentCellCoords
.GetCol() - 1 );
5703 SetCurrentCell( m_currentCellCoords
.GetRow(),
5704 m_currentCellCoords
.GetCol() - 1 );
5713 bool wxGrid::MoveCursorRight()
5715 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5716 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5718 MakeCellVisible( m_currentCellCoords
.GetRow(),
5719 m_currentCellCoords
.GetCol() + 1 );
5721 SetCurrentCell( m_currentCellCoords
.GetRow(),
5722 m_currentCellCoords
.GetCol() + 1 );
5731 bool wxGrid::MovePageUp()
5733 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5735 int row
= m_currentCellCoords
.GetRow();
5739 m_gridWin
->GetClientSize( &cw
, &ch
);
5741 int y
= GetRowTop(row
);
5742 int newRow
= YToRow( y
- ch
+ 1 );
5747 else if ( newRow
== row
)
5752 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5753 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5761 bool wxGrid::MovePageDown()
5763 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5765 int row
= m_currentCellCoords
.GetRow();
5766 if ( row
< m_numRows
)
5769 m_gridWin
->GetClientSize( &cw
, &ch
);
5771 int y
= GetRowTop(row
);
5772 int newRow
= YToRow( y
+ ch
);
5775 newRow
= m_numRows
- 1;
5777 else if ( newRow
== row
)
5782 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5783 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5791 bool wxGrid::MoveCursorUpBlock()
5794 m_currentCellCoords
!= wxGridNoCellCoords
&&
5795 m_currentCellCoords
.GetRow() > 0 )
5797 int row
= m_currentCellCoords
.GetRow();
5798 int col
= m_currentCellCoords
.GetCol();
5800 if ( m_table
->IsEmptyCell(row
, col
) )
5802 // starting in an empty cell: find the next block of
5808 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5811 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5813 // starting at the top of a block: find the next block
5819 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5824 // starting within a block: find the top of the block
5829 if ( m_table
->IsEmptyCell(row
, col
) )
5837 MakeCellVisible( row
, col
);
5838 SetCurrentCell( row
, col
);
5846 bool wxGrid::MoveCursorDownBlock()
5849 m_currentCellCoords
!= wxGridNoCellCoords
&&
5850 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5852 int row
= m_currentCellCoords
.GetRow();
5853 int col
= m_currentCellCoords
.GetCol();
5855 if ( m_table
->IsEmptyCell(row
, col
) )
5857 // starting in an empty cell: find the next block of
5860 while ( row
< m_numRows
-1 )
5863 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5866 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5868 // starting at the bottom of a block: find the next block
5871 while ( row
< m_numRows
-1 )
5874 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5879 // starting within a block: find the bottom of the block
5881 while ( row
< m_numRows
-1 )
5884 if ( m_table
->IsEmptyCell(row
, col
) )
5892 MakeCellVisible( row
, col
);
5893 SetCurrentCell( row
, col
);
5901 bool wxGrid::MoveCursorLeftBlock()
5904 m_currentCellCoords
!= wxGridNoCellCoords
&&
5905 m_currentCellCoords
.GetCol() > 0 )
5907 int row
= m_currentCellCoords
.GetRow();
5908 int col
= m_currentCellCoords
.GetCol();
5910 if ( m_table
->IsEmptyCell(row
, col
) )
5912 // starting in an empty cell: find the next block of
5918 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5921 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5923 // starting at the left of a block: find the next block
5929 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5934 // starting within a block: find the left of the block
5939 if ( m_table
->IsEmptyCell(row
, col
) )
5947 MakeCellVisible( row
, col
);
5948 SetCurrentCell( row
, col
);
5956 bool wxGrid::MoveCursorRightBlock()
5959 m_currentCellCoords
!= wxGridNoCellCoords
&&
5960 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5962 int row
= m_currentCellCoords
.GetRow();
5963 int col
= m_currentCellCoords
.GetCol();
5965 if ( m_table
->IsEmptyCell(row
, col
) )
5967 // starting in an empty cell: find the next block of
5970 while ( col
< m_numCols
-1 )
5973 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5976 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5978 // starting at the right of a block: find the next block
5981 while ( col
< m_numCols
-1 )
5984 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5989 // starting within a block: find the right of the block
5991 while ( col
< m_numCols
-1 )
5994 if ( m_table
->IsEmptyCell(row
, col
) )
6002 MakeCellVisible( row
, col
);
6003 SetCurrentCell( row
, col
);
6014 // ------ Label values and formatting
6017 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6019 *horiz
= m_rowLabelHorizAlign
;
6020 *vert
= m_rowLabelVertAlign
;
6023 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6025 *horiz
= m_colLabelHorizAlign
;
6026 *vert
= m_colLabelVertAlign
;
6029 wxString
wxGrid::GetRowLabelValue( int row
)
6033 return m_table
->GetRowLabelValue( row
);
6043 wxString
wxGrid::GetColLabelValue( int col
)
6047 return m_table
->GetColLabelValue( col
);
6058 void wxGrid::SetRowLabelSize( int width
)
6060 width
= wxMax( width
, 0 );
6061 if ( width
!= m_rowLabelWidth
)
6065 m_rowLabelWin
->Show( FALSE
);
6066 m_cornerLabelWin
->Show( FALSE
);
6068 else if ( m_rowLabelWidth
== 0 )
6070 m_rowLabelWin
->Show( TRUE
);
6071 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6074 m_rowLabelWidth
= width
;
6081 void wxGrid::SetColLabelSize( int height
)
6083 height
= wxMax( height
, 0 );
6084 if ( height
!= m_colLabelHeight
)
6088 m_colLabelWin
->Show( FALSE
);
6089 m_cornerLabelWin
->Show( FALSE
);
6091 else if ( m_colLabelHeight
== 0 )
6093 m_colLabelWin
->Show( TRUE
);
6094 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6097 m_colLabelHeight
= height
;
6104 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6106 if ( m_labelBackgroundColour
!= colour
)
6108 m_labelBackgroundColour
= colour
;
6109 m_rowLabelWin
->SetBackgroundColour( colour
);
6110 m_colLabelWin
->SetBackgroundColour( colour
);
6111 m_cornerLabelWin
->SetBackgroundColour( colour
);
6113 if ( !GetBatchCount() )
6115 m_rowLabelWin
->Refresh();
6116 m_colLabelWin
->Refresh();
6117 m_cornerLabelWin
->Refresh();
6122 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6124 if ( m_labelTextColour
!= colour
)
6126 m_labelTextColour
= colour
;
6127 if ( !GetBatchCount() )
6129 m_rowLabelWin
->Refresh();
6130 m_colLabelWin
->Refresh();
6135 void wxGrid::SetLabelFont( const wxFont
& font
)
6138 if ( !GetBatchCount() )
6140 m_rowLabelWin
->Refresh();
6141 m_colLabelWin
->Refresh();
6145 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6147 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6149 m_rowLabelHorizAlign
= horiz
;
6152 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6154 m_rowLabelVertAlign
= vert
;
6157 if ( !GetBatchCount() )
6159 m_rowLabelWin
->Refresh();
6163 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6165 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6167 m_colLabelHorizAlign
= horiz
;
6170 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6172 m_colLabelVertAlign
= vert
;
6175 if ( !GetBatchCount() )
6177 m_colLabelWin
->Refresh();
6181 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6185 m_table
->SetRowLabelValue( row
, s
);
6186 if ( !GetBatchCount() )
6188 wxRect rect
= CellToRect( row
, 0);
6189 if ( rect
.height
> 0 )
6191 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6193 rect
.width
= m_rowLabelWidth
;
6194 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6200 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6204 m_table
->SetColLabelValue( col
, s
);
6205 if ( !GetBatchCount() )
6207 wxRect rect
= CellToRect( 0, col
);
6208 if ( rect
.width
> 0 )
6210 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6212 rect
.height
= m_colLabelHeight
;
6213 m_colLabelWin
->Refresh( TRUE
, &rect
);
6219 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6221 if ( m_gridLineColour
!= colour
)
6223 m_gridLineColour
= colour
;
6225 wxClientDC
dc( m_gridWin
);
6227 DrawAllGridLines( dc
, wxRegion() );
6231 void wxGrid::EnableGridLines( bool enable
)
6233 if ( enable
!= m_gridLinesEnabled
)
6235 m_gridLinesEnabled
= enable
;
6237 if ( !GetBatchCount() )
6241 wxClientDC
dc( m_gridWin
);
6243 DrawAllGridLines( dc
, wxRegion() );
6247 m_gridWin
->Refresh();
6254 int wxGrid::GetDefaultRowSize()
6256 return m_defaultRowHeight
;
6259 int wxGrid::GetRowSize( int row
)
6261 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6263 return GetRowHeight(row
);
6266 int wxGrid::GetDefaultColSize()
6268 return m_defaultColWidth
;
6271 int wxGrid::GetColSize( int col
)
6273 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6275 return GetColWidth(col
);
6278 // ============================================================================
6279 // access to the grid attributes: each of them has a default value in the grid
6280 // itself and may be overidden on a per-cell basis
6281 // ============================================================================
6283 // ----------------------------------------------------------------------------
6284 // setting default attributes
6285 // ----------------------------------------------------------------------------
6287 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6289 m_defaultCellAttr
->SetBackgroundColour(col
);
6291 m_gridWin
->SetBackgroundColour(col
);
6295 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6297 m_defaultCellAttr
->SetTextColour(col
);
6300 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6302 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6305 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6307 m_defaultCellAttr
->SetFont(font
);
6310 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6312 m_defaultCellAttr
->SetRenderer(renderer
);
6315 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6317 m_defaultCellAttr
->SetEditor(editor
);
6320 // ----------------------------------------------------------------------------
6321 // access to the default attrbiutes
6322 // ----------------------------------------------------------------------------
6324 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6326 return m_defaultCellAttr
->GetBackgroundColour();
6329 wxColour
wxGrid::GetDefaultCellTextColour()
6331 return m_defaultCellAttr
->GetTextColour();
6334 wxFont
wxGrid::GetDefaultCellFont()
6336 return m_defaultCellAttr
->GetFont();
6339 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6341 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6344 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6346 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6349 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6351 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6354 // ----------------------------------------------------------------------------
6355 // access to cell attributes
6356 // ----------------------------------------------------------------------------
6358 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6360 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6361 wxColour colour
= attr
->GetBackgroundColour();
6366 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6368 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6369 wxColour colour
= attr
->GetTextColour();
6374 wxFont
wxGrid::GetCellFont( int row
, int col
)
6376 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6377 wxFont font
= attr
->GetFont();
6382 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6384 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6385 attr
->GetAlignment(horiz
, vert
);
6389 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6391 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6392 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6397 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6399 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6400 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6405 bool wxGrid::IsReadOnly(int row
, int col
) const
6407 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6408 bool isReadOnly
= attr
->IsReadOnly();
6413 // ----------------------------------------------------------------------------
6414 // attribute support: cache, automatic provider creation, ...
6415 // ----------------------------------------------------------------------------
6417 bool wxGrid::CanHaveAttributes()
6424 return m_table
->CanHaveAttributes();
6427 void wxGrid::ClearAttrCache()
6429 if ( m_attrCache
.row
!= -1 )
6431 m_attrCache
.attr
->SafeDecRef();
6432 m_attrCache
.row
= -1;
6436 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6438 wxGrid
*self
= (wxGrid
*)this; // const_cast
6440 self
->ClearAttrCache();
6441 self
->m_attrCache
.row
= row
;
6442 self
->m_attrCache
.col
= col
;
6443 self
->m_attrCache
.attr
= attr
;
6447 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6449 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6451 *attr
= m_attrCache
.attr
;
6452 (*attr
)->SafeIncRef();
6454 #ifdef DEBUG_ATTR_CACHE
6455 gs_nAttrCacheHits
++;
6462 #ifdef DEBUG_ATTR_CACHE
6463 gs_nAttrCacheMisses
++;
6469 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6471 wxGridCellAttr
*attr
;
6472 if ( !LookupAttr(row
, col
, &attr
) )
6474 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6475 CacheAttr(row
, col
, attr
);
6479 attr
->SetDefAttr(m_defaultCellAttr
);
6483 attr
= m_defaultCellAttr
;
6490 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6492 wxGridCellAttr
*attr
;
6493 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6495 wxASSERT_MSG( m_table
,
6496 _T("we may only be called if CanHaveAttributes() "
6497 "returned TRUE and then m_table should be !NULL") );
6499 attr
= m_table
->GetAttr(row
, col
);
6502 attr
= new wxGridCellAttr
;
6504 // artificially inc the ref count to match DecRef() in caller
6507 m_table
->SetAttr(attr
, row
, col
);
6510 CacheAttr(row
, col
, attr
);
6512 attr
->SetDefAttr(m_defaultCellAttr
);
6516 // ----------------------------------------------------------------------------
6517 // setting cell attributes: this is forwarded to the table
6518 // ----------------------------------------------------------------------------
6520 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6522 if ( CanHaveAttributes() )
6524 m_table
->SetRowAttr(attr
, row
);
6532 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6534 if ( CanHaveAttributes() )
6536 m_table
->SetColAttr(attr
, col
);
6544 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6546 if ( CanHaveAttributes() )
6548 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6549 attr
->SetBackgroundColour(colour
);
6554 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6556 if ( CanHaveAttributes() )
6558 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6559 attr
->SetTextColour(colour
);
6564 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6566 if ( CanHaveAttributes() )
6568 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6569 attr
->SetFont(font
);
6574 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6576 if ( CanHaveAttributes() )
6578 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6579 attr
->SetAlignment(horiz
, vert
);
6584 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6586 if ( CanHaveAttributes() )
6588 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6589 attr
->SetRenderer(renderer
);
6594 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6596 if ( CanHaveAttributes() )
6598 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6599 attr
->SetEditor(editor
);
6604 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6606 if ( CanHaveAttributes() )
6608 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6609 attr
->SetReadOnly(isReadOnly
);
6614 // ----------------------------------------------------------------------------
6615 // Data type registration
6616 // ----------------------------------------------------------------------------
6618 void wxGrid::RegisterDataType(const wxString
& typeName
,
6619 wxGridCellRenderer
* renderer
,
6620 wxGridCellEditor
* editor
)
6622 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6626 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6628 wxString typeName
= m_table
->GetTypeName(row
, col
);
6629 return GetDefaultEditorForType(typeName
);
6632 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6634 wxString typeName
= m_table
->GetTypeName(row
, col
);
6635 return GetDefaultRendererForType(typeName
);
6639 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6641 int index
= m_typeRegistry
->FindDataType(typeName
);
6643 // Should we force the failure here or let it fallback to string handling???
6644 // wxFAIL_MSG(wxT("Unknown data type name"));
6647 return m_typeRegistry
->GetEditor(index
);
6651 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6653 int index
= m_typeRegistry
->FindDataType(typeName
);
6655 // Should we force the failure here or let it fallback to string handling???
6656 // wxFAIL_MSG(wxT("Unknown data type name"));
6659 return m_typeRegistry
->GetRenderer(index
);
6663 // ----------------------------------------------------------------------------
6665 // ----------------------------------------------------------------------------
6667 void wxGrid::EnableDragRowSize( bool enable
)
6669 m_canDragRowSize
= enable
;
6673 void wxGrid::EnableDragColSize( bool enable
)
6675 m_canDragColSize
= enable
;
6678 void wxGrid::EnableDragGridSize( bool enable
)
6680 m_canDragGridSize
= enable
;
6684 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6686 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6688 if ( resizeExistingRows
)
6696 void wxGrid::SetRowSize( int row
, int height
)
6698 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6700 if ( m_rowHeights
.IsEmpty() )
6702 // need to really create the array
6706 int h
= wxMax( 0, height
);
6707 int diff
= h
- m_rowHeights
[row
];
6709 m_rowHeights
[row
] = h
;
6711 for ( i
= row
; i
< m_numRows
; i
++ )
6713 m_rowBottoms
[i
] += diff
;
6718 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6720 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6722 if ( resizeExistingCols
)
6730 void wxGrid::SetColSize( int col
, int width
)
6732 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6734 // should we check that it's bigger than GetColMinimalWidth(col) here?
6736 if ( m_colWidths
.IsEmpty() )
6738 // need to really create the array
6742 int w
= wxMax( 0, width
);
6743 int diff
= w
- m_colWidths
[col
];
6744 m_colWidths
[col
] = w
;
6747 for ( i
= col
; i
< m_numCols
; i
++ )
6749 m_colRights
[i
] += diff
;
6755 void wxGrid::SetColMinimalWidth( int col
, int width
)
6757 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6760 int wxGrid::GetColMinimalWidth(int col
) const
6762 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6763 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6766 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6768 wxClientDC
dc(m_gridWin
);
6770 wxCoord width
, widthMax
= 0;
6771 for ( int row
= 0; row
< m_numRows
; row
++ )
6773 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6774 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6777 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6778 if ( width
> widthMax
)
6787 // now also compare with the column label width
6788 dc
.SetFont( GetLabelFont() );
6789 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6790 if ( width
> widthMax
)
6797 // empty column - give default width (notice that if widthMax is less
6798 // than default width but != 0, it's ok)
6799 widthMax
= m_defaultColWidth
;
6803 // leave some space around text
6807 SetColSize(col
, widthMax
);
6810 SetColMinimalWidth(col
, widthMax
);
6814 void wxGrid::AutoSizeColumns( bool setAsMin
)
6816 for ( int col
= 0; col
< m_numCols
; col
++ )
6818 AutoSizeColumn(col
, setAsMin
);
6823 // ------ cell value accessor functions
6826 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6830 m_table
->SetValue( row
, col
, s
.c_str() );
6831 if ( !GetBatchCount() )
6833 wxClientDC
dc( m_gridWin
);
6835 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6838 if ( m_currentCellCoords
.GetRow() == row
&&
6839 m_currentCellCoords
.GetCol() == col
&&
6840 IsCellEditControlEnabled())
6842 HideCellEditControl();
6843 ShowCellEditControl(); // will reread data from table
6850 // ------ Block, row and col selection
6853 void wxGrid::SelectRow( int row
, bool addToSelected
)
6857 if ( IsSelection() && addToSelected
)
6860 bool need_refresh
[4];
6864 need_refresh
[3] = FALSE
;
6868 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6869 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6870 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6871 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6875 need_refresh
[0] = TRUE
;
6876 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6877 wxGridCellCoords ( oldTop
- 1,
6879 m_selectedTopLeft
.SetRow( row
);
6884 need_refresh
[1] = TRUE
;
6885 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6886 wxGridCellCoords ( oldBottom
,
6889 m_selectedTopLeft
.SetCol( 0 );
6892 if ( oldBottom
< row
)
6894 need_refresh
[2] = TRUE
;
6895 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6896 wxGridCellCoords ( row
,
6898 m_selectedBottomRight
.SetRow( row
);
6901 if ( oldRight
< m_numCols
- 1 )
6903 need_refresh
[3] = TRUE
;
6904 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6906 wxGridCellCoords ( oldBottom
,
6908 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6911 for (i
= 0; i
< 4; i
++ )
6912 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6913 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6917 r
= SelectionToDeviceRect();
6919 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6921 m_selectedTopLeft
.Set( row
, 0 );
6922 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6923 r
= SelectionToDeviceRect();
6924 m_gridWin
->Refresh( FALSE
, &r
);
6927 wxGridRangeSelectEvent
gridEvt( GetId(),
6928 wxEVT_GRID_RANGE_SELECT
,
6931 m_selectedBottomRight
);
6933 GetEventHandler()->ProcessEvent(gridEvt
);
6937 void wxGrid::SelectCol( int col
, bool addToSelected
)
6939 if ( IsSelection() && addToSelected
)
6942 bool need_refresh
[4];
6946 need_refresh
[3] = FALSE
;
6949 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6950 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6951 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6952 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6954 if ( oldLeft
> col
)
6956 need_refresh
[0] = TRUE
;
6957 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6958 wxGridCellCoords ( m_numRows
- 1,
6960 m_selectedTopLeft
.SetCol( col
);
6965 need_refresh
[1] = TRUE
;
6966 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6967 wxGridCellCoords ( oldTop
- 1,
6969 m_selectedTopLeft
.SetRow( 0 );
6972 if ( oldRight
< col
)
6974 need_refresh
[2] = TRUE
;
6975 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6976 wxGridCellCoords ( m_numRows
- 1,
6978 m_selectedBottomRight
.SetCol( col
);
6981 if ( oldBottom
< m_numRows
- 1 )
6983 need_refresh
[3] = TRUE
;
6984 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6986 wxGridCellCoords ( m_numRows
- 1,
6988 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6991 for (i
= 0; i
< 4; i
++ )
6992 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6993 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6999 r
= SelectionToDeviceRect();
7001 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7003 m_selectedTopLeft
.Set( 0, col
);
7004 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7005 r
= SelectionToDeviceRect();
7006 m_gridWin
->Refresh( FALSE
, &r
);
7009 wxGridRangeSelectEvent
gridEvt( GetId(),
7010 wxEVT_GRID_RANGE_SELECT
,
7013 m_selectedBottomRight
);
7015 GetEventHandler()->ProcessEvent(gridEvt
);
7019 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7022 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7024 if ( topRow
> bottomRow
)
7031 if ( leftCol
> rightCol
)
7038 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7039 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7041 if ( m_selectedTopLeft
!= updateTopLeft
||
7042 m_selectedBottomRight
!= updateBottomRight
)
7044 // Compute two optimal update rectangles:
7045 // Either one rectangle is a real subset of the
7046 // other, or they are (almost) disjoint!
7048 bool need_refresh
[4];
7052 need_refresh
[3] = FALSE
;
7055 // Store intermediate values
7056 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7057 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7058 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7059 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7061 // Determine the outer/inner coordinates.
7062 if (oldLeft
> leftCol
)
7068 if (oldTop
> topRow
)
7074 if (oldRight
< rightCol
)
7077 oldRight
= rightCol
;
7080 if (oldBottom
< bottomRow
)
7083 oldBottom
= bottomRow
;
7087 // Now, either the stuff marked old is the outer
7088 // rectangle or we don't have a situation where one
7089 // is contained in the other.
7091 if ( oldLeft
< leftCol
)
7093 need_refresh
[0] = TRUE
;
7094 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7096 wxGridCellCoords ( oldBottom
,
7100 if ( oldTop
< topRow
)
7102 need_refresh
[1] = TRUE
;
7103 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7105 wxGridCellCoords ( topRow
- 1,
7109 if ( oldRight
> rightCol
)
7111 need_refresh
[2] = TRUE
;
7112 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7114 wxGridCellCoords ( oldBottom
,
7118 if ( oldBottom
> bottomRow
)
7120 need_refresh
[3] = TRUE
;
7121 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7123 wxGridCellCoords ( oldBottom
,
7129 m_selectedTopLeft
= updateTopLeft
;
7130 m_selectedBottomRight
= updateBottomRight
;
7132 // various Refresh() calls
7133 for (i
= 0; i
< 4; i
++ )
7134 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7135 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7138 // only generate an event if the block is not being selected by
7139 // dragging the mouse (in which case the event will be generated in
7140 // the mouse event handler)
7141 if ( !m_isDragging
)
7143 wxGridRangeSelectEvent
gridEvt( GetId(),
7144 wxEVT_GRID_RANGE_SELECT
,
7147 m_selectedBottomRight
);
7149 GetEventHandler()->ProcessEvent(gridEvt
);
7153 void wxGrid::SelectAll()
7155 m_selectedTopLeft
.Set( 0, 0 );
7156 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7158 m_gridWin
->Refresh();
7162 void wxGrid::ClearSelection()
7164 m_selectedTopLeft
= wxGridNoCellCoords
;
7165 m_selectedBottomRight
= wxGridNoCellCoords
;
7169 // This function returns the rectangle that encloses the given block
7170 // in device coords clipped to the client size of the grid window.
7172 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7173 const wxGridCellCoords
&bottomRight
)
7175 wxRect
rect( wxGridNoCellRect
);
7178 cellRect
= CellToRect( topLeft
);
7179 if ( cellRect
!= wxGridNoCellRect
)
7185 rect
= wxRect( 0, 0, 0, 0 );
7188 cellRect
= CellToRect( bottomRight
);
7189 if ( cellRect
!= wxGridNoCellRect
)
7195 return wxGridNoCellRect
;
7198 // convert to scrolled coords
7200 int left
, top
, right
, bottom
;
7201 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7202 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7205 m_gridWin
->GetClientSize( &cw
, &ch
);
7207 rect
.SetLeft( wxMax(0, left
) );
7208 rect
.SetTop( wxMax(0, top
) );
7209 rect
.SetRight( wxMin(cw
, right
) );
7210 rect
.SetBottom( wxMin(ch
, bottom
) );
7218 // ------ Grid event classes
7221 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7223 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7224 int row
, int col
, int x
, int y
,
7225 bool control
, bool shift
, bool alt
, bool meta
)
7226 : wxNotifyEvent( type
, id
)
7232 m_control
= control
;
7237 SetEventObject(obj
);
7241 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7243 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7244 int rowOrCol
, int x
, int y
,
7245 bool control
, bool shift
, bool alt
, bool meta
)
7246 : wxNotifyEvent( type
, id
)
7248 m_rowOrCol
= rowOrCol
;
7251 m_control
= control
;
7256 SetEventObject(obj
);
7260 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7262 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7263 const wxGridCellCoords
& topLeft
,
7264 const wxGridCellCoords
& bottomRight
,
7265 bool control
, bool shift
, bool alt
, bool meta
)
7266 : wxNotifyEvent( type
, id
)
7268 m_topLeft
= topLeft
;
7269 m_bottomRight
= bottomRight
;
7270 m_control
= control
;
7275 SetEventObject(obj
);
7279 #endif // ifndef wxUSE_NEW_GRID