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() { delete m_renderer
; delete m_editor
; }
269 wxGridCellRenderer
* m_renderer
;
270 wxGridCellEditor
* m_editor
;
274 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
277 class WXDLLEXPORT wxGridTypeRegistry
280 ~wxGridTypeRegistry();
282 void RegisterDataType(const wxString
& typeName
,
283 wxGridCellRenderer
* renderer
,
284 wxGridCellEditor
* editor
);
285 int FindDataType(const wxString
& typeName
);
286 wxGridCellRenderer
* GetRenderer(int index
);
287 wxGridCellEditor
* GetEditor(int index
);
290 wxGridDataTypeInfoArray m_typeinfo
;
293 // ----------------------------------------------------------------------------
294 // conditional compilation
295 // ----------------------------------------------------------------------------
297 #ifndef WXGRID_DRAW_LINES
298 #define WXGRID_DRAW_LINES 1
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 //#define DEBUG_ATTR_CACHE
306 #ifdef DEBUG_ATTR_CACHE
307 static size_t gs_nAttrCacheHits
= 0;
308 static size_t gs_nAttrCacheMisses
= 0;
309 #endif // DEBUG_ATTR_CACHE
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
316 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
319 // TODO: fixed so far - make configurable later (and also different for x/y)
320 static const size_t GRID_SCROLL_LINE
= 10;
322 // the size of hash tables used a bit everywhere (the max number of elements
323 // in these hash tables is the number of rows/columns)
324 static const int GRID_HASH_SIZE
= 100;
326 // ============================================================================
328 // ============================================================================
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 wxGridCellEditor
::wxGridCellEditor()
340 wxGridCellEditor
::~wxGridCellEditor()
345 void wxGridCellEditor
::Create(wxWindow
* WXUNUSED(parent
),
346 wxWindowID
WXUNUSED(id
),
347 wxEvtHandler
* evtHandler
)
350 m_control
->PushEventHandler(evtHandler
);
353 void wxGridCellEditor
::PaintBackground(const wxRect
& rectCell
,
354 wxGridCellAttr
*attr
)
356 // erase the background because we might not fill the cell
357 wxClientDC
dc(m_control
->GetParent());
358 dc
.SetPen(*wxTRANSPARENT_PEN
);
359 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
360 dc
.DrawRectangle(rectCell
);
362 // redraw the control we just painted over
363 m_control
->Refresh();
366 void wxGridCellEditor
::Destroy()
370 m_control
->PopEventHandler(TRUE
/* delete it*/);
372 m_control
->Destroy();
377 void wxGridCellEditor
::Show(bool show
, wxGridCellAttr
*attr
)
379 wxASSERT_MSG(m_control
,
380 wxT("The wxGridCellEditor must be Created first!"));
381 m_control
->Show(show
);
385 // set the colours/fonts if we have any
388 m_colFgOld
= m_control
->GetForegroundColour();
389 m_control
->SetForegroundColour(attr
->GetTextColour());
391 m_colBgOld
= m_control
->GetBackgroundColour();
392 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
394 m_fontOld
= m_control
->GetFont();
395 m_control
->SetFont(attr
->GetFont());
397 // can't do anything more in the base class version, the other
398 // attributes may only be used by the derived classes
403 // restore the standard colours fonts
404 if ( m_colFgOld
.Ok() )
406 m_control
->SetForegroundColour(m_colFgOld
);
407 m_colFgOld
= wxNullColour
;
410 if ( m_colBgOld
.Ok() )
412 m_control
->SetBackgroundColour(m_colBgOld
);
413 m_colBgOld
= wxNullColour
;
416 if ( m_fontOld
.Ok() )
418 m_control
->SetFont(m_fontOld
);
419 m_fontOld
= wxNullFont
;
424 void wxGridCellEditor
::SetSize(const wxRect
& rect
)
426 wxASSERT_MSG(m_control
,
427 wxT("The wxGridCellEditor must be Created first!"));
428 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
431 void wxGridCellEditor
::HandleReturn(wxKeyEvent
& event
)
437 void wxGridCellEditor
::StartingKey(wxKeyEvent
& event
)
442 void wxGridCellEditor
::StartingClick()
446 // ----------------------------------------------------------------------------
447 // wxGridCellTextEditor
448 // ----------------------------------------------------------------------------
450 wxGridCellTextEditor
::wxGridCellTextEditor()
454 void wxGridCellTextEditor
::Create(wxWindow
* parent
,
456 wxEvtHandler
* evtHandler
)
458 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
459 wxDefaultPosition
, wxDefaultSize
460 #if defined(__WXMSW__)
461 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
465 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
468 void wxGridCellTextEditor
::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
469 wxGridCellAttr
* WXUNUSED(attr
))
471 // as we fill the entire client area, don't do anything here to minimize
475 void wxGridCellTextEditor
::SetSize(const wxRect
& rectOrig
)
477 wxRect
rect(rectOrig
);
479 // Make the edit control large enough to allow for internal
482 // TODO: remove this if the text ctrl sizing is improved esp. for
485 #if defined(__WXGTK__)
494 int extra_x
= ( rect
.x
> 2 )?
2 : 1;
495 int extra_y
= ( rect
.y
> 2 )?
2 : 1;
496 #if defined(__WXMOTIF__)
500 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
501 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
502 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
503 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
506 wxGridCellEditor
::SetSize(rect
);
509 void wxGridCellTextEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
511 wxASSERT_MSG(m_control
,
512 wxT("The wxGridCellEditor must be Created first!"));
514 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
516 DoBeginEdit(m_startValue
);
519 void wxGridCellTextEditor
::DoBeginEdit(const wxString
& startValue
)
521 Text()->SetValue(startValue
);
522 Text()->SetInsertionPointEnd();
526 bool wxGridCellTextEditor
::EndEdit(int row
, int col
,
529 wxASSERT_MSG(m_control
,
530 wxT("The wxGridCellEditor must be Created first!"));
532 bool changed
= FALSE
;
533 wxString value
= Text()->GetValue();
534 if (value
!= m_startValue
)
538 grid
->GetTable()->SetValue(row
, col
, value
);
540 m_startValue
= wxEmptyString
;
541 Text()->SetValue(m_startValue
);
547 void wxGridCellTextEditor
::Reset()
549 wxASSERT_MSG(m_control
,
550 wxT("The wxGridCellEditor must be Created first!"));
552 DoReset(m_startValue
);
555 void wxGridCellTextEditor
::DoReset(const wxString
& startValue
)
557 Text()->SetValue(startValue
);
558 Text()->SetInsertionPointEnd();
561 void wxGridCellTextEditor
::StartingKey(wxKeyEvent
& event
)
563 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
565 // insert the key in the control
566 long keycode
= event
.KeyCode();
567 if ( isprint(keycode
) )
569 // FIXME this is not going to work for non letters...
570 if ( !event
.ShiftDown() )
572 keycode
= tolower(keycode
);
575 Text()->AppendText((wxChar
)keycode
);
585 void wxGridCellTextEditor
::HandleReturn(wxKeyEvent
& event
)
587 #if defined(__WXMOTIF__) || defined(__WXGTK__)
588 // wxMotif needs a little extra help...
589 int pos
= Text()->GetInsertionPoint();
590 wxString
s( Text()->GetValue() );
591 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
593 Text()->SetInsertionPoint( pos
);
595 // the other ports can handle a Return key press
601 // ----------------------------------------------------------------------------
602 // wxGridCellNumberEditor
603 // ----------------------------------------------------------------------------
605 wxGridCellNumberEditor
::wxGridCellNumberEditor(int min
, int max
)
611 void wxGridCellNumberEditor
::Create(wxWindow
* parent
,
613 wxEvtHandler
* evtHandler
)
617 // create a spin ctrl
618 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
619 wxDefaultPosition
, wxDefaultSize
,
623 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
627 // just a text control
628 wxGridCellTextEditor
::Create(parent
, id
, evtHandler
);
631 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
632 #endif // wxUSE_VALIDATORS
636 void wxGridCellNumberEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
638 // first get the value
639 wxGridTableBase
*table
= grid
->GetTable();
640 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
642 m_valueOld
= table
->GetValueAsLong(row
, col
);
646 wxString sValue
= table
->GetValue(row
, col
);
647 if (! sValue
.ToLong(&m_valueOld
))
649 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
656 Spin()->SetValue(m_valueOld
);
660 DoBeginEdit(GetString());
664 bool wxGridCellNumberEditor
::EndEdit(int row
, int col
,
672 value
= Spin()->GetValue();
673 changed
= value
!= m_valueOld
;
677 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
682 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
683 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
685 grid
->GetTable()->SetValue(row
, col
, wxString
::Format("%ld", value
));
691 void wxGridCellNumberEditor
::Reset()
695 Spin()->SetValue(m_valueOld
);
699 DoReset(GetString());
703 void wxGridCellNumberEditor
::StartingKey(wxKeyEvent
& event
)
707 long keycode
= event
.KeyCode();
708 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
710 wxGridCellTextEditor
::StartingKey(event
);
720 // ----------------------------------------------------------------------------
721 // wxGridCellFloatEditor
722 // ----------------------------------------------------------------------------
724 void wxGridCellFloatEditor
::Create(wxWindow
* parent
,
726 wxEvtHandler
* evtHandler
)
728 wxGridCellTextEditor
::Create(parent
, id
, evtHandler
);
731 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
732 #endif // wxUSE_VALIDATORS
735 void wxGridCellFloatEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
737 // first get the value
738 wxGridTableBase
*table
= grid
->GetTable();
739 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
741 m_valueOld
= table
->GetValueAsDouble(row
, col
);
745 wxString sValue
= table
->GetValue(row
, col
);
746 if (! sValue
.ToDouble(&m_valueOld
))
748 wxFAIL_MSG( _T("this cell doesn't have float value") );
753 DoBeginEdit(GetString());
756 bool wxGridCellFloatEditor
::EndEdit(int row
, int col
,
760 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
762 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
763 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
765 grid
->GetTable()->SetValue(row
, col
, wxString
::Format("%f", value
));
775 void wxGridCellFloatEditor
::Reset()
777 DoReset(GetString());
780 void wxGridCellFloatEditor
::StartingKey(wxKeyEvent
& event
)
782 long keycode
= event
.KeyCode();
783 if ( isdigit(keycode
) ||
784 keycode
== '+' || keycode
== '-' || keycode
== '.' )
786 wxGridCellTextEditor
::StartingKey(event
);
795 // ----------------------------------------------------------------------------
796 // wxGridCellBoolEditor
797 // ----------------------------------------------------------------------------
799 void wxGridCellBoolEditor
::Create(wxWindow
* parent
,
801 wxEvtHandler
* evtHandler
)
803 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
804 wxDefaultPosition
, wxDefaultSize
,
807 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
810 void wxGridCellBoolEditor
::SetSize(const wxRect
& r
)
813 wxSize size
= m_control
->GetSize();
814 wxCoord minSize
= wxMin(r
.width
, r
.height
);
816 // check if the checkbox is not too big/small for this cell
817 wxSize sizeBest
= m_control
->GetBestSize();
818 if ( !(size
== sizeBest
) )
820 // reset to default size if it had been made smaller
826 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
828 // leave 1 pixel margin
829 size
.x
= size
.y
= minSize
- 2;
836 m_control
->SetSize(size
);
839 // position it in the centre of the rectangle (TODO: support alignment?)
841 #if defined(__WXGTK__) || defined (__WXMOTIF__)
842 // the checkbox without label still has some space to the right in wxGTK,
843 // so shift it to the right
845 #elif defined(__WXMSW__)
851 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
854 void wxGridCellBoolEditor
::Show(bool show
, wxGridCellAttr
*attr
)
856 m_control
->Show(show
);
860 wxColour colBg
= attr ? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
861 CBox()->SetBackgroundColour(colBg
);
865 void wxGridCellBoolEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
867 wxASSERT_MSG(m_control
,
868 wxT("The wxGridCellEditor must be Created first!"));
870 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
871 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
873 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
874 CBox()->SetValue(m_startValue
);
878 bool wxGridCellBoolEditor
::EndEdit(int row
, int col
,
881 wxASSERT_MSG(m_control
,
882 wxT("The wxGridCellEditor must be Created first!"));
884 bool changed
= FALSE
;
885 bool value
= CBox()->GetValue();
886 if ( value
!= m_startValue
)
891 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
892 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
894 grid
->GetTable()->SetValue(row
, col
, value ?
_T("1") : wxEmptyString
);
900 void wxGridCellBoolEditor
::Reset()
902 wxASSERT_MSG(m_control
,
903 wxT("The wxGridCellEditor must be Created first!"));
905 CBox()->SetValue(m_startValue
);
908 void wxGridCellBoolEditor
::StartingClick()
910 CBox()->SetValue(!CBox()->GetValue());
913 // ----------------------------------------------------------------------------
914 // wxGridCellChoiceEditor
915 // ----------------------------------------------------------------------------
917 wxGridCellChoiceEditor
::wxGridCellChoiceEditor(size_t count
,
918 const wxChar
* choices
[],
920 : m_allowOthers(allowOthers
)
922 m_choices
.Alloc(count
);
923 for ( size_t n
= 0; n
< count
; n
++ )
925 m_choices
.Add(choices
[n
]);
929 void wxGridCellChoiceEditor
::Create(wxWindow
* parent
,
931 wxEvtHandler
* evtHandler
)
933 size_t count
= m_choices
.GetCount();
934 wxString
*choices
= new wxString
[count
];
935 for ( size_t n
= 0; n
< count
; n
++ )
937 choices
[n
] = m_choices
[n
];
940 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
941 wxDefaultPosition
, wxDefaultSize
,
943 m_allowOthers ?
0 : wxCB_READONLY
);
947 wxGridCellEditor
::Create(parent
, id
, evtHandler
);
950 void wxGridCellChoiceEditor
::PaintBackground(const wxRect
& rectCell
,
951 wxGridCellAttr
* attr
)
953 // as we fill the entire client area, don't do anything here to minimize
956 // TODO: It doesn't actually fill the client area since the height of a
957 // combo always defaults to the standard... Until someone has time to
958 // figure out the right rectangle to paint, just do it the normal way...
959 wxGridCellEditor
::PaintBackground(rectCell
, attr
);
962 void wxGridCellChoiceEditor
::BeginEdit(int row
, int col
, wxGrid
* grid
)
964 wxASSERT_MSG(m_control
,
965 wxT("The wxGridCellEditor must be Created first!"));
967 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
969 Combo()->SetValue(m_startValue
);
970 size_t count
= m_choices
.GetCount();
971 for (size_t i
=0; i
<count
; i
++)
973 if (m_startValue
== m_choices
[i
])
975 Combo()->SetSelection(i
);
979 Combo()->SetInsertionPointEnd();
983 bool wxGridCellChoiceEditor
::EndEdit(int row
, int col
,
986 wxString value
= Combo()->GetValue();
987 bool changed
= value
!= m_startValue
;
990 grid
->GetTable()->SetValue(row
, col
, value
);
992 m_startValue
= wxEmptyString
;
993 Combo()->SetValue(m_startValue
);
998 void wxGridCellChoiceEditor
::Reset()
1000 Combo()->SetValue(m_startValue
);
1001 Combo()->SetInsertionPointEnd();
1004 // ----------------------------------------------------------------------------
1005 // wxGridCellEditorEvtHandler
1006 // ----------------------------------------------------------------------------
1008 void wxGridCellEditorEvtHandler
::OnKeyDown(wxKeyEvent
& event
)
1010 switch ( event
.KeyCode() )
1014 m_grid
->DisableCellEditControl();
1018 event
.Skip( m_grid
->ProcessEvent( event
) );
1022 if (!m_grid
->ProcessEvent(event
))
1023 m_editor
->HandleReturn(event
);
1032 void wxGridCellEditorEvtHandler
::OnChar(wxKeyEvent
& event
)
1034 switch ( event
.KeyCode() )
1046 // ============================================================================
1048 // ============================================================================
1050 // ----------------------------------------------------------------------------
1051 // wxGridCellRenderer
1052 // ----------------------------------------------------------------------------
1054 void wxGridCellRenderer
::Draw(wxGrid
& grid
,
1055 wxGridCellAttr
& attr
,
1061 dc
.SetBackgroundMode( wxSOLID
);
1065 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1069 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1072 dc
.SetPen( *wxTRANSPARENT_PEN
);
1073 dc
.DrawRectangle(rect
);
1076 wxGridCellRenderer
::~wxGridCellRenderer()
1080 // ----------------------------------------------------------------------------
1081 // wxGridCellStringRenderer
1082 // ----------------------------------------------------------------------------
1084 void wxGridCellStringRenderer
::SetTextColoursAndFont(wxGrid
& grid
,
1085 wxGridCellAttr
& attr
,
1089 dc
.SetBackgroundMode( wxTRANSPARENT
);
1091 // TODO some special colours for attr.IsReadOnly() case?
1095 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1096 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1100 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1101 dc
.SetTextForeground( attr
.GetTextColour() );
1104 dc
.SetFont( attr
.GetFont() );
1107 wxSize wxGridCellStringRenderer
::DoGetBestSize(wxGridCellAttr
& attr
,
1109 const wxString
& text
)
1112 dc
.SetFont(attr
.GetFont());
1113 dc
.GetTextExtent(text
, &x
, &y
);
1115 return wxSize(x
, y
);
1118 wxSize wxGridCellStringRenderer
::GetBestSize(wxGrid
& grid
,
1119 wxGridCellAttr
& attr
,
1123 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1126 void wxGridCellStringRenderer
::Draw(wxGrid
& grid
,
1127 wxGridCellAttr
& attr
,
1129 const wxRect
& rectCell
,
1133 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1135 // now we only have to draw the text
1136 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1139 attr
.GetAlignment(&hAlign
, &vAlign
);
1141 wxRect rect
= rectCell
;
1144 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1145 rect
, hAlign
, vAlign
);
1148 // ----------------------------------------------------------------------------
1149 // wxGridCellNumberRenderer
1150 // ----------------------------------------------------------------------------
1152 wxString wxGridCellNumberRenderer
::GetString(wxGrid
& grid
, int row
, int col
)
1154 wxGridTableBase
*table
= grid
.GetTable();
1156 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1158 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1162 text
= table
->GetValue(row
, col
);
1168 void wxGridCellNumberRenderer
::Draw(wxGrid
& grid
,
1169 wxGridCellAttr
& attr
,
1171 const wxRect
& rectCell
,
1175 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1177 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1179 // draw the text right aligned by default
1181 attr
.GetAlignment(&hAlign
, &vAlign
);
1184 wxRect rect
= rectCell
;
1187 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1190 wxSize wxGridCellNumberRenderer
::GetBestSize(wxGrid
& grid
,
1191 wxGridCellAttr
& attr
,
1195 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1198 // ----------------------------------------------------------------------------
1199 // wxGridCellFloatRenderer
1200 // ----------------------------------------------------------------------------
1202 wxGridCellFloatRenderer
::wxGridCellFloatRenderer(int width
, int precision
)
1205 SetPrecision(precision
);
1208 wxString wxGridCellFloatRenderer
::GetString(wxGrid
& grid
, int row
, int col
)
1210 wxGridTableBase
*table
= grid
.GetTable();
1212 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1216 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1219 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1223 text
= table
->GetValue(row
, col
);
1229 void wxGridCellFloatRenderer
::Draw(wxGrid
& grid
,
1230 wxGridCellAttr
& attr
,
1232 const wxRect
& rectCell
,
1236 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1238 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1240 // draw the text right aligned by default
1242 attr
.GetAlignment(&hAlign
, &vAlign
);
1245 wxRect rect
= rectCell
;
1248 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1251 wxSize wxGridCellFloatRenderer
::GetBestSize(wxGrid
& grid
,
1252 wxGridCellAttr
& attr
,
1256 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1259 // ----------------------------------------------------------------------------
1260 // wxGridCellBoolRenderer
1261 // ----------------------------------------------------------------------------
1263 wxSize wxGridCellBoolRenderer
::ms_sizeCheckMark
;
1265 // FIXME these checkbox size calculations are really ugly...
1267 // between checkmark and box
1269 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1271 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1274 wxSize wxGridCellBoolRenderer
::GetBestSize(wxGrid
& grid
,
1275 wxGridCellAttr
& WXUNUSED(attr
),
1280 // compute it only once (no locks for MT safeness in GUI thread...)
1281 if ( !ms_sizeCheckMark
.x
)
1283 // get checkbox size
1284 wxCoord checkSize
= 0;
1285 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1286 wxSize size
= checkbox
->GetBestSize();
1287 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1289 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1290 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1291 checkSize
-= size
.y
/ 2;
1296 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1299 return ms_sizeCheckMark
;
1302 void wxGridCellBoolRenderer
::Draw(wxGrid
& grid
,
1303 wxGridCellAttr
& attr
,
1309 wxGridCellRenderer
::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1311 // draw a check mark in the centre (ignoring alignment - TODO)
1312 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1314 // don't draw outside the cell
1315 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1316 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1318 // and even leave (at least) 1 pixel margin
1319 size
.x
= size
.y
= minSize
- 2;
1322 // draw a border around checkmark
1324 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1325 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1326 rectMark
.width
= size
.x
;
1327 rectMark
.height
= size
.y
;
1329 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1330 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1331 dc
.DrawRectangle(rectMark
);
1333 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1336 // looks nicer under MSW
1341 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1342 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1344 value
= !!grid
.GetTable()->GetValue(row
, col
);
1348 dc
.SetTextForeground(attr
.GetTextColour());
1349 dc
.DrawCheckMark(rectMark
);
1353 // ----------------------------------------------------------------------------
1355 // ----------------------------------------------------------------------------
1357 wxGridCellAttr
*wxGridCellAttr
::Clone()
1359 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1360 if ( HasTextColour() )
1361 attr
->SetTextColour(GetTextColour());
1362 if ( HasBackgroundColour() )
1363 attr
->SetBackgroundColour(GetBackgroundColour());
1365 attr
->SetFont(GetFont());
1366 if ( HasAlignment() )
1367 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1371 attr
->SetRenderer(m_renderer
);
1376 attr
->SetEditor(m_editor
);
1381 attr
->SetReadOnly();
1383 attr
->SetDefAttr(m_defGridAttr
);
1388 const wxColour
& wxGridCellAttr
::GetTextColour() const
1390 if (HasTextColour())
1394 else if (m_defGridAttr
!= this)
1396 return m_defGridAttr
->GetTextColour();
1400 wxFAIL_MSG(wxT("Missing default cell attribute"));
1401 return wxNullColour
;
1406 const wxColour
& wxGridCellAttr
::GetBackgroundColour() const
1408 if (HasBackgroundColour())
1410 else if (m_defGridAttr
!= this)
1411 return m_defGridAttr
->GetBackgroundColour();
1414 wxFAIL_MSG(wxT("Missing default cell attribute"));
1415 return wxNullColour
;
1420 const wxFont
& wxGridCellAttr
::GetFont() const
1424 else if (m_defGridAttr
!= this)
1425 return m_defGridAttr
->GetFont();
1428 wxFAIL_MSG(wxT("Missing default cell attribute"));
1434 void wxGridCellAttr
::GetAlignment(int *hAlign
, int *vAlign
) const
1438 if ( hAlign
) *hAlign
= m_hAlign
;
1439 if ( vAlign
) *vAlign
= m_vAlign
;
1441 else if (m_defGridAttr
!= this)
1442 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1445 wxFAIL_MSG(wxT("Missing default cell attribute"));
1450 // GetRenderer and GetEditor use a slightly different decision path about
1451 // which attribute to use. If a non-default attr object has one then it is
1452 // used, otherwise the default editor or renderer is fetched from the grid and
1453 // used. It should be the default for the data type of the cell. If it is
1454 // NULL (because the table has a type that the grid does not have in its
1455 // registry,) then the grid's default editor or renderer is used.
1457 wxGridCellRenderer
* wxGridCellAttr
::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1459 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1460 return m_renderer
; // use local attribute
1462 wxGridCellRenderer
* renderer
= NULL
;
1463 if (grid
) // get renderer for the data type
1464 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1467 // if we still don't have one then use the grid default
1468 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1471 wxFAIL_MSG(wxT("Missing default cell attribute"));
1476 wxGridCellEditor
* wxGridCellAttr
::GetEditor(wxGrid
* grid
, int row
, int col
) const
1478 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1479 return m_editor
; // use local attribute
1481 wxGridCellEditor
* editor
= NULL
;
1482 if (grid
) // get renderer for the data type
1483 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1486 // if we still don't have one then use the grid default
1487 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1490 wxFAIL_MSG(wxT("Missing default cell attribute"));
1495 // ----------------------------------------------------------------------------
1496 // wxGridCellAttrData
1497 // ----------------------------------------------------------------------------
1499 void wxGridCellAttrData
::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1501 int n
= FindIndex(row
, col
);
1502 if ( n
== wxNOT_FOUND
)
1504 // add the attribute
1505 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1511 // change the attribute
1512 m_attrs
[(size_t)n
].attr
= attr
;
1516 // remove this attribute
1517 m_attrs
.RemoveAt((size_t)n
);
1522 wxGridCellAttr
*wxGridCellAttrData
::GetAttr(int row
, int col
) const
1524 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1526 int n
= FindIndex(row
, col
);
1527 if ( n
!= wxNOT_FOUND
)
1529 attr
= m_attrs
[(size_t)n
].attr
;
1536 void wxGridCellAttrData
::UpdateAttrRows( size_t pos
, int numRows
)
1538 size_t count
= m_attrs
.GetCount();
1539 for ( size_t n
= 0; n
< count
; n
++ )
1541 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1542 wxCoord row
= coords
.GetRow();
1543 if ((size_t)row
>= pos
)
1547 // If rows inserted, include row counter where necessary
1548 coords
.SetRow(row
+ numRows
);
1550 else if (numRows
< 0)
1552 // If rows deleted ...
1553 if ((size_t)row
>= pos
- numRows
)
1555 // ...either decrement row counter (if row still exists)...
1556 coords
.SetRow(row
+ numRows
);
1560 // ...or remove the attribute
1561 m_attrs
.RemoveAt((size_t)n
);
1569 void wxGridCellAttrData
::UpdateAttrCols( size_t pos
, int numCols
)
1571 size_t count
= m_attrs
.GetCount();
1572 for ( size_t n
= 0; n
< count
; n
++ )
1574 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1575 wxCoord col
= coords
.GetCol();
1576 if ( (size_t)col
>= pos
)
1580 // If rows inserted, include row counter where necessary
1581 coords
.SetCol(col
+ numCols
);
1583 else if (numCols
< 0)
1585 // If rows deleted ...
1586 if ((size_t)col
>= pos
- numCols
)
1588 // ...either decrement row counter (if row still exists)...
1589 coords
.SetCol(col
+ numCols
);
1593 // ...or remove the attribute
1594 m_attrs
.RemoveAt((size_t)n
);
1602 int wxGridCellAttrData
::FindIndex(int row
, int col
) const
1604 size_t count
= m_attrs
.GetCount();
1605 for ( size_t n
= 0; n
< count
; n
++ )
1607 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1608 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1617 // ----------------------------------------------------------------------------
1618 // wxGridRowOrColAttrData
1619 // ----------------------------------------------------------------------------
1621 wxGridRowOrColAttrData
::~wxGridRowOrColAttrData()
1623 size_t count
= m_attrs
.Count();
1624 for ( size_t n
= 0; n
< count
; n
++ )
1626 m_attrs
[n
]->DecRef();
1630 wxGridCellAttr
*wxGridRowOrColAttrData
::GetAttr(int rowOrCol
) const
1632 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1634 int n
= m_rowsOrCols
.Index(rowOrCol
);
1635 if ( n
!= wxNOT_FOUND
)
1637 attr
= m_attrs
[(size_t)n
];
1644 void wxGridRowOrColAttrData
::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1646 int n
= m_rowsOrCols
.Index(rowOrCol
);
1647 if ( n
== wxNOT_FOUND
)
1649 // add the attribute
1650 m_rowsOrCols
.Add(rowOrCol
);
1657 // change the attribute
1658 m_attrs
[(size_t)n
] = attr
;
1662 // remove this attribute
1663 m_attrs
[(size_t)n
]->DecRef();
1664 m_rowsOrCols
.RemoveAt((size_t)n
);
1665 m_attrs
.RemoveAt((size_t)n
);
1670 void wxGridRowOrColAttrData
::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1672 size_t count
= m_attrs
.GetCount();
1673 for ( size_t n
= 0; n
< count
; n
++ )
1675 int & rowOrCol
= m_rowsOrCols
[n
];
1676 if ( (size_t)rowOrCol
>= pos
)
1678 if ( numRowsOrCols
> 0 )
1680 // If rows inserted, include row counter where necessary
1681 rowOrCol
+= numRowsOrCols
;
1683 else if ( numRowsOrCols
< 0)
1685 // If rows deleted, either decrement row counter (if row still exists)
1686 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1687 rowOrCol
+= numRowsOrCols
;
1690 m_rowsOrCols
.RemoveAt((size_t)n
);
1691 m_attrs
.RemoveAt((size_t)n
);
1699 // ----------------------------------------------------------------------------
1700 // wxGridCellAttrProvider
1701 // ----------------------------------------------------------------------------
1703 wxGridCellAttrProvider
::wxGridCellAttrProvider()
1705 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1708 wxGridCellAttrProvider
::~wxGridCellAttrProvider()
1713 void wxGridCellAttrProvider
::InitData()
1715 m_data
= new wxGridCellAttrProviderData
;
1718 wxGridCellAttr
*wxGridCellAttrProvider
::GetAttr(int row
, int col
) const
1720 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1723 // first look for the attribute of this specific cell
1724 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1728 // then look for the col attr (col attributes are more common than
1729 // the row ones, hence they have priority)
1730 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1735 // finally try the row attributes
1736 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1743 void wxGridCellAttrProvider
::SetAttr(wxGridCellAttr
*attr
,
1749 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1752 void wxGridCellAttrProvider
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1757 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1760 void wxGridCellAttrProvider
::SetColAttr(wxGridCellAttr
*attr
, int col
)
1765 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1768 void wxGridCellAttrProvider
::UpdateAttrRows( size_t pos
, int numRows
)
1772 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1774 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1778 void wxGridCellAttrProvider
::UpdateAttrCols( size_t pos
, int numCols
)
1782 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1784 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1788 // ----------------------------------------------------------------------------
1789 // wxGridTypeRegistry
1790 // ----------------------------------------------------------------------------
1792 wxGridTypeRegistry
::~wxGridTypeRegistry()
1794 size_t count
= m_typeinfo
.Count();
1795 for ( size_t i
= 0; i
< count
; i
++ )
1796 delete m_typeinfo
[i
];
1800 void wxGridTypeRegistry
::RegisterDataType(const wxString
& typeName
,
1801 wxGridCellRenderer
* renderer
,
1802 wxGridCellEditor
* editor
)
1805 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1807 // is it already registered?
1808 if ((loc
= FindDataType(typeName
)) != -1) {
1809 delete m_typeinfo
[loc
];
1810 m_typeinfo
[loc
] = info
;
1813 m_typeinfo
.Add(info
);
1817 int wxGridTypeRegistry
::FindDataType(const wxString
& typeName
)
1821 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1822 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1831 wxGridCellRenderer
* wxGridTypeRegistry
::GetRenderer(int index
)
1833 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1837 wxGridCellEditor
* wxGridTypeRegistry
::GetEditor(int index
)
1839 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1843 // ----------------------------------------------------------------------------
1845 // ----------------------------------------------------------------------------
1847 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1850 wxGridTableBase
::wxGridTableBase()
1852 m_view
= (wxGrid
*) NULL
;
1853 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1856 wxGridTableBase
::~wxGridTableBase()
1858 delete m_attrProvider
;
1861 void wxGridTableBase
::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1863 delete m_attrProvider
;
1864 m_attrProvider
= attrProvider
;
1867 bool wxGridTableBase
::CanHaveAttributes()
1869 if ( ! GetAttrProvider() )
1871 // use the default attr provider by default
1872 SetAttrProvider(new wxGridCellAttrProvider
);
1877 wxGridCellAttr
*wxGridTableBase
::GetAttr(int row
, int col
)
1879 if ( m_attrProvider
)
1880 return m_attrProvider
->GetAttr(row
, col
);
1882 return (wxGridCellAttr
*)NULL
;
1885 void wxGridTableBase
::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1887 if ( m_attrProvider
)
1889 m_attrProvider
->SetAttr(attr
, row
, col
);
1893 // as we take ownership of the pointer and don't store it, we must
1899 void wxGridTableBase
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1901 if ( m_attrProvider
)
1903 m_attrProvider
->SetRowAttr(attr
, row
);
1907 // as we take ownership of the pointer and don't store it, we must
1913 void wxGridTableBase
::SetColAttr(wxGridCellAttr
*attr
, int col
)
1915 if ( m_attrProvider
)
1917 m_attrProvider
->SetColAttr(attr
, col
);
1921 // as we take ownership of the pointer and don't store it, we must
1927 void wxGridTableBase
::UpdateAttrRows( size_t pos
, int numRows
)
1929 if ( m_attrProvider
)
1931 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1935 void wxGridTableBase
::UpdateAttrCols( size_t pos
, int numCols
)
1937 if ( m_attrProvider
)
1939 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1943 bool wxGridTableBase
::InsertRows( size_t pos
, size_t numRows
)
1945 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1946 "but your derived table class does not override this function") );
1951 bool wxGridTableBase
::AppendRows( size_t numRows
)
1953 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1954 "but your derived table class does not override this function"));
1959 bool wxGridTableBase
::DeleteRows( size_t pos
, size_t numRows
)
1961 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1962 "but your derived table class does not override this function"));
1967 bool wxGridTableBase
::InsertCols( size_t pos
, size_t numCols
)
1969 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1970 "but your derived table class does not override this function"));
1975 bool wxGridTableBase
::AppendCols( size_t numCols
)
1977 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1978 "but your derived table class does not override this function"));
1983 bool wxGridTableBase
::DeleteCols( size_t pos
, size_t numCols
)
1985 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1986 "but your derived table class does not override this function"));
1992 wxString wxGridTableBase
::GetRowLabelValue( int row
)
1995 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1996 // how much it makes sense to us geeks.
2000 wxString wxGridTableBase
::GetColLabelValue( int col
)
2002 // default col labels are:
2003 // cols 0 to 25 : A-Z
2004 // cols 26 to 675 : AA-ZZ
2009 for ( n
= 1; ; n
++ )
2011 s
+= (_T('A') + (wxChar
)( col
%26 ));
2013 if ( col
< 0 ) break;
2016 // reverse the string...
2018 for ( i
= 0; i
< n
; i
++ )
2027 wxString wxGridTableBase
::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2029 return wxGRID_VALUE_STRING
;
2032 bool wxGridTableBase
::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2033 const wxString
& typeName
)
2035 return typeName
== wxGRID_VALUE_STRING
;
2038 bool wxGridTableBase
::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2040 return CanGetValueAs(row
, col
, typeName
);
2043 long wxGridTableBase
::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2048 double wxGridTableBase
::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2053 bool wxGridTableBase
::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2058 void wxGridTableBase
::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2059 long WXUNUSED(value
) )
2063 void wxGridTableBase
::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2064 double WXUNUSED(value
) )
2068 void wxGridTableBase
::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2069 bool WXUNUSED(value
) )
2074 void* wxGridTableBase
::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2075 const wxString
& WXUNUSED(typeName
) )
2080 void wxGridTableBase
::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2081 const wxString
& WXUNUSED(typeName
),
2082 void* WXUNUSED(value
) )
2087 //////////////////////////////////////////////////////////////////////
2089 // Message class for the grid table to send requests and notifications
2093 wxGridTableMessage
::wxGridTableMessage()
2095 m_table
= (wxGridTableBase
*) NULL
;
2101 wxGridTableMessage
::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2102 int commandInt1
, int commandInt2
)
2106 m_comInt1
= commandInt1
;
2107 m_comInt2
= commandInt2
;
2112 //////////////////////////////////////////////////////////////////////
2114 // A basic grid table for string data. An object of this class will
2115 // created by wxGrid if you don't specify an alternative table class.
2118 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2120 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2122 wxGridStringTable
::wxGridStringTable()
2127 wxGridStringTable
::wxGridStringTable( int numRows
, int numCols
)
2132 m_data
.Alloc( numRows
);
2135 sa
.Alloc( numCols
);
2136 for ( col
= 0; col
< numCols
; col
++ )
2138 sa
.Add( wxEmptyString
);
2141 for ( row
= 0; row
< numRows
; row
++ )
2147 wxGridStringTable
::~wxGridStringTable()
2151 long wxGridStringTable
::GetNumberRows()
2153 return m_data
.GetCount();
2156 long wxGridStringTable
::GetNumberCols()
2158 if ( m_data
.GetCount() > 0 )
2159 return m_data
[0].GetCount();
2164 wxString wxGridStringTable
::GetValue( int row
, int col
)
2166 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2167 _T("invalid row or column index in wxGridStringTable") );
2169 return m_data
[row
][col
];
2172 void wxGridStringTable
::SetValue( int row
, int col
, const wxString
& value
)
2174 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2175 _T("invalid row or column index in wxGridStringTable") );
2177 m_data
[row
][col
] = value
;
2180 bool wxGridStringTable
::IsEmptyCell( int row
, int col
)
2182 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2183 _T("invalid row or column index in wxGridStringTable") );
2185 return (m_data
[row
][col
] == wxEmptyString
);
2188 void wxGridStringTable
::Clear()
2191 int numRows
, numCols
;
2193 numRows
= m_data
.GetCount();
2196 numCols
= m_data
[0].GetCount();
2198 for ( row
= 0; row
< numRows
; row
++ )
2200 for ( col
= 0; col
< numCols
; col
++ )
2202 m_data
[row
][col
] = wxEmptyString
;
2209 bool wxGridStringTable
::InsertRows( size_t pos
, size_t numRows
)
2213 size_t curNumRows
= m_data
.GetCount();
2214 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2216 if ( pos
>= curNumRows
)
2218 return AppendRows( numRows
);
2222 sa
.Alloc( curNumCols
);
2223 for ( col
= 0; col
< curNumCols
; col
++ )
2225 sa
.Add( wxEmptyString
);
2228 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2230 m_data
.Insert( sa
, row
);
2232 UpdateAttrRows( pos
, numRows
);
2235 wxGridTableMessage
msg( this,
2236 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2240 GetView()->ProcessTableMessage( msg
);
2246 bool wxGridStringTable
::AppendRows( size_t numRows
)
2250 size_t curNumRows
= m_data
.GetCount();
2251 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2254 if ( curNumCols
> 0 )
2256 sa
.Alloc( curNumCols
);
2257 for ( col
= 0; col
< curNumCols
; col
++ )
2259 sa
.Add( wxEmptyString
);
2263 for ( row
= 0; row
< numRows
; row
++ )
2270 wxGridTableMessage
msg( this,
2271 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2274 GetView()->ProcessTableMessage( msg
);
2280 bool wxGridStringTable
::DeleteRows( size_t pos
, size_t numRows
)
2284 size_t curNumRows
= m_data
.GetCount();
2286 if ( pos
>= curNumRows
)
2289 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2290 "Pos value is invalid for present table with %d rows",
2291 pos
, numRows
, curNumRows
);
2292 wxFAIL_MSG( wxT(errmsg
) );
2296 if ( numRows
> curNumRows
- pos
)
2298 numRows
= curNumRows
- pos
;
2301 if ( numRows
>= curNumRows
)
2303 m_data
.Empty(); // don't release memory just yet
2307 for ( n
= 0; n
< numRows
; n
++ )
2309 m_data
.Remove( pos
);
2312 UpdateAttrRows( pos
, -((int)numRows
) );
2315 wxGridTableMessage
msg( this,
2316 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2320 GetView()->ProcessTableMessage( msg
);
2326 bool wxGridStringTable
::InsertCols( size_t pos
, size_t numCols
)
2330 size_t curNumRows
= m_data
.GetCount();
2331 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2333 if ( pos
>= curNumCols
)
2335 return AppendCols( numCols
);
2338 for ( row
= 0; row
< curNumRows
; row
++ )
2340 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2342 m_data
[row
].Insert( wxEmptyString
, col
);
2345 UpdateAttrCols( pos
, numCols
);
2348 wxGridTableMessage
msg( this,
2349 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2353 GetView()->ProcessTableMessage( msg
);
2359 bool wxGridStringTable
::AppendCols( size_t numCols
)
2363 size_t curNumRows
= m_data
.GetCount();
2366 // TODO: something better than this ?
2368 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2369 "Call AppendRows() first") );
2373 for ( row
= 0; row
< curNumRows
; row
++ )
2375 for ( n
= 0; n
< numCols
; n
++ )
2377 m_data
[row
].Add( wxEmptyString
);
2383 wxGridTableMessage
msg( this,
2384 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2387 GetView()->ProcessTableMessage( msg
);
2393 bool wxGridStringTable
::DeleteCols( size_t pos
, size_t numCols
)
2397 size_t curNumRows
= m_data
.GetCount();
2398 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2400 if ( pos
>= curNumCols
)
2403 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2404 "Pos value is invalid for present table with %d cols",
2405 pos
, numCols
, curNumCols
);
2406 wxFAIL_MSG( wxT( errmsg
) );
2410 if ( numCols
> curNumCols
- pos
)
2412 numCols
= curNumCols
- pos
;
2415 for ( row
= 0; row
< curNumRows
; row
++ )
2417 if ( numCols
>= curNumCols
)
2419 m_data
[row
].Clear();
2423 for ( n
= 0; n
< numCols
; n
++ )
2425 m_data
[row
].Remove( pos
);
2429 UpdateAttrCols( pos
, -((int)numCols
) );
2432 wxGridTableMessage
msg( this,
2433 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2437 GetView()->ProcessTableMessage( msg
);
2443 wxString wxGridStringTable
::GetRowLabelValue( int row
)
2445 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2447 // using default label
2449 return wxGridTableBase
::GetRowLabelValue( row
);
2453 return m_rowLabels
[ row
];
2457 wxString wxGridStringTable
::GetColLabelValue( int col
)
2459 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2461 // using default label
2463 return wxGridTableBase
::GetColLabelValue( col
);
2467 return m_colLabels
[ col
];
2471 void wxGridStringTable
::SetRowLabelValue( int row
, const wxString
& value
)
2473 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2475 int n
= m_rowLabels
.GetCount();
2477 for ( i
= n
; i
<= row
; i
++ )
2479 m_rowLabels
.Add( wxGridTableBase
::GetRowLabelValue(i
) );
2483 m_rowLabels
[row
] = value
;
2486 void wxGridStringTable
::SetColLabelValue( int col
, const wxString
& value
)
2488 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2490 int n
= m_colLabels
.GetCount();
2492 for ( i
= n
; i
<= col
; i
++ )
2494 m_colLabels
.Add( wxGridTableBase
::GetColLabelValue(i
) );
2498 m_colLabels
[col
] = value
;
2503 //////////////////////////////////////////////////////////////////////
2504 //////////////////////////////////////////////////////////////////////
2506 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2508 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2509 EVT_PAINT( wxGridRowLabelWindow
::OnPaint
)
2510 EVT_MOUSE_EVENTS( wxGridRowLabelWindow
::OnMouseEvent
)
2511 EVT_KEY_DOWN( wxGridRowLabelWindow
::OnKeyDown
)
2514 wxGridRowLabelWindow
::wxGridRowLabelWindow( wxGrid
*parent
,
2516 const wxPoint
&pos
, const wxSize
&size
)
2517 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2522 void wxGridRowLabelWindow
::OnPaint( wxPaintEvent
&event
)
2526 // NO - don't do this because it will set both the x and y origin
2527 // coords to match the parent scrolled window and we just want to
2528 // set the y coord - MB
2530 // m_owner->PrepareDC( dc );
2533 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2534 dc
.SetDeviceOrigin( 0, -y
);
2536 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2537 m_owner
->DrawRowLabels( dc
);
2541 void wxGridRowLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
2543 m_owner
->ProcessRowLabelMouseEvent( event
);
2547 // This seems to be required for wxMotif otherwise the mouse
2548 // cursor must be in the cell edit control to get key events
2550 void wxGridRowLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
2552 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2557 //////////////////////////////////////////////////////////////////////
2559 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2561 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2562 EVT_PAINT( wxGridColLabelWindow
::OnPaint
)
2563 EVT_MOUSE_EVENTS( wxGridColLabelWindow
::OnMouseEvent
)
2564 EVT_KEY_DOWN( wxGridColLabelWindow
::OnKeyDown
)
2567 wxGridColLabelWindow
::wxGridColLabelWindow( wxGrid
*parent
,
2569 const wxPoint
&pos
, const wxSize
&size
)
2570 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2575 void wxGridColLabelWindow
::OnPaint( wxPaintEvent
&event
)
2579 // NO - don't do this because it will set both the x and y origin
2580 // coords to match the parent scrolled window and we just want to
2581 // set the x coord - MB
2583 // m_owner->PrepareDC( dc );
2586 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2587 dc
.SetDeviceOrigin( -x
, 0 );
2589 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2590 m_owner
->DrawColLabels( dc
);
2594 void wxGridColLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
2596 m_owner
->ProcessColLabelMouseEvent( event
);
2600 // This seems to be required for wxMotif otherwise the mouse
2601 // cursor must be in the cell edit control to get key events
2603 void wxGridColLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
2605 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2610 //////////////////////////////////////////////////////////////////////
2612 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2614 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2615 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow
::OnMouseEvent
)
2616 EVT_PAINT( wxGridCornerLabelWindow
::OnPaint
)
2617 EVT_KEY_DOWN( wxGridCornerLabelWindow
::OnKeyDown
)
2620 wxGridCornerLabelWindow
::wxGridCornerLabelWindow( wxGrid
*parent
,
2622 const wxPoint
&pos
, const wxSize
&size
)
2623 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2628 void wxGridCornerLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2632 int client_height
= 0;
2633 int client_width
= 0;
2634 GetClientSize( &client_width
, &client_height
);
2636 dc
.SetPen( *wxBLACK_PEN
);
2637 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2638 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2640 dc
.SetPen( *wxWHITE_PEN
);
2641 dc
.DrawLine( 0, 0, client_width
, 0 );
2642 dc
.DrawLine( 0, 0, 0, client_height
);
2646 void wxGridCornerLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
2648 m_owner
->ProcessCornerLabelMouseEvent( event
);
2652 // This seems to be required for wxMotif otherwise the mouse
2653 // cursor must be in the cell edit control to get key events
2655 void wxGridCornerLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
2657 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2662 //////////////////////////////////////////////////////////////////////
2664 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2666 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2667 EVT_PAINT( wxGridWindow
::OnPaint
)
2668 EVT_MOUSE_EVENTS( wxGridWindow
::OnMouseEvent
)
2669 EVT_KEY_DOWN( wxGridWindow
::OnKeyDown
)
2670 EVT_ERASE_BACKGROUND( wxGridWindow
::OnEraseBackground
)
2673 wxGridWindow
::wxGridWindow( wxGrid
*parent
,
2674 wxGridRowLabelWindow
*rowLblWin
,
2675 wxGridColLabelWindow
*colLblWin
,
2676 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2677 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
2680 m_rowLabelWin
= rowLblWin
;
2681 m_colLabelWin
= colLblWin
;
2682 SetBackgroundColour( "WHITE" );
2686 wxGridWindow
::~wxGridWindow()
2691 void wxGridWindow
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2693 wxPaintDC
dc( this );
2694 m_owner
->PrepareDC( dc
);
2695 wxRegion reg
= GetUpdateRegion();
2696 m_owner
->CalcCellsExposed( reg
);
2697 m_owner
->DrawGridCellArea( dc
);
2698 #if WXGRID_DRAW_LINES
2699 m_owner
->DrawAllGridLines( dc
, reg
);
2701 m_owner
->DrawGridSpace( dc
);
2702 m_owner
->DrawHighlight( dc
);
2706 void wxGridWindow
::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2708 wxPanel
::ScrollWindow( dx
, dy
, rect
);
2709 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2710 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2714 void wxGridWindow
::OnMouseEvent( wxMouseEvent
& event
)
2716 m_owner
->ProcessGridCellMouseEvent( event
);
2720 // This seems to be required for wxMotif otherwise the mouse
2721 // cursor must be in the cell edit control to get key events
2723 void wxGridWindow
::OnKeyDown( wxKeyEvent
& event
)
2725 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2729 void wxGridWindow
::OnEraseBackground(wxEraseEvent
& event
)
2734 //////////////////////////////////////////////////////////////////////
2737 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2739 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2740 EVT_PAINT( wxGrid
::OnPaint
)
2741 EVT_SIZE( wxGrid
::OnSize
)
2742 EVT_KEY_DOWN( wxGrid
::OnKeyDown
)
2743 EVT_ERASE_BACKGROUND( wxGrid
::OnEraseBackground
)
2746 wxGrid
::wxGrid( wxWindow
*parent
,
2751 const wxString
& name
)
2752 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
2753 m_colMinWidths(GRID_HASH_SIZE
),
2754 m_rowMinHeights(GRID_HASH_SIZE
)
2763 m_defaultCellAttr
->SafeDecRef();
2765 #ifdef DEBUG_ATTR_CACHE
2766 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2767 wxPrintf(_T("wxGrid attribute cache statistics: "
2768 "total: %u, hits: %u (%u%%)\n"),
2769 total
, gs_nAttrCacheHits
,
2770 total ?
(gs_nAttrCacheHits
*100) / total
: 0);
2776 delete m_typeRegistry
;
2781 // ----- internal init and update functions
2784 void wxGrid
::Create()
2786 m_created
= FALSE
; // set to TRUE by CreateGrid
2787 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2789 m_table
= (wxGridTableBase
*) NULL
;
2792 m_cellEditCtrlEnabled
= FALSE
;
2794 m_defaultCellAttr
= new wxGridCellAttr
;
2795 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2797 // Set default cell attributes
2798 m_defaultCellAttr
->SetFont(GetFont());
2799 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2800 m_defaultCellAttr
->SetTextColour(
2801 wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2802 m_defaultCellAttr
->SetBackgroundColour(
2803 wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2804 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2805 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2810 m_currentCellCoords
= wxGridNoCellCoords
;
2812 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2813 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2815 // data type registration: register all standard data types
2816 // TODO: may be allow the app to selectively disable some of them?
2817 m_typeRegistry
= new wxGridTypeRegistry
;
2818 RegisterDataType(wxGRID_VALUE_STRING
,
2819 new wxGridCellStringRenderer
,
2820 new wxGridCellTextEditor
);
2821 RegisterDataType(wxGRID_VALUE_BOOL
,
2822 new wxGridCellBoolRenderer
,
2823 new wxGridCellBoolEditor
);
2824 RegisterDataType(wxGRID_VALUE_NUMBER
,
2825 new wxGridCellNumberRenderer
,
2826 new wxGridCellNumberEditor
);
2828 // subwindow components that make up the wxGrid
2829 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2834 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2839 m_colLabelWin
= new wxGridColLabelWindow( this,
2844 m_gridWin
= new wxGridWindow( this,
2851 SetTargetWindow( m_gridWin
);
2855 bool wxGrid
::CreateGrid( int numRows
, int numCols
)
2859 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2864 m_numRows
= numRows
;
2865 m_numCols
= numCols
;
2867 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2868 m_table
->SetView( this );
2877 bool wxGrid
::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2881 // RD: Actually, this should probably be allowed. I think it would be
2882 // nice to be able to switch multiple Tables in and out of a single
2883 // View at runtime. Is there anything in the implmentation that would
2886 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2891 m_numRows
= table
->GetNumberRows();
2892 m_numCols
= table
->GetNumberCols();
2895 m_table
->SetView( this );
2908 if ( m_numRows
<= 0 )
2909 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2911 if ( m_numCols
<= 0 )
2912 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2914 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2915 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2917 if ( m_rowLabelWin
)
2919 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2923 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2926 m_labelTextColour
= wxColour( _T("BLACK") );
2929 m_attrCache
.row
= -1;
2931 // TODO: something better than this ?
2933 m_labelFont
= this->GetFont();
2934 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2936 m_rowLabelHorizAlign
= wxLEFT
;
2937 m_rowLabelVertAlign
= wxCENTRE
;
2939 m_colLabelHorizAlign
= wxCENTRE
;
2940 m_colLabelVertAlign
= wxTOP
;
2942 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2943 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2945 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2946 m_defaultRowHeight
+= 8;
2948 m_defaultRowHeight
+= 4;
2951 m_gridLineColour
= wxColour( 128, 128, 255 );
2952 m_gridLinesEnabled
= TRUE
;
2954 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2955 m_winCapture
= (wxWindow
*)NULL
;
2956 m_canDragRowSize
= TRUE
;
2957 m_canDragColSize
= TRUE
;
2958 m_canDragGridSize
= TRUE
;
2960 m_dragRowOrCol
= -1;
2961 m_isDragging
= FALSE
;
2962 m_startDragPos
= wxDefaultPosition
;
2964 m_waitForSlowClick
= FALSE
;
2966 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2967 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2969 m_currentCellCoords
= wxGridNoCellCoords
;
2971 m_selectedTopLeft
= wxGridNoCellCoords
;
2972 m_selectedBottomRight
= wxGridNoCellCoords
;
2973 m_selectionBackground
= wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2974 m_selectionForeground
= wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2976 m_editable
= TRUE
; // default for whole grid
2978 m_inOnKeyDown
= FALSE
;
2987 // ----------------------------------------------------------------------------
2988 // the idea is to call these functions only when necessary because they create
2989 // quite big arrays which eat memory mostly unnecessary - in particular, if
2990 // default widths/heights are used for all rows/columns, we may not use these
2993 // with some extra code, it should be possible to only store the
2994 // widths/heights different from default ones but this will be done later...
2995 // ----------------------------------------------------------------------------
2997 void wxGrid
::InitRowHeights()
2999 m_rowHeights
.Empty();
3000 m_rowBottoms
.Empty();
3002 m_rowHeights
.Alloc( m_numRows
);
3003 m_rowBottoms
.Alloc( m_numRows
);
3006 for ( int i
= 0; i
< m_numRows
; i
++ )
3008 m_rowHeights
.Add( m_defaultRowHeight
);
3009 rowBottom
+= m_defaultRowHeight
;
3010 m_rowBottoms
.Add( rowBottom
);
3014 void wxGrid
::InitColWidths()
3016 m_colWidths
.Empty();
3017 m_colRights
.Empty();
3019 m_colWidths
.Alloc( m_numCols
);
3020 m_colRights
.Alloc( m_numCols
);
3022 for ( int i
= 0; i
< m_numCols
; i
++ )
3024 m_colWidths
.Add( m_defaultColWidth
);
3025 colRight
+= m_defaultColWidth
;
3026 m_colRights
.Add( colRight
);
3030 int wxGrid
::GetColWidth(int col
) const
3032 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3035 int wxGrid
::GetColLeft(int col
) const
3037 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3038 : m_colRights
[col
] - m_colWidths
[col
];
3041 int wxGrid
::GetColRight(int col
) const
3043 return m_colRights
.IsEmpty() ?
(col
+ 1) * m_defaultColWidth
3047 int wxGrid
::GetRowHeight(int row
) const
3049 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3052 int wxGrid
::GetRowTop(int row
) const
3054 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3055 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3058 int wxGrid
::GetRowBottom(int row
) const
3060 return m_rowBottoms
.IsEmpty() ?
(row
+ 1) * m_defaultRowHeight
3061 : m_rowBottoms
[row
];
3064 void wxGrid
::CalcDimensions()
3067 GetClientSize( &cw
, &ch
);
3069 if ( m_numRows
> 0 && m_numCols
> 0 )
3071 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3072 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3074 // TODO: restore the scroll position that we had before sizing
3077 GetViewStart( &x
, &y
);
3078 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3079 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3085 void wxGrid
::CalcWindowSizes()
3088 GetClientSize( &cw
, &ch
);
3090 if ( m_cornerLabelWin
->IsShown() )
3091 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3093 if ( m_colLabelWin
->IsShown() )
3094 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3096 if ( m_rowLabelWin
->IsShown() )
3097 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3099 if ( m_gridWin
->IsShown() )
3100 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3104 // this is called when the grid table sends a message to say that it
3105 // has been redimensioned
3107 bool wxGrid
::Redimension( wxGridTableMessage
& msg
)
3111 // if we were using the default widths/heights so far, we must change them
3113 if ( m_colWidths
.IsEmpty() )
3118 if ( m_rowHeights
.IsEmpty() )
3123 switch ( msg
.GetId() )
3125 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3127 size_t pos
= msg
.GetCommandInt();
3128 int numRows
= msg
.GetCommandInt2();
3129 for ( i
= 0; i
< numRows
; i
++ )
3131 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3132 m_rowBottoms
.Insert( 0, pos
);
3134 m_numRows
+= numRows
;
3137 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3139 for ( i
= pos
; i
< m_numRows
; i
++ )
3141 bottom
+= m_rowHeights
[i
];
3142 m_rowBottoms
[i
] = bottom
;
3148 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3150 int numRows
= msg
.GetCommandInt();
3151 for ( i
= 0; i
< numRows
; i
++ )
3153 m_rowHeights
.Add( m_defaultRowHeight
);
3154 m_rowBottoms
.Add( 0 );
3157 int oldNumRows
= m_numRows
;
3158 m_numRows
+= numRows
;
3161 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3163 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3165 bottom
+= m_rowHeights
[i
];
3166 m_rowBottoms
[i
] = bottom
;
3172 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3174 size_t pos
= msg
.GetCommandInt();
3175 int numRows
= msg
.GetCommandInt2();
3176 for ( i
= 0; i
< numRows
; i
++ )
3178 m_rowHeights
.Remove( pos
);
3179 m_rowBottoms
.Remove( pos
);
3181 m_numRows
-= numRows
;
3186 m_colWidths
.Clear();
3187 m_colRights
.Clear();
3188 m_currentCellCoords
= wxGridNoCellCoords
;
3192 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3193 m_currentCellCoords
.Set( 0, 0 );
3196 for ( i
= 0; i
< m_numRows
; i
++ )
3198 h
+= m_rowHeights
[i
];
3199 m_rowBottoms
[i
] = h
;
3207 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3209 size_t pos
= msg
.GetCommandInt();
3210 int numCols
= msg
.GetCommandInt2();
3211 for ( i
= 0; i
< numCols
; i
++ )
3213 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3214 m_colRights
.Insert( 0, pos
);
3216 m_numCols
+= numCols
;
3219 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3221 for ( i
= pos
; i
< m_numCols
; i
++ )
3223 right
+= m_colWidths
[i
];
3224 m_colRights
[i
] = right
;
3230 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3232 int numCols
= msg
.GetCommandInt();
3233 for ( i
= 0; i
< numCols
; i
++ )
3235 m_colWidths
.Add( m_defaultColWidth
);
3236 m_colRights
.Add( 0 );
3239 int oldNumCols
= m_numCols
;
3240 m_numCols
+= numCols
;
3243 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3245 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3247 right
+= m_colWidths
[i
];
3248 m_colRights
[i
] = right
;
3254 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3256 size_t pos
= msg
.GetCommandInt();
3257 int numCols
= msg
.GetCommandInt2();
3258 for ( i
= 0; i
< numCols
; i
++ )
3260 m_colWidths
.Remove( pos
);
3261 m_colRights
.Remove( pos
);
3263 m_numCols
-= numCols
;
3267 #if 0 // leave the row alone here so that AppendCols will work subsequently
3269 m_rowHeights
.Clear();
3270 m_rowBottoms
.Clear();
3272 m_currentCellCoords
= wxGridNoCellCoords
;
3276 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3277 m_currentCellCoords
.Set( 0, 0 );
3280 for ( i
= 0; i
< m_numCols
; i
++ )
3282 w
+= m_colWidths
[i
];
3295 void wxGrid
::CalcRowLabelsExposed( wxRegion
& reg
)
3297 wxRegionIterator
iter( reg
);
3300 m_rowLabelsExposed
.Empty();
3307 // TODO: remove this when we can...
3308 // There is a bug in wxMotif that gives garbage update
3309 // rectangles if you jump-scroll a long way by clicking the
3310 // scrollbar with middle button. This is a work-around
3312 #if defined(__WXMOTIF__)
3314 m_gridWin
->GetClientSize( &cw
, &ch
);
3315 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3316 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3319 // logical bounds of update region
3322 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3323 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3325 // find the row labels within these bounds
3328 for ( row
= 0; row
< m_numRows
; row
++ )
3330 if ( GetRowBottom(row
) < top
)
3333 if ( GetRowTop(row
) > bottom
)
3336 m_rowLabelsExposed
.Add( row
);
3344 void wxGrid
::CalcColLabelsExposed( wxRegion
& reg
)
3346 wxRegionIterator
iter( reg
);
3349 m_colLabelsExposed
.Empty();
3356 // TODO: remove this when we can...
3357 // There is a bug in wxMotif that gives garbage update
3358 // rectangles if you jump-scroll a long way by clicking the
3359 // scrollbar with middle button. This is a work-around
3361 #if defined(__WXMOTIF__)
3363 m_gridWin
->GetClientSize( &cw
, &ch
);
3364 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3365 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3368 // logical bounds of update region
3371 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3372 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3374 // find the cells within these bounds
3377 for ( col
= 0; col
< m_numCols
; col
++ )
3379 if ( GetColRight(col
) < left
)
3382 if ( GetColLeft(col
) > right
)
3385 m_colLabelsExposed
.Add( col
);
3393 void wxGrid
::CalcCellsExposed( wxRegion
& reg
)
3395 wxRegionIterator
iter( reg
);
3398 m_cellsExposed
.Empty();
3399 m_rowsExposed
.Empty();
3400 m_colsExposed
.Empty();
3402 int left
, top
, right
, bottom
;
3407 // TODO: remove this when we can...
3408 // There is a bug in wxMotif that gives garbage update
3409 // rectangles if you jump-scroll a long way by clicking the
3410 // scrollbar with middle button. This is a work-around
3412 #if defined(__WXMOTIF__)
3414 m_gridWin
->GetClientSize( &cw
, &ch
);
3415 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3416 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3417 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3418 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3421 // logical bounds of update region
3423 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3424 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3426 // find the cells within these bounds
3429 for ( row
= 0; row
< m_numRows
; row
++ )
3431 if ( GetRowBottom(row
) <= top
)
3434 if ( GetRowTop(row
) > bottom
)
3437 m_rowsExposed
.Add( row
);
3439 for ( col
= 0; col
< m_numCols
; col
++ )
3441 if ( GetColRight(col
) <= left
)
3444 if ( GetColLeft(col
) > right
)
3447 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3448 m_colsExposed
.Add( col
);
3449 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3458 void wxGrid
::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3461 wxPoint
pos( event
.GetPosition() );
3462 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3464 if ( event
.Dragging() )
3466 m_isDragging
= TRUE
;
3468 if ( event
.LeftIsDown() )
3470 switch( m_cursorMode
)
3472 case WXGRID_CURSOR_RESIZE_ROW
:
3474 int cw
, ch
, left
, dummy
;
3475 m_gridWin
->GetClientSize( &cw
, &ch
);
3476 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3478 wxClientDC
dc( m_gridWin
);
3481 GetRowTop(m_dragRowOrCol
) +
3482 GetRowMinimalHeight(m_dragRowOrCol
) );
3483 dc
.SetLogicalFunction(wxINVERT
);
3484 if ( m_dragLastPos
>= 0 )
3486 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3488 dc
.DrawLine( left
, y
, left
+cw
, y
);
3493 case WXGRID_CURSOR_SELECT_ROW
:
3494 if ( (row
= YToRow( y
)) >= 0 &&
3495 !IsInSelection( row
, 0 ) )
3497 SelectRow( row
, TRUE
);
3500 // default label to suppress warnings about "enumeration value
3501 // 'xxx' not handled in switch
3509 m_isDragging
= FALSE
;
3512 // ------------ Entering or leaving the window
3514 if ( event
.Entering() || event
.Leaving() )
3516 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3520 // ------------ Left button pressed
3522 else if ( event
.LeftDown() )
3524 // don't send a label click event for a hit on the
3525 // edge of the row label - this is probably the user
3526 // wanting to resize the row
3528 if ( YToEdgeOfRow(y
) < 0 )
3532 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3534 SelectRow( row
, event
.ShiftDown() );
3535 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3540 // starting to drag-resize a row
3542 if ( CanDragRowSize() )
3543 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3548 // ------------ Left double click
3550 else if (event
.LeftDClick() )
3552 if ( YToEdgeOfRow(y
) < 0 )
3555 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3560 // ------------ Left button released
3562 else if ( event
.LeftUp() )
3564 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3566 DoEndDragResizeRow();
3568 // Note: we are ending the event *after* doing
3569 // default processing in this case
3571 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3574 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3579 // ------------ Right button down
3581 else if ( event
.RightDown() )
3584 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3586 // no default action at the moment
3591 // ------------ Right double click
3593 else if ( event
.RightDClick() )
3596 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3598 // no default action at the moment
3603 // ------------ No buttons down and mouse moving
3605 else if ( event
.Moving() )
3607 m_dragRowOrCol
= YToEdgeOfRow( y
);
3608 if ( m_dragRowOrCol
>= 0 )
3610 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3612 // don't capture the mouse yet
3613 if ( CanDragRowSize() )
3614 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3617 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3619 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3625 void wxGrid
::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3628 wxPoint
pos( event
.GetPosition() );
3629 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3631 if ( event
.Dragging() )
3633 m_isDragging
= TRUE
;
3635 if ( event
.LeftIsDown() )
3637 switch( m_cursorMode
)
3639 case WXGRID_CURSOR_RESIZE_COL
:
3641 int cw
, ch
, dummy
, top
;
3642 m_gridWin
->GetClientSize( &cw
, &ch
);
3643 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3645 wxClientDC
dc( m_gridWin
);
3648 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3649 GetColMinimalWidth(m_dragRowOrCol
));
3650 dc
.SetLogicalFunction(wxINVERT
);
3651 if ( m_dragLastPos
>= 0 )
3653 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3655 dc
.DrawLine( x
, top
, x
, top
+ch
);
3660 case WXGRID_CURSOR_SELECT_COL
:
3661 if ( (col
= XToCol( x
)) >= 0 &&
3662 !IsInSelection( 0, col
) )
3664 SelectCol( col
, TRUE
);
3667 // default label to suppress warnings about "enumeration value
3668 // 'xxx' not handled in switch
3676 m_isDragging
= FALSE
;
3679 // ------------ Entering or leaving the window
3681 if ( event
.Entering() || event
.Leaving() )
3683 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3687 // ------------ Left button pressed
3689 else if ( event
.LeftDown() )
3691 // don't send a label click event for a hit on the
3692 // edge of the col label - this is probably the user
3693 // wanting to resize the col
3695 if ( XToEdgeOfCol(x
) < 0 )
3699 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3701 SelectCol( col
, event
.ShiftDown() );
3702 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3707 // starting to drag-resize a col
3709 if ( CanDragColSize() )
3710 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3715 // ------------ Left double click
3717 if ( event
.LeftDClick() )
3719 if ( XToEdgeOfCol(x
) < 0 )
3722 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3727 // ------------ Left button released
3729 else if ( event
.LeftUp() )
3731 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3733 DoEndDragResizeCol();
3735 // Note: we are ending the event *after* doing
3736 // default processing in this case
3738 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3741 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3746 // ------------ Right button down
3748 else if ( event
.RightDown() )
3751 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3753 // no default action at the moment
3758 // ------------ Right double click
3760 else if ( event
.RightDClick() )
3763 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3765 // no default action at the moment
3770 // ------------ No buttons down and mouse moving
3772 else if ( event
.Moving() )
3774 m_dragRowOrCol
= XToEdgeOfCol( x
);
3775 if ( m_dragRowOrCol
>= 0 )
3777 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3779 // don't capture the cursor yet
3780 if ( CanDragColSize() )
3781 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3784 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3786 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3792 void wxGrid
::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3794 if ( event
.LeftDown() )
3796 // indicate corner label by having both row and
3799 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3805 else if ( event
.LeftDClick() )
3807 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3810 else if ( event
.RightDown() )
3812 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3814 // no default action at the moment
3818 else if ( event
.RightDClick() )
3820 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3822 // no default action at the moment
3827 void wxGrid
::ChangeCursorMode(CursorMode mode
,
3832 static const wxChar
*cursorModes
[] =
3841 wxLogTrace(_T("grid"),
3842 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3843 win
== m_colLabelWin ?
_T("colLabelWin")
3844 : win ?
_T("rowLabelWin")
3846 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3847 #endif // __WXDEBUG__
3849 if ( mode
== m_cursorMode
)
3854 // by default use the grid itself
3860 m_winCapture
->ReleaseMouse();
3861 m_winCapture
= (wxWindow
*)NULL
;
3864 m_cursorMode
= mode
;
3866 switch ( m_cursorMode
)
3868 case WXGRID_CURSOR_RESIZE_ROW
:
3869 win
->SetCursor( m_rowResizeCursor
);
3872 case WXGRID_CURSOR_RESIZE_COL
:
3873 win
->SetCursor( m_colResizeCursor
);
3877 win
->SetCursor( *wxSTANDARD_CURSOR
);
3880 // we need to capture mouse when resizing
3881 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3882 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3884 if ( captureMouse
&& resize
)
3886 win
->CaptureMouse();
3891 void wxGrid
::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3894 wxPoint
pos( event
.GetPosition() );
3895 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3897 wxGridCellCoords coords
;
3898 XYToCell( x
, y
, coords
);
3900 if ( event
.Dragging() )
3902 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3904 // Don't start doing anything until the mouse has been drug at
3905 // least 3 pixels in any direction...
3908 if (m_startDragPos
== wxDefaultPosition
)
3910 m_startDragPos
= pos
;
3913 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3917 m_isDragging
= TRUE
;
3918 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3920 // Hide the edit control, so it
3921 // won't interfer with drag-shrinking.
3922 if ( IsCellEditControlEnabled() )
3924 HideCellEditControl();
3925 SaveEditControlValue();
3928 // Have we captured the mouse yet?
3931 m_winCapture
= m_gridWin
;
3932 m_winCapture
->CaptureMouse();
3935 if ( coords
!= wxGridNoCellCoords
)
3937 if ( !IsSelection() )
3939 SelectBlock( coords
, coords
);
3943 SelectBlock( m_currentCellCoords
, coords
);
3946 if (! IsVisible(coords
))
3948 MakeCellVisible(coords
);
3949 // TODO: need to introduce a delay or something here. The
3950 // scrolling is way to fast, at least on MSW.
3954 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3956 int cw
, ch
, left
, dummy
;
3957 m_gridWin
->GetClientSize( &cw
, &ch
);
3958 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3960 wxClientDC
dc( m_gridWin
);
3962 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3963 dc
.SetLogicalFunction(wxINVERT
);
3964 if ( m_dragLastPos
>= 0 )
3966 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3968 dc
.DrawLine( left
, y
, left
+cw
, y
);
3971 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3973 int cw
, ch
, dummy
, top
;
3974 m_gridWin
->GetClientSize( &cw
, &ch
);
3975 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3977 wxClientDC
dc( m_gridWin
);
3979 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3980 GetColMinimalWidth(m_dragRowOrCol
) );
3981 dc
.SetLogicalFunction(wxINVERT
);
3982 if ( m_dragLastPos
>= 0 )
3984 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3986 dc
.DrawLine( x
, top
, x
, top
+ch
);
3993 m_isDragging
= FALSE
;
3994 m_startDragPos
= wxDefaultPosition
;
3996 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3997 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4000 if ( event
.Entering() || event
.Leaving() )
4002 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4003 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4008 // ------------ Left button pressed
4010 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4012 if ( event
.ShiftDown() )
4014 SelectBlock( m_currentCellCoords
, coords
);
4016 else if ( XToEdgeOfCol(x
) < 0 &&
4017 YToEdgeOfRow(y
) < 0 )
4019 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4024 DisableCellEditControl();
4025 MakeCellVisible( coords
);
4027 // if this is the second click on this cell then start
4029 if ( m_waitForSlowClick
&&
4030 (coords
== m_currentCellCoords
) &&
4031 CanEnableCellControl())
4033 EnableCellEditControl();
4035 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4036 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
4039 m_waitForSlowClick
= FALSE
;
4043 SetCurrentCell( coords
);
4044 m_waitForSlowClick
= TRUE
;
4051 // ------------ Left double click
4053 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4055 DisableCellEditControl();
4057 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4059 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4067 // ------------ Left button released
4069 else if ( event
.LeftUp() )
4071 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4073 if ( IsSelection() )
4077 m_winCapture
->ReleaseMouse();
4078 m_winCapture
= NULL
;
4080 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4083 // Show the edit control, if it has been hidden for
4085 ShowCellEditControl();
4087 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4089 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4090 DoEndDragResizeRow();
4092 // Note: we are ending the event *after* doing
4093 // default processing in this case
4095 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4097 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4099 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4100 DoEndDragResizeCol();
4102 // Note: we are ending the event *after* doing
4103 // default processing in this case
4105 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4112 // ------------ Right button down
4114 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4116 DisableCellEditControl();
4117 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4122 // no default action at the moment
4127 // ------------ Right double click
4129 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4131 DisableCellEditControl();
4132 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4137 // no default action at the moment
4141 // ------------ Moving and no button action
4143 else if ( event
.Moving() && !event
.IsButton() )
4145 int dragRow
= YToEdgeOfRow( y
);
4146 int dragCol
= XToEdgeOfCol( x
);
4148 // Dragging on the corner of a cell to resize in both
4149 // directions is not implemented yet...
4151 if ( dragRow
>= 0 && dragCol
>= 0 )
4153 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4159 m_dragRowOrCol
= dragRow
;
4161 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4163 if ( CanDragRowSize() && CanDragGridSize() )
4164 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4169 m_dragRowOrCol
= dragCol
;
4177 m_dragRowOrCol
= dragCol
;
4179 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4181 if ( CanDragColSize() && CanDragGridSize() )
4182 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4188 // Neither on a row or col edge
4190 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4192 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4198 void wxGrid
::DoEndDragResizeRow()
4200 if ( m_dragLastPos
>= 0 )
4202 // erase the last line and resize the row
4204 int cw
, ch
, left
, dummy
;
4205 m_gridWin
->GetClientSize( &cw
, &ch
);
4206 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4208 wxClientDC
dc( m_gridWin
);
4210 dc
.SetLogicalFunction( wxINVERT
);
4211 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4212 HideCellEditControl();
4213 SaveEditControlValue();
4215 int rowTop
= GetRowTop(m_dragRowOrCol
);
4216 SetRowSize( m_dragRowOrCol
,
4217 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4219 if ( !GetBatchCount() )
4221 // Only needed to get the correct rect.y:
4222 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4224 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4225 rect
.width
= m_rowLabelWidth
;
4226 rect
.height
= ch
- rect
.y
;
4227 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4229 m_gridWin
->Refresh( FALSE
, &rect
);
4232 ShowCellEditControl();
4237 void wxGrid
::DoEndDragResizeCol()
4239 if ( m_dragLastPos
>= 0 )
4241 // erase the last line and resize the col
4243 int cw
, ch
, dummy
, top
;
4244 m_gridWin
->GetClientSize( &cw
, &ch
);
4245 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4247 wxClientDC
dc( m_gridWin
);
4249 dc
.SetLogicalFunction( wxINVERT
);
4250 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4251 HideCellEditControl();
4252 SaveEditControlValue();
4254 int colLeft
= GetColLeft(m_dragRowOrCol
);
4255 SetColSize( m_dragRowOrCol
,
4256 wxMax( m_dragLastPos
- colLeft
,
4257 GetColMinimalWidth(m_dragRowOrCol
) ) );
4259 if ( !GetBatchCount() )
4261 // Only needed to get the correct rect.x:
4262 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4264 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4265 rect
.width
= cw
- rect
.x
;
4266 rect
.height
= m_colLabelHeight
;
4267 m_colLabelWin
->Refresh( TRUE
, &rect
);
4269 m_gridWin
->Refresh( FALSE
, &rect
);
4272 ShowCellEditControl();
4279 // ------ interaction with data model
4281 bool wxGrid
::ProcessTableMessage( wxGridTableMessage
& msg
)
4283 switch ( msg
.GetId() )
4285 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4286 return GetModelValues();
4288 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4289 return SetModelValues();
4291 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4292 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4293 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4294 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4295 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4296 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4297 return Redimension( msg
);
4306 // The behaviour of this function depends on the grid table class
4307 // Clear() function. For the default wxGridStringTable class the
4308 // behavious is to replace all cell contents with wxEmptyString but
4309 // not to change the number of rows or cols.
4311 void wxGrid
::ClearGrid()
4315 if (IsCellEditControlEnabled())
4316 DisableCellEditControl();
4319 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4324 bool wxGrid
::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4326 // TODO: something with updateLabels flag
4330 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4336 if (IsCellEditControlEnabled())
4337 DisableCellEditControl();
4339 bool ok
= m_table
->InsertRows( pos
, numRows
);
4341 // the table will have sent the results of the insert row
4342 // operation to this view object as a grid table message
4346 if ( m_numCols
== 0 )
4348 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4350 // TODO: perhaps instead of appending the default number of cols
4351 // we should remember what the last non-zero number of cols was ?
4355 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4357 // if we have just inserted cols into an empty grid the current
4358 // cell will be undefined...
4360 SetCurrentCell( 0, 0 );
4364 if ( !GetBatchCount() ) Refresh();
4376 bool wxGrid
::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4378 // TODO: something with updateLabels flag
4382 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4386 if ( m_table
&& m_table
->AppendRows( numRows
) )
4388 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4390 // if we have just inserted cols into an empty grid the current
4391 // cell will be undefined...
4393 SetCurrentCell( 0, 0 );
4396 // the table will have sent the results of the append row
4397 // operation to this view object as a grid table message
4400 if ( !GetBatchCount() ) Refresh();
4410 bool wxGrid
::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4412 // TODO: something with updateLabels flag
4416 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4422 if (IsCellEditControlEnabled())
4423 DisableCellEditControl();
4425 if (m_table
->DeleteRows( pos
, numRows
))
4428 // the table will have sent the results of the delete row
4429 // operation to this view object as a grid table message
4432 if ( !GetBatchCount() ) Refresh();
4440 bool wxGrid
::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4442 // TODO: something with updateLabels flag
4446 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4452 if (IsCellEditControlEnabled())
4453 DisableCellEditControl();
4455 bool ok
= m_table
->InsertCols( pos
, numCols
);
4457 // the table will have sent the results of the insert col
4458 // operation to this view object as a grid table message
4462 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4464 // if we have just inserted cols into an empty grid the current
4465 // cell will be undefined...
4467 SetCurrentCell( 0, 0 );
4471 if ( !GetBatchCount() ) Refresh();
4483 bool wxGrid
::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4485 // TODO: something with updateLabels flag
4489 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4493 if ( m_table
&& m_table
->AppendCols( numCols
) )
4495 // the table will have sent the results of the append col
4496 // operation to this view object as a grid table message
4498 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4500 // if we have just inserted cols into an empty grid the current
4501 // cell will be undefined...
4503 SetCurrentCell( 0, 0 );
4507 if ( !GetBatchCount() ) Refresh();
4517 bool wxGrid
::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4519 // TODO: something with updateLabels flag
4523 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4529 if (IsCellEditControlEnabled())
4530 DisableCellEditControl();
4532 if ( m_table
->DeleteCols( pos
, numCols
) )
4534 // the table will have sent the results of the delete col
4535 // operation to this view object as a grid table message
4538 if ( !GetBatchCount() ) Refresh();
4548 // ----- event handlers
4551 // Generate a grid event based on a mouse event and
4552 // return the result of ProcessEvent()
4554 bool wxGrid
::SendEvent( const wxEventType type
,
4556 wxMouseEvent
& mouseEv
)
4558 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4560 int rowOrCol
= (row
== -1 ? col
: row
);
4562 wxGridSizeEvent
gridEvt( GetId(),
4566 mouseEv
.GetX(), mouseEv
.GetY(),
4567 mouseEv
.ControlDown(),
4568 mouseEv
.ShiftDown(),
4570 mouseEv
.MetaDown() );
4572 return GetEventHandler()->ProcessEvent(gridEvt
);
4574 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4576 wxGridRangeSelectEvent
gridEvt( GetId(),
4580 m_selectedBottomRight
,
4581 mouseEv
.ControlDown(),
4582 mouseEv
.ShiftDown(),
4584 mouseEv
.MetaDown() );
4586 return GetEventHandler()->ProcessEvent(gridEvt
);
4590 wxGridEvent
gridEvt( GetId(),
4594 mouseEv
.GetX(), mouseEv
.GetY(),
4595 mouseEv
.ControlDown(),
4596 mouseEv
.ShiftDown(),
4598 mouseEv
.MetaDown() );
4600 return GetEventHandler()->ProcessEvent(gridEvt
);
4605 // Generate a grid event of specified type and return the result
4606 // of ProcessEvent().
4608 bool wxGrid
::SendEvent( const wxEventType type
,
4611 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4613 int rowOrCol
= (row
== -1 ? col
: row
);
4615 wxGridSizeEvent
gridEvt( GetId(),
4620 return GetEventHandler()->ProcessEvent(gridEvt
);
4624 wxGridEvent
gridEvt( GetId(),
4629 return GetEventHandler()->ProcessEvent(gridEvt
);
4634 void wxGrid
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4636 wxPaintDC
dc( this );
4638 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4639 m_numRows
&& m_numCols
)
4641 m_currentCellCoords
.Set(0, 0);
4642 ShowCellEditControl();
4649 // This is just here to make sure that CalcDimensions gets called when
4650 // the grid view is resized... then the size event is skipped to allow
4651 // the box sizers to handle everything
4653 void wxGrid
::OnSize( wxSizeEvent
& event
)
4660 void wxGrid
::OnKeyDown( wxKeyEvent
& event
)
4662 if ( m_inOnKeyDown
)
4664 // shouldn't be here - we are going round in circles...
4666 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4669 m_inOnKeyDown
= TRUE
;
4671 // propagate the event up and see if it gets processed
4673 wxWindow
*parent
= GetParent();
4674 wxKeyEvent
keyEvt( event
);
4675 keyEvt
.SetEventObject( parent
);
4677 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4680 // TODO: Should also support Shift-cursor keys for
4681 // extending the selection. Maybe add a flag to
4682 // MoveCursorXXX() and MoveCursorXXXBlock() and
4683 // just send event.ShiftDown().
4685 // try local handlers
4687 switch ( event
.KeyCode() )
4690 if ( event
.ControlDown() )
4692 MoveCursorUpBlock();
4701 if ( event
.ControlDown() )
4703 MoveCursorDownBlock();
4712 if ( event
.ControlDown() )
4714 MoveCursorLeftBlock();
4723 if ( event
.ControlDown() )
4725 MoveCursorRightBlock();
4734 if ( event
.ControlDown() )
4736 event
.Skip(); // to let the edit control have the return
4745 if (event
.ShiftDown())
4752 if ( event
.ControlDown() )
4754 MakeCellVisible( 0, 0 );
4755 SetCurrentCell( 0, 0 );
4764 if ( event
.ControlDown() )
4766 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4767 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4784 if ( !IsEditable() )
4789 // Otherwise fall through to default
4792 // alphanumeric keys or F2 (special key just for this) enable
4793 // the cell edit control
4794 if ( !(event
.AltDown() ||
4796 event
.ControlDown()) &&
4797 (isalnum(event
.KeyCode()) || event
.KeyCode() == WXK_F2
) &&
4798 !IsCellEditControlEnabled() &&
4799 CanEnableCellControl() )
4801 EnableCellEditControl();
4802 int row
= m_currentCellCoords
.GetRow();
4803 int col
= m_currentCellCoords
.GetCol();
4804 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4805 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4810 // let others process char events with modifiers or all
4811 // char events for readonly cells
4818 m_inOnKeyDown
= FALSE
;
4822 void wxGrid
::OnEraseBackground(wxEraseEvent
&)
4826 void wxGrid
::SetCurrentCell( const wxGridCellCoords
& coords
)
4828 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4830 // the event has been intercepted - do nothing
4835 m_currentCellCoords
!= wxGridNoCellCoords
)
4837 HideCellEditControl();
4838 DisableCellEditControl();
4840 // Clear the old current cell highlight
4841 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4843 // Otherwise refresh redraws the highlight!
4844 m_currentCellCoords
= coords
;
4846 m_gridWin
->Refresh( FALSE
, &r
);
4849 m_currentCellCoords
= coords
;
4853 wxClientDC
dc(m_gridWin
);
4856 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4857 DrawCellHighlight(dc
, attr
);
4860 if ( IsSelection() )
4862 wxRect
r( SelectionToDeviceRect() );
4864 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4871 // ------ functions to get/send data (see also public functions)
4874 bool wxGrid
::GetModelValues()
4878 // all we need to do is repaint the grid
4880 m_gridWin
->Refresh();
4888 bool wxGrid
::SetModelValues()
4894 for ( row
= 0; row
< m_numRows
; row
++ )
4896 for ( col
= 0; col
< m_numCols
; col
++ )
4898 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4910 // Note - this function only draws cells that are in the list of
4911 // exposed cells (usually set from the update region by
4912 // CalcExposedCells)
4914 void wxGrid
::DrawGridCellArea( wxDC
& dc
)
4916 if ( !m_numRows
|| !m_numCols
) return;
4919 size_t numCells
= m_cellsExposed
.GetCount();
4921 for ( i
= 0; i
< numCells
; i
++ )
4923 DrawCell( dc
, m_cellsExposed
[i
] );
4928 void wxGrid
::DrawGridSpace( wxDC
& dc
)
4930 if ( m_numRows
&& m_numCols
)
4933 m_gridWin
->GetClientSize( &cw
, &ch
);
4936 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4938 if ( right
> GetColRight(m_numCols
-1) ||
4939 bottom
> GetRowBottom(m_numRows
-1) )
4942 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4944 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4945 dc
.SetPen( *wxTRANSPARENT_PEN
);
4947 if ( right
> GetColRight(m_numCols
-1) )
4948 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
4949 right
- GetColRight(m_numCols
-1), ch
);
4951 if ( bottom
> GetRowBottom(m_numRows
-1) )
4952 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
4953 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4959 void wxGrid
::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4961 int row
= coords
.GetRow();
4962 int col
= coords
.GetCol();
4964 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4967 // we draw the cell border ourselves
4968 #if !WXGRID_DRAW_LINES
4969 if ( m_gridLinesEnabled
)
4970 DrawCellBorder( dc
, coords
);
4973 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4975 bool isCurrent
= coords
== m_currentCellCoords
;
4978 rect
.x
= GetColLeft(col
);
4979 rect
.y
= GetRowTop(row
);
4980 rect
.width
= GetColWidth(col
) - 1;
4981 rect
.height
= GetRowHeight(row
) - 1;
4983 // if the editor is shown, we should use it and not the renderer
4984 if ( isCurrent
&& IsCellEditControlEnabled() )
4986 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4990 // but all the rest is drawn by the cell renderer and hence may be
4992 attr
->GetRenderer(this, row
, col
)->
4993 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5000 void wxGrid
::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5002 int row
= m_currentCellCoords
.GetRow();
5003 int col
= m_currentCellCoords
.GetCol();
5005 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5009 rect
.x
= GetColLeft(col
);
5010 rect
.y
= GetRowTop(row
);
5011 rect
.width
= GetColWidth(col
) - 1;
5012 rect
.height
= GetRowHeight(row
) - 1;
5014 // hmmm... what could we do here to show that the cell is disabled?
5015 // for now, I just draw a thinner border than for the other ones, but
5016 // it doesn't look really good
5017 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ?
1 : 3, wxSOLID
));
5018 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5020 dc
.DrawRectangle(rect
);
5023 // VZ: my experiments with 3d borders...
5025 // how to properly set colours for arbitrary bg?
5026 wxCoord x1
= rect
.x
,
5028 x2
= rect
.x
+ rect
.width
-1,
5029 y2
= rect
.y
+ rect
.height
-1;
5031 dc
.SetPen(*wxWHITE_PEN
);
5032 dc
.DrawLine(x1
, y1
, x2
, y1
);
5033 dc
.DrawLine(x1
, y1
, x1
, y2
);
5035 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5036 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5038 dc
.SetPen(*wxBLACK_PEN
);
5039 dc
.DrawLine(x1
, y2
, x2
, y2
);
5040 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5045 void wxGrid
::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5047 int row
= coords
.GetRow();
5048 int col
= coords
.GetCol();
5049 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5052 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5054 // right hand border
5056 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5057 GetColRight(col
), GetRowBottom(row
) );
5061 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5062 GetColRight(col
), GetRowBottom(row
) );
5065 void wxGrid
::DrawHighlight(wxDC
& dc
)
5067 if ( IsCellEditControlEnabled() )
5069 // don't show highlight when the edit control is shown
5073 // if the active cell was repainted, repaint its highlight too because it
5074 // might have been damaged by the grid lines
5075 size_t count
= m_cellsExposed
.GetCount();
5076 for ( size_t n
= 0; n
< count
; n
++ )
5078 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5080 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5081 DrawCellHighlight(dc
, attr
);
5089 // TODO: remove this ???
5090 // This is used to redraw all grid lines e.g. when the grid line colour
5093 void wxGrid
::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5095 if ( !m_gridLinesEnabled
||
5097 !m_numCols
) return;
5099 int top
, bottom
, left
, right
;
5105 m_gridWin
->GetClientSize(&cw
, &ch
);
5107 // virtual coords of visible area
5109 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5110 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5115 reg
.GetBox(x
, y
, w
, h
);
5116 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5117 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5121 m_gridWin
->GetClientSize(&cw
, &ch
);
5122 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5123 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5126 // avoid drawing grid lines past the last row and col
5128 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5129 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5131 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5133 // horizontal grid lines
5136 for ( i
= 0; i
< m_numRows
; i
++ )
5138 int bot
= GetRowBottom(i
) - 1;
5147 dc
.DrawLine( left
, bot
, right
, bot
);
5152 // vertical grid lines
5154 for ( i
= 0; i
< m_numCols
; i
++ )
5156 int colRight
= GetColRight(i
) - 1;
5157 if ( colRight
> right
)
5162 if ( colRight
>= left
)
5164 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5170 void wxGrid
::DrawRowLabels( wxDC
& dc
)
5172 if ( !m_numRows
|| !m_numCols
) return;
5175 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5177 for ( i
= 0; i
< numLabels
; i
++ )
5179 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5184 void wxGrid
::DrawRowLabel( wxDC
& dc
, int row
)
5186 if ( GetRowHeight(row
) <= 0 )
5189 int rowTop
= GetRowTop(row
),
5190 rowBottom
= GetRowBottom(row
) - 1;
5192 dc
.SetPen( *wxBLACK_PEN
);
5193 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5194 m_rowLabelWidth
-1, rowBottom
);
5196 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5198 dc
.SetPen( *wxWHITE_PEN
);
5199 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5200 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5202 dc
.SetBackgroundMode( wxTRANSPARENT
);
5203 dc
.SetTextForeground( GetLabelTextColour() );
5204 dc
.SetFont( GetLabelFont() );
5207 GetRowLabelAlignment( &hAlign
, &vAlign
);
5211 rect
.SetY( GetRowTop(row
) + 2 );
5212 rect
.SetWidth( m_rowLabelWidth
- 4 );
5213 rect
.SetHeight( GetRowHeight(row
) - 4 );
5214 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5218 void wxGrid
::DrawColLabels( wxDC
& dc
)
5220 if ( !m_numRows
|| !m_numCols
) return;
5223 size_t numLabels
= m_colLabelsExposed
.GetCount();
5225 for ( i
= 0; i
< numLabels
; i
++ )
5227 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5232 void wxGrid
::DrawColLabel( wxDC
& dc
, int col
)
5234 if ( GetColWidth(col
) <= 0 )
5237 int colLeft
= GetColLeft(col
),
5238 colRight
= GetColRight(col
) - 1;
5240 dc
.SetPen( *wxBLACK_PEN
);
5241 dc
.DrawLine( colRight
, 0,
5242 colRight
, m_colLabelHeight
-1 );
5244 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5245 colRight
, m_colLabelHeight
-1 );
5247 dc
.SetPen( *wxWHITE_PEN
);
5248 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5249 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5251 dc
.SetBackgroundMode( wxTRANSPARENT
);
5252 dc
.SetTextForeground( GetLabelTextColour() );
5253 dc
.SetFont( GetLabelFont() );
5255 dc
.SetBackgroundMode( wxTRANSPARENT
);
5256 dc
.SetTextForeground( GetLabelTextColour() );
5257 dc
.SetFont( GetLabelFont() );
5260 GetColLabelAlignment( &hAlign
, &vAlign
);
5263 rect
.SetX( colLeft
+ 2 );
5265 rect
.SetWidth( GetColWidth(col
) - 4 );
5266 rect
.SetHeight( m_colLabelHeight
- 4 );
5267 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5271 void wxGrid
::DrawTextRectangle( wxDC
& dc
,
5272 const wxString
& value
,
5277 long textWidth
, textHeight
;
5278 long lineWidth
, lineHeight
;
5279 wxArrayString lines
;
5281 dc
.SetClippingRegion( rect
);
5282 StringToLines( value
, lines
);
5283 if ( lines
.GetCount() )
5285 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5286 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5289 switch ( horizAlign
)
5292 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5296 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5305 switch ( vertAlign
)
5308 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5312 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5321 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5323 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5328 dc
.DestroyClippingRegion();
5332 // Split multi line text up into an array of strings. Any existing
5333 // contents of the string array are preserved.
5335 void wxGrid
::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5339 wxString eol
= wxTextFile
::GetEOL( wxTextFileType_Unix
);
5340 wxString tVal
= wxTextFile
::Translate( value
, wxTextFileType_Unix
);
5342 while ( startPos
< (int)tVal
.Length() )
5344 pos
= tVal
.Mid(startPos
).Find( eol
);
5349 else if ( pos
== 0 )
5351 lines
.Add( wxEmptyString
);
5355 lines
.Add( value
.Mid(startPos
, pos
) );
5359 if ( startPos
< (int)value
.Length() )
5361 lines
.Add( value
.Mid( startPos
) );
5366 void wxGrid
::GetTextBoxSize( wxDC
& dc
,
5367 wxArrayString
& lines
,
5368 long *width
, long *height
)
5375 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5377 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5378 w
= wxMax( w
, lineW
);
5388 // ------ Edit control functions
5392 void wxGrid
::EnableEditing( bool edit
)
5394 // TODO: improve this ?
5396 if ( edit
!= m_editable
)
5400 // FIXME IMHO this won't disable the edit control if edit == FALSE
5401 // because of the check in the beginning of
5402 // EnableCellEditControl() just below (VZ)
5403 EnableCellEditControl(m_editable
);
5408 void wxGrid
::EnableCellEditControl( bool enable
)
5413 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5414 SetCurrentCell( 0, 0 );
5416 if ( enable
!= m_cellEditCtrlEnabled
)
5418 // TODO allow the app to Veto() this event?
5419 SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5423 // this should be checked by the caller!
5424 wxASSERT_MSG( CanEnableCellControl(),
5425 _T("can't enable editing for this cell!") );
5427 // do it before ShowCellEditControl()
5428 m_cellEditCtrlEnabled
= enable
;
5430 ShowCellEditControl();
5434 HideCellEditControl();
5435 SaveEditControlValue();
5437 // do it after HideCellEditControl()
5438 m_cellEditCtrlEnabled
= enable
;
5443 bool wxGrid
::IsCurrentCellReadOnly() const
5446 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5447 bool readonly
= attr
->IsReadOnly();
5453 bool wxGrid
::CanEnableCellControl() const
5455 return m_editable
&& !IsCurrentCellReadOnly();
5458 bool wxGrid
::IsCellEditControlEnabled() const
5460 // the cell edit control might be disable for all cells or just for the
5461 // current one if it's read only
5462 return m_cellEditCtrlEnabled ?
!IsCurrentCellReadOnly() : FALSE
;
5465 void wxGrid
::ShowCellEditControl()
5467 if ( IsCellEditControlEnabled() )
5469 if ( !IsVisible( m_currentCellCoords
) )
5475 wxRect rect
= CellToRect( m_currentCellCoords
);
5476 int row
= m_currentCellCoords
.GetRow();
5477 int col
= m_currentCellCoords
.GetCol();
5479 // convert to scrolled coords
5481 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5483 // done in PaintBackground()
5485 // erase the highlight and the cell contents because the editor
5486 // might not cover the entire cell
5487 wxClientDC
dc( m_gridWin
);
5489 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5490 dc
.SetPen(*wxTRANSPARENT_PEN
);
5491 dc
.DrawRectangle(rect
);
5494 // cell is shifted by one pixel
5498 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5499 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5500 if ( !editor
->IsCreated() )
5502 editor
->Create(m_gridWin
, -1,
5503 new wxGridCellEditorEvtHandler(this, editor
));
5506 editor
->Show( TRUE
, attr
);
5508 editor
->SetSize( rect
);
5510 editor
->BeginEdit(row
, col
, this);
5517 void wxGrid
::HideCellEditControl()
5519 if ( IsCellEditControlEnabled() )
5521 int row
= m_currentCellCoords
.GetRow();
5522 int col
= m_currentCellCoords
.GetCol();
5524 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5525 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5527 m_gridWin
->SetFocus();
5532 void wxGrid
::SaveEditControlValue()
5534 if ( IsCellEditControlEnabled() )
5536 int row
= m_currentCellCoords
.GetRow();
5537 int col
= m_currentCellCoords
.GetCol();
5539 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5540 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5541 bool changed
= editor
->EndEdit(row
, col
, this);
5547 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5548 m_currentCellCoords
.GetRow(),
5549 m_currentCellCoords
.GetCol() );
5556 // ------ Grid location functions
5557 // Note that all of these functions work with the logical coordinates of
5558 // grid cells and labels so you will need to convert from device
5559 // coordinates for mouse events etc.
5562 void wxGrid
::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5564 int row
= YToRow(y
);
5565 int col
= XToCol(x
);
5567 if ( row
== -1 || col
== -1 )
5569 coords
= wxGridNoCellCoords
;
5573 coords
.Set( row
, col
);
5578 int wxGrid
::YToRow( int y
)
5582 for ( i
= 0; i
< m_numRows
; i
++ )
5584 if ( y
< GetRowBottom(i
) )
5592 int wxGrid
::XToCol( int x
)
5596 for ( i
= 0; i
< m_numCols
; i
++ )
5598 if ( x
< GetColRight(i
) )
5606 // return the row number that that the y coord is near the edge of, or
5607 // -1 if not near an edge
5609 int wxGrid
::YToEdgeOfRow( int y
)
5613 for ( i
= 0; i
< m_numRows
; i
++ )
5615 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5617 d
= abs( y
- GetRowBottom(i
) );
5618 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5627 // return the col number that that the x coord is near the edge of, or
5628 // -1 if not near an edge
5630 int wxGrid
::XToEdgeOfCol( int x
)
5634 for ( i
= 0; i
< m_numCols
; i
++ )
5636 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5638 d
= abs( x
- GetColRight(i
) );
5639 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5648 wxRect wxGrid
::CellToRect( int row
, int col
)
5650 wxRect
rect( -1, -1, -1, -1 );
5652 if ( row
>= 0 && row
< m_numRows
&&
5653 col
>= 0 && col
< m_numCols
)
5655 rect
.x
= GetColLeft(col
);
5656 rect
.y
= GetRowTop(row
);
5657 rect
.width
= GetColWidth(col
);
5658 rect
.height
= GetRowHeight(row
);
5665 bool wxGrid
::IsVisible( int row
, int col
, bool wholeCellVisible
)
5667 // get the cell rectangle in logical coords
5669 wxRect
r( CellToRect( row
, col
) );
5671 // convert to device coords
5673 int left
, top
, right
, bottom
;
5674 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5675 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5677 // check against the client area of the grid window
5680 m_gridWin
->GetClientSize( &cw
, &ch
);
5682 if ( wholeCellVisible
)
5684 // is the cell wholly visible ?
5686 return ( left
>= 0 && right
<= cw
&&
5687 top
>= 0 && bottom
<= ch
);
5691 // is the cell partly visible ?
5693 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5694 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5699 // make the specified cell location visible by doing a minimal amount
5702 void wxGrid
::MakeCellVisible( int row
, int col
)
5705 int xpos
= -1, ypos
= -1;
5707 if ( row
>= 0 && row
< m_numRows
&&
5708 col
>= 0 && col
< m_numCols
)
5710 // get the cell rectangle in logical coords
5712 wxRect
r( CellToRect( row
, col
) );
5714 // convert to device coords
5716 int left
, top
, right
, bottom
;
5717 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5718 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5721 m_gridWin
->GetClientSize( &cw
, &ch
);
5727 else if ( bottom
> ch
)
5729 int h
= r
.GetHeight();
5731 for ( i
= row
-1; i
>= 0; i
-- )
5733 int rowHeight
= GetRowHeight(i
);
5734 if ( h
+ rowHeight
> ch
)
5741 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5742 // have rounding errors (this is important, because if we do, we
5743 // might not scroll at all and some cells won't be redrawn)
5744 ypos
+= GRID_SCROLL_LINE
/ 2;
5751 else if ( right
> cw
)
5753 int w
= r
.GetWidth();
5755 for ( i
= col
-1; i
>= 0; i
-- )
5757 int colWidth
= GetColWidth(i
);
5758 if ( w
+ colWidth
> cw
)
5765 // see comment for ypos above
5766 xpos
+= GRID_SCROLL_LINE
/ 2;
5769 if ( xpos
!= -1 || ypos
!= -1 )
5771 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5772 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5773 Scroll( xpos
, ypos
);
5781 // ------ Grid cursor movement functions
5784 bool wxGrid
::MoveCursorUp()
5786 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5787 m_currentCellCoords
.GetRow() > 0 )
5789 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5790 m_currentCellCoords
.GetCol() );
5792 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5793 m_currentCellCoords
.GetCol() );
5802 bool wxGrid
::MoveCursorDown()
5804 // TODO: allow for scrolling
5806 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5807 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5809 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5810 m_currentCellCoords
.GetCol() );
5812 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5813 m_currentCellCoords
.GetCol() );
5822 bool wxGrid
::MoveCursorLeft()
5824 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5825 m_currentCellCoords
.GetCol() > 0 )
5827 MakeCellVisible( m_currentCellCoords
.GetRow(),
5828 m_currentCellCoords
.GetCol() - 1 );
5830 SetCurrentCell( m_currentCellCoords
.GetRow(),
5831 m_currentCellCoords
.GetCol() - 1 );
5840 bool wxGrid
::MoveCursorRight()
5842 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5843 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5845 MakeCellVisible( m_currentCellCoords
.GetRow(),
5846 m_currentCellCoords
.GetCol() + 1 );
5848 SetCurrentCell( m_currentCellCoords
.GetRow(),
5849 m_currentCellCoords
.GetCol() + 1 );
5858 bool wxGrid
::MovePageUp()
5860 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5862 int row
= m_currentCellCoords
.GetRow();
5866 m_gridWin
->GetClientSize( &cw
, &ch
);
5868 int y
= GetRowTop(row
);
5869 int newRow
= YToRow( y
- ch
+ 1 );
5874 else if ( newRow
== row
)
5879 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5880 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5888 bool wxGrid
::MovePageDown()
5890 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5892 int row
= m_currentCellCoords
.GetRow();
5893 if ( row
< m_numRows
)
5896 m_gridWin
->GetClientSize( &cw
, &ch
);
5898 int y
= GetRowTop(row
);
5899 int newRow
= YToRow( y
+ ch
);
5902 newRow
= m_numRows
- 1;
5904 else if ( newRow
== row
)
5909 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5910 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5918 bool wxGrid
::MoveCursorUpBlock()
5921 m_currentCellCoords
!= wxGridNoCellCoords
&&
5922 m_currentCellCoords
.GetRow() > 0 )
5924 int row
= m_currentCellCoords
.GetRow();
5925 int col
= m_currentCellCoords
.GetCol();
5927 if ( m_table
->IsEmptyCell(row
, col
) )
5929 // starting in an empty cell: find the next block of
5935 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5938 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5940 // starting at the top of a block: find the next block
5946 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5951 // starting within a block: find the top of the block
5956 if ( m_table
->IsEmptyCell(row
, col
) )
5964 MakeCellVisible( row
, col
);
5965 SetCurrentCell( row
, col
);
5973 bool wxGrid
::MoveCursorDownBlock()
5976 m_currentCellCoords
!= wxGridNoCellCoords
&&
5977 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5979 int row
= m_currentCellCoords
.GetRow();
5980 int col
= m_currentCellCoords
.GetCol();
5982 if ( m_table
->IsEmptyCell(row
, col
) )
5984 // starting in an empty cell: find the next block of
5987 while ( row
< m_numRows
-1 )
5990 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5993 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5995 // starting at the bottom of a block: find the next block
5998 while ( row
< m_numRows
-1 )
6001 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6006 // starting within a block: find the bottom of the block
6008 while ( row
< m_numRows
-1 )
6011 if ( m_table
->IsEmptyCell(row
, col
) )
6019 MakeCellVisible( row
, col
);
6020 SetCurrentCell( row
, col
);
6028 bool wxGrid
::MoveCursorLeftBlock()
6031 m_currentCellCoords
!= wxGridNoCellCoords
&&
6032 m_currentCellCoords
.GetCol() > 0 )
6034 int row
= m_currentCellCoords
.GetRow();
6035 int col
= m_currentCellCoords
.GetCol();
6037 if ( m_table
->IsEmptyCell(row
, col
) )
6039 // starting in an empty cell: find the next block of
6045 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6048 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6050 // starting at the left of a block: find the next block
6056 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6061 // starting within a block: find the left of the block
6066 if ( m_table
->IsEmptyCell(row
, col
) )
6074 MakeCellVisible( row
, col
);
6075 SetCurrentCell( row
, col
);
6083 bool wxGrid
::MoveCursorRightBlock()
6086 m_currentCellCoords
!= wxGridNoCellCoords
&&
6087 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6089 int row
= m_currentCellCoords
.GetRow();
6090 int col
= m_currentCellCoords
.GetCol();
6092 if ( m_table
->IsEmptyCell(row
, col
) )
6094 // starting in an empty cell: find the next block of
6097 while ( col
< m_numCols
-1 )
6100 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6103 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6105 // starting at the right of a block: find the next block
6108 while ( col
< m_numCols
-1 )
6111 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6116 // starting within a block: find the right of the block
6118 while ( col
< m_numCols
-1 )
6121 if ( m_table
->IsEmptyCell(row
, col
) )
6129 MakeCellVisible( row
, col
);
6130 SetCurrentCell( row
, col
);
6141 // ------ Label values and formatting
6144 void wxGrid
::GetRowLabelAlignment( int *horiz
, int *vert
)
6146 *horiz
= m_rowLabelHorizAlign
;
6147 *vert
= m_rowLabelVertAlign
;
6150 void wxGrid
::GetColLabelAlignment( int *horiz
, int *vert
)
6152 *horiz
= m_colLabelHorizAlign
;
6153 *vert
= m_colLabelVertAlign
;
6156 wxString wxGrid
::GetRowLabelValue( int row
)
6160 return m_table
->GetRowLabelValue( row
);
6170 wxString wxGrid
::GetColLabelValue( int col
)
6174 return m_table
->GetColLabelValue( col
);
6185 void wxGrid
::SetRowLabelSize( int width
)
6187 width
= wxMax( width
, 0 );
6188 if ( width
!= m_rowLabelWidth
)
6192 m_rowLabelWin
->Show( FALSE
);
6193 m_cornerLabelWin
->Show( FALSE
);
6195 else if ( m_rowLabelWidth
== 0 )
6197 m_rowLabelWin
->Show( TRUE
);
6198 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6201 m_rowLabelWidth
= width
;
6208 void wxGrid
::SetColLabelSize( int height
)
6210 height
= wxMax( height
, 0 );
6211 if ( height
!= m_colLabelHeight
)
6215 m_colLabelWin
->Show( FALSE
);
6216 m_cornerLabelWin
->Show( FALSE
);
6218 else if ( m_colLabelHeight
== 0 )
6220 m_colLabelWin
->Show( TRUE
);
6221 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6224 m_colLabelHeight
= height
;
6231 void wxGrid
::SetLabelBackgroundColour( const wxColour
& colour
)
6233 if ( m_labelBackgroundColour
!= colour
)
6235 m_labelBackgroundColour
= colour
;
6236 m_rowLabelWin
->SetBackgroundColour( colour
);
6237 m_colLabelWin
->SetBackgroundColour( colour
);
6238 m_cornerLabelWin
->SetBackgroundColour( colour
);
6240 if ( !GetBatchCount() )
6242 m_rowLabelWin
->Refresh();
6243 m_colLabelWin
->Refresh();
6244 m_cornerLabelWin
->Refresh();
6249 void wxGrid
::SetLabelTextColour( const wxColour
& colour
)
6251 if ( m_labelTextColour
!= colour
)
6253 m_labelTextColour
= colour
;
6254 if ( !GetBatchCount() )
6256 m_rowLabelWin
->Refresh();
6257 m_colLabelWin
->Refresh();
6262 void wxGrid
::SetLabelFont( const wxFont
& font
)
6265 if ( !GetBatchCount() )
6267 m_rowLabelWin
->Refresh();
6268 m_colLabelWin
->Refresh();
6272 void wxGrid
::SetRowLabelAlignment( int horiz
, int vert
)
6274 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6276 m_rowLabelHorizAlign
= horiz
;
6279 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6281 m_rowLabelVertAlign
= vert
;
6284 if ( !GetBatchCount() )
6286 m_rowLabelWin
->Refresh();
6290 void wxGrid
::SetColLabelAlignment( int horiz
, int vert
)
6292 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6294 m_colLabelHorizAlign
= horiz
;
6297 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6299 m_colLabelVertAlign
= vert
;
6302 if ( !GetBatchCount() )
6304 m_colLabelWin
->Refresh();
6308 void wxGrid
::SetRowLabelValue( int row
, const wxString
& s
)
6312 m_table
->SetRowLabelValue( row
, s
);
6313 if ( !GetBatchCount() )
6315 wxRect rect
= CellToRect( row
, 0);
6316 if ( rect
.height
> 0 )
6318 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6320 rect
.width
= m_rowLabelWidth
;
6321 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6327 void wxGrid
::SetColLabelValue( int col
, const wxString
& s
)
6331 m_table
->SetColLabelValue( col
, s
);
6332 if ( !GetBatchCount() )
6334 wxRect rect
= CellToRect( 0, col
);
6335 if ( rect
.width
> 0 )
6337 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6339 rect
.height
= m_colLabelHeight
;
6340 m_colLabelWin
->Refresh( TRUE
, &rect
);
6346 void wxGrid
::SetGridLineColour( const wxColour
& colour
)
6348 if ( m_gridLineColour
!= colour
)
6350 m_gridLineColour
= colour
;
6352 wxClientDC
dc( m_gridWin
);
6354 DrawAllGridLines( dc
, wxRegion() );
6358 void wxGrid
::EnableGridLines( bool enable
)
6360 if ( enable
!= m_gridLinesEnabled
)
6362 m_gridLinesEnabled
= enable
;
6364 if ( !GetBatchCount() )
6368 wxClientDC
dc( m_gridWin
);
6370 DrawAllGridLines( dc
, wxRegion() );
6374 m_gridWin
->Refresh();
6381 int wxGrid
::GetDefaultRowSize()
6383 return m_defaultRowHeight
;
6386 int wxGrid
::GetRowSize( int row
)
6388 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6390 return GetRowHeight(row
);
6393 int wxGrid
::GetDefaultColSize()
6395 return m_defaultColWidth
;
6398 int wxGrid
::GetColSize( int col
)
6400 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6402 return GetColWidth(col
);
6405 // ============================================================================
6406 // access to the grid attributes: each of them has a default value in the grid
6407 // itself and may be overidden on a per-cell basis
6408 // ============================================================================
6410 // ----------------------------------------------------------------------------
6411 // setting default attributes
6412 // ----------------------------------------------------------------------------
6414 void wxGrid
::SetDefaultCellBackgroundColour( const wxColour
& col
)
6416 m_defaultCellAttr
->SetBackgroundColour(col
);
6418 m_gridWin
->SetBackgroundColour(col
);
6422 void wxGrid
::SetDefaultCellTextColour( const wxColour
& col
)
6424 m_defaultCellAttr
->SetTextColour(col
);
6427 void wxGrid
::SetDefaultCellAlignment( int horiz
, int vert
)
6429 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6432 void wxGrid
::SetDefaultCellFont( const wxFont
& font
)
6434 m_defaultCellAttr
->SetFont(font
);
6437 void wxGrid
::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6439 m_defaultCellAttr
->SetRenderer(renderer
);
6442 void wxGrid
::SetDefaultEditor(wxGridCellEditor
*editor
)
6444 m_defaultCellAttr
->SetEditor(editor
);
6447 // ----------------------------------------------------------------------------
6448 // access to the default attrbiutes
6449 // ----------------------------------------------------------------------------
6451 wxColour wxGrid
::GetDefaultCellBackgroundColour()
6453 return m_defaultCellAttr
->GetBackgroundColour();
6456 wxColour wxGrid
::GetDefaultCellTextColour()
6458 return m_defaultCellAttr
->GetTextColour();
6461 wxFont wxGrid
::GetDefaultCellFont()
6463 return m_defaultCellAttr
->GetFont();
6466 void wxGrid
::GetDefaultCellAlignment( int *horiz
, int *vert
)
6468 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6471 wxGridCellRenderer
*wxGrid
::GetDefaultRenderer() const
6473 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6476 wxGridCellEditor
*wxGrid
::GetDefaultEditor() const
6478 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6481 // ----------------------------------------------------------------------------
6482 // access to cell attributes
6483 // ----------------------------------------------------------------------------
6485 wxColour wxGrid
::GetCellBackgroundColour(int row
, int col
)
6487 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6488 wxColour colour
= attr
->GetBackgroundColour();
6493 wxColour wxGrid
::GetCellTextColour( int row
, int col
)
6495 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6496 wxColour colour
= attr
->GetTextColour();
6501 wxFont wxGrid
::GetCellFont( int row
, int col
)
6503 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6504 wxFont font
= attr
->GetFont();
6509 void wxGrid
::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6511 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6512 attr
->GetAlignment(horiz
, vert
);
6516 wxGridCellRenderer
* wxGrid
::GetCellRenderer(int row
, int col
)
6518 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6519 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6524 wxGridCellEditor
* wxGrid
::GetCellEditor(int row
, int col
)
6526 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6527 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6532 bool wxGrid
::IsReadOnly(int row
, int col
) const
6534 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6535 bool isReadOnly
= attr
->IsReadOnly();
6540 // ----------------------------------------------------------------------------
6541 // attribute support: cache, automatic provider creation, ...
6542 // ----------------------------------------------------------------------------
6544 bool wxGrid
::CanHaveAttributes()
6551 return m_table
->CanHaveAttributes();
6554 void wxGrid
::ClearAttrCache()
6556 if ( m_attrCache
.row
!= -1 )
6558 m_attrCache
.attr
->SafeDecRef();
6559 m_attrCache
.row
= -1;
6563 void wxGrid
::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6565 wxGrid
*self
= (wxGrid
*)this; // const_cast
6567 self
->ClearAttrCache();
6568 self
->m_attrCache
.row
= row
;
6569 self
->m_attrCache
.col
= col
;
6570 self
->m_attrCache
.attr
= attr
;
6574 bool wxGrid
::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6576 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6578 *attr
= m_attrCache
.attr
;
6579 (*attr
)->SafeIncRef();
6581 #ifdef DEBUG_ATTR_CACHE
6582 gs_nAttrCacheHits
++;
6589 #ifdef DEBUG_ATTR_CACHE
6590 gs_nAttrCacheMisses
++;
6596 wxGridCellAttr
*wxGrid
::GetCellAttr(int row
, int col
) const
6598 wxGridCellAttr
*attr
;
6599 if ( !LookupAttr(row
, col
, &attr
) )
6601 attr
= m_table ? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6602 CacheAttr(row
, col
, attr
);
6606 attr
->SetDefAttr(m_defaultCellAttr
);
6610 attr
= m_defaultCellAttr
;
6617 wxGridCellAttr
*wxGrid
::GetOrCreateCellAttr(int row
, int col
) const
6619 wxGridCellAttr
*attr
;
6620 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6622 wxASSERT_MSG( m_table
,
6623 _T("we may only be called if CanHaveAttributes() "
6624 "returned TRUE and then m_table should be !NULL") );
6626 attr
= m_table
->GetAttr(row
, col
);
6629 attr
= new wxGridCellAttr
;
6631 // artificially inc the ref count to match DecRef() in caller
6634 m_table
->SetAttr(attr
, row
, col
);
6637 CacheAttr(row
, col
, attr
);
6639 attr
->SetDefAttr(m_defaultCellAttr
);
6643 // ----------------------------------------------------------------------------
6644 // setting cell attributes: this is forwarded to the table
6645 // ----------------------------------------------------------------------------
6647 void wxGrid
::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6649 if ( CanHaveAttributes() )
6651 m_table
->SetRowAttr(attr
, row
);
6659 void wxGrid
::SetColAttr(int col
, wxGridCellAttr
*attr
)
6661 if ( CanHaveAttributes() )
6663 m_table
->SetColAttr(attr
, col
);
6671 void wxGrid
::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6673 if ( CanHaveAttributes() )
6675 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6676 attr
->SetBackgroundColour(colour
);
6681 void wxGrid
::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6683 if ( CanHaveAttributes() )
6685 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6686 attr
->SetTextColour(colour
);
6691 void wxGrid
::SetCellFont( int row
, int col
, const wxFont
& font
)
6693 if ( CanHaveAttributes() )
6695 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6696 attr
->SetFont(font
);
6701 void wxGrid
::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6703 if ( CanHaveAttributes() )
6705 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6706 attr
->SetAlignment(horiz
, vert
);
6711 void wxGrid
::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6713 if ( CanHaveAttributes() )
6715 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6716 attr
->SetRenderer(renderer
);
6721 void wxGrid
::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6723 if ( CanHaveAttributes() )
6725 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6726 attr
->SetEditor(editor
);
6731 void wxGrid
::SetReadOnly(int row
, int col
, bool isReadOnly
)
6733 if ( CanHaveAttributes() )
6735 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6736 attr
->SetReadOnly(isReadOnly
);
6741 // ----------------------------------------------------------------------------
6742 // Data type registration
6743 // ----------------------------------------------------------------------------
6745 void wxGrid
::RegisterDataType(const wxString
& typeName
,
6746 wxGridCellRenderer
* renderer
,
6747 wxGridCellEditor
* editor
)
6749 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6753 wxGridCellEditor
* wxGrid
::GetDefaultEditorForCell(int row
, int col
) const
6755 wxString typeName
= m_table
->GetTypeName(row
, col
);
6756 return GetDefaultEditorForType(typeName
);
6759 wxGridCellRenderer
* wxGrid
::GetDefaultRendererForCell(int row
, int col
) const
6761 wxString typeName
= m_table
->GetTypeName(row
, col
);
6762 return GetDefaultRendererForType(typeName
);
6766 wxGrid
::GetDefaultEditorForType(const wxString
& typeName
) const
6768 int index
= m_typeRegistry
->FindDataType(typeName
);
6770 // Should we force the failure here or let it fallback to string handling???
6771 // wxFAIL_MSG(wxT("Unknown data type name"));
6774 return m_typeRegistry
->GetEditor(index
);
6778 wxGrid
::GetDefaultRendererForType(const wxString
& typeName
) const
6780 int index
= m_typeRegistry
->FindDataType(typeName
);
6782 // Should we force the failure here or let it fallback to string handling???
6783 // wxFAIL_MSG(wxT("Unknown data type name"));
6786 return m_typeRegistry
->GetRenderer(index
);
6790 // ----------------------------------------------------------------------------
6792 // ----------------------------------------------------------------------------
6794 void wxGrid
::EnableDragRowSize( bool enable
)
6796 m_canDragRowSize
= enable
;
6800 void wxGrid
::EnableDragColSize( bool enable
)
6802 m_canDragColSize
= enable
;
6805 void wxGrid
::EnableDragGridSize( bool enable
)
6807 m_canDragGridSize
= enable
;
6811 void wxGrid
::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6813 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6815 if ( resizeExistingRows
)
6823 void wxGrid
::SetRowSize( int row
, int height
)
6825 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6827 if ( m_rowHeights
.IsEmpty() )
6829 // need to really create the array
6833 int h
= wxMax( 0, height
);
6834 int diff
= h
- m_rowHeights
[row
];
6836 m_rowHeights
[row
] = h
;
6838 for ( i
= row
; i
< m_numRows
; i
++ )
6840 m_rowBottoms
[i
] += diff
;
6845 void wxGrid
::SetDefaultColSize( int width
, bool resizeExistingCols
)
6847 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6849 if ( resizeExistingCols
)
6857 void wxGrid
::SetColSize( int col
, int width
)
6859 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6861 // should we check that it's bigger than GetColMinimalWidth(col) here?
6863 if ( m_colWidths
.IsEmpty() )
6865 // need to really create the array
6869 int w
= wxMax( 0, width
);
6870 int diff
= w
- m_colWidths
[col
];
6871 m_colWidths
[col
] = w
;
6874 for ( i
= col
; i
< m_numCols
; i
++ )
6876 m_colRights
[i
] += diff
;
6882 void wxGrid
::SetColMinimalWidth( int col
, int width
)
6884 m_colMinWidths
.Put(col
, width
);
6887 void wxGrid
::SetRowMinimalHeight( int row
, int width
)
6889 m_rowMinHeights
.Put(row
, width
);
6892 int wxGrid
::GetColMinimalWidth(int col
) const
6894 long value
= m_colMinWidths
.Get(col
);
6895 return value
!= wxNOT_FOUND ?
(int)value
: WXGRID_MIN_COL_WIDTH
;
6898 int wxGrid
::GetRowMinimalHeight(int row
) const
6900 long value
= m_rowMinHeights
.Get(row
);
6901 return value
!= wxNOT_FOUND ?
(int)value
: WXGRID_MIN_ROW_HEIGHT
;
6904 // ----------------------------------------------------------------------------
6906 // ----------------------------------------------------------------------------
6908 void wxGrid
::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
6910 wxClientDC
dc(m_gridWin
);
6918 wxCoord extent
, extentMax
= 0;
6919 int max
= column ? m_numRows
: m_numCols
;
6920 for ( int rowOrCol
= 0; rowOrCol
< m_numRows
; rowOrCol
++ )
6927 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6928 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6931 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
6932 extent
= column ? size
.x
: size
.y
;
6933 if ( extent
> extentMax
)
6942 // now also compare with the column label extent
6944 dc
.SetFont( GetLabelFont() );
6945 dc
.GetTextExtent( column ?
GetColLabelValue(col
)
6946 : GetRowLabelValue(row
), &w
, &h
);
6947 extent
= column ? w
: h
;
6948 if ( extent
> extentMax
)
6955 // empty column - give default extent (notice that if extentMax is less
6956 // than default extent but != 0, it's ok)
6957 extentMax
= column ? m_defaultColWidth
: m_defaultRowHeight
;
6961 // leave some space around text
6966 SetColSize(col
, extentMax
);
6968 SetRowSize(col
, extentMax
);
6973 SetColMinimalWidth(col
, extentMax
);
6975 SetRowMinimalHeight(col
, extentMax
);
6979 int wxGrid
::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
6981 int width
= m_rowLabelWidth
;
6983 for ( int col
= 0; col
< m_numCols
; col
++ )
6987 AutoSizeColumn(col
, setAsMin
);
6990 width
+= GetColWidth(col
);
6996 int wxGrid
::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
6998 int height
= m_colLabelHeight
;
7000 for ( int row
= 0; row
< m_numRows
; row
++ )
7004 AutoSizeRow(row
, setAsMin
);
7007 height
+= GetRowHeight(row
);
7013 void wxGrid
::AutoSize()
7016 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
7019 wxSize wxGrid
::DoGetBestSize() const
7021 // don't set sizes, only calculate them
7022 wxGrid
*self
= (wxGrid
*)this; // const_cast
7024 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
7025 self
->SetOrCalcRowSizes(TRUE
));
7033 // ----------------------------------------------------------------------------
7034 // cell value accessor functions
7035 // ----------------------------------------------------------------------------
7037 void wxGrid
::SetCellValue( int row
, int col
, const wxString
& s
)
7041 m_table
->SetValue( row
, col
, s
.c_str() );
7042 if ( !GetBatchCount() )
7044 wxClientDC
dc( m_gridWin
);
7046 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7049 if ( m_currentCellCoords
.GetRow() == row
&&
7050 m_currentCellCoords
.GetCol() == col
&&
7051 IsCellEditControlEnabled())
7053 HideCellEditControl();
7054 ShowCellEditControl(); // will reread data from table
7061 // ------ Block, row and col selection
7064 void wxGrid
::SelectRow( int row
, bool addToSelected
)
7068 if ( IsSelection() && addToSelected
)
7071 bool need_refresh
[4];
7075 need_refresh
[3] = FALSE
;
7079 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7080 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7081 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7082 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7086 need_refresh
[0] = TRUE
;
7087 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
7088 wxGridCellCoords ( oldTop
- 1,
7090 m_selectedTopLeft
.SetRow( row
);
7095 need_refresh
[1] = TRUE
;
7096 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
7097 wxGridCellCoords ( oldBottom
,
7100 m_selectedTopLeft
.SetCol( 0 );
7103 if ( oldBottom
< row
)
7105 need_refresh
[2] = TRUE
;
7106 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
7107 wxGridCellCoords ( row
,
7109 m_selectedBottomRight
.SetRow( row
);
7112 if ( oldRight
< m_numCols
- 1 )
7114 need_refresh
[3] = TRUE
;
7115 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7117 wxGridCellCoords ( oldBottom
,
7119 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
7122 for (i
= 0; i
< 4; i
++ )
7123 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7124 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7128 r
= SelectionToDeviceRect();
7130 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7132 m_selectedTopLeft
.Set( row
, 0 );
7133 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7134 r
= SelectionToDeviceRect();
7135 m_gridWin
->Refresh( FALSE
, &r
);
7138 wxGridRangeSelectEvent
gridEvt( GetId(),
7139 wxEVT_GRID_RANGE_SELECT
,
7142 m_selectedBottomRight
);
7144 GetEventHandler()->ProcessEvent(gridEvt
);
7148 void wxGrid
::SelectCol( int col
, bool addToSelected
)
7150 if ( IsSelection() && addToSelected
)
7153 bool need_refresh
[4];
7157 need_refresh
[3] = FALSE
;
7160 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7161 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7162 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7163 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7165 if ( oldLeft
> col
)
7167 need_refresh
[0] = TRUE
;
7168 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7169 wxGridCellCoords ( m_numRows
- 1,
7171 m_selectedTopLeft
.SetCol( col
);
7176 need_refresh
[1] = TRUE
;
7177 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7178 wxGridCellCoords ( oldTop
- 1,
7180 m_selectedTopLeft
.SetRow( 0 );
7183 if ( oldRight
< col
)
7185 need_refresh
[2] = TRUE
;
7186 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7187 wxGridCellCoords ( m_numRows
- 1,
7189 m_selectedBottomRight
.SetCol( col
);
7192 if ( oldBottom
< m_numRows
- 1 )
7194 need_refresh
[3] = TRUE
;
7195 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7197 wxGridCellCoords ( m_numRows
- 1,
7199 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7202 for (i
= 0; i
< 4; i
++ )
7203 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7204 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7210 r
= SelectionToDeviceRect();
7212 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7214 m_selectedTopLeft
.Set( 0, col
);
7215 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7216 r
= SelectionToDeviceRect();
7217 m_gridWin
->Refresh( FALSE
, &r
);
7220 wxGridRangeSelectEvent
gridEvt( GetId(),
7221 wxEVT_GRID_RANGE_SELECT
,
7224 m_selectedBottomRight
);
7226 GetEventHandler()->ProcessEvent(gridEvt
);
7230 void wxGrid
::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7233 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7235 if ( topRow
> bottomRow
)
7242 if ( leftCol
> rightCol
)
7249 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7250 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7252 if ( m_selectedTopLeft
!= updateTopLeft
||
7253 m_selectedBottomRight
!= updateBottomRight
)
7255 // Compute two optimal update rectangles:
7256 // Either one rectangle is a real subset of the
7257 // other, or they are (almost) disjoint!
7259 bool need_refresh
[4];
7263 need_refresh
[3] = FALSE
;
7266 // Store intermediate values
7267 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7268 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7269 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7270 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7272 // Determine the outer/inner coordinates.
7273 if (oldLeft
> leftCol
)
7279 if (oldTop
> topRow
)
7285 if (oldRight
< rightCol
)
7288 oldRight
= rightCol
;
7291 if (oldBottom
< bottomRow
)
7294 oldBottom
= bottomRow
;
7298 // Now, either the stuff marked old is the outer
7299 // rectangle or we don't have a situation where one
7300 // is contained in the other.
7302 if ( oldLeft
< leftCol
)
7304 need_refresh
[0] = TRUE
;
7305 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7307 wxGridCellCoords ( oldBottom
,
7311 if ( oldTop
< topRow
)
7313 need_refresh
[1] = TRUE
;
7314 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7316 wxGridCellCoords ( topRow
- 1,
7320 if ( oldRight
> rightCol
)
7322 need_refresh
[2] = TRUE
;
7323 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7325 wxGridCellCoords ( oldBottom
,
7329 if ( oldBottom
> bottomRow
)
7331 need_refresh
[3] = TRUE
;
7332 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7334 wxGridCellCoords ( oldBottom
,
7340 m_selectedTopLeft
= updateTopLeft
;
7341 m_selectedBottomRight
= updateBottomRight
;
7343 // various Refresh() calls
7344 for (i
= 0; i
< 4; i
++ )
7345 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7346 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7349 // only generate an event if the block is not being selected by
7350 // dragging the mouse (in which case the event will be generated in
7351 // the mouse event handler)
7352 if ( !m_isDragging
)
7354 wxGridRangeSelectEvent
gridEvt( GetId(),
7355 wxEVT_GRID_RANGE_SELECT
,
7358 m_selectedBottomRight
);
7360 GetEventHandler()->ProcessEvent(gridEvt
);
7364 void wxGrid
::SelectAll()
7366 m_selectedTopLeft
.Set( 0, 0 );
7367 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7369 m_gridWin
->Refresh();
7373 void wxGrid
::ClearSelection()
7375 m_selectedTopLeft
= wxGridNoCellCoords
;
7376 m_selectedBottomRight
= wxGridNoCellCoords
;
7380 // This function returns the rectangle that encloses the given block
7381 // in device coords clipped to the client size of the grid window.
7383 wxRect wxGrid
::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7384 const wxGridCellCoords
&bottomRight
)
7386 wxRect
rect( wxGridNoCellRect
);
7389 cellRect
= CellToRect( topLeft
);
7390 if ( cellRect
!= wxGridNoCellRect
)
7396 rect
= wxRect( 0, 0, 0, 0 );
7399 cellRect
= CellToRect( bottomRight
);
7400 if ( cellRect
!= wxGridNoCellRect
)
7406 return wxGridNoCellRect
;
7409 // convert to scrolled coords
7411 int left
, top
, right
, bottom
;
7412 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7413 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7416 m_gridWin
->GetClientSize( &cw
, &ch
);
7418 rect
.SetLeft( wxMax(0, left
) );
7419 rect
.SetTop( wxMax(0, top
) );
7420 rect
.SetRight( wxMin(cw
, right
) );
7421 rect
.SetBottom( wxMin(ch
, bottom
) );
7429 // ------ Grid event classes
7432 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7434 wxGridEvent
::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7435 int row
, int col
, int x
, int y
,
7436 bool control
, bool shift
, bool alt
, bool meta
)
7437 : wxNotifyEvent( type
, id
)
7443 m_control
= control
;
7448 SetEventObject(obj
);
7452 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7454 wxGridSizeEvent
::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7455 int rowOrCol
, int x
, int y
,
7456 bool control
, bool shift
, bool alt
, bool meta
)
7457 : wxNotifyEvent( type
, id
)
7459 m_rowOrCol
= rowOrCol
;
7462 m_control
= control
;
7467 SetEventObject(obj
);
7471 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7473 wxGridRangeSelectEvent
::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7474 const wxGridCellCoords
& topLeft
,
7475 const wxGridCellCoords
& bottomRight
,
7476 bool control
, bool shift
, bool alt
, bool meta
)
7477 : wxNotifyEvent( type
, id
)
7479 m_topLeft
= topLeft
;
7480 m_bottomRight
= bottomRight
;
7481 m_control
= control
;
7486 SetEventObject(obj
);
7490 #endif // ifndef wxUSE_NEW_GRID