1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
59 struct wxGridCellWithAttr
61 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
62 : coords(row
, col
), attr(attr_
)
71 wxGridCellCoords coords
;
75 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
77 #include "wx/arrimpl.cpp"
79 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
80 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
89 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
90 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
91 const wxPoint
&pos
, const wxSize
&size
);
96 void OnPaint( wxPaintEvent
& event
);
97 void OnMouseEvent( wxMouseEvent
& event
);
98 void OnKeyDown( wxKeyEvent
& event
);
100 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
108 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
109 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
110 const wxPoint
&pos
, const wxSize
&size
);
115 void OnPaint( wxPaintEvent
&event
);
116 void OnMouseEvent( wxMouseEvent
& event
);
117 void OnKeyDown( wxKeyEvent
& event
);
119 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
120 DECLARE_EVENT_TABLE()
124 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
127 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
128 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnKeyDown( wxKeyEvent
& event
);
136 void OnPaint( wxPaintEvent
& event
);
138 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
139 DECLARE_EVENT_TABLE()
142 class WXDLLEXPORT wxGridWindow
: public wxPanel
147 m_owner
= (wxGrid
*)NULL
;
148 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
149 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
152 wxGridWindow( wxGrid
*parent
,
153 wxGridRowLabelWindow
*rowLblWin
,
154 wxGridColLabelWindow
*colLblWin
,
155 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
158 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
162 wxGridRowLabelWindow
*m_rowLabelWin
;
163 wxGridColLabelWindow
*m_colLabelWin
;
165 void OnPaint( wxPaintEvent
&event
);
166 void OnMouseEvent( wxMouseEvent
& event
);
167 void OnKeyDown( wxKeyEvent
& );
168 void OnEraseBackground( wxEraseEvent
& );
171 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
172 DECLARE_EVENT_TABLE()
177 class wxGridCellEditorEvtHandler
: public wxEvtHandler
180 wxGridCellEditorEvtHandler()
181 : m_grid(0), m_editor(0)
183 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
184 : m_grid(grid
), m_editor(editor
)
187 void OnKeyDown(wxKeyEvent
& event
);
188 void OnChar(wxKeyEvent
& event
);
192 wxGridCellEditor
* m_editor
;
193 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
194 DECLARE_EVENT_TABLE()
198 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
199 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
200 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
201 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
206 // ----------------------------------------------------------------------------
207 // the internal data representation used by wxGridCellAttrProvider
208 // ----------------------------------------------------------------------------
210 // this class stores attributes set for cells
211 class WXDLLEXPORT wxGridCellAttrData
214 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
215 wxGridCellAttr
*GetAttr(int row
, int col
) const;
216 void UpdateAttrRows( size_t pos
, int numRows
);
217 void UpdateAttrCols( size_t pos
, int numCols
);
220 // searches for the attr for given cell, returns wxNOT_FOUND if not found
221 int FindIndex(int row
, int col
) const;
223 wxGridCellWithAttrArray m_attrs
;
226 // this class stores attributes set for rows or columns
227 class WXDLLEXPORT wxGridRowOrColAttrData
230 // empty ctor to suppress warnings
231 wxGridRowOrColAttrData() { }
232 ~wxGridRowOrColAttrData();
234 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
235 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
236 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
239 wxArrayInt m_rowsOrCols
;
240 wxArrayAttrs m_attrs
;
243 // NB: this is just a wrapper around 3 objects: one which stores cell
244 // attributes, and 2 others for row/col ones
245 class WXDLLEXPORT wxGridCellAttrProviderData
248 wxGridCellAttrData m_cellAttrs
;
249 wxGridRowOrColAttrData m_rowAttrs
,
254 // ----------------------------------------------------------------------------
255 // data structures used for the data type registry
256 // ----------------------------------------------------------------------------
258 struct wxGridDataTypeInfo
260 wxGridDataTypeInfo(const wxString
& typeName
,
261 wxGridCellRenderer
* renderer
,
262 wxGridCellEditor
* editor
)
263 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
266 ~wxGridDataTypeInfo()
268 wxSafeDecRef(m_renderer
);
269 wxSafeDecRef(m_editor
);
273 wxGridCellRenderer
* m_renderer
;
274 wxGridCellEditor
* m_editor
;
278 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
281 class WXDLLEXPORT wxGridTypeRegistry
284 ~wxGridTypeRegistry();
286 void RegisterDataType(const wxString
& typeName
,
287 wxGridCellRenderer
* renderer
,
288 wxGridCellEditor
* editor
);
289 int FindDataType(const wxString
& typeName
);
290 wxGridCellRenderer
* GetRenderer(int index
);
291 wxGridCellEditor
* GetEditor(int index
);
294 wxGridDataTypeInfoArray m_typeinfo
;
297 // ----------------------------------------------------------------------------
298 // conditional compilation
299 // ----------------------------------------------------------------------------
301 #ifndef WXGRID_DRAW_LINES
302 #define WXGRID_DRAW_LINES 1
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 //#define DEBUG_ATTR_CACHE
310 #ifdef DEBUG_ATTR_CACHE
311 static size_t gs_nAttrCacheHits
= 0;
312 static size_t gs_nAttrCacheMisses
= 0;
313 #endif // DEBUG_ATTR_CACHE
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
320 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
323 // TODO: fixed so far - make configurable later (and also different for x/y)
324 static const size_t GRID_SCROLL_LINE
= 10;
326 // the size of hash tables used a bit everywhere (the max number of elements
327 // in these hash tables is the number of rows/columns)
328 static const int GRID_HASH_SIZE
= 100;
330 // ============================================================================
332 // ============================================================================
334 // ----------------------------------------------------------------------------
336 // ----------------------------------------------------------------------------
338 wxGridCellEditor::wxGridCellEditor()
346 wxGridCellEditor::~wxGridCellEditor()
351 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
352 wxWindowID
WXUNUSED(id
),
353 wxEvtHandler
* evtHandler
)
356 m_control
->PushEventHandler(evtHandler
);
359 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
360 wxGridCellAttr
*attr
)
362 // erase the background because we might not fill the cell
363 wxClientDC
dc(m_control
->GetParent());
364 dc
.SetPen(*wxTRANSPARENT_PEN
);
365 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
366 dc
.DrawRectangle(rectCell
);
368 // redraw the control we just painted over
369 m_control
->Refresh();
372 void wxGridCellEditor::Destroy()
376 m_control
->PopEventHandler(TRUE
/* delete it*/);
378 m_control
->Destroy();
383 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
385 wxASSERT_MSG(m_control
,
386 wxT("The wxGridCellEditor must be Created first!"));
387 m_control
->Show(show
);
391 // set the colours/fonts if we have any
394 m_colFgOld
= m_control
->GetForegroundColour();
395 m_control
->SetForegroundColour(attr
->GetTextColour());
397 m_colBgOld
= m_control
->GetBackgroundColour();
398 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
400 m_fontOld
= m_control
->GetFont();
401 m_control
->SetFont(attr
->GetFont());
403 // can't do anything more in the base class version, the other
404 // attributes may only be used by the derived classes
409 // restore the standard colours fonts
410 if ( m_colFgOld
.Ok() )
412 m_control
->SetForegroundColour(m_colFgOld
);
413 m_colFgOld
= wxNullColour
;
416 if ( m_colBgOld
.Ok() )
418 m_control
->SetBackgroundColour(m_colBgOld
);
419 m_colBgOld
= wxNullColour
;
422 if ( m_fontOld
.Ok() )
424 m_control
->SetFont(m_fontOld
);
425 m_fontOld
= wxNullFont
;
430 void wxGridCellEditor::SetSize(const wxRect
& rect
)
432 wxASSERT_MSG(m_control
,
433 wxT("The wxGridCellEditor must be Created first!"));
434 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
437 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
443 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
448 void wxGridCellEditor::StartingClick()
452 // ----------------------------------------------------------------------------
453 // wxGridCellTextEditor
454 // ----------------------------------------------------------------------------
456 wxGridCellTextEditor::wxGridCellTextEditor()
460 void wxGridCellTextEditor::Create(wxWindow
* parent
,
462 wxEvtHandler
* evtHandler
)
464 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
465 wxDefaultPosition
, wxDefaultSize
466 #if defined(__WXMSW__)
467 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
471 wxGridCellEditor::Create(parent
, id
, evtHandler
);
474 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
475 wxGridCellAttr
* WXUNUSED(attr
))
477 // as we fill the entire client area, don't do anything here to minimize
481 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
483 wxRect
rect(rectOrig
);
485 // Make the edit control large enough to allow for internal
488 // TODO: remove this if the text ctrl sizing is improved esp. for
491 #if defined(__WXGTK__)
500 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
501 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
502 #if defined(__WXMOTIF__)
506 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
507 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
508 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
509 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
512 wxGridCellEditor::SetSize(rect
);
515 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
517 wxASSERT_MSG(m_control
,
518 wxT("The wxGridCellEditor must be Created first!"));
520 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
522 DoBeginEdit(m_startValue
);
525 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
527 Text()->SetValue(startValue
);
528 Text()->SetInsertionPointEnd();
532 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
535 wxASSERT_MSG(m_control
,
536 wxT("The wxGridCellEditor must be Created first!"));
538 bool changed
= FALSE
;
539 wxString value
= Text()->GetValue();
540 if (value
!= m_startValue
)
544 grid
->GetTable()->SetValue(row
, col
, value
);
546 m_startValue
= wxEmptyString
;
547 Text()->SetValue(m_startValue
);
553 void wxGridCellTextEditor::Reset()
555 wxASSERT_MSG(m_control
,
556 wxT("The wxGridCellEditor must be Created first!"));
558 DoReset(m_startValue
);
561 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
563 Text()->SetValue(startValue
);
564 Text()->SetInsertionPointEnd();
567 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
569 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
571 // insert the key in the control
572 long keycode
= event
.KeyCode();
573 if ( isprint(keycode
) )
575 // FIXME this is not going to work for non letters...
576 if ( !event
.ShiftDown() )
578 keycode
= tolower(keycode
);
581 Text()->AppendText((wxChar
)keycode
);
591 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
593 #if defined(__WXMOTIF__) || defined(__WXGTK__)
594 // wxMotif needs a little extra help...
595 int pos
= Text()->GetInsertionPoint();
596 wxString
s( Text()->GetValue() );
597 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
599 Text()->SetInsertionPoint( pos
);
601 // the other ports can handle a Return key press
607 // ----------------------------------------------------------------------------
608 // wxGridCellNumberEditor
609 // ----------------------------------------------------------------------------
611 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
617 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
619 wxEvtHandler
* evtHandler
)
623 // create a spin ctrl
624 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
625 wxDefaultPosition
, wxDefaultSize
,
629 wxGridCellEditor::Create(parent
, id
, evtHandler
);
633 // just a text control
634 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
637 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
638 #endif // wxUSE_VALIDATORS
642 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
644 // first get the value
645 wxGridTableBase
*table
= grid
->GetTable();
646 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
648 m_valueOld
= table
->GetValueAsLong(row
, col
);
652 wxString sValue
= table
->GetValue(row
, col
);
653 if (! sValue
.ToLong(&m_valueOld
))
655 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
662 Spin()->SetValue(m_valueOld
);
666 DoBeginEdit(GetString());
670 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
678 value
= Spin()->GetValue();
679 changed
= value
!= m_valueOld
;
683 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
688 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
689 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
691 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%ld", value
));
697 void wxGridCellNumberEditor::Reset()
701 Spin()->SetValue(m_valueOld
);
705 DoReset(GetString());
709 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
713 long keycode
= event
.KeyCode();
714 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
716 wxGridCellTextEditor::StartingKey(event
);
726 // ----------------------------------------------------------------------------
727 // wxGridCellFloatEditor
728 // ----------------------------------------------------------------------------
730 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
732 wxEvtHandler
* evtHandler
)
734 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
737 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
738 #endif // wxUSE_VALIDATORS
741 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
743 // first get the value
744 wxGridTableBase
*table
= grid
->GetTable();
745 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
747 m_valueOld
= table
->GetValueAsDouble(row
, col
);
751 wxString sValue
= table
->GetValue(row
, col
);
752 if (! sValue
.ToDouble(&m_valueOld
))
754 wxFAIL_MSG( _T("this cell doesn't have float value") );
759 DoBeginEdit(GetString());
762 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
766 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
768 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
769 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
771 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%f", value
));
781 void wxGridCellFloatEditor::Reset()
783 DoReset(GetString());
786 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
788 long keycode
= event
.KeyCode();
789 if ( isdigit(keycode
) ||
790 keycode
== '+' || keycode
== '-' || keycode
== '.' )
792 wxGridCellTextEditor::StartingKey(event
);
801 // ----------------------------------------------------------------------------
802 // wxGridCellBoolEditor
803 // ----------------------------------------------------------------------------
805 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
807 wxEvtHandler
* evtHandler
)
809 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
810 wxDefaultPosition
, wxDefaultSize
,
813 wxGridCellEditor::Create(parent
, id
, evtHandler
);
816 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
819 wxSize size
= m_control
->GetSize();
820 wxCoord minSize
= wxMin(r
.width
, r
.height
);
822 // check if the checkbox is not too big/small for this cell
823 wxSize sizeBest
= m_control
->GetBestSize();
824 if ( !(size
== sizeBest
) )
826 // reset to default size if it had been made smaller
832 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
834 // leave 1 pixel margin
835 size
.x
= size
.y
= minSize
- 2;
842 m_control
->SetSize(size
);
845 // position it in the centre of the rectangle (TODO: support alignment?)
847 #if defined(__WXGTK__) || defined (__WXMOTIF__)
848 // the checkbox without label still has some space to the right in wxGTK,
849 // so shift it to the right
851 #elif defined(__WXMSW__)
857 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
860 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
862 m_control
->Show(show
);
866 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
867 CBox()->SetBackgroundColour(colBg
);
871 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
873 wxASSERT_MSG(m_control
,
874 wxT("The wxGridCellEditor must be Created first!"));
876 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
877 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
879 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
880 CBox()->SetValue(m_startValue
);
884 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
887 wxASSERT_MSG(m_control
,
888 wxT("The wxGridCellEditor must be Created first!"));
890 bool changed
= FALSE
;
891 bool value
= CBox()->GetValue();
892 if ( value
!= m_startValue
)
897 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
898 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
900 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
906 void wxGridCellBoolEditor::Reset()
908 wxASSERT_MSG(m_control
,
909 wxT("The wxGridCellEditor must be Created first!"));
911 CBox()->SetValue(m_startValue
);
914 void wxGridCellBoolEditor::StartingClick()
916 CBox()->SetValue(!CBox()->GetValue());
919 // ----------------------------------------------------------------------------
920 // wxGridCellChoiceEditor
921 // ----------------------------------------------------------------------------
923 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
924 const wxChar
* choices
[],
926 : m_allowOthers(allowOthers
)
928 m_choices
.Alloc(count
);
929 for ( size_t n
= 0; n
< count
; n
++ )
931 m_choices
.Add(choices
[n
]);
935 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
937 wxEvtHandler
* evtHandler
)
939 size_t count
= m_choices
.GetCount();
940 wxString
*choices
= new wxString
[count
];
941 for ( size_t n
= 0; n
< count
; n
++ )
943 choices
[n
] = m_choices
[n
];
946 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
947 wxDefaultPosition
, wxDefaultSize
,
949 m_allowOthers
? 0 : wxCB_READONLY
);
953 wxGridCellEditor::Create(parent
, id
, evtHandler
);
956 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
957 wxGridCellAttr
* attr
)
959 // as we fill the entire client area, don't do anything here to minimize
962 // TODO: It doesn't actually fill the client area since the height of a
963 // combo always defaults to the standard... Until someone has time to
964 // figure out the right rectangle to paint, just do it the normal way...
965 wxGridCellEditor::PaintBackground(rectCell
, attr
);
968 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
970 wxASSERT_MSG(m_control
,
971 wxT("The wxGridCellEditor must be Created first!"));
973 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
975 Combo()->SetValue(m_startValue
);
976 size_t count
= m_choices
.GetCount();
977 for (size_t i
=0; i
<count
; i
++)
979 if (m_startValue
== m_choices
[i
])
981 Combo()->SetSelection(i
);
985 Combo()->SetInsertionPointEnd();
989 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
992 wxString value
= Combo()->GetValue();
993 bool changed
= value
!= m_startValue
;
996 grid
->GetTable()->SetValue(row
, col
, value
);
998 m_startValue
= wxEmptyString
;
999 Combo()->SetValue(m_startValue
);
1004 void wxGridCellChoiceEditor::Reset()
1006 Combo()->SetValue(m_startValue
);
1007 Combo()->SetInsertionPointEnd();
1010 // ----------------------------------------------------------------------------
1011 // wxGridCellEditorEvtHandler
1012 // ----------------------------------------------------------------------------
1014 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1016 switch ( event
.KeyCode() )
1020 m_grid
->DisableCellEditControl();
1024 event
.Skip( m_grid
->ProcessEvent( event
) );
1028 if (!m_grid
->ProcessEvent(event
))
1029 m_editor
->HandleReturn(event
);
1038 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1040 switch ( event
.KeyCode() )
1052 // ============================================================================
1054 // ============================================================================
1056 // ----------------------------------------------------------------------------
1057 // wxGridCellRenderer
1058 // ----------------------------------------------------------------------------
1060 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1061 wxGridCellAttr
& attr
,
1067 dc
.SetBackgroundMode( wxSOLID
);
1071 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1075 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1078 dc
.SetPen( *wxTRANSPARENT_PEN
);
1079 dc
.DrawRectangle(rect
);
1082 wxGridCellRenderer::~wxGridCellRenderer()
1086 // ----------------------------------------------------------------------------
1087 // wxGridCellStringRenderer
1088 // ----------------------------------------------------------------------------
1090 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1091 wxGridCellAttr
& attr
,
1095 dc
.SetBackgroundMode( wxTRANSPARENT
);
1097 // TODO some special colours for attr.IsReadOnly() case?
1101 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1102 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1106 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1107 dc
.SetTextForeground( attr
.GetTextColour() );
1110 dc
.SetFont( attr
.GetFont() );
1113 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1115 const wxString
& text
)
1118 dc
.SetFont(attr
.GetFont());
1119 dc
.GetTextExtent(text
, &x
, &y
);
1121 return wxSize(x
, y
);
1124 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1125 wxGridCellAttr
& attr
,
1129 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1132 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1133 wxGridCellAttr
& attr
,
1135 const wxRect
& rectCell
,
1139 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1141 // now we only have to draw the text
1142 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1145 attr
.GetAlignment(&hAlign
, &vAlign
);
1147 wxRect rect
= rectCell
;
1150 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1151 rect
, hAlign
, vAlign
);
1154 // ----------------------------------------------------------------------------
1155 // wxGridCellNumberRenderer
1156 // ----------------------------------------------------------------------------
1158 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1160 wxGridTableBase
*table
= grid
.GetTable();
1162 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1164 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1168 text
= table
->GetValue(row
, col
);
1174 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1175 wxGridCellAttr
& attr
,
1177 const wxRect
& rectCell
,
1181 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1183 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1185 // draw the text right aligned by default
1187 attr
.GetAlignment(&hAlign
, &vAlign
);
1190 wxRect rect
= rectCell
;
1193 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1196 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1197 wxGridCellAttr
& attr
,
1201 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1204 // ----------------------------------------------------------------------------
1205 // wxGridCellFloatRenderer
1206 // ----------------------------------------------------------------------------
1208 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1211 SetPrecision(precision
);
1214 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1216 wxGridTableBase
*table
= grid
.GetTable();
1218 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1222 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1225 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1229 text
= table
->GetValue(row
, col
);
1235 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1236 wxGridCellAttr
& attr
,
1238 const wxRect
& rectCell
,
1242 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1244 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1246 // draw the text right aligned by default
1248 attr
.GetAlignment(&hAlign
, &vAlign
);
1251 wxRect rect
= rectCell
;
1254 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1257 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1258 wxGridCellAttr
& attr
,
1262 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1265 // ----------------------------------------------------------------------------
1266 // wxGridCellBoolRenderer
1267 // ----------------------------------------------------------------------------
1269 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1271 // FIXME these checkbox size calculations are really ugly...
1273 // between checkmark and box
1275 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1277 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1280 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1281 wxGridCellAttr
& WXUNUSED(attr
),
1286 // compute it only once (no locks for MT safeness in GUI thread...)
1287 if ( !ms_sizeCheckMark
.x
)
1289 // get checkbox size
1290 wxCoord checkSize
= 0;
1291 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1292 wxSize size
= checkbox
->GetBestSize();
1293 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1295 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1296 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1297 checkSize
-= size
.y
/ 2;
1302 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1305 return ms_sizeCheckMark
;
1308 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1309 wxGridCellAttr
& attr
,
1315 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1317 // draw a check mark in the centre (ignoring alignment - TODO)
1318 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1320 // don't draw outside the cell
1321 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1322 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1324 // and even leave (at least) 1 pixel margin
1325 size
.x
= size
.y
= minSize
- 2;
1328 // draw a border around checkmark
1330 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1331 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1332 rectMark
.width
= size
.x
;
1333 rectMark
.height
= size
.y
;
1335 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1336 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1337 dc
.DrawRectangle(rectMark
);
1339 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1342 // looks nicer under MSW
1347 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1348 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1350 value
= !!grid
.GetTable()->GetValue(row
, col
);
1354 dc
.SetTextForeground(attr
.GetTextColour());
1355 dc
.DrawCheckMark(rectMark
);
1359 // ----------------------------------------------------------------------------
1361 // ----------------------------------------------------------------------------
1363 wxGridCellAttr
*wxGridCellAttr::Clone() const
1365 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1366 if ( HasTextColour() )
1367 attr
->SetTextColour(GetTextColour());
1368 if ( HasBackgroundColour() )
1369 attr
->SetBackgroundColour(GetBackgroundColour());
1371 attr
->SetFont(GetFont());
1372 if ( HasAlignment() )
1373 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1377 attr
->SetRenderer(m_renderer
);
1378 m_renderer
->IncRef();
1382 attr
->SetEditor(m_editor
);
1387 attr
->SetReadOnly();
1389 attr
->SetDefAttr(m_defGridAttr
);
1394 const wxColour
& wxGridCellAttr::GetTextColour() const
1396 if (HasTextColour())
1400 else if (m_defGridAttr
!= this)
1402 return m_defGridAttr
->GetTextColour();
1406 wxFAIL_MSG(wxT("Missing default cell attribute"));
1407 return wxNullColour
;
1412 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1414 if (HasBackgroundColour())
1416 else if (m_defGridAttr
!= this)
1417 return m_defGridAttr
->GetBackgroundColour();
1420 wxFAIL_MSG(wxT("Missing default cell attribute"));
1421 return wxNullColour
;
1426 const wxFont
& wxGridCellAttr::GetFont() const
1430 else if (m_defGridAttr
!= this)
1431 return m_defGridAttr
->GetFont();
1434 wxFAIL_MSG(wxT("Missing default cell attribute"));
1440 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1444 if ( hAlign
) *hAlign
= m_hAlign
;
1445 if ( vAlign
) *vAlign
= m_vAlign
;
1447 else if (m_defGridAttr
!= this)
1448 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1451 wxFAIL_MSG(wxT("Missing default cell attribute"));
1456 // GetRenderer and GetEditor use a slightly different decision path about
1457 // which attribute to use. If a non-default attr object has one then it is
1458 // used, otherwise the default editor or renderer is fetched from the grid and
1459 // used. It should be the default for the data type of the cell. If it is
1460 // NULL (because the table has a type that the grid does not have in its
1461 // registry,) then the grid's default editor or renderer is used.
1463 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1465 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1466 return m_renderer
; // use local attribute
1468 wxGridCellRenderer
* renderer
= NULL
;
1469 if (grid
) // get renderer for the data type
1470 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1473 // if we still don't have one then use the grid default
1474 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1477 wxFAIL_MSG(wxT("Missing default cell attribute"));
1482 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1484 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1485 return m_editor
; // use local attribute
1487 wxGridCellEditor
* editor
= NULL
;
1488 if (grid
) // get renderer for the data type
1489 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1492 // if we still don't have one then use the grid default
1493 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1496 wxFAIL_MSG(wxT("Missing default cell attribute"));
1501 // ----------------------------------------------------------------------------
1502 // wxGridCellAttrData
1503 // ----------------------------------------------------------------------------
1505 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1507 int n
= FindIndex(row
, col
);
1508 if ( n
== wxNOT_FOUND
)
1510 // add the attribute
1511 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1517 // change the attribute
1518 m_attrs
[(size_t)n
].attr
= attr
;
1522 // remove this attribute
1523 m_attrs
.RemoveAt((size_t)n
);
1528 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1530 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1532 int n
= FindIndex(row
, col
);
1533 if ( n
!= wxNOT_FOUND
)
1535 attr
= m_attrs
[(size_t)n
].attr
;
1542 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1544 size_t count
= m_attrs
.GetCount();
1545 for ( size_t n
= 0; n
< count
; n
++ )
1547 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1548 wxCoord row
= coords
.GetRow();
1549 if ((size_t)row
>= pos
)
1553 // If rows inserted, include row counter where necessary
1554 coords
.SetRow(row
+ numRows
);
1556 else if (numRows
< 0)
1558 // If rows deleted ...
1559 if ((size_t)row
>= pos
- numRows
)
1561 // ...either decrement row counter (if row still exists)...
1562 coords
.SetRow(row
+ numRows
);
1566 // ...or remove the attribute
1567 m_attrs
.RemoveAt((size_t)n
);
1575 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1577 size_t count
= m_attrs
.GetCount();
1578 for ( size_t n
= 0; n
< count
; n
++ )
1580 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1581 wxCoord col
= coords
.GetCol();
1582 if ( (size_t)col
>= pos
)
1586 // If rows inserted, include row counter where necessary
1587 coords
.SetCol(col
+ numCols
);
1589 else if (numCols
< 0)
1591 // If rows deleted ...
1592 if ((size_t)col
>= pos
- numCols
)
1594 // ...either decrement row counter (if row still exists)...
1595 coords
.SetCol(col
+ numCols
);
1599 // ...or remove the attribute
1600 m_attrs
.RemoveAt((size_t)n
);
1608 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1610 size_t count
= m_attrs
.GetCount();
1611 for ( size_t n
= 0; n
< count
; n
++ )
1613 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1614 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1623 // ----------------------------------------------------------------------------
1624 // wxGridRowOrColAttrData
1625 // ----------------------------------------------------------------------------
1627 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1629 size_t count
= m_attrs
.Count();
1630 for ( size_t n
= 0; n
< count
; n
++ )
1632 m_attrs
[n
]->DecRef();
1636 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1638 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1640 int n
= m_rowsOrCols
.Index(rowOrCol
);
1641 if ( n
!= wxNOT_FOUND
)
1643 attr
= m_attrs
[(size_t)n
];
1650 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1652 int n
= m_rowsOrCols
.Index(rowOrCol
);
1653 if ( n
== wxNOT_FOUND
)
1655 // add the attribute
1656 m_rowsOrCols
.Add(rowOrCol
);
1663 // change the attribute
1664 m_attrs
[(size_t)n
] = attr
;
1668 // remove this attribute
1669 m_attrs
[(size_t)n
]->DecRef();
1670 m_rowsOrCols
.RemoveAt((size_t)n
);
1671 m_attrs
.RemoveAt((size_t)n
);
1676 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1678 size_t count
= m_attrs
.GetCount();
1679 for ( size_t n
= 0; n
< count
; n
++ )
1681 int & rowOrCol
= m_rowsOrCols
[n
];
1682 if ( (size_t)rowOrCol
>= pos
)
1684 if ( numRowsOrCols
> 0 )
1686 // If rows inserted, include row counter where necessary
1687 rowOrCol
+= numRowsOrCols
;
1689 else if ( numRowsOrCols
< 0)
1691 // If rows deleted, either decrement row counter (if row still exists)
1692 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1693 rowOrCol
+= numRowsOrCols
;
1696 m_rowsOrCols
.RemoveAt((size_t)n
);
1697 m_attrs
.RemoveAt((size_t)n
);
1705 // ----------------------------------------------------------------------------
1706 // wxGridCellAttrProvider
1707 // ----------------------------------------------------------------------------
1709 wxGridCellAttrProvider::wxGridCellAttrProvider()
1711 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1714 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1719 void wxGridCellAttrProvider::InitData()
1721 m_data
= new wxGridCellAttrProviderData
;
1724 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1726 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1729 // first look for the attribute of this specific cell
1730 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1734 // then look for the col attr (col attributes are more common than
1735 // the row ones, hence they have priority)
1736 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1741 // finally try the row attributes
1742 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1749 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1755 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1758 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1763 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1766 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1771 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1774 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1778 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1780 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1784 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1788 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1790 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1794 // ----------------------------------------------------------------------------
1795 // wxGridTypeRegistry
1796 // ----------------------------------------------------------------------------
1798 wxGridTypeRegistry::~wxGridTypeRegistry()
1800 size_t count
= m_typeinfo
.Count();
1801 for ( size_t i
= 0; i
< count
; i
++ )
1802 delete m_typeinfo
[i
];
1806 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1807 wxGridCellRenderer
* renderer
,
1808 wxGridCellEditor
* editor
)
1810 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1812 // is it already registered?
1813 int loc
= FindDataType(typeName
);
1814 if ( loc
!= wxNOT_FOUND
)
1816 delete m_typeinfo
[loc
];
1817 m_typeinfo
[loc
] = info
;
1821 m_typeinfo
.Add(info
);
1825 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1829 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1830 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1839 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1841 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1845 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1847 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1851 // ----------------------------------------------------------------------------
1853 // ----------------------------------------------------------------------------
1855 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1858 wxGridTableBase::wxGridTableBase()
1860 m_view
= (wxGrid
*) NULL
;
1861 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1864 wxGridTableBase::~wxGridTableBase()
1866 delete m_attrProvider
;
1869 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1871 delete m_attrProvider
;
1872 m_attrProvider
= attrProvider
;
1875 bool wxGridTableBase::CanHaveAttributes()
1877 if ( ! GetAttrProvider() )
1879 // use the default attr provider by default
1880 SetAttrProvider(new wxGridCellAttrProvider
);
1885 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1887 if ( m_attrProvider
)
1888 return m_attrProvider
->GetAttr(row
, col
);
1890 return (wxGridCellAttr
*)NULL
;
1893 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1895 if ( m_attrProvider
)
1897 m_attrProvider
->SetAttr(attr
, row
, col
);
1901 // as we take ownership of the pointer and don't store it, we must
1907 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1909 if ( m_attrProvider
)
1911 m_attrProvider
->SetRowAttr(attr
, row
);
1915 // as we take ownership of the pointer and don't store it, we must
1921 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1923 if ( m_attrProvider
)
1925 m_attrProvider
->SetColAttr(attr
, col
);
1929 // as we take ownership of the pointer and don't store it, we must
1935 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1937 if ( m_attrProvider
)
1939 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1943 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1945 if ( m_attrProvider
)
1947 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1951 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1953 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1954 "but your derived table class does not override this function") );
1959 bool wxGridTableBase::AppendRows( size_t numRows
)
1961 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1962 "but your derived table class does not override this function"));
1967 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1969 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1970 "but your derived table class does not override this function"));
1975 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1977 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1978 "but your derived table class does not override this function"));
1983 bool wxGridTableBase::AppendCols( size_t numCols
)
1985 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1986 "but your derived table class does not override this function"));
1991 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1993 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1994 "but your derived table class does not override this function"));
2000 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2003 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2004 // how much it makes sense to us geeks.
2008 wxString
wxGridTableBase::GetColLabelValue( int col
)
2010 // default col labels are:
2011 // cols 0 to 25 : A-Z
2012 // cols 26 to 675 : AA-ZZ
2017 for ( n
= 1; ; n
++ )
2019 s
+= (_T('A') + (wxChar
)( col%26
));
2021 if ( col
< 0 ) break;
2024 // reverse the string...
2026 for ( i
= 0; i
< n
; i
++ )
2035 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2037 return wxGRID_VALUE_STRING
;
2040 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2041 const wxString
& typeName
)
2043 return typeName
== wxGRID_VALUE_STRING
;
2046 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2048 return CanGetValueAs(row
, col
, typeName
);
2051 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2056 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2061 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2066 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2067 long WXUNUSED(value
) )
2071 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2072 double WXUNUSED(value
) )
2076 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2077 bool WXUNUSED(value
) )
2082 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2083 const wxString
& WXUNUSED(typeName
) )
2088 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2089 const wxString
& WXUNUSED(typeName
),
2090 void* WXUNUSED(value
) )
2095 //////////////////////////////////////////////////////////////////////
2097 // Message class for the grid table to send requests and notifications
2101 wxGridTableMessage::wxGridTableMessage()
2103 m_table
= (wxGridTableBase
*) NULL
;
2109 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2110 int commandInt1
, int commandInt2
)
2114 m_comInt1
= commandInt1
;
2115 m_comInt2
= commandInt2
;
2120 //////////////////////////////////////////////////////////////////////
2122 // A basic grid table for string data. An object of this class will
2123 // created by wxGrid if you don't specify an alternative table class.
2126 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2128 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2130 wxGridStringTable::wxGridStringTable()
2135 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2140 m_data
.Alloc( numRows
);
2143 sa
.Alloc( numCols
);
2144 for ( col
= 0; col
< numCols
; col
++ )
2146 sa
.Add( wxEmptyString
);
2149 for ( row
= 0; row
< numRows
; row
++ )
2155 wxGridStringTable::~wxGridStringTable()
2159 long wxGridStringTable::GetNumberRows()
2161 return m_data
.GetCount();
2164 long wxGridStringTable::GetNumberCols()
2166 if ( m_data
.GetCount() > 0 )
2167 return m_data
[0].GetCount();
2172 wxString
wxGridStringTable::GetValue( int row
, int col
)
2174 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2175 _T("invalid row or column index in wxGridStringTable") );
2177 return m_data
[row
][col
];
2180 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2182 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2183 _T("invalid row or column index in wxGridStringTable") );
2185 m_data
[row
][col
] = value
;
2188 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2190 wxASSERT_MSG( (row
< GetNumberCols()) && (col
< GetNumberCols()),
2191 _T("invalid row or column index in wxGridStringTable") );
2193 return (m_data
[row
][col
] == wxEmptyString
);
2196 void wxGridStringTable::Clear()
2199 int numRows
, numCols
;
2201 numRows
= m_data
.GetCount();
2204 numCols
= m_data
[0].GetCount();
2206 for ( row
= 0; row
< numRows
; row
++ )
2208 for ( col
= 0; col
< numCols
; col
++ )
2210 m_data
[row
][col
] = wxEmptyString
;
2217 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2221 size_t curNumRows
= m_data
.GetCount();
2222 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2224 if ( pos
>= curNumRows
)
2226 return AppendRows( numRows
);
2230 sa
.Alloc( curNumCols
);
2231 for ( col
= 0; col
< curNumCols
; col
++ )
2233 sa
.Add( wxEmptyString
);
2236 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2238 m_data
.Insert( sa
, row
);
2240 UpdateAttrRows( pos
, numRows
);
2243 wxGridTableMessage
msg( this,
2244 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2248 GetView()->ProcessTableMessage( msg
);
2254 bool wxGridStringTable::AppendRows( size_t numRows
)
2258 size_t curNumRows
= m_data
.GetCount();
2259 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2262 if ( curNumCols
> 0 )
2264 sa
.Alloc( curNumCols
);
2265 for ( col
= 0; col
< curNumCols
; col
++ )
2267 sa
.Add( wxEmptyString
);
2271 for ( row
= 0; row
< numRows
; row
++ )
2278 wxGridTableMessage
msg( this,
2279 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2282 GetView()->ProcessTableMessage( msg
);
2288 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2292 size_t curNumRows
= m_data
.GetCount();
2294 if ( pos
>= curNumRows
)
2297 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2298 "Pos value is invalid for present table with %d rows",
2299 pos
, numRows
, curNumRows
);
2300 wxFAIL_MSG( wxT(errmsg
) );
2304 if ( numRows
> curNumRows
- pos
)
2306 numRows
= curNumRows
- pos
;
2309 if ( numRows
>= curNumRows
)
2311 m_data
.Empty(); // don't release memory just yet
2315 for ( n
= 0; n
< numRows
; n
++ )
2317 m_data
.Remove( pos
);
2320 UpdateAttrRows( pos
, -((int)numRows
) );
2323 wxGridTableMessage
msg( this,
2324 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2328 GetView()->ProcessTableMessage( msg
);
2334 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2338 size_t curNumRows
= m_data
.GetCount();
2339 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2341 if ( pos
>= curNumCols
)
2343 return AppendCols( numCols
);
2346 for ( row
= 0; row
< curNumRows
; row
++ )
2348 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2350 m_data
[row
].Insert( wxEmptyString
, col
);
2353 UpdateAttrCols( pos
, numCols
);
2356 wxGridTableMessage
msg( this,
2357 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2361 GetView()->ProcessTableMessage( msg
);
2367 bool wxGridStringTable::AppendCols( size_t numCols
)
2371 size_t curNumRows
= m_data
.GetCount();
2374 // TODO: something better than this ?
2376 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2377 "Call AppendRows() first") );
2381 for ( row
= 0; row
< curNumRows
; row
++ )
2383 for ( n
= 0; n
< numCols
; n
++ )
2385 m_data
[row
].Add( wxEmptyString
);
2391 wxGridTableMessage
msg( this,
2392 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2395 GetView()->ProcessTableMessage( msg
);
2401 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2405 size_t curNumRows
= m_data
.GetCount();
2406 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2408 if ( pos
>= curNumCols
)
2411 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2412 "Pos value is invalid for present table with %d cols",
2413 pos
, numCols
, curNumCols
);
2414 wxFAIL_MSG( wxT( errmsg
) );
2418 if ( numCols
> curNumCols
- pos
)
2420 numCols
= curNumCols
- pos
;
2423 for ( row
= 0; row
< curNumRows
; row
++ )
2425 if ( numCols
>= curNumCols
)
2427 m_data
[row
].Clear();
2431 for ( n
= 0; n
< numCols
; n
++ )
2433 m_data
[row
].Remove( pos
);
2437 UpdateAttrCols( pos
, -((int)numCols
) );
2440 wxGridTableMessage
msg( this,
2441 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2445 GetView()->ProcessTableMessage( msg
);
2451 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2453 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2455 // using default label
2457 return wxGridTableBase::GetRowLabelValue( row
);
2461 return m_rowLabels
[ row
];
2465 wxString
wxGridStringTable::GetColLabelValue( int col
)
2467 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2469 // using default label
2471 return wxGridTableBase::GetColLabelValue( col
);
2475 return m_colLabels
[ col
];
2479 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2481 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2483 int n
= m_rowLabels
.GetCount();
2485 for ( i
= n
; i
<= row
; i
++ )
2487 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2491 m_rowLabels
[row
] = value
;
2494 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2496 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2498 int n
= m_colLabels
.GetCount();
2500 for ( i
= n
; i
<= col
; i
++ )
2502 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2506 m_colLabels
[col
] = value
;
2511 //////////////////////////////////////////////////////////////////////
2512 //////////////////////////////////////////////////////////////////////
2514 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2516 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2517 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2518 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2519 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2522 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2524 const wxPoint
&pos
, const wxSize
&size
)
2525 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2530 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2534 // NO - don't do this because it will set both the x and y origin
2535 // coords to match the parent scrolled window and we just want to
2536 // set the y coord - MB
2538 // m_owner->PrepareDC( dc );
2541 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2542 dc
.SetDeviceOrigin( 0, -y
);
2544 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2545 m_owner
->DrawRowLabels( dc
);
2549 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2551 m_owner
->ProcessRowLabelMouseEvent( event
);
2555 // This seems to be required for wxMotif otherwise the mouse
2556 // cursor must be in the cell edit control to get key events
2558 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2560 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2565 //////////////////////////////////////////////////////////////////////
2567 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2569 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2570 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2571 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2572 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2575 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2577 const wxPoint
&pos
, const wxSize
&size
)
2578 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2583 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2587 // NO - don't do this because it will set both the x and y origin
2588 // coords to match the parent scrolled window and we just want to
2589 // set the x coord - MB
2591 // m_owner->PrepareDC( dc );
2594 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2595 dc
.SetDeviceOrigin( -x
, 0 );
2597 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2598 m_owner
->DrawColLabels( dc
);
2602 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2604 m_owner
->ProcessColLabelMouseEvent( event
);
2608 // This seems to be required for wxMotif otherwise the mouse
2609 // cursor must be in the cell edit control to get key events
2611 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2613 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2618 //////////////////////////////////////////////////////////////////////
2620 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2622 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2623 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2624 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2625 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2628 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2630 const wxPoint
&pos
, const wxSize
&size
)
2631 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2636 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2640 int client_height
= 0;
2641 int client_width
= 0;
2642 GetClientSize( &client_width
, &client_height
);
2644 dc
.SetPen( *wxBLACK_PEN
);
2645 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2646 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2648 dc
.SetPen( *wxWHITE_PEN
);
2649 dc
.DrawLine( 0, 0, client_width
, 0 );
2650 dc
.DrawLine( 0, 0, 0, client_height
);
2654 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2656 m_owner
->ProcessCornerLabelMouseEvent( event
);
2660 // This seems to be required for wxMotif otherwise the mouse
2661 // cursor must be in the cell edit control to get key events
2663 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2665 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2670 //////////////////////////////////////////////////////////////////////
2672 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2674 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2675 EVT_PAINT( wxGridWindow::OnPaint
)
2676 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2677 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2678 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2681 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2682 wxGridRowLabelWindow
*rowLblWin
,
2683 wxGridColLabelWindow
*colLblWin
,
2684 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2685 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
2688 m_rowLabelWin
= rowLblWin
;
2689 m_colLabelWin
= colLblWin
;
2690 SetBackgroundColour( "WHITE" );
2694 wxGridWindow::~wxGridWindow()
2699 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2701 wxPaintDC
dc( this );
2702 m_owner
->PrepareDC( dc
);
2703 wxRegion reg
= GetUpdateRegion();
2704 m_owner
->CalcCellsExposed( reg
);
2705 m_owner
->DrawGridCellArea( dc
);
2706 #if WXGRID_DRAW_LINES
2707 m_owner
->DrawAllGridLines( dc
, reg
);
2709 m_owner
->DrawGridSpace( dc
);
2710 m_owner
->DrawHighlight( dc
);
2714 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2716 wxPanel::ScrollWindow( dx
, dy
, rect
);
2717 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2718 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2722 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2724 m_owner
->ProcessGridCellMouseEvent( event
);
2728 // This seems to be required for wxMotif otherwise the mouse
2729 // cursor must be in the cell edit control to get key events
2731 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2733 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2737 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2742 //////////////////////////////////////////////////////////////////////
2745 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2747 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2748 EVT_PAINT( wxGrid::OnPaint
)
2749 EVT_SIZE( wxGrid::OnSize
)
2750 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2751 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2754 wxGrid::wxGrid( wxWindow
*parent
,
2759 const wxString
& name
)
2760 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
2761 m_colMinWidths(GRID_HASH_SIZE
),
2762 m_rowMinHeights(GRID_HASH_SIZE
)
2771 wxSafeDecRef(m_defaultCellAttr
);
2773 #ifdef DEBUG_ATTR_CACHE
2774 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2775 wxPrintf(_T("wxGrid attribute cache statistics: "
2776 "total: %u, hits: %u (%u%%)\n"),
2777 total
, gs_nAttrCacheHits
,
2778 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2784 delete m_typeRegistry
;
2789 // ----- internal init and update functions
2792 void wxGrid::Create()
2794 m_created
= FALSE
; // set to TRUE by CreateGrid
2795 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2797 m_table
= (wxGridTableBase
*) NULL
;
2800 m_cellEditCtrlEnabled
= FALSE
;
2802 m_defaultCellAttr
= new wxGridCellAttr
;
2803 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2805 // Set default cell attributes
2806 m_defaultCellAttr
->SetFont(GetFont());
2807 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2808 m_defaultCellAttr
->SetTextColour(
2809 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2810 m_defaultCellAttr
->SetBackgroundColour(
2811 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2812 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2813 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2818 m_currentCellCoords
= wxGridNoCellCoords
;
2820 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2821 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2823 // data type registration: register all standard data types
2824 // TODO: may be allow the app to selectively disable some of them?
2825 m_typeRegistry
= new wxGridTypeRegistry
;
2826 RegisterDataType(wxGRID_VALUE_STRING
,
2827 new wxGridCellStringRenderer
,
2828 new wxGridCellTextEditor
);
2829 RegisterDataType(wxGRID_VALUE_BOOL
,
2830 new wxGridCellBoolRenderer
,
2831 new wxGridCellBoolEditor
);
2832 RegisterDataType(wxGRID_VALUE_NUMBER
,
2833 new wxGridCellNumberRenderer
,
2834 new wxGridCellNumberEditor
);
2836 // subwindow components that make up the wxGrid
2837 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2842 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2847 m_colLabelWin
= new wxGridColLabelWindow( this,
2852 m_gridWin
= new wxGridWindow( this,
2859 SetTargetWindow( m_gridWin
);
2863 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2867 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2872 m_numRows
= numRows
;
2873 m_numCols
= numCols
;
2875 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2876 m_table
->SetView( this );
2885 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2889 // RD: Actually, this should probably be allowed. I think it would be
2890 // nice to be able to switch multiple Tables in and out of a single
2891 // View at runtime. Is there anything in the implmentation that would
2894 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2899 m_numRows
= table
->GetNumberRows();
2900 m_numCols
= table
->GetNumberCols();
2903 m_table
->SetView( this );
2916 if ( m_numRows
<= 0 )
2917 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2919 if ( m_numCols
<= 0 )
2920 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2922 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2923 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2925 if ( m_rowLabelWin
)
2927 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2931 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2934 m_labelTextColour
= wxColour( _T("BLACK") );
2937 m_attrCache
.row
= -1;
2939 // TODO: something better than this ?
2941 m_labelFont
= this->GetFont();
2942 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2944 m_rowLabelHorizAlign
= wxLEFT
;
2945 m_rowLabelVertAlign
= wxCENTRE
;
2947 m_colLabelHorizAlign
= wxCENTRE
;
2948 m_colLabelVertAlign
= wxTOP
;
2950 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2951 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2953 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2954 m_defaultRowHeight
+= 8;
2956 m_defaultRowHeight
+= 4;
2959 m_gridLineColour
= wxColour( 128, 128, 255 );
2960 m_gridLinesEnabled
= TRUE
;
2962 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2963 m_winCapture
= (wxWindow
*)NULL
;
2964 m_canDragRowSize
= TRUE
;
2965 m_canDragColSize
= TRUE
;
2966 m_canDragGridSize
= TRUE
;
2968 m_dragRowOrCol
= -1;
2969 m_isDragging
= FALSE
;
2970 m_startDragPos
= wxDefaultPosition
;
2972 m_waitForSlowClick
= FALSE
;
2974 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2975 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2977 m_currentCellCoords
= wxGridNoCellCoords
;
2979 m_selectedTopLeft
= wxGridNoCellCoords
;
2980 m_selectedBottomRight
= wxGridNoCellCoords
;
2981 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2982 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2984 m_editable
= TRUE
; // default for whole grid
2986 m_inOnKeyDown
= FALSE
;
2995 // ----------------------------------------------------------------------------
2996 // the idea is to call these functions only when necessary because they create
2997 // quite big arrays which eat memory mostly unnecessary - in particular, if
2998 // default widths/heights are used for all rows/columns, we may not use these
3001 // with some extra code, it should be possible to only store the
3002 // widths/heights different from default ones but this will be done later...
3003 // ----------------------------------------------------------------------------
3005 void wxGrid::InitRowHeights()
3007 m_rowHeights
.Empty();
3008 m_rowBottoms
.Empty();
3010 m_rowHeights
.Alloc( m_numRows
);
3011 m_rowBottoms
.Alloc( m_numRows
);
3014 for ( int i
= 0; i
< m_numRows
; i
++ )
3016 m_rowHeights
.Add( m_defaultRowHeight
);
3017 rowBottom
+= m_defaultRowHeight
;
3018 m_rowBottoms
.Add( rowBottom
);
3022 void wxGrid::InitColWidths()
3024 m_colWidths
.Empty();
3025 m_colRights
.Empty();
3027 m_colWidths
.Alloc( m_numCols
);
3028 m_colRights
.Alloc( m_numCols
);
3030 for ( int i
= 0; i
< m_numCols
; i
++ )
3032 m_colWidths
.Add( m_defaultColWidth
);
3033 colRight
+= m_defaultColWidth
;
3034 m_colRights
.Add( colRight
);
3038 int wxGrid::GetColWidth(int col
) const
3040 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3043 int wxGrid::GetColLeft(int col
) const
3045 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3046 : m_colRights
[col
] - m_colWidths
[col
];
3049 int wxGrid::GetColRight(int col
) const
3051 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3055 int wxGrid::GetRowHeight(int row
) const
3057 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3060 int wxGrid::GetRowTop(int row
) const
3062 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3063 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3066 int wxGrid::GetRowBottom(int row
) const
3068 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3069 : m_rowBottoms
[row
];
3072 void wxGrid::CalcDimensions()
3075 GetClientSize( &cw
, &ch
);
3077 if ( m_numRows
> 0 && m_numCols
> 0 )
3079 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3080 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3082 // TODO: restore the scroll position that we had before sizing
3085 GetViewStart( &x
, &y
);
3086 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3087 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3093 void wxGrid::CalcWindowSizes()
3096 GetClientSize( &cw
, &ch
);
3098 if ( m_cornerLabelWin
->IsShown() )
3099 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3101 if ( m_colLabelWin
->IsShown() )
3102 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3104 if ( m_rowLabelWin
->IsShown() )
3105 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3107 if ( m_gridWin
->IsShown() )
3108 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3112 // this is called when the grid table sends a message to say that it
3113 // has been redimensioned
3115 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3119 // if we were using the default widths/heights so far, we must change them
3121 if ( m_colWidths
.IsEmpty() )
3126 if ( m_rowHeights
.IsEmpty() )
3131 switch ( msg
.GetId() )
3133 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3135 size_t pos
= msg
.GetCommandInt();
3136 int numRows
= msg
.GetCommandInt2();
3137 for ( i
= 0; i
< numRows
; i
++ )
3139 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3140 m_rowBottoms
.Insert( 0, pos
);
3142 m_numRows
+= numRows
;
3145 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3147 for ( i
= pos
; i
< m_numRows
; i
++ )
3149 bottom
+= m_rowHeights
[i
];
3150 m_rowBottoms
[i
] = bottom
;
3156 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3158 int numRows
= msg
.GetCommandInt();
3159 for ( i
= 0; i
< numRows
; i
++ )
3161 m_rowHeights
.Add( m_defaultRowHeight
);
3162 m_rowBottoms
.Add( 0 );
3165 int oldNumRows
= m_numRows
;
3166 m_numRows
+= numRows
;
3169 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3171 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3173 bottom
+= m_rowHeights
[i
];
3174 m_rowBottoms
[i
] = bottom
;
3180 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3182 size_t pos
= msg
.GetCommandInt();
3183 int numRows
= msg
.GetCommandInt2();
3184 for ( i
= 0; i
< numRows
; i
++ )
3186 m_rowHeights
.Remove( pos
);
3187 m_rowBottoms
.Remove( pos
);
3189 m_numRows
-= numRows
;
3194 m_colWidths
.Clear();
3195 m_colRights
.Clear();
3196 m_currentCellCoords
= wxGridNoCellCoords
;
3200 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3201 m_currentCellCoords
.Set( 0, 0 );
3204 for ( i
= 0; i
< m_numRows
; i
++ )
3206 h
+= m_rowHeights
[i
];
3207 m_rowBottoms
[i
] = h
;
3215 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3217 size_t pos
= msg
.GetCommandInt();
3218 int numCols
= msg
.GetCommandInt2();
3219 for ( i
= 0; i
< numCols
; i
++ )
3221 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3222 m_colRights
.Insert( 0, pos
);
3224 m_numCols
+= numCols
;
3227 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3229 for ( i
= pos
; i
< m_numCols
; i
++ )
3231 right
+= m_colWidths
[i
];
3232 m_colRights
[i
] = right
;
3238 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3240 int numCols
= msg
.GetCommandInt();
3241 for ( i
= 0; i
< numCols
; i
++ )
3243 m_colWidths
.Add( m_defaultColWidth
);
3244 m_colRights
.Add( 0 );
3247 int oldNumCols
= m_numCols
;
3248 m_numCols
+= numCols
;
3251 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3253 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3255 right
+= m_colWidths
[i
];
3256 m_colRights
[i
] = right
;
3262 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3264 size_t pos
= msg
.GetCommandInt();
3265 int numCols
= msg
.GetCommandInt2();
3266 for ( i
= 0; i
< numCols
; i
++ )
3268 m_colWidths
.Remove( pos
);
3269 m_colRights
.Remove( pos
);
3271 m_numCols
-= numCols
;
3275 #if 0 // leave the row alone here so that AppendCols will work subsequently
3277 m_rowHeights
.Clear();
3278 m_rowBottoms
.Clear();
3280 m_currentCellCoords
= wxGridNoCellCoords
;
3284 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3285 m_currentCellCoords
.Set( 0, 0 );
3288 for ( i
= 0; i
< m_numCols
; i
++ )
3290 w
+= m_colWidths
[i
];
3303 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3305 wxRegionIterator
iter( reg
);
3308 m_rowLabelsExposed
.Empty();
3315 // TODO: remove this when we can...
3316 // There is a bug in wxMotif that gives garbage update
3317 // rectangles if you jump-scroll a long way by clicking the
3318 // scrollbar with middle button. This is a work-around
3320 #if defined(__WXMOTIF__)
3322 m_gridWin
->GetClientSize( &cw
, &ch
);
3323 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3324 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3327 // logical bounds of update region
3330 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3331 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3333 // find the row labels within these bounds
3336 for ( row
= 0; row
< m_numRows
; row
++ )
3338 if ( GetRowBottom(row
) < top
)
3341 if ( GetRowTop(row
) > bottom
)
3344 m_rowLabelsExposed
.Add( row
);
3352 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3354 wxRegionIterator
iter( reg
);
3357 m_colLabelsExposed
.Empty();
3364 // TODO: remove this when we can...
3365 // There is a bug in wxMotif that gives garbage update
3366 // rectangles if you jump-scroll a long way by clicking the
3367 // scrollbar with middle button. This is a work-around
3369 #if defined(__WXMOTIF__)
3371 m_gridWin
->GetClientSize( &cw
, &ch
);
3372 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3373 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3376 // logical bounds of update region
3379 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3380 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3382 // find the cells within these bounds
3385 for ( col
= 0; col
< m_numCols
; col
++ )
3387 if ( GetColRight(col
) < left
)
3390 if ( GetColLeft(col
) > right
)
3393 m_colLabelsExposed
.Add( col
);
3401 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3403 wxRegionIterator
iter( reg
);
3406 m_cellsExposed
.Empty();
3407 m_rowsExposed
.Empty();
3408 m_colsExposed
.Empty();
3410 int left
, top
, right
, bottom
;
3415 // TODO: remove this when we can...
3416 // There is a bug in wxMotif that gives garbage update
3417 // rectangles if you jump-scroll a long way by clicking the
3418 // scrollbar with middle button. This is a work-around
3420 #if defined(__WXMOTIF__)
3422 m_gridWin
->GetClientSize( &cw
, &ch
);
3423 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3424 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3425 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3426 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3429 // logical bounds of update region
3431 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3432 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3434 // find the cells within these bounds
3437 for ( row
= 0; row
< m_numRows
; row
++ )
3439 if ( GetRowBottom(row
) <= top
)
3442 if ( GetRowTop(row
) > bottom
)
3445 m_rowsExposed
.Add( row
);
3447 for ( col
= 0; col
< m_numCols
; col
++ )
3449 if ( GetColRight(col
) <= left
)
3452 if ( GetColLeft(col
) > right
)
3455 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3456 m_colsExposed
.Add( col
);
3457 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3466 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3469 wxPoint
pos( event
.GetPosition() );
3470 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3472 if ( event
.Dragging() )
3474 m_isDragging
= TRUE
;
3476 if ( event
.LeftIsDown() )
3478 switch( m_cursorMode
)
3480 case WXGRID_CURSOR_RESIZE_ROW
:
3482 int cw
, ch
, left
, dummy
;
3483 m_gridWin
->GetClientSize( &cw
, &ch
);
3484 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3486 wxClientDC
dc( m_gridWin
);
3489 GetRowTop(m_dragRowOrCol
) +
3490 GetRowMinimalHeight(m_dragRowOrCol
) );
3491 dc
.SetLogicalFunction(wxINVERT
);
3492 if ( m_dragLastPos
>= 0 )
3494 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3496 dc
.DrawLine( left
, y
, left
+cw
, y
);
3501 case WXGRID_CURSOR_SELECT_ROW
:
3502 if ( (row
= YToRow( y
)) >= 0 &&
3503 !IsInSelection( row
, 0 ) )
3505 SelectRow( row
, TRUE
);
3508 // default label to suppress warnings about "enumeration value
3509 // 'xxx' not handled in switch
3517 m_isDragging
= FALSE
;
3520 // ------------ Entering or leaving the window
3522 if ( event
.Entering() || event
.Leaving() )
3524 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3528 // ------------ Left button pressed
3530 else if ( event
.LeftDown() )
3532 // don't send a label click event for a hit on the
3533 // edge of the row label - this is probably the user
3534 // wanting to resize the row
3536 if ( YToEdgeOfRow(y
) < 0 )
3540 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3542 SelectRow( row
, event
.ShiftDown() );
3543 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3548 // starting to drag-resize a row
3550 if ( CanDragRowSize() )
3551 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3556 // ------------ Left double click
3558 else if (event
.LeftDClick() )
3560 if ( YToEdgeOfRow(y
) < 0 )
3563 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3568 // ------------ Left button released
3570 else if ( event
.LeftUp() )
3572 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3574 DoEndDragResizeRow();
3576 // Note: we are ending the event *after* doing
3577 // default processing in this case
3579 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3582 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3587 // ------------ Right button down
3589 else if ( event
.RightDown() )
3592 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3594 // no default action at the moment
3599 // ------------ Right double click
3601 else if ( event
.RightDClick() )
3604 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3606 // no default action at the moment
3611 // ------------ No buttons down and mouse moving
3613 else if ( event
.Moving() )
3615 m_dragRowOrCol
= YToEdgeOfRow( y
);
3616 if ( m_dragRowOrCol
>= 0 )
3618 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3620 // don't capture the mouse yet
3621 if ( CanDragRowSize() )
3622 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3625 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3627 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3633 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3636 wxPoint
pos( event
.GetPosition() );
3637 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3639 if ( event
.Dragging() )
3641 m_isDragging
= TRUE
;
3643 if ( event
.LeftIsDown() )
3645 switch( m_cursorMode
)
3647 case WXGRID_CURSOR_RESIZE_COL
:
3649 int cw
, ch
, dummy
, top
;
3650 m_gridWin
->GetClientSize( &cw
, &ch
);
3651 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3653 wxClientDC
dc( m_gridWin
);
3656 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3657 GetColMinimalWidth(m_dragRowOrCol
));
3658 dc
.SetLogicalFunction(wxINVERT
);
3659 if ( m_dragLastPos
>= 0 )
3661 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3663 dc
.DrawLine( x
, top
, x
, top
+ch
);
3668 case WXGRID_CURSOR_SELECT_COL
:
3669 if ( (col
= XToCol( x
)) >= 0 &&
3670 !IsInSelection( 0, col
) )
3672 SelectCol( col
, TRUE
);
3675 // default label to suppress warnings about "enumeration value
3676 // 'xxx' not handled in switch
3684 m_isDragging
= FALSE
;
3687 // ------------ Entering or leaving the window
3689 if ( event
.Entering() || event
.Leaving() )
3691 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3695 // ------------ Left button pressed
3697 else if ( event
.LeftDown() )
3699 // don't send a label click event for a hit on the
3700 // edge of the col label - this is probably the user
3701 // wanting to resize the col
3703 if ( XToEdgeOfCol(x
) < 0 )
3707 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3709 SelectCol( col
, event
.ShiftDown() );
3710 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3715 // starting to drag-resize a col
3717 if ( CanDragColSize() )
3718 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3723 // ------------ Left double click
3725 if ( event
.LeftDClick() )
3727 if ( XToEdgeOfCol(x
) < 0 )
3730 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3735 // ------------ Left button released
3737 else if ( event
.LeftUp() )
3739 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3741 DoEndDragResizeCol();
3743 // Note: we are ending the event *after* doing
3744 // default processing in this case
3746 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3749 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3754 // ------------ Right button down
3756 else if ( event
.RightDown() )
3759 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3761 // no default action at the moment
3766 // ------------ Right double click
3768 else if ( event
.RightDClick() )
3771 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3773 // no default action at the moment
3778 // ------------ No buttons down and mouse moving
3780 else if ( event
.Moving() )
3782 m_dragRowOrCol
= XToEdgeOfCol( x
);
3783 if ( m_dragRowOrCol
>= 0 )
3785 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3787 // don't capture the cursor yet
3788 if ( CanDragColSize() )
3789 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3792 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3794 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3800 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3802 if ( event
.LeftDown() )
3804 // indicate corner label by having both row and
3807 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3813 else if ( event
.LeftDClick() )
3815 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3818 else if ( event
.RightDown() )
3820 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3822 // no default action at the moment
3826 else if ( event
.RightDClick() )
3828 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3830 // no default action at the moment
3835 void wxGrid::ChangeCursorMode(CursorMode mode
,
3840 static const wxChar
*cursorModes
[] =
3849 wxLogTrace(_T("grid"),
3850 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3851 win
== m_colLabelWin
? _T("colLabelWin")
3852 : win
? _T("rowLabelWin")
3854 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3855 #endif // __WXDEBUG__
3857 if ( mode
== m_cursorMode
)
3862 // by default use the grid itself
3868 m_winCapture
->ReleaseMouse();
3869 m_winCapture
= (wxWindow
*)NULL
;
3872 m_cursorMode
= mode
;
3874 switch ( m_cursorMode
)
3876 case WXGRID_CURSOR_RESIZE_ROW
:
3877 win
->SetCursor( m_rowResizeCursor
);
3880 case WXGRID_CURSOR_RESIZE_COL
:
3881 win
->SetCursor( m_colResizeCursor
);
3885 win
->SetCursor( *wxSTANDARD_CURSOR
);
3888 // we need to capture mouse when resizing
3889 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3890 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3892 if ( captureMouse
&& resize
)
3894 win
->CaptureMouse();
3899 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3902 wxPoint
pos( event
.GetPosition() );
3903 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3905 wxGridCellCoords coords
;
3906 XYToCell( x
, y
, coords
);
3908 if ( event
.Dragging() )
3910 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3912 // Don't start doing anything until the mouse has been drug at
3913 // least 3 pixels in any direction...
3916 if (m_startDragPos
== wxDefaultPosition
)
3918 m_startDragPos
= pos
;
3921 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3925 m_isDragging
= TRUE
;
3926 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3928 // Hide the edit control, so it
3929 // won't interfer with drag-shrinking.
3930 if ( IsCellEditControlEnabled() )
3932 HideCellEditControl();
3933 SaveEditControlValue();
3936 // Have we captured the mouse yet?
3939 m_winCapture
= m_gridWin
;
3940 m_winCapture
->CaptureMouse();
3943 if ( coords
!= wxGridNoCellCoords
)
3945 if ( !IsSelection() )
3947 SelectBlock( coords
, coords
);
3951 SelectBlock( m_currentCellCoords
, coords
);
3954 if (! IsVisible(coords
))
3956 MakeCellVisible(coords
);
3957 // TODO: need to introduce a delay or something here. The
3958 // scrolling is way to fast, at least on MSW.
3962 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3964 int cw
, ch
, left
, dummy
;
3965 m_gridWin
->GetClientSize( &cw
, &ch
);
3966 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3968 wxClientDC
dc( m_gridWin
);
3970 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3971 dc
.SetLogicalFunction(wxINVERT
);
3972 if ( m_dragLastPos
>= 0 )
3974 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3976 dc
.DrawLine( left
, y
, left
+cw
, y
);
3979 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3981 int cw
, ch
, dummy
, top
;
3982 m_gridWin
->GetClientSize( &cw
, &ch
);
3983 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3985 wxClientDC
dc( m_gridWin
);
3987 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3988 GetColMinimalWidth(m_dragRowOrCol
) );
3989 dc
.SetLogicalFunction(wxINVERT
);
3990 if ( m_dragLastPos
>= 0 )
3992 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3994 dc
.DrawLine( x
, top
, x
, top
+ch
);
4001 m_isDragging
= FALSE
;
4002 m_startDragPos
= wxDefaultPosition
;
4004 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4005 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4008 if ( event
.Entering() || event
.Leaving() )
4010 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4011 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4016 // ------------ Left button pressed
4018 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4020 if ( event
.ShiftDown() )
4022 SelectBlock( m_currentCellCoords
, coords
);
4024 else if ( XToEdgeOfCol(x
) < 0 &&
4025 YToEdgeOfRow(y
) < 0 )
4027 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4032 DisableCellEditControl();
4033 MakeCellVisible( coords
);
4035 // if this is the second click on this cell then start
4037 if ( m_waitForSlowClick
&&
4038 (coords
== m_currentCellCoords
) &&
4039 CanEnableCellControl())
4041 EnableCellEditControl();
4043 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4044 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
4047 m_waitForSlowClick
= FALSE
;
4051 SetCurrentCell( coords
);
4052 m_waitForSlowClick
= TRUE
;
4059 // ------------ Left double click
4061 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4063 DisableCellEditControl();
4065 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4067 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4075 // ------------ Left button released
4077 else if ( event
.LeftUp() )
4079 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4081 if ( IsSelection() )
4085 m_winCapture
->ReleaseMouse();
4086 m_winCapture
= NULL
;
4088 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4091 // Show the edit control, if it has been hidden for
4093 ShowCellEditControl();
4095 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4097 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4098 DoEndDragResizeRow();
4100 // Note: we are ending the event *after* doing
4101 // default processing in this case
4103 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4105 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4107 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4108 DoEndDragResizeCol();
4110 // Note: we are ending the event *after* doing
4111 // default processing in this case
4113 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4120 // ------------ Right button down
4122 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4124 DisableCellEditControl();
4125 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4130 // no default action at the moment
4135 // ------------ Right double click
4137 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4139 DisableCellEditControl();
4140 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4145 // no default action at the moment
4149 // ------------ Moving and no button action
4151 else if ( event
.Moving() && !event
.IsButton() )
4153 int dragRow
= YToEdgeOfRow( y
);
4154 int dragCol
= XToEdgeOfCol( x
);
4156 // Dragging on the corner of a cell to resize in both
4157 // directions is not implemented yet...
4159 if ( dragRow
>= 0 && dragCol
>= 0 )
4161 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4167 m_dragRowOrCol
= dragRow
;
4169 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4171 if ( CanDragRowSize() && CanDragGridSize() )
4172 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4177 m_dragRowOrCol
= dragCol
;
4185 m_dragRowOrCol
= dragCol
;
4187 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4189 if ( CanDragColSize() && CanDragGridSize() )
4190 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4196 // Neither on a row or col edge
4198 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4200 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4206 void wxGrid::DoEndDragResizeRow()
4208 if ( m_dragLastPos
>= 0 )
4210 // erase the last line and resize the row
4212 int cw
, ch
, left
, dummy
;
4213 m_gridWin
->GetClientSize( &cw
, &ch
);
4214 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4216 wxClientDC
dc( m_gridWin
);
4218 dc
.SetLogicalFunction( wxINVERT
);
4219 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4220 HideCellEditControl();
4221 SaveEditControlValue();
4223 int rowTop
= GetRowTop(m_dragRowOrCol
);
4224 SetRowSize( m_dragRowOrCol
,
4225 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4227 if ( !GetBatchCount() )
4229 // Only needed to get the correct rect.y:
4230 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4232 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4233 rect
.width
= m_rowLabelWidth
;
4234 rect
.height
= ch
- rect
.y
;
4235 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4237 m_gridWin
->Refresh( FALSE
, &rect
);
4240 ShowCellEditControl();
4245 void wxGrid::DoEndDragResizeCol()
4247 if ( m_dragLastPos
>= 0 )
4249 // erase the last line and resize the col
4251 int cw
, ch
, dummy
, top
;
4252 m_gridWin
->GetClientSize( &cw
, &ch
);
4253 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4255 wxClientDC
dc( m_gridWin
);
4257 dc
.SetLogicalFunction( wxINVERT
);
4258 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4259 HideCellEditControl();
4260 SaveEditControlValue();
4262 int colLeft
= GetColLeft(m_dragRowOrCol
);
4263 SetColSize( m_dragRowOrCol
,
4264 wxMax( m_dragLastPos
- colLeft
,
4265 GetColMinimalWidth(m_dragRowOrCol
) ) );
4267 if ( !GetBatchCount() )
4269 // Only needed to get the correct rect.x:
4270 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4272 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4273 rect
.width
= cw
- rect
.x
;
4274 rect
.height
= m_colLabelHeight
;
4275 m_colLabelWin
->Refresh( TRUE
, &rect
);
4277 m_gridWin
->Refresh( FALSE
, &rect
);
4280 ShowCellEditControl();
4287 // ------ interaction with data model
4289 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4291 switch ( msg
.GetId() )
4293 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4294 return GetModelValues();
4296 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4297 return SetModelValues();
4299 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4300 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4301 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4302 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4303 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4304 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4305 return Redimension( msg
);
4314 // The behaviour of this function depends on the grid table class
4315 // Clear() function. For the default wxGridStringTable class the
4316 // behavious is to replace all cell contents with wxEmptyString but
4317 // not to change the number of rows or cols.
4319 void wxGrid::ClearGrid()
4323 if (IsCellEditControlEnabled())
4324 DisableCellEditControl();
4327 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4332 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4334 // TODO: something with updateLabels flag
4338 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4344 if (IsCellEditControlEnabled())
4345 DisableCellEditControl();
4347 bool ok
= m_table
->InsertRows( pos
, numRows
);
4349 // the table will have sent the results of the insert row
4350 // operation to this view object as a grid table message
4354 if ( m_numCols
== 0 )
4356 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4358 // TODO: perhaps instead of appending the default number of cols
4359 // we should remember what the last non-zero number of cols was ?
4363 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4365 // if we have just inserted cols into an empty grid the current
4366 // cell will be undefined...
4368 SetCurrentCell( 0, 0 );
4372 if ( !GetBatchCount() ) Refresh();
4384 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4386 // TODO: something with updateLabels flag
4390 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4394 if ( m_table
&& m_table
->AppendRows( numRows
) )
4396 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4398 // if we have just inserted cols into an empty grid the current
4399 // cell will be undefined...
4401 SetCurrentCell( 0, 0 );
4404 // the table will have sent the results of the append row
4405 // operation to this view object as a grid table message
4408 if ( !GetBatchCount() ) Refresh();
4418 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4420 // TODO: something with updateLabels flag
4424 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4430 if (IsCellEditControlEnabled())
4431 DisableCellEditControl();
4433 if (m_table
->DeleteRows( pos
, numRows
))
4436 // the table will have sent the results of the delete row
4437 // operation to this view object as a grid table message
4440 if ( !GetBatchCount() ) Refresh();
4448 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4450 // TODO: something with updateLabels flag
4454 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4460 if (IsCellEditControlEnabled())
4461 DisableCellEditControl();
4463 bool ok
= m_table
->InsertCols( pos
, numCols
);
4465 // the table will have sent the results of the insert col
4466 // operation to this view object as a grid table message
4470 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4472 // if we have just inserted cols into an empty grid the current
4473 // cell will be undefined...
4475 SetCurrentCell( 0, 0 );
4479 if ( !GetBatchCount() ) Refresh();
4491 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4493 // TODO: something with updateLabels flag
4497 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4501 if ( m_table
&& m_table
->AppendCols( numCols
) )
4503 // the table will have sent the results of the append col
4504 // operation to this view object as a grid table message
4506 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4508 // if we have just inserted cols into an empty grid the current
4509 // cell will be undefined...
4511 SetCurrentCell( 0, 0 );
4515 if ( !GetBatchCount() ) Refresh();
4525 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4527 // TODO: something with updateLabels flag
4531 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4537 if (IsCellEditControlEnabled())
4538 DisableCellEditControl();
4540 if ( m_table
->DeleteCols( pos
, numCols
) )
4542 // the table will have sent the results of the delete col
4543 // operation to this view object as a grid table message
4546 if ( !GetBatchCount() ) Refresh();
4556 // ----- event handlers
4559 // Generate a grid event based on a mouse event and
4560 // return the result of ProcessEvent()
4562 bool wxGrid::SendEvent( const wxEventType type
,
4564 wxMouseEvent
& mouseEv
)
4566 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4568 int rowOrCol
= (row
== -1 ? col
: row
);
4570 wxGridSizeEvent
gridEvt( GetId(),
4574 mouseEv
.GetX(), mouseEv
.GetY(),
4575 mouseEv
.ControlDown(),
4576 mouseEv
.ShiftDown(),
4578 mouseEv
.MetaDown() );
4580 return GetEventHandler()->ProcessEvent(gridEvt
);
4582 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4584 wxGridRangeSelectEvent
gridEvt( GetId(),
4588 m_selectedBottomRight
,
4589 mouseEv
.ControlDown(),
4590 mouseEv
.ShiftDown(),
4592 mouseEv
.MetaDown() );
4594 return GetEventHandler()->ProcessEvent(gridEvt
);
4598 wxGridEvent
gridEvt( GetId(),
4602 mouseEv
.GetX(), mouseEv
.GetY(),
4603 mouseEv
.ControlDown(),
4604 mouseEv
.ShiftDown(),
4606 mouseEv
.MetaDown() );
4608 return GetEventHandler()->ProcessEvent(gridEvt
);
4613 // Generate a grid event of specified type and return the result
4614 // of ProcessEvent().
4616 bool wxGrid::SendEvent( const wxEventType type
,
4619 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4621 int rowOrCol
= (row
== -1 ? col
: row
);
4623 wxGridSizeEvent
gridEvt( GetId(),
4628 return GetEventHandler()->ProcessEvent(gridEvt
);
4632 wxGridEvent
gridEvt( GetId(),
4637 return GetEventHandler()->ProcessEvent(gridEvt
);
4642 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4644 wxPaintDC
dc( this );
4646 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4647 m_numRows
&& m_numCols
)
4649 m_currentCellCoords
.Set(0, 0);
4650 ShowCellEditControl();
4657 // This is just here to make sure that CalcDimensions gets called when
4658 // the grid view is resized... then the size event is skipped to allow
4659 // the box sizers to handle everything
4661 void wxGrid::OnSize( wxSizeEvent
& event
)
4668 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4670 if ( m_inOnKeyDown
)
4672 // shouldn't be here - we are going round in circles...
4674 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4677 m_inOnKeyDown
= TRUE
;
4679 // propagate the event up and see if it gets processed
4681 wxWindow
*parent
= GetParent();
4682 wxKeyEvent
keyEvt( event
);
4683 keyEvt
.SetEventObject( parent
);
4685 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4688 // TODO: Should also support Shift-cursor keys for
4689 // extending the selection. Maybe add a flag to
4690 // MoveCursorXXX() and MoveCursorXXXBlock() and
4691 // just send event.ShiftDown().
4693 // try local handlers
4695 switch ( event
.KeyCode() )
4698 if ( event
.ControlDown() )
4700 MoveCursorUpBlock();
4709 if ( event
.ControlDown() )
4711 MoveCursorDownBlock();
4720 if ( event
.ControlDown() )
4722 MoveCursorLeftBlock();
4731 if ( event
.ControlDown() )
4733 MoveCursorRightBlock();
4742 if ( event
.ControlDown() )
4744 event
.Skip(); // to let the edit control have the return
4753 if (event
.ShiftDown())
4760 if ( event
.ControlDown() )
4762 MakeCellVisible( 0, 0 );
4763 SetCurrentCell( 0, 0 );
4772 if ( event
.ControlDown() )
4774 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4775 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4792 if ( !IsEditable() )
4797 // Otherwise fall through to default
4800 // alphanumeric keys or F2 (special key just for this) enable
4801 // the cell edit control
4802 if ( !(event
.AltDown() ||
4804 event
.ControlDown()) &&
4805 (isalnum(event
.KeyCode()) || event
.KeyCode() == WXK_F2
) &&
4806 !IsCellEditControlEnabled() &&
4807 CanEnableCellControl() )
4809 EnableCellEditControl();
4810 int row
= m_currentCellCoords
.GetRow();
4811 int col
= m_currentCellCoords
.GetCol();
4812 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4813 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4818 // let others process char events with modifiers or all
4819 // char events for readonly cells
4826 m_inOnKeyDown
= FALSE
;
4830 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4834 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4836 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4838 // the event has been intercepted - do nothing
4843 m_currentCellCoords
!= wxGridNoCellCoords
)
4845 HideCellEditControl();
4846 DisableCellEditControl();
4848 // Clear the old current cell highlight
4849 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4851 // Otherwise refresh redraws the highlight!
4852 m_currentCellCoords
= coords
;
4854 m_gridWin
->Refresh( FALSE
, &r
);
4857 m_currentCellCoords
= coords
;
4861 wxClientDC
dc(m_gridWin
);
4864 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4865 DrawCellHighlight(dc
, attr
);
4868 if ( IsSelection() )
4870 wxRect
r( SelectionToDeviceRect() );
4872 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4879 // ------ functions to get/send data (see also public functions)
4882 bool wxGrid::GetModelValues()
4886 // all we need to do is repaint the grid
4888 m_gridWin
->Refresh();
4896 bool wxGrid::SetModelValues()
4902 for ( row
= 0; row
< m_numRows
; row
++ )
4904 for ( col
= 0; col
< m_numCols
; col
++ )
4906 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4918 // Note - this function only draws cells that are in the list of
4919 // exposed cells (usually set from the update region by
4920 // CalcExposedCells)
4922 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4924 if ( !m_numRows
|| !m_numCols
) return;
4927 size_t numCells
= m_cellsExposed
.GetCount();
4929 for ( i
= 0; i
< numCells
; i
++ )
4931 DrawCell( dc
, m_cellsExposed
[i
] );
4936 void wxGrid::DrawGridSpace( wxDC
& dc
)
4938 if ( m_numRows
&& m_numCols
)
4941 m_gridWin
->GetClientSize( &cw
, &ch
);
4944 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4946 if ( right
> GetColRight(m_numCols
-1) ||
4947 bottom
> GetRowBottom(m_numRows
-1) )
4950 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4952 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4953 dc
.SetPen( *wxTRANSPARENT_PEN
);
4955 if ( right
> GetColRight(m_numCols
-1) )
4956 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
4957 right
- GetColRight(m_numCols
-1), ch
);
4959 if ( bottom
> GetRowBottom(m_numRows
-1) )
4960 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
4961 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4967 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4969 int row
= coords
.GetRow();
4970 int col
= coords
.GetCol();
4972 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4975 // we draw the cell border ourselves
4976 #if !WXGRID_DRAW_LINES
4977 if ( m_gridLinesEnabled
)
4978 DrawCellBorder( dc
, coords
);
4981 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4983 bool isCurrent
= coords
== m_currentCellCoords
;
4986 rect
.x
= GetColLeft(col
);
4987 rect
.y
= GetRowTop(row
);
4988 rect
.width
= GetColWidth(col
) - 1;
4989 rect
.height
= GetRowHeight(row
) - 1;
4991 // if the editor is shown, we should use it and not the renderer
4992 if ( isCurrent
&& IsCellEditControlEnabled() )
4994 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4998 // but all the rest is drawn by the cell renderer and hence may be
5000 attr
->GetRenderer(this, row
, col
)->
5001 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5008 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5010 int row
= m_currentCellCoords
.GetRow();
5011 int col
= m_currentCellCoords
.GetCol();
5013 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5017 rect
.x
= GetColLeft(col
);
5018 rect
.y
= GetRowTop(row
);
5019 rect
.width
= GetColWidth(col
) - 1;
5020 rect
.height
= GetRowHeight(row
) - 1;
5022 // hmmm... what could we do here to show that the cell is disabled?
5023 // for now, I just draw a thinner border than for the other ones, but
5024 // it doesn't look really good
5025 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5026 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5028 dc
.DrawRectangle(rect
);
5031 // VZ: my experiments with 3d borders...
5033 // how to properly set colours for arbitrary bg?
5034 wxCoord x1
= rect
.x
,
5036 x2
= rect
.x
+ rect
.width
-1,
5037 y2
= rect
.y
+ rect
.height
-1;
5039 dc
.SetPen(*wxWHITE_PEN
);
5040 dc
.DrawLine(x1
, y1
, x2
, y1
);
5041 dc
.DrawLine(x1
, y1
, x1
, y2
);
5043 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5044 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5046 dc
.SetPen(*wxBLACK_PEN
);
5047 dc
.DrawLine(x1
, y2
, x2
, y2
);
5048 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5053 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5055 int row
= coords
.GetRow();
5056 int col
= coords
.GetCol();
5057 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5060 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5062 // right hand border
5064 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5065 GetColRight(col
), GetRowBottom(row
) );
5069 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5070 GetColRight(col
), GetRowBottom(row
) );
5073 void wxGrid::DrawHighlight(wxDC
& dc
)
5075 if ( IsCellEditControlEnabled() )
5077 // don't show highlight when the edit control is shown
5081 // if the active cell was repainted, repaint its highlight too because it
5082 // might have been damaged by the grid lines
5083 size_t count
= m_cellsExposed
.GetCount();
5084 for ( size_t n
= 0; n
< count
; n
++ )
5086 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5088 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5089 DrawCellHighlight(dc
, attr
);
5097 // TODO: remove this ???
5098 // This is used to redraw all grid lines e.g. when the grid line colour
5101 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5103 if ( !m_gridLinesEnabled
||
5105 !m_numCols
) return;
5107 int top
, bottom
, left
, right
;
5113 m_gridWin
->GetClientSize(&cw
, &ch
);
5115 // virtual coords of visible area
5117 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5118 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5123 reg
.GetBox(x
, y
, w
, h
);
5124 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5125 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5129 m_gridWin
->GetClientSize(&cw
, &ch
);
5130 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5131 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5134 // avoid drawing grid lines past the last row and col
5136 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5137 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5139 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5141 // horizontal grid lines
5144 for ( i
= 0; i
< m_numRows
; i
++ )
5146 int bot
= GetRowBottom(i
) - 1;
5155 dc
.DrawLine( left
, bot
, right
, bot
);
5160 // vertical grid lines
5162 for ( i
= 0; i
< m_numCols
; i
++ )
5164 int colRight
= GetColRight(i
) - 1;
5165 if ( colRight
> right
)
5170 if ( colRight
>= left
)
5172 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5178 void wxGrid::DrawRowLabels( wxDC
& dc
)
5180 if ( !m_numRows
|| !m_numCols
) return;
5183 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5185 for ( i
= 0; i
< numLabels
; i
++ )
5187 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5192 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5194 if ( GetRowHeight(row
) <= 0 )
5197 int rowTop
= GetRowTop(row
),
5198 rowBottom
= GetRowBottom(row
) - 1;
5200 dc
.SetPen( *wxBLACK_PEN
);
5201 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5202 m_rowLabelWidth
-1, rowBottom
);
5204 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5206 dc
.SetPen( *wxWHITE_PEN
);
5207 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5208 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5210 dc
.SetBackgroundMode( wxTRANSPARENT
);
5211 dc
.SetTextForeground( GetLabelTextColour() );
5212 dc
.SetFont( GetLabelFont() );
5215 GetRowLabelAlignment( &hAlign
, &vAlign
);
5219 rect
.SetY( GetRowTop(row
) + 2 );
5220 rect
.SetWidth( m_rowLabelWidth
- 4 );
5221 rect
.SetHeight( GetRowHeight(row
) - 4 );
5222 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5226 void wxGrid::DrawColLabels( wxDC
& dc
)
5228 if ( !m_numRows
|| !m_numCols
) return;
5231 size_t numLabels
= m_colLabelsExposed
.GetCount();
5233 for ( i
= 0; i
< numLabels
; i
++ )
5235 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5240 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5242 if ( GetColWidth(col
) <= 0 )
5245 int colLeft
= GetColLeft(col
),
5246 colRight
= GetColRight(col
) - 1;
5248 dc
.SetPen( *wxBLACK_PEN
);
5249 dc
.DrawLine( colRight
, 0,
5250 colRight
, m_colLabelHeight
-1 );
5252 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5253 colRight
, m_colLabelHeight
-1 );
5255 dc
.SetPen( *wxWHITE_PEN
);
5256 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5257 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5259 dc
.SetBackgroundMode( wxTRANSPARENT
);
5260 dc
.SetTextForeground( GetLabelTextColour() );
5261 dc
.SetFont( GetLabelFont() );
5263 dc
.SetBackgroundMode( wxTRANSPARENT
);
5264 dc
.SetTextForeground( GetLabelTextColour() );
5265 dc
.SetFont( GetLabelFont() );
5268 GetColLabelAlignment( &hAlign
, &vAlign
);
5271 rect
.SetX( colLeft
+ 2 );
5273 rect
.SetWidth( GetColWidth(col
) - 4 );
5274 rect
.SetHeight( m_colLabelHeight
- 4 );
5275 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5279 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5280 const wxString
& value
,
5285 long textWidth
, textHeight
;
5286 long lineWidth
, lineHeight
;
5287 wxArrayString lines
;
5289 dc
.SetClippingRegion( rect
);
5290 StringToLines( value
, lines
);
5291 if ( lines
.GetCount() )
5293 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5294 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5297 switch ( horizAlign
)
5300 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5304 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5313 switch ( vertAlign
)
5316 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5320 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5329 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5331 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5336 dc
.DestroyClippingRegion();
5340 // Split multi line text up into an array of strings. Any existing
5341 // contents of the string array are preserved.
5343 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5347 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5348 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5350 while ( startPos
< (int)tVal
.Length() )
5352 pos
= tVal
.Mid(startPos
).Find( eol
);
5357 else if ( pos
== 0 )
5359 lines
.Add( wxEmptyString
);
5363 lines
.Add( value
.Mid(startPos
, pos
) );
5367 if ( startPos
< (int)value
.Length() )
5369 lines
.Add( value
.Mid( startPos
) );
5374 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5375 wxArrayString
& lines
,
5376 long *width
, long *height
)
5383 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5385 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5386 w
= wxMax( w
, lineW
);
5396 // ------ Edit control functions
5400 void wxGrid::EnableEditing( bool edit
)
5402 // TODO: improve this ?
5404 if ( edit
!= m_editable
)
5408 // FIXME IMHO this won't disable the edit control if edit == FALSE
5409 // because of the check in the beginning of
5410 // EnableCellEditControl() just below (VZ)
5411 EnableCellEditControl(m_editable
);
5416 void wxGrid::EnableCellEditControl( bool enable
)
5421 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5422 SetCurrentCell( 0, 0 );
5424 if ( enable
!= m_cellEditCtrlEnabled
)
5426 // TODO allow the app to Veto() this event?
5427 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5431 // this should be checked by the caller!
5432 wxASSERT_MSG( CanEnableCellControl(),
5433 _T("can't enable editing for this cell!") );
5435 // do it before ShowCellEditControl()
5436 m_cellEditCtrlEnabled
= enable
;
5438 ShowCellEditControl();
5442 HideCellEditControl();
5443 SaveEditControlValue();
5445 // do it after HideCellEditControl()
5446 m_cellEditCtrlEnabled
= enable
;
5451 bool wxGrid::IsCurrentCellReadOnly() const
5454 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5455 bool readonly
= attr
->IsReadOnly();
5461 bool wxGrid::CanEnableCellControl() const
5463 return m_editable
&& !IsCurrentCellReadOnly();
5466 bool wxGrid::IsCellEditControlEnabled() const
5468 // the cell edit control might be disable for all cells or just for the
5469 // current one if it's read only
5470 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5473 void wxGrid::ShowCellEditControl()
5475 if ( IsCellEditControlEnabled() )
5477 if ( !IsVisible( m_currentCellCoords
) )
5483 wxRect rect
= CellToRect( m_currentCellCoords
);
5484 int row
= m_currentCellCoords
.GetRow();
5485 int col
= m_currentCellCoords
.GetCol();
5487 // convert to scrolled coords
5489 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5491 // done in PaintBackground()
5493 // erase the highlight and the cell contents because the editor
5494 // might not cover the entire cell
5495 wxClientDC
dc( m_gridWin
);
5497 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5498 dc
.SetPen(*wxTRANSPARENT_PEN
);
5499 dc
.DrawRectangle(rect
);
5502 // cell is shifted by one pixel
5506 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5507 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5508 if ( !editor
->IsCreated() )
5510 editor
->Create(m_gridWin
, -1,
5511 new wxGridCellEditorEvtHandler(this, editor
));
5514 editor
->Show( TRUE
, attr
);
5516 editor
->SetSize( rect
);
5518 editor
->BeginEdit(row
, col
, this);
5525 void wxGrid::HideCellEditControl()
5527 if ( IsCellEditControlEnabled() )
5529 int row
= m_currentCellCoords
.GetRow();
5530 int col
= m_currentCellCoords
.GetCol();
5532 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5533 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5535 m_gridWin
->SetFocus();
5540 void wxGrid::SaveEditControlValue()
5542 if ( IsCellEditControlEnabled() )
5544 int row
= m_currentCellCoords
.GetRow();
5545 int col
= m_currentCellCoords
.GetCol();
5547 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5548 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5549 bool changed
= editor
->EndEdit(row
, col
, this);
5555 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5556 m_currentCellCoords
.GetRow(),
5557 m_currentCellCoords
.GetCol() );
5564 // ------ Grid location functions
5565 // Note that all of these functions work with the logical coordinates of
5566 // grid cells and labels so you will need to convert from device
5567 // coordinates for mouse events etc.
5570 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5572 int row
= YToRow(y
);
5573 int col
= XToCol(x
);
5575 if ( row
== -1 || col
== -1 )
5577 coords
= wxGridNoCellCoords
;
5581 coords
.Set( row
, col
);
5586 int wxGrid::YToRow( int y
)
5590 for ( i
= 0; i
< m_numRows
; i
++ )
5592 if ( y
< GetRowBottom(i
) )
5600 int wxGrid::XToCol( int x
)
5604 for ( i
= 0; i
< m_numCols
; i
++ )
5606 if ( x
< GetColRight(i
) )
5614 // return the row number that that the y coord is near the edge of, or
5615 // -1 if not near an edge
5617 int wxGrid::YToEdgeOfRow( int y
)
5621 for ( i
= 0; i
< m_numRows
; i
++ )
5623 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5625 d
= abs( y
- GetRowBottom(i
) );
5626 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5635 // return the col number that that the x coord is near the edge of, or
5636 // -1 if not near an edge
5638 int wxGrid::XToEdgeOfCol( int x
)
5642 for ( i
= 0; i
< m_numCols
; i
++ )
5644 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5646 d
= abs( x
- GetColRight(i
) );
5647 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5656 wxRect
wxGrid::CellToRect( int row
, int col
)
5658 wxRect
rect( -1, -1, -1, -1 );
5660 if ( row
>= 0 && row
< m_numRows
&&
5661 col
>= 0 && col
< m_numCols
)
5663 rect
.x
= GetColLeft(col
);
5664 rect
.y
= GetRowTop(row
);
5665 rect
.width
= GetColWidth(col
);
5666 rect
.height
= GetRowHeight(row
);
5673 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5675 // get the cell rectangle in logical coords
5677 wxRect
r( CellToRect( row
, col
) );
5679 // convert to device coords
5681 int left
, top
, right
, bottom
;
5682 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5683 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5685 // check against the client area of the grid window
5688 m_gridWin
->GetClientSize( &cw
, &ch
);
5690 if ( wholeCellVisible
)
5692 // is the cell wholly visible ?
5694 return ( left
>= 0 && right
<= cw
&&
5695 top
>= 0 && bottom
<= ch
);
5699 // is the cell partly visible ?
5701 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5702 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5707 // make the specified cell location visible by doing a minimal amount
5710 void wxGrid::MakeCellVisible( int row
, int col
)
5713 int xpos
= -1, ypos
= -1;
5715 if ( row
>= 0 && row
< m_numRows
&&
5716 col
>= 0 && col
< m_numCols
)
5718 // get the cell rectangle in logical coords
5720 wxRect
r( CellToRect( row
, col
) );
5722 // convert to device coords
5724 int left
, top
, right
, bottom
;
5725 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5726 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5729 m_gridWin
->GetClientSize( &cw
, &ch
);
5735 else if ( bottom
> ch
)
5737 int h
= r
.GetHeight();
5739 for ( i
= row
-1; i
>= 0; i
-- )
5741 int rowHeight
= GetRowHeight(i
);
5742 if ( h
+ rowHeight
> ch
)
5749 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5750 // have rounding errors (this is important, because if we do, we
5751 // might not scroll at all and some cells won't be redrawn)
5752 ypos
+= GRID_SCROLL_LINE
/ 2;
5759 else if ( right
> cw
)
5761 int w
= r
.GetWidth();
5763 for ( i
= col
-1; i
>= 0; i
-- )
5765 int colWidth
= GetColWidth(i
);
5766 if ( w
+ colWidth
> cw
)
5773 // see comment for ypos above
5774 xpos
+= GRID_SCROLL_LINE
/ 2;
5777 if ( xpos
!= -1 || ypos
!= -1 )
5779 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5780 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5781 Scroll( xpos
, ypos
);
5789 // ------ Grid cursor movement functions
5792 bool wxGrid::MoveCursorUp()
5794 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5795 m_currentCellCoords
.GetRow() > 0 )
5797 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5798 m_currentCellCoords
.GetCol() );
5800 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5801 m_currentCellCoords
.GetCol() );
5810 bool wxGrid::MoveCursorDown()
5812 // TODO: allow for scrolling
5814 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5815 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5817 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5818 m_currentCellCoords
.GetCol() );
5820 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5821 m_currentCellCoords
.GetCol() );
5830 bool wxGrid::MoveCursorLeft()
5832 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5833 m_currentCellCoords
.GetCol() > 0 )
5835 MakeCellVisible( m_currentCellCoords
.GetRow(),
5836 m_currentCellCoords
.GetCol() - 1 );
5838 SetCurrentCell( m_currentCellCoords
.GetRow(),
5839 m_currentCellCoords
.GetCol() - 1 );
5848 bool wxGrid::MoveCursorRight()
5850 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5851 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5853 MakeCellVisible( m_currentCellCoords
.GetRow(),
5854 m_currentCellCoords
.GetCol() + 1 );
5856 SetCurrentCell( m_currentCellCoords
.GetRow(),
5857 m_currentCellCoords
.GetCol() + 1 );
5866 bool wxGrid::MovePageUp()
5868 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5870 int row
= m_currentCellCoords
.GetRow();
5874 m_gridWin
->GetClientSize( &cw
, &ch
);
5876 int y
= GetRowTop(row
);
5877 int newRow
= YToRow( y
- ch
+ 1 );
5882 else if ( newRow
== row
)
5887 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5888 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5896 bool wxGrid::MovePageDown()
5898 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5900 int row
= m_currentCellCoords
.GetRow();
5901 if ( row
< m_numRows
)
5904 m_gridWin
->GetClientSize( &cw
, &ch
);
5906 int y
= GetRowTop(row
);
5907 int newRow
= YToRow( y
+ ch
);
5910 newRow
= m_numRows
- 1;
5912 else if ( newRow
== row
)
5917 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5918 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5926 bool wxGrid::MoveCursorUpBlock()
5929 m_currentCellCoords
!= wxGridNoCellCoords
&&
5930 m_currentCellCoords
.GetRow() > 0 )
5932 int row
= m_currentCellCoords
.GetRow();
5933 int col
= m_currentCellCoords
.GetCol();
5935 if ( m_table
->IsEmptyCell(row
, col
) )
5937 // starting in an empty cell: find the next block of
5943 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5946 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5948 // starting at the top of a block: find the next block
5954 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5959 // starting within a block: find the top of the block
5964 if ( m_table
->IsEmptyCell(row
, col
) )
5972 MakeCellVisible( row
, col
);
5973 SetCurrentCell( row
, col
);
5981 bool wxGrid::MoveCursorDownBlock()
5984 m_currentCellCoords
!= wxGridNoCellCoords
&&
5985 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5987 int row
= m_currentCellCoords
.GetRow();
5988 int col
= m_currentCellCoords
.GetCol();
5990 if ( m_table
->IsEmptyCell(row
, col
) )
5992 // starting in an empty cell: find the next block of
5995 while ( row
< m_numRows
-1 )
5998 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6001 else if ( m_table
->IsEmptyCell(row
+1, col
) )
6003 // starting at the bottom of a block: find the next block
6006 while ( row
< m_numRows
-1 )
6009 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6014 // starting within a block: find the bottom of the block
6016 while ( row
< m_numRows
-1 )
6019 if ( m_table
->IsEmptyCell(row
, col
) )
6027 MakeCellVisible( row
, col
);
6028 SetCurrentCell( row
, col
);
6036 bool wxGrid::MoveCursorLeftBlock()
6039 m_currentCellCoords
!= wxGridNoCellCoords
&&
6040 m_currentCellCoords
.GetCol() > 0 )
6042 int row
= m_currentCellCoords
.GetRow();
6043 int col
= m_currentCellCoords
.GetCol();
6045 if ( m_table
->IsEmptyCell(row
, col
) )
6047 // starting in an empty cell: find the next block of
6053 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6056 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6058 // starting at the left of a block: find the next block
6064 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6069 // starting within a block: find the left of the block
6074 if ( m_table
->IsEmptyCell(row
, col
) )
6082 MakeCellVisible( row
, col
);
6083 SetCurrentCell( row
, col
);
6091 bool wxGrid::MoveCursorRightBlock()
6094 m_currentCellCoords
!= wxGridNoCellCoords
&&
6095 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6097 int row
= m_currentCellCoords
.GetRow();
6098 int col
= m_currentCellCoords
.GetCol();
6100 if ( m_table
->IsEmptyCell(row
, col
) )
6102 // starting in an empty cell: find the next block of
6105 while ( col
< m_numCols
-1 )
6108 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6111 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6113 // starting at the right of a block: find the next block
6116 while ( col
< m_numCols
-1 )
6119 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6124 // starting within a block: find the right of the block
6126 while ( col
< m_numCols
-1 )
6129 if ( m_table
->IsEmptyCell(row
, col
) )
6137 MakeCellVisible( row
, col
);
6138 SetCurrentCell( row
, col
);
6149 // ------ Label values and formatting
6152 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6154 *horiz
= m_rowLabelHorizAlign
;
6155 *vert
= m_rowLabelVertAlign
;
6158 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6160 *horiz
= m_colLabelHorizAlign
;
6161 *vert
= m_colLabelVertAlign
;
6164 wxString
wxGrid::GetRowLabelValue( int row
)
6168 return m_table
->GetRowLabelValue( row
);
6178 wxString
wxGrid::GetColLabelValue( int col
)
6182 return m_table
->GetColLabelValue( col
);
6193 void wxGrid::SetRowLabelSize( int width
)
6195 width
= wxMax( width
, 0 );
6196 if ( width
!= m_rowLabelWidth
)
6200 m_rowLabelWin
->Show( FALSE
);
6201 m_cornerLabelWin
->Show( FALSE
);
6203 else if ( m_rowLabelWidth
== 0 )
6205 m_rowLabelWin
->Show( TRUE
);
6206 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6209 m_rowLabelWidth
= width
;
6216 void wxGrid::SetColLabelSize( int height
)
6218 height
= wxMax( height
, 0 );
6219 if ( height
!= m_colLabelHeight
)
6223 m_colLabelWin
->Show( FALSE
);
6224 m_cornerLabelWin
->Show( FALSE
);
6226 else if ( m_colLabelHeight
== 0 )
6228 m_colLabelWin
->Show( TRUE
);
6229 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6232 m_colLabelHeight
= height
;
6239 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6241 if ( m_labelBackgroundColour
!= colour
)
6243 m_labelBackgroundColour
= colour
;
6244 m_rowLabelWin
->SetBackgroundColour( colour
);
6245 m_colLabelWin
->SetBackgroundColour( colour
);
6246 m_cornerLabelWin
->SetBackgroundColour( colour
);
6248 if ( !GetBatchCount() )
6250 m_rowLabelWin
->Refresh();
6251 m_colLabelWin
->Refresh();
6252 m_cornerLabelWin
->Refresh();
6257 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6259 if ( m_labelTextColour
!= colour
)
6261 m_labelTextColour
= colour
;
6262 if ( !GetBatchCount() )
6264 m_rowLabelWin
->Refresh();
6265 m_colLabelWin
->Refresh();
6270 void wxGrid::SetLabelFont( const wxFont
& font
)
6273 if ( !GetBatchCount() )
6275 m_rowLabelWin
->Refresh();
6276 m_colLabelWin
->Refresh();
6280 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6282 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6284 m_rowLabelHorizAlign
= horiz
;
6287 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6289 m_rowLabelVertAlign
= vert
;
6292 if ( !GetBatchCount() )
6294 m_rowLabelWin
->Refresh();
6298 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6300 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6302 m_colLabelHorizAlign
= horiz
;
6305 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6307 m_colLabelVertAlign
= vert
;
6310 if ( !GetBatchCount() )
6312 m_colLabelWin
->Refresh();
6316 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6320 m_table
->SetRowLabelValue( row
, s
);
6321 if ( !GetBatchCount() )
6323 wxRect rect
= CellToRect( row
, 0);
6324 if ( rect
.height
> 0 )
6326 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6328 rect
.width
= m_rowLabelWidth
;
6329 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6335 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6339 m_table
->SetColLabelValue( col
, s
);
6340 if ( !GetBatchCount() )
6342 wxRect rect
= CellToRect( 0, col
);
6343 if ( rect
.width
> 0 )
6345 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6347 rect
.height
= m_colLabelHeight
;
6348 m_colLabelWin
->Refresh( TRUE
, &rect
);
6354 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6356 if ( m_gridLineColour
!= colour
)
6358 m_gridLineColour
= colour
;
6360 wxClientDC
dc( m_gridWin
);
6362 DrawAllGridLines( dc
, wxRegion() );
6366 void wxGrid::EnableGridLines( bool enable
)
6368 if ( enable
!= m_gridLinesEnabled
)
6370 m_gridLinesEnabled
= enable
;
6372 if ( !GetBatchCount() )
6376 wxClientDC
dc( m_gridWin
);
6378 DrawAllGridLines( dc
, wxRegion() );
6382 m_gridWin
->Refresh();
6389 int wxGrid::GetDefaultRowSize()
6391 return m_defaultRowHeight
;
6394 int wxGrid::GetRowSize( int row
)
6396 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6398 return GetRowHeight(row
);
6401 int wxGrid::GetDefaultColSize()
6403 return m_defaultColWidth
;
6406 int wxGrid::GetColSize( int col
)
6408 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6410 return GetColWidth(col
);
6413 // ============================================================================
6414 // access to the grid attributes: each of them has a default value in the grid
6415 // itself and may be overidden on a per-cell basis
6416 // ============================================================================
6418 // ----------------------------------------------------------------------------
6419 // setting default attributes
6420 // ----------------------------------------------------------------------------
6422 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6424 m_defaultCellAttr
->SetBackgroundColour(col
);
6426 m_gridWin
->SetBackgroundColour(col
);
6430 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6432 m_defaultCellAttr
->SetTextColour(col
);
6435 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6437 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6440 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6442 m_defaultCellAttr
->SetFont(font
);
6445 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6447 m_defaultCellAttr
->SetRenderer(renderer
);
6450 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6452 m_defaultCellAttr
->SetEditor(editor
);
6455 // ----------------------------------------------------------------------------
6456 // access to the default attrbiutes
6457 // ----------------------------------------------------------------------------
6459 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6461 return m_defaultCellAttr
->GetBackgroundColour();
6464 wxColour
wxGrid::GetDefaultCellTextColour()
6466 return m_defaultCellAttr
->GetTextColour();
6469 wxFont
wxGrid::GetDefaultCellFont()
6471 return m_defaultCellAttr
->GetFont();
6474 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6476 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6479 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6481 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6484 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6486 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6489 // ----------------------------------------------------------------------------
6490 // access to cell attributes
6491 // ----------------------------------------------------------------------------
6493 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6495 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6496 wxColour colour
= attr
->GetBackgroundColour();
6501 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6503 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6504 wxColour colour
= attr
->GetTextColour();
6509 wxFont
wxGrid::GetCellFont( int row
, int col
)
6511 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6512 wxFont font
= attr
->GetFont();
6517 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6519 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6520 attr
->GetAlignment(horiz
, vert
);
6524 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6526 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6527 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6532 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6534 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6535 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6540 bool wxGrid::IsReadOnly(int row
, int col
) const
6542 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6543 bool isReadOnly
= attr
->IsReadOnly();
6548 // ----------------------------------------------------------------------------
6549 // attribute support: cache, automatic provider creation, ...
6550 // ----------------------------------------------------------------------------
6552 bool wxGrid::CanHaveAttributes()
6559 return m_table
->CanHaveAttributes();
6562 void wxGrid::ClearAttrCache()
6564 if ( m_attrCache
.row
!= -1 )
6566 wxSafeDecRef(m_attrCache
.attr
);
6567 m_attrCache
.row
= -1;
6571 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6573 wxGrid
*self
= (wxGrid
*)this; // const_cast
6575 self
->ClearAttrCache();
6576 self
->m_attrCache
.row
= row
;
6577 self
->m_attrCache
.col
= col
;
6578 self
->m_attrCache
.attr
= attr
;
6582 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6584 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6586 *attr
= m_attrCache
.attr
;
6587 wxSafeIncRef(m_attrCache
.attr
);
6589 #ifdef DEBUG_ATTR_CACHE
6590 gs_nAttrCacheHits
++;
6597 #ifdef DEBUG_ATTR_CACHE
6598 gs_nAttrCacheMisses
++;
6604 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6606 wxGridCellAttr
*attr
;
6607 if ( !LookupAttr(row
, col
, &attr
) )
6609 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6610 CacheAttr(row
, col
, attr
);
6614 attr
->SetDefAttr(m_defaultCellAttr
);
6618 attr
= m_defaultCellAttr
;
6625 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6627 wxGridCellAttr
*attr
;
6628 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6630 wxASSERT_MSG( m_table
,
6631 _T("we may only be called if CanHaveAttributes() "
6632 "returned TRUE and then m_table should be !NULL") );
6634 attr
= m_table
->GetAttr(row
, col
);
6637 attr
= new wxGridCellAttr
;
6639 // artificially inc the ref count to match DecRef() in caller
6642 m_table
->SetAttr(attr
, row
, col
);
6645 CacheAttr(row
, col
, attr
);
6647 attr
->SetDefAttr(m_defaultCellAttr
);
6651 // ----------------------------------------------------------------------------
6652 // setting cell attributes: this is forwarded to the table
6653 // ----------------------------------------------------------------------------
6655 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6657 if ( CanHaveAttributes() )
6659 m_table
->SetRowAttr(attr
, row
);
6667 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6669 if ( CanHaveAttributes() )
6671 m_table
->SetColAttr(attr
, col
);
6679 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6681 if ( CanHaveAttributes() )
6683 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6684 attr
->SetBackgroundColour(colour
);
6689 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6691 if ( CanHaveAttributes() )
6693 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6694 attr
->SetTextColour(colour
);
6699 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6701 if ( CanHaveAttributes() )
6703 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6704 attr
->SetFont(font
);
6709 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6711 if ( CanHaveAttributes() )
6713 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6714 attr
->SetAlignment(horiz
, vert
);
6719 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6721 if ( CanHaveAttributes() )
6723 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6724 attr
->SetRenderer(renderer
);
6729 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6731 if ( CanHaveAttributes() )
6733 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6734 attr
->SetEditor(editor
);
6739 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6741 if ( CanHaveAttributes() )
6743 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6744 attr
->SetReadOnly(isReadOnly
);
6749 // ----------------------------------------------------------------------------
6750 // Data type registration
6751 // ----------------------------------------------------------------------------
6753 void wxGrid::RegisterDataType(const wxString
& typeName
,
6754 wxGridCellRenderer
* renderer
,
6755 wxGridCellEditor
* editor
)
6757 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6761 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6763 wxString typeName
= m_table
->GetTypeName(row
, col
);
6764 return GetDefaultEditorForType(typeName
);
6767 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6769 wxString typeName
= m_table
->GetTypeName(row
, col
);
6770 return GetDefaultRendererForType(typeName
);
6774 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6776 int index
= m_typeRegistry
->FindDataType(typeName
);
6778 // Should we force the failure here or let it fallback to string handling???
6779 // wxFAIL_MSG(wxT("Unknown data type name"));
6782 return m_typeRegistry
->GetEditor(index
);
6786 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6788 int index
= m_typeRegistry
->FindDataType(typeName
);
6790 // Should we force the failure here or let it fallback to string handling???
6791 // wxFAIL_MSG(wxT("Unknown data type name"));
6794 return m_typeRegistry
->GetRenderer(index
);
6798 // ----------------------------------------------------------------------------
6800 // ----------------------------------------------------------------------------
6802 void wxGrid::EnableDragRowSize( bool enable
)
6804 m_canDragRowSize
= enable
;
6808 void wxGrid::EnableDragColSize( bool enable
)
6810 m_canDragColSize
= enable
;
6813 void wxGrid::EnableDragGridSize( bool enable
)
6815 m_canDragGridSize
= enable
;
6819 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6821 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6823 if ( resizeExistingRows
)
6831 void wxGrid::SetRowSize( int row
, int height
)
6833 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6835 if ( m_rowHeights
.IsEmpty() )
6837 // need to really create the array
6841 int h
= wxMax( 0, height
);
6842 int diff
= h
- m_rowHeights
[row
];
6844 m_rowHeights
[row
] = h
;
6846 for ( i
= row
; i
< m_numRows
; i
++ )
6848 m_rowBottoms
[i
] += diff
;
6853 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6855 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6857 if ( resizeExistingCols
)
6865 void wxGrid::SetColSize( int col
, int width
)
6867 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6869 // should we check that it's bigger than GetColMinimalWidth(col) here?
6871 if ( m_colWidths
.IsEmpty() )
6873 // need to really create the array
6877 int w
= wxMax( 0, width
);
6878 int diff
= w
- m_colWidths
[col
];
6879 m_colWidths
[col
] = w
;
6882 for ( i
= col
; i
< m_numCols
; i
++ )
6884 m_colRights
[i
] += diff
;
6890 void wxGrid::SetColMinimalWidth( int col
, int width
)
6892 m_colMinWidths
.Put(col
, width
);
6895 void wxGrid::SetRowMinimalHeight( int row
, int width
)
6897 m_rowMinHeights
.Put(row
, width
);
6900 int wxGrid::GetColMinimalWidth(int col
) const
6902 long value
= m_colMinWidths
.Get(col
);
6903 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
6906 int wxGrid::GetRowMinimalHeight(int row
) const
6908 long value
= m_rowMinHeights
.Get(row
);
6909 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
6912 // ----------------------------------------------------------------------------
6914 // ----------------------------------------------------------------------------
6916 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
6918 wxClientDC
dc(m_gridWin
);
6926 wxCoord extent
, extentMax
= 0;
6927 int max
= column
? m_numRows
: m_numCols
;
6928 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
6935 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6936 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6939 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
6940 extent
= column
? size
.x
: size
.y
;
6941 if ( extent
> extentMax
)
6950 // now also compare with the column label extent
6952 dc
.SetFont( GetLabelFont() );
6953 dc
.GetTextExtent( column
? GetColLabelValue(col
)
6954 : GetRowLabelValue(row
), &w
, &h
);
6955 extent
= column
? w
: h
;
6956 if ( extent
> extentMax
)
6963 // empty column - give default extent (notice that if extentMax is less
6964 // than default extent but != 0, it's ok)
6965 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
6969 // leave some space around text
6974 SetColSize(col
, extentMax
);
6976 SetRowSize(row
, extentMax
);
6981 SetColMinimalWidth(col
, extentMax
);
6983 SetRowMinimalHeight(row
, extentMax
);
6987 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
6989 int width
= m_rowLabelWidth
;
6991 for ( int col
= 0; col
< m_numCols
; col
++ )
6995 AutoSizeColumn(col
, setAsMin
);
6998 width
+= GetColWidth(col
);
7004 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7006 int height
= m_colLabelHeight
;
7008 for ( int row
= 0; row
< m_numRows
; row
++ )
7012 AutoSizeRow(row
, setAsMin
);
7015 height
+= GetRowHeight(row
);
7021 void wxGrid::AutoSize()
7024 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
7027 wxSize
wxGrid::DoGetBestSize() const
7029 // don't set sizes, only calculate them
7030 wxGrid
*self
= (wxGrid
*)this; // const_cast
7032 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
7033 self
->SetOrCalcRowSizes(TRUE
));
7041 // ----------------------------------------------------------------------------
7042 // cell value accessor functions
7043 // ----------------------------------------------------------------------------
7045 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
7049 m_table
->SetValue( row
, col
, s
.c_str() );
7050 if ( !GetBatchCount() )
7052 wxClientDC
dc( m_gridWin
);
7054 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7057 if ( m_currentCellCoords
.GetRow() == row
&&
7058 m_currentCellCoords
.GetCol() == col
&&
7059 IsCellEditControlEnabled())
7061 HideCellEditControl();
7062 ShowCellEditControl(); // will reread data from table
7069 // ------ Block, row and col selection
7072 void wxGrid::SelectRow( int row
, bool addToSelected
)
7076 if ( IsSelection() && addToSelected
)
7079 bool need_refresh
[4];
7083 need_refresh
[3] = FALSE
;
7087 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7088 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7089 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7090 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7094 need_refresh
[0] = TRUE
;
7095 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
7096 wxGridCellCoords ( oldTop
- 1,
7098 m_selectedTopLeft
.SetRow( row
);
7103 need_refresh
[1] = TRUE
;
7104 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
7105 wxGridCellCoords ( oldBottom
,
7108 m_selectedTopLeft
.SetCol( 0 );
7111 if ( oldBottom
< row
)
7113 need_refresh
[2] = TRUE
;
7114 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
7115 wxGridCellCoords ( row
,
7117 m_selectedBottomRight
.SetRow( row
);
7120 if ( oldRight
< m_numCols
- 1 )
7122 need_refresh
[3] = TRUE
;
7123 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7125 wxGridCellCoords ( oldBottom
,
7127 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
7130 for (i
= 0; i
< 4; i
++ )
7131 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7132 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7136 r
= SelectionToDeviceRect();
7138 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7140 m_selectedTopLeft
.Set( row
, 0 );
7141 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7142 r
= SelectionToDeviceRect();
7143 m_gridWin
->Refresh( FALSE
, &r
);
7146 wxGridRangeSelectEvent
gridEvt( GetId(),
7147 wxEVT_GRID_RANGE_SELECT
,
7150 m_selectedBottomRight
);
7152 GetEventHandler()->ProcessEvent(gridEvt
);
7156 void wxGrid::SelectCol( int col
, bool addToSelected
)
7158 if ( IsSelection() && addToSelected
)
7161 bool need_refresh
[4];
7165 need_refresh
[3] = FALSE
;
7168 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7169 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7170 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7171 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7173 if ( oldLeft
> col
)
7175 need_refresh
[0] = TRUE
;
7176 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7177 wxGridCellCoords ( m_numRows
- 1,
7179 m_selectedTopLeft
.SetCol( col
);
7184 need_refresh
[1] = TRUE
;
7185 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7186 wxGridCellCoords ( oldTop
- 1,
7188 m_selectedTopLeft
.SetRow( 0 );
7191 if ( oldRight
< col
)
7193 need_refresh
[2] = TRUE
;
7194 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7195 wxGridCellCoords ( m_numRows
- 1,
7197 m_selectedBottomRight
.SetCol( col
);
7200 if ( oldBottom
< m_numRows
- 1 )
7202 need_refresh
[3] = TRUE
;
7203 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7205 wxGridCellCoords ( m_numRows
- 1,
7207 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7210 for (i
= 0; i
< 4; i
++ )
7211 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7212 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7218 r
= SelectionToDeviceRect();
7220 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7222 m_selectedTopLeft
.Set( 0, col
);
7223 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7224 r
= SelectionToDeviceRect();
7225 m_gridWin
->Refresh( FALSE
, &r
);
7228 wxGridRangeSelectEvent
gridEvt( GetId(),
7229 wxEVT_GRID_RANGE_SELECT
,
7232 m_selectedBottomRight
);
7234 GetEventHandler()->ProcessEvent(gridEvt
);
7238 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7241 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7243 if ( topRow
> bottomRow
)
7250 if ( leftCol
> rightCol
)
7257 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7258 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7260 if ( m_selectedTopLeft
!= updateTopLeft
||
7261 m_selectedBottomRight
!= updateBottomRight
)
7263 // Compute two optimal update rectangles:
7264 // Either one rectangle is a real subset of the
7265 // other, or they are (almost) disjoint!
7267 bool need_refresh
[4];
7271 need_refresh
[3] = FALSE
;
7274 // Store intermediate values
7275 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7276 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7277 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7278 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7280 // Determine the outer/inner coordinates.
7281 if (oldLeft
> leftCol
)
7287 if (oldTop
> topRow
)
7293 if (oldRight
< rightCol
)
7296 oldRight
= rightCol
;
7299 if (oldBottom
< bottomRow
)
7302 oldBottom
= bottomRow
;
7306 // Now, either the stuff marked old is the outer
7307 // rectangle or we don't have a situation where one
7308 // is contained in the other.
7310 if ( oldLeft
< leftCol
)
7312 need_refresh
[0] = TRUE
;
7313 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7315 wxGridCellCoords ( oldBottom
,
7319 if ( oldTop
< topRow
)
7321 need_refresh
[1] = TRUE
;
7322 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7324 wxGridCellCoords ( topRow
- 1,
7328 if ( oldRight
> rightCol
)
7330 need_refresh
[2] = TRUE
;
7331 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7333 wxGridCellCoords ( oldBottom
,
7337 if ( oldBottom
> bottomRow
)
7339 need_refresh
[3] = TRUE
;
7340 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7342 wxGridCellCoords ( oldBottom
,
7348 m_selectedTopLeft
= updateTopLeft
;
7349 m_selectedBottomRight
= updateBottomRight
;
7351 // various Refresh() calls
7352 for (i
= 0; i
< 4; i
++ )
7353 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7354 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7357 // only generate an event if the block is not being selected by
7358 // dragging the mouse (in which case the event will be generated in
7359 // the mouse event handler)
7360 if ( !m_isDragging
)
7362 wxGridRangeSelectEvent
gridEvt( GetId(),
7363 wxEVT_GRID_RANGE_SELECT
,
7366 m_selectedBottomRight
);
7368 GetEventHandler()->ProcessEvent(gridEvt
);
7372 void wxGrid::SelectAll()
7374 m_selectedTopLeft
.Set( 0, 0 );
7375 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7377 m_gridWin
->Refresh();
7381 void wxGrid::ClearSelection()
7383 m_selectedTopLeft
= wxGridNoCellCoords
;
7384 m_selectedBottomRight
= wxGridNoCellCoords
;
7388 // This function returns the rectangle that encloses the given block
7389 // in device coords clipped to the client size of the grid window.
7391 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7392 const wxGridCellCoords
&bottomRight
)
7394 wxRect
rect( wxGridNoCellRect
);
7397 cellRect
= CellToRect( topLeft
);
7398 if ( cellRect
!= wxGridNoCellRect
)
7404 rect
= wxRect( 0, 0, 0, 0 );
7407 cellRect
= CellToRect( bottomRight
);
7408 if ( cellRect
!= wxGridNoCellRect
)
7414 return wxGridNoCellRect
;
7417 // convert to scrolled coords
7419 int left
, top
, right
, bottom
;
7420 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7421 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7424 m_gridWin
->GetClientSize( &cw
, &ch
);
7426 rect
.SetLeft( wxMax(0, left
) );
7427 rect
.SetTop( wxMax(0, top
) );
7428 rect
.SetRight( wxMin(cw
, right
) );
7429 rect
.SetBottom( wxMin(ch
, bottom
) );
7437 // ------ Grid event classes
7440 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7442 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7443 int row
, int col
, int x
, int y
,
7444 bool control
, bool shift
, bool alt
, bool meta
)
7445 : wxNotifyEvent( type
, id
)
7451 m_control
= control
;
7456 SetEventObject(obj
);
7460 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7462 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7463 int rowOrCol
, int x
, int y
,
7464 bool control
, bool shift
, bool alt
, bool meta
)
7465 : wxNotifyEvent( type
, id
)
7467 m_rowOrCol
= rowOrCol
;
7470 m_control
= control
;
7475 SetEventObject(obj
);
7479 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7481 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7482 const wxGridCellCoords
& topLeft
,
7483 const wxGridCellCoords
& bottomRight
,
7484 bool control
, bool shift
, bool alt
, bool meta
)
7485 : wxNotifyEvent( type
, id
)
7487 m_topLeft
= topLeft
;
7488 m_bottomRight
= bottomRight
;
7489 m_control
= control
;
7494 SetEventObject(obj
);
7498 #endif // ifndef wxUSE_NEW_GRID