1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
59 struct wxGridCellWithAttr
61 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
62 : coords(row
, col
), attr(attr_
)
71 wxGridCellCoords coords
;
75 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
77 #include "wx/arrimpl.cpp"
79 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
80 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
89 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
90 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
91 const wxPoint
&pos
, const wxSize
&size
);
96 void OnPaint( wxPaintEvent
& event
);
97 void OnMouseEvent( wxMouseEvent
& event
);
98 void OnKeyDown( wxKeyEvent
& event
);
100 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
108 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
109 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
110 const wxPoint
&pos
, const wxSize
&size
);
115 void OnPaint( wxPaintEvent
&event
);
116 void OnMouseEvent( wxMouseEvent
& event
);
117 void OnKeyDown( wxKeyEvent
& event
);
119 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
120 DECLARE_EVENT_TABLE()
124 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
127 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
128 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnKeyDown( wxKeyEvent
& event
);
136 void OnPaint( wxPaintEvent
& event
);
138 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
139 DECLARE_EVENT_TABLE()
142 class WXDLLEXPORT wxGridWindow
: public wxPanel
147 m_owner
= (wxGrid
*)NULL
;
148 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
149 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
152 wxGridWindow( wxGrid
*parent
,
153 wxGridRowLabelWindow
*rowLblWin
,
154 wxGridColLabelWindow
*colLblWin
,
155 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
158 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
162 wxGridRowLabelWindow
*m_rowLabelWin
;
163 wxGridColLabelWindow
*m_colLabelWin
;
165 void OnPaint( wxPaintEvent
&event
);
166 void OnMouseEvent( wxMouseEvent
& event
);
167 void OnKeyDown( wxKeyEvent
& );
168 void OnEraseBackground( wxEraseEvent
& );
171 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
172 DECLARE_EVENT_TABLE()
177 class wxGridCellEditorEvtHandler
: public wxEvtHandler
180 wxGridCellEditorEvtHandler()
181 : m_grid(0), m_editor(0)
183 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
184 : m_grid(grid
), m_editor(editor
)
187 void OnKeyDown(wxKeyEvent
& event
);
188 void OnChar(wxKeyEvent
& event
);
192 wxGridCellEditor
* m_editor
;
193 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
194 DECLARE_EVENT_TABLE()
198 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
199 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
200 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
201 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
206 // ----------------------------------------------------------------------------
207 // the internal data representation used by wxGridCellAttrProvider
208 // ----------------------------------------------------------------------------
210 // this class stores attributes set for cells
211 class WXDLLEXPORT wxGridCellAttrData
214 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
215 wxGridCellAttr
*GetAttr(int row
, int col
) const;
216 void UpdateAttrRows( size_t pos
, int numRows
);
217 void UpdateAttrCols( size_t pos
, int numCols
);
220 // searches for the attr for given cell, returns wxNOT_FOUND if not found
221 int FindIndex(int row
, int col
) const;
223 wxGridCellWithAttrArray m_attrs
;
226 // this class stores attributes set for rows or columns
227 class WXDLLEXPORT wxGridRowOrColAttrData
230 // empty ctor to suppress warnings
231 wxGridRowOrColAttrData() { }
232 ~wxGridRowOrColAttrData();
234 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
235 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
236 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
239 wxArrayInt m_rowsOrCols
;
240 wxArrayAttrs m_attrs
;
243 // NB: this is just a wrapper around 3 objects: one which stores cell
244 // attributes, and 2 others for row/col ones
245 class WXDLLEXPORT wxGridCellAttrProviderData
248 wxGridCellAttrData m_cellAttrs
;
249 wxGridRowOrColAttrData m_rowAttrs
,
254 // ----------------------------------------------------------------------------
255 // data structures used for the data type registry
256 // ----------------------------------------------------------------------------
258 struct wxGridDataTypeInfo
{
259 wxGridDataTypeInfo(const wxString
& typeName
,
260 wxGridCellRenderer
* renderer
,
261 wxGridCellEditor
* editor
)
262 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
265 ~wxGridDataTypeInfo() { delete m_renderer
; delete m_editor
; }
268 wxGridCellRenderer
* m_renderer
;
269 wxGridCellEditor
* m_editor
;
273 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
276 class WXDLLEXPORT wxGridTypeRegistry
{
278 ~wxGridTypeRegistry();
279 void RegisterDataType(const wxString
& typeName
,
280 wxGridCellRenderer
* renderer
,
281 wxGridCellEditor
* editor
);
282 int FindDataType(const wxString
& typeName
);
283 wxGridCellRenderer
* GetRenderer(int index
);
284 wxGridCellEditor
* GetEditor(int index
);
287 wxGridDataTypeInfoArray m_typeinfo
;
293 // ----------------------------------------------------------------------------
294 // conditional compilation
295 // ----------------------------------------------------------------------------
297 #ifndef WXGRID_DRAW_LINES
298 #define WXGRID_DRAW_LINES 1
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 //#define DEBUG_ATTR_CACHE
306 #ifdef DEBUG_ATTR_CACHE
307 static size_t gs_nAttrCacheHits
= 0;
308 static size_t gs_nAttrCacheMisses
= 0;
309 #endif // DEBUG_ATTR_CACHE
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
316 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
319 // TODO: fixed so far - make configurable later (and also different for x/y)
320 static const size_t GRID_SCROLL_LINE
= 10;
322 // the size of hash tables used a bit everywhere (the max number of elements
323 // in these hash tables is the number of rows/columns)
324 static const int GRID_HASH_SIZE
= 100;
326 // ============================================================================
328 // ============================================================================
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 wxGridCellEditor::wxGridCellEditor()
340 wxGridCellEditor::~wxGridCellEditor()
345 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
346 wxWindowID
WXUNUSED(id
),
347 wxEvtHandler
* evtHandler
)
350 m_control
->PushEventHandler(evtHandler
);
353 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
354 wxGridCellAttr
*attr
)
356 // erase the background because we might not fill the cell
357 wxClientDC
dc(m_control
->GetParent());
358 dc
.SetPen(*wxTRANSPARENT_PEN
);
359 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
360 dc
.DrawRectangle(rectCell
);
362 // redraw the control we just painted over
363 m_control
->Refresh();
366 void wxGridCellEditor::Destroy()
370 m_control
->Destroy();
375 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
377 wxASSERT_MSG(m_control
,
378 wxT("The wxGridCellEditor must be Created first!"));
379 m_control
->Show(show
);
383 // set the colours/fonts if we have any
386 m_colFgOld
= m_control
->GetForegroundColour();
387 m_control
->SetForegroundColour(attr
->GetTextColour());
389 m_colBgOld
= m_control
->GetBackgroundColour();
390 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
392 m_fontOld
= m_control
->GetFont();
393 m_control
->SetFont(attr
->GetFont());
395 // can't do anything more in the base class version, the other
396 // attributes may only be used by the derived classes
401 // restore the standard colours fonts
402 if ( m_colFgOld
.Ok() )
404 m_control
->SetForegroundColour(m_colFgOld
);
405 m_colFgOld
= wxNullColour
;
408 if ( m_colBgOld
.Ok() )
410 m_control
->SetBackgroundColour(m_colBgOld
);
411 m_colBgOld
= wxNullColour
;
414 if ( m_fontOld
.Ok() )
416 m_control
->SetFont(m_fontOld
);
417 m_fontOld
= wxNullFont
;
422 void wxGridCellEditor::SetSize(const wxRect
& rect
)
424 wxASSERT_MSG(m_control
,
425 wxT("The wxGridCellEditor must be Created first!"));
426 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
429 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
435 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
440 void wxGridCellEditor::StartingClick()
444 // ----------------------------------------------------------------------------
445 // wxGridCellTextEditor
446 // ----------------------------------------------------------------------------
448 wxGridCellTextEditor::wxGridCellTextEditor()
452 void wxGridCellTextEditor::Create(wxWindow
* parent
,
454 wxEvtHandler
* evtHandler
)
456 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
457 wxDefaultPosition
, wxDefaultSize
458 #if defined(__WXMSW__)
459 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
463 wxGridCellEditor::Create(parent
, id
, evtHandler
);
466 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
467 wxGridCellAttr
* WXUNUSED(attr
))
469 // as we fill the entire client area, don't do anything here to minimize
473 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
475 wxRect
rect(rectOrig
);
477 // Make the edit control large enough to allow for internal
480 // TODO: remove this if the text ctrl sizing is improved esp. for
483 #if defined(__WXGTK__)
484 rect
.Inflate(rect
.x
? 1 : 0, rect
.y
? 1 : 0);
486 int extra
= rect
.x
&& rect
.y
? 2 : 1;
487 #if defined(__WXMOTIF__)
490 rect
.SetLeft( wxMax(0, rect
.x
- extra
) );
491 rect
.SetTop( wxMax(0, rect
.y
- extra
) );
492 rect
.SetRight( rect
.GetRight() + 2*extra
);
493 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
496 wxGridCellEditor::SetSize(rect
);
499 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
501 wxASSERT_MSG(m_control
,
502 wxT("The wxGridCellEditor must be Created first!"));
504 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
506 DoBeginEdit(m_startValue
);
509 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
511 Text()->SetValue(startValue
);
512 Text()->SetInsertionPointEnd();
516 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
519 wxASSERT_MSG(m_control
,
520 wxT("The wxGridCellEditor must be Created first!"));
522 bool changed
= FALSE
;
523 wxString value
= Text()->GetValue();
524 if (value
!= m_startValue
)
528 grid
->GetTable()->SetValue(row
, col
, value
);
530 m_startValue
= wxEmptyString
;
531 Text()->SetValue(m_startValue
);
537 void wxGridCellTextEditor::Reset()
539 wxASSERT_MSG(m_control
,
540 wxT("The wxGridCellEditor must be Created first!"));
542 DoReset(m_startValue
);
545 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
547 Text()->SetValue(startValue
);
548 Text()->SetInsertionPointEnd();
551 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
553 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
555 // insert the key in the control
556 long keycode
= event
.KeyCode();
557 if ( isprint(keycode
) )
559 // FIXME this is not going to work for non letters...
560 if ( !event
.ShiftDown() )
562 keycode
= tolower(keycode
);
565 Text()->AppendText((wxChar
)keycode
);
575 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
577 #if defined(__WXMOTIF__) || defined(__WXGTK__)
578 // wxMotif needs a little extra help...
579 int pos
= Text()->GetInsertionPoint();
580 wxString
s( Text()->GetValue() );
581 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
583 Text()->SetInsertionPoint( pos
);
585 // the other ports can handle a Return key press
591 // ----------------------------------------------------------------------------
592 // wxGridCellNumberEditor
593 // ----------------------------------------------------------------------------
595 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
601 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
603 wxEvtHandler
* evtHandler
)
607 // create a spin ctrl
608 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
609 wxDefaultPosition
, wxDefaultSize
,
613 wxGridCellEditor::Create(parent
, id
, evtHandler
);
617 // just a text control
618 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
621 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
622 #endif // wxUSE_VALIDATORS
626 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
628 // first get the value
629 wxGridTableBase
*table
= grid
->GetTable();
630 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
632 m_valueOld
= table
->GetValueAsLong(row
, col
);
636 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
643 Spin()->SetValue(m_valueOld
);
647 DoBeginEdit(GetString());
651 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
659 value
= Spin()->GetValue();
660 changed
= value
!= m_valueOld
;
664 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
669 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
675 void wxGridCellNumberEditor::Reset()
679 Spin()->SetValue(m_valueOld
);
683 DoReset(GetString());
687 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
691 long keycode
= event
.KeyCode();
692 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
694 wxGridCellTextEditor::StartingKey(event
);
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
,
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
,
829 wxASSERT_MSG(m_control
,
830 wxT("The wxGridCellEditor must be Created first!"));
832 bool changed
= FALSE
;
833 bool value
= CBox()->GetValue();
834 if ( value
!= m_startValue
)
839 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
840 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
842 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
848 void wxGridCellBoolEditor::Reset()
850 wxASSERT_MSG(m_control
,
851 wxT("The wxGridCellEditor must be Created first!"));
853 CBox()->SetValue(m_startValue
);
856 void wxGridCellBoolEditor::StartingClick()
858 CBox()->SetValue(!CBox()->GetValue());
861 // ----------------------------------------------------------------------------
862 // wxGridCellChoiceEditor
863 // ----------------------------------------------------------------------------
865 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
866 const wxChar
* choices
[],
868 : m_allowOthers(allowOthers
)
870 m_choices
.Alloc(count
);
871 for ( size_t n
= 0; n
< count
; n
++ )
873 m_choices
.Add(choices
[n
]);
877 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
879 wxEvtHandler
* evtHandler
)
881 size_t count
= m_choices
.GetCount();
882 wxString
*choices
= new wxString
[count
];
883 for ( size_t n
= 0; n
< count
; n
++ )
885 choices
[n
] = m_choices
[n
];
888 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
889 wxDefaultPosition
, wxDefaultSize
,
891 m_allowOthers
? 0 : wxCB_READONLY
);
895 wxGridCellEditor::Create(parent
, id
, evtHandler
);
898 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
899 wxGridCellAttr
* WXUNUSED(attr
))
901 // as we fill the entire client area, don't do anything here to minimize
905 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
907 wxASSERT_MSG(m_control
,
908 wxT("The wxGridCellEditor must be Created first!"));
910 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
912 Combo()->SetValue(m_startValue
);
913 size_t count
= m_choices
.GetCount();
914 for (size_t i
=0; i
<count
; i
++)
916 if (m_startValue
== m_choices
[i
])
918 Combo()->SetSelection(i
);
922 Combo()->SetInsertionPointEnd();
926 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
929 wxString value
= Combo()->GetValue();
930 bool changed
= value
!= m_startValue
;
933 grid
->GetTable()->SetValue(row
, col
, value
);
935 m_startValue
= wxEmptyString
;
936 Combo()->SetValue(m_startValue
);
941 void wxGridCellChoiceEditor::Reset()
943 Combo()->SetValue(m_startValue
);
944 Combo()->SetInsertionPointEnd();
947 // ----------------------------------------------------------------------------
948 // wxGridCellEditorEvtHandler
949 // ----------------------------------------------------------------------------
951 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
953 switch ( event
.KeyCode() )
957 m_grid
->DisableCellEditControl();
961 event
.Skip( m_grid
->ProcessEvent( event
) );
965 if (!m_grid
->ProcessEvent(event
))
966 m_editor
->HandleReturn(event
);
975 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
977 switch ( event
.KeyCode() )
989 // ============================================================================
991 // ============================================================================
993 // ----------------------------------------------------------------------------
994 // wxGridCellRenderer
995 // ----------------------------------------------------------------------------
997 void wxGridCellRenderer::Draw(wxGrid
& grid
,
998 wxGridCellAttr
& attr
,
1004 dc
.SetBackgroundMode( wxSOLID
);
1008 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1012 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1015 dc
.SetPen( *wxTRANSPARENT_PEN
);
1016 dc
.DrawRectangle(rect
);
1019 wxGridCellRenderer::~wxGridCellRenderer()
1023 // ----------------------------------------------------------------------------
1024 // wxGridCellStringRenderer
1025 // ----------------------------------------------------------------------------
1027 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1028 wxGridCellAttr
& attr
,
1032 dc
.SetBackgroundMode( wxTRANSPARENT
);
1034 // TODO some special colours for attr.IsReadOnly() case?
1038 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1039 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1043 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1044 dc
.SetTextForeground( attr
.GetTextColour() );
1047 dc
.SetFont( attr
.GetFont() );
1050 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1052 const wxString
& text
)
1055 dc
.SetFont(attr
.GetFont());
1056 dc
.GetTextExtent(text
, &x
, &y
);
1058 return wxSize(x
, y
);
1061 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1062 wxGridCellAttr
& attr
,
1066 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1069 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1070 wxGridCellAttr
& attr
,
1072 const wxRect
& rectCell
,
1076 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1078 // now we only have to draw the text
1079 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1082 attr
.GetAlignment(&hAlign
, &vAlign
);
1084 wxRect rect
= rectCell
;
1087 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1088 rect
, hAlign
, vAlign
);
1091 // ----------------------------------------------------------------------------
1092 // wxGridCellNumberRenderer
1093 // ----------------------------------------------------------------------------
1095 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1097 wxGridTableBase
*table
= grid
.GetTable();
1099 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1101 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1103 //else: leave the string empty or put 0 into it?
1108 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1109 wxGridCellAttr
& attr
,
1111 const wxRect
& rectCell
,
1115 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1117 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1119 // draw the text right aligned by default
1121 attr
.GetAlignment(&hAlign
, &vAlign
);
1124 wxRect rect
= rectCell
;
1127 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1130 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1131 wxGridCellAttr
& attr
,
1135 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1138 // ----------------------------------------------------------------------------
1139 // wxGridCellFloatRenderer
1140 // ----------------------------------------------------------------------------
1142 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1145 SetPrecision(precision
);
1148 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1150 wxGridTableBase
*table
= grid
.GetTable();
1152 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1156 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1159 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1161 //else: leave the string empty or put 0 into it?
1166 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1167 wxGridCellAttr
& attr
,
1169 const wxRect
& rectCell
,
1173 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1175 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1177 // draw the text right aligned by default
1179 attr
.GetAlignment(&hAlign
, &vAlign
);
1182 wxRect rect
= rectCell
;
1185 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1188 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1189 wxGridCellAttr
& attr
,
1193 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1196 // ----------------------------------------------------------------------------
1197 // wxGridCellBoolRenderer
1198 // ----------------------------------------------------------------------------
1200 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1202 // between checkmark and box
1203 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1205 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1206 wxGridCellAttr
& WXUNUSED(attr
),
1211 // compute it only once (no locks for MT safeness in GUI thread...)
1212 if ( !ms_sizeCheckMark
.x
)
1214 // get checkbox size
1215 wxCoord checkSize
= 0;
1216 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1217 wxSize size
= checkbox
->GetBestSize();
1218 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1220 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1222 checkSize
-= size
.y
/ 2;
1227 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1230 return ms_sizeCheckMark
;
1233 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1234 wxGridCellAttr
& attr
,
1240 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1242 // draw a check mark in the centre (ignoring alignment - TODO)
1243 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1245 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1246 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1247 rectMark
.width
= size
.x
;
1248 rectMark
.height
= size
.y
;
1250 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1251 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1252 dc
.DrawRectangle(rectMark
);
1254 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1257 if (grid
.GetTable()->CanGetValueAs(row
, col
, wxT("bool")))
1258 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1260 value
= !!grid
.GetTable()->GetValue(row
, col
);
1264 dc
.SetTextForeground(attr
.GetTextColour());
1265 dc
.DrawCheckMark(rectMark
);
1269 // ----------------------------------------------------------------------------
1271 // ----------------------------------------------------------------------------
1273 const wxColour
& wxGridCellAttr::GetTextColour() const
1275 if (HasTextColour())
1279 else if (m_defGridAttr
!= this)
1281 return m_defGridAttr
->GetTextColour();
1285 wxFAIL_MSG(wxT("Missing default cell attribute"));
1286 return wxNullColour
;
1291 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1293 if (HasBackgroundColour())
1295 else if (m_defGridAttr
!= this)
1296 return m_defGridAttr
->GetBackgroundColour();
1299 wxFAIL_MSG(wxT("Missing default cell attribute"));
1300 return wxNullColour
;
1305 const wxFont
& wxGridCellAttr::GetFont() const
1309 else if (m_defGridAttr
!= this)
1310 return m_defGridAttr
->GetFont();
1313 wxFAIL_MSG(wxT("Missing default cell attribute"));
1319 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1323 if ( hAlign
) *hAlign
= m_hAlign
;
1324 if ( vAlign
) *vAlign
= m_vAlign
;
1326 else if (m_defGridAttr
!= this)
1327 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1330 wxFAIL_MSG(wxT("Missing default cell attribute"));
1335 // GetRenderer and GetEditor use a slightly different decision path about
1336 // which attribute to use. If a non-default attr object has one then it is
1337 // used, otherwise the default editor or renderer is fetched from the grid and
1338 // used. It should be the default for the data type of the cell. If it is
1339 // NULL (because the table has a type that the grid does not have in its
1340 // registry,) then the grid's default editor or renderer is used.
1342 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1344 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1345 return m_renderer
; // use local attribute
1347 wxGridCellRenderer
* renderer
= NULL
;
1348 if (grid
) // get renderer for the data type
1349 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1352 // if we still don't have one then use the grid default
1353 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1356 wxFAIL_MSG(wxT("Missing default cell attribute"));
1361 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1363 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1364 return m_editor
; // use local attribute
1366 wxGridCellEditor
* editor
= NULL
;
1367 if (grid
) // get renderer for the data type
1368 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1371 // if we still don't have one then use the grid default
1372 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1375 wxFAIL_MSG(wxT("Missing default cell attribute"));
1380 // ----------------------------------------------------------------------------
1381 // wxGridCellAttrData
1382 // ----------------------------------------------------------------------------
1384 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1386 int n
= FindIndex(row
, col
);
1387 if ( n
== wxNOT_FOUND
)
1389 // add the attribute
1390 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1396 // change the attribute
1397 m_attrs
[(size_t)n
].attr
= attr
;
1401 // remove this attribute
1402 m_attrs
.RemoveAt((size_t)n
);
1407 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1409 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1411 int n
= FindIndex(row
, col
);
1412 if ( n
!= wxNOT_FOUND
)
1414 attr
= m_attrs
[(size_t)n
].attr
;
1421 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1423 size_t count
= m_attrs
.GetCount();
1424 for ( size_t n
= 0; n
< count
; n
++ )
1426 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1427 wxCoord row
= coords
.GetRow();
1428 if ((size_t)row
>= pos
)
1432 // If rows inserted, include row counter where necessary
1433 coords
.SetRow(row
+ numRows
);
1435 else if (numRows
< 0)
1437 // If rows deleted ...
1438 if ((size_t)row
>= pos
- numRows
)
1440 // ...either decrement row counter (if row still exists)...
1441 coords
.SetRow(row
+ numRows
);
1445 // ...or remove the attribute
1446 m_attrs
.RemoveAt((size_t)n
);
1454 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1456 size_t count
= m_attrs
.GetCount();
1457 for ( size_t n
= 0; n
< count
; n
++ )
1459 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1460 wxCoord col
= coords
.GetCol();
1461 if ( (size_t)col
>= pos
)
1465 // If rows inserted, include row counter where necessary
1466 coords
.SetCol(col
+ numCols
);
1468 else if (numCols
< 0)
1470 // If rows deleted ...
1471 if ((size_t)col
>= pos
- numCols
)
1473 // ...either decrement row counter (if row still exists)...
1474 coords
.SetCol(col
+ numCols
);
1478 // ...or remove the attribute
1479 m_attrs
.RemoveAt((size_t)n
);
1487 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1489 size_t count
= m_attrs
.GetCount();
1490 for ( size_t n
= 0; n
< count
; n
++ )
1492 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1493 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1502 // ----------------------------------------------------------------------------
1503 // wxGridRowOrColAttrData
1504 // ----------------------------------------------------------------------------
1506 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1508 size_t count
= m_attrs
.Count();
1509 for ( size_t n
= 0; n
< count
; n
++ )
1511 m_attrs
[n
]->DecRef();
1515 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1517 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1519 int n
= m_rowsOrCols
.Index(rowOrCol
);
1520 if ( n
!= wxNOT_FOUND
)
1522 attr
= m_attrs
[(size_t)n
];
1529 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1531 int n
= m_rowsOrCols
.Index(rowOrCol
);
1532 if ( n
== wxNOT_FOUND
)
1534 // add the attribute
1535 m_rowsOrCols
.Add(rowOrCol
);
1542 // change the attribute
1543 m_attrs
[(size_t)n
] = attr
;
1547 // remove this attribute
1548 m_attrs
[(size_t)n
]->DecRef();
1549 m_rowsOrCols
.RemoveAt((size_t)n
);
1550 m_attrs
.RemoveAt((size_t)n
);
1555 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1557 size_t count
= m_attrs
.GetCount();
1558 for ( size_t n
= 0; n
< count
; n
++ )
1560 int & rowOrCol
= m_rowsOrCols
[n
];
1561 if ( (size_t)rowOrCol
>= pos
)
1563 if ( numRowsOrCols
> 0 )
1565 // If rows inserted, include row counter where necessary
1566 rowOrCol
+= numRowsOrCols
;
1568 else if ( numRowsOrCols
< 0)
1570 // If rows deleted, either decrement row counter (if row still exists)
1571 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1572 rowOrCol
+= numRowsOrCols
;
1575 m_rowsOrCols
.RemoveAt((size_t)n
);
1576 m_attrs
.RemoveAt((size_t)n
);
1584 // ----------------------------------------------------------------------------
1585 // wxGridCellAttrProvider
1586 // ----------------------------------------------------------------------------
1588 wxGridCellAttrProvider::wxGridCellAttrProvider()
1590 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1593 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1598 void wxGridCellAttrProvider::InitData()
1600 m_data
= new wxGridCellAttrProviderData
;
1603 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1605 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1608 // first look for the attribute of this specific cell
1609 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1613 // then look for the col attr (col attributes are more common than
1614 // the row ones, hence they have priority)
1615 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1620 // finally try the row attributes
1621 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1628 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1634 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1637 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1642 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1645 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1650 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1653 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1657 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1659 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1663 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1667 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1669 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1673 // ----------------------------------------------------------------------------
1674 // wxGridTypeRegistry
1675 // ----------------------------------------------------------------------------
1677 wxGridTypeRegistry::~wxGridTypeRegistry()
1679 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++)
1680 delete m_typeinfo
[i
];
1684 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1685 wxGridCellRenderer
* renderer
,
1686 wxGridCellEditor
* editor
)
1689 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1691 // is it already registered?
1692 if ((loc
= FindDataType(typeName
)) != -1) {
1693 delete m_typeinfo
[loc
];
1694 m_typeinfo
[loc
] = info
;
1697 m_typeinfo
.Add(info
);
1701 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1705 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1706 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1715 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1717 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1721 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1723 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1727 // ----------------------------------------------------------------------------
1729 // ----------------------------------------------------------------------------
1731 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1734 wxGridTableBase::wxGridTableBase()
1736 m_view
= (wxGrid
*) NULL
;
1737 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1740 wxGridTableBase::~wxGridTableBase()
1742 delete m_attrProvider
;
1745 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1747 delete m_attrProvider
;
1748 m_attrProvider
= attrProvider
;
1751 bool wxGridTableBase::CanHaveAttributes()
1753 if ( ! GetAttrProvider() )
1755 // use the default attr provider by default
1756 SetAttrProvider(new wxGridCellAttrProvider
);
1761 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1763 if ( m_attrProvider
)
1764 return m_attrProvider
->GetAttr(row
, col
);
1766 return (wxGridCellAttr
*)NULL
;
1769 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1771 if ( m_attrProvider
)
1773 m_attrProvider
->SetAttr(attr
, row
, col
);
1777 // as we take ownership of the pointer and don't store it, we must
1783 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1785 if ( m_attrProvider
)
1787 m_attrProvider
->SetRowAttr(attr
, row
);
1791 // as we take ownership of the pointer and don't store it, we must
1797 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1799 if ( m_attrProvider
)
1801 m_attrProvider
->SetColAttr(attr
, col
);
1805 // as we take ownership of the pointer and don't store it, we must
1811 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1813 if ( m_attrProvider
)
1815 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1819 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1821 if ( m_attrProvider
)
1823 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1827 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1829 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1830 "but your derived table class does not override this function") );
1835 bool wxGridTableBase::AppendRows( size_t numRows
)
1837 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1838 "but your derived table class does not override this function"));
1843 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1845 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1846 "but your derived table class does not override this function"));
1851 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1853 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1854 "but your derived table class does not override this function"));
1859 bool wxGridTableBase::AppendCols( size_t numCols
)
1861 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1862 "but your derived table class does not override this function"));
1867 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1869 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1870 "but your derived table class does not override this function"));
1876 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1879 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1880 // how much it makes sense to us geeks.
1884 wxString
wxGridTableBase::GetColLabelValue( int col
)
1886 // default col labels are:
1887 // cols 0 to 25 : A-Z
1888 // cols 26 to 675 : AA-ZZ
1893 for ( n
= 1; ; n
++ )
1895 s
+= (_T('A') + (wxChar
)( col%26
));
1897 if ( col
< 0 ) break;
1900 // reverse the string...
1902 for ( i
= 0; i
< n
; i
++ )
1911 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1913 return wxGRID_VALUE_STRING
;
1916 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1917 const wxString
& typeName
)
1919 return typeName
== wxGRID_VALUE_STRING
;
1922 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1924 return CanGetValueAs(row
, col
, typeName
);
1927 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1932 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1937 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1942 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1943 long WXUNUSED(value
) )
1947 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1948 double WXUNUSED(value
) )
1952 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1953 bool WXUNUSED(value
) )
1958 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1959 const wxString
& WXUNUSED(typeName
) )
1964 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1965 const wxString
& WXUNUSED(typeName
),
1966 void* WXUNUSED(value
) )
1971 //////////////////////////////////////////////////////////////////////
1973 // Message class for the grid table to send requests and notifications
1977 wxGridTableMessage::wxGridTableMessage()
1979 m_table
= (wxGridTableBase
*) NULL
;
1985 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1986 int commandInt1
, int commandInt2
)
1990 m_comInt1
= commandInt1
;
1991 m_comInt2
= commandInt2
;
1996 //////////////////////////////////////////////////////////////////////
1998 // A basic grid table for string data. An object of this class will
1999 // created by wxGrid if you don't specify an alternative table class.
2002 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2004 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2006 wxGridStringTable::wxGridStringTable()
2011 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2016 m_data
.Alloc( numRows
);
2019 sa
.Alloc( numCols
);
2020 for ( col
= 0; col
< numCols
; col
++ )
2022 sa
.Add( wxEmptyString
);
2025 for ( row
= 0; row
< numRows
; row
++ )
2031 wxGridStringTable::~wxGridStringTable()
2035 long wxGridStringTable::GetNumberRows()
2037 return m_data
.GetCount();
2040 long wxGridStringTable::GetNumberCols()
2042 if ( m_data
.GetCount() > 0 )
2043 return m_data
[0].GetCount();
2048 wxString
wxGridStringTable::GetValue( int row
, int col
)
2050 // TODO: bounds checking
2052 return m_data
[row
][col
];
2055 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2057 // TODO: bounds checking
2059 m_data
[row
][col
] = value
;
2062 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2064 // TODO: bounds checking
2066 return (m_data
[row
][col
] == wxEmptyString
);
2070 void wxGridStringTable::Clear()
2073 int numRows
, numCols
;
2075 numRows
= m_data
.GetCount();
2078 numCols
= m_data
[0].GetCount();
2080 for ( row
= 0; row
< numRows
; row
++ )
2082 for ( col
= 0; col
< numCols
; col
++ )
2084 m_data
[row
][col
] = wxEmptyString
;
2091 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2095 size_t curNumRows
= m_data
.GetCount();
2096 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2098 if ( pos
>= curNumRows
)
2100 return AppendRows( numRows
);
2104 sa
.Alloc( curNumCols
);
2105 for ( col
= 0; col
< curNumCols
; col
++ )
2107 sa
.Add( wxEmptyString
);
2110 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2112 m_data
.Insert( sa
, row
);
2114 UpdateAttrRows( pos
, numRows
);
2117 wxGridTableMessage
msg( this,
2118 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2122 GetView()->ProcessTableMessage( msg
);
2128 bool wxGridStringTable::AppendRows( size_t numRows
)
2132 size_t curNumRows
= m_data
.GetCount();
2133 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2136 if ( curNumCols
> 0 )
2138 sa
.Alloc( curNumCols
);
2139 for ( col
= 0; col
< curNumCols
; col
++ )
2141 sa
.Add( wxEmptyString
);
2145 for ( row
= 0; row
< numRows
; row
++ )
2152 wxGridTableMessage
msg( this,
2153 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2156 GetView()->ProcessTableMessage( msg
);
2162 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2166 size_t curNumRows
= m_data
.GetCount();
2168 if ( pos
>= curNumRows
)
2171 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2172 "Pos value is invalid for present table with %d rows",
2173 pos
, numRows
, curNumRows
);
2174 wxFAIL_MSG( wxT(errmsg
) );
2178 if ( numRows
> curNumRows
- pos
)
2180 numRows
= curNumRows
- pos
;
2183 if ( numRows
>= curNumRows
)
2185 m_data
.Empty(); // don't release memory just yet
2189 for ( n
= 0; n
< numRows
; n
++ )
2191 m_data
.Remove( pos
);
2194 UpdateAttrRows( pos
, -((int)numRows
) );
2197 wxGridTableMessage
msg( this,
2198 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2202 GetView()->ProcessTableMessage( msg
);
2208 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2212 size_t curNumRows
= m_data
.GetCount();
2213 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2215 if ( pos
>= curNumCols
)
2217 return AppendCols( numCols
);
2220 for ( row
= 0; row
< curNumRows
; row
++ )
2222 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2224 m_data
[row
].Insert( wxEmptyString
, col
);
2227 UpdateAttrCols( pos
, numCols
);
2230 wxGridTableMessage
msg( this,
2231 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2235 GetView()->ProcessTableMessage( msg
);
2241 bool wxGridStringTable::AppendCols( size_t numCols
)
2245 size_t curNumRows
= m_data
.GetCount();
2248 // TODO: something better than this ?
2250 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2251 "Call AppendRows() first") );
2255 for ( row
= 0; row
< curNumRows
; row
++ )
2257 for ( n
= 0; n
< numCols
; n
++ )
2259 m_data
[row
].Add( wxEmptyString
);
2265 wxGridTableMessage
msg( this,
2266 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2269 GetView()->ProcessTableMessage( msg
);
2275 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2279 size_t curNumRows
= m_data
.GetCount();
2280 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2282 if ( pos
>= curNumCols
)
2285 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2286 "Pos value is invalid for present table with %d cols",
2287 pos
, numCols
, curNumCols
);
2288 wxFAIL_MSG( wxT( errmsg
) );
2292 if ( numCols
> curNumCols
- pos
)
2294 numCols
= curNumCols
- pos
;
2297 for ( row
= 0; row
< curNumRows
; row
++ )
2299 if ( numCols
>= curNumCols
)
2301 m_data
[row
].Clear();
2305 for ( n
= 0; n
< numCols
; n
++ )
2307 m_data
[row
].Remove( pos
);
2311 UpdateAttrCols( pos
, -((int)numCols
) );
2314 wxGridTableMessage
msg( this,
2315 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2319 GetView()->ProcessTableMessage( msg
);
2325 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2327 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2329 // using default label
2331 return wxGridTableBase::GetRowLabelValue( row
);
2335 return m_rowLabels
[ row
];
2339 wxString
wxGridStringTable::GetColLabelValue( int col
)
2341 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2343 // using default label
2345 return wxGridTableBase::GetColLabelValue( col
);
2349 return m_colLabels
[ col
];
2353 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2355 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2357 int n
= m_rowLabels
.GetCount();
2359 for ( i
= n
; i
<= row
; i
++ )
2361 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2365 m_rowLabels
[row
] = value
;
2368 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2370 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2372 int n
= m_colLabels
.GetCount();
2374 for ( i
= n
; i
<= col
; i
++ )
2376 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2380 m_colLabels
[col
] = value
;
2385 //////////////////////////////////////////////////////////////////////
2386 //////////////////////////////////////////////////////////////////////
2388 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2390 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2391 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2392 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2393 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2396 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2398 const wxPoint
&pos
, const wxSize
&size
)
2399 : wxWindow( parent
, id
, pos
, size
)
2404 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2408 // NO - don't do this because it will set both the x and y origin
2409 // coords to match the parent scrolled window and we just want to
2410 // set the y coord - MB
2412 // m_owner->PrepareDC( dc );
2415 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2416 dc
.SetDeviceOrigin( 0, -y
);
2418 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2419 m_owner
->DrawRowLabels( dc
);
2423 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2425 m_owner
->ProcessRowLabelMouseEvent( event
);
2429 // This seems to be required for wxMotif otherwise the mouse
2430 // cursor must be in the cell edit control to get key events
2432 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2434 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2439 //////////////////////////////////////////////////////////////////////
2441 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2443 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2444 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2445 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2446 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2449 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2451 const wxPoint
&pos
, const wxSize
&size
)
2452 : wxWindow( parent
, id
, pos
, size
)
2457 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2461 // NO - don't do this because it will set both the x and y origin
2462 // coords to match the parent scrolled window and we just want to
2463 // set the x coord - MB
2465 // m_owner->PrepareDC( dc );
2468 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2469 dc
.SetDeviceOrigin( -x
, 0 );
2471 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2472 m_owner
->DrawColLabels( dc
);
2476 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2478 m_owner
->ProcessColLabelMouseEvent( event
);
2482 // This seems to be required for wxMotif otherwise the mouse
2483 // cursor must be in the cell edit control to get key events
2485 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2487 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2492 //////////////////////////////////////////////////////////////////////
2494 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2496 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2497 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2498 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2499 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2502 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2504 const wxPoint
&pos
, const wxSize
&size
)
2505 : wxWindow( parent
, id
, pos
, size
)
2510 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2514 int client_height
= 0;
2515 int client_width
= 0;
2516 GetClientSize( &client_width
, &client_height
);
2518 dc
.SetPen( *wxBLACK_PEN
);
2519 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2520 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2522 dc
.SetPen( *wxWHITE_PEN
);
2523 dc
.DrawLine( 0, 0, client_width
, 0 );
2524 dc
.DrawLine( 0, 0, 0, client_height
);
2528 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2530 m_owner
->ProcessCornerLabelMouseEvent( event
);
2534 // This seems to be required for wxMotif otherwise the mouse
2535 // cursor must be in the cell edit control to get key events
2537 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2539 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2544 //////////////////////////////////////////////////////////////////////
2546 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2548 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2549 EVT_PAINT( wxGridWindow::OnPaint
)
2550 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2551 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2552 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2555 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2556 wxGridRowLabelWindow
*rowLblWin
,
2557 wxGridColLabelWindow
*colLblWin
,
2558 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2559 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
2562 m_rowLabelWin
= rowLblWin
;
2563 m_colLabelWin
= colLblWin
;
2564 SetBackgroundColour( "WHITE" );
2568 wxGridWindow::~wxGridWindow()
2573 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2575 wxPaintDC
dc( this );
2576 m_owner
->PrepareDC( dc
);
2577 wxRegion reg
= GetUpdateRegion();
2578 m_owner
->CalcCellsExposed( reg
);
2579 m_owner
->DrawGridCellArea( dc
);
2580 m_owner
->DrawGridSpace( dc
);
2581 #if WXGRID_DRAW_LINES
2582 m_owner
->DrawAllGridLines( dc
, reg
);
2584 m_owner
->DrawHighlight( dc
);
2588 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2590 wxPanel::ScrollWindow( dx
, dy
, rect
);
2591 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2592 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2596 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2598 m_owner
->ProcessGridCellMouseEvent( event
);
2602 // This seems to be required for wxMotif otherwise the mouse
2603 // cursor must be in the cell edit control to get key events
2605 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2607 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2611 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2616 //////////////////////////////////////////////////////////////////////
2619 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2621 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2622 EVT_PAINT( wxGrid::OnPaint
)
2623 EVT_SIZE( wxGrid::OnSize
)
2624 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2625 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2628 wxGrid::wxGrid( wxWindow
*parent
,
2633 const wxString
& name
)
2634 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2635 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2644 m_defaultCellAttr
->SafeDecRef();
2646 #ifdef DEBUG_ATTR_CACHE
2647 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2648 wxPrintf(_T("wxGrid attribute cache statistics: "
2649 "total: %u, hits: %u (%u%%)\n"),
2650 total
, gs_nAttrCacheHits
,
2651 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2657 delete m_typeRegistry
;
2662 // ----- internal init and update functions
2665 void wxGrid::Create()
2667 m_created
= FALSE
; // set to TRUE by CreateGrid
2668 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2670 m_table
= (wxGridTableBase
*) NULL
;
2673 m_cellEditCtrlEnabled
= FALSE
;
2675 m_defaultCellAttr
= new wxGridCellAttr
;
2676 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2678 // Set default cell attributes
2679 m_defaultCellAttr
->SetFont(GetFont());
2680 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2681 m_defaultCellAttr
->SetTextColour(
2682 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2683 m_defaultCellAttr
->SetBackgroundColour(
2684 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2685 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2686 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2691 m_currentCellCoords
= wxGridNoCellCoords
;
2693 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2694 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2696 // data type registration: register all standard data types
2697 // TODO: may be allow the app to selectively disable some of them?
2698 m_typeRegistry
= new wxGridTypeRegistry
;
2699 RegisterDataType(wxGRID_VALUE_STRING
,
2700 new wxGridCellStringRenderer
,
2701 new wxGridCellTextEditor
);
2702 RegisterDataType(wxGRID_VALUE_BOOL
,
2703 new wxGridCellBoolRenderer
,
2704 new wxGridCellBoolEditor
);
2705 RegisterDataType(wxGRID_VALUE_NUMBER
,
2706 new wxGridCellNumberRenderer
,
2707 new wxGridCellNumberEditor
);
2709 // subwindow components that make up the wxGrid
2710 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2715 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2720 m_colLabelWin
= new wxGridColLabelWindow( this,
2725 m_gridWin
= new wxGridWindow( this,
2732 SetTargetWindow( m_gridWin
);
2736 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2740 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2745 m_numRows
= numRows
;
2746 m_numCols
= numCols
;
2748 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2749 m_table
->SetView( this );
2758 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2762 // RD: Actually, this should probably be allowed. I think it would be
2763 // nice to be able to switch multiple Tables in and out of a single
2764 // View at runtime. Is there anything in the implmentation that would
2767 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2772 m_numRows
= table
->GetNumberRows();
2773 m_numCols
= table
->GetNumberCols();
2776 m_table
->SetView( this );
2789 if ( m_numRows
<= 0 )
2790 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2792 if ( m_numCols
<= 0 )
2793 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2795 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2796 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2798 if ( m_rowLabelWin
)
2800 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2804 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2807 m_labelTextColour
= wxColour( _T("BLACK") );
2810 m_attrCache
.row
= -1;
2812 // TODO: something better than this ?
2814 m_labelFont
= this->GetFont();
2815 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2817 m_rowLabelHorizAlign
= wxLEFT
;
2818 m_rowLabelVertAlign
= wxCENTRE
;
2820 m_colLabelHorizAlign
= wxCENTRE
;
2821 m_colLabelVertAlign
= wxTOP
;
2823 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2824 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2826 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2827 m_defaultRowHeight
+= 8;
2829 m_defaultRowHeight
+= 4;
2832 m_gridLineColour
= wxColour( 128, 128, 255 );
2833 m_gridLinesEnabled
= TRUE
;
2835 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2836 m_winCapture
= (wxWindow
*)NULL
;
2837 m_canDragRowSize
= TRUE
;
2838 m_canDragColSize
= TRUE
;
2839 m_canDragGridSize
= TRUE
;
2841 m_dragRowOrCol
= -1;
2842 m_isDragging
= FALSE
;
2843 m_startDragPos
= wxDefaultPosition
;
2845 m_waitForSlowClick
= FALSE
;
2847 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2848 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2850 m_currentCellCoords
= wxGridNoCellCoords
;
2852 m_selectedTopLeft
= wxGridNoCellCoords
;
2853 m_selectedBottomRight
= wxGridNoCellCoords
;
2854 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2855 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2857 m_editable
= TRUE
; // default for whole grid
2859 m_inOnKeyDown
= FALSE
;
2863 // ----------------------------------------------------------------------------
2864 // the idea is to call these functions only when necessary because they create
2865 // quite big arrays which eat memory mostly unnecessary - in particular, if
2866 // default widths/heights are used for all rows/columns, we may not use these
2869 // with some extra code, it should be possible to only store the
2870 // widths/heights different from default ones but this will be done later...
2871 // ----------------------------------------------------------------------------
2873 void wxGrid::InitRowHeights()
2875 m_rowHeights
.Empty();
2876 m_rowBottoms
.Empty();
2878 m_rowHeights
.Alloc( m_numRows
);
2879 m_rowBottoms
.Alloc( m_numRows
);
2882 for ( int i
= 0; i
< m_numRows
; i
++ )
2884 m_rowHeights
.Add( m_defaultRowHeight
);
2885 rowBottom
+= m_defaultRowHeight
;
2886 m_rowBottoms
.Add( rowBottom
);
2890 void wxGrid::InitColWidths()
2892 m_colWidths
.Empty();
2893 m_colRights
.Empty();
2895 m_colWidths
.Alloc( m_numCols
);
2896 m_colRights
.Alloc( m_numCols
);
2898 for ( int i
= 0; i
< m_numCols
; i
++ )
2900 m_colWidths
.Add( m_defaultColWidth
);
2901 colRight
+= m_defaultColWidth
;
2902 m_colRights
.Add( colRight
);
2906 int wxGrid::GetColWidth(int col
) const
2908 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2911 int wxGrid::GetColLeft(int col
) const
2913 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2914 : m_colRights
[col
] - m_colWidths
[col
];
2917 int wxGrid::GetColRight(int col
) const
2919 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
2923 int wxGrid::GetRowHeight(int row
) const
2925 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2928 int wxGrid::GetRowTop(int row
) const
2930 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2931 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2934 int wxGrid::GetRowBottom(int row
) const
2936 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2937 : m_rowBottoms
[row
];
2940 void wxGrid::CalcDimensions()
2943 GetClientSize( &cw
, &ch
);
2945 if ( m_numRows
> 0 && m_numCols
> 0 )
2947 int right
= GetColRight( m_numCols
-1 ) + 50;
2948 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
2950 // TODO: restore the scroll position that we had before sizing
2953 GetViewStart( &x
, &y
);
2954 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2955 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2961 void wxGrid::CalcWindowSizes()
2964 GetClientSize( &cw
, &ch
);
2966 if ( m_cornerLabelWin
->IsShown() )
2967 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2969 if ( m_colLabelWin
->IsShown() )
2970 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2972 if ( m_rowLabelWin
->IsShown() )
2973 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2975 if ( m_gridWin
->IsShown() )
2976 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2980 // this is called when the grid table sends a message to say that it
2981 // has been redimensioned
2983 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2987 // if we were using the default widths/heights so far, we must change them
2989 if ( m_colWidths
.IsEmpty() )
2994 if ( m_rowHeights
.IsEmpty() )
2999 switch ( msg
.GetId() )
3001 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3003 size_t pos
= msg
.GetCommandInt();
3004 int numRows
= msg
.GetCommandInt2();
3005 for ( i
= 0; i
< numRows
; i
++ )
3007 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3008 m_rowBottoms
.Insert( 0, pos
);
3010 m_numRows
+= numRows
;
3013 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3015 for ( i
= pos
; i
< m_numRows
; i
++ )
3017 bottom
+= m_rowHeights
[i
];
3018 m_rowBottoms
[i
] = bottom
;
3024 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3026 int numRows
= msg
.GetCommandInt();
3027 for ( i
= 0; i
< numRows
; i
++ )
3029 m_rowHeights
.Add( m_defaultRowHeight
);
3030 m_rowBottoms
.Add( 0 );
3033 int oldNumRows
= m_numRows
;
3034 m_numRows
+= numRows
;
3037 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3039 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3041 bottom
+= m_rowHeights
[i
];
3042 m_rowBottoms
[i
] = bottom
;
3048 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3050 size_t pos
= msg
.GetCommandInt();
3051 int numRows
= msg
.GetCommandInt2();
3052 for ( i
= 0; i
< numRows
; i
++ )
3054 m_rowHeights
.Remove( pos
);
3055 m_rowBottoms
.Remove( pos
);
3057 m_numRows
-= numRows
;
3062 m_colWidths
.Clear();
3063 m_colRights
.Clear();
3064 m_currentCellCoords
= wxGridNoCellCoords
;
3068 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3069 m_currentCellCoords
.Set( 0, 0 );
3072 for ( i
= 0; i
< m_numRows
; i
++ )
3074 h
+= m_rowHeights
[i
];
3075 m_rowBottoms
[i
] = h
;
3083 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3085 size_t pos
= msg
.GetCommandInt();
3086 int numCols
= msg
.GetCommandInt2();
3087 for ( i
= 0; i
< numCols
; i
++ )
3089 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3090 m_colRights
.Insert( 0, pos
);
3092 m_numCols
+= numCols
;
3095 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3097 for ( i
= pos
; i
< m_numCols
; i
++ )
3099 right
+= m_colWidths
[i
];
3100 m_colRights
[i
] = right
;
3106 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3108 int numCols
= msg
.GetCommandInt();
3109 for ( i
= 0; i
< numCols
; i
++ )
3111 m_colWidths
.Add( m_defaultColWidth
);
3112 m_colRights
.Add( 0 );
3115 int oldNumCols
= m_numCols
;
3116 m_numCols
+= numCols
;
3119 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3121 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3123 right
+= m_colWidths
[i
];
3124 m_colRights
[i
] = right
;
3130 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3132 size_t pos
= msg
.GetCommandInt();
3133 int numCols
= msg
.GetCommandInt2();
3134 for ( i
= 0; i
< numCols
; i
++ )
3136 m_colWidths
.Remove( pos
);
3137 m_colRights
.Remove( pos
);
3139 m_numCols
-= numCols
;
3143 #if 0 // leave the row alone here so that AppendCols will work subsequently
3145 m_rowHeights
.Clear();
3146 m_rowBottoms
.Clear();
3148 m_currentCellCoords
= wxGridNoCellCoords
;
3152 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3153 m_currentCellCoords
.Set( 0, 0 );
3156 for ( i
= 0; i
< m_numCols
; i
++ )
3158 w
+= m_colWidths
[i
];
3171 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3173 wxRegionIterator
iter( reg
);
3176 m_rowLabelsExposed
.Empty();
3183 // TODO: remove this when we can...
3184 // There is a bug in wxMotif that gives garbage update
3185 // rectangles if you jump-scroll a long way by clicking the
3186 // scrollbar with middle button. This is a work-around
3188 #if defined(__WXMOTIF__)
3190 m_gridWin
->GetClientSize( &cw
, &ch
);
3191 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3192 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3195 // logical bounds of update region
3198 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3199 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3201 // find the row labels within these bounds
3204 for ( row
= 0; row
< m_numRows
; row
++ )
3206 if ( GetRowBottom(row
) < top
)
3209 if ( GetRowTop(row
) > bottom
)
3212 m_rowLabelsExposed
.Add( row
);
3220 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3222 wxRegionIterator
iter( reg
);
3225 m_colLabelsExposed
.Empty();
3232 // TODO: remove this when we can...
3233 // There is a bug in wxMotif that gives garbage update
3234 // rectangles if you jump-scroll a long way by clicking the
3235 // scrollbar with middle button. This is a work-around
3237 #if defined(__WXMOTIF__)
3239 m_gridWin
->GetClientSize( &cw
, &ch
);
3240 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3241 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3244 // logical bounds of update region
3247 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3248 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3250 // find the cells within these bounds
3253 for ( col
= 0; col
< m_numCols
; col
++ )
3255 if ( GetColRight(col
) < left
)
3258 if ( GetColLeft(col
) > right
)
3261 m_colLabelsExposed
.Add( col
);
3269 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3271 wxRegionIterator
iter( reg
);
3274 m_cellsExposed
.Empty();
3275 m_rowsExposed
.Empty();
3276 m_colsExposed
.Empty();
3278 int left
, top
, right
, bottom
;
3283 // TODO: remove this when we can...
3284 // There is a bug in wxMotif that gives garbage update
3285 // rectangles if you jump-scroll a long way by clicking the
3286 // scrollbar with middle button. This is a work-around
3288 #if defined(__WXMOTIF__)
3290 m_gridWin
->GetClientSize( &cw
, &ch
);
3291 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3292 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3293 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3294 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3297 // logical bounds of update region
3299 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3300 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3302 // find the cells within these bounds
3305 for ( row
= 0; row
< m_numRows
; row
++ )
3307 if ( GetRowBottom(row
) <= top
)
3310 if ( GetRowTop(row
) > bottom
)
3313 m_rowsExposed
.Add( row
);
3315 for ( col
= 0; col
< m_numCols
; col
++ )
3317 if ( GetColRight(col
) <= left
)
3320 if ( GetColLeft(col
) > right
)
3323 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3324 m_colsExposed
.Add( col
);
3325 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3334 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3337 wxPoint
pos( event
.GetPosition() );
3338 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3340 if ( event
.Dragging() )
3342 m_isDragging
= TRUE
;
3344 if ( event
.LeftIsDown() )
3346 switch( m_cursorMode
)
3348 case WXGRID_CURSOR_RESIZE_ROW
:
3350 int cw
, ch
, left
, dummy
;
3351 m_gridWin
->GetClientSize( &cw
, &ch
);
3352 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3354 wxClientDC
dc( m_gridWin
);
3356 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3357 dc
.SetLogicalFunction(wxINVERT
);
3358 if ( m_dragLastPos
>= 0 )
3360 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3362 dc
.DrawLine( left
, y
, left
+cw
, y
);
3367 case WXGRID_CURSOR_SELECT_ROW
:
3368 if ( (row
= YToRow( y
)) >= 0 &&
3369 !IsInSelection( row
, 0 ) )
3371 SelectRow( row
, TRUE
);
3374 // default label to suppress warnings about "enumeration value
3375 // 'xxx' not handled in switch
3383 m_isDragging
= FALSE
;
3386 // ------------ Entering or leaving the window
3388 if ( event
.Entering() || event
.Leaving() )
3390 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3394 // ------------ Left button pressed
3396 else if ( event
.LeftDown() )
3398 // don't send a label click event for a hit on the
3399 // edge of the row label - this is probably the user
3400 // wanting to resize the row
3402 if ( YToEdgeOfRow(y
) < 0 )
3406 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3408 SelectRow( row
, event
.ShiftDown() );
3409 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3414 // starting to drag-resize a row
3416 if ( CanDragRowSize() )
3417 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3422 // ------------ Left double click
3424 else if (event
.LeftDClick() )
3426 if ( YToEdgeOfRow(y
) < 0 )
3429 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3434 // ------------ Left button released
3436 else if ( event
.LeftUp() )
3438 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3440 DoEndDragResizeRow();
3442 // Note: we are ending the event *after* doing
3443 // default processing in this case
3445 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3448 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3453 // ------------ Right button down
3455 else if ( event
.RightDown() )
3458 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3460 // no default action at the moment
3465 // ------------ Right double click
3467 else if ( event
.RightDClick() )
3470 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3472 // no default action at the moment
3477 // ------------ No buttons down and mouse moving
3479 else if ( event
.Moving() )
3481 m_dragRowOrCol
= YToEdgeOfRow( y
);
3482 if ( m_dragRowOrCol
>= 0 )
3484 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3486 // don't capture the mouse yet
3487 if ( CanDragRowSize() )
3488 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3491 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3493 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3499 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3502 wxPoint
pos( event
.GetPosition() );
3503 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3505 if ( event
.Dragging() )
3507 m_isDragging
= TRUE
;
3509 if ( event
.LeftIsDown() )
3511 switch( m_cursorMode
)
3513 case WXGRID_CURSOR_RESIZE_COL
:
3515 int cw
, ch
, dummy
, top
;
3516 m_gridWin
->GetClientSize( &cw
, &ch
);
3517 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3519 wxClientDC
dc( m_gridWin
);
3522 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3523 GetColMinimalWidth(m_dragRowOrCol
));
3524 dc
.SetLogicalFunction(wxINVERT
);
3525 if ( m_dragLastPos
>= 0 )
3527 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3529 dc
.DrawLine( x
, top
, x
, top
+ch
);
3534 case WXGRID_CURSOR_SELECT_COL
:
3535 if ( (col
= XToCol( x
)) >= 0 &&
3536 !IsInSelection( 0, col
) )
3538 SelectCol( col
, TRUE
);
3541 // default label to suppress warnings about "enumeration value
3542 // 'xxx' not handled in switch
3550 m_isDragging
= FALSE
;
3553 // ------------ Entering or leaving the window
3555 if ( event
.Entering() || event
.Leaving() )
3557 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3561 // ------------ Left button pressed
3563 else if ( event
.LeftDown() )
3565 // don't send a label click event for a hit on the
3566 // edge of the col label - this is probably the user
3567 // wanting to resize the col
3569 if ( XToEdgeOfCol(x
) < 0 )
3573 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3575 SelectCol( col
, event
.ShiftDown() );
3576 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3581 // starting to drag-resize a col
3583 if ( CanDragColSize() )
3584 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3589 // ------------ Left double click
3591 if ( event
.LeftDClick() )
3593 if ( XToEdgeOfCol(x
) < 0 )
3596 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3601 // ------------ Left button released
3603 else if ( event
.LeftUp() )
3605 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3607 DoEndDragResizeCol();
3609 // Note: we are ending the event *after* doing
3610 // default processing in this case
3612 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3615 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3620 // ------------ Right button down
3622 else if ( event
.RightDown() )
3625 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3627 // no default action at the moment
3632 // ------------ Right double click
3634 else if ( event
.RightDClick() )
3637 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3639 // no default action at the moment
3644 // ------------ No buttons down and mouse moving
3646 else if ( event
.Moving() )
3648 m_dragRowOrCol
= XToEdgeOfCol( x
);
3649 if ( m_dragRowOrCol
>= 0 )
3651 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3653 // don't capture the cursor yet
3654 if ( CanDragColSize() )
3655 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3658 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3660 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3666 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3668 if ( event
.LeftDown() )
3670 // indicate corner label by having both row and
3673 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3679 else if ( event
.LeftDClick() )
3681 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3684 else if ( event
.RightDown() )
3686 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3688 // no default action at the moment
3692 else if ( event
.RightDClick() )
3694 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3696 // no default action at the moment
3701 void wxGrid::ChangeCursorMode(CursorMode mode
,
3706 static const wxChar
*cursorModes
[] =
3715 wxLogTrace(_T("grid"),
3716 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3717 win
== m_colLabelWin
? _T("colLabelWin")
3718 : win
? _T("rowLabelWin")
3720 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3721 #endif // __WXDEBUG__
3723 if ( mode
== m_cursorMode
)
3728 // by default use the grid itself
3734 m_winCapture
->ReleaseMouse();
3735 m_winCapture
= (wxWindow
*)NULL
;
3738 m_cursorMode
= mode
;
3740 switch ( m_cursorMode
)
3742 case WXGRID_CURSOR_RESIZE_ROW
:
3743 win
->SetCursor( m_rowResizeCursor
);
3746 case WXGRID_CURSOR_RESIZE_COL
:
3747 win
->SetCursor( m_colResizeCursor
);
3751 win
->SetCursor( *wxSTANDARD_CURSOR
);
3754 // we need to capture mouse when resizing
3755 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3756 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3758 if ( captureMouse
&& resize
)
3760 win
->CaptureMouse();
3765 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3768 wxPoint
pos( event
.GetPosition() );
3769 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3771 wxGridCellCoords coords
;
3772 XYToCell( x
, y
, coords
);
3774 if ( event
.Dragging() )
3776 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3778 // Don't start doing anything until the mouse has been drug at
3779 // least 3 pixels in any direction...
3782 if (m_startDragPos
== wxDefaultPosition
)
3784 m_startDragPos
= pos
;
3787 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3791 m_isDragging
= TRUE
;
3792 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3794 // Hide the edit control, so it
3795 // won't interfer with drag-shrinking.
3796 if ( IsCellEditControlEnabled() )
3797 HideCellEditControl();
3799 // Have we captured the mouse yet?
3802 m_winCapture
= m_gridWin
;
3803 m_winCapture
->CaptureMouse();
3806 if ( coords
!= wxGridNoCellCoords
)
3808 if ( !IsSelection() )
3810 SelectBlock( coords
, coords
);
3814 SelectBlock( m_currentCellCoords
, coords
);
3817 if (! IsVisible(coords
))
3819 MakeCellVisible(coords
);
3820 // TODO: need to introduce a delay or something here. The
3821 // scrolling is way to fast, at least on MSW.
3825 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3827 int cw
, ch
, left
, dummy
;
3828 m_gridWin
->GetClientSize( &cw
, &ch
);
3829 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3831 wxClientDC
dc( m_gridWin
);
3833 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3834 dc
.SetLogicalFunction(wxINVERT
);
3835 if ( m_dragLastPos
>= 0 )
3837 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3839 dc
.DrawLine( left
, y
, left
+cw
, y
);
3842 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3844 int cw
, ch
, dummy
, top
;
3845 m_gridWin
->GetClientSize( &cw
, &ch
);
3846 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3848 wxClientDC
dc( m_gridWin
);
3850 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3851 GetColMinimalWidth(m_dragRowOrCol
) );
3852 dc
.SetLogicalFunction(wxINVERT
);
3853 if ( m_dragLastPos
>= 0 )
3855 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3857 dc
.DrawLine( x
, top
, x
, top
+ch
);
3864 m_isDragging
= FALSE
;
3865 m_startDragPos
= wxDefaultPosition
;
3867 // if ( coords == wxGridNoCellCoords && m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3869 // ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3872 // if ( coords != wxGridNoCellCoords )
3874 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3875 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3878 if ( event
.Entering() || event
.Leaving() )
3880 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3881 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3886 // ------------ Left button pressed
3888 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
3890 DisableCellEditControl();
3891 if ( event
.ShiftDown() )
3893 SelectBlock( m_currentCellCoords
, coords
);
3895 else if ( XToEdgeOfCol(x
) < 0 &&
3896 YToEdgeOfRow(y
) < 0 )
3898 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3903 MakeCellVisible( coords
);
3905 // if this is the second click on this cell then start
3907 if ( m_waitForSlowClick
&&
3908 (coords
== m_currentCellCoords
) &&
3909 CanEnableCellControl())
3911 EnableCellEditControl();
3913 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3914 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
3917 m_waitForSlowClick
= FALSE
;
3921 SetCurrentCell( coords
);
3922 m_waitForSlowClick
= TRUE
;
3929 // ------------ Left double click
3931 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
3933 DisableCellEditControl();
3934 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3936 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3944 // ------------ Left button released
3946 else if ( event
.LeftUp() )
3948 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3950 if ( IsSelection() )
3954 m_winCapture
->ReleaseMouse();
3955 m_winCapture
= NULL
;
3957 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3960 // Show the edit control, if it has been hidden for
3962 ShowCellEditControl();
3964 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3966 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3967 DoEndDragResizeRow();
3969 // Note: we are ending the event *after* doing
3970 // default processing in this case
3972 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3974 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3976 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3977 DoEndDragResizeCol();
3979 // Note: we are ending the event *after* doing
3980 // default processing in this case
3982 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3989 // ------------ Right button down
3991 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
3993 DisableCellEditControl();
3994 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
3999 // no default action at the moment
4004 // ------------ Right double click
4006 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4008 DisableCellEditControl();
4009 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4014 // no default action at the moment
4018 // ------------ Moving and no button action
4020 else if ( event
.Moving() && !event
.IsButton() )
4022 int dragRow
= YToEdgeOfRow( y
);
4023 int dragCol
= XToEdgeOfCol( x
);
4025 // Dragging on the corner of a cell to resize in both
4026 // directions is not implemented yet...
4028 if ( dragRow
>= 0 && dragCol
>= 0 )
4030 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4036 m_dragRowOrCol
= dragRow
;
4038 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4040 if ( CanDragRowSize() && CanDragGridSize() )
4041 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4049 m_dragRowOrCol
= dragCol
;
4051 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4053 if ( CanDragColSize() && CanDragGridSize() )
4054 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4060 // Neither on a row or col edge
4062 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4064 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4070 void wxGrid::DoEndDragResizeRow()
4072 if ( m_dragLastPos
>= 0 )
4074 // erase the last line and resize the row
4076 int cw
, ch
, left
, dummy
;
4077 m_gridWin
->GetClientSize( &cw
, &ch
);
4078 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4080 wxClientDC
dc( m_gridWin
);
4082 dc
.SetLogicalFunction( wxINVERT
);
4083 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4084 HideCellEditControl();
4086 int rowTop
= GetRowTop(m_dragRowOrCol
);
4087 SetRowSize( m_dragRowOrCol
,
4088 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4090 if ( !GetBatchCount() )
4092 // Only needed to get the correct rect.y:
4093 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4095 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4096 rect
.width
= m_rowLabelWidth
;
4097 rect
.height
= ch
- rect
.y
;
4098 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4100 m_gridWin
->Refresh( FALSE
, &rect
);
4103 ShowCellEditControl();
4108 void wxGrid::DoEndDragResizeCol()
4110 if ( m_dragLastPos
>= 0 )
4112 // erase the last line and resize the col
4114 int cw
, ch
, dummy
, top
;
4115 m_gridWin
->GetClientSize( &cw
, &ch
);
4116 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4118 wxClientDC
dc( m_gridWin
);
4120 dc
.SetLogicalFunction( wxINVERT
);
4121 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4122 HideCellEditControl();
4124 int colLeft
= GetColLeft(m_dragRowOrCol
);
4125 SetColSize( m_dragRowOrCol
,
4126 wxMax( m_dragLastPos
- colLeft
,
4127 GetColMinimalWidth(m_dragRowOrCol
) ) );
4129 if ( !GetBatchCount() )
4131 // Only needed to get the correct rect.x:
4132 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4134 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4135 rect
.width
= cw
- rect
.x
;
4136 rect
.height
= m_colLabelHeight
;
4137 m_colLabelWin
->Refresh( TRUE
, &rect
);
4139 m_gridWin
->Refresh( FALSE
, &rect
);
4142 ShowCellEditControl();
4149 // ------ interaction with data model
4151 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4153 switch ( msg
.GetId() )
4155 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4156 return GetModelValues();
4158 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4159 return SetModelValues();
4161 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4162 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4163 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4164 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4165 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4166 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4167 return Redimension( msg
);
4176 // The behaviour of this function depends on the grid table class
4177 // Clear() function. For the default wxGridStringTable class the
4178 // behavious is to replace all cell contents with wxEmptyString but
4179 // not to change the number of rows or cols.
4181 void wxGrid::ClearGrid()
4185 if (IsCellEditControlEnabled())
4186 DisableCellEditControl();
4189 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4194 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4196 // TODO: something with updateLabels flag
4200 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4206 if (IsCellEditControlEnabled())
4207 DisableCellEditControl();
4209 bool ok
= m_table
->InsertRows( pos
, numRows
);
4211 // the table will have sent the results of the insert row
4212 // operation to this view object as a grid table message
4216 if ( m_numCols
== 0 )
4218 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4220 // TODO: perhaps instead of appending the default number of cols
4221 // we should remember what the last non-zero number of cols was ?
4225 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4227 // if we have just inserted cols into an empty grid the current
4228 // cell will be undefined...
4230 SetCurrentCell( 0, 0 );
4234 if ( !GetBatchCount() ) Refresh();
4246 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4248 // TODO: something with updateLabels flag
4252 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4256 if ( m_table
&& m_table
->AppendRows( numRows
) )
4258 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4260 // if we have just inserted cols into an empty grid the current
4261 // cell will be undefined...
4263 SetCurrentCell( 0, 0 );
4266 // the table will have sent the results of the append row
4267 // operation to this view object as a grid table message
4270 if ( !GetBatchCount() ) Refresh();
4280 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4282 // TODO: something with updateLabels flag
4286 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4292 if (IsCellEditControlEnabled())
4293 DisableCellEditControl();
4295 if (m_table
->DeleteRows( pos
, numRows
))
4298 // the table will have sent the results of the delete row
4299 // operation to this view object as a grid table message
4302 if ( !GetBatchCount() ) Refresh();
4310 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4312 // TODO: something with updateLabels flag
4316 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4322 if (IsCellEditControlEnabled())
4323 DisableCellEditControl();
4325 bool ok
= m_table
->InsertCols( pos
, numCols
);
4327 // the table will have sent the results of the insert col
4328 // operation to this view object as a grid table message
4332 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4334 // if we have just inserted cols into an empty grid the current
4335 // cell will be undefined...
4337 SetCurrentCell( 0, 0 );
4341 if ( !GetBatchCount() ) Refresh();
4353 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4355 // TODO: something with updateLabels flag
4359 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4363 if ( m_table
&& m_table
->AppendCols( numCols
) )
4365 // the table will have sent the results of the append col
4366 // operation to this view object as a grid table message
4368 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4370 // if we have just inserted cols into an empty grid the current
4371 // cell will be undefined...
4373 SetCurrentCell( 0, 0 );
4377 if ( !GetBatchCount() ) Refresh();
4387 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4389 // TODO: something with updateLabels flag
4393 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4399 if (IsCellEditControlEnabled())
4400 DisableCellEditControl();
4402 if ( m_table
->DeleteCols( pos
, numCols
) )
4404 // the table will have sent the results of the delete col
4405 // operation to this view object as a grid table message
4408 if ( !GetBatchCount() ) Refresh();
4418 // ----- event handlers
4421 // Generate a grid event based on a mouse event and
4422 // return the result of ProcessEvent()
4424 bool wxGrid::SendEvent( const wxEventType type
,
4426 wxMouseEvent
& mouseEv
)
4428 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4430 int rowOrCol
= (row
== -1 ? col
: row
);
4432 wxGridSizeEvent
gridEvt( GetId(),
4436 mouseEv
.GetX(), mouseEv
.GetY(),
4437 mouseEv
.ControlDown(),
4438 mouseEv
.ShiftDown(),
4440 mouseEv
.MetaDown() );
4442 return GetEventHandler()->ProcessEvent(gridEvt
);
4444 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4446 wxGridRangeSelectEvent
gridEvt( GetId(),
4450 m_selectedBottomRight
,
4451 mouseEv
.ControlDown(),
4452 mouseEv
.ShiftDown(),
4454 mouseEv
.MetaDown() );
4456 return GetEventHandler()->ProcessEvent(gridEvt
);
4460 wxGridEvent
gridEvt( GetId(),
4464 mouseEv
.GetX(), mouseEv
.GetY(),
4465 mouseEv
.ControlDown(),
4466 mouseEv
.ShiftDown(),
4468 mouseEv
.MetaDown() );
4470 return GetEventHandler()->ProcessEvent(gridEvt
);
4475 // Generate a grid event of specified type and return the result
4476 // of ProcessEvent().
4478 bool wxGrid::SendEvent( const wxEventType type
,
4481 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4483 int rowOrCol
= (row
== -1 ? col
: row
);
4485 wxGridSizeEvent
gridEvt( GetId(),
4490 return GetEventHandler()->ProcessEvent(gridEvt
);
4494 wxGridEvent
gridEvt( GetId(),
4499 return GetEventHandler()->ProcessEvent(gridEvt
);
4504 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4506 wxPaintDC
dc( this );
4508 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4509 m_numRows
&& m_numCols
)
4511 m_currentCellCoords
.Set(0, 0);
4512 ShowCellEditControl();
4519 // This is just here to make sure that CalcDimensions gets called when
4520 // the grid view is resized... then the size event is skipped to allow
4521 // the box sizers to handle everything
4523 void wxGrid::OnSize( wxSizeEvent
& event
)
4530 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4532 if ( m_inOnKeyDown
)
4534 // shouldn't be here - we are going round in circles...
4536 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4539 m_inOnKeyDown
= TRUE
;
4541 // propagate the event up and see if it gets processed
4543 wxWindow
*parent
= GetParent();
4544 wxKeyEvent
keyEvt( event
);
4545 keyEvt
.SetEventObject( parent
);
4547 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4550 // TODO: Should also support Shift-cursor keys for
4551 // extending the selection. Maybe add a flag to
4552 // MoveCursorXXX() and MoveCursorXXXBlock() and
4553 // just send event.ShiftDown().
4555 // try local handlers
4557 switch ( event
.KeyCode() )
4560 if ( event
.ControlDown() )
4562 MoveCursorUpBlock();
4571 if ( event
.ControlDown() )
4573 MoveCursorDownBlock();
4582 if ( event
.ControlDown() )
4584 MoveCursorLeftBlock();
4593 if ( event
.ControlDown() )
4595 MoveCursorRightBlock();
4604 if ( event
.ControlDown() )
4606 event
.Skip(); // to let the edit control have the return
4615 if (event
.ShiftDown())
4622 if ( event
.ControlDown() )
4624 MakeCellVisible( 0, 0 );
4625 SetCurrentCell( 0, 0 );
4634 if ( event
.ControlDown() )
4636 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4637 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4653 // We don't want these keys to trigger the edit control, any others?
4662 if ( !IsEditable() )
4667 // Otherwise fall through to default
4670 // now try the cell edit control
4672 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4674 EnableCellEditControl();
4675 int row
= m_currentCellCoords
.GetRow();
4676 int col
= m_currentCellCoords
.GetCol();
4677 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4678 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4683 // let others process char events for readonly cells
4690 m_inOnKeyDown
= FALSE
;
4694 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4698 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4700 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4702 // the event has been intercepted - do nothing
4707 m_currentCellCoords
!= wxGridNoCellCoords
)
4709 HideCellEditControl();
4710 DisableCellEditControl();
4712 // Clear the old current cell highlight
4713 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4715 // Otherwise refresh redraws the highlight!
4716 m_currentCellCoords
= coords
;
4718 m_gridWin
->Refresh( FALSE
, &r
);
4721 m_currentCellCoords
= coords
;
4725 wxClientDC
dc(m_gridWin
);
4728 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4729 DrawCellHighlight(dc
, attr
);
4732 if ( IsSelection() )
4734 wxRect
r( SelectionToDeviceRect() );
4736 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4743 // ------ functions to get/send data (see also public functions)
4746 bool wxGrid::GetModelValues()
4750 // all we need to do is repaint the grid
4752 m_gridWin
->Refresh();
4760 bool wxGrid::SetModelValues()
4766 for ( row
= 0; row
< m_numRows
; row
++ )
4768 for ( col
= 0; col
< m_numCols
; col
++ )
4770 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4782 // Note - this function only draws cells that are in the list of
4783 // exposed cells (usually set from the update region by
4784 // CalcExposedCells)
4786 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4788 if ( !m_numRows
|| !m_numCols
) return;
4791 size_t numCells
= m_cellsExposed
.GetCount();
4793 for ( i
= 0; i
< numCells
; i
++ )
4795 DrawCell( dc
, m_cellsExposed
[i
] );
4800 void wxGrid::DrawGridSpace( wxDC
& dc
)
4802 if ( m_numRows
&& m_numCols
)
4805 m_gridWin
->GetClientSize( &cw
, &ch
);
4808 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4810 if ( right
> GetColRight(m_numCols
-1) ||
4811 bottom
> GetRowBottom(m_numRows
-1) )
4814 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4816 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4817 dc
.SetPen( *wxTRANSPARENT_PEN
);
4819 if ( right
> GetColRight(m_numCols
-1) )
4820 dc
.DrawRectangle( GetColRight(m_numCols
-1)+1, top
,
4821 right
- GetColRight(m_numCols
-1), ch
);
4823 if ( bottom
> GetRowBottom(m_numRows
-1) )
4824 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1)+1,
4825 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4831 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4833 int row
= coords
.GetRow();
4834 int col
= coords
.GetCol();
4836 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4839 // we draw the cell border ourselves
4840 #if !WXGRID_DRAW_LINES
4841 if ( m_gridLinesEnabled
)
4842 DrawCellBorder( dc
, coords
);
4845 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4847 bool isCurrent
= coords
== m_currentCellCoords
;
4850 rect
.x
= GetColLeft(col
);
4851 rect
.y
= GetRowTop(row
);
4852 rect
.width
= GetColWidth(col
) - 1;
4853 rect
.height
= GetRowHeight(row
) - 1;
4855 // if the editor is shown, we should use it and not the renderer
4856 if ( isCurrent
&& IsCellEditControlEnabled() )
4858 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4862 // but all the rest is drawn by the cell renderer and hence may be
4864 attr
->GetRenderer(this, row
, col
)->
4865 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4872 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4874 int row
= m_currentCellCoords
.GetRow();
4875 int col
= m_currentCellCoords
.GetCol();
4877 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4881 rect
.x
= GetColLeft(col
);
4882 rect
.y
= GetRowTop(row
);
4883 rect
.width
= GetColWidth(col
) - 1;
4884 rect
.height
= GetRowHeight(row
) - 1;
4886 // hmmm... what could we do here to show that the cell is disabled?
4887 // for now, I just draw a thinner border than for the other ones, but
4888 // it doesn't look really good
4889 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4890 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4892 dc
.DrawRectangle(rect
);
4895 // VZ: my experiments with 3d borders...
4897 // how to properly set colours for arbitrary bg?
4898 wxCoord x1
= rect
.x
,
4900 x2
= rect
.x
+ rect
.width
-1,
4901 y2
= rect
.y
+ rect
.height
-1;
4903 dc
.SetPen(*wxWHITE_PEN
);
4904 dc
.DrawLine(x1
, y1
, x2
, y1
);
4905 dc
.DrawLine(x1
, y1
, x1
, y2
);
4907 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4908 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4910 dc
.SetPen(*wxBLACK_PEN
);
4911 dc
.DrawLine(x1
, y2
, x2
, y2
);
4912 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4917 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4919 int row
= coords
.GetRow();
4920 int col
= coords
.GetCol();
4921 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4924 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4926 // right hand border
4928 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
4929 GetColRight(col
), GetRowBottom(row
) );
4933 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
4934 GetColRight(col
), GetRowBottom(row
) );
4937 void wxGrid::DrawHighlight(wxDC
& dc
)
4939 if ( IsCellEditControlEnabled() )
4941 // don't show highlight when the edit control is shown
4945 // if the active cell was repainted, repaint its highlight too because it
4946 // might have been damaged by the grid lines
4947 size_t count
= m_cellsExposed
.GetCount();
4948 for ( size_t n
= 0; n
< count
; n
++ )
4950 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4952 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4953 DrawCellHighlight(dc
, attr
);
4961 // TODO: remove this ???
4962 // This is used to redraw all grid lines e.g. when the grid line colour
4965 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4967 if ( !m_gridLinesEnabled
||
4969 !m_numCols
) return;
4971 int top
, bottom
, left
, right
;
4977 m_gridWin
->GetClientSize(&cw
, &ch
);
4979 // virtual coords of visible area
4981 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4982 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4987 reg
.GetBox(x
, y
, w
, h
);
4988 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4989 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
4993 m_gridWin
->GetClientSize(&cw
, &ch
);
4994 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4995 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4998 // avoid drawing grid lines past the last row and col
5000 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5001 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5003 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5005 // horizontal grid lines
5008 for ( i
= 0; i
< m_numRows
; i
++ )
5010 int bot
= GetRowBottom(i
) - 1;
5019 dc
.DrawLine( left
, bot
, right
, bot
);
5024 // vertical grid lines
5026 for ( i
= 0; i
< m_numCols
; i
++ )
5028 int colRight
= GetColRight(i
) - 1;
5029 if ( colRight
> right
)
5034 if ( colRight
>= left
)
5036 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5042 void wxGrid::DrawRowLabels( wxDC
& dc
)
5044 if ( !m_numRows
|| !m_numCols
) return;
5047 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5049 for ( i
= 0; i
< numLabels
; i
++ )
5051 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5056 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5058 if ( GetRowHeight(row
) <= 0 )
5061 int rowTop
= GetRowTop(row
),
5062 rowBottom
= GetRowBottom(row
) - 1;
5064 dc
.SetPen( *wxBLACK_PEN
);
5065 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5066 m_rowLabelWidth
-1, rowBottom
);
5068 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5070 dc
.SetPen( *wxWHITE_PEN
);
5071 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5072 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5074 dc
.SetBackgroundMode( wxTRANSPARENT
);
5075 dc
.SetTextForeground( GetLabelTextColour() );
5076 dc
.SetFont( GetLabelFont() );
5079 GetRowLabelAlignment( &hAlign
, &vAlign
);
5083 rect
.SetY( GetRowTop(row
) + 2 );
5084 rect
.SetWidth( m_rowLabelWidth
- 4 );
5085 rect
.SetHeight( GetRowHeight(row
) - 4 );
5086 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5090 void wxGrid::DrawColLabels( wxDC
& dc
)
5092 if ( !m_numRows
|| !m_numCols
) return;
5095 size_t numLabels
= m_colLabelsExposed
.GetCount();
5097 for ( i
= 0; i
< numLabels
; i
++ )
5099 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5104 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5106 if ( GetColWidth(col
) <= 0 )
5109 int colLeft
= GetColLeft(col
),
5110 colRight
= GetColRight(col
) - 1;
5112 dc
.SetPen( *wxBLACK_PEN
);
5113 dc
.DrawLine( colRight
, 0,
5114 colRight
, m_colLabelHeight
-1 );
5116 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5117 colRight
, m_colLabelHeight
-1 );
5119 dc
.SetPen( *wxWHITE_PEN
);
5120 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5121 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5123 dc
.SetBackgroundMode( wxTRANSPARENT
);
5124 dc
.SetTextForeground( GetLabelTextColour() );
5125 dc
.SetFont( GetLabelFont() );
5127 dc
.SetBackgroundMode( wxTRANSPARENT
);
5128 dc
.SetTextForeground( GetLabelTextColour() );
5129 dc
.SetFont( GetLabelFont() );
5132 GetColLabelAlignment( &hAlign
, &vAlign
);
5135 rect
.SetX( colLeft
+ 2 );
5137 rect
.SetWidth( GetColWidth(col
) - 4 );
5138 rect
.SetHeight( m_colLabelHeight
- 4 );
5139 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5143 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5144 const wxString
& value
,
5149 long textWidth
, textHeight
;
5150 long lineWidth
, lineHeight
;
5151 wxArrayString lines
;
5153 dc
.SetClippingRegion( rect
);
5154 StringToLines( value
, lines
);
5155 if ( lines
.GetCount() )
5157 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5158 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5161 switch ( horizAlign
)
5164 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5168 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5177 switch ( vertAlign
)
5180 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5184 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5193 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5195 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5200 dc
.DestroyClippingRegion();
5204 // Split multi line text up into an array of strings. Any existing
5205 // contents of the string array are preserved.
5207 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5211 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5212 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5214 while ( startPos
< (int)tVal
.Length() )
5216 pos
= tVal
.Mid(startPos
).Find( eol
);
5221 else if ( pos
== 0 )
5223 lines
.Add( wxEmptyString
);
5227 lines
.Add( value
.Mid(startPos
, pos
) );
5231 if ( startPos
< (int)value
.Length() )
5233 lines
.Add( value
.Mid( startPos
) );
5238 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5239 wxArrayString
& lines
,
5240 long *width
, long *height
)
5247 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5249 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5250 w
= wxMax( w
, lineW
);
5260 // ------ Edit control functions
5264 void wxGrid::EnableEditing( bool edit
)
5266 // TODO: improve this ?
5268 if ( edit
!= m_editable
)
5272 // FIXME IMHO this won't disable the edit control if edit == FALSE
5273 // because of the check in the beginning of
5274 // EnableCellEditControl() just below (VZ)
5275 EnableCellEditControl(m_editable
);
5280 void wxGrid::EnableCellEditControl( bool enable
)
5285 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5286 SetCurrentCell( 0, 0 );
5288 if ( enable
!= m_cellEditCtrlEnabled
)
5290 // TODO allow the app to Veto() this event?
5291 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5295 // this should be checked by the caller!
5296 wxASSERT_MSG( CanEnableCellControl(),
5297 _T("can't enable editing for this cell!") );
5299 // do it before ShowCellEditControl()
5300 m_cellEditCtrlEnabled
= enable
;
5302 ShowCellEditControl();
5306 HideCellEditControl();
5307 SaveEditControlValue();
5309 // do it after HideCellEditControl()
5310 m_cellEditCtrlEnabled
= enable
;
5315 bool wxGrid::IsCurrentCellReadOnly() const
5318 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5319 bool readonly
= attr
->IsReadOnly();
5325 bool wxGrid::CanEnableCellControl() const
5327 return m_editable
&& !IsCurrentCellReadOnly();
5330 bool wxGrid::IsCellEditControlEnabled() const
5332 // the cell edit control might be disable for all cells or just for the
5333 // current one if it's read only
5334 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5337 void wxGrid::ShowCellEditControl()
5339 if ( IsCellEditControlEnabled() )
5341 if ( !IsVisible( m_currentCellCoords
) )
5347 wxRect rect
= CellToRect( m_currentCellCoords
);
5348 int row
= m_currentCellCoords
.GetRow();
5349 int col
= m_currentCellCoords
.GetCol();
5351 // convert to scrolled coords
5353 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5355 // done in PaintBackground()
5357 // erase the highlight and the cell contents because the editor
5358 // might not cover the entire cell
5359 wxClientDC
dc( m_gridWin
);
5361 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5362 dc
.SetPen(*wxTRANSPARENT_PEN
);
5363 dc
.DrawRectangle(rect
);
5366 // cell is shifted by one pixel
5370 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5371 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5372 if ( !editor
->IsCreated() )
5374 editor
->Create(m_gridWin
, -1,
5375 new wxGridCellEditorEvtHandler(this, editor
));
5378 editor
->SetSize( rect
);
5380 editor
->Show( TRUE
, attr
);
5381 editor
->BeginEdit(row
, col
, this);
5388 void wxGrid::HideCellEditControl()
5390 if ( IsCellEditControlEnabled() )
5392 int row
= m_currentCellCoords
.GetRow();
5393 int col
= m_currentCellCoords
.GetCol();
5395 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5396 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5398 m_gridWin
->SetFocus();
5403 void wxGrid::SaveEditControlValue()
5405 if ( IsCellEditControlEnabled() )
5407 int row
= m_currentCellCoords
.GetRow();
5408 int col
= m_currentCellCoords
.GetCol();
5410 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5411 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5412 bool changed
= editor
->EndEdit(row
, col
, this);
5418 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5419 m_currentCellCoords
.GetRow(),
5420 m_currentCellCoords
.GetCol() );
5427 // ------ Grid location functions
5428 // Note that all of these functions work with the logical coordinates of
5429 // grid cells and labels so you will need to convert from device
5430 // coordinates for mouse events etc.
5433 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5435 int row
= YToRow(y
);
5436 int col
= XToCol(x
);
5438 if ( row
== -1 || col
== -1 )
5440 coords
= wxGridNoCellCoords
;
5444 coords
.Set( row
, col
);
5449 int wxGrid::YToRow( int y
)
5453 for ( i
= 0; i
< m_numRows
; i
++ )
5455 if ( y
< GetRowBottom(i
) )
5463 int wxGrid::XToCol( int x
)
5467 for ( i
= 0; i
< m_numCols
; i
++ )
5469 if ( x
< GetColRight(i
) )
5477 // return the row number that that the y coord is near the edge of, or
5478 // -1 if not near an edge
5480 int wxGrid::YToEdgeOfRow( int y
)
5484 for ( i
= 0; i
< m_numRows
; i
++ )
5486 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5488 d
= abs( y
- GetRowBottom(i
) );
5489 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5498 // return the col number that that the x coord is near the edge of, or
5499 // -1 if not near an edge
5501 int wxGrid::XToEdgeOfCol( int x
)
5505 for ( i
= 0; i
< m_numCols
; i
++ )
5507 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5509 d
= abs( x
- GetColRight(i
) );
5510 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5519 wxRect
wxGrid::CellToRect( int row
, int col
)
5521 wxRect
rect( -1, -1, -1, -1 );
5523 if ( row
>= 0 && row
< m_numRows
&&
5524 col
>= 0 && col
< m_numCols
)
5526 rect
.x
= GetColLeft(col
);
5527 rect
.y
= GetRowTop(row
);
5528 rect
.width
= GetColWidth(col
);
5529 rect
.height
= GetRowHeight(row
);
5536 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5538 // get the cell rectangle in logical coords
5540 wxRect
r( CellToRect( row
, col
) );
5542 // convert to device coords
5544 int left
, top
, right
, bottom
;
5545 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5546 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5548 // check against the client area of the grid window
5551 m_gridWin
->GetClientSize( &cw
, &ch
);
5553 if ( wholeCellVisible
)
5555 // is the cell wholly visible ?
5557 return ( left
>= 0 && right
<= cw
&&
5558 top
>= 0 && bottom
<= ch
);
5562 // is the cell partly visible ?
5564 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5565 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5570 // make the specified cell location visible by doing a minimal amount
5573 void wxGrid::MakeCellVisible( int row
, int col
)
5576 int xpos
= -1, ypos
= -1;
5578 if ( row
>= 0 && row
< m_numRows
&&
5579 col
>= 0 && col
< m_numCols
)
5581 // get the cell rectangle in logical coords
5583 wxRect
r( CellToRect( row
, col
) );
5585 // convert to device coords
5587 int left
, top
, right
, bottom
;
5588 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5589 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5592 m_gridWin
->GetClientSize( &cw
, &ch
);
5598 else if ( bottom
> ch
)
5600 int h
= r
.GetHeight();
5602 for ( i
= row
-1; i
>= 0; i
-- )
5604 int rowHeight
= GetRowHeight(i
);
5605 if ( h
+ rowHeight
> ch
)
5612 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5613 // have rounding errors (this is important, because if we do, we
5614 // might not scroll at all and some cells won't be redrawn)
5615 ypos
+= GRID_SCROLL_LINE
/ 2;
5622 else if ( right
> cw
)
5624 int w
= r
.GetWidth();
5626 for ( i
= col
-1; i
>= 0; i
-- )
5628 int colWidth
= GetColWidth(i
);
5629 if ( w
+ colWidth
> cw
)
5636 // see comment for ypos above
5637 xpos
+= GRID_SCROLL_LINE
/ 2;
5640 if ( xpos
!= -1 || ypos
!= -1 )
5642 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5643 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5644 Scroll( xpos
, ypos
);
5652 // ------ Grid cursor movement functions
5655 bool wxGrid::MoveCursorUp()
5657 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5658 m_currentCellCoords
.GetRow() > 0 )
5660 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5661 m_currentCellCoords
.GetCol() );
5663 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5664 m_currentCellCoords
.GetCol() );
5673 bool wxGrid::MoveCursorDown()
5675 // TODO: allow for scrolling
5677 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5678 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5680 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5681 m_currentCellCoords
.GetCol() );
5683 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5684 m_currentCellCoords
.GetCol() );
5693 bool wxGrid::MoveCursorLeft()
5695 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5696 m_currentCellCoords
.GetCol() > 0 )
5698 MakeCellVisible( m_currentCellCoords
.GetRow(),
5699 m_currentCellCoords
.GetCol() - 1 );
5701 SetCurrentCell( m_currentCellCoords
.GetRow(),
5702 m_currentCellCoords
.GetCol() - 1 );
5711 bool wxGrid::MoveCursorRight()
5713 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5714 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5716 MakeCellVisible( m_currentCellCoords
.GetRow(),
5717 m_currentCellCoords
.GetCol() + 1 );
5719 SetCurrentCell( m_currentCellCoords
.GetRow(),
5720 m_currentCellCoords
.GetCol() + 1 );
5729 bool wxGrid::MovePageUp()
5731 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5733 int row
= m_currentCellCoords
.GetRow();
5737 m_gridWin
->GetClientSize( &cw
, &ch
);
5739 int y
= GetRowTop(row
);
5740 int newRow
= YToRow( y
- ch
+ 1 );
5745 else if ( newRow
== row
)
5750 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5751 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5759 bool wxGrid::MovePageDown()
5761 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5763 int row
= m_currentCellCoords
.GetRow();
5764 if ( row
< m_numRows
)
5767 m_gridWin
->GetClientSize( &cw
, &ch
);
5769 int y
= GetRowTop(row
);
5770 int newRow
= YToRow( y
+ ch
);
5773 newRow
= m_numRows
- 1;
5775 else if ( newRow
== row
)
5780 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5781 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5789 bool wxGrid::MoveCursorUpBlock()
5792 m_currentCellCoords
!= wxGridNoCellCoords
&&
5793 m_currentCellCoords
.GetRow() > 0 )
5795 int row
= m_currentCellCoords
.GetRow();
5796 int col
= m_currentCellCoords
.GetCol();
5798 if ( m_table
->IsEmptyCell(row
, col
) )
5800 // starting in an empty cell: find the next block of
5806 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5809 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5811 // starting at the top of a block: find the next block
5817 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5822 // starting within a block: find the top of the block
5827 if ( m_table
->IsEmptyCell(row
, col
) )
5835 MakeCellVisible( row
, col
);
5836 SetCurrentCell( row
, col
);
5844 bool wxGrid::MoveCursorDownBlock()
5847 m_currentCellCoords
!= wxGridNoCellCoords
&&
5848 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5850 int row
= m_currentCellCoords
.GetRow();
5851 int col
= m_currentCellCoords
.GetCol();
5853 if ( m_table
->IsEmptyCell(row
, col
) )
5855 // starting in an empty cell: find the next block of
5858 while ( row
< m_numRows
-1 )
5861 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5864 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5866 // starting at the bottom of a block: find the next block
5869 while ( row
< m_numRows
-1 )
5872 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5877 // starting within a block: find the bottom of the block
5879 while ( row
< m_numRows
-1 )
5882 if ( m_table
->IsEmptyCell(row
, col
) )
5890 MakeCellVisible( row
, col
);
5891 SetCurrentCell( row
, col
);
5899 bool wxGrid::MoveCursorLeftBlock()
5902 m_currentCellCoords
!= wxGridNoCellCoords
&&
5903 m_currentCellCoords
.GetCol() > 0 )
5905 int row
= m_currentCellCoords
.GetRow();
5906 int col
= m_currentCellCoords
.GetCol();
5908 if ( m_table
->IsEmptyCell(row
, col
) )
5910 // starting in an empty cell: find the next block of
5916 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5919 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5921 // starting at the left of a block: find the next block
5927 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5932 // starting within a block: find the left of the block
5937 if ( m_table
->IsEmptyCell(row
, col
) )
5945 MakeCellVisible( row
, col
);
5946 SetCurrentCell( row
, col
);
5954 bool wxGrid::MoveCursorRightBlock()
5957 m_currentCellCoords
!= wxGridNoCellCoords
&&
5958 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5960 int row
= m_currentCellCoords
.GetRow();
5961 int col
= m_currentCellCoords
.GetCol();
5963 if ( m_table
->IsEmptyCell(row
, col
) )
5965 // starting in an empty cell: find the next block of
5968 while ( col
< m_numCols
-1 )
5971 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5974 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5976 // starting at the right of a block: find the next block
5979 while ( col
< m_numCols
-1 )
5982 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5987 // starting within a block: find the right of the block
5989 while ( col
< m_numCols
-1 )
5992 if ( m_table
->IsEmptyCell(row
, col
) )
6000 MakeCellVisible( row
, col
);
6001 SetCurrentCell( row
, col
);
6012 // ------ Label values and formatting
6015 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6017 *horiz
= m_rowLabelHorizAlign
;
6018 *vert
= m_rowLabelVertAlign
;
6021 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6023 *horiz
= m_colLabelHorizAlign
;
6024 *vert
= m_colLabelVertAlign
;
6027 wxString
wxGrid::GetRowLabelValue( int row
)
6031 return m_table
->GetRowLabelValue( row
);
6041 wxString
wxGrid::GetColLabelValue( int col
)
6045 return m_table
->GetColLabelValue( col
);
6056 void wxGrid::SetRowLabelSize( int width
)
6058 width
= wxMax( width
, 0 );
6059 if ( width
!= m_rowLabelWidth
)
6063 m_rowLabelWin
->Show( FALSE
);
6064 m_cornerLabelWin
->Show( FALSE
);
6066 else if ( m_rowLabelWidth
== 0 )
6068 m_rowLabelWin
->Show( TRUE
);
6069 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6072 m_rowLabelWidth
= width
;
6079 void wxGrid::SetColLabelSize( int height
)
6081 height
= wxMax( height
, 0 );
6082 if ( height
!= m_colLabelHeight
)
6086 m_colLabelWin
->Show( FALSE
);
6087 m_cornerLabelWin
->Show( FALSE
);
6089 else if ( m_colLabelHeight
== 0 )
6091 m_colLabelWin
->Show( TRUE
);
6092 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6095 m_colLabelHeight
= height
;
6102 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6104 if ( m_labelBackgroundColour
!= colour
)
6106 m_labelBackgroundColour
= colour
;
6107 m_rowLabelWin
->SetBackgroundColour( colour
);
6108 m_colLabelWin
->SetBackgroundColour( colour
);
6109 m_cornerLabelWin
->SetBackgroundColour( colour
);
6111 if ( !GetBatchCount() )
6113 m_rowLabelWin
->Refresh();
6114 m_colLabelWin
->Refresh();
6115 m_cornerLabelWin
->Refresh();
6120 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6122 if ( m_labelTextColour
!= colour
)
6124 m_labelTextColour
= colour
;
6125 if ( !GetBatchCount() )
6127 m_rowLabelWin
->Refresh();
6128 m_colLabelWin
->Refresh();
6133 void wxGrid::SetLabelFont( const wxFont
& font
)
6136 if ( !GetBatchCount() )
6138 m_rowLabelWin
->Refresh();
6139 m_colLabelWin
->Refresh();
6143 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6145 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6147 m_rowLabelHorizAlign
= horiz
;
6150 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6152 m_rowLabelVertAlign
= vert
;
6155 if ( !GetBatchCount() )
6157 m_rowLabelWin
->Refresh();
6161 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6163 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6165 m_colLabelHorizAlign
= horiz
;
6168 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6170 m_colLabelVertAlign
= vert
;
6173 if ( !GetBatchCount() )
6175 m_colLabelWin
->Refresh();
6179 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6183 m_table
->SetRowLabelValue( row
, s
);
6184 if ( !GetBatchCount() )
6186 wxRect rect
= CellToRect( row
, 0);
6187 if ( rect
.height
> 0 )
6189 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6191 rect
.width
= m_rowLabelWidth
;
6192 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6198 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6202 m_table
->SetColLabelValue( col
, s
);
6203 if ( !GetBatchCount() )
6205 wxRect rect
= CellToRect( 0, col
);
6206 if ( rect
.width
> 0 )
6208 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6210 rect
.height
= m_colLabelHeight
;
6211 m_colLabelWin
->Refresh( TRUE
, &rect
);
6217 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6219 if ( m_gridLineColour
!= colour
)
6221 m_gridLineColour
= colour
;
6223 wxClientDC
dc( m_gridWin
);
6225 DrawAllGridLines( dc
, wxRegion() );
6229 void wxGrid::EnableGridLines( bool enable
)
6231 if ( enable
!= m_gridLinesEnabled
)
6233 m_gridLinesEnabled
= enable
;
6235 if ( !GetBatchCount() )
6239 wxClientDC
dc( m_gridWin
);
6241 DrawAllGridLines( dc
, wxRegion() );
6245 m_gridWin
->Refresh();
6252 int wxGrid::GetDefaultRowSize()
6254 return m_defaultRowHeight
;
6257 int wxGrid::GetRowSize( int row
)
6259 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6261 return GetRowHeight(row
);
6264 int wxGrid::GetDefaultColSize()
6266 return m_defaultColWidth
;
6269 int wxGrid::GetColSize( int col
)
6271 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6273 return GetColWidth(col
);
6276 // ============================================================================
6277 // access to the grid attributes: each of them has a default value in the grid
6278 // itself and may be overidden on a per-cell basis
6279 // ============================================================================
6281 // ----------------------------------------------------------------------------
6282 // setting default attributes
6283 // ----------------------------------------------------------------------------
6285 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6287 m_defaultCellAttr
->SetBackgroundColour(col
);
6289 m_gridWin
->SetBackgroundColour(col
);
6293 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6295 m_defaultCellAttr
->SetTextColour(col
);
6298 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6300 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6303 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6305 m_defaultCellAttr
->SetFont(font
);
6308 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6310 m_defaultCellAttr
->SetRenderer(renderer
);
6313 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6315 m_defaultCellAttr
->SetEditor(editor
);
6318 // ----------------------------------------------------------------------------
6319 // access to the default attrbiutes
6320 // ----------------------------------------------------------------------------
6322 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6324 return m_defaultCellAttr
->GetBackgroundColour();
6327 wxColour
wxGrid::GetDefaultCellTextColour()
6329 return m_defaultCellAttr
->GetTextColour();
6332 wxFont
wxGrid::GetDefaultCellFont()
6334 return m_defaultCellAttr
->GetFont();
6337 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6339 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6342 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6344 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6347 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6349 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6352 // ----------------------------------------------------------------------------
6353 // access to cell attributes
6354 // ----------------------------------------------------------------------------
6356 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6358 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6359 wxColour colour
= attr
->GetBackgroundColour();
6364 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6366 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6367 wxColour colour
= attr
->GetTextColour();
6372 wxFont
wxGrid::GetCellFont( int row
, int col
)
6374 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6375 wxFont font
= attr
->GetFont();
6380 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6382 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6383 attr
->GetAlignment(horiz
, vert
);
6387 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6389 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6390 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6395 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6397 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6398 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6403 bool wxGrid::IsReadOnly(int row
, int col
) const
6405 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6406 bool isReadOnly
= attr
->IsReadOnly();
6411 // ----------------------------------------------------------------------------
6412 // attribute support: cache, automatic provider creation, ...
6413 // ----------------------------------------------------------------------------
6415 bool wxGrid::CanHaveAttributes()
6422 return m_table
->CanHaveAttributes();
6425 void wxGrid::ClearAttrCache()
6427 if ( m_attrCache
.row
!= -1 )
6429 m_attrCache
.attr
->SafeDecRef();
6430 m_attrCache
.row
= -1;
6434 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6436 wxGrid
*self
= (wxGrid
*)this; // const_cast
6438 self
->ClearAttrCache();
6439 self
->m_attrCache
.row
= row
;
6440 self
->m_attrCache
.col
= col
;
6441 self
->m_attrCache
.attr
= attr
;
6445 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6447 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6449 *attr
= m_attrCache
.attr
;
6450 (*attr
)->SafeIncRef();
6452 #ifdef DEBUG_ATTR_CACHE
6453 gs_nAttrCacheHits
++;
6460 #ifdef DEBUG_ATTR_CACHE
6461 gs_nAttrCacheMisses
++;
6467 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6469 wxGridCellAttr
*attr
;
6470 if ( !LookupAttr(row
, col
, &attr
) )
6472 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6473 CacheAttr(row
, col
, attr
);
6477 attr
->SetDefAttr(m_defaultCellAttr
);
6481 attr
= m_defaultCellAttr
;
6488 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6490 wxGridCellAttr
*attr
;
6491 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6493 wxASSERT_MSG( m_table
,
6494 _T("we may only be called if CanHaveAttributes() "
6495 "returned TRUE and then m_table should be !NULL") );
6497 attr
= m_table
->GetAttr(row
, col
);
6500 attr
= new wxGridCellAttr
;
6502 // artificially inc the ref count to match DecRef() in caller
6505 m_table
->SetAttr(attr
, row
, col
);
6508 CacheAttr(row
, col
, attr
);
6510 attr
->SetDefAttr(m_defaultCellAttr
);
6514 // ----------------------------------------------------------------------------
6515 // setting cell attributes: this is forwarded to the table
6516 // ----------------------------------------------------------------------------
6518 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6520 if ( CanHaveAttributes() )
6522 m_table
->SetRowAttr(attr
, row
);
6530 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6532 if ( CanHaveAttributes() )
6534 m_table
->SetColAttr(attr
, col
);
6542 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6544 if ( CanHaveAttributes() )
6546 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6547 attr
->SetBackgroundColour(colour
);
6552 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6554 if ( CanHaveAttributes() )
6556 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6557 attr
->SetTextColour(colour
);
6562 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6564 if ( CanHaveAttributes() )
6566 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6567 attr
->SetFont(font
);
6572 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6574 if ( CanHaveAttributes() )
6576 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6577 attr
->SetAlignment(horiz
, vert
);
6582 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6584 if ( CanHaveAttributes() )
6586 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6587 attr
->SetRenderer(renderer
);
6592 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6594 if ( CanHaveAttributes() )
6596 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6597 attr
->SetEditor(editor
);
6602 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6604 if ( CanHaveAttributes() )
6606 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6607 attr
->SetReadOnly(isReadOnly
);
6612 // ----------------------------------------------------------------------------
6613 // Data type registration
6614 // ----------------------------------------------------------------------------
6616 void wxGrid::RegisterDataType(const wxString
& typeName
,
6617 wxGridCellRenderer
* renderer
,
6618 wxGridCellEditor
* editor
)
6620 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6624 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6626 wxString typeName
= m_table
->GetTypeName(row
, col
);
6627 return GetDefaultEditorForType(typeName
);
6630 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6632 wxString typeName
= m_table
->GetTypeName(row
, col
);
6633 return GetDefaultRendererForType(typeName
);
6637 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6639 int index
= m_typeRegistry
->FindDataType(typeName
);
6641 // Should we force the failure here or let it fallback to string handling???
6642 // wxFAIL_MSG(wxT("Unknown data type name"));
6645 return m_typeRegistry
->GetEditor(index
);
6649 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6651 int index
= m_typeRegistry
->FindDataType(typeName
);
6653 // Should we force the failure here or let it fallback to string handling???
6654 // wxFAIL_MSG(wxT("Unknown data type name"));
6657 return m_typeRegistry
->GetRenderer(index
);
6661 // ----------------------------------------------------------------------------
6663 // ----------------------------------------------------------------------------
6665 void wxGrid::EnableDragRowSize( bool enable
)
6667 m_canDragRowSize
= enable
;
6671 void wxGrid::EnableDragColSize( bool enable
)
6673 m_canDragColSize
= enable
;
6676 void wxGrid::EnableDragGridSize( bool enable
)
6678 m_canDragGridSize
= enable
;
6682 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6684 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6686 if ( resizeExistingRows
)
6694 void wxGrid::SetRowSize( int row
, int height
)
6696 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6698 if ( m_rowHeights
.IsEmpty() )
6700 // need to really create the array
6704 int h
= wxMax( 0, height
);
6705 int diff
= h
- m_rowHeights
[row
];
6707 m_rowHeights
[row
] = h
;
6709 for ( i
= row
; i
< m_numRows
; i
++ )
6711 m_rowBottoms
[i
] += diff
;
6716 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6718 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6720 if ( resizeExistingCols
)
6728 void wxGrid::SetColSize( int col
, int width
)
6730 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6732 // should we check that it's bigger than GetColMinimalWidth(col) here?
6734 if ( m_colWidths
.IsEmpty() )
6736 // need to really create the array
6740 int w
= wxMax( 0, width
);
6741 int diff
= w
- m_colWidths
[col
];
6742 m_colWidths
[col
] = w
;
6745 for ( i
= col
; i
< m_numCols
; i
++ )
6747 m_colRights
[i
] += diff
;
6753 void wxGrid::SetColMinimalWidth( int col
, int width
)
6755 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6758 int wxGrid::GetColMinimalWidth(int col
) const
6760 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6761 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6764 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6766 wxClientDC
dc(m_gridWin
);
6768 wxCoord width
, widthMax
= 0;
6769 for ( int row
= 0; row
< m_numRows
; row
++ )
6771 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6772 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6775 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6776 if ( width
> widthMax
)
6785 // now also compare with the column label width
6786 dc
.SetFont( GetLabelFont() );
6787 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6788 if ( width
> widthMax
)
6795 // empty column - give default width (notice that if widthMax is less
6796 // than default width but != 0, it's ok)
6797 widthMax
= m_defaultColWidth
;
6801 // leave some space around text
6805 SetColSize(col
, widthMax
);
6808 SetColMinimalWidth(col
, widthMax
);
6812 void wxGrid::AutoSizeColumns( bool setAsMin
)
6814 for ( int col
= 0; col
< m_numCols
; col
++ )
6816 AutoSizeColumn(col
, setAsMin
);
6821 // ------ cell value accessor functions
6824 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6828 m_table
->SetValue( row
, col
, s
.c_str() );
6829 if ( !GetBatchCount() )
6831 wxClientDC
dc( m_gridWin
);
6833 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6836 if ( m_currentCellCoords
.GetRow() == row
&&
6837 m_currentCellCoords
.GetCol() == col
&&
6838 IsCellEditControlEnabled())
6840 HideCellEditControl();
6841 ShowCellEditControl(); // will reread data from table
6848 // ------ Block, row and col selection
6851 void wxGrid::SelectRow( int row
, bool addToSelected
)
6855 if ( IsSelection() && addToSelected
)
6858 bool need_refresh
[4];
6862 need_refresh
[3] = FALSE
;
6866 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6867 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6868 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6869 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6873 need_refresh
[0] = TRUE
;
6874 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6875 wxGridCellCoords ( oldTop
- 1,
6877 m_selectedTopLeft
.SetRow( row
);
6882 need_refresh
[1] = TRUE
;
6883 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6884 wxGridCellCoords ( oldBottom
,
6887 m_selectedTopLeft
.SetCol( 0 );
6890 if ( oldBottom
< row
)
6892 need_refresh
[2] = TRUE
;
6893 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6894 wxGridCellCoords ( row
,
6896 m_selectedBottomRight
.SetRow( row
);
6899 if ( oldRight
< m_numCols
- 1 )
6901 need_refresh
[3] = TRUE
;
6902 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6904 wxGridCellCoords ( oldBottom
,
6906 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6909 for (i
= 0; i
< 4; i
++ )
6910 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6911 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6915 r
= SelectionToDeviceRect();
6917 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6919 m_selectedTopLeft
.Set( row
, 0 );
6920 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6921 r
= SelectionToDeviceRect();
6922 m_gridWin
->Refresh( FALSE
, &r
);
6925 wxGridRangeSelectEvent
gridEvt( GetId(),
6926 wxEVT_GRID_RANGE_SELECT
,
6929 m_selectedBottomRight
);
6931 GetEventHandler()->ProcessEvent(gridEvt
);
6935 void wxGrid::SelectCol( int col
, bool addToSelected
)
6937 if ( IsSelection() && addToSelected
)
6940 bool need_refresh
[4];
6944 need_refresh
[3] = FALSE
;
6947 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6948 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6949 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6950 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6952 if ( oldLeft
> col
)
6954 need_refresh
[0] = TRUE
;
6955 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6956 wxGridCellCoords ( m_numRows
- 1,
6958 m_selectedTopLeft
.SetCol( col
);
6963 need_refresh
[1] = TRUE
;
6964 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6965 wxGridCellCoords ( oldTop
- 1,
6967 m_selectedTopLeft
.SetRow( 0 );
6970 if ( oldRight
< col
)
6972 need_refresh
[2] = TRUE
;
6973 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6974 wxGridCellCoords ( m_numRows
- 1,
6976 m_selectedBottomRight
.SetCol( col
);
6979 if ( oldBottom
< m_numRows
- 1 )
6981 need_refresh
[3] = TRUE
;
6982 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6984 wxGridCellCoords ( m_numRows
- 1,
6986 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6989 for (i
= 0; i
< 4; i
++ )
6990 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6991 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6997 r
= SelectionToDeviceRect();
6999 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7001 m_selectedTopLeft
.Set( 0, col
);
7002 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7003 r
= SelectionToDeviceRect();
7004 m_gridWin
->Refresh( FALSE
, &r
);
7007 wxGridRangeSelectEvent
gridEvt( GetId(),
7008 wxEVT_GRID_RANGE_SELECT
,
7011 m_selectedBottomRight
);
7013 GetEventHandler()->ProcessEvent(gridEvt
);
7017 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7020 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7022 if ( topRow
> bottomRow
)
7029 if ( leftCol
> rightCol
)
7036 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7037 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7039 if ( m_selectedTopLeft
!= updateTopLeft
||
7040 m_selectedBottomRight
!= updateBottomRight
)
7042 // Compute two optimal update rectangles:
7043 // Either one rectangle is a real subset of the
7044 // other, or they are (almost) disjoint!
7046 bool need_refresh
[4];
7050 need_refresh
[3] = FALSE
;
7053 // Store intermediate values
7054 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7055 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7056 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7057 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7059 // Determine the outer/inner coordinates.
7060 if (oldLeft
> leftCol
)
7066 if (oldTop
> topRow
)
7072 if (oldRight
< rightCol
)
7075 oldRight
= rightCol
;
7078 if (oldBottom
< bottomRow
)
7081 oldBottom
= bottomRow
;
7085 // Now, either the stuff marked old is the outer
7086 // rectangle or we don't have a situation where one
7087 // is contained in the other.
7089 if ( oldLeft
< leftCol
)
7091 need_refresh
[0] = TRUE
;
7092 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7094 wxGridCellCoords ( oldBottom
,
7098 if ( oldTop
< topRow
)
7100 need_refresh
[1] = TRUE
;
7101 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7103 wxGridCellCoords ( topRow
- 1,
7107 if ( oldRight
> rightCol
)
7109 need_refresh
[2] = TRUE
;
7110 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7112 wxGridCellCoords ( oldBottom
,
7116 if ( oldBottom
> bottomRow
)
7118 need_refresh
[3] = TRUE
;
7119 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7121 wxGridCellCoords ( oldBottom
,
7127 m_selectedTopLeft
= updateTopLeft
;
7128 m_selectedBottomRight
= updateBottomRight
;
7130 // various Refresh() calls
7131 for (i
= 0; i
< 4; i
++ )
7132 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7133 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7136 // only generate an event if the block is not being selected by
7137 // dragging the mouse (in which case the event will be generated in
7138 // the mouse event handler)
7139 if ( !m_isDragging
)
7141 wxGridRangeSelectEvent
gridEvt( GetId(),
7142 wxEVT_GRID_RANGE_SELECT
,
7145 m_selectedBottomRight
);
7147 GetEventHandler()->ProcessEvent(gridEvt
);
7151 void wxGrid::SelectAll()
7153 m_selectedTopLeft
.Set( 0, 0 );
7154 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7156 m_gridWin
->Refresh();
7160 void wxGrid::ClearSelection()
7162 m_selectedTopLeft
= wxGridNoCellCoords
;
7163 m_selectedBottomRight
= wxGridNoCellCoords
;
7167 // This function returns the rectangle that encloses the given block
7168 // in device coords clipped to the client size of the grid window.
7170 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7171 const wxGridCellCoords
&bottomRight
)
7173 wxRect
rect( wxGridNoCellRect
);
7176 cellRect
= CellToRect( topLeft
);
7177 if ( cellRect
!= wxGridNoCellRect
)
7183 rect
= wxRect( 0, 0, 0, 0 );
7186 cellRect
= CellToRect( bottomRight
);
7187 if ( cellRect
!= wxGridNoCellRect
)
7193 return wxGridNoCellRect
;
7196 // convert to scrolled coords
7198 int left
, top
, right
, bottom
;
7199 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7200 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7203 m_gridWin
->GetClientSize( &cw
, &ch
);
7205 rect
.SetLeft( wxMax(0, left
) );
7206 rect
.SetTop( wxMax(0, top
) );
7207 rect
.SetRight( wxMin(cw
, right
) );
7208 rect
.SetBottom( wxMin(ch
, bottom
) );
7216 // ------ Grid event classes
7219 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7221 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7222 int row
, int col
, int x
, int y
,
7223 bool control
, bool shift
, bool alt
, bool meta
)
7224 : wxNotifyEvent( type
, id
)
7230 m_control
= control
;
7235 SetEventObject(obj
);
7239 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7241 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7242 int rowOrCol
, int x
, int y
,
7243 bool control
, bool shift
, bool alt
, bool meta
)
7244 : wxNotifyEvent( type
, id
)
7246 m_rowOrCol
= rowOrCol
;
7249 m_control
= control
;
7254 SetEventObject(obj
);
7258 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7260 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7261 const wxGridCellCoords
& topLeft
,
7262 const wxGridCellCoords
& bottomRight
,
7263 bool control
, bool shift
, bool alt
, bool meta
)
7264 : wxNotifyEvent( type
, id
)
7266 m_topLeft
= topLeft
;
7267 m_bottomRight
= bottomRight
;
7268 m_control
= control
;
7273 SetEventObject(obj
);
7277 #endif // ifndef wxUSE_NEW_GRID