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
260 wxGridDataTypeInfo(const wxString
& typeName
,
261 wxGridCellRenderer
* renderer
,
262 wxGridCellEditor
* editor
)
263 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
266 ~wxGridDataTypeInfo()
268 wxSafeDecRef(m_renderer
);
269 wxSafeDecRef(m_editor
);
273 wxGridCellRenderer
* m_renderer
;
274 wxGridCellEditor
* m_editor
;
278 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
281 class WXDLLEXPORT wxGridTypeRegistry
284 ~wxGridTypeRegistry();
286 void RegisterDataType(const wxString
& typeName
,
287 wxGridCellRenderer
* renderer
,
288 wxGridCellEditor
* editor
);
289 int FindDataType(const wxString
& typeName
);
290 wxGridCellRenderer
* GetRenderer(int index
);
291 wxGridCellEditor
* GetEditor(int index
);
294 wxGridDataTypeInfoArray m_typeinfo
;
297 // ----------------------------------------------------------------------------
298 // conditional compilation
299 // ----------------------------------------------------------------------------
301 #ifndef WXGRID_DRAW_LINES
302 #define WXGRID_DRAW_LINES 1
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 //#define DEBUG_ATTR_CACHE
310 #ifdef DEBUG_ATTR_CACHE
311 static size_t gs_nAttrCacheHits
= 0;
312 static size_t gs_nAttrCacheMisses
= 0;
313 #endif // DEBUG_ATTR_CACHE
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
320 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
323 // TODO: fixed so far - make configurable later (and also different for x/y)
324 static const size_t GRID_SCROLL_LINE
= 10;
326 // the size of hash tables used a bit everywhere (the max number of elements
327 // in these hash tables is the number of rows/columns)
328 static const int GRID_HASH_SIZE
= 100;
330 // ============================================================================
332 // ============================================================================
334 // ----------------------------------------------------------------------------
336 // ----------------------------------------------------------------------------
338 wxGridCellEditor
::wxGridCellEditor()
346 wxGridCellEditor
::~wxGridCellEditor()
351 void wxGridCellEditor
::Create(wxWindow
* WXUNUSED(parent
),
352 wxWindowID
WXUNUSED(id
),
353 wxEvtHandler
* evtHandler
)
356 m_control
->PushEventHandler(evtHandler
);
359 void wxGridCellEditor
::PaintBackground(const wxRect
& rectCell
,
360 wxGridCellAttr
*attr
)
362 // erase the background because we might not fill the cell
363 wxClientDC
dc(m_control
->GetParent());
364 dc
.SetPen(*wxTRANSPARENT_PEN
);
365 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
366 dc
.DrawRectangle(rectCell
);
368 // redraw the control we just painted over
369 m_control
->Refresh();
372 void wxGridCellEditor
::Destroy()
376 m_control
->PopEventHandler(TRUE
/* delete it*/);
378 m_control
->Destroy();
383 void wxGridCellEditor
::Show(bool show
, wxGridCellAttr
*attr
)
385 wxASSERT_MSG(m_control
,
386 wxT("The wxGridCellEditor must be Created first!"));
387 m_control
->Show(show
);
391 // set the colours/fonts if we have any
394 m_colFgOld
= m_control
->GetForegroundColour();
395 m_control
->SetForegroundColour(attr
->GetTextColour());
397 m_colBgOld
= m_control
->GetBackgroundColour();
398 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
400 m_fontOld
= m_control
->GetFont();
401 m_control
->SetFont(attr
->GetFont());
403 // can't do anything more in the base class version, the other
404 // attributes may only be used by the derived classes
409 // restore the standard colours fonts
410 if ( m_colFgOld
.Ok() )
412 m_control
->SetForegroundColour(m_colFgOld
);
413 m_colFgOld
= wxNullColour
;
416 if ( m_colBgOld
.Ok() )
418 m_control
->SetBackgroundColour(m_colBgOld
);
419 m_colBgOld
= wxNullColour
;
422 if ( m_fontOld
.Ok() )
424 m_control
->SetFont(m_fontOld
);
425 m_fontOld
= wxNullFont
;
430 void wxGridCellEditor
::SetSize(const wxRect
& rect
)
432 wxASSERT_MSG(m_control
,
433 wxT("The wxGridCellEditor must be Created first!"));
434 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
437 void wxGridCellEditor
::HandleReturn(wxKeyEvent
& event
)
443 void wxGridCellEditor
::StartingKey(wxKeyEvent
& event
)
448 void wxGridCellEditor
::StartingClick()
452 // ----------------------------------------------------------------------------
453 // wxGridCellTextEditor
454 // ----------------------------------------------------------------------------
456 wxGridCellTextEditor
::wxGridCellTextEditor()
460 void wxGridCellTextEditor
::Create(wxWindow
* parent
,
462 wxEvtHandler
* evtHandler
)
464 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
465 wxDefaultPosition
, wxDefaultSize
466 #if defined(__WXMSW__)
467 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
471 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
474 void wxGridCellTextEditor
::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
475 wxGridCellAttr
* WXUNUSED(attr
))
477 // as we fill the entire client area, don't do anything here to minimize
481 void wxGridCellTextEditor
::SetSize(const wxRect
& rectOrig
)
483 wxRect
rect(rectOrig
);
485 // Make the edit control large enough to allow for internal
488 // TODO: remove this if the text ctrl sizing is improved esp. for
491 #if defined(__WXGTK__)
500 int extra_x
= ( rect
.x
> 2 )?
2 : 1;
501 int extra_y
= ( rect
.y
> 2 )?
2 : 1;
502 #if defined(__WXMOTIF__)
506 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
507 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
508 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
509 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
512 wxGridCellEditor
::SetSize(rect
);
515 void wxGridCellTextEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
517 wxASSERT_MSG(m_control
,
518 wxT("The wxGridCellEditor must be Created first!"));
520 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
522 DoBeginEdit(m_startValue
);
525 void wxGridCellTextEditor
::DoBeginEdit(const wxString
& startValue
)
527 Text()->SetValue(startValue
);
528 Text()->SetInsertionPointEnd();
532 bool wxGridCellTextEditor
::EndEdit(int row
, int col
,
535 wxASSERT_MSG(m_control
,
536 wxT("The wxGridCellEditor must be Created first!"));
538 bool changed
= FALSE
;
539 wxString value
= Text()->GetValue();
540 if (value
!= m_startValue
)
544 grid
->GetTable()->SetValue(row
, col
, value
);
546 m_startValue
= wxEmptyString
;
547 Text()->SetValue(m_startValue
);
553 void wxGridCellTextEditor
::Reset()
555 wxASSERT_MSG(m_control
,
556 wxT("The wxGridCellEditor must be Created first!"));
558 DoReset(m_startValue
);
561 void wxGridCellTextEditor
::DoReset(const wxString
& startValue
)
563 Text()->SetValue(startValue
);
564 Text()->SetInsertionPointEnd();
567 void wxGridCellTextEditor
::StartingKey(wxKeyEvent
& event
)
569 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
571 // insert the key in the control
572 long keycode
= event
.KeyCode();
573 if ( isprint(keycode
) )
575 // FIXME this is not going to work for non letters...
576 if ( !event
.ShiftDown() )
578 keycode
= tolower(keycode
);
581 Text()->AppendText((wxChar
)keycode
);
591 void wxGridCellTextEditor
::HandleReturn(wxKeyEvent
& event
)
593 #if defined(__WXMOTIF__) || defined(__WXGTK__)
594 // wxMotif needs a little extra help...
595 int pos
= Text()->GetInsertionPoint();
596 wxString
s( Text()->GetValue() );
597 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
599 Text()->SetInsertionPoint( pos
);
601 // the other ports can handle a Return key press
607 // ----------------------------------------------------------------------------
608 // wxGridCellNumberEditor
609 // ----------------------------------------------------------------------------
611 wxGridCellNumberEditor
::wxGridCellNumberEditor(int min
, int max
)
617 void wxGridCellNumberEditor
::Create(wxWindow
* parent
,
619 wxEvtHandler
* evtHandler
)
623 // create a spin ctrl
624 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
625 wxDefaultPosition
, wxDefaultSize
,
629 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
633 // just a text control
634 wxGridCellTextEditor
::Create(parent
, id
, evtHandler
);
637 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
638 #endif // wxUSE_VALIDATORS
642 void wxGridCellNumberEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
644 // first get the value
645 wxGridTableBase
*table
= grid
->GetTable();
646 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
648 m_valueOld
= table
->GetValueAsLong(row
, col
);
652 wxString sValue
= table
->GetValue(row
, col
);
653 if (! sValue
.ToLong(&m_valueOld
))
655 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
662 Spin()->SetValue(m_valueOld
);
666 DoBeginEdit(GetString());
670 bool wxGridCellNumberEditor
::EndEdit(int row
, int col
,
678 value
= Spin()->GetValue();
679 changed
= value
!= m_valueOld
;
683 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
688 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
689 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
691 grid
->GetTable()->SetValue(row
, col
, wxString
::Format("%ld", value
));
697 void wxGridCellNumberEditor
::Reset()
701 Spin()->SetValue(m_valueOld
);
705 DoReset(GetString());
709 void wxGridCellNumberEditor
::StartingKey(wxKeyEvent
& event
)
713 long keycode
= event
.KeyCode();
714 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
716 wxGridCellTextEditor
::StartingKey(event
);
726 // ----------------------------------------------------------------------------
727 // wxGridCellFloatEditor
728 // ----------------------------------------------------------------------------
730 void wxGridCellFloatEditor
::Create(wxWindow
* parent
,
732 wxEvtHandler
* evtHandler
)
734 wxGridCellTextEditor
::Create(parent
, id
, evtHandler
);
737 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
738 #endif // wxUSE_VALIDATORS
741 void wxGridCellFloatEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
743 // first get the value
744 wxGridTableBase
*table
= grid
->GetTable();
745 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
747 m_valueOld
= table
->GetValueAsDouble(row
, col
);
751 wxString sValue
= table
->GetValue(row
, col
);
752 if (! sValue
.ToDouble(&m_valueOld
))
754 wxFAIL_MSG( _T("this cell doesn't have float value") );
759 DoBeginEdit(GetString());
762 bool wxGridCellFloatEditor
::EndEdit(int row
, int col
,
766 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
768 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
769 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
771 grid
->GetTable()->SetValue(row
, col
, wxString
::Format("%f", value
));
781 void wxGridCellFloatEditor
::Reset()
783 DoReset(GetString());
786 void wxGridCellFloatEditor
::StartingKey(wxKeyEvent
& event
)
788 long keycode
= event
.KeyCode();
789 if ( isdigit(keycode
) ||
790 keycode
== '+' || keycode
== '-' || keycode
== '.' )
792 wxGridCellTextEditor
::StartingKey(event
);
801 // ----------------------------------------------------------------------------
802 // wxGridCellBoolEditor
803 // ----------------------------------------------------------------------------
805 void wxGridCellBoolEditor
::Create(wxWindow
* parent
,
807 wxEvtHandler
* evtHandler
)
809 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
810 wxDefaultPosition
, wxDefaultSize
,
813 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
816 void wxGridCellBoolEditor
::SetSize(const wxRect
& r
)
819 wxSize size
= m_control
->GetSize();
820 wxCoord minSize
= wxMin(r
.width
, r
.height
);
822 // check if the checkbox is not too big/small for this cell
823 wxSize sizeBest
= m_control
->GetBestSize();
824 if ( !(size
== sizeBest
) )
826 // reset to default size if it had been made smaller
832 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
834 // leave 1 pixel margin
835 size
.x
= size
.y
= minSize
- 2;
842 m_control
->SetSize(size
);
845 // position it in the centre of the rectangle (TODO: support alignment?)
847 #if defined(__WXGTK__) || defined (__WXMOTIF__)
848 // the checkbox without label still has some space to the right in wxGTK,
849 // so shift it to the right
851 #elif defined(__WXMSW__)
857 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
860 void wxGridCellBoolEditor
::Show(bool show
, wxGridCellAttr
*attr
)
862 m_control
->Show(show
);
866 wxColour colBg
= attr ? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
867 CBox()->SetBackgroundColour(colBg
);
871 void wxGridCellBoolEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
873 wxASSERT_MSG(m_control
,
874 wxT("The wxGridCellEditor must be Created first!"));
876 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
877 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
879 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
880 CBox()->SetValue(m_startValue
);
884 bool wxGridCellBoolEditor
::EndEdit(int row
, int col
,
887 wxASSERT_MSG(m_control
,
888 wxT("The wxGridCellEditor must be Created first!"));
890 bool changed
= FALSE
;
891 bool value
= CBox()->GetValue();
892 if ( value
!= m_startValue
)
897 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
898 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
900 grid
->GetTable()->SetValue(row
, col
, value ?
_T("1") : wxEmptyString
);
906 void wxGridCellBoolEditor
::Reset()
908 wxASSERT_MSG(m_control
,
909 wxT("The wxGridCellEditor must be Created first!"));
911 CBox()->SetValue(m_startValue
);
914 void wxGridCellBoolEditor
::StartingClick()
916 CBox()->SetValue(!CBox()->GetValue());
919 // ----------------------------------------------------------------------------
920 // wxGridCellChoiceEditor
921 // ----------------------------------------------------------------------------
923 wxGridCellChoiceEditor
::wxGridCellChoiceEditor(size_t count
,
924 const wxChar
* choices
[],
926 : m_allowOthers(allowOthers
)
928 m_choices
.Alloc(count
);
929 for ( size_t n
= 0; n
< count
; n
++ )
931 m_choices
.Add(choices
[n
]);
935 void wxGridCellChoiceEditor
::Create(wxWindow
* parent
,
937 wxEvtHandler
* evtHandler
)
939 size_t count
= m_choices
.GetCount();
940 wxString
*choices
= new wxString
[count
];
941 for ( size_t n
= 0; n
< count
; n
++ )
943 choices
[n
] = m_choices
[n
];
946 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
947 wxDefaultPosition
, wxDefaultSize
,
949 m_allowOthers ?
0 : wxCB_READONLY
);
953 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
956 void wxGridCellChoiceEditor
::PaintBackground(const wxRect
& rectCell
,
957 wxGridCellAttr
* attr
)
959 // as we fill the entire client area, don't do anything here to minimize
962 // TODO: It doesn't actually fill the client area since the height of a
963 // combo always defaults to the standard... Until someone has time to
964 // figure out the right rectangle to paint, just do it the normal way...
965 wxGridCellEditor
::PaintBackground(rectCell
, attr
);
968 void wxGridCellChoiceEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
970 wxASSERT_MSG(m_control
,
971 wxT("The wxGridCellEditor must be Created first!"));
973 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
975 Combo()->SetValue(m_startValue
);
976 size_t count
= m_choices
.GetCount();
977 for (size_t i
=0; i
<count
; i
++)
979 if (m_startValue
== m_choices
[i
])
981 Combo()->SetSelection(i
);
985 Combo()->SetInsertionPointEnd();
989 bool wxGridCellChoiceEditor
::EndEdit(int row
, int col
,
992 wxString value
= Combo()->GetValue();
993 bool changed
= value
!= m_startValue
;
996 grid
->GetTable()->SetValue(row
, col
, value
);
998 m_startValue
= wxEmptyString
;
999 Combo()->SetValue(m_startValue
);
1004 void wxGridCellChoiceEditor
::Reset()
1006 Combo()->SetValue(m_startValue
);
1007 Combo()->SetInsertionPointEnd();
1010 // ----------------------------------------------------------------------------
1011 // wxGridCellEditorEvtHandler
1012 // ----------------------------------------------------------------------------
1014 void wxGridCellEditorEvtHandler
::OnKeyDown(wxKeyEvent
& event
)
1016 switch ( event
.KeyCode() )
1020 m_grid
->DisableCellEditControl();
1024 event
.Skip( m_grid
->ProcessEvent( event
) );
1028 if (!m_grid
->ProcessEvent(event
))
1029 m_editor
->HandleReturn(event
);
1038 void wxGridCellEditorEvtHandler
::OnChar(wxKeyEvent
& event
)
1040 switch ( event
.KeyCode() )
1052 // ============================================================================
1054 // ============================================================================
1056 // ----------------------------------------------------------------------------
1057 // wxGridCellRenderer
1058 // ----------------------------------------------------------------------------
1060 void wxGridCellRenderer
::Draw(wxGrid
& grid
,
1061 wxGridCellAttr
& attr
,
1067 dc
.SetBackgroundMode( wxSOLID
);
1071 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1075 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1078 dc
.SetPen( *wxTRANSPARENT_PEN
);
1079 dc
.DrawRectangle(rect
);
1082 void wxGridCellRenderer
::SetParameters(const wxString
& WXUNUSED(params
))
1087 wxGridCellRenderer
::~wxGridCellRenderer()
1091 // ----------------------------------------------------------------------------
1092 // wxGridCellStringRenderer
1093 // ----------------------------------------------------------------------------
1095 void wxGridCellStringRenderer
::SetTextColoursAndFont(wxGrid
& grid
,
1096 wxGridCellAttr
& attr
,
1100 dc
.SetBackgroundMode( wxTRANSPARENT
);
1102 // TODO some special colours for attr.IsReadOnly() case?
1106 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1107 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1111 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1112 dc
.SetTextForeground( attr
.GetTextColour() );
1115 dc
.SetFont( attr
.GetFont() );
1118 wxSize wxGridCellStringRenderer
::DoGetBestSize(wxGridCellAttr
& attr
,
1120 const wxString
& text
)
1123 dc
.SetFont(attr
.GetFont());
1124 dc
.GetTextExtent(text
, &x
, &y
);
1126 return wxSize(x
, y
);
1129 wxSize wxGridCellStringRenderer
::GetBestSize(wxGrid
& grid
,
1130 wxGridCellAttr
& attr
,
1134 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1137 void wxGridCellStringRenderer
::Draw(wxGrid
& grid
,
1138 wxGridCellAttr
& attr
,
1140 const wxRect
& rectCell
,
1144 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1146 // now we only have to draw the text
1147 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1150 attr
.GetAlignment(&hAlign
, &vAlign
);
1152 wxRect rect
= rectCell
;
1155 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1156 rect
, hAlign
, vAlign
);
1159 // ----------------------------------------------------------------------------
1160 // wxGridCellNumberRenderer
1161 // ----------------------------------------------------------------------------
1163 wxString wxGridCellNumberRenderer
::GetString(wxGrid
& grid
, int row
, int col
)
1165 wxGridTableBase
*table
= grid
.GetTable();
1167 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1169 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1173 text
= table
->GetValue(row
, col
);
1179 void wxGridCellNumberRenderer
::Draw(wxGrid
& grid
,
1180 wxGridCellAttr
& attr
,
1182 const wxRect
& rectCell
,
1186 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1188 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1190 // draw the text right aligned by default
1192 attr
.GetAlignment(&hAlign
, &vAlign
);
1195 wxRect rect
= rectCell
;
1198 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1201 wxSize wxGridCellNumberRenderer
::GetBestSize(wxGrid
& grid
,
1202 wxGridCellAttr
& attr
,
1206 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1209 // ----------------------------------------------------------------------------
1210 // wxGridCellFloatRenderer
1211 // ----------------------------------------------------------------------------
1213 wxGridCellFloatRenderer
::wxGridCellFloatRenderer(int width
, int precision
)
1216 SetPrecision(precision
);
1219 wxString wxGridCellFloatRenderer
::GetString(wxGrid
& grid
, int row
, int col
)
1221 wxGridTableBase
*table
= grid
.GetTable();
1226 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1228 val
= table
->GetValueAsDouble(row
, col
);
1233 text
= table
->GetValue(row
, col
);
1234 hasDouble
= text
.ToDouble(&val
);
1241 if ( m_width
== -1 )
1243 // default width/precision
1244 m_format
= _T("%f");
1246 else if ( m_precision
== -1 )
1248 // default precision
1249 m_format
.Printf(_T("%%%d.f"), m_width
);
1253 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1257 text
.Printf(m_format
, val
);
1259 //else: text already contains the string
1264 void wxGridCellFloatRenderer
::Draw(wxGrid
& grid
,
1265 wxGridCellAttr
& attr
,
1267 const wxRect
& rectCell
,
1271 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1273 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1275 // draw the text right aligned by default
1277 attr
.GetAlignment(&hAlign
, &vAlign
);
1280 wxRect rect
= rectCell
;
1283 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1286 wxSize wxGridCellFloatRenderer
::GetBestSize(wxGrid
& grid
,
1287 wxGridCellAttr
& attr
,
1291 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1294 void wxGridCellFloatRenderer
::SetParameters(const wxString
& params
)
1300 // reset to defaults
1306 wxString tmp
= params
.BeforeFirst(_T(','));
1310 if ( !tmp
.ToLong(&width
) )
1316 SetWidth((int)width
);
1318 tmp
= params
.AfterFirst(_T(','));
1322 if ( !tmp
.ToLong(&precision
) )
1328 SetPrecision((int)precision
);
1336 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string "
1337 "'%s ignored"), params
.c_str());
1342 // ----------------------------------------------------------------------------
1343 // wxGridCellBoolRenderer
1344 // ----------------------------------------------------------------------------
1346 wxSize wxGridCellBoolRenderer
::ms_sizeCheckMark
;
1348 // FIXME these checkbox size calculations are really ugly...
1350 // between checkmark and box
1352 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1354 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1357 wxSize wxGridCellBoolRenderer
::GetBestSize(wxGrid
& grid
,
1358 wxGridCellAttr
& WXUNUSED(attr
),
1363 // compute it only once (no locks for MT safeness in GUI thread...)
1364 if ( !ms_sizeCheckMark
.x
)
1366 // get checkbox size
1367 wxCoord checkSize
= 0;
1368 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1369 wxSize size
= checkbox
->GetBestSize();
1370 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1372 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1373 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1374 checkSize
-= size
.y
/ 2;
1379 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1382 return ms_sizeCheckMark
;
1385 void wxGridCellBoolRenderer
::Draw(wxGrid
& grid
,
1386 wxGridCellAttr
& attr
,
1392 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1394 // draw a check mark in the centre (ignoring alignment - TODO)
1395 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1397 // don't draw outside the cell
1398 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1399 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1401 // and even leave (at least) 1 pixel margin
1402 size
.x
= size
.y
= minSize
- 2;
1405 // draw a border around checkmark
1407 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1408 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1409 rectMark
.width
= size
.x
;
1410 rectMark
.height
= size
.y
;
1412 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1413 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1414 dc
.DrawRectangle(rectMark
);
1416 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1419 // looks nicer under MSW
1424 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1425 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1427 value
= !!grid
.GetTable()->GetValue(row
, col
);
1431 dc
.SetTextForeground(attr
.GetTextColour());
1432 dc
.DrawCheckMark(rectMark
);
1436 // ----------------------------------------------------------------------------
1438 // ----------------------------------------------------------------------------
1440 wxGridCellAttr
*wxGridCellAttr
::Clone() const
1442 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1443 if ( HasTextColour() )
1444 attr
->SetTextColour(GetTextColour());
1445 if ( HasBackgroundColour() )
1446 attr
->SetBackgroundColour(GetBackgroundColour());
1448 attr
->SetFont(GetFont());
1449 if ( HasAlignment() )
1450 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1454 attr
->SetRenderer(m_renderer
);
1455 m_renderer
->IncRef();
1459 attr
->SetEditor(m_editor
);
1464 attr
->SetReadOnly();
1466 attr
->SetDefAttr(m_defGridAttr
);
1471 const wxColour
& wxGridCellAttr
::GetTextColour() const
1473 if (HasTextColour())
1477 else if (m_defGridAttr
!= this)
1479 return m_defGridAttr
->GetTextColour();
1483 wxFAIL_MSG(wxT("Missing default cell attribute"));
1484 return wxNullColour
;
1489 const wxColour
& wxGridCellAttr
::GetBackgroundColour() const
1491 if (HasBackgroundColour())
1493 else if (m_defGridAttr
!= this)
1494 return m_defGridAttr
->GetBackgroundColour();
1497 wxFAIL_MSG(wxT("Missing default cell attribute"));
1498 return wxNullColour
;
1503 const wxFont
& wxGridCellAttr
::GetFont() const
1507 else if (m_defGridAttr
!= this)
1508 return m_defGridAttr
->GetFont();
1511 wxFAIL_MSG(wxT("Missing default cell attribute"));
1517 void wxGridCellAttr
::GetAlignment(int *hAlign
, int *vAlign
) const
1521 if ( hAlign
) *hAlign
= m_hAlign
;
1522 if ( vAlign
) *vAlign
= m_vAlign
;
1524 else if (m_defGridAttr
!= this)
1525 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1528 wxFAIL_MSG(wxT("Missing default cell attribute"));
1533 // GetRenderer and GetEditor use a slightly different decision path about
1534 // which attribute to use. If a non-default attr object has one then it is
1535 // used, otherwise the default editor or renderer is fetched from the grid and
1536 // used. It should be the default for the data type of the cell. If it is
1537 // NULL (because the table has a type that the grid does not have in its
1538 // registry,) then the grid's default editor or renderer is used.
1540 wxGridCellRenderer
* wxGridCellAttr
::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1542 wxGridCellRenderer
* renderer
= NULL
;
1544 if ( m_defGridAttr
!= this || grid
== NULL
)
1546 renderer
= m_renderer
; // use local attribute
1551 if ( !renderer
&& grid
) // get renderer for the data type
1553 // GetDefaultRendererForCell() will do IncRef() for us
1554 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1559 // if we still don't have one then use the grid default
1560 // (no need for IncRef() here neither)
1561 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1566 wxFAIL_MSG(wxT("Missing default cell attribute"));
1572 wxGridCellEditor
* wxGridCellAttr
::GetEditor(wxGrid
* grid
, int row
, int col
) const
1574 wxGridCellEditor
* editor
= NULL
;
1576 if ( m_defGridAttr
!= this || grid
== NULL
)
1578 editor
= m_editor
; // use local attribute
1583 if ( grid
) // get renderer for the data type
1584 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1587 // if we still don't have one then use the grid default
1588 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1592 wxFAIL_MSG(wxT("Missing default cell attribute"));
1598 // ----------------------------------------------------------------------------
1599 // wxGridCellAttrData
1600 // ----------------------------------------------------------------------------
1602 void wxGridCellAttrData
::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1604 int n
= FindIndex(row
, col
);
1605 if ( n
== wxNOT_FOUND
)
1607 // add the attribute
1608 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1614 // change the attribute
1615 m_attrs
[(size_t)n
].attr
= attr
;
1619 // remove this attribute
1620 m_attrs
.RemoveAt((size_t)n
);
1625 wxGridCellAttr
*wxGridCellAttrData
::GetAttr(int row
, int col
) const
1627 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1629 int n
= FindIndex(row
, col
);
1630 if ( n
!= wxNOT_FOUND
)
1632 attr
= m_attrs
[(size_t)n
].attr
;
1639 void wxGridCellAttrData
::UpdateAttrRows( size_t pos
, int numRows
)
1641 size_t count
= m_attrs
.GetCount();
1642 for ( size_t n
= 0; n
< count
; n
++ )
1644 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1645 wxCoord row
= coords
.GetRow();
1646 if ((size_t)row
>= pos
)
1650 // If rows inserted, include row counter where necessary
1651 coords
.SetRow(row
+ numRows
);
1653 else if (numRows
< 0)
1655 // If rows deleted ...
1656 if ((size_t)row
>= pos
- numRows
)
1658 // ...either decrement row counter (if row still exists)...
1659 coords
.SetRow(row
+ numRows
);
1663 // ...or remove the attribute
1664 m_attrs
.RemoveAt((size_t)n
);
1672 void wxGridCellAttrData
::UpdateAttrCols( size_t pos
, int numCols
)
1674 size_t count
= m_attrs
.GetCount();
1675 for ( size_t n
= 0; n
< count
; n
++ )
1677 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1678 wxCoord col
= coords
.GetCol();
1679 if ( (size_t)col
>= pos
)
1683 // If rows inserted, include row counter where necessary
1684 coords
.SetCol(col
+ numCols
);
1686 else if (numCols
< 0)
1688 // If rows deleted ...
1689 if ((size_t)col
>= pos
- numCols
)
1691 // ...either decrement row counter (if row still exists)...
1692 coords
.SetCol(col
+ numCols
);
1696 // ...or remove the attribute
1697 m_attrs
.RemoveAt((size_t)n
);
1705 int wxGridCellAttrData
::FindIndex(int row
, int col
) const
1707 size_t count
= m_attrs
.GetCount();
1708 for ( size_t n
= 0; n
< count
; n
++ )
1710 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1711 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1720 // ----------------------------------------------------------------------------
1721 // wxGridRowOrColAttrData
1722 // ----------------------------------------------------------------------------
1724 wxGridRowOrColAttrData
::~wxGridRowOrColAttrData()
1726 size_t count
= m_attrs
.Count();
1727 for ( size_t n
= 0; n
< count
; n
++ )
1729 m_attrs
[n
]->DecRef();
1733 wxGridCellAttr
*wxGridRowOrColAttrData
::GetAttr(int rowOrCol
) const
1735 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1737 int n
= m_rowsOrCols
.Index(rowOrCol
);
1738 if ( n
!= wxNOT_FOUND
)
1740 attr
= m_attrs
[(size_t)n
];
1747 void wxGridRowOrColAttrData
::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1749 int n
= m_rowsOrCols
.Index(rowOrCol
);
1750 if ( n
== wxNOT_FOUND
)
1752 // add the attribute
1753 m_rowsOrCols
.Add(rowOrCol
);
1760 // change the attribute
1761 m_attrs
[(size_t)n
] = attr
;
1765 // remove this attribute
1766 m_attrs
[(size_t)n
]->DecRef();
1767 m_rowsOrCols
.RemoveAt((size_t)n
);
1768 m_attrs
.RemoveAt((size_t)n
);
1773 void wxGridRowOrColAttrData
::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1775 size_t count
= m_attrs
.GetCount();
1776 for ( size_t n
= 0; n
< count
; n
++ )
1778 int & rowOrCol
= m_rowsOrCols
[n
];
1779 if ( (size_t)rowOrCol
>= pos
)
1781 if ( numRowsOrCols
> 0 )
1783 // If rows inserted, include row counter where necessary
1784 rowOrCol
+= numRowsOrCols
;
1786 else if ( numRowsOrCols
< 0)
1788 // If rows deleted, either decrement row counter (if row still exists)
1789 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1790 rowOrCol
+= numRowsOrCols
;
1793 m_rowsOrCols
.RemoveAt((size_t)n
);
1794 m_attrs
.RemoveAt((size_t)n
);
1802 // ----------------------------------------------------------------------------
1803 // wxGridCellAttrProvider
1804 // ----------------------------------------------------------------------------
1806 wxGridCellAttrProvider
::wxGridCellAttrProvider()
1808 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1811 wxGridCellAttrProvider
::~wxGridCellAttrProvider()
1816 void wxGridCellAttrProvider
::InitData()
1818 m_data
= new wxGridCellAttrProviderData
;
1821 wxGridCellAttr
*wxGridCellAttrProvider
::GetAttr(int row
, int col
) const
1823 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1826 // first look for the attribute of this specific cell
1827 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1831 // then look for the col attr (col attributes are more common than
1832 // the row ones, hence they have priority)
1833 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1838 // finally try the row attributes
1839 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1846 void wxGridCellAttrProvider
::SetAttr(wxGridCellAttr
*attr
,
1852 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1855 void wxGridCellAttrProvider
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1860 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1863 void wxGridCellAttrProvider
::SetColAttr(wxGridCellAttr
*attr
, int col
)
1868 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1871 void wxGridCellAttrProvider
::UpdateAttrRows( size_t pos
, int numRows
)
1875 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1877 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1881 void wxGridCellAttrProvider
::UpdateAttrCols( size_t pos
, int numCols
)
1885 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1887 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1891 // ----------------------------------------------------------------------------
1892 // wxGridTypeRegistry
1893 // ----------------------------------------------------------------------------
1895 wxGridTypeRegistry
::~wxGridTypeRegistry()
1897 size_t count
= m_typeinfo
.Count();
1898 for ( size_t i
= 0; i
< count
; i
++ )
1899 delete m_typeinfo
[i
];
1903 void wxGridTypeRegistry
::RegisterDataType(const wxString
& typeName
,
1904 wxGridCellRenderer
* renderer
,
1905 wxGridCellEditor
* editor
)
1907 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1909 // is it already registered?
1910 int loc
= FindDataType(typeName
);
1911 if ( loc
!= wxNOT_FOUND
)
1913 delete m_typeinfo
[loc
];
1914 m_typeinfo
[loc
] = info
;
1918 m_typeinfo
.Add(info
);
1922 int wxGridTypeRegistry
::FindDataType(const wxString
& typeName
)
1926 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1927 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1936 wxGridCellRenderer
* wxGridTypeRegistry
::GetRenderer(int index
)
1938 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1943 wxGridCellEditor
* wxGridTypeRegistry
::GetEditor(int index
)
1945 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1950 // ----------------------------------------------------------------------------
1952 // ----------------------------------------------------------------------------
1954 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1957 wxGridTableBase
::wxGridTableBase()
1959 m_view
= (wxGrid
*) NULL
;
1960 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1963 wxGridTableBase
::~wxGridTableBase()
1965 delete m_attrProvider
;
1968 void wxGridTableBase
::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1970 delete m_attrProvider
;
1971 m_attrProvider
= attrProvider
;
1974 bool wxGridTableBase
::CanHaveAttributes()
1976 if ( ! GetAttrProvider() )
1978 // use the default attr provider by default
1979 SetAttrProvider(new wxGridCellAttrProvider
);
1984 wxGridCellAttr
*wxGridTableBase
::GetAttr(int row
, int col
)
1986 if ( m_attrProvider
)
1987 return m_attrProvider
->GetAttr(row
, col
);
1989 return (wxGridCellAttr
*)NULL
;
1992 void wxGridTableBase
::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1994 if ( m_attrProvider
)
1996 m_attrProvider
->SetAttr(attr
, row
, col
);
2000 // as we take ownership of the pointer and don't store it, we must
2006 void wxGridTableBase
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2008 if ( m_attrProvider
)
2010 m_attrProvider
->SetRowAttr(attr
, row
);
2014 // as we take ownership of the pointer and don't store it, we must
2020 void wxGridTableBase
::SetColAttr(wxGridCellAttr
*attr
, int col
)
2022 if ( m_attrProvider
)
2024 m_attrProvider
->SetColAttr(attr
, col
);
2028 // as we take ownership of the pointer and don't store it, we must
2034 void wxGridTableBase
::UpdateAttrRows( size_t pos
, int numRows
)
2036 if ( m_attrProvider
)
2038 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
2042 void wxGridTableBase
::UpdateAttrCols( size_t pos
, int numCols
)
2044 if ( m_attrProvider
)
2046 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
2050 bool wxGridTableBase
::InsertRows( size_t pos
, size_t numRows
)
2052 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
2053 "but your derived table class does not override this function") );
2058 bool wxGridTableBase
::AppendRows( size_t numRows
)
2060 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
2061 "but your derived table class does not override this function"));
2066 bool wxGridTableBase
::DeleteRows( size_t pos
, size_t numRows
)
2068 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
2069 "but your derived table class does not override this function"));
2074 bool wxGridTableBase
::InsertCols( size_t pos
, size_t numCols
)
2076 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
2077 "but your derived table class does not override this function"));
2082 bool wxGridTableBase
::AppendCols( size_t numCols
)
2084 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
2085 "but your derived table class does not override this function"));
2090 bool wxGridTableBase
::DeleteCols( size_t pos
, size_t numCols
)
2092 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
2093 "but your derived table class does not override this function"));
2099 wxString wxGridTableBase
::GetRowLabelValue( int row
)
2102 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2103 // how much it makes sense to us geeks.
2107 wxString wxGridTableBase
::GetColLabelValue( int col
)
2109 // default col labels are:
2110 // cols 0 to 25 : A-Z
2111 // cols 26 to 675 : AA-ZZ
2116 for ( n
= 1; ; n
++ )
2118 s
+= (_T('A') + (wxChar
)( col
%26 ));
2120 if ( col
< 0 ) break;
2123 // reverse the string...
2125 for ( i
= 0; i
< n
; i
++ )
2134 wxString wxGridTableBase
::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2136 return wxGRID_VALUE_STRING
;
2139 bool wxGridTableBase
::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2140 const wxString
& typeName
)
2142 return typeName
== wxGRID_VALUE_STRING
;
2145 bool wxGridTableBase
::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2147 return CanGetValueAs(row
, col
, typeName
);
2150 long wxGridTableBase
::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2155 double wxGridTableBase
::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2160 bool wxGridTableBase
::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2165 void wxGridTableBase
::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2166 long WXUNUSED(value
) )
2170 void wxGridTableBase
::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2171 double WXUNUSED(value
) )
2175 void wxGridTableBase
::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2176 bool WXUNUSED(value
) )
2181 void* wxGridTableBase
::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2182 const wxString
& WXUNUSED(typeName
) )
2187 void wxGridTableBase
::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2188 const wxString
& WXUNUSED(typeName
),
2189 void* WXUNUSED(value
) )
2193 //////////////////////////////////////////////////////////////////////
2195 // Message class for the grid table to send requests and notifications
2199 wxGridTableMessage
::wxGridTableMessage()
2201 m_table
= (wxGridTableBase
*) NULL
;
2207 wxGridTableMessage
::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2208 int commandInt1
, int commandInt2
)
2212 m_comInt1
= commandInt1
;
2213 m_comInt2
= commandInt2
;
2218 //////////////////////////////////////////////////////////////////////
2220 // A basic grid table for string data. An object of this class will
2221 // created by wxGrid if you don't specify an alternative table class.
2224 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2226 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2228 wxGridStringTable
::wxGridStringTable()
2233 wxGridStringTable
::wxGridStringTable( int numRows
, int numCols
)
2238 m_data
.Alloc( numRows
);
2241 sa
.Alloc( numCols
);
2242 for ( col
= 0; col
< numCols
; col
++ )
2244 sa
.Add( wxEmptyString
);
2247 for ( row
= 0; row
< numRows
; row
++ )
2253 wxGridStringTable
::~wxGridStringTable()
2257 long wxGridStringTable
::GetNumberRows()
2259 return m_data
.GetCount();
2262 long wxGridStringTable
::GetNumberCols()
2264 if ( m_data
.GetCount() > 0 )
2265 return m_data
[0].GetCount();
2270 wxString wxGridStringTable
::GetValue( int row
, int col
)
2272 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2273 _T("invalid row or column index in wxGridStringTable") );
2275 return m_data
[row
][col
];
2278 void wxGridStringTable
::SetValue( int row
, int col
, const wxString
& value
)
2280 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2281 _T("invalid row or column index in wxGridStringTable") );
2283 m_data
[row
][col
] = value
;
2286 bool wxGridStringTable
::IsEmptyCell( int row
, int col
)
2288 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2289 _T("invalid row or column index in wxGridStringTable") );
2291 return (m_data
[row
][col
] == wxEmptyString
);
2294 void wxGridStringTable
::Clear()
2297 int numRows
, numCols
;
2299 numRows
= m_data
.GetCount();
2302 numCols
= m_data
[0].GetCount();
2304 for ( row
= 0; row
< numRows
; row
++ )
2306 for ( col
= 0; col
< numCols
; col
++ )
2308 m_data
[row
][col
] = wxEmptyString
;
2315 bool wxGridStringTable
::InsertRows( size_t pos
, size_t numRows
)
2319 size_t curNumRows
= m_data
.GetCount();
2320 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2322 if ( pos
>= curNumRows
)
2324 return AppendRows( numRows
);
2328 sa
.Alloc( curNumCols
);
2329 for ( col
= 0; col
< curNumCols
; col
++ )
2331 sa
.Add( wxEmptyString
);
2334 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2336 m_data
.Insert( sa
, row
);
2338 UpdateAttrRows( pos
, numRows
);
2341 wxGridTableMessage
msg( this,
2342 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2346 GetView()->ProcessTableMessage( msg
);
2352 bool wxGridStringTable
::AppendRows( size_t numRows
)
2356 size_t curNumRows
= m_data
.GetCount();
2357 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2360 if ( curNumCols
> 0 )
2362 sa
.Alloc( curNumCols
);
2363 for ( col
= 0; col
< curNumCols
; col
++ )
2365 sa
.Add( wxEmptyString
);
2369 for ( row
= 0; row
< numRows
; row
++ )
2376 wxGridTableMessage
msg( this,
2377 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2380 GetView()->ProcessTableMessage( msg
);
2386 bool wxGridStringTable
::DeleteRows( size_t pos
, size_t numRows
)
2390 size_t curNumRows
= m_data
.GetCount();
2392 if ( pos
>= curNumRows
)
2395 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2396 "Pos value is invalid for present table with %d rows",
2397 pos
, numRows
, curNumRows
);
2398 wxFAIL_MSG( wxT(errmsg
) );
2402 if ( numRows
> curNumRows
- pos
)
2404 numRows
= curNumRows
- pos
;
2407 if ( numRows
>= curNumRows
)
2409 m_data
.Empty(); // don't release memory just yet
2413 for ( n
= 0; n
< numRows
; n
++ )
2415 m_data
.Remove( pos
);
2418 UpdateAttrRows( pos
, -((int)numRows
) );
2421 wxGridTableMessage
msg( this,
2422 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2426 GetView()->ProcessTableMessage( msg
);
2432 bool wxGridStringTable
::InsertCols( size_t pos
, size_t numCols
)
2436 size_t curNumRows
= m_data
.GetCount();
2437 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2439 if ( pos
>= curNumCols
)
2441 return AppendCols( numCols
);
2444 for ( row
= 0; row
< curNumRows
; row
++ )
2446 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2448 m_data
[row
].Insert( wxEmptyString
, col
);
2451 UpdateAttrCols( pos
, numCols
);
2454 wxGridTableMessage
msg( this,
2455 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2459 GetView()->ProcessTableMessage( msg
);
2465 bool wxGridStringTable
::AppendCols( size_t numCols
)
2469 size_t curNumRows
= m_data
.GetCount();
2472 // TODO: something better than this ?
2474 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2475 "Call AppendRows() first") );
2479 for ( row
= 0; row
< curNumRows
; row
++ )
2481 for ( n
= 0; n
< numCols
; n
++ )
2483 m_data
[row
].Add( wxEmptyString
);
2489 wxGridTableMessage
msg( this,
2490 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2493 GetView()->ProcessTableMessage( msg
);
2499 bool wxGridStringTable
::DeleteCols( size_t pos
, size_t numCols
)
2503 size_t curNumRows
= m_data
.GetCount();
2504 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2506 if ( pos
>= curNumCols
)
2509 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2510 "Pos value is invalid for present table with %d cols",
2511 pos
, numCols
, curNumCols
);
2512 wxFAIL_MSG( wxT( errmsg
) );
2516 if ( numCols
> curNumCols
- pos
)
2518 numCols
= curNumCols
- pos
;
2521 for ( row
= 0; row
< curNumRows
; row
++ )
2523 if ( numCols
>= curNumCols
)
2525 m_data
[row
].Clear();
2529 for ( n
= 0; n
< numCols
; n
++ )
2531 m_data
[row
].Remove( pos
);
2535 UpdateAttrCols( pos
, -((int)numCols
) );
2538 wxGridTableMessage
msg( this,
2539 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2543 GetView()->ProcessTableMessage( msg
);
2549 wxString wxGridStringTable
::GetRowLabelValue( int row
)
2551 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2553 // using default label
2555 return wxGridTableBase
::GetRowLabelValue( row
);
2559 return m_rowLabels
[ row
];
2563 wxString wxGridStringTable
::GetColLabelValue( int col
)
2565 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2567 // using default label
2569 return wxGridTableBase
::GetColLabelValue( col
);
2573 return m_colLabels
[ col
];
2577 void wxGridStringTable
::SetRowLabelValue( int row
, const wxString
& value
)
2579 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2581 int n
= m_rowLabels
.GetCount();
2583 for ( i
= n
; i
<= row
; i
++ )
2585 m_rowLabels
.Add( wxGridTableBase
::GetRowLabelValue(i
) );
2589 m_rowLabels
[row
] = value
;
2592 void wxGridStringTable
::SetColLabelValue( int col
, const wxString
& value
)
2594 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2596 int n
= m_colLabels
.GetCount();
2598 for ( i
= n
; i
<= col
; i
++ )
2600 m_colLabels
.Add( wxGridTableBase
::GetColLabelValue(i
) );
2604 m_colLabels
[col
] = value
;
2609 //////////////////////////////////////////////////////////////////////
2610 //////////////////////////////////////////////////////////////////////
2612 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2614 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2615 EVT_PAINT( wxGridRowLabelWindow
::OnPaint
)
2616 EVT_MOUSE_EVENTS( wxGridRowLabelWindow
::OnMouseEvent
)
2617 EVT_KEY_DOWN( wxGridRowLabelWindow
::OnKeyDown
)
2620 wxGridRowLabelWindow
::wxGridRowLabelWindow( wxGrid
*parent
,
2622 const wxPoint
&pos
, const wxSize
&size
)
2623 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2628 void wxGridRowLabelWindow
::OnPaint( wxPaintEvent
&event
)
2632 // NO - don't do this because it will set both the x and y origin
2633 // coords to match the parent scrolled window and we just want to
2634 // set the y coord - MB
2636 // m_owner->PrepareDC( dc );
2639 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2640 dc
.SetDeviceOrigin( 0, -y
);
2642 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2643 m_owner
->DrawRowLabels( dc
);
2647 void wxGridRowLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
2649 m_owner
->ProcessRowLabelMouseEvent( event
);
2653 // This seems to be required for wxMotif otherwise the mouse
2654 // cursor must be in the cell edit control to get key events
2656 void wxGridRowLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
2658 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2663 //////////////////////////////////////////////////////////////////////
2665 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2667 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2668 EVT_PAINT( wxGridColLabelWindow
::OnPaint
)
2669 EVT_MOUSE_EVENTS( wxGridColLabelWindow
::OnMouseEvent
)
2670 EVT_KEY_DOWN( wxGridColLabelWindow
::OnKeyDown
)
2673 wxGridColLabelWindow
::wxGridColLabelWindow( wxGrid
*parent
,
2675 const wxPoint
&pos
, const wxSize
&size
)
2676 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2681 void wxGridColLabelWindow
::OnPaint( wxPaintEvent
&event
)
2685 // NO - don't do this because it will set both the x and y origin
2686 // coords to match the parent scrolled window and we just want to
2687 // set the x coord - MB
2689 // m_owner->PrepareDC( dc );
2692 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2693 dc
.SetDeviceOrigin( -x
, 0 );
2695 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2696 m_owner
->DrawColLabels( dc
);
2700 void wxGridColLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
2702 m_owner
->ProcessColLabelMouseEvent( event
);
2706 // This seems to be required for wxMotif otherwise the mouse
2707 // cursor must be in the cell edit control to get key events
2709 void wxGridColLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
2711 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2716 //////////////////////////////////////////////////////////////////////
2718 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2720 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2721 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow
::OnMouseEvent
)
2722 EVT_PAINT( wxGridCornerLabelWindow
::OnPaint
)
2723 EVT_KEY_DOWN( wxGridCornerLabelWindow
::OnKeyDown
)
2726 wxGridCornerLabelWindow
::wxGridCornerLabelWindow( wxGrid
*parent
,
2728 const wxPoint
&pos
, const wxSize
&size
)
2729 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2734 void wxGridCornerLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2738 int client_height
= 0;
2739 int client_width
= 0;
2740 GetClientSize( &client_width
, &client_height
);
2742 dc
.SetPen( *wxBLACK_PEN
);
2743 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2744 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2746 dc
.SetPen( *wxWHITE_PEN
);
2747 dc
.DrawLine( 0, 0, client_width
, 0 );
2748 dc
.DrawLine( 0, 0, 0, client_height
);
2752 void wxGridCornerLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
2754 m_owner
->ProcessCornerLabelMouseEvent( event
);
2758 // This seems to be required for wxMotif otherwise the mouse
2759 // cursor must be in the cell edit control to get key events
2761 void wxGridCornerLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
2763 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2768 //////////////////////////////////////////////////////////////////////
2770 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2772 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2773 EVT_PAINT( wxGridWindow
::OnPaint
)
2774 EVT_MOUSE_EVENTS( wxGridWindow
::OnMouseEvent
)
2775 EVT_KEY_DOWN( wxGridWindow
::OnKeyDown
)
2776 EVT_ERASE_BACKGROUND( wxGridWindow
::OnEraseBackground
)
2779 wxGridWindow
::wxGridWindow( wxGrid
*parent
,
2780 wxGridRowLabelWindow
*rowLblWin
,
2781 wxGridColLabelWindow
*colLblWin
,
2782 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2783 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
2786 m_rowLabelWin
= rowLblWin
;
2787 m_colLabelWin
= colLblWin
;
2788 SetBackgroundColour( "WHITE" );
2792 wxGridWindow
::~wxGridWindow()
2797 void wxGridWindow
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2799 wxPaintDC
dc( this );
2800 m_owner
->PrepareDC( dc
);
2801 wxRegion reg
= GetUpdateRegion();
2802 m_owner
->CalcCellsExposed( reg
);
2803 m_owner
->DrawGridCellArea( dc
);
2804 #if WXGRID_DRAW_LINES
2805 m_owner
->DrawAllGridLines( dc
, reg
);
2807 m_owner
->DrawGridSpace( dc
);
2808 m_owner
->DrawHighlight( dc
);
2812 void wxGridWindow
::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2814 wxPanel
::ScrollWindow( dx
, dy
, rect
);
2815 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2816 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2820 void wxGridWindow
::OnMouseEvent( wxMouseEvent
& event
)
2822 m_owner
->ProcessGridCellMouseEvent( event
);
2826 // This seems to be required for wxMotif otherwise the mouse
2827 // cursor must be in the cell edit control to get key events
2829 void wxGridWindow
::OnKeyDown( wxKeyEvent
& event
)
2831 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2835 void wxGridWindow
::OnEraseBackground(wxEraseEvent
& event
)
2840 //////////////////////////////////////////////////////////////////////
2843 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2845 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2846 EVT_PAINT( wxGrid
::OnPaint
)
2847 EVT_SIZE( wxGrid
::OnSize
)
2848 EVT_KEY_DOWN( wxGrid
::OnKeyDown
)
2849 EVT_ERASE_BACKGROUND( wxGrid
::OnEraseBackground
)
2852 wxGrid
::wxGrid( wxWindow
*parent
,
2857 const wxString
& name
)
2858 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
2859 m_colMinWidths(GRID_HASH_SIZE
),
2860 m_rowMinHeights(GRID_HASH_SIZE
)
2869 wxSafeDecRef(m_defaultCellAttr
);
2871 #ifdef DEBUG_ATTR_CACHE
2872 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2873 wxPrintf(_T("wxGrid attribute cache statistics: "
2874 "total: %u, hits: %u (%u%%)\n"),
2875 total
, gs_nAttrCacheHits
,
2876 total ?
(gs_nAttrCacheHits
*100) / total
: 0);
2882 delete m_typeRegistry
;
2887 // ----- internal init and update functions
2890 void wxGrid
::Create()
2892 m_created
= FALSE
; // set to TRUE by CreateGrid
2893 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2895 m_table
= (wxGridTableBase
*) NULL
;
2898 m_cellEditCtrlEnabled
= FALSE
;
2900 m_defaultCellAttr
= new wxGridCellAttr
;
2901 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2903 // Set default cell attributes
2904 m_defaultCellAttr
->SetFont(GetFont());
2905 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2906 m_defaultCellAttr
->SetTextColour(
2907 wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2908 m_defaultCellAttr
->SetBackgroundColour(
2909 wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2910 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2911 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2916 m_currentCellCoords
= wxGridNoCellCoords
;
2918 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2919 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2921 // data type registration: register all standard data types
2922 // TODO: may be allow the app to selectively disable some of them?
2923 m_typeRegistry
= new wxGridTypeRegistry
;
2924 RegisterDataType(wxGRID_VALUE_STRING
,
2925 new wxGridCellStringRenderer
,
2926 new wxGridCellTextEditor
);
2927 RegisterDataType(wxGRID_VALUE_BOOL
,
2928 new wxGridCellBoolRenderer
,
2929 new wxGridCellBoolEditor
);
2930 RegisterDataType(wxGRID_VALUE_NUMBER
,
2931 new wxGridCellNumberRenderer
,
2932 new wxGridCellNumberEditor
);
2933 RegisterDataType(wxGRID_VALUE_FLOAT
,
2934 new wxGridCellFloatRenderer
,
2935 new wxGridCellFloatEditor
);
2937 // subwindow components that make up the wxGrid
2938 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2943 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2948 m_colLabelWin
= new wxGridColLabelWindow( this,
2953 m_gridWin
= new wxGridWindow( this,
2960 SetTargetWindow( m_gridWin
);
2964 bool wxGrid
::CreateGrid( int numRows
, int numCols
)
2968 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2973 m_numRows
= numRows
;
2974 m_numCols
= numCols
;
2976 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2977 m_table
->SetView( this );
2986 bool wxGrid
::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2990 // RD: Actually, this should probably be allowed. I think it would be
2991 // nice to be able to switch multiple Tables in and out of a single
2992 // View at runtime. Is there anything in the implmentation that would
2995 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3000 m_numRows
= table
->GetNumberRows();
3001 m_numCols
= table
->GetNumberCols();
3004 m_table
->SetView( this );
3017 if ( m_numRows
<= 0 )
3018 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
3020 if ( m_numCols
<= 0 )
3021 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
3023 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3024 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3026 if ( m_rowLabelWin
)
3028 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3032 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3035 m_labelTextColour
= wxColour( _T("BLACK") );
3038 m_attrCache
.row
= -1;
3040 // TODO: something better than this ?
3042 m_labelFont
= this->GetFont();
3043 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3045 m_rowLabelHorizAlign
= wxLEFT
;
3046 m_rowLabelVertAlign
= wxCENTRE
;
3048 m_colLabelHorizAlign
= wxCENTRE
;
3049 m_colLabelVertAlign
= wxTOP
;
3051 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3052 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3054 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3055 m_defaultRowHeight
+= 8;
3057 m_defaultRowHeight
+= 4;
3060 m_gridLineColour
= wxColour( 128, 128, 255 );
3061 m_gridLinesEnabled
= TRUE
;
3063 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3064 m_winCapture
= (wxWindow
*)NULL
;
3065 m_canDragRowSize
= TRUE
;
3066 m_canDragColSize
= TRUE
;
3067 m_canDragGridSize
= TRUE
;
3069 m_dragRowOrCol
= -1;
3070 m_isDragging
= FALSE
;
3071 m_startDragPos
= wxDefaultPosition
;
3073 m_waitForSlowClick
= FALSE
;
3075 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3076 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3078 m_currentCellCoords
= wxGridNoCellCoords
;
3080 m_selectedTopLeft
= wxGridNoCellCoords
;
3081 m_selectedBottomRight
= wxGridNoCellCoords
;
3082 m_selectionBackground
= wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3083 m_selectionForeground
= wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3085 m_editable
= TRUE
; // default for whole grid
3087 m_inOnKeyDown
= FALSE
;
3096 // ----------------------------------------------------------------------------
3097 // the idea is to call these functions only when necessary because they create
3098 // quite big arrays which eat memory mostly unnecessary - in particular, if
3099 // default widths/heights are used for all rows/columns, we may not use these
3102 // with some extra code, it should be possible to only store the
3103 // widths/heights different from default ones but this will be done later...
3104 // ----------------------------------------------------------------------------
3106 void wxGrid
::InitRowHeights()
3108 m_rowHeights
.Empty();
3109 m_rowBottoms
.Empty();
3111 m_rowHeights
.Alloc( m_numRows
);
3112 m_rowBottoms
.Alloc( m_numRows
);
3115 for ( int i
= 0; i
< m_numRows
; i
++ )
3117 m_rowHeights
.Add( m_defaultRowHeight
);
3118 rowBottom
+= m_defaultRowHeight
;
3119 m_rowBottoms
.Add( rowBottom
);
3123 void wxGrid
::InitColWidths()
3125 m_colWidths
.Empty();
3126 m_colRights
.Empty();
3128 m_colWidths
.Alloc( m_numCols
);
3129 m_colRights
.Alloc( m_numCols
);
3131 for ( int i
= 0; i
< m_numCols
; i
++ )
3133 m_colWidths
.Add( m_defaultColWidth
);
3134 colRight
+= m_defaultColWidth
;
3135 m_colRights
.Add( colRight
);
3139 int wxGrid
::GetColWidth(int col
) const
3141 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3144 int wxGrid
::GetColLeft(int col
) const
3146 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3147 : m_colRights
[col
] - m_colWidths
[col
];
3150 int wxGrid
::GetColRight(int col
) const
3152 return m_colRights
.IsEmpty() ?
(col
+ 1) * m_defaultColWidth
3156 int wxGrid
::GetRowHeight(int row
) const
3158 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3161 int wxGrid
::GetRowTop(int row
) const
3163 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3164 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3167 int wxGrid
::GetRowBottom(int row
) const
3169 return m_rowBottoms
.IsEmpty() ?
(row
+ 1) * m_defaultRowHeight
3170 : m_rowBottoms
[row
];
3173 void wxGrid
::CalcDimensions()
3176 GetClientSize( &cw
, &ch
);
3178 if ( m_numRows
> 0 && m_numCols
> 0 )
3180 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3181 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3183 // TODO: restore the scroll position that we had before sizing
3186 GetViewStart( &x
, &y
);
3187 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3188 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3194 void wxGrid
::CalcWindowSizes()
3197 GetClientSize( &cw
, &ch
);
3199 if ( m_cornerLabelWin
->IsShown() )
3200 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3202 if ( m_colLabelWin
->IsShown() )
3203 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3205 if ( m_rowLabelWin
->IsShown() )
3206 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3208 if ( m_gridWin
->IsShown() )
3209 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3213 // this is called when the grid table sends a message to say that it
3214 // has been redimensioned
3216 bool wxGrid
::Redimension( wxGridTableMessage
& msg
)
3220 // if we were using the default widths/heights so far, we must change them
3222 if ( m_colWidths
.IsEmpty() )
3227 if ( m_rowHeights
.IsEmpty() )
3232 switch ( msg
.GetId() )
3234 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3236 size_t pos
= msg
.GetCommandInt();
3237 int numRows
= msg
.GetCommandInt2();
3238 for ( i
= 0; i
< numRows
; i
++ )
3240 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3241 m_rowBottoms
.Insert( 0, pos
);
3243 m_numRows
+= numRows
;
3246 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3248 for ( i
= pos
; i
< m_numRows
; i
++ )
3250 bottom
+= m_rowHeights
[i
];
3251 m_rowBottoms
[i
] = bottom
;
3257 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3259 int numRows
= msg
.GetCommandInt();
3260 for ( i
= 0; i
< numRows
; i
++ )
3262 m_rowHeights
.Add( m_defaultRowHeight
);
3263 m_rowBottoms
.Add( 0 );
3266 int oldNumRows
= m_numRows
;
3267 m_numRows
+= numRows
;
3270 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3272 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3274 bottom
+= m_rowHeights
[i
];
3275 m_rowBottoms
[i
] = bottom
;
3281 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3283 size_t pos
= msg
.GetCommandInt();
3284 int numRows
= msg
.GetCommandInt2();
3285 for ( i
= 0; i
< numRows
; i
++ )
3287 m_rowHeights
.Remove( pos
);
3288 m_rowBottoms
.Remove( pos
);
3290 m_numRows
-= numRows
;
3295 m_colWidths
.Clear();
3296 m_colRights
.Clear();
3297 m_currentCellCoords
= wxGridNoCellCoords
;
3301 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3302 m_currentCellCoords
.Set( 0, 0 );
3305 for ( i
= 0; i
< m_numRows
; i
++ )
3307 h
+= m_rowHeights
[i
];
3308 m_rowBottoms
[i
] = h
;
3316 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3318 size_t pos
= msg
.GetCommandInt();
3319 int numCols
= msg
.GetCommandInt2();
3320 for ( i
= 0; i
< numCols
; i
++ )
3322 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3323 m_colRights
.Insert( 0, pos
);
3325 m_numCols
+= numCols
;
3328 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3330 for ( i
= pos
; i
< m_numCols
; i
++ )
3332 right
+= m_colWidths
[i
];
3333 m_colRights
[i
] = right
;
3339 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3341 int numCols
= msg
.GetCommandInt();
3342 for ( i
= 0; i
< numCols
; i
++ )
3344 m_colWidths
.Add( m_defaultColWidth
);
3345 m_colRights
.Add( 0 );
3348 int oldNumCols
= m_numCols
;
3349 m_numCols
+= numCols
;
3352 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3354 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3356 right
+= m_colWidths
[i
];
3357 m_colRights
[i
] = right
;
3363 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3365 size_t pos
= msg
.GetCommandInt();
3366 int numCols
= msg
.GetCommandInt2();
3367 for ( i
= 0; i
< numCols
; i
++ )
3369 m_colWidths
.Remove( pos
);
3370 m_colRights
.Remove( pos
);
3372 m_numCols
-= numCols
;
3376 #if 0 // leave the row alone here so that AppendCols will work subsequently
3378 m_rowHeights
.Clear();
3379 m_rowBottoms
.Clear();
3381 m_currentCellCoords
= wxGridNoCellCoords
;
3385 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3386 m_currentCellCoords
.Set( 0, 0 );
3389 for ( i
= 0; i
< m_numCols
; i
++ )
3391 w
+= m_colWidths
[i
];
3404 void wxGrid
::CalcRowLabelsExposed( wxRegion
& reg
)
3406 wxRegionIterator
iter( reg
);
3409 m_rowLabelsExposed
.Empty();
3416 // TODO: remove this when we can...
3417 // There is a bug in wxMotif that gives garbage update
3418 // rectangles if you jump-scroll a long way by clicking the
3419 // scrollbar with middle button. This is a work-around
3421 #if defined(__WXMOTIF__)
3423 m_gridWin
->GetClientSize( &cw
, &ch
);
3424 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3425 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3428 // logical bounds of update region
3431 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3432 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3434 // find the row labels within these bounds
3437 for ( row
= 0; row
< m_numRows
; row
++ )
3439 if ( GetRowBottom(row
) < top
)
3442 if ( GetRowTop(row
) > bottom
)
3445 m_rowLabelsExposed
.Add( row
);
3453 void wxGrid
::CalcColLabelsExposed( wxRegion
& reg
)
3455 wxRegionIterator
iter( reg
);
3458 m_colLabelsExposed
.Empty();
3465 // TODO: remove this when we can...
3466 // There is a bug in wxMotif that gives garbage update
3467 // rectangles if you jump-scroll a long way by clicking the
3468 // scrollbar with middle button. This is a work-around
3470 #if defined(__WXMOTIF__)
3472 m_gridWin
->GetClientSize( &cw
, &ch
);
3473 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3474 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3477 // logical bounds of update region
3480 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3481 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3483 // find the cells within these bounds
3486 for ( col
= 0; col
< m_numCols
; col
++ )
3488 if ( GetColRight(col
) < left
)
3491 if ( GetColLeft(col
) > right
)
3494 m_colLabelsExposed
.Add( col
);
3502 void wxGrid
::CalcCellsExposed( wxRegion
& reg
)
3504 wxRegionIterator
iter( reg
);
3507 m_cellsExposed
.Empty();
3508 m_rowsExposed
.Empty();
3509 m_colsExposed
.Empty();
3511 int left
, top
, right
, bottom
;
3516 // TODO: remove this when we can...
3517 // There is a bug in wxMotif that gives garbage update
3518 // rectangles if you jump-scroll a long way by clicking the
3519 // scrollbar with middle button. This is a work-around
3521 #if defined(__WXMOTIF__)
3523 m_gridWin
->GetClientSize( &cw
, &ch
);
3524 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3525 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3526 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3527 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3530 // logical bounds of update region
3532 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3533 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3535 // find the cells within these bounds
3538 for ( row
= 0; row
< m_numRows
; row
++ )
3540 if ( GetRowBottom(row
) <= top
)
3543 if ( GetRowTop(row
) > bottom
)
3546 m_rowsExposed
.Add( row
);
3548 for ( col
= 0; col
< m_numCols
; col
++ )
3550 if ( GetColRight(col
) <= left
)
3553 if ( GetColLeft(col
) > right
)
3556 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3557 m_colsExposed
.Add( col
);
3558 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3567 void wxGrid
::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3570 wxPoint
pos( event
.GetPosition() );
3571 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3573 if ( event
.Dragging() )
3575 m_isDragging
= TRUE
;
3577 if ( event
.LeftIsDown() )
3579 switch( m_cursorMode
)
3581 case WXGRID_CURSOR_RESIZE_ROW
:
3583 int cw
, ch
, left
, dummy
;
3584 m_gridWin
->GetClientSize( &cw
, &ch
);
3585 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3587 wxClientDC
dc( m_gridWin
);
3590 GetRowTop(m_dragRowOrCol
) +
3591 GetRowMinimalHeight(m_dragRowOrCol
) );
3592 dc
.SetLogicalFunction(wxINVERT
);
3593 if ( m_dragLastPos
>= 0 )
3595 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3597 dc
.DrawLine( left
, y
, left
+cw
, y
);
3602 case WXGRID_CURSOR_SELECT_ROW
:
3603 if ( (row
= YToRow( y
)) >= 0 &&
3604 !IsInSelection( row
, 0 ) )
3606 SelectRow( row
, TRUE
);
3609 // default label to suppress warnings about "enumeration value
3610 // 'xxx' not handled in switch
3618 m_isDragging
= FALSE
;
3621 // ------------ Entering or leaving the window
3623 if ( event
.Entering() || event
.Leaving() )
3625 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3629 // ------------ Left button pressed
3631 else if ( event
.LeftDown() )
3633 // don't send a label click event for a hit on the
3634 // edge of the row label - this is probably the user
3635 // wanting to resize the row
3637 if ( YToEdgeOfRow(y
) < 0 )
3641 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3643 SelectRow( row
, event
.ShiftDown() );
3644 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3649 // starting to drag-resize a row
3651 if ( CanDragRowSize() )
3652 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3657 // ------------ Left double click
3659 else if (event
.LeftDClick() )
3661 if ( YToEdgeOfRow(y
) < 0 )
3664 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3669 // ------------ Left button released
3671 else if ( event
.LeftUp() )
3673 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3675 DoEndDragResizeRow();
3677 // Note: we are ending the event *after* doing
3678 // default processing in this case
3680 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3683 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3688 // ------------ Right button down
3690 else if ( event
.RightDown() )
3693 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3695 // no default action at the moment
3700 // ------------ Right double click
3702 else if ( event
.RightDClick() )
3705 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3707 // no default action at the moment
3712 // ------------ No buttons down and mouse moving
3714 else if ( event
.Moving() )
3716 m_dragRowOrCol
= YToEdgeOfRow( y
);
3717 if ( m_dragRowOrCol
>= 0 )
3719 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3721 // don't capture the mouse yet
3722 if ( CanDragRowSize() )
3723 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3726 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3728 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3734 void wxGrid
::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3737 wxPoint
pos( event
.GetPosition() );
3738 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3740 if ( event
.Dragging() )
3742 m_isDragging
= TRUE
;
3744 if ( event
.LeftIsDown() )
3746 switch( m_cursorMode
)
3748 case WXGRID_CURSOR_RESIZE_COL
:
3750 int cw
, ch
, dummy
, top
;
3751 m_gridWin
->GetClientSize( &cw
, &ch
);
3752 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3754 wxClientDC
dc( m_gridWin
);
3757 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3758 GetColMinimalWidth(m_dragRowOrCol
));
3759 dc
.SetLogicalFunction(wxINVERT
);
3760 if ( m_dragLastPos
>= 0 )
3762 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3764 dc
.DrawLine( x
, top
, x
, top
+ch
);
3769 case WXGRID_CURSOR_SELECT_COL
:
3770 if ( (col
= XToCol( x
)) >= 0 &&
3771 !IsInSelection( 0, col
) )
3773 SelectCol( col
, TRUE
);
3776 // default label to suppress warnings about "enumeration value
3777 // 'xxx' not handled in switch
3785 m_isDragging
= FALSE
;
3788 // ------------ Entering or leaving the window
3790 if ( event
.Entering() || event
.Leaving() )
3792 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3796 // ------------ Left button pressed
3798 else if ( event
.LeftDown() )
3800 // don't send a label click event for a hit on the
3801 // edge of the col label - this is probably the user
3802 // wanting to resize the col
3804 if ( XToEdgeOfCol(x
) < 0 )
3808 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3810 SelectCol( col
, event
.ShiftDown() );
3811 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3816 // starting to drag-resize a col
3818 if ( CanDragColSize() )
3819 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3824 // ------------ Left double click
3826 if ( event
.LeftDClick() )
3828 if ( XToEdgeOfCol(x
) < 0 )
3831 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3836 // ------------ Left button released
3838 else if ( event
.LeftUp() )
3840 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3842 DoEndDragResizeCol();
3844 // Note: we are ending the event *after* doing
3845 // default processing in this case
3847 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3850 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3855 // ------------ Right button down
3857 else if ( event
.RightDown() )
3860 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3862 // no default action at the moment
3867 // ------------ Right double click
3869 else if ( event
.RightDClick() )
3872 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3874 // no default action at the moment
3879 // ------------ No buttons down and mouse moving
3881 else if ( event
.Moving() )
3883 m_dragRowOrCol
= XToEdgeOfCol( x
);
3884 if ( m_dragRowOrCol
>= 0 )
3886 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3888 // don't capture the cursor yet
3889 if ( CanDragColSize() )
3890 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3893 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3895 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3901 void wxGrid
::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3903 if ( event
.LeftDown() )
3905 // indicate corner label by having both row and
3908 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3914 else if ( event
.LeftDClick() )
3916 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3919 else if ( event
.RightDown() )
3921 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3923 // no default action at the moment
3927 else if ( event
.RightDClick() )
3929 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3931 // no default action at the moment
3936 void wxGrid
::ChangeCursorMode(CursorMode mode
,
3941 static const wxChar
*cursorModes
[] =
3950 wxLogTrace(_T("grid"),
3951 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3952 win
== m_colLabelWin ?
_T("colLabelWin")
3953 : win ?
_T("rowLabelWin")
3955 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3956 #endif // __WXDEBUG__
3958 if ( mode
== m_cursorMode
)
3963 // by default use the grid itself
3969 m_winCapture
->ReleaseMouse();
3970 m_winCapture
= (wxWindow
*)NULL
;
3973 m_cursorMode
= mode
;
3975 switch ( m_cursorMode
)
3977 case WXGRID_CURSOR_RESIZE_ROW
:
3978 win
->SetCursor( m_rowResizeCursor
);
3981 case WXGRID_CURSOR_RESIZE_COL
:
3982 win
->SetCursor( m_colResizeCursor
);
3986 win
->SetCursor( *wxSTANDARD_CURSOR
);
3989 // we need to capture mouse when resizing
3990 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3991 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3993 if ( captureMouse
&& resize
)
3995 win
->CaptureMouse();
4000 void wxGrid
::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4003 wxPoint
pos( event
.GetPosition() );
4004 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4006 wxGridCellCoords coords
;
4007 XYToCell( x
, y
, coords
);
4009 if ( event
.Dragging() )
4011 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4013 // Don't start doing anything until the mouse has been drug at
4014 // least 3 pixels in any direction...
4017 if (m_startDragPos
== wxDefaultPosition
)
4019 m_startDragPos
= pos
;
4022 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4026 m_isDragging
= TRUE
;
4027 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4029 // Hide the edit control, so it
4030 // won't interfer with drag-shrinking.
4031 if ( IsCellEditControlEnabled() )
4033 HideCellEditControl();
4034 SaveEditControlValue();
4037 // Have we captured the mouse yet?
4040 m_winCapture
= m_gridWin
;
4041 m_winCapture
->CaptureMouse();
4044 if ( coords
!= wxGridNoCellCoords
)
4046 if ( !IsSelection() )
4048 SelectBlock( coords
, coords
);
4052 SelectBlock( m_currentCellCoords
, coords
);
4055 if (! IsVisible(coords
))
4057 MakeCellVisible(coords
);
4058 // TODO: need to introduce a delay or something here. The
4059 // scrolling is way to fast, at least on MSW.
4063 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4065 int cw
, ch
, left
, dummy
;
4066 m_gridWin
->GetClientSize( &cw
, &ch
);
4067 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4069 wxClientDC
dc( m_gridWin
);
4071 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
4072 dc
.SetLogicalFunction(wxINVERT
);
4073 if ( m_dragLastPos
>= 0 )
4075 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4077 dc
.DrawLine( left
, y
, left
+cw
, y
);
4080 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4082 int cw
, ch
, dummy
, top
;
4083 m_gridWin
->GetClientSize( &cw
, &ch
);
4084 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4086 wxClientDC
dc( m_gridWin
);
4088 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4089 GetColMinimalWidth(m_dragRowOrCol
) );
4090 dc
.SetLogicalFunction(wxINVERT
);
4091 if ( m_dragLastPos
>= 0 )
4093 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4095 dc
.DrawLine( x
, top
, x
, top
+ch
);
4102 m_isDragging
= FALSE
;
4103 m_startDragPos
= wxDefaultPosition
;
4105 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4106 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4109 if ( event
.Entering() || event
.Leaving() )
4111 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4112 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4117 // ------------ Left button pressed
4119 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4121 if ( event
.ShiftDown() )
4123 SelectBlock( m_currentCellCoords
, coords
);
4125 else if ( XToEdgeOfCol(x
) < 0 &&
4126 YToEdgeOfRow(y
) < 0 )
4128 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4133 DisableCellEditControl();
4134 MakeCellVisible( coords
);
4136 // if this is the second click on this cell then start
4138 if ( m_waitForSlowClick
&&
4139 (coords
== m_currentCellCoords
) &&
4140 CanEnableCellControl())
4142 EnableCellEditControl();
4144 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4145 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4148 editor
->StartingClick();
4152 m_waitForSlowClick
= FALSE
;
4156 SetCurrentCell( coords
);
4157 m_waitForSlowClick
= TRUE
;
4164 // ------------ Left double click
4166 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4168 DisableCellEditControl();
4170 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4172 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4180 // ------------ Left button released
4182 else if ( event
.LeftUp() )
4184 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4186 if ( IsSelection() )
4190 m_winCapture
->ReleaseMouse();
4191 m_winCapture
= NULL
;
4193 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4196 // Show the edit control, if it has been hidden for
4198 ShowCellEditControl();
4200 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4202 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4203 DoEndDragResizeRow();
4205 // Note: we are ending the event *after* doing
4206 // default processing in this case
4208 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4210 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4212 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4213 DoEndDragResizeCol();
4215 // Note: we are ending the event *after* doing
4216 // default processing in this case
4218 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4225 // ------------ Right button down
4227 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4229 DisableCellEditControl();
4230 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4235 // no default action at the moment
4240 // ------------ Right double click
4242 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4244 DisableCellEditControl();
4245 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4250 // no default action at the moment
4254 // ------------ Moving and no button action
4256 else if ( event
.Moving() && !event
.IsButton() )
4258 int dragRow
= YToEdgeOfRow( y
);
4259 int dragCol
= XToEdgeOfCol( x
);
4261 // Dragging on the corner of a cell to resize in both
4262 // directions is not implemented yet...
4264 if ( dragRow
>= 0 && dragCol
>= 0 )
4266 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4272 m_dragRowOrCol
= dragRow
;
4274 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4276 if ( CanDragRowSize() && CanDragGridSize() )
4277 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4282 m_dragRowOrCol
= dragCol
;
4290 m_dragRowOrCol
= dragCol
;
4292 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4294 if ( CanDragColSize() && CanDragGridSize() )
4295 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4301 // Neither on a row or col edge
4303 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4305 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4311 void wxGrid
::DoEndDragResizeRow()
4313 if ( m_dragLastPos
>= 0 )
4315 // erase the last line and resize the row
4317 int cw
, ch
, left
, dummy
;
4318 m_gridWin
->GetClientSize( &cw
, &ch
);
4319 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4321 wxClientDC
dc( m_gridWin
);
4323 dc
.SetLogicalFunction( wxINVERT
);
4324 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4325 HideCellEditControl();
4326 SaveEditControlValue();
4328 int rowTop
= GetRowTop(m_dragRowOrCol
);
4329 SetRowSize( m_dragRowOrCol
,
4330 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4332 if ( !GetBatchCount() )
4334 // Only needed to get the correct rect.y:
4335 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4337 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4338 rect
.width
= m_rowLabelWidth
;
4339 rect
.height
= ch
- rect
.y
;
4340 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4342 m_gridWin
->Refresh( FALSE
, &rect
);
4345 ShowCellEditControl();
4350 void wxGrid
::DoEndDragResizeCol()
4352 if ( m_dragLastPos
>= 0 )
4354 // erase the last line and resize the col
4356 int cw
, ch
, dummy
, top
;
4357 m_gridWin
->GetClientSize( &cw
, &ch
);
4358 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4360 wxClientDC
dc( m_gridWin
);
4362 dc
.SetLogicalFunction( wxINVERT
);
4363 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4364 HideCellEditControl();
4365 SaveEditControlValue();
4367 int colLeft
= GetColLeft(m_dragRowOrCol
);
4368 SetColSize( m_dragRowOrCol
,
4369 wxMax( m_dragLastPos
- colLeft
,
4370 GetColMinimalWidth(m_dragRowOrCol
) ) );
4372 if ( !GetBatchCount() )
4374 // Only needed to get the correct rect.x:
4375 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4377 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4378 rect
.width
= cw
- rect
.x
;
4379 rect
.height
= m_colLabelHeight
;
4380 m_colLabelWin
->Refresh( TRUE
, &rect
);
4382 m_gridWin
->Refresh( FALSE
, &rect
);
4385 ShowCellEditControl();
4392 // ------ interaction with data model
4394 bool wxGrid
::ProcessTableMessage( wxGridTableMessage
& msg
)
4396 switch ( msg
.GetId() )
4398 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4399 return GetModelValues();
4401 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4402 return SetModelValues();
4404 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4405 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4406 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4407 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4408 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4409 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4410 return Redimension( msg
);
4419 // The behaviour of this function depends on the grid table class
4420 // Clear() function. For the default wxGridStringTable class the
4421 // behavious is to replace all cell contents with wxEmptyString but
4422 // not to change the number of rows or cols.
4424 void wxGrid
::ClearGrid()
4428 if (IsCellEditControlEnabled())
4429 DisableCellEditControl();
4432 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4437 bool wxGrid
::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4439 // TODO: something with updateLabels flag
4443 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4449 if (IsCellEditControlEnabled())
4450 DisableCellEditControl();
4452 bool ok
= m_table
->InsertRows( pos
, numRows
);
4454 // the table will have sent the results of the insert row
4455 // operation to this view object as a grid table message
4459 if ( m_numCols
== 0 )
4461 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4463 // TODO: perhaps instead of appending the default number of cols
4464 // we should remember what the last non-zero number of cols was ?
4468 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4470 // if we have just inserted cols into an empty grid the current
4471 // cell will be undefined...
4473 SetCurrentCell( 0, 0 );
4477 if ( !GetBatchCount() ) Refresh();
4489 bool wxGrid
::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4491 // TODO: something with updateLabels flag
4495 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4499 if ( m_table
&& m_table
->AppendRows( numRows
) )
4501 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4503 // if we have just inserted cols into an empty grid the current
4504 // cell will be undefined...
4506 SetCurrentCell( 0, 0 );
4509 // the table will have sent the results of the append row
4510 // operation to this view object as a grid table message
4513 if ( !GetBatchCount() ) Refresh();
4523 bool wxGrid
::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4525 // TODO: something with updateLabels flag
4529 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4535 if (IsCellEditControlEnabled())
4536 DisableCellEditControl();
4538 if (m_table
->DeleteRows( pos
, numRows
))
4541 // the table will have sent the results of the delete row
4542 // operation to this view object as a grid table message
4545 if ( !GetBatchCount() ) Refresh();
4553 bool wxGrid
::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4555 // TODO: something with updateLabels flag
4559 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4565 if (IsCellEditControlEnabled())
4566 DisableCellEditControl();
4568 bool ok
= m_table
->InsertCols( pos
, numCols
);
4570 // the table will have sent the results of the insert col
4571 // operation to this view object as a grid table message
4575 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4577 // if we have just inserted cols into an empty grid the current
4578 // cell will be undefined...
4580 SetCurrentCell( 0, 0 );
4584 if ( !GetBatchCount() ) Refresh();
4596 bool wxGrid
::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4598 // TODO: something with updateLabels flag
4602 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4606 if ( m_table
&& m_table
->AppendCols( numCols
) )
4608 // the table will have sent the results of the append col
4609 // operation to this view object as a grid table message
4611 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4613 // if we have just inserted cols into an empty grid the current
4614 // cell will be undefined...
4616 SetCurrentCell( 0, 0 );
4620 if ( !GetBatchCount() ) Refresh();
4630 bool wxGrid
::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4632 // TODO: something with updateLabels flag
4636 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4642 if (IsCellEditControlEnabled())
4643 DisableCellEditControl();
4645 if ( m_table
->DeleteCols( pos
, numCols
) )
4647 // the table will have sent the results of the delete col
4648 // operation to this view object as a grid table message
4651 if ( !GetBatchCount() ) Refresh();
4661 // ----- event handlers
4664 // Generate a grid event based on a mouse event and
4665 // return the result of ProcessEvent()
4667 bool wxGrid
::SendEvent( const wxEventType type
,
4669 wxMouseEvent
& mouseEv
)
4671 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4673 int rowOrCol
= (row
== -1 ? col
: row
);
4675 wxGridSizeEvent
gridEvt( GetId(),
4679 mouseEv
.GetX(), mouseEv
.GetY(),
4680 mouseEv
.ControlDown(),
4681 mouseEv
.ShiftDown(),
4683 mouseEv
.MetaDown() );
4685 return GetEventHandler()->ProcessEvent(gridEvt
);
4687 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4689 wxGridRangeSelectEvent
gridEvt( GetId(),
4693 m_selectedBottomRight
,
4694 mouseEv
.ControlDown(),
4695 mouseEv
.ShiftDown(),
4697 mouseEv
.MetaDown() );
4699 return GetEventHandler()->ProcessEvent(gridEvt
);
4703 wxGridEvent
gridEvt( GetId(),
4707 mouseEv
.GetX(), mouseEv
.GetY(),
4708 mouseEv
.ControlDown(),
4709 mouseEv
.ShiftDown(),
4711 mouseEv
.MetaDown() );
4713 return GetEventHandler()->ProcessEvent(gridEvt
);
4718 // Generate a grid event of specified type and return the result
4719 // of ProcessEvent().
4721 bool wxGrid
::SendEvent( const wxEventType type
,
4724 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4726 int rowOrCol
= (row
== -1 ? col
: row
);
4728 wxGridSizeEvent
gridEvt( GetId(),
4733 return GetEventHandler()->ProcessEvent(gridEvt
);
4737 wxGridEvent
gridEvt( GetId(),
4742 return GetEventHandler()->ProcessEvent(gridEvt
);
4747 void wxGrid
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4749 wxPaintDC
dc( this );
4751 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4752 m_numRows
&& m_numCols
)
4754 m_currentCellCoords
.Set(0, 0);
4755 ShowCellEditControl();
4762 // This is just here to make sure that CalcDimensions gets called when
4763 // the grid view is resized... then the size event is skipped to allow
4764 // the box sizers to handle everything
4766 void wxGrid
::OnSize( wxSizeEvent
& event
)
4773 void wxGrid
::OnKeyDown( wxKeyEvent
& event
)
4775 if ( m_inOnKeyDown
)
4777 // shouldn't be here - we are going round in circles...
4779 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4782 m_inOnKeyDown
= TRUE
;
4784 // propagate the event up and see if it gets processed
4786 wxWindow
*parent
= GetParent();
4787 wxKeyEvent
keyEvt( event
);
4788 keyEvt
.SetEventObject( parent
);
4790 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4793 // TODO: Should also support Shift-cursor keys for
4794 // extending the selection. Maybe add a flag to
4795 // MoveCursorXXX() and MoveCursorXXXBlock() and
4796 // just send event.ShiftDown().
4798 // try local handlers
4800 switch ( event
.KeyCode() )
4803 if ( event
.ControlDown() )
4805 MoveCursorUpBlock();
4814 if ( event
.ControlDown() )
4816 MoveCursorDownBlock();
4825 if ( event
.ControlDown() )
4827 MoveCursorLeftBlock();
4836 if ( event
.ControlDown() )
4838 MoveCursorRightBlock();
4847 if ( event
.ControlDown() )
4849 event
.Skip(); // to let the edit control have the return
4858 if (event
.ShiftDown())
4865 if ( event
.ControlDown() )
4867 MakeCellVisible( 0, 0 );
4868 SetCurrentCell( 0, 0 );
4877 if ( event
.ControlDown() )
4879 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4880 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4897 if ( !IsEditable() )
4902 // Otherwise fall through to default
4905 // alphanumeric keys or F2 (special key just for this) enable
4906 // the cell edit control
4907 if ( !(event
.AltDown() ||
4909 event
.ControlDown()) &&
4910 (isalnum(event
.KeyCode()) || event
.KeyCode() == WXK_F2
) &&
4911 !IsCellEditControlEnabled() &&
4912 CanEnableCellControl() )
4914 EnableCellEditControl();
4915 int row
= m_currentCellCoords
.GetRow();
4916 int col
= m_currentCellCoords
.GetCol();
4917 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4918 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4919 editor
->StartingKey(event
);
4925 // let others process char events with modifiers or all
4926 // char events for readonly cells
4933 m_inOnKeyDown
= FALSE
;
4937 void wxGrid
::OnEraseBackground(wxEraseEvent
&)
4941 void wxGrid
::SetCurrentCell( const wxGridCellCoords
& coords
)
4943 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4945 // the event has been intercepted - do nothing
4950 m_currentCellCoords
!= wxGridNoCellCoords
)
4952 HideCellEditControl();
4953 DisableCellEditControl();
4955 // Clear the old current cell highlight
4956 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4958 // Otherwise refresh redraws the highlight!
4959 m_currentCellCoords
= coords
;
4961 m_gridWin
->Refresh( FALSE
, &r
);
4964 m_currentCellCoords
= coords
;
4968 wxClientDC
dc(m_gridWin
);
4971 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4972 DrawCellHighlight(dc
, attr
);
4975 if ( IsSelection() )
4977 wxRect
r( SelectionToDeviceRect() );
4979 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4986 // ------ functions to get/send data (see also public functions)
4989 bool wxGrid
::GetModelValues()
4993 // all we need to do is repaint the grid
4995 m_gridWin
->Refresh();
5003 bool wxGrid
::SetModelValues()
5009 for ( row
= 0; row
< m_numRows
; row
++ )
5011 for ( col
= 0; col
< m_numCols
; col
++ )
5013 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5025 // Note - this function only draws cells that are in the list of
5026 // exposed cells (usually set from the update region by
5027 // CalcExposedCells)
5029 void wxGrid
::DrawGridCellArea( wxDC
& dc
)
5031 if ( !m_numRows
|| !m_numCols
) return;
5034 size_t numCells
= m_cellsExposed
.GetCount();
5036 for ( i
= 0; i
< numCells
; i
++ )
5038 DrawCell( dc
, m_cellsExposed
[i
] );
5043 void wxGrid
::DrawGridSpace( wxDC
& dc
)
5045 if ( m_numRows
&& m_numCols
)
5048 m_gridWin
->GetClientSize( &cw
, &ch
);
5051 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5053 if ( right
> GetColRight(m_numCols
-1) ||
5054 bottom
> GetRowBottom(m_numRows
-1) )
5057 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5059 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5060 dc
.SetPen( *wxTRANSPARENT_PEN
);
5062 if ( right
> GetColRight(m_numCols
-1) )
5063 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
5064 right
- GetColRight(m_numCols
-1), ch
);
5066 if ( bottom
> GetRowBottom(m_numRows
-1) )
5067 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
5068 cw
, bottom
- GetRowBottom(m_numRows
-1) );
5074 void wxGrid
::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5076 int row
= coords
.GetRow();
5077 int col
= coords
.GetCol();
5079 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5082 // we draw the cell border ourselves
5083 #if !WXGRID_DRAW_LINES
5084 if ( m_gridLinesEnabled
)
5085 DrawCellBorder( dc
, coords
);
5088 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5090 bool isCurrent
= coords
== m_currentCellCoords
;
5093 rect
.x
= GetColLeft(col
);
5094 rect
.y
= GetRowTop(row
);
5095 rect
.width
= GetColWidth(col
) - 1;
5096 rect
.height
= GetRowHeight(row
) - 1;
5098 // if the editor is shown, we should use it and not the renderer
5099 if ( isCurrent
&& IsCellEditControlEnabled() )
5101 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5102 editor
->PaintBackground(rect
, attr
);
5107 // but all the rest is drawn by the cell renderer and hence may be
5109 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5110 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5117 void wxGrid
::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5119 int row
= m_currentCellCoords
.GetRow();
5120 int col
= m_currentCellCoords
.GetCol();
5122 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5126 rect
.x
= GetColLeft(col
);
5127 rect
.y
= GetRowTop(row
);
5128 rect
.width
= GetColWidth(col
) - 1;
5129 rect
.height
= GetRowHeight(row
) - 1;
5131 // hmmm... what could we do here to show that the cell is disabled?
5132 // for now, I just draw a thinner border than for the other ones, but
5133 // it doesn't look really good
5134 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ?
1 : 3, wxSOLID
));
5135 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5137 dc
.DrawRectangle(rect
);
5140 // VZ: my experiments with 3d borders...
5142 // how to properly set colours for arbitrary bg?
5143 wxCoord x1
= rect
.x
,
5145 x2
= rect
.x
+ rect
.width
-1,
5146 y2
= rect
.y
+ rect
.height
-1;
5148 dc
.SetPen(*wxWHITE_PEN
);
5149 dc
.DrawLine(x1
, y1
, x2
, y1
);
5150 dc
.DrawLine(x1
, y1
, x1
, y2
);
5152 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5153 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5155 dc
.SetPen(*wxBLACK_PEN
);
5156 dc
.DrawLine(x1
, y2
, x2
, y2
);
5157 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5162 void wxGrid
::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5164 int row
= coords
.GetRow();
5165 int col
= coords
.GetCol();
5166 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5169 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5171 // right hand border
5173 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5174 GetColRight(col
), GetRowBottom(row
) );
5178 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5179 GetColRight(col
), GetRowBottom(row
) );
5182 void wxGrid
::DrawHighlight(wxDC
& dc
)
5184 if ( IsCellEditControlEnabled() )
5186 // don't show highlight when the edit control is shown
5190 // if the active cell was repainted, repaint its highlight too because it
5191 // might have been damaged by the grid lines
5192 size_t count
= m_cellsExposed
.GetCount();
5193 for ( size_t n
= 0; n
< count
; n
++ )
5195 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5197 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5198 DrawCellHighlight(dc
, attr
);
5206 // TODO: remove this ???
5207 // This is used to redraw all grid lines e.g. when the grid line colour
5210 void wxGrid
::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5212 if ( !m_gridLinesEnabled
||
5214 !m_numCols
) return;
5216 int top
, bottom
, left
, right
;
5222 m_gridWin
->GetClientSize(&cw
, &ch
);
5224 // virtual coords of visible area
5226 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5227 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5232 reg
.GetBox(x
, y
, w
, h
);
5233 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5234 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5238 m_gridWin
->GetClientSize(&cw
, &ch
);
5239 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5240 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5243 // avoid drawing grid lines past the last row and col
5245 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5246 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5248 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5250 // horizontal grid lines
5253 for ( i
= 0; i
< m_numRows
; i
++ )
5255 int bot
= GetRowBottom(i
) - 1;
5264 dc
.DrawLine( left
, bot
, right
, bot
);
5269 // vertical grid lines
5271 for ( i
= 0; i
< m_numCols
; i
++ )
5273 int colRight
= GetColRight(i
) - 1;
5274 if ( colRight
> right
)
5279 if ( colRight
>= left
)
5281 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5287 void wxGrid
::DrawRowLabels( wxDC
& dc
)
5289 if ( !m_numRows
|| !m_numCols
) return;
5292 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5294 for ( i
= 0; i
< numLabels
; i
++ )
5296 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5301 void wxGrid
::DrawRowLabel( wxDC
& dc
, int row
)
5303 if ( GetRowHeight(row
) <= 0 )
5306 int rowTop
= GetRowTop(row
),
5307 rowBottom
= GetRowBottom(row
) - 1;
5309 dc
.SetPen( *wxBLACK_PEN
);
5310 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5311 m_rowLabelWidth
-1, rowBottom
);
5313 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5315 dc
.SetPen( *wxWHITE_PEN
);
5316 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5317 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5319 dc
.SetBackgroundMode( wxTRANSPARENT
);
5320 dc
.SetTextForeground( GetLabelTextColour() );
5321 dc
.SetFont( GetLabelFont() );
5324 GetRowLabelAlignment( &hAlign
, &vAlign
);
5328 rect
.SetY( GetRowTop(row
) + 2 );
5329 rect
.SetWidth( m_rowLabelWidth
- 4 );
5330 rect
.SetHeight( GetRowHeight(row
) - 4 );
5331 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5335 void wxGrid
::DrawColLabels( wxDC
& dc
)
5337 if ( !m_numRows
|| !m_numCols
) return;
5340 size_t numLabels
= m_colLabelsExposed
.GetCount();
5342 for ( i
= 0; i
< numLabels
; i
++ )
5344 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5349 void wxGrid
::DrawColLabel( wxDC
& dc
, int col
)
5351 if ( GetColWidth(col
) <= 0 )
5354 int colLeft
= GetColLeft(col
),
5355 colRight
= GetColRight(col
) - 1;
5357 dc
.SetPen( *wxBLACK_PEN
);
5358 dc
.DrawLine( colRight
, 0,
5359 colRight
, m_colLabelHeight
-1 );
5361 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5362 colRight
, m_colLabelHeight
-1 );
5364 dc
.SetPen( *wxWHITE_PEN
);
5365 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5366 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5368 dc
.SetBackgroundMode( wxTRANSPARENT
);
5369 dc
.SetTextForeground( GetLabelTextColour() );
5370 dc
.SetFont( GetLabelFont() );
5372 dc
.SetBackgroundMode( wxTRANSPARENT
);
5373 dc
.SetTextForeground( GetLabelTextColour() );
5374 dc
.SetFont( GetLabelFont() );
5377 GetColLabelAlignment( &hAlign
, &vAlign
);
5380 rect
.SetX( colLeft
+ 2 );
5382 rect
.SetWidth( GetColWidth(col
) - 4 );
5383 rect
.SetHeight( m_colLabelHeight
- 4 );
5384 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5388 void wxGrid
::DrawTextRectangle( wxDC
& dc
,
5389 const wxString
& value
,
5394 long textWidth
, textHeight
;
5395 long lineWidth
, lineHeight
;
5396 wxArrayString lines
;
5398 dc
.SetClippingRegion( rect
);
5399 StringToLines( value
, lines
);
5400 if ( lines
.GetCount() )
5402 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5403 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5406 switch ( horizAlign
)
5409 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5413 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5422 switch ( vertAlign
)
5425 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5429 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5438 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5440 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5445 dc
.DestroyClippingRegion();
5449 // Split multi line text up into an array of strings. Any existing
5450 // contents of the string array are preserved.
5452 void wxGrid
::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5456 wxString eol
= wxTextFile
::GetEOL( wxTextFileType_Unix
);
5457 wxString tVal
= wxTextFile
::Translate( value
, wxTextFileType_Unix
);
5459 while ( startPos
< (int)tVal
.Length() )
5461 pos
= tVal
.Mid(startPos
).Find( eol
);
5466 else if ( pos
== 0 )
5468 lines
.Add( wxEmptyString
);
5472 lines
.Add( value
.Mid(startPos
, pos
) );
5476 if ( startPos
< (int)value
.Length() )
5478 lines
.Add( value
.Mid( startPos
) );
5483 void wxGrid
::GetTextBoxSize( wxDC
& dc
,
5484 wxArrayString
& lines
,
5485 long *width
, long *height
)
5492 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5494 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5495 w
= wxMax( w
, lineW
);
5505 // ------ Edit control functions
5509 void wxGrid
::EnableEditing( bool edit
)
5511 // TODO: improve this ?
5513 if ( edit
!= m_editable
)
5517 // FIXME IMHO this won't disable the edit control if edit == FALSE
5518 // because of the check in the beginning of
5519 // EnableCellEditControl() just below (VZ)
5520 EnableCellEditControl(m_editable
);
5525 void wxGrid
::EnableCellEditControl( bool enable
)
5530 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5531 SetCurrentCell( 0, 0 );
5533 if ( enable
!= m_cellEditCtrlEnabled
)
5535 // TODO allow the app to Veto() this event?
5536 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5540 // this should be checked by the caller!
5541 wxASSERT_MSG( CanEnableCellControl(),
5542 _T("can't enable editing for this cell!") );
5544 // do it before ShowCellEditControl()
5545 m_cellEditCtrlEnabled
= enable
;
5547 ShowCellEditControl();
5551 HideCellEditControl();
5552 SaveEditControlValue();
5554 // do it after HideCellEditControl()
5555 m_cellEditCtrlEnabled
= enable
;
5560 bool wxGrid
::IsCurrentCellReadOnly() const
5563 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5564 bool readonly
= attr
->IsReadOnly();
5570 bool wxGrid
::CanEnableCellControl() const
5572 return m_editable
&& !IsCurrentCellReadOnly();
5575 bool wxGrid
::IsCellEditControlEnabled() const
5577 // the cell edit control might be disable for all cells or just for the
5578 // current one if it's read only
5579 return m_cellEditCtrlEnabled ?
!IsCurrentCellReadOnly() : FALSE
;
5582 void wxGrid
::ShowCellEditControl()
5584 if ( IsCellEditControlEnabled() )
5586 if ( !IsVisible( m_currentCellCoords
) )
5592 wxRect rect
= CellToRect( m_currentCellCoords
);
5593 int row
= m_currentCellCoords
.GetRow();
5594 int col
= m_currentCellCoords
.GetCol();
5596 // convert to scrolled coords
5598 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5600 // done in PaintBackground()
5602 // erase the highlight and the cell contents because the editor
5603 // might not cover the entire cell
5604 wxClientDC
dc( m_gridWin
);
5606 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5607 dc
.SetPen(*wxTRANSPARENT_PEN
);
5608 dc
.DrawRectangle(rect
);
5611 // cell is shifted by one pixel
5615 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5616 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5617 if ( !editor
->IsCreated() )
5619 editor
->Create(m_gridWin
, -1,
5620 new wxGridCellEditorEvtHandler(this, editor
));
5623 editor
->Show( TRUE
, attr
);
5625 editor
->SetSize( rect
);
5627 editor
->BeginEdit(row
, col
, this);
5636 void wxGrid
::HideCellEditControl()
5638 if ( IsCellEditControlEnabled() )
5640 int row
= m_currentCellCoords
.GetRow();
5641 int col
= m_currentCellCoords
.GetCol();
5643 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5644 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5645 editor
->Show( FALSE
);
5648 m_gridWin
->SetFocus();
5653 void wxGrid
::SaveEditControlValue()
5655 if ( IsCellEditControlEnabled() )
5657 int row
= m_currentCellCoords
.GetRow();
5658 int col
= m_currentCellCoords
.GetCol();
5660 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5661 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5662 bool changed
= editor
->EndEdit(row
, col
, this);
5669 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5670 m_currentCellCoords
.GetRow(),
5671 m_currentCellCoords
.GetCol() );
5678 // ------ Grid location functions
5679 // Note that all of these functions work with the logical coordinates of
5680 // grid cells and labels so you will need to convert from device
5681 // coordinates for mouse events etc.
5684 void wxGrid
::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5686 int row
= YToRow(y
);
5687 int col
= XToCol(x
);
5689 if ( row
== -1 || col
== -1 )
5691 coords
= wxGridNoCellCoords
;
5695 coords
.Set( row
, col
);
5700 int wxGrid
::YToRow( int y
)
5704 for ( i
= 0; i
< m_numRows
; i
++ )
5706 if ( y
< GetRowBottom(i
) )
5714 int wxGrid
::XToCol( int x
)
5718 for ( i
= 0; i
< m_numCols
; i
++ )
5720 if ( x
< GetColRight(i
) )
5728 // return the row number that that the y coord is near the edge of, or
5729 // -1 if not near an edge
5731 int wxGrid
::YToEdgeOfRow( int y
)
5735 for ( i
= 0; i
< m_numRows
; i
++ )
5737 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5739 d
= abs( y
- GetRowBottom(i
) );
5740 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5749 // return the col number that that the x coord is near the edge of, or
5750 // -1 if not near an edge
5752 int wxGrid
::XToEdgeOfCol( int x
)
5756 for ( i
= 0; i
< m_numCols
; i
++ )
5758 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5760 d
= abs( x
- GetColRight(i
) );
5761 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5770 wxRect wxGrid
::CellToRect( int row
, int col
)
5772 wxRect
rect( -1, -1, -1, -1 );
5774 if ( row
>= 0 && row
< m_numRows
&&
5775 col
>= 0 && col
< m_numCols
)
5777 rect
.x
= GetColLeft(col
);
5778 rect
.y
= GetRowTop(row
);
5779 rect
.width
= GetColWidth(col
);
5780 rect
.height
= GetRowHeight(row
);
5787 bool wxGrid
::IsVisible( int row
, int col
, bool wholeCellVisible
)
5789 // get the cell rectangle in logical coords
5791 wxRect
r( CellToRect( row
, col
) );
5793 // convert to device coords
5795 int left
, top
, right
, bottom
;
5796 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5797 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5799 // check against the client area of the grid window
5802 m_gridWin
->GetClientSize( &cw
, &ch
);
5804 if ( wholeCellVisible
)
5806 // is the cell wholly visible ?
5808 return ( left
>= 0 && right
<= cw
&&
5809 top
>= 0 && bottom
<= ch
);
5813 // is the cell partly visible ?
5815 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5816 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5821 // make the specified cell location visible by doing a minimal amount
5824 void wxGrid
::MakeCellVisible( int row
, int col
)
5827 int xpos
= -1, ypos
= -1;
5829 if ( row
>= 0 && row
< m_numRows
&&
5830 col
>= 0 && col
< m_numCols
)
5832 // get the cell rectangle in logical coords
5834 wxRect
r( CellToRect( row
, col
) );
5836 // convert to device coords
5838 int left
, top
, right
, bottom
;
5839 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5840 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5843 m_gridWin
->GetClientSize( &cw
, &ch
);
5849 else if ( bottom
> ch
)
5851 int h
= r
.GetHeight();
5853 for ( i
= row
-1; i
>= 0; i
-- )
5855 int rowHeight
= GetRowHeight(i
);
5856 if ( h
+ rowHeight
> ch
)
5863 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5864 // have rounding errors (this is important, because if we do, we
5865 // might not scroll at all and some cells won't be redrawn)
5866 ypos
+= GRID_SCROLL_LINE
/ 2;
5873 else if ( right
> cw
)
5875 int w
= r
.GetWidth();
5877 for ( i
= col
-1; i
>= 0; i
-- )
5879 int colWidth
= GetColWidth(i
);
5880 if ( w
+ colWidth
> cw
)
5887 // see comment for ypos above
5888 xpos
+= GRID_SCROLL_LINE
/ 2;
5891 if ( xpos
!= -1 || ypos
!= -1 )
5893 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5894 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5895 Scroll( xpos
, ypos
);
5903 // ------ Grid cursor movement functions
5906 bool wxGrid
::MoveCursorUp()
5908 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5909 m_currentCellCoords
.GetRow() > 0 )
5911 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5912 m_currentCellCoords
.GetCol() );
5914 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5915 m_currentCellCoords
.GetCol() );
5924 bool wxGrid
::MoveCursorDown()
5926 // TODO: allow for scrolling
5928 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5929 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5931 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5932 m_currentCellCoords
.GetCol() );
5934 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5935 m_currentCellCoords
.GetCol() );
5944 bool wxGrid
::MoveCursorLeft()
5946 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5947 m_currentCellCoords
.GetCol() > 0 )
5949 MakeCellVisible( m_currentCellCoords
.GetRow(),
5950 m_currentCellCoords
.GetCol() - 1 );
5952 SetCurrentCell( m_currentCellCoords
.GetRow(),
5953 m_currentCellCoords
.GetCol() - 1 );
5962 bool wxGrid
::MoveCursorRight()
5964 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5965 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5967 MakeCellVisible( m_currentCellCoords
.GetRow(),
5968 m_currentCellCoords
.GetCol() + 1 );
5970 SetCurrentCell( m_currentCellCoords
.GetRow(),
5971 m_currentCellCoords
.GetCol() + 1 );
5980 bool wxGrid
::MovePageUp()
5982 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5984 int row
= m_currentCellCoords
.GetRow();
5988 m_gridWin
->GetClientSize( &cw
, &ch
);
5990 int y
= GetRowTop(row
);
5991 int newRow
= YToRow( y
- ch
+ 1 );
5996 else if ( newRow
== row
)
6001 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6002 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6010 bool wxGrid
::MovePageDown()
6012 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6014 int row
= m_currentCellCoords
.GetRow();
6015 if ( row
< m_numRows
)
6018 m_gridWin
->GetClientSize( &cw
, &ch
);
6020 int y
= GetRowTop(row
);
6021 int newRow
= YToRow( y
+ ch
);
6024 newRow
= m_numRows
- 1;
6026 else if ( newRow
== row
)
6031 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6032 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6040 bool wxGrid
::MoveCursorUpBlock()
6043 m_currentCellCoords
!= wxGridNoCellCoords
&&
6044 m_currentCellCoords
.GetRow() > 0 )
6046 int row
= m_currentCellCoords
.GetRow();
6047 int col
= m_currentCellCoords
.GetCol();
6049 if ( m_table
->IsEmptyCell(row
, col
) )
6051 // starting in an empty cell: find the next block of
6057 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6060 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6062 // starting at the top of a block: find the next block
6068 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6073 // starting within a block: find the top of the block
6078 if ( m_table
->IsEmptyCell(row
, col
) )
6086 MakeCellVisible( row
, col
);
6087 SetCurrentCell( row
, col
);
6095 bool wxGrid
::MoveCursorDownBlock()
6098 m_currentCellCoords
!= wxGridNoCellCoords
&&
6099 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6101 int row
= m_currentCellCoords
.GetRow();
6102 int col
= m_currentCellCoords
.GetCol();
6104 if ( m_table
->IsEmptyCell(row
, col
) )
6106 // starting in an empty cell: find the next block of
6109 while ( row
< m_numRows
-1 )
6112 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6115 else if ( m_table
->IsEmptyCell(row
+1, col
) )
6117 // starting at the bottom of a block: find the next block
6120 while ( row
< m_numRows
-1 )
6123 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6128 // starting within a block: find the bottom of the block
6130 while ( row
< m_numRows
-1 )
6133 if ( m_table
->IsEmptyCell(row
, col
) )
6141 MakeCellVisible( row
, col
);
6142 SetCurrentCell( row
, col
);
6150 bool wxGrid
::MoveCursorLeftBlock()
6153 m_currentCellCoords
!= wxGridNoCellCoords
&&
6154 m_currentCellCoords
.GetCol() > 0 )
6156 int row
= m_currentCellCoords
.GetRow();
6157 int col
= m_currentCellCoords
.GetCol();
6159 if ( m_table
->IsEmptyCell(row
, col
) )
6161 // starting in an empty cell: find the next block of
6167 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6170 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6172 // starting at the left of a block: find the next block
6178 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6183 // starting within a block: find the left of the block
6188 if ( m_table
->IsEmptyCell(row
, col
) )
6196 MakeCellVisible( row
, col
);
6197 SetCurrentCell( row
, col
);
6205 bool wxGrid
::MoveCursorRightBlock()
6208 m_currentCellCoords
!= wxGridNoCellCoords
&&
6209 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6211 int row
= m_currentCellCoords
.GetRow();
6212 int col
= m_currentCellCoords
.GetCol();
6214 if ( m_table
->IsEmptyCell(row
, col
) )
6216 // starting in an empty cell: find the next block of
6219 while ( col
< m_numCols
-1 )
6222 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6225 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6227 // starting at the right of a block: find the next block
6230 while ( col
< m_numCols
-1 )
6233 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6238 // starting within a block: find the right of the block
6240 while ( col
< m_numCols
-1 )
6243 if ( m_table
->IsEmptyCell(row
, col
) )
6251 MakeCellVisible( row
, col
);
6252 SetCurrentCell( row
, col
);
6263 // ------ Label values and formatting
6266 void wxGrid
::GetRowLabelAlignment( int *horiz
, int *vert
)
6268 *horiz
= m_rowLabelHorizAlign
;
6269 *vert
= m_rowLabelVertAlign
;
6272 void wxGrid
::GetColLabelAlignment( int *horiz
, int *vert
)
6274 *horiz
= m_colLabelHorizAlign
;
6275 *vert
= m_colLabelVertAlign
;
6278 wxString wxGrid
::GetRowLabelValue( int row
)
6282 return m_table
->GetRowLabelValue( row
);
6292 wxString wxGrid
::GetColLabelValue( int col
)
6296 return m_table
->GetColLabelValue( col
);
6307 void wxGrid
::SetRowLabelSize( int width
)
6309 width
= wxMax( width
, 0 );
6310 if ( width
!= m_rowLabelWidth
)
6314 m_rowLabelWin
->Show( FALSE
);
6315 m_cornerLabelWin
->Show( FALSE
);
6317 else if ( m_rowLabelWidth
== 0 )
6319 m_rowLabelWin
->Show( TRUE
);
6320 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6323 m_rowLabelWidth
= width
;
6330 void wxGrid
::SetColLabelSize( int height
)
6332 height
= wxMax( height
, 0 );
6333 if ( height
!= m_colLabelHeight
)
6337 m_colLabelWin
->Show( FALSE
);
6338 m_cornerLabelWin
->Show( FALSE
);
6340 else if ( m_colLabelHeight
== 0 )
6342 m_colLabelWin
->Show( TRUE
);
6343 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6346 m_colLabelHeight
= height
;
6353 void wxGrid
::SetLabelBackgroundColour( const wxColour
& colour
)
6355 if ( m_labelBackgroundColour
!= colour
)
6357 m_labelBackgroundColour
= colour
;
6358 m_rowLabelWin
->SetBackgroundColour( colour
);
6359 m_colLabelWin
->SetBackgroundColour( colour
);
6360 m_cornerLabelWin
->SetBackgroundColour( colour
);
6362 if ( !GetBatchCount() )
6364 m_rowLabelWin
->Refresh();
6365 m_colLabelWin
->Refresh();
6366 m_cornerLabelWin
->Refresh();
6371 void wxGrid
::SetLabelTextColour( const wxColour
& colour
)
6373 if ( m_labelTextColour
!= colour
)
6375 m_labelTextColour
= colour
;
6376 if ( !GetBatchCount() )
6378 m_rowLabelWin
->Refresh();
6379 m_colLabelWin
->Refresh();
6384 void wxGrid
::SetLabelFont( const wxFont
& font
)
6387 if ( !GetBatchCount() )
6389 m_rowLabelWin
->Refresh();
6390 m_colLabelWin
->Refresh();
6394 void wxGrid
::SetRowLabelAlignment( int horiz
, int vert
)
6396 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6398 m_rowLabelHorizAlign
= horiz
;
6401 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6403 m_rowLabelVertAlign
= vert
;
6406 if ( !GetBatchCount() )
6408 m_rowLabelWin
->Refresh();
6412 void wxGrid
::SetColLabelAlignment( int horiz
, int vert
)
6414 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6416 m_colLabelHorizAlign
= horiz
;
6419 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6421 m_colLabelVertAlign
= vert
;
6424 if ( !GetBatchCount() )
6426 m_colLabelWin
->Refresh();
6430 void wxGrid
::SetRowLabelValue( int row
, const wxString
& s
)
6434 m_table
->SetRowLabelValue( row
, s
);
6435 if ( !GetBatchCount() )
6437 wxRect rect
= CellToRect( row
, 0);
6438 if ( rect
.height
> 0 )
6440 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6442 rect
.width
= m_rowLabelWidth
;
6443 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6449 void wxGrid
::SetColLabelValue( int col
, const wxString
& s
)
6453 m_table
->SetColLabelValue( col
, s
);
6454 if ( !GetBatchCount() )
6456 wxRect rect
= CellToRect( 0, col
);
6457 if ( rect
.width
> 0 )
6459 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6461 rect
.height
= m_colLabelHeight
;
6462 m_colLabelWin
->Refresh( TRUE
, &rect
);
6468 void wxGrid
::SetGridLineColour( const wxColour
& colour
)
6470 if ( m_gridLineColour
!= colour
)
6472 m_gridLineColour
= colour
;
6474 wxClientDC
dc( m_gridWin
);
6476 DrawAllGridLines( dc
, wxRegion() );
6480 void wxGrid
::EnableGridLines( bool enable
)
6482 if ( enable
!= m_gridLinesEnabled
)
6484 m_gridLinesEnabled
= enable
;
6486 if ( !GetBatchCount() )
6490 wxClientDC
dc( m_gridWin
);
6492 DrawAllGridLines( dc
, wxRegion() );
6496 m_gridWin
->Refresh();
6503 int wxGrid
::GetDefaultRowSize()
6505 return m_defaultRowHeight
;
6508 int wxGrid
::GetRowSize( int row
)
6510 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6512 return GetRowHeight(row
);
6515 int wxGrid
::GetDefaultColSize()
6517 return m_defaultColWidth
;
6520 int wxGrid
::GetColSize( int col
)
6522 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6524 return GetColWidth(col
);
6527 // ============================================================================
6528 // access to the grid attributes: each of them has a default value in the grid
6529 // itself and may be overidden on a per-cell basis
6530 // ============================================================================
6532 // ----------------------------------------------------------------------------
6533 // setting default attributes
6534 // ----------------------------------------------------------------------------
6536 void wxGrid
::SetDefaultCellBackgroundColour( const wxColour
& col
)
6538 m_defaultCellAttr
->SetBackgroundColour(col
);
6540 m_gridWin
->SetBackgroundColour(col
);
6544 void wxGrid
::SetDefaultCellTextColour( const wxColour
& col
)
6546 m_defaultCellAttr
->SetTextColour(col
);
6549 void wxGrid
::SetDefaultCellAlignment( int horiz
, int vert
)
6551 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6554 void wxGrid
::SetDefaultCellFont( const wxFont
& font
)
6556 m_defaultCellAttr
->SetFont(font
);
6559 void wxGrid
::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6561 m_defaultCellAttr
->SetRenderer(renderer
);
6564 void wxGrid
::SetDefaultEditor(wxGridCellEditor
*editor
)
6566 m_defaultCellAttr
->SetEditor(editor
);
6569 // ----------------------------------------------------------------------------
6570 // access to the default attrbiutes
6571 // ----------------------------------------------------------------------------
6573 wxColour wxGrid
::GetDefaultCellBackgroundColour()
6575 return m_defaultCellAttr
->GetBackgroundColour();
6578 wxColour wxGrid
::GetDefaultCellTextColour()
6580 return m_defaultCellAttr
->GetTextColour();
6583 wxFont wxGrid
::GetDefaultCellFont()
6585 return m_defaultCellAttr
->GetFont();
6588 void wxGrid
::GetDefaultCellAlignment( int *horiz
, int *vert
)
6590 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6593 wxGridCellRenderer
*wxGrid
::GetDefaultRenderer() const
6595 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
6598 wxGridCellEditor
*wxGrid
::GetDefaultEditor() const
6600 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6603 // ----------------------------------------------------------------------------
6604 // access to cell attributes
6605 // ----------------------------------------------------------------------------
6607 wxColour wxGrid
::GetCellBackgroundColour(int row
, int col
)
6609 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6610 wxColour colour
= attr
->GetBackgroundColour();
6615 wxColour wxGrid
::GetCellTextColour( int row
, int col
)
6617 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6618 wxColour colour
= attr
->GetTextColour();
6623 wxFont wxGrid
::GetCellFont( int row
, int col
)
6625 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6626 wxFont font
= attr
->GetFont();
6631 void wxGrid
::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6633 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6634 attr
->GetAlignment(horiz
, vert
);
6638 wxGridCellRenderer
* wxGrid
::GetCellRenderer(int row
, int col
)
6640 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6641 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6647 wxGridCellEditor
* wxGrid
::GetCellEditor(int row
, int col
)
6649 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6650 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6656 bool wxGrid
::IsReadOnly(int row
, int col
) const
6658 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6659 bool isReadOnly
= attr
->IsReadOnly();
6664 // ----------------------------------------------------------------------------
6665 // attribute support: cache, automatic provider creation, ...
6666 // ----------------------------------------------------------------------------
6668 bool wxGrid
::CanHaveAttributes()
6675 return m_table
->CanHaveAttributes();
6678 void wxGrid
::ClearAttrCache()
6680 if ( m_attrCache
.row
!= -1 )
6682 wxSafeDecRef(m_attrCache
.attr
);
6683 m_attrCache
.row
= -1;
6687 void wxGrid
::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6689 wxGrid
*self
= (wxGrid
*)this; // const_cast
6691 self
->ClearAttrCache();
6692 self
->m_attrCache
.row
= row
;
6693 self
->m_attrCache
.col
= col
;
6694 self
->m_attrCache
.attr
= attr
;
6698 bool wxGrid
::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6700 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6702 *attr
= m_attrCache
.attr
;
6703 wxSafeIncRef(m_attrCache
.attr
);
6705 #ifdef DEBUG_ATTR_CACHE
6706 gs_nAttrCacheHits
++;
6713 #ifdef DEBUG_ATTR_CACHE
6714 gs_nAttrCacheMisses
++;
6720 wxGridCellAttr
*wxGrid
::GetCellAttr(int row
, int col
) const
6722 wxGridCellAttr
*attr
;
6723 if ( !LookupAttr(row
, col
, &attr
) )
6725 attr
= m_table ? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6726 CacheAttr(row
, col
, attr
);
6730 attr
->SetDefAttr(m_defaultCellAttr
);
6734 attr
= m_defaultCellAttr
;
6741 wxGridCellAttr
*wxGrid
::GetOrCreateCellAttr(int row
, int col
) const
6743 wxGridCellAttr
*attr
;
6744 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6746 wxASSERT_MSG( m_table
,
6747 _T("we may only be called if CanHaveAttributes() "
6748 "returned TRUE and then m_table should be !NULL") );
6750 attr
= m_table
->GetAttr(row
, col
);
6753 attr
= new wxGridCellAttr
;
6755 // artificially inc the ref count to match DecRef() in caller
6758 m_table
->SetAttr(attr
, row
, col
);
6761 CacheAttr(row
, col
, attr
);
6763 attr
->SetDefAttr(m_defaultCellAttr
);
6767 // ----------------------------------------------------------------------------
6768 // setting column attributes (wrappers around SetColAttr)
6769 // ----------------------------------------------------------------------------
6771 void wxGrid
::SetColFormatBool(int col
)
6773 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
6776 void wxGrid
::SetColFormatNumber(int col
)
6778 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
6781 void wxGrid
::SetColFormatFloat(int col
, int width
, int precision
)
6783 wxString typeName
= wxGRID_VALUE_FLOAT
;
6784 if ( (width
!= -1) || (precision
!= -1) )
6786 typeName
<< _T(':') << width
<< _T(',') << precision
;
6789 SetColFormatCustom(col
, typeName
);
6792 void wxGrid
::SetColFormatCustom(int col
, const wxString
& typeName
)
6794 wxGridCellAttr
*attr
= new wxGridCellAttr
;
6795 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
6796 attr
->SetRenderer(renderer
);
6798 SetColAttr(col
, attr
);
6801 // ----------------------------------------------------------------------------
6802 // setting cell attributes: this is forwarded to the table
6803 // ----------------------------------------------------------------------------
6805 void wxGrid
::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6807 if ( CanHaveAttributes() )
6809 m_table
->SetRowAttr(attr
, row
);
6817 void wxGrid
::SetColAttr(int col
, wxGridCellAttr
*attr
)
6819 if ( CanHaveAttributes() )
6821 m_table
->SetColAttr(attr
, col
);
6829 void wxGrid
::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6831 if ( CanHaveAttributes() )
6833 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6834 attr
->SetBackgroundColour(colour
);
6839 void wxGrid
::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6841 if ( CanHaveAttributes() )
6843 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6844 attr
->SetTextColour(colour
);
6849 void wxGrid
::SetCellFont( int row
, int col
, const wxFont
& font
)
6851 if ( CanHaveAttributes() )
6853 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6854 attr
->SetFont(font
);
6859 void wxGrid
::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6861 if ( CanHaveAttributes() )
6863 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6864 attr
->SetAlignment(horiz
, vert
);
6869 void wxGrid
::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6871 if ( CanHaveAttributes() )
6873 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6874 attr
->SetRenderer(renderer
);
6879 void wxGrid
::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6881 if ( CanHaveAttributes() )
6883 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6884 attr
->SetEditor(editor
);
6889 void wxGrid
::SetReadOnly(int row
, int col
, bool isReadOnly
)
6891 if ( CanHaveAttributes() )
6893 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6894 attr
->SetReadOnly(isReadOnly
);
6899 // ----------------------------------------------------------------------------
6900 // Data type registration
6901 // ----------------------------------------------------------------------------
6903 void wxGrid
::RegisterDataType(const wxString
& typeName
,
6904 wxGridCellRenderer
* renderer
,
6905 wxGridCellEditor
* editor
)
6907 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6911 wxGridCellEditor
* wxGrid
::GetDefaultEditorForCell(int row
, int col
) const
6913 wxString typeName
= m_table
->GetTypeName(row
, col
);
6914 return GetDefaultEditorForType(typeName
);
6917 wxGridCellRenderer
* wxGrid
::GetDefaultRendererForCell(int row
, int col
) const
6919 wxString typeName
= m_table
->GetTypeName(row
, col
);
6920 return GetDefaultRendererForType(typeName
);
6924 wxGrid
::GetDefaultEditorForType(const wxString
& typeName
) const
6926 int index
= m_typeRegistry
->FindDataType(typeName
);
6927 if ( index
== wxNOT_FOUND
)
6929 wxFAIL_MSG(wxT("Unknown data type name"));
6934 return m_typeRegistry
->GetEditor(index
);
6938 wxGrid
::GetDefaultRendererForType(const wxString
& typeName
) const
6940 // the first part of the typename is the "real" type, anything after ':'
6941 // are the parameters for the renderer
6942 wxString type
= typeName
.BeforeFirst(_T(':'));
6944 int index
= m_typeRegistry
->FindDataType(type
);
6945 if ( index
== wxNOT_FOUND
)
6947 wxFAIL_MSG(wxT("Unknown data type name"));
6952 wxGridCellRenderer
*renderer
= m_typeRegistry
->GetRenderer(index
);
6954 // do it even if there are no parameters to reset them to defaults
6955 renderer
->SetParameters(typeName
.AfterFirst(_T(':')));
6961 // ----------------------------------------------------------------------------
6963 // ----------------------------------------------------------------------------
6965 void wxGrid
::EnableDragRowSize( bool enable
)
6967 m_canDragRowSize
= enable
;
6971 void wxGrid
::EnableDragColSize( bool enable
)
6973 m_canDragColSize
= enable
;
6976 void wxGrid
::EnableDragGridSize( bool enable
)
6978 m_canDragGridSize
= enable
;
6982 void wxGrid
::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6984 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6986 if ( resizeExistingRows
)
6994 void wxGrid
::SetRowSize( int row
, int height
)
6996 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6998 if ( m_rowHeights
.IsEmpty() )
7000 // need to really create the array
7004 int h
= wxMax( 0, height
);
7005 int diff
= h
- m_rowHeights
[row
];
7007 m_rowHeights
[row
] = h
;
7009 for ( i
= row
; i
< m_numRows
; i
++ )
7011 m_rowBottoms
[i
] += diff
;
7016 void wxGrid
::SetDefaultColSize( int width
, bool resizeExistingCols
)
7018 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
7020 if ( resizeExistingCols
)
7028 void wxGrid
::SetColSize( int col
, int width
)
7030 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
7032 // should we check that it's bigger than GetColMinimalWidth(col) here?
7034 if ( m_colWidths
.IsEmpty() )
7036 // need to really create the array
7040 int w
= wxMax( 0, width
);
7041 int diff
= w
- m_colWidths
[col
];
7042 m_colWidths
[col
] = w
;
7045 for ( i
= col
; i
< m_numCols
; i
++ )
7047 m_colRights
[i
] += diff
;
7053 void wxGrid
::SetColMinimalWidth( int col
, int width
)
7055 m_colMinWidths
.Put(col
, width
);
7058 void wxGrid
::SetRowMinimalHeight( int row
, int width
)
7060 m_rowMinHeights
.Put(row
, width
);
7063 int wxGrid
::GetColMinimalWidth(int col
) const
7065 long value
= m_colMinWidths
.Get(col
);
7066 return value
!= wxNOT_FOUND ?
(int)value
: WXGRID_MIN_COL_WIDTH
;
7069 int wxGrid
::GetRowMinimalHeight(int row
) const
7071 long value
= m_rowMinHeights
.Get(row
);
7072 return value
!= wxNOT_FOUND ?
(int)value
: WXGRID_MIN_ROW_HEIGHT
;
7075 // ----------------------------------------------------------------------------
7077 // ----------------------------------------------------------------------------
7079 void wxGrid
::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
7081 wxClientDC
dc(m_gridWin
);
7089 wxCoord extent
, extentMax
= 0;
7090 int max
= column ? m_numRows
: m_numCols
;
7091 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7098 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7099 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7102 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7103 extent
= column ? size
.x
: size
.y
;
7104 if ( extent
> extentMax
)
7115 // now also compare with the column label extent
7117 dc
.SetFont( GetLabelFont() );
7118 dc
.GetTextExtent( column ?
GetColLabelValue(col
)
7119 : GetRowLabelValue(row
), &w
, &h
);
7120 extent
= column ? w
: h
;
7121 if ( extent
> extentMax
)
7128 // empty column - give default extent (notice that if extentMax is less
7129 // than default extent but != 0, it's ok)
7130 extentMax
= column ? m_defaultColWidth
: m_defaultRowHeight
;
7134 // leave some space around text
7139 SetColSize(col
, extentMax
);
7141 SetRowSize(row
, extentMax
);
7146 SetColMinimalWidth(col
, extentMax
);
7148 SetRowMinimalHeight(row
, extentMax
);
7152 int wxGrid
::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7154 int width
= m_rowLabelWidth
;
7156 for ( int col
= 0; col
< m_numCols
; col
++ )
7160 AutoSizeColumn(col
, setAsMin
);
7163 width
+= GetColWidth(col
);
7169 int wxGrid
::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7171 int height
= m_colLabelHeight
;
7173 for ( int row
= 0; row
< m_numRows
; row
++ )
7177 AutoSizeRow(row
, setAsMin
);
7180 height
+= GetRowHeight(row
);
7186 void wxGrid
::AutoSize()
7189 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
7192 wxSize wxGrid
::DoGetBestSize() const
7194 // don't set sizes, only calculate them
7195 wxGrid
*self
= (wxGrid
*)this; // const_cast
7197 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
7198 self
->SetOrCalcRowSizes(TRUE
));
7206 // ----------------------------------------------------------------------------
7207 // cell value accessor functions
7208 // ----------------------------------------------------------------------------
7210 void wxGrid
::SetCellValue( int row
, int col
, const wxString
& s
)
7214 m_table
->SetValue( row
, col
, s
.c_str() );
7215 if ( !GetBatchCount() )
7217 wxClientDC
dc( m_gridWin
);
7219 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7222 if ( m_currentCellCoords
.GetRow() == row
&&
7223 m_currentCellCoords
.GetCol() == col
&&
7224 IsCellEditControlEnabled())
7226 HideCellEditControl();
7227 ShowCellEditControl(); // will reread data from table
7234 // ------ Block, row and col selection
7237 void wxGrid
::SelectRow( int row
, bool addToSelected
)
7241 if ( IsSelection() && addToSelected
)
7244 bool need_refresh
[4];
7248 need_refresh
[3] = FALSE
;
7252 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7253 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7254 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7255 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7259 need_refresh
[0] = TRUE
;
7260 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
7261 wxGridCellCoords ( oldTop
- 1,
7263 m_selectedTopLeft
.SetRow( row
);
7268 need_refresh
[1] = TRUE
;
7269 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
7270 wxGridCellCoords ( oldBottom
,
7273 m_selectedTopLeft
.SetCol( 0 );
7276 if ( oldBottom
< row
)
7278 need_refresh
[2] = TRUE
;
7279 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
7280 wxGridCellCoords ( row
,
7282 m_selectedBottomRight
.SetRow( row
);
7285 if ( oldRight
< m_numCols
- 1 )
7287 need_refresh
[3] = TRUE
;
7288 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7290 wxGridCellCoords ( oldBottom
,
7292 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
7295 for (i
= 0; i
< 4; i
++ )
7296 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7297 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7301 r
= SelectionToDeviceRect();
7303 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7305 m_selectedTopLeft
.Set( row
, 0 );
7306 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7307 r
= SelectionToDeviceRect();
7308 m_gridWin
->Refresh( FALSE
, &r
);
7311 wxGridRangeSelectEvent
gridEvt( GetId(),
7312 wxEVT_GRID_RANGE_SELECT
,
7315 m_selectedBottomRight
);
7317 GetEventHandler()->ProcessEvent(gridEvt
);
7321 void wxGrid
::SelectCol( int col
, bool addToSelected
)
7323 if ( IsSelection() && addToSelected
)
7326 bool need_refresh
[4];
7330 need_refresh
[3] = FALSE
;
7333 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7334 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7335 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7336 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7338 if ( oldLeft
> col
)
7340 need_refresh
[0] = TRUE
;
7341 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7342 wxGridCellCoords ( m_numRows
- 1,
7344 m_selectedTopLeft
.SetCol( col
);
7349 need_refresh
[1] = TRUE
;
7350 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7351 wxGridCellCoords ( oldTop
- 1,
7353 m_selectedTopLeft
.SetRow( 0 );
7356 if ( oldRight
< col
)
7358 need_refresh
[2] = TRUE
;
7359 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7360 wxGridCellCoords ( m_numRows
- 1,
7362 m_selectedBottomRight
.SetCol( col
);
7365 if ( oldBottom
< m_numRows
- 1 )
7367 need_refresh
[3] = TRUE
;
7368 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7370 wxGridCellCoords ( m_numRows
- 1,
7372 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7375 for (i
= 0; i
< 4; i
++ )
7376 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7377 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7383 r
= SelectionToDeviceRect();
7385 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7387 m_selectedTopLeft
.Set( 0, col
);
7388 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7389 r
= SelectionToDeviceRect();
7390 m_gridWin
->Refresh( FALSE
, &r
);
7393 wxGridRangeSelectEvent
gridEvt( GetId(),
7394 wxEVT_GRID_RANGE_SELECT
,
7397 m_selectedBottomRight
);
7399 GetEventHandler()->ProcessEvent(gridEvt
);
7403 void wxGrid
::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7406 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7408 if ( topRow
> bottomRow
)
7415 if ( leftCol
> rightCol
)
7422 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7423 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7425 if ( m_selectedTopLeft
!= updateTopLeft
||
7426 m_selectedBottomRight
!= updateBottomRight
)
7428 // Compute two optimal update rectangles:
7429 // Either one rectangle is a real subset of the
7430 // other, or they are (almost) disjoint!
7432 bool need_refresh
[4];
7436 need_refresh
[3] = FALSE
;
7439 // Store intermediate values
7440 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7441 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7442 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7443 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7445 // Determine the outer/inner coordinates.
7446 if (oldLeft
> leftCol
)
7452 if (oldTop
> topRow
)
7458 if (oldRight
< rightCol
)
7461 oldRight
= rightCol
;
7464 if (oldBottom
< bottomRow
)
7467 oldBottom
= bottomRow
;
7471 // Now, either the stuff marked old is the outer
7472 // rectangle or we don't have a situation where one
7473 // is contained in the other.
7475 if ( oldLeft
< leftCol
)
7477 need_refresh
[0] = TRUE
;
7478 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7480 wxGridCellCoords ( oldBottom
,
7484 if ( oldTop
< topRow
)
7486 need_refresh
[1] = TRUE
;
7487 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7489 wxGridCellCoords ( topRow
- 1,
7493 if ( oldRight
> rightCol
)
7495 need_refresh
[2] = TRUE
;
7496 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7498 wxGridCellCoords ( oldBottom
,
7502 if ( oldBottom
> bottomRow
)
7504 need_refresh
[3] = TRUE
;
7505 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7507 wxGridCellCoords ( oldBottom
,
7513 m_selectedTopLeft
= updateTopLeft
;
7514 m_selectedBottomRight
= updateBottomRight
;
7516 // various Refresh() calls
7517 for (i
= 0; i
< 4; i
++ )
7518 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7519 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7522 // only generate an event if the block is not being selected by
7523 // dragging the mouse (in which case the event will be generated in
7524 // the mouse event handler)
7525 if ( !m_isDragging
)
7527 wxGridRangeSelectEvent
gridEvt( GetId(),
7528 wxEVT_GRID_RANGE_SELECT
,
7531 m_selectedBottomRight
);
7533 GetEventHandler()->ProcessEvent(gridEvt
);
7537 void wxGrid
::SelectAll()
7539 m_selectedTopLeft
.Set( 0, 0 );
7540 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7542 m_gridWin
->Refresh();
7546 void wxGrid
::ClearSelection()
7548 m_selectedTopLeft
= wxGridNoCellCoords
;
7549 m_selectedBottomRight
= wxGridNoCellCoords
;
7553 // This function returns the rectangle that encloses the given block
7554 // in device coords clipped to the client size of the grid window.
7556 wxRect wxGrid
::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7557 const wxGridCellCoords
&bottomRight
)
7559 wxRect
rect( wxGridNoCellRect
);
7562 cellRect
= CellToRect( topLeft
);
7563 if ( cellRect
!= wxGridNoCellRect
)
7569 rect
= wxRect( 0, 0, 0, 0 );
7572 cellRect
= CellToRect( bottomRight
);
7573 if ( cellRect
!= wxGridNoCellRect
)
7579 return wxGridNoCellRect
;
7582 // convert to scrolled coords
7584 int left
, top
, right
, bottom
;
7585 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7586 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7589 m_gridWin
->GetClientSize( &cw
, &ch
);
7591 rect
.SetLeft( wxMax(0, left
) );
7592 rect
.SetTop( wxMax(0, top
) );
7593 rect
.SetRight( wxMin(cw
, right
) );
7594 rect
.SetBottom( wxMin(ch
, bottom
) );
7602 // ------ Grid event classes
7605 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7607 wxGridEvent
::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7608 int row
, int col
, int x
, int y
,
7609 bool control
, bool shift
, bool alt
, bool meta
)
7610 : wxNotifyEvent( type
, id
)
7616 m_control
= control
;
7621 SetEventObject(obj
);
7625 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7627 wxGridSizeEvent
::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7628 int rowOrCol
, int x
, int y
,
7629 bool control
, bool shift
, bool alt
, bool meta
)
7630 : wxNotifyEvent( type
, id
)
7632 m_rowOrCol
= rowOrCol
;
7635 m_control
= control
;
7640 SetEventObject(obj
);
7644 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7646 wxGridRangeSelectEvent
::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7647 const wxGridCellCoords
& topLeft
,
7648 const wxGridCellCoords
& bottomRight
,
7649 bool control
, bool shift
, bool alt
, bool meta
)
7650 : wxNotifyEvent( type
, id
)
7652 m_topLeft
= topLeft
;
7653 m_bottomRight
= bottomRight
;
7654 m_control
= control
;
7659 SetEventObject(obj
);
7663 #endif // ifndef wxUSE_NEW_GRID