1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
59 struct wxGridCellWithAttr
61 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
62 : coords(row
, col
), attr(attr_
)
71 wxGridCellCoords coords
;
75 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
77 #include "wx/arrimpl.cpp"
79 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
80 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
89 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
90 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
91 const wxPoint
&pos
, const wxSize
&size
);
96 void OnPaint( wxPaintEvent
& event
);
97 void OnMouseEvent( wxMouseEvent
& event
);
98 void OnKeyDown( wxKeyEvent
& event
);
100 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
108 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
109 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
110 const wxPoint
&pos
, const wxSize
&size
);
115 void OnPaint( wxPaintEvent
&event
);
116 void OnMouseEvent( wxMouseEvent
& event
);
117 void OnKeyDown( wxKeyEvent
& event
);
119 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
120 DECLARE_EVENT_TABLE()
124 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
127 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
128 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnKeyDown( wxKeyEvent
& event
);
136 void OnPaint( wxPaintEvent
& event
);
138 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
139 DECLARE_EVENT_TABLE()
142 class WXDLLEXPORT wxGridWindow
: public wxPanel
147 m_owner
= (wxGrid
*)NULL
;
148 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
149 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
152 wxGridWindow( wxGrid
*parent
,
153 wxGridRowLabelWindow
*rowLblWin
,
154 wxGridColLabelWindow
*colLblWin
,
155 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
158 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
162 wxGridRowLabelWindow
*m_rowLabelWin
;
163 wxGridColLabelWindow
*m_colLabelWin
;
165 void OnPaint( wxPaintEvent
&event
);
166 void OnMouseEvent( wxMouseEvent
& event
);
167 void OnKeyDown( wxKeyEvent
& );
168 void OnEraseBackground( wxEraseEvent
& );
171 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
172 DECLARE_EVENT_TABLE()
177 class wxGridCellEditorEvtHandler
: public wxEvtHandler
180 wxGridCellEditorEvtHandler()
181 : m_grid(0), m_editor(0)
183 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
184 : m_grid(grid
), m_editor(editor
)
187 void OnKeyDown(wxKeyEvent
& event
);
188 void OnChar(wxKeyEvent
& event
);
192 wxGridCellEditor
* m_editor
;
193 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
194 DECLARE_EVENT_TABLE()
198 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
199 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
200 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
201 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
206 // ----------------------------------------------------------------------------
207 // the internal data representation used by wxGridCellAttrProvider
208 // ----------------------------------------------------------------------------
210 // this class stores attributes set for cells
211 class WXDLLEXPORT wxGridCellAttrData
214 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
215 wxGridCellAttr
*GetAttr(int row
, int col
) const;
216 void UpdateAttrRows( size_t pos
, int numRows
);
217 void UpdateAttrCols( size_t pos
, int numCols
);
220 // searches for the attr for given cell, returns wxNOT_FOUND if not found
221 int FindIndex(int row
, int col
) const;
223 wxGridCellWithAttrArray m_attrs
;
226 // this class stores attributes set for rows or columns
227 class WXDLLEXPORT wxGridRowOrColAttrData
230 // empty ctor to suppress warnings
231 wxGridRowOrColAttrData() { }
232 ~wxGridRowOrColAttrData();
234 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
235 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
236 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
239 wxArrayInt m_rowsOrCols
;
240 wxArrayAttrs m_attrs
;
243 // NB: this is just a wrapper around 3 objects: one which stores cell
244 // attributes, and 2 others for row/col ones
245 class WXDLLEXPORT wxGridCellAttrProviderData
248 wxGridCellAttrData m_cellAttrs
;
249 wxGridRowOrColAttrData m_rowAttrs
,
254 // ----------------------------------------------------------------------------
255 // data structures used for the data type registry
256 // ----------------------------------------------------------------------------
258 struct wxGridDataTypeInfo
260 wxGridDataTypeInfo(const wxString
& typeName
,
261 wxGridCellRenderer
* renderer
,
262 wxGridCellEditor
* editor
)
263 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
266 ~wxGridDataTypeInfo() { delete m_renderer
; delete m_editor
; }
269 wxGridCellRenderer
* m_renderer
;
270 wxGridCellEditor
* m_editor
;
274 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
277 class WXDLLEXPORT wxGridTypeRegistry
280 ~wxGridTypeRegistry();
282 void RegisterDataType(const wxString
& typeName
,
283 wxGridCellRenderer
* renderer
,
284 wxGridCellEditor
* editor
);
285 int FindDataType(const wxString
& typeName
);
286 wxGridCellRenderer
* GetRenderer(int index
);
287 wxGridCellEditor
* GetEditor(int index
);
290 wxGridDataTypeInfoArray m_typeinfo
;
293 // ----------------------------------------------------------------------------
294 // conditional compilation
295 // ----------------------------------------------------------------------------
297 #ifndef WXGRID_DRAW_LINES
298 #define WXGRID_DRAW_LINES 1
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 //#define DEBUG_ATTR_CACHE
306 #ifdef DEBUG_ATTR_CACHE
307 static size_t gs_nAttrCacheHits
= 0;
308 static size_t gs_nAttrCacheMisses
= 0;
309 #endif // DEBUG_ATTR_CACHE
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
316 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
319 // TODO: fixed so far - make configurable later (and also different for x/y)
320 static const size_t GRID_SCROLL_LINE
= 10;
322 // the size of hash tables used a bit everywhere (the max number of elements
323 // in these hash tables is the number of rows/columns)
324 static const int GRID_HASH_SIZE
= 100;
326 // ============================================================================
328 // ============================================================================
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 wxGridCellEditor::wxGridCellEditor()
340 wxGridCellEditor::~wxGridCellEditor()
345 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
346 wxWindowID
WXUNUSED(id
),
347 wxEvtHandler
* evtHandler
)
350 m_control
->PushEventHandler(evtHandler
);
353 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
354 wxGridCellAttr
*attr
)
356 // erase the background because we might not fill the cell
357 wxClientDC
dc(m_control
->GetParent());
358 dc
.SetPen(*wxTRANSPARENT_PEN
);
359 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
360 dc
.DrawRectangle(rectCell
);
362 // redraw the control we just painted over
363 m_control
->Refresh();
366 void wxGridCellEditor::Destroy()
370 m_control
->PopEventHandler(TRUE
/* delete it*/);
372 m_control
->Destroy();
377 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
379 wxASSERT_MSG(m_control
,
380 wxT("The wxGridCellEditor must be Created first!"));
381 m_control
->Show(show
);
385 // set the colours/fonts if we have any
388 m_colFgOld
= m_control
->GetForegroundColour();
389 m_control
->SetForegroundColour(attr
->GetTextColour());
391 m_colBgOld
= m_control
->GetBackgroundColour();
392 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
394 m_fontOld
= m_control
->GetFont();
395 m_control
->SetFont(attr
->GetFont());
397 // can't do anything more in the base class version, the other
398 // attributes may only be used by the derived classes
403 // restore the standard colours fonts
404 if ( m_colFgOld
.Ok() )
406 m_control
->SetForegroundColour(m_colFgOld
);
407 m_colFgOld
= wxNullColour
;
410 if ( m_colBgOld
.Ok() )
412 m_control
->SetBackgroundColour(m_colBgOld
);
413 m_colBgOld
= wxNullColour
;
416 if ( m_fontOld
.Ok() )
418 m_control
->SetFont(m_fontOld
);
419 m_fontOld
= wxNullFont
;
424 void wxGridCellEditor::SetSize(const wxRect
& rect
)
426 wxASSERT_MSG(m_control
,
427 wxT("The wxGridCellEditor must be Created first!"));
428 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
431 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
437 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
442 void wxGridCellEditor::StartingClick()
446 // ----------------------------------------------------------------------------
447 // wxGridCellTextEditor
448 // ----------------------------------------------------------------------------
450 wxGridCellTextEditor::wxGridCellTextEditor()
454 void wxGridCellTextEditor::Create(wxWindow
* parent
,
456 wxEvtHandler
* evtHandler
)
458 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
459 wxDefaultPosition
, wxDefaultSize
460 #if defined(__WXMSW__)
461 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
465 wxGridCellEditor::Create(parent
, id
, evtHandler
);
468 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
469 wxGridCellAttr
* WXUNUSED(attr
))
471 // as we fill the entire client area, don't do anything here to minimize
475 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
477 wxRect
rect(rectOrig
);
479 // Make the edit control large enough to allow for internal
482 // TODO: remove this if the text ctrl sizing is improved esp. for
485 #if defined(__WXGTK__)
494 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
495 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
496 #if defined(__WXMOTIF__)
500 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
501 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
502 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
503 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
506 wxGridCellEditor::SetSize(rect
);
509 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
511 wxASSERT_MSG(m_control
,
512 wxT("The wxGridCellEditor must be Created first!"));
514 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
516 DoBeginEdit(m_startValue
);
519 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
521 Text()->SetValue(startValue
);
522 Text()->SetInsertionPointEnd();
526 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
529 wxASSERT_MSG(m_control
,
530 wxT("The wxGridCellEditor must be Created first!"));
532 bool changed
= FALSE
;
533 wxString value
= Text()->GetValue();
534 if (value
!= m_startValue
)
538 grid
->GetTable()->SetValue(row
, col
, value
);
540 m_startValue
= wxEmptyString
;
541 Text()->SetValue(m_startValue
);
547 void wxGridCellTextEditor::Reset()
549 wxASSERT_MSG(m_control
,
550 wxT("The wxGridCellEditor must be Created first!"));
552 DoReset(m_startValue
);
555 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
557 Text()->SetValue(startValue
);
558 Text()->SetInsertionPointEnd();
561 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
563 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
565 // insert the key in the control
566 long keycode
= event
.KeyCode();
567 if ( isprint(keycode
) )
569 // FIXME this is not going to work for non letters...
570 if ( !event
.ShiftDown() )
572 keycode
= tolower(keycode
);
575 Text()->AppendText((wxChar
)keycode
);
585 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
587 #if defined(__WXMOTIF__) || defined(__WXGTK__)
588 // wxMotif needs a little extra help...
589 int pos
= Text()->GetInsertionPoint();
590 wxString
s( Text()->GetValue() );
591 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
593 Text()->SetInsertionPoint( pos
);
595 // the other ports can handle a Return key press
601 // ----------------------------------------------------------------------------
602 // wxGridCellNumberEditor
603 // ----------------------------------------------------------------------------
605 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
611 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
613 wxEvtHandler
* evtHandler
)
617 // create a spin ctrl
618 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
619 wxDefaultPosition
, wxDefaultSize
,
623 wxGridCellEditor::Create(parent
, id
, evtHandler
);
627 // just a text control
628 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
631 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
632 #endif // wxUSE_VALIDATORS
636 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
638 // first get the value
639 wxGridTableBase
*table
= grid
->GetTable();
640 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
642 m_valueOld
= table
->GetValueAsLong(row
, col
);
646 wxString sValue
= table
->GetValue(row
, col
);
647 if (! sValue
.ToLong(&m_valueOld
))
649 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
656 Spin()->SetValue(m_valueOld
);
660 DoBeginEdit(GetString());
664 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
672 value
= Spin()->GetValue();
673 changed
= value
!= m_valueOld
;
677 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
682 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
683 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
685 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%ld", value
));
691 void wxGridCellNumberEditor::Reset()
695 Spin()->SetValue(m_valueOld
);
699 DoReset(GetString());
703 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
707 long keycode
= event
.KeyCode();
708 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
710 wxGridCellTextEditor::StartingKey(event
);
720 // ----------------------------------------------------------------------------
721 // wxGridCellFloatEditor
722 // ----------------------------------------------------------------------------
724 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
726 wxEvtHandler
* evtHandler
)
728 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
731 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
732 #endif // wxUSE_VALIDATORS
735 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
737 // first get the value
738 wxGridTableBase
*table
= grid
->GetTable();
739 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
741 m_valueOld
= table
->GetValueAsDouble(row
, col
);
745 wxString sValue
= table
->GetValue(row
, col
);
746 if (! sValue
.ToDouble(&m_valueOld
))
748 wxFAIL_MSG( _T("this cell doesn't have float value") );
753 DoBeginEdit(GetString());
756 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
760 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
762 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
763 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
765 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%f", value
));
775 void wxGridCellFloatEditor::Reset()
777 DoReset(GetString());
780 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
782 long keycode
= event
.KeyCode();
783 if ( isdigit(keycode
) ||
784 keycode
== '+' || keycode
== '-' || keycode
== '.' )
786 wxGridCellTextEditor::StartingKey(event
);
795 // ----------------------------------------------------------------------------
796 // wxGridCellBoolEditor
797 // ----------------------------------------------------------------------------
799 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
801 wxEvtHandler
* evtHandler
)
803 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
804 wxDefaultPosition
, wxDefaultSize
,
807 wxGridCellEditor::Create(parent
, id
, evtHandler
);
810 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
813 wxSize size
= m_control
->GetSize();
814 wxCoord minSize
= wxMin(r
.width
, r
.height
);
816 // check if the checkbox is not too big/small for this cell
817 wxSize sizeBest
= m_control
->GetBestSize();
818 if ( !(size
== sizeBest
) )
820 // reset to default size if it had been made smaller
826 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
828 // leave 1 pixel margin
829 size
.x
= size
.y
= minSize
- 2;
836 m_control
->SetSize(size
);
839 // position it in the centre of the rectangle (TODO: support alignment?)
841 #if defined(__WXGTK__) || defined (__WXMOTIF__)
842 // the checkbox without label still has some space to the right in wxGTK,
843 // so shift it to the right
845 #elif defined(__WXMSW__)
851 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
854 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
856 m_control
->Show(show
);
860 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
861 CBox()->SetBackgroundColour(colBg
);
865 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
867 wxASSERT_MSG(m_control
,
868 wxT("The wxGridCellEditor must be Created first!"));
870 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
871 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
873 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
874 CBox()->SetValue(m_startValue
);
878 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
881 wxASSERT_MSG(m_control
,
882 wxT("The wxGridCellEditor must be Created first!"));
884 bool changed
= FALSE
;
885 bool value
= CBox()->GetValue();
886 if ( value
!= m_startValue
)
891 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
892 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
894 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
900 void wxGridCellBoolEditor::Reset()
902 wxASSERT_MSG(m_control
,
903 wxT("The wxGridCellEditor must be Created first!"));
905 CBox()->SetValue(m_startValue
);
908 void wxGridCellBoolEditor::StartingClick()
910 CBox()->SetValue(!CBox()->GetValue());
913 // ----------------------------------------------------------------------------
914 // wxGridCellChoiceEditor
915 // ----------------------------------------------------------------------------
917 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
918 const wxChar
* choices
[],
920 : m_allowOthers(allowOthers
)
922 m_choices
.Alloc(count
);
923 for ( size_t n
= 0; n
< count
; n
++ )
925 m_choices
.Add(choices
[n
]);
929 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
931 wxEvtHandler
* evtHandler
)
933 size_t count
= m_choices
.GetCount();
934 wxString
*choices
= new wxString
[count
];
935 for ( size_t n
= 0; n
< count
; n
++ )
937 choices
[n
] = m_choices
[n
];
940 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
941 wxDefaultPosition
, wxDefaultSize
,
943 m_allowOthers
? 0 : wxCB_READONLY
);
947 wxGridCellEditor::Create(parent
, id
, evtHandler
);
950 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
951 wxGridCellAttr
* attr
)
953 // as we fill the entire client area, don't do anything here to minimize
956 // TODO: It doesn't actually fill the client area since the height of a
957 // combo always defaults to the standard... Until someone has time to
958 // figure out the right rectangle to paint, just do it the normal way...
959 wxGridCellEditor::PaintBackground(rectCell
, attr
);
962 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
964 wxASSERT_MSG(m_control
,
965 wxT("The wxGridCellEditor must be Created first!"));
967 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
969 Combo()->SetValue(m_startValue
);
970 size_t count
= m_choices
.GetCount();
971 for (size_t i
=0; i
<count
; i
++)
973 if (m_startValue
== m_choices
[i
])
975 Combo()->SetSelection(i
);
979 Combo()->SetInsertionPointEnd();
983 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
986 wxString value
= Combo()->GetValue();
987 bool changed
= value
!= m_startValue
;
990 grid
->GetTable()->SetValue(row
, col
, value
);
992 m_startValue
= wxEmptyString
;
993 Combo()->SetValue(m_startValue
);
998 void wxGridCellChoiceEditor::Reset()
1000 Combo()->SetValue(m_startValue
);
1001 Combo()->SetInsertionPointEnd();
1004 // ----------------------------------------------------------------------------
1005 // wxGridCellEditorEvtHandler
1006 // ----------------------------------------------------------------------------
1008 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1010 switch ( event
.KeyCode() )
1014 m_grid
->DisableCellEditControl();
1018 event
.Skip( m_grid
->ProcessEvent( event
) );
1022 if (!m_grid
->ProcessEvent(event
))
1023 m_editor
->HandleReturn(event
);
1032 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1034 switch ( event
.KeyCode() )
1046 // ============================================================================
1048 // ============================================================================
1050 // ----------------------------------------------------------------------------
1051 // wxGridCellRenderer
1052 // ----------------------------------------------------------------------------
1054 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1055 wxGridCellAttr
& attr
,
1061 dc
.SetBackgroundMode( wxSOLID
);
1065 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1069 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1072 dc
.SetPen( *wxTRANSPARENT_PEN
);
1073 dc
.DrawRectangle(rect
);
1076 wxGridCellRenderer::~wxGridCellRenderer()
1080 // ----------------------------------------------------------------------------
1081 // wxGridCellStringRenderer
1082 // ----------------------------------------------------------------------------
1084 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1085 wxGridCellAttr
& attr
,
1089 dc
.SetBackgroundMode( wxTRANSPARENT
);
1091 // TODO some special colours for attr.IsReadOnly() case?
1095 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1096 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1100 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1101 dc
.SetTextForeground( attr
.GetTextColour() );
1104 dc
.SetFont( attr
.GetFont() );
1107 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1109 const wxString
& text
)
1112 dc
.SetFont(attr
.GetFont());
1113 dc
.GetTextExtent(text
, &x
, &y
);
1115 return wxSize(x
, y
);
1118 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1119 wxGridCellAttr
& attr
,
1123 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1126 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1127 wxGridCellAttr
& attr
,
1129 const wxRect
& rectCell
,
1133 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1135 // now we only have to draw the text
1136 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1139 attr
.GetAlignment(&hAlign
, &vAlign
);
1141 wxRect rect
= rectCell
;
1144 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1145 rect
, hAlign
, vAlign
);
1148 // ----------------------------------------------------------------------------
1149 // wxGridCellNumberRenderer
1150 // ----------------------------------------------------------------------------
1152 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1154 wxGridTableBase
*table
= grid
.GetTable();
1156 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1158 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1162 text
= table
->GetValue(row
, col
);
1168 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1169 wxGridCellAttr
& attr
,
1171 const wxRect
& rectCell
,
1175 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1177 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1179 // draw the text right aligned by default
1181 attr
.GetAlignment(&hAlign
, &vAlign
);
1184 wxRect rect
= rectCell
;
1187 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1190 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1191 wxGridCellAttr
& attr
,
1195 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1198 // ----------------------------------------------------------------------------
1199 // wxGridCellFloatRenderer
1200 // ----------------------------------------------------------------------------
1202 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1205 SetPrecision(precision
);
1208 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1210 wxGridTableBase
*table
= grid
.GetTable();
1212 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1216 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1219 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1223 text
= table
->GetValue(row
, col
);
1229 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1230 wxGridCellAttr
& attr
,
1232 const wxRect
& rectCell
,
1236 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1238 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1240 // draw the text right aligned by default
1242 attr
.GetAlignment(&hAlign
, &vAlign
);
1245 wxRect rect
= rectCell
;
1248 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1251 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1252 wxGridCellAttr
& attr
,
1256 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1259 // ----------------------------------------------------------------------------
1260 // wxGridCellBoolRenderer
1261 // ----------------------------------------------------------------------------
1263 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1265 // FIXME these checkbox size calculations are really ugly...
1267 // between checkmark and box
1269 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1271 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1274 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1275 wxGridCellAttr
& WXUNUSED(attr
),
1280 // compute it only once (no locks for MT safeness in GUI thread...)
1281 if ( !ms_sizeCheckMark
.x
)
1283 // get checkbox size
1284 wxCoord checkSize
= 0;
1285 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1286 wxSize size
= checkbox
->GetBestSize();
1287 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1289 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1290 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1291 checkSize
-= size
.y
/ 2;
1296 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1299 return ms_sizeCheckMark
;
1302 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1303 wxGridCellAttr
& attr
,
1309 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1311 // draw a check mark in the centre (ignoring alignment - TODO)
1312 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1314 // don't draw outside the cell
1315 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1316 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1318 // and even leave (at least) 1 pixel margin
1319 size
.x
= size
.y
= minSize
- 2;
1322 // draw a border around checkmark
1324 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1325 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1326 rectMark
.width
= size
.x
;
1327 rectMark
.height
= size
.y
;
1329 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1330 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1331 dc
.DrawRectangle(rectMark
);
1333 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1336 // looks nicer under MSW
1341 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1342 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1344 value
= !!grid
.GetTable()->GetValue(row
, col
);
1348 dc
.SetTextForeground(attr
.GetTextColour());
1349 dc
.DrawCheckMark(rectMark
);
1353 // ----------------------------------------------------------------------------
1355 // ----------------------------------------------------------------------------
1357 wxGridCellAttr
*wxGridCellAttr::Clone()
1359 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1360 if ( HasTextColour() )
1361 attr
->SetTextColour(GetTextColour());
1362 if ( HasBackgroundColour() )
1363 attr
->SetBackgroundColour(GetBackgroundColour());
1365 attr
->SetFont(GetFont());
1366 if ( HasAlignment() )
1367 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1371 attr
->SetRenderer(m_renderer
);
1376 attr
->SetEditor(m_editor
);
1381 attr
->SetReadOnly();
1383 attr
->SetDefAttr(m_defGridAttr
);
1388 const wxColour
& wxGridCellAttr::GetTextColour() const
1390 if (HasTextColour())
1394 else if (m_defGridAttr
!= this)
1396 return m_defGridAttr
->GetTextColour();
1400 wxFAIL_MSG(wxT("Missing default cell attribute"));
1401 return wxNullColour
;
1406 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1408 if (HasBackgroundColour())
1410 else if (m_defGridAttr
!= this)
1411 return m_defGridAttr
->GetBackgroundColour();
1414 wxFAIL_MSG(wxT("Missing default cell attribute"));
1415 return wxNullColour
;
1420 const wxFont
& wxGridCellAttr::GetFont() const
1424 else if (m_defGridAttr
!= this)
1425 return m_defGridAttr
->GetFont();
1428 wxFAIL_MSG(wxT("Missing default cell attribute"));
1434 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1438 if ( hAlign
) *hAlign
= m_hAlign
;
1439 if ( vAlign
) *vAlign
= m_vAlign
;
1441 else if (m_defGridAttr
!= this)
1442 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1445 wxFAIL_MSG(wxT("Missing default cell attribute"));
1450 // GetRenderer and GetEditor use a slightly different decision path about
1451 // which attribute to use. If a non-default attr object has one then it is
1452 // used, otherwise the default editor or renderer is fetched from the grid and
1453 // used. It should be the default for the data type of the cell. If it is
1454 // NULL (because the table has a type that the grid does not have in its
1455 // registry,) then the grid's default editor or renderer is used.
1457 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1459 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1460 return m_renderer
; // use local attribute
1462 wxGridCellRenderer
* renderer
= NULL
;
1463 if (grid
) // get renderer for the data type
1464 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1467 // if we still don't have one then use the grid default
1468 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1471 wxFAIL_MSG(wxT("Missing default cell attribute"));
1476 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1478 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1479 return m_editor
; // use local attribute
1481 wxGridCellEditor
* editor
= NULL
;
1482 if (grid
) // get renderer for the data type
1483 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1486 // if we still don't have one then use the grid default
1487 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1490 wxFAIL_MSG(wxT("Missing default cell attribute"));
1495 // ----------------------------------------------------------------------------
1496 // wxGridCellAttrData
1497 // ----------------------------------------------------------------------------
1499 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1501 int n
= FindIndex(row
, col
);
1502 if ( n
== wxNOT_FOUND
)
1504 // add the attribute
1505 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1511 // change the attribute
1512 m_attrs
[(size_t)n
].attr
= attr
;
1516 // remove this attribute
1517 m_attrs
.RemoveAt((size_t)n
);
1522 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1524 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1526 int n
= FindIndex(row
, col
);
1527 if ( n
!= wxNOT_FOUND
)
1529 attr
= m_attrs
[(size_t)n
].attr
;
1536 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1538 size_t count
= m_attrs
.GetCount();
1539 for ( size_t n
= 0; n
< count
; n
++ )
1541 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1542 wxCoord row
= coords
.GetRow();
1543 if ((size_t)row
>= pos
)
1547 // If rows inserted, include row counter where necessary
1548 coords
.SetRow(row
+ numRows
);
1550 else if (numRows
< 0)
1552 // If rows deleted ...
1553 if ((size_t)row
>= pos
- numRows
)
1555 // ...either decrement row counter (if row still exists)...
1556 coords
.SetRow(row
+ numRows
);
1560 // ...or remove the attribute
1561 m_attrs
.RemoveAt((size_t)n
);
1569 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1571 size_t count
= m_attrs
.GetCount();
1572 for ( size_t n
= 0; n
< count
; n
++ )
1574 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1575 wxCoord col
= coords
.GetCol();
1576 if ( (size_t)col
>= pos
)
1580 // If rows inserted, include row counter where necessary
1581 coords
.SetCol(col
+ numCols
);
1583 else if (numCols
< 0)
1585 // If rows deleted ...
1586 if ((size_t)col
>= pos
- numCols
)
1588 // ...either decrement row counter (if row still exists)...
1589 coords
.SetCol(col
+ numCols
);
1593 // ...or remove the attribute
1594 m_attrs
.RemoveAt((size_t)n
);
1602 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1604 size_t count
= m_attrs
.GetCount();
1605 for ( size_t n
= 0; n
< count
; n
++ )
1607 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1608 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1617 // ----------------------------------------------------------------------------
1618 // wxGridRowOrColAttrData
1619 // ----------------------------------------------------------------------------
1621 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1623 size_t count
= m_attrs
.Count();
1624 for ( size_t n
= 0; n
< count
; n
++ )
1626 m_attrs
[n
]->DecRef();
1630 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1632 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1634 int n
= m_rowsOrCols
.Index(rowOrCol
);
1635 if ( n
!= wxNOT_FOUND
)
1637 attr
= m_attrs
[(size_t)n
];
1644 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1646 int n
= m_rowsOrCols
.Index(rowOrCol
);
1647 if ( n
== wxNOT_FOUND
)
1649 // add the attribute
1650 m_rowsOrCols
.Add(rowOrCol
);
1657 // change the attribute
1658 m_attrs
[(size_t)n
] = attr
;
1662 // remove this attribute
1663 m_attrs
[(size_t)n
]->DecRef();
1664 m_rowsOrCols
.RemoveAt((size_t)n
);
1665 m_attrs
.RemoveAt((size_t)n
);
1670 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1672 size_t count
= m_attrs
.GetCount();
1673 for ( size_t n
= 0; n
< count
; n
++ )
1675 int & rowOrCol
= m_rowsOrCols
[n
];
1676 if ( (size_t)rowOrCol
>= pos
)
1678 if ( numRowsOrCols
> 0 )
1680 // If rows inserted, include row counter where necessary
1681 rowOrCol
+= numRowsOrCols
;
1683 else if ( numRowsOrCols
< 0)
1685 // If rows deleted, either decrement row counter (if row still exists)
1686 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1687 rowOrCol
+= numRowsOrCols
;
1690 m_rowsOrCols
.RemoveAt((size_t)n
);
1691 m_attrs
.RemoveAt((size_t)n
);
1699 // ----------------------------------------------------------------------------
1700 // wxGridCellAttrProvider
1701 // ----------------------------------------------------------------------------
1703 wxGridCellAttrProvider::wxGridCellAttrProvider()
1705 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1708 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1713 void wxGridCellAttrProvider::InitData()
1715 m_data
= new wxGridCellAttrProviderData
;
1718 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1720 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1723 // first look for the attribute of this specific cell
1724 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1728 // then look for the col attr (col attributes are more common than
1729 // the row ones, hence they have priority)
1730 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1735 // finally try the row attributes
1736 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1743 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1749 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1752 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1757 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1760 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1765 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1768 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1772 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1774 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1778 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1782 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1784 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1788 // ----------------------------------------------------------------------------
1789 // wxGridTypeRegistry
1790 // ----------------------------------------------------------------------------
1792 wxGridTypeRegistry::~wxGridTypeRegistry()
1794 size_t count
= m_typeinfo
.Count();
1795 for ( size_t i
= 0; i
< count
; i
++ )
1796 delete m_typeinfo
[i
];
1800 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1801 wxGridCellRenderer
* renderer
,
1802 wxGridCellEditor
* editor
)
1805 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1807 // is it already registered?
1808 if ((loc
= FindDataType(typeName
)) != -1) {
1809 delete m_typeinfo
[loc
];
1810 m_typeinfo
[loc
] = info
;
1813 m_typeinfo
.Add(info
);
1817 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1821 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1822 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1831 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1833 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1837 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1839 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1843 // ----------------------------------------------------------------------------
1845 // ----------------------------------------------------------------------------
1847 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1850 wxGridTableBase::wxGridTableBase()
1852 m_view
= (wxGrid
*) NULL
;
1853 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1856 wxGridTableBase::~wxGridTableBase()
1858 delete m_attrProvider
;
1861 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1863 delete m_attrProvider
;
1864 m_attrProvider
= attrProvider
;
1867 bool wxGridTableBase::CanHaveAttributes()
1869 if ( ! GetAttrProvider() )
1871 // use the default attr provider by default
1872 SetAttrProvider(new wxGridCellAttrProvider
);
1877 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1879 if ( m_attrProvider
)
1880 return m_attrProvider
->GetAttr(row
, col
);
1882 return (wxGridCellAttr
*)NULL
;
1885 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1887 if ( m_attrProvider
)
1889 m_attrProvider
->SetAttr(attr
, row
, col
);
1893 // as we take ownership of the pointer and don't store it, we must
1899 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1901 if ( m_attrProvider
)
1903 m_attrProvider
->SetRowAttr(attr
, row
);
1907 // as we take ownership of the pointer and don't store it, we must
1913 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1915 if ( m_attrProvider
)
1917 m_attrProvider
->SetColAttr(attr
, col
);
1921 // as we take ownership of the pointer and don't store it, we must
1927 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1929 if ( m_attrProvider
)
1931 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1935 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1937 if ( m_attrProvider
)
1939 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1943 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1945 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1946 "but your derived table class does not override this function") );
1951 bool wxGridTableBase::AppendRows( size_t numRows
)
1953 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1954 "but your derived table class does not override this function"));
1959 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1961 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1962 "but your derived table class does not override this function"));
1967 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1969 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1970 "but your derived table class does not override this function"));
1975 bool wxGridTableBase::AppendCols( size_t numCols
)
1977 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1978 "but your derived table class does not override this function"));
1983 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1985 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1986 "but your derived table class does not override this function"));
1992 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1995 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1996 // how much it makes sense to us geeks.
2000 wxString
wxGridTableBase::GetColLabelValue( int col
)
2002 // default col labels are:
2003 // cols 0 to 25 : A-Z
2004 // cols 26 to 675 : AA-ZZ
2009 for ( n
= 1; ; n
++ )
2011 s
+= (_T('A') + (wxChar
)( col%26
));
2013 if ( col
< 0 ) break;
2016 // reverse the string...
2018 for ( i
= 0; i
< n
; i
++ )
2027 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2029 return wxGRID_VALUE_STRING
;
2032 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2033 const wxString
& typeName
)
2035 return typeName
== wxGRID_VALUE_STRING
;
2038 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2040 return CanGetValueAs(row
, col
, typeName
);
2043 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2048 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2053 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2058 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2059 long WXUNUSED(value
) )
2063 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2064 double WXUNUSED(value
) )
2068 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2069 bool WXUNUSED(value
) )
2074 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2075 const wxString
& WXUNUSED(typeName
) )
2080 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2081 const wxString
& WXUNUSED(typeName
),
2082 void* WXUNUSED(value
) )
2087 //////////////////////////////////////////////////////////////////////
2089 // Message class for the grid table to send requests and notifications
2093 wxGridTableMessage::wxGridTableMessage()
2095 m_table
= (wxGridTableBase
*) NULL
;
2101 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2102 int commandInt1
, int commandInt2
)
2106 m_comInt1
= commandInt1
;
2107 m_comInt2
= commandInt2
;
2112 //////////////////////////////////////////////////////////////////////
2114 // A basic grid table for string data. An object of this class will
2115 // created by wxGrid if you don't specify an alternative table class.
2118 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2120 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2122 wxGridStringTable::wxGridStringTable()
2127 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2132 m_data
.Alloc( numRows
);
2135 sa
.Alloc( numCols
);
2136 for ( col
= 0; col
< numCols
; col
++ )
2138 sa
.Add( wxEmptyString
);
2141 for ( row
= 0; row
< numRows
; row
++ )
2147 wxGridStringTable::~wxGridStringTable()
2151 long wxGridStringTable::GetNumberRows()
2153 return m_data
.GetCount();
2156 long wxGridStringTable::GetNumberCols()
2158 if ( m_data
.GetCount() > 0 )
2159 return m_data
[0].GetCount();
2164 wxString
wxGridStringTable::GetValue( int row
, int col
)
2166 // TODO: bounds checking
2168 return m_data
[row
][col
];
2171 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2173 // TODO: bounds checking
2175 m_data
[row
][col
] = value
;
2178 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2180 // TODO: bounds checking
2182 return (m_data
[row
][col
] == wxEmptyString
);
2186 void wxGridStringTable::Clear()
2189 int numRows
, numCols
;
2191 numRows
= m_data
.GetCount();
2194 numCols
= m_data
[0].GetCount();
2196 for ( row
= 0; row
< numRows
; row
++ )
2198 for ( col
= 0; col
< numCols
; col
++ )
2200 m_data
[row
][col
] = wxEmptyString
;
2207 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2211 size_t curNumRows
= m_data
.GetCount();
2212 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2214 if ( pos
>= curNumRows
)
2216 return AppendRows( numRows
);
2220 sa
.Alloc( curNumCols
);
2221 for ( col
= 0; col
< curNumCols
; col
++ )
2223 sa
.Add( wxEmptyString
);
2226 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2228 m_data
.Insert( sa
, row
);
2230 UpdateAttrRows( pos
, numRows
);
2233 wxGridTableMessage
msg( this,
2234 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2238 GetView()->ProcessTableMessage( msg
);
2244 bool wxGridStringTable::AppendRows( size_t numRows
)
2248 size_t curNumRows
= m_data
.GetCount();
2249 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2252 if ( curNumCols
> 0 )
2254 sa
.Alloc( curNumCols
);
2255 for ( col
= 0; col
< curNumCols
; col
++ )
2257 sa
.Add( wxEmptyString
);
2261 for ( row
= 0; row
< numRows
; row
++ )
2268 wxGridTableMessage
msg( this,
2269 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2272 GetView()->ProcessTableMessage( msg
);
2278 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2282 size_t curNumRows
= m_data
.GetCount();
2284 if ( pos
>= curNumRows
)
2287 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2288 "Pos value is invalid for present table with %d rows",
2289 pos
, numRows
, curNumRows
);
2290 wxFAIL_MSG( wxT(errmsg
) );
2294 if ( numRows
> curNumRows
- pos
)
2296 numRows
= curNumRows
- pos
;
2299 if ( numRows
>= curNumRows
)
2301 m_data
.Empty(); // don't release memory just yet
2305 for ( n
= 0; n
< numRows
; n
++ )
2307 m_data
.Remove( pos
);
2310 UpdateAttrRows( pos
, -((int)numRows
) );
2313 wxGridTableMessage
msg( this,
2314 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2318 GetView()->ProcessTableMessage( msg
);
2324 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2328 size_t curNumRows
= m_data
.GetCount();
2329 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2331 if ( pos
>= curNumCols
)
2333 return AppendCols( numCols
);
2336 for ( row
= 0; row
< curNumRows
; row
++ )
2338 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2340 m_data
[row
].Insert( wxEmptyString
, col
);
2343 UpdateAttrCols( pos
, numCols
);
2346 wxGridTableMessage
msg( this,
2347 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2351 GetView()->ProcessTableMessage( msg
);
2357 bool wxGridStringTable::AppendCols( size_t numCols
)
2361 size_t curNumRows
= m_data
.GetCount();
2364 // TODO: something better than this ?
2366 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2367 "Call AppendRows() first") );
2371 for ( row
= 0; row
< curNumRows
; row
++ )
2373 for ( n
= 0; n
< numCols
; n
++ )
2375 m_data
[row
].Add( wxEmptyString
);
2381 wxGridTableMessage
msg( this,
2382 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2385 GetView()->ProcessTableMessage( msg
);
2391 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2395 size_t curNumRows
= m_data
.GetCount();
2396 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2398 if ( pos
>= curNumCols
)
2401 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2402 "Pos value is invalid for present table with %d cols",
2403 pos
, numCols
, curNumCols
);
2404 wxFAIL_MSG( wxT( errmsg
) );
2408 if ( numCols
> curNumCols
- pos
)
2410 numCols
= curNumCols
- pos
;
2413 for ( row
= 0; row
< curNumRows
; row
++ )
2415 if ( numCols
>= curNumCols
)
2417 m_data
[row
].Clear();
2421 for ( n
= 0; n
< numCols
; n
++ )
2423 m_data
[row
].Remove( pos
);
2427 UpdateAttrCols( pos
, -((int)numCols
) );
2430 wxGridTableMessage
msg( this,
2431 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2435 GetView()->ProcessTableMessage( msg
);
2441 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2443 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2445 // using default label
2447 return wxGridTableBase::GetRowLabelValue( row
);
2451 return m_rowLabels
[ row
];
2455 wxString
wxGridStringTable::GetColLabelValue( int col
)
2457 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2459 // using default label
2461 return wxGridTableBase::GetColLabelValue( col
);
2465 return m_colLabels
[ col
];
2469 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2471 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2473 int n
= m_rowLabels
.GetCount();
2475 for ( i
= n
; i
<= row
; i
++ )
2477 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2481 m_rowLabels
[row
] = value
;
2484 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2486 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2488 int n
= m_colLabels
.GetCount();
2490 for ( i
= n
; i
<= col
; i
++ )
2492 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2496 m_colLabels
[col
] = value
;
2501 //////////////////////////////////////////////////////////////////////
2502 //////////////////////////////////////////////////////////////////////
2504 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2506 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2507 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2508 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2509 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2512 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2514 const wxPoint
&pos
, const wxSize
&size
)
2515 : wxWindow( parent
, id
, pos
, size
)
2520 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2524 // NO - don't do this because it will set both the x and y origin
2525 // coords to match the parent scrolled window and we just want to
2526 // set the y coord - MB
2528 // m_owner->PrepareDC( dc );
2531 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2532 dc
.SetDeviceOrigin( 0, -y
);
2534 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2535 m_owner
->DrawRowLabels( dc
);
2539 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2541 m_owner
->ProcessRowLabelMouseEvent( event
);
2545 // This seems to be required for wxMotif otherwise the mouse
2546 // cursor must be in the cell edit control to get key events
2548 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2550 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2555 //////////////////////////////////////////////////////////////////////
2557 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2559 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2560 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2561 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2562 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2565 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2567 const wxPoint
&pos
, const wxSize
&size
)
2568 : wxWindow( parent
, id
, pos
, size
)
2573 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2577 // NO - don't do this because it will set both the x and y origin
2578 // coords to match the parent scrolled window and we just want to
2579 // set the x coord - MB
2581 // m_owner->PrepareDC( dc );
2584 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2585 dc
.SetDeviceOrigin( -x
, 0 );
2587 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2588 m_owner
->DrawColLabels( dc
);
2592 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2594 m_owner
->ProcessColLabelMouseEvent( event
);
2598 // This seems to be required for wxMotif otherwise the mouse
2599 // cursor must be in the cell edit control to get key events
2601 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2603 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2608 //////////////////////////////////////////////////////////////////////
2610 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2612 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2613 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2614 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2615 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2618 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2620 const wxPoint
&pos
, const wxSize
&size
)
2621 : wxWindow( parent
, id
, pos
, size
)
2626 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2630 int client_height
= 0;
2631 int client_width
= 0;
2632 GetClientSize( &client_width
, &client_height
);
2634 dc
.SetPen( *wxBLACK_PEN
);
2635 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2636 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2638 dc
.SetPen( *wxWHITE_PEN
);
2639 dc
.DrawLine( 0, 0, client_width
, 0 );
2640 dc
.DrawLine( 0, 0, 0, client_height
);
2644 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2646 m_owner
->ProcessCornerLabelMouseEvent( event
);
2650 // This seems to be required for wxMotif otherwise the mouse
2651 // cursor must be in the cell edit control to get key events
2653 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2655 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2660 //////////////////////////////////////////////////////////////////////
2662 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2664 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2665 EVT_PAINT( wxGridWindow::OnPaint
)
2666 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2667 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2668 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2671 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2672 wxGridRowLabelWindow
*rowLblWin
,
2673 wxGridColLabelWindow
*colLblWin
,
2674 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2675 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
2678 m_rowLabelWin
= rowLblWin
;
2679 m_colLabelWin
= colLblWin
;
2680 SetBackgroundColour( "WHITE" );
2684 wxGridWindow::~wxGridWindow()
2689 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2691 wxPaintDC
dc( this );
2692 m_owner
->PrepareDC( dc
);
2693 wxRegion reg
= GetUpdateRegion();
2694 m_owner
->CalcCellsExposed( reg
);
2695 m_owner
->DrawGridCellArea( dc
);
2696 #if WXGRID_DRAW_LINES
2697 m_owner
->DrawAllGridLines( dc
, reg
);
2699 m_owner
->DrawGridSpace( dc
);
2700 m_owner
->DrawHighlight( dc
);
2704 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2706 wxPanel::ScrollWindow( dx
, dy
, rect
);
2707 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2708 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2712 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2714 m_owner
->ProcessGridCellMouseEvent( event
);
2718 // This seems to be required for wxMotif otherwise the mouse
2719 // cursor must be in the cell edit control to get key events
2721 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2723 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2727 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2732 //////////////////////////////////////////////////////////////////////
2735 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2737 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2738 EVT_PAINT( wxGrid::OnPaint
)
2739 EVT_SIZE( wxGrid::OnSize
)
2740 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2741 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2744 wxGrid::wxGrid( wxWindow
*parent
,
2749 const wxString
& name
)
2750 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2751 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2760 m_defaultCellAttr
->SafeDecRef();
2762 #ifdef DEBUG_ATTR_CACHE
2763 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2764 wxPrintf(_T("wxGrid attribute cache statistics: "
2765 "total: %u, hits: %u (%u%%)\n"),
2766 total
, gs_nAttrCacheHits
,
2767 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2773 delete m_typeRegistry
;
2778 // ----- internal init and update functions
2781 void wxGrid::Create()
2783 m_created
= FALSE
; // set to TRUE by CreateGrid
2784 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2786 m_table
= (wxGridTableBase
*) NULL
;
2789 m_cellEditCtrlEnabled
= FALSE
;
2791 m_defaultCellAttr
= new wxGridCellAttr
;
2792 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2794 // Set default cell attributes
2795 m_defaultCellAttr
->SetFont(GetFont());
2796 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2797 m_defaultCellAttr
->SetTextColour(
2798 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2799 m_defaultCellAttr
->SetBackgroundColour(
2800 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2801 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2802 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2807 m_currentCellCoords
= wxGridNoCellCoords
;
2809 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2810 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2812 // data type registration: register all standard data types
2813 // TODO: may be allow the app to selectively disable some of them?
2814 m_typeRegistry
= new wxGridTypeRegistry
;
2815 RegisterDataType(wxGRID_VALUE_STRING
,
2816 new wxGridCellStringRenderer
,
2817 new wxGridCellTextEditor
);
2818 RegisterDataType(wxGRID_VALUE_BOOL
,
2819 new wxGridCellBoolRenderer
,
2820 new wxGridCellBoolEditor
);
2821 RegisterDataType(wxGRID_VALUE_NUMBER
,
2822 new wxGridCellNumberRenderer
,
2823 new wxGridCellNumberEditor
);
2825 // subwindow components that make up the wxGrid
2826 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2831 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2836 m_colLabelWin
= new wxGridColLabelWindow( this,
2841 m_gridWin
= new wxGridWindow( this,
2848 SetTargetWindow( m_gridWin
);
2852 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2856 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2861 m_numRows
= numRows
;
2862 m_numCols
= numCols
;
2864 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2865 m_table
->SetView( this );
2874 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2878 // RD: Actually, this should probably be allowed. I think it would be
2879 // nice to be able to switch multiple Tables in and out of a single
2880 // View at runtime. Is there anything in the implmentation that would
2883 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2888 m_numRows
= table
->GetNumberRows();
2889 m_numCols
= table
->GetNumberCols();
2892 m_table
->SetView( this );
2905 if ( m_numRows
<= 0 )
2906 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2908 if ( m_numCols
<= 0 )
2909 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2911 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2912 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2914 if ( m_rowLabelWin
)
2916 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2920 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2923 m_labelTextColour
= wxColour( _T("BLACK") );
2926 m_attrCache
.row
= -1;
2928 // TODO: something better than this ?
2930 m_labelFont
= this->GetFont();
2931 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2933 m_rowLabelHorizAlign
= wxLEFT
;
2934 m_rowLabelVertAlign
= wxCENTRE
;
2936 m_colLabelHorizAlign
= wxCENTRE
;
2937 m_colLabelVertAlign
= wxTOP
;
2939 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2940 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2942 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2943 m_defaultRowHeight
+= 8;
2945 m_defaultRowHeight
+= 4;
2948 m_gridLineColour
= wxColour( 128, 128, 255 );
2949 m_gridLinesEnabled
= TRUE
;
2951 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2952 m_winCapture
= (wxWindow
*)NULL
;
2953 m_canDragRowSize
= TRUE
;
2954 m_canDragColSize
= TRUE
;
2955 m_canDragGridSize
= TRUE
;
2957 m_dragRowOrCol
= -1;
2958 m_isDragging
= FALSE
;
2959 m_startDragPos
= wxDefaultPosition
;
2961 m_waitForSlowClick
= FALSE
;
2963 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2964 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2966 m_currentCellCoords
= wxGridNoCellCoords
;
2968 m_selectedTopLeft
= wxGridNoCellCoords
;
2969 m_selectedBottomRight
= wxGridNoCellCoords
;
2970 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2971 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2973 m_editable
= TRUE
; // default for whole grid
2975 m_inOnKeyDown
= FALSE
;
2984 // ----------------------------------------------------------------------------
2985 // the idea is to call these functions only when necessary because they create
2986 // quite big arrays which eat memory mostly unnecessary - in particular, if
2987 // default widths/heights are used for all rows/columns, we may not use these
2990 // with some extra code, it should be possible to only store the
2991 // widths/heights different from default ones but this will be done later...
2992 // ----------------------------------------------------------------------------
2994 void wxGrid::InitRowHeights()
2996 m_rowHeights
.Empty();
2997 m_rowBottoms
.Empty();
2999 m_rowHeights
.Alloc( m_numRows
);
3000 m_rowBottoms
.Alloc( m_numRows
);
3003 for ( int i
= 0; i
< m_numRows
; i
++ )
3005 m_rowHeights
.Add( m_defaultRowHeight
);
3006 rowBottom
+= m_defaultRowHeight
;
3007 m_rowBottoms
.Add( rowBottom
);
3011 void wxGrid::InitColWidths()
3013 m_colWidths
.Empty();
3014 m_colRights
.Empty();
3016 m_colWidths
.Alloc( m_numCols
);
3017 m_colRights
.Alloc( m_numCols
);
3019 for ( int i
= 0; i
< m_numCols
; i
++ )
3021 m_colWidths
.Add( m_defaultColWidth
);
3022 colRight
+= m_defaultColWidth
;
3023 m_colRights
.Add( colRight
);
3027 int wxGrid::GetColWidth(int col
) const
3029 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3032 int wxGrid::GetColLeft(int col
) const
3034 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3035 : m_colRights
[col
] - m_colWidths
[col
];
3038 int wxGrid::GetColRight(int col
) const
3040 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3044 int wxGrid::GetRowHeight(int row
) const
3046 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3049 int wxGrid::GetRowTop(int row
) const
3051 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3052 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3055 int wxGrid::GetRowBottom(int row
) const
3057 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3058 : m_rowBottoms
[row
];
3061 void wxGrid::CalcDimensions()
3064 GetClientSize( &cw
, &ch
);
3066 if ( m_numRows
> 0 && m_numCols
> 0 )
3068 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3069 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3071 // TODO: restore the scroll position that we had before sizing
3074 GetViewStart( &x
, &y
);
3075 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3076 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3082 void wxGrid::CalcWindowSizes()
3085 GetClientSize( &cw
, &ch
);
3087 if ( m_cornerLabelWin
->IsShown() )
3088 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3090 if ( m_colLabelWin
->IsShown() )
3091 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3093 if ( m_rowLabelWin
->IsShown() )
3094 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3096 if ( m_gridWin
->IsShown() )
3097 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3101 // this is called when the grid table sends a message to say that it
3102 // has been redimensioned
3104 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3108 // if we were using the default widths/heights so far, we must change them
3110 if ( m_colWidths
.IsEmpty() )
3115 if ( m_rowHeights
.IsEmpty() )
3120 switch ( msg
.GetId() )
3122 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3124 size_t pos
= msg
.GetCommandInt();
3125 int numRows
= msg
.GetCommandInt2();
3126 for ( i
= 0; i
< numRows
; i
++ )
3128 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3129 m_rowBottoms
.Insert( 0, pos
);
3131 m_numRows
+= numRows
;
3134 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3136 for ( i
= pos
; i
< m_numRows
; i
++ )
3138 bottom
+= m_rowHeights
[i
];
3139 m_rowBottoms
[i
] = bottom
;
3145 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3147 int numRows
= msg
.GetCommandInt();
3148 for ( i
= 0; i
< numRows
; i
++ )
3150 m_rowHeights
.Add( m_defaultRowHeight
);
3151 m_rowBottoms
.Add( 0 );
3154 int oldNumRows
= m_numRows
;
3155 m_numRows
+= numRows
;
3158 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3160 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3162 bottom
+= m_rowHeights
[i
];
3163 m_rowBottoms
[i
] = bottom
;
3169 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3171 size_t pos
= msg
.GetCommandInt();
3172 int numRows
= msg
.GetCommandInt2();
3173 for ( i
= 0; i
< numRows
; i
++ )
3175 m_rowHeights
.Remove( pos
);
3176 m_rowBottoms
.Remove( pos
);
3178 m_numRows
-= numRows
;
3183 m_colWidths
.Clear();
3184 m_colRights
.Clear();
3185 m_currentCellCoords
= wxGridNoCellCoords
;
3189 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3190 m_currentCellCoords
.Set( 0, 0 );
3193 for ( i
= 0; i
< m_numRows
; i
++ )
3195 h
+= m_rowHeights
[i
];
3196 m_rowBottoms
[i
] = h
;
3204 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3206 size_t pos
= msg
.GetCommandInt();
3207 int numCols
= msg
.GetCommandInt2();
3208 for ( i
= 0; i
< numCols
; i
++ )
3210 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3211 m_colRights
.Insert( 0, pos
);
3213 m_numCols
+= numCols
;
3216 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3218 for ( i
= pos
; i
< m_numCols
; i
++ )
3220 right
+= m_colWidths
[i
];
3221 m_colRights
[i
] = right
;
3227 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3229 int numCols
= msg
.GetCommandInt();
3230 for ( i
= 0; i
< numCols
; i
++ )
3232 m_colWidths
.Add( m_defaultColWidth
);
3233 m_colRights
.Add( 0 );
3236 int oldNumCols
= m_numCols
;
3237 m_numCols
+= numCols
;
3240 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3242 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3244 right
+= m_colWidths
[i
];
3245 m_colRights
[i
] = right
;
3251 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3253 size_t pos
= msg
.GetCommandInt();
3254 int numCols
= msg
.GetCommandInt2();
3255 for ( i
= 0; i
< numCols
; i
++ )
3257 m_colWidths
.Remove( pos
);
3258 m_colRights
.Remove( pos
);
3260 m_numCols
-= numCols
;
3264 #if 0 // leave the row alone here so that AppendCols will work subsequently
3266 m_rowHeights
.Clear();
3267 m_rowBottoms
.Clear();
3269 m_currentCellCoords
= wxGridNoCellCoords
;
3273 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3274 m_currentCellCoords
.Set( 0, 0 );
3277 for ( i
= 0; i
< m_numCols
; i
++ )
3279 w
+= m_colWidths
[i
];
3292 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3294 wxRegionIterator
iter( reg
);
3297 m_rowLabelsExposed
.Empty();
3304 // TODO: remove this when we can...
3305 // There is a bug in wxMotif that gives garbage update
3306 // rectangles if you jump-scroll a long way by clicking the
3307 // scrollbar with middle button. This is a work-around
3309 #if defined(__WXMOTIF__)
3311 m_gridWin
->GetClientSize( &cw
, &ch
);
3312 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3313 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3316 // logical bounds of update region
3319 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3320 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3322 // find the row labels within these bounds
3325 for ( row
= 0; row
< m_numRows
; row
++ )
3327 if ( GetRowBottom(row
) < top
)
3330 if ( GetRowTop(row
) > bottom
)
3333 m_rowLabelsExposed
.Add( row
);
3341 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3343 wxRegionIterator
iter( reg
);
3346 m_colLabelsExposed
.Empty();
3353 // TODO: remove this when we can...
3354 // There is a bug in wxMotif that gives garbage update
3355 // rectangles if you jump-scroll a long way by clicking the
3356 // scrollbar with middle button. This is a work-around
3358 #if defined(__WXMOTIF__)
3360 m_gridWin
->GetClientSize( &cw
, &ch
);
3361 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3362 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3365 // logical bounds of update region
3368 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3369 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3371 // find the cells within these bounds
3374 for ( col
= 0; col
< m_numCols
; col
++ )
3376 if ( GetColRight(col
) < left
)
3379 if ( GetColLeft(col
) > right
)
3382 m_colLabelsExposed
.Add( col
);
3390 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3392 wxRegionIterator
iter( reg
);
3395 m_cellsExposed
.Empty();
3396 m_rowsExposed
.Empty();
3397 m_colsExposed
.Empty();
3399 int left
, top
, right
, bottom
;
3404 // TODO: remove this when we can...
3405 // There is a bug in wxMotif that gives garbage update
3406 // rectangles if you jump-scroll a long way by clicking the
3407 // scrollbar with middle button. This is a work-around
3409 #if defined(__WXMOTIF__)
3411 m_gridWin
->GetClientSize( &cw
, &ch
);
3412 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3413 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3414 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3415 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3418 // logical bounds of update region
3420 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3421 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3423 // find the cells within these bounds
3426 for ( row
= 0; row
< m_numRows
; row
++ )
3428 if ( GetRowBottom(row
) <= top
)
3431 if ( GetRowTop(row
) > bottom
)
3434 m_rowsExposed
.Add( row
);
3436 for ( col
= 0; col
< m_numCols
; col
++ )
3438 if ( GetColRight(col
) <= left
)
3441 if ( GetColLeft(col
) > right
)
3444 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3445 m_colsExposed
.Add( col
);
3446 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3455 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3458 wxPoint
pos( event
.GetPosition() );
3459 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3461 if ( event
.Dragging() )
3463 m_isDragging
= TRUE
;
3465 if ( event
.LeftIsDown() )
3467 switch( m_cursorMode
)
3469 case WXGRID_CURSOR_RESIZE_ROW
:
3471 int cw
, ch
, left
, dummy
;
3472 m_gridWin
->GetClientSize( &cw
, &ch
);
3473 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3475 wxClientDC
dc( m_gridWin
);
3477 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3478 dc
.SetLogicalFunction(wxINVERT
);
3479 if ( m_dragLastPos
>= 0 )
3481 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3483 dc
.DrawLine( left
, y
, left
+cw
, y
);
3488 case WXGRID_CURSOR_SELECT_ROW
:
3489 if ( (row
= YToRow( y
)) >= 0 &&
3490 !IsInSelection( row
, 0 ) )
3492 SelectRow( row
, TRUE
);
3495 // default label to suppress warnings about "enumeration value
3496 // 'xxx' not handled in switch
3504 m_isDragging
= FALSE
;
3507 // ------------ Entering or leaving the window
3509 if ( event
.Entering() || event
.Leaving() )
3511 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3515 // ------------ Left button pressed
3517 else if ( event
.LeftDown() )
3519 // don't send a label click event for a hit on the
3520 // edge of the row label - this is probably the user
3521 // wanting to resize the row
3523 if ( YToEdgeOfRow(y
) < 0 )
3527 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3529 SelectRow( row
, event
.ShiftDown() );
3530 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3535 // starting to drag-resize a row
3537 if ( CanDragRowSize() )
3538 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3543 // ------------ Left double click
3545 else if (event
.LeftDClick() )
3547 if ( YToEdgeOfRow(y
) < 0 )
3550 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3555 // ------------ Left button released
3557 else if ( event
.LeftUp() )
3559 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3561 DoEndDragResizeRow();
3563 // Note: we are ending the event *after* doing
3564 // default processing in this case
3566 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3569 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3574 // ------------ Right button down
3576 else if ( event
.RightDown() )
3579 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3581 // no default action at the moment
3586 // ------------ Right double click
3588 else if ( event
.RightDClick() )
3591 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3593 // no default action at the moment
3598 // ------------ No buttons down and mouse moving
3600 else if ( event
.Moving() )
3602 m_dragRowOrCol
= YToEdgeOfRow( y
);
3603 if ( m_dragRowOrCol
>= 0 )
3605 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3607 // don't capture the mouse yet
3608 if ( CanDragRowSize() )
3609 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3612 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3614 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3620 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3623 wxPoint
pos( event
.GetPosition() );
3624 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3626 if ( event
.Dragging() )
3628 m_isDragging
= TRUE
;
3630 if ( event
.LeftIsDown() )
3632 switch( m_cursorMode
)
3634 case WXGRID_CURSOR_RESIZE_COL
:
3636 int cw
, ch
, dummy
, top
;
3637 m_gridWin
->GetClientSize( &cw
, &ch
);
3638 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3640 wxClientDC
dc( m_gridWin
);
3643 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3644 GetColMinimalWidth(m_dragRowOrCol
));
3645 dc
.SetLogicalFunction(wxINVERT
);
3646 if ( m_dragLastPos
>= 0 )
3648 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3650 dc
.DrawLine( x
, top
, x
, top
+ch
);
3655 case WXGRID_CURSOR_SELECT_COL
:
3656 if ( (col
= XToCol( x
)) >= 0 &&
3657 !IsInSelection( 0, col
) )
3659 SelectCol( col
, TRUE
);
3662 // default label to suppress warnings about "enumeration value
3663 // 'xxx' not handled in switch
3671 m_isDragging
= FALSE
;
3674 // ------------ Entering or leaving the window
3676 if ( event
.Entering() || event
.Leaving() )
3678 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3682 // ------------ Left button pressed
3684 else if ( event
.LeftDown() )
3686 // don't send a label click event for a hit on the
3687 // edge of the col label - this is probably the user
3688 // wanting to resize the col
3690 if ( XToEdgeOfCol(x
) < 0 )
3694 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3696 SelectCol( col
, event
.ShiftDown() );
3697 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3702 // starting to drag-resize a col
3704 if ( CanDragColSize() )
3705 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3710 // ------------ Left double click
3712 if ( event
.LeftDClick() )
3714 if ( XToEdgeOfCol(x
) < 0 )
3717 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3722 // ------------ Left button released
3724 else if ( event
.LeftUp() )
3726 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3728 DoEndDragResizeCol();
3730 // Note: we are ending the event *after* doing
3731 // default processing in this case
3733 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3736 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3741 // ------------ Right button down
3743 else if ( event
.RightDown() )
3746 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3748 // no default action at the moment
3753 // ------------ Right double click
3755 else if ( event
.RightDClick() )
3758 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3760 // no default action at the moment
3765 // ------------ No buttons down and mouse moving
3767 else if ( event
.Moving() )
3769 m_dragRowOrCol
= XToEdgeOfCol( x
);
3770 if ( m_dragRowOrCol
>= 0 )
3772 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3774 // don't capture the cursor yet
3775 if ( CanDragColSize() )
3776 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3779 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3781 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3787 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3789 if ( event
.LeftDown() )
3791 // indicate corner label by having both row and
3794 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3800 else if ( event
.LeftDClick() )
3802 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3805 else if ( event
.RightDown() )
3807 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3809 // no default action at the moment
3813 else if ( event
.RightDClick() )
3815 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3817 // no default action at the moment
3822 void wxGrid::ChangeCursorMode(CursorMode mode
,
3827 static const wxChar
*cursorModes
[] =
3836 wxLogTrace(_T("grid"),
3837 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3838 win
== m_colLabelWin
? _T("colLabelWin")
3839 : win
? _T("rowLabelWin")
3841 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3842 #endif // __WXDEBUG__
3844 if ( mode
== m_cursorMode
)
3849 // by default use the grid itself
3855 m_winCapture
->ReleaseMouse();
3856 m_winCapture
= (wxWindow
*)NULL
;
3859 m_cursorMode
= mode
;
3861 switch ( m_cursorMode
)
3863 case WXGRID_CURSOR_RESIZE_ROW
:
3864 win
->SetCursor( m_rowResizeCursor
);
3867 case WXGRID_CURSOR_RESIZE_COL
:
3868 win
->SetCursor( m_colResizeCursor
);
3872 win
->SetCursor( *wxSTANDARD_CURSOR
);
3875 // we need to capture mouse when resizing
3876 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3877 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3879 if ( captureMouse
&& resize
)
3881 win
->CaptureMouse();
3886 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3889 wxPoint
pos( event
.GetPosition() );
3890 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3892 wxGridCellCoords coords
;
3893 XYToCell( x
, y
, coords
);
3895 if ( event
.Dragging() )
3897 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3899 // Don't start doing anything until the mouse has been drug at
3900 // least 3 pixels in any direction...
3903 if (m_startDragPos
== wxDefaultPosition
)
3905 m_startDragPos
= pos
;
3908 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3912 m_isDragging
= TRUE
;
3913 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3915 // Hide the edit control, so it
3916 // won't interfer with drag-shrinking.
3917 if ( IsCellEditControlEnabled() )
3919 HideCellEditControl();
3920 SaveEditControlValue();
3923 // Have we captured the mouse yet?
3926 m_winCapture
= m_gridWin
;
3927 m_winCapture
->CaptureMouse();
3930 if ( coords
!= wxGridNoCellCoords
)
3932 if ( !IsSelection() )
3934 SelectBlock( coords
, coords
);
3938 SelectBlock( m_currentCellCoords
, coords
);
3941 if (! IsVisible(coords
))
3943 MakeCellVisible(coords
);
3944 // TODO: need to introduce a delay or something here. The
3945 // scrolling is way to fast, at least on MSW.
3949 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3951 int cw
, ch
, left
, dummy
;
3952 m_gridWin
->GetClientSize( &cw
, &ch
);
3953 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3955 wxClientDC
dc( m_gridWin
);
3957 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3958 dc
.SetLogicalFunction(wxINVERT
);
3959 if ( m_dragLastPos
>= 0 )
3961 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3963 dc
.DrawLine( left
, y
, left
+cw
, y
);
3966 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3968 int cw
, ch
, dummy
, top
;
3969 m_gridWin
->GetClientSize( &cw
, &ch
);
3970 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3972 wxClientDC
dc( m_gridWin
);
3974 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3975 GetColMinimalWidth(m_dragRowOrCol
) );
3976 dc
.SetLogicalFunction(wxINVERT
);
3977 if ( m_dragLastPos
>= 0 )
3979 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3981 dc
.DrawLine( x
, top
, x
, top
+ch
);
3988 m_isDragging
= FALSE
;
3989 m_startDragPos
= wxDefaultPosition
;
3991 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3992 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3995 if ( event
.Entering() || event
.Leaving() )
3997 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3998 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4003 // ------------ Left button pressed
4005 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4007 if ( event
.ShiftDown() )
4009 SelectBlock( m_currentCellCoords
, coords
);
4011 else if ( XToEdgeOfCol(x
) < 0 &&
4012 YToEdgeOfRow(y
) < 0 )
4014 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4019 DisableCellEditControl();
4020 MakeCellVisible( coords
);
4022 // if this is the second click on this cell then start
4024 if ( m_waitForSlowClick
&&
4025 (coords
== m_currentCellCoords
) &&
4026 CanEnableCellControl())
4028 EnableCellEditControl();
4030 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4031 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
4034 m_waitForSlowClick
= FALSE
;
4038 SetCurrentCell( coords
);
4039 m_waitForSlowClick
= TRUE
;
4046 // ------------ Left double click
4048 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4050 DisableCellEditControl();
4052 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4054 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4062 // ------------ Left button released
4064 else if ( event
.LeftUp() )
4066 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4068 if ( IsSelection() )
4072 m_winCapture
->ReleaseMouse();
4073 m_winCapture
= NULL
;
4075 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4078 // Show the edit control, if it has been hidden for
4080 ShowCellEditControl();
4082 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4084 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4085 DoEndDragResizeRow();
4087 // Note: we are ending the event *after* doing
4088 // default processing in this case
4090 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4092 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4094 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4095 DoEndDragResizeCol();
4097 // Note: we are ending the event *after* doing
4098 // default processing in this case
4100 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4107 // ------------ Right button down
4109 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4111 DisableCellEditControl();
4112 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4117 // no default action at the moment
4122 // ------------ Right double click
4124 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4126 DisableCellEditControl();
4127 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4132 // no default action at the moment
4136 // ------------ Moving and no button action
4138 else if ( event
.Moving() && !event
.IsButton() )
4140 int dragRow
= YToEdgeOfRow( y
);
4141 int dragCol
= XToEdgeOfCol( x
);
4143 // Dragging on the corner of a cell to resize in both
4144 // directions is not implemented yet...
4146 if ( dragRow
>= 0 && dragCol
>= 0 )
4148 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4154 m_dragRowOrCol
= dragRow
;
4156 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4158 if ( CanDragRowSize() && CanDragGridSize() )
4159 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4164 m_dragRowOrCol
= dragCol
;
4172 m_dragRowOrCol
= dragCol
;
4174 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4176 if ( CanDragColSize() && CanDragGridSize() )
4177 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4183 // Neither on a row or col edge
4185 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4187 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4193 void wxGrid::DoEndDragResizeRow()
4195 if ( m_dragLastPos
>= 0 )
4197 // erase the last line and resize the row
4199 int cw
, ch
, left
, dummy
;
4200 m_gridWin
->GetClientSize( &cw
, &ch
);
4201 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4203 wxClientDC
dc( m_gridWin
);
4205 dc
.SetLogicalFunction( wxINVERT
);
4206 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4207 HideCellEditControl();
4208 SaveEditControlValue();
4210 int rowTop
= GetRowTop(m_dragRowOrCol
);
4211 SetRowSize( m_dragRowOrCol
,
4212 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4214 if ( !GetBatchCount() )
4216 // Only needed to get the correct rect.y:
4217 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4219 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4220 rect
.width
= m_rowLabelWidth
;
4221 rect
.height
= ch
- rect
.y
;
4222 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4224 m_gridWin
->Refresh( FALSE
, &rect
);
4227 ShowCellEditControl();
4232 void wxGrid::DoEndDragResizeCol()
4234 if ( m_dragLastPos
>= 0 )
4236 // erase the last line and resize the col
4238 int cw
, ch
, dummy
, top
;
4239 m_gridWin
->GetClientSize( &cw
, &ch
);
4240 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4242 wxClientDC
dc( m_gridWin
);
4244 dc
.SetLogicalFunction( wxINVERT
);
4245 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4246 HideCellEditControl();
4247 SaveEditControlValue();
4249 int colLeft
= GetColLeft(m_dragRowOrCol
);
4250 SetColSize( m_dragRowOrCol
,
4251 wxMax( m_dragLastPos
- colLeft
,
4252 GetColMinimalWidth(m_dragRowOrCol
) ) );
4254 if ( !GetBatchCount() )
4256 // Only needed to get the correct rect.x:
4257 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4259 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4260 rect
.width
= cw
- rect
.x
;
4261 rect
.height
= m_colLabelHeight
;
4262 m_colLabelWin
->Refresh( TRUE
, &rect
);
4264 m_gridWin
->Refresh( FALSE
, &rect
);
4267 ShowCellEditControl();
4274 // ------ interaction with data model
4276 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4278 switch ( msg
.GetId() )
4280 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4281 return GetModelValues();
4283 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4284 return SetModelValues();
4286 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4287 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4288 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4289 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4290 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4291 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4292 return Redimension( msg
);
4301 // The behaviour of this function depends on the grid table class
4302 // Clear() function. For the default wxGridStringTable class the
4303 // behavious is to replace all cell contents with wxEmptyString but
4304 // not to change the number of rows or cols.
4306 void wxGrid::ClearGrid()
4310 if (IsCellEditControlEnabled())
4311 DisableCellEditControl();
4314 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4319 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4321 // TODO: something with updateLabels flag
4325 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4331 if (IsCellEditControlEnabled())
4332 DisableCellEditControl();
4334 bool ok
= m_table
->InsertRows( pos
, numRows
);
4336 // the table will have sent the results of the insert row
4337 // operation to this view object as a grid table message
4341 if ( m_numCols
== 0 )
4343 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4345 // TODO: perhaps instead of appending the default number of cols
4346 // we should remember what the last non-zero number of cols was ?
4350 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4352 // if we have just inserted cols into an empty grid the current
4353 // cell will be undefined...
4355 SetCurrentCell( 0, 0 );
4359 if ( !GetBatchCount() ) Refresh();
4371 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4373 // TODO: something with updateLabels flag
4377 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4381 if ( m_table
&& m_table
->AppendRows( numRows
) )
4383 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4385 // if we have just inserted cols into an empty grid the current
4386 // cell will be undefined...
4388 SetCurrentCell( 0, 0 );
4391 // the table will have sent the results of the append row
4392 // operation to this view object as a grid table message
4395 if ( !GetBatchCount() ) Refresh();
4405 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4407 // TODO: something with updateLabels flag
4411 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4417 if (IsCellEditControlEnabled())
4418 DisableCellEditControl();
4420 if (m_table
->DeleteRows( pos
, numRows
))
4423 // the table will have sent the results of the delete row
4424 // operation to this view object as a grid table message
4427 if ( !GetBatchCount() ) Refresh();
4435 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4437 // TODO: something with updateLabels flag
4441 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4447 if (IsCellEditControlEnabled())
4448 DisableCellEditControl();
4450 bool ok
= m_table
->InsertCols( pos
, numCols
);
4452 // the table will have sent the results of the insert col
4453 // operation to this view object as a grid table message
4457 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4459 // if we have just inserted cols into an empty grid the current
4460 // cell will be undefined...
4462 SetCurrentCell( 0, 0 );
4466 if ( !GetBatchCount() ) Refresh();
4478 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4480 // TODO: something with updateLabels flag
4484 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4488 if ( m_table
&& m_table
->AppendCols( numCols
) )
4490 // the table will have sent the results of the append col
4491 // operation to this view object as a grid table message
4493 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4495 // if we have just inserted cols into an empty grid the current
4496 // cell will be undefined...
4498 SetCurrentCell( 0, 0 );
4502 if ( !GetBatchCount() ) Refresh();
4512 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4514 // TODO: something with updateLabels flag
4518 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4524 if (IsCellEditControlEnabled())
4525 DisableCellEditControl();
4527 if ( m_table
->DeleteCols( pos
, numCols
) )
4529 // the table will have sent the results of the delete col
4530 // operation to this view object as a grid table message
4533 if ( !GetBatchCount() ) Refresh();
4543 // ----- event handlers
4546 // Generate a grid event based on a mouse event and
4547 // return the result of ProcessEvent()
4549 bool wxGrid::SendEvent( const wxEventType type
,
4551 wxMouseEvent
& mouseEv
)
4553 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4555 int rowOrCol
= (row
== -1 ? col
: row
);
4557 wxGridSizeEvent
gridEvt( GetId(),
4561 mouseEv
.GetX(), mouseEv
.GetY(),
4562 mouseEv
.ControlDown(),
4563 mouseEv
.ShiftDown(),
4565 mouseEv
.MetaDown() );
4567 return GetEventHandler()->ProcessEvent(gridEvt
);
4569 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4571 wxGridRangeSelectEvent
gridEvt( GetId(),
4575 m_selectedBottomRight
,
4576 mouseEv
.ControlDown(),
4577 mouseEv
.ShiftDown(),
4579 mouseEv
.MetaDown() );
4581 return GetEventHandler()->ProcessEvent(gridEvt
);
4585 wxGridEvent
gridEvt( GetId(),
4589 mouseEv
.GetX(), mouseEv
.GetY(),
4590 mouseEv
.ControlDown(),
4591 mouseEv
.ShiftDown(),
4593 mouseEv
.MetaDown() );
4595 return GetEventHandler()->ProcessEvent(gridEvt
);
4600 // Generate a grid event of specified type and return the result
4601 // of ProcessEvent().
4603 bool wxGrid::SendEvent( const wxEventType type
,
4606 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4608 int rowOrCol
= (row
== -1 ? col
: row
);
4610 wxGridSizeEvent
gridEvt( GetId(),
4615 return GetEventHandler()->ProcessEvent(gridEvt
);
4619 wxGridEvent
gridEvt( GetId(),
4624 return GetEventHandler()->ProcessEvent(gridEvt
);
4629 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4631 wxPaintDC
dc( this );
4633 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4634 m_numRows
&& m_numCols
)
4636 m_currentCellCoords
.Set(0, 0);
4637 ShowCellEditControl();
4644 // This is just here to make sure that CalcDimensions gets called when
4645 // the grid view is resized... then the size event is skipped to allow
4646 // the box sizers to handle everything
4648 void wxGrid::OnSize( wxSizeEvent
& event
)
4655 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4657 if ( m_inOnKeyDown
)
4659 // shouldn't be here - we are going round in circles...
4661 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4664 m_inOnKeyDown
= TRUE
;
4666 // propagate the event up and see if it gets processed
4668 wxWindow
*parent
= GetParent();
4669 wxKeyEvent
keyEvt( event
);
4670 keyEvt
.SetEventObject( parent
);
4672 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4675 // TODO: Should also support Shift-cursor keys for
4676 // extending the selection. Maybe add a flag to
4677 // MoveCursorXXX() and MoveCursorXXXBlock() and
4678 // just send event.ShiftDown().
4680 // try local handlers
4682 switch ( event
.KeyCode() )
4685 if ( event
.ControlDown() )
4687 MoveCursorUpBlock();
4696 if ( event
.ControlDown() )
4698 MoveCursorDownBlock();
4707 if ( event
.ControlDown() )
4709 MoveCursorLeftBlock();
4718 if ( event
.ControlDown() )
4720 MoveCursorRightBlock();
4729 if ( event
.ControlDown() )
4731 event
.Skip(); // to let the edit control have the return
4740 if (event
.ShiftDown())
4747 if ( event
.ControlDown() )
4749 MakeCellVisible( 0, 0 );
4750 SetCurrentCell( 0, 0 );
4759 if ( event
.ControlDown() )
4761 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4762 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4779 if ( !IsEditable() )
4784 // Otherwise fall through to default
4787 // alphanumeric keys or F2 (special key just for this) enable
4788 // the cell edit control
4789 if ( !(event
.AltDown() ||
4791 event
.ControlDown()) &&
4792 (isalnum(event
.KeyCode()) || event
.KeyCode() == WXK_F2
) &&
4793 !IsCellEditControlEnabled() &&
4794 CanEnableCellControl() )
4796 EnableCellEditControl();
4797 int row
= m_currentCellCoords
.GetRow();
4798 int col
= m_currentCellCoords
.GetCol();
4799 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4800 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4805 // let others process char events with modifiers or all
4806 // char events for readonly cells
4813 m_inOnKeyDown
= FALSE
;
4817 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4821 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4823 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4825 // the event has been intercepted - do nothing
4830 m_currentCellCoords
!= wxGridNoCellCoords
)
4832 HideCellEditControl();
4833 DisableCellEditControl();
4835 // Clear the old current cell highlight
4836 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4838 // Otherwise refresh redraws the highlight!
4839 m_currentCellCoords
= coords
;
4841 m_gridWin
->Refresh( FALSE
, &r
);
4844 m_currentCellCoords
= coords
;
4848 wxClientDC
dc(m_gridWin
);
4851 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4852 DrawCellHighlight(dc
, attr
);
4855 if ( IsSelection() )
4857 wxRect
r( SelectionToDeviceRect() );
4859 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4866 // ------ functions to get/send data (see also public functions)
4869 bool wxGrid::GetModelValues()
4873 // all we need to do is repaint the grid
4875 m_gridWin
->Refresh();
4883 bool wxGrid::SetModelValues()
4889 for ( row
= 0; row
< m_numRows
; row
++ )
4891 for ( col
= 0; col
< m_numCols
; col
++ )
4893 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4905 // Note - this function only draws cells that are in the list of
4906 // exposed cells (usually set from the update region by
4907 // CalcExposedCells)
4909 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4911 if ( !m_numRows
|| !m_numCols
) return;
4914 size_t numCells
= m_cellsExposed
.GetCount();
4916 for ( i
= 0; i
< numCells
; i
++ )
4918 DrawCell( dc
, m_cellsExposed
[i
] );
4923 void wxGrid::DrawGridSpace( wxDC
& dc
)
4925 if ( m_numRows
&& m_numCols
)
4928 m_gridWin
->GetClientSize( &cw
, &ch
);
4931 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4933 if ( right
> GetColRight(m_numCols
-1) ||
4934 bottom
> GetRowBottom(m_numRows
-1) )
4937 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4939 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4940 dc
.SetPen( *wxTRANSPARENT_PEN
);
4942 if ( right
> GetColRight(m_numCols
-1) )
4943 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
4944 right
- GetColRight(m_numCols
-1), ch
);
4946 if ( bottom
> GetRowBottom(m_numRows
-1) )
4947 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
4948 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4954 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4956 int row
= coords
.GetRow();
4957 int col
= coords
.GetCol();
4959 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4962 // we draw the cell border ourselves
4963 #if !WXGRID_DRAW_LINES
4964 if ( m_gridLinesEnabled
)
4965 DrawCellBorder( dc
, coords
);
4968 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4970 bool isCurrent
= coords
== m_currentCellCoords
;
4973 rect
.x
= GetColLeft(col
);
4974 rect
.y
= GetRowTop(row
);
4975 rect
.width
= GetColWidth(col
) - 1;
4976 rect
.height
= GetRowHeight(row
) - 1;
4978 // if the editor is shown, we should use it and not the renderer
4979 if ( isCurrent
&& IsCellEditControlEnabled() )
4981 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4985 // but all the rest is drawn by the cell renderer and hence may be
4987 attr
->GetRenderer(this, row
, col
)->
4988 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4995 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4997 int row
= m_currentCellCoords
.GetRow();
4998 int col
= m_currentCellCoords
.GetCol();
5000 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5004 rect
.x
= GetColLeft(col
);
5005 rect
.y
= GetRowTop(row
);
5006 rect
.width
= GetColWidth(col
) - 1;
5007 rect
.height
= GetRowHeight(row
) - 1;
5009 // hmmm... what could we do here to show that the cell is disabled?
5010 // for now, I just draw a thinner border than for the other ones, but
5011 // it doesn't look really good
5012 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5013 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5015 dc
.DrawRectangle(rect
);
5018 // VZ: my experiments with 3d borders...
5020 // how to properly set colours for arbitrary bg?
5021 wxCoord x1
= rect
.x
,
5023 x2
= rect
.x
+ rect
.width
-1,
5024 y2
= rect
.y
+ rect
.height
-1;
5026 dc
.SetPen(*wxWHITE_PEN
);
5027 dc
.DrawLine(x1
, y1
, x2
, y1
);
5028 dc
.DrawLine(x1
, y1
, x1
, y2
);
5030 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5031 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5033 dc
.SetPen(*wxBLACK_PEN
);
5034 dc
.DrawLine(x1
, y2
, x2
, y2
);
5035 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5040 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5042 int row
= coords
.GetRow();
5043 int col
= coords
.GetCol();
5044 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5047 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5049 // right hand border
5051 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5052 GetColRight(col
), GetRowBottom(row
) );
5056 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5057 GetColRight(col
), GetRowBottom(row
) );
5060 void wxGrid::DrawHighlight(wxDC
& dc
)
5062 if ( IsCellEditControlEnabled() )
5064 // don't show highlight when the edit control is shown
5068 // if the active cell was repainted, repaint its highlight too because it
5069 // might have been damaged by the grid lines
5070 size_t count
= m_cellsExposed
.GetCount();
5071 for ( size_t n
= 0; n
< count
; n
++ )
5073 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5075 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5076 DrawCellHighlight(dc
, attr
);
5084 // TODO: remove this ???
5085 // This is used to redraw all grid lines e.g. when the grid line colour
5088 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5090 if ( !m_gridLinesEnabled
||
5092 !m_numCols
) return;
5094 int top
, bottom
, left
, right
;
5100 m_gridWin
->GetClientSize(&cw
, &ch
);
5102 // virtual coords of visible area
5104 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5105 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5110 reg
.GetBox(x
, y
, w
, h
);
5111 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5112 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5116 m_gridWin
->GetClientSize(&cw
, &ch
);
5117 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5118 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5121 // avoid drawing grid lines past the last row and col
5123 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5124 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5126 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5128 // horizontal grid lines
5131 for ( i
= 0; i
< m_numRows
; i
++ )
5133 int bot
= GetRowBottom(i
) - 1;
5142 dc
.DrawLine( left
, bot
, right
, bot
);
5147 // vertical grid lines
5149 for ( i
= 0; i
< m_numCols
; i
++ )
5151 int colRight
= GetColRight(i
) - 1;
5152 if ( colRight
> right
)
5157 if ( colRight
>= left
)
5159 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5165 void wxGrid::DrawRowLabels( wxDC
& dc
)
5167 if ( !m_numRows
|| !m_numCols
) return;
5170 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5172 for ( i
= 0; i
< numLabels
; i
++ )
5174 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5179 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5181 if ( GetRowHeight(row
) <= 0 )
5184 int rowTop
= GetRowTop(row
),
5185 rowBottom
= GetRowBottom(row
) - 1;
5187 dc
.SetPen( *wxBLACK_PEN
);
5188 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5189 m_rowLabelWidth
-1, rowBottom
);
5191 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5193 dc
.SetPen( *wxWHITE_PEN
);
5194 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5195 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5197 dc
.SetBackgroundMode( wxTRANSPARENT
);
5198 dc
.SetTextForeground( GetLabelTextColour() );
5199 dc
.SetFont( GetLabelFont() );
5202 GetRowLabelAlignment( &hAlign
, &vAlign
);
5206 rect
.SetY( GetRowTop(row
) + 2 );
5207 rect
.SetWidth( m_rowLabelWidth
- 4 );
5208 rect
.SetHeight( GetRowHeight(row
) - 4 );
5209 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5213 void wxGrid::DrawColLabels( wxDC
& dc
)
5215 if ( !m_numRows
|| !m_numCols
) return;
5218 size_t numLabels
= m_colLabelsExposed
.GetCount();
5220 for ( i
= 0; i
< numLabels
; i
++ )
5222 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5227 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5229 if ( GetColWidth(col
) <= 0 )
5232 int colLeft
= GetColLeft(col
),
5233 colRight
= GetColRight(col
) - 1;
5235 dc
.SetPen( *wxBLACK_PEN
);
5236 dc
.DrawLine( colRight
, 0,
5237 colRight
, m_colLabelHeight
-1 );
5239 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5240 colRight
, m_colLabelHeight
-1 );
5242 dc
.SetPen( *wxWHITE_PEN
);
5243 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5244 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5246 dc
.SetBackgroundMode( wxTRANSPARENT
);
5247 dc
.SetTextForeground( GetLabelTextColour() );
5248 dc
.SetFont( GetLabelFont() );
5250 dc
.SetBackgroundMode( wxTRANSPARENT
);
5251 dc
.SetTextForeground( GetLabelTextColour() );
5252 dc
.SetFont( GetLabelFont() );
5255 GetColLabelAlignment( &hAlign
, &vAlign
);
5258 rect
.SetX( colLeft
+ 2 );
5260 rect
.SetWidth( GetColWidth(col
) - 4 );
5261 rect
.SetHeight( m_colLabelHeight
- 4 );
5262 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5266 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5267 const wxString
& value
,
5272 long textWidth
, textHeight
;
5273 long lineWidth
, lineHeight
;
5274 wxArrayString lines
;
5276 dc
.SetClippingRegion( rect
);
5277 StringToLines( value
, lines
);
5278 if ( lines
.GetCount() )
5280 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5281 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5284 switch ( horizAlign
)
5287 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5291 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5300 switch ( vertAlign
)
5303 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5307 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5316 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5318 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5323 dc
.DestroyClippingRegion();
5327 // Split multi line text up into an array of strings. Any existing
5328 // contents of the string array are preserved.
5330 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5334 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5335 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5337 while ( startPos
< (int)tVal
.Length() )
5339 pos
= tVal
.Mid(startPos
).Find( eol
);
5344 else if ( pos
== 0 )
5346 lines
.Add( wxEmptyString
);
5350 lines
.Add( value
.Mid(startPos
, pos
) );
5354 if ( startPos
< (int)value
.Length() )
5356 lines
.Add( value
.Mid( startPos
) );
5361 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5362 wxArrayString
& lines
,
5363 long *width
, long *height
)
5370 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5372 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5373 w
= wxMax( w
, lineW
);
5383 // ------ Edit control functions
5387 void wxGrid::EnableEditing( bool edit
)
5389 // TODO: improve this ?
5391 if ( edit
!= m_editable
)
5395 // FIXME IMHO this won't disable the edit control if edit == FALSE
5396 // because of the check in the beginning of
5397 // EnableCellEditControl() just below (VZ)
5398 EnableCellEditControl(m_editable
);
5403 void wxGrid::EnableCellEditControl( bool enable
)
5408 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5409 SetCurrentCell( 0, 0 );
5411 if ( enable
!= m_cellEditCtrlEnabled
)
5413 // TODO allow the app to Veto() this event?
5414 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5418 // this should be checked by the caller!
5419 wxASSERT_MSG( CanEnableCellControl(),
5420 _T("can't enable editing for this cell!") );
5422 // do it before ShowCellEditControl()
5423 m_cellEditCtrlEnabled
= enable
;
5425 ShowCellEditControl();
5429 HideCellEditControl();
5430 SaveEditControlValue();
5432 // do it after HideCellEditControl()
5433 m_cellEditCtrlEnabled
= enable
;
5438 bool wxGrid::IsCurrentCellReadOnly() const
5441 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5442 bool readonly
= attr
->IsReadOnly();
5448 bool wxGrid::CanEnableCellControl() const
5450 return m_editable
&& !IsCurrentCellReadOnly();
5453 bool wxGrid::IsCellEditControlEnabled() const
5455 // the cell edit control might be disable for all cells or just for the
5456 // current one if it's read only
5457 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5460 void wxGrid::ShowCellEditControl()
5462 if ( IsCellEditControlEnabled() )
5464 if ( !IsVisible( m_currentCellCoords
) )
5470 wxRect rect
= CellToRect( m_currentCellCoords
);
5471 int row
= m_currentCellCoords
.GetRow();
5472 int col
= m_currentCellCoords
.GetCol();
5474 // convert to scrolled coords
5476 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5478 // done in PaintBackground()
5480 // erase the highlight and the cell contents because the editor
5481 // might not cover the entire cell
5482 wxClientDC
dc( m_gridWin
);
5484 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5485 dc
.SetPen(*wxTRANSPARENT_PEN
);
5486 dc
.DrawRectangle(rect
);
5489 // cell is shifted by one pixel
5493 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5494 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5495 if ( !editor
->IsCreated() )
5497 editor
->Create(m_gridWin
, -1,
5498 new wxGridCellEditorEvtHandler(this, editor
));
5501 editor
->Show( TRUE
, attr
);
5503 editor
->SetSize( rect
);
5505 editor
->BeginEdit(row
, col
, this);
5512 void wxGrid::HideCellEditControl()
5514 if ( IsCellEditControlEnabled() )
5516 int row
= m_currentCellCoords
.GetRow();
5517 int col
= m_currentCellCoords
.GetCol();
5519 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5520 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5522 m_gridWin
->SetFocus();
5527 void wxGrid::SaveEditControlValue()
5529 if ( IsCellEditControlEnabled() )
5531 int row
= m_currentCellCoords
.GetRow();
5532 int col
= m_currentCellCoords
.GetCol();
5534 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5535 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5536 bool changed
= editor
->EndEdit(row
, col
, this);
5542 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5543 m_currentCellCoords
.GetRow(),
5544 m_currentCellCoords
.GetCol() );
5551 // ------ Grid location functions
5552 // Note that all of these functions work with the logical coordinates of
5553 // grid cells and labels so you will need to convert from device
5554 // coordinates for mouse events etc.
5557 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5559 int row
= YToRow(y
);
5560 int col
= XToCol(x
);
5562 if ( row
== -1 || col
== -1 )
5564 coords
= wxGridNoCellCoords
;
5568 coords
.Set( row
, col
);
5573 int wxGrid::YToRow( int y
)
5577 for ( i
= 0; i
< m_numRows
; i
++ )
5579 if ( y
< GetRowBottom(i
) )
5587 int wxGrid::XToCol( int x
)
5591 for ( i
= 0; i
< m_numCols
; i
++ )
5593 if ( x
< GetColRight(i
) )
5601 // return the row number that that the y coord is near the edge of, or
5602 // -1 if not near an edge
5604 int wxGrid::YToEdgeOfRow( int y
)
5608 for ( i
= 0; i
< m_numRows
; i
++ )
5610 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5612 d
= abs( y
- GetRowBottom(i
) );
5613 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5622 // return the col number that that the x coord is near the edge of, or
5623 // -1 if not near an edge
5625 int wxGrid::XToEdgeOfCol( int x
)
5629 for ( i
= 0; i
< m_numCols
; i
++ )
5631 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5633 d
= abs( x
- GetColRight(i
) );
5634 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5643 wxRect
wxGrid::CellToRect( int row
, int col
)
5645 wxRect
rect( -1, -1, -1, -1 );
5647 if ( row
>= 0 && row
< m_numRows
&&
5648 col
>= 0 && col
< m_numCols
)
5650 rect
.x
= GetColLeft(col
);
5651 rect
.y
= GetRowTop(row
);
5652 rect
.width
= GetColWidth(col
);
5653 rect
.height
= GetRowHeight(row
);
5660 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5662 // get the cell rectangle in logical coords
5664 wxRect
r( CellToRect( row
, col
) );
5666 // convert to device coords
5668 int left
, top
, right
, bottom
;
5669 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5670 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5672 // check against the client area of the grid window
5675 m_gridWin
->GetClientSize( &cw
, &ch
);
5677 if ( wholeCellVisible
)
5679 // is the cell wholly visible ?
5681 return ( left
>= 0 && right
<= cw
&&
5682 top
>= 0 && bottom
<= ch
);
5686 // is the cell partly visible ?
5688 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5689 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5694 // make the specified cell location visible by doing a minimal amount
5697 void wxGrid::MakeCellVisible( int row
, int col
)
5700 int xpos
= -1, ypos
= -1;
5702 if ( row
>= 0 && row
< m_numRows
&&
5703 col
>= 0 && col
< m_numCols
)
5705 // get the cell rectangle in logical coords
5707 wxRect
r( CellToRect( row
, col
) );
5709 // convert to device coords
5711 int left
, top
, right
, bottom
;
5712 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5713 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5716 m_gridWin
->GetClientSize( &cw
, &ch
);
5722 else if ( bottom
> ch
)
5724 int h
= r
.GetHeight();
5726 for ( i
= row
-1; i
>= 0; i
-- )
5728 int rowHeight
= GetRowHeight(i
);
5729 if ( h
+ rowHeight
> ch
)
5736 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5737 // have rounding errors (this is important, because if we do, we
5738 // might not scroll at all and some cells won't be redrawn)
5739 ypos
+= GRID_SCROLL_LINE
/ 2;
5746 else if ( right
> cw
)
5748 int w
= r
.GetWidth();
5750 for ( i
= col
-1; i
>= 0; i
-- )
5752 int colWidth
= GetColWidth(i
);
5753 if ( w
+ colWidth
> cw
)
5760 // see comment for ypos above
5761 xpos
+= GRID_SCROLL_LINE
/ 2;
5764 if ( xpos
!= -1 || ypos
!= -1 )
5766 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5767 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5768 Scroll( xpos
, ypos
);
5776 // ------ Grid cursor movement functions
5779 bool wxGrid::MoveCursorUp()
5781 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5782 m_currentCellCoords
.GetRow() > 0 )
5784 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5785 m_currentCellCoords
.GetCol() );
5787 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5788 m_currentCellCoords
.GetCol() );
5797 bool wxGrid::MoveCursorDown()
5799 // TODO: allow for scrolling
5801 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5802 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5804 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5805 m_currentCellCoords
.GetCol() );
5807 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5808 m_currentCellCoords
.GetCol() );
5817 bool wxGrid::MoveCursorLeft()
5819 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5820 m_currentCellCoords
.GetCol() > 0 )
5822 MakeCellVisible( m_currentCellCoords
.GetRow(),
5823 m_currentCellCoords
.GetCol() - 1 );
5825 SetCurrentCell( m_currentCellCoords
.GetRow(),
5826 m_currentCellCoords
.GetCol() - 1 );
5835 bool wxGrid::MoveCursorRight()
5837 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5838 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5840 MakeCellVisible( m_currentCellCoords
.GetRow(),
5841 m_currentCellCoords
.GetCol() + 1 );
5843 SetCurrentCell( m_currentCellCoords
.GetRow(),
5844 m_currentCellCoords
.GetCol() + 1 );
5853 bool wxGrid::MovePageUp()
5855 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5857 int row
= m_currentCellCoords
.GetRow();
5861 m_gridWin
->GetClientSize( &cw
, &ch
);
5863 int y
= GetRowTop(row
);
5864 int newRow
= YToRow( y
- ch
+ 1 );
5869 else if ( newRow
== row
)
5874 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5875 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5883 bool wxGrid::MovePageDown()
5885 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5887 int row
= m_currentCellCoords
.GetRow();
5888 if ( row
< m_numRows
)
5891 m_gridWin
->GetClientSize( &cw
, &ch
);
5893 int y
= GetRowTop(row
);
5894 int newRow
= YToRow( y
+ ch
);
5897 newRow
= m_numRows
- 1;
5899 else if ( newRow
== row
)
5904 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5905 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5913 bool wxGrid::MoveCursorUpBlock()
5916 m_currentCellCoords
!= wxGridNoCellCoords
&&
5917 m_currentCellCoords
.GetRow() > 0 )
5919 int row
= m_currentCellCoords
.GetRow();
5920 int col
= m_currentCellCoords
.GetCol();
5922 if ( m_table
->IsEmptyCell(row
, col
) )
5924 // starting in an empty cell: find the next block of
5930 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5933 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5935 // starting at the top of a block: find the next block
5941 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5946 // starting within a block: find the top of the block
5951 if ( m_table
->IsEmptyCell(row
, col
) )
5959 MakeCellVisible( row
, col
);
5960 SetCurrentCell( row
, col
);
5968 bool wxGrid::MoveCursorDownBlock()
5971 m_currentCellCoords
!= wxGridNoCellCoords
&&
5972 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5974 int row
= m_currentCellCoords
.GetRow();
5975 int col
= m_currentCellCoords
.GetCol();
5977 if ( m_table
->IsEmptyCell(row
, col
) )
5979 // starting in an empty cell: find the next block of
5982 while ( row
< m_numRows
-1 )
5985 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5988 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5990 // starting at the bottom of a block: find the next block
5993 while ( row
< m_numRows
-1 )
5996 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6001 // starting within a block: find the bottom of the block
6003 while ( row
< m_numRows
-1 )
6006 if ( m_table
->IsEmptyCell(row
, col
) )
6014 MakeCellVisible( row
, col
);
6015 SetCurrentCell( row
, col
);
6023 bool wxGrid::MoveCursorLeftBlock()
6026 m_currentCellCoords
!= wxGridNoCellCoords
&&
6027 m_currentCellCoords
.GetCol() > 0 )
6029 int row
= m_currentCellCoords
.GetRow();
6030 int col
= m_currentCellCoords
.GetCol();
6032 if ( m_table
->IsEmptyCell(row
, col
) )
6034 // starting in an empty cell: find the next block of
6040 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6043 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6045 // starting at the left of a block: find the next block
6051 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6056 // starting within a block: find the left of the block
6061 if ( m_table
->IsEmptyCell(row
, col
) )
6069 MakeCellVisible( row
, col
);
6070 SetCurrentCell( row
, col
);
6078 bool wxGrid::MoveCursorRightBlock()
6081 m_currentCellCoords
!= wxGridNoCellCoords
&&
6082 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6084 int row
= m_currentCellCoords
.GetRow();
6085 int col
= m_currentCellCoords
.GetCol();
6087 if ( m_table
->IsEmptyCell(row
, col
) )
6089 // starting in an empty cell: find the next block of
6092 while ( col
< m_numCols
-1 )
6095 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6098 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6100 // starting at the right of a block: find the next block
6103 while ( col
< m_numCols
-1 )
6106 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6111 // starting within a block: find the right of the block
6113 while ( col
< m_numCols
-1 )
6116 if ( m_table
->IsEmptyCell(row
, col
) )
6124 MakeCellVisible( row
, col
);
6125 SetCurrentCell( row
, col
);
6136 // ------ Label values and formatting
6139 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6141 *horiz
= m_rowLabelHorizAlign
;
6142 *vert
= m_rowLabelVertAlign
;
6145 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6147 *horiz
= m_colLabelHorizAlign
;
6148 *vert
= m_colLabelVertAlign
;
6151 wxString
wxGrid::GetRowLabelValue( int row
)
6155 return m_table
->GetRowLabelValue( row
);
6165 wxString
wxGrid::GetColLabelValue( int col
)
6169 return m_table
->GetColLabelValue( col
);
6180 void wxGrid::SetRowLabelSize( int width
)
6182 width
= wxMax( width
, 0 );
6183 if ( width
!= m_rowLabelWidth
)
6187 m_rowLabelWin
->Show( FALSE
);
6188 m_cornerLabelWin
->Show( FALSE
);
6190 else if ( m_rowLabelWidth
== 0 )
6192 m_rowLabelWin
->Show( TRUE
);
6193 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6196 m_rowLabelWidth
= width
;
6203 void wxGrid::SetColLabelSize( int height
)
6205 height
= wxMax( height
, 0 );
6206 if ( height
!= m_colLabelHeight
)
6210 m_colLabelWin
->Show( FALSE
);
6211 m_cornerLabelWin
->Show( FALSE
);
6213 else if ( m_colLabelHeight
== 0 )
6215 m_colLabelWin
->Show( TRUE
);
6216 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6219 m_colLabelHeight
= height
;
6226 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6228 if ( m_labelBackgroundColour
!= colour
)
6230 m_labelBackgroundColour
= colour
;
6231 m_rowLabelWin
->SetBackgroundColour( colour
);
6232 m_colLabelWin
->SetBackgroundColour( colour
);
6233 m_cornerLabelWin
->SetBackgroundColour( colour
);
6235 if ( !GetBatchCount() )
6237 m_rowLabelWin
->Refresh();
6238 m_colLabelWin
->Refresh();
6239 m_cornerLabelWin
->Refresh();
6244 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6246 if ( m_labelTextColour
!= colour
)
6248 m_labelTextColour
= colour
;
6249 if ( !GetBatchCount() )
6251 m_rowLabelWin
->Refresh();
6252 m_colLabelWin
->Refresh();
6257 void wxGrid::SetLabelFont( const wxFont
& font
)
6260 if ( !GetBatchCount() )
6262 m_rowLabelWin
->Refresh();
6263 m_colLabelWin
->Refresh();
6267 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6269 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6271 m_rowLabelHorizAlign
= horiz
;
6274 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6276 m_rowLabelVertAlign
= vert
;
6279 if ( !GetBatchCount() )
6281 m_rowLabelWin
->Refresh();
6285 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6287 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6289 m_colLabelHorizAlign
= horiz
;
6292 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6294 m_colLabelVertAlign
= vert
;
6297 if ( !GetBatchCount() )
6299 m_colLabelWin
->Refresh();
6303 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6307 m_table
->SetRowLabelValue( row
, s
);
6308 if ( !GetBatchCount() )
6310 wxRect rect
= CellToRect( row
, 0);
6311 if ( rect
.height
> 0 )
6313 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6315 rect
.width
= m_rowLabelWidth
;
6316 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6322 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6326 m_table
->SetColLabelValue( col
, s
);
6327 if ( !GetBatchCount() )
6329 wxRect rect
= CellToRect( 0, col
);
6330 if ( rect
.width
> 0 )
6332 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6334 rect
.height
= m_colLabelHeight
;
6335 m_colLabelWin
->Refresh( TRUE
, &rect
);
6341 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6343 if ( m_gridLineColour
!= colour
)
6345 m_gridLineColour
= colour
;
6347 wxClientDC
dc( m_gridWin
);
6349 DrawAllGridLines( dc
, wxRegion() );
6353 void wxGrid::EnableGridLines( bool enable
)
6355 if ( enable
!= m_gridLinesEnabled
)
6357 m_gridLinesEnabled
= enable
;
6359 if ( !GetBatchCount() )
6363 wxClientDC
dc( m_gridWin
);
6365 DrawAllGridLines( dc
, wxRegion() );
6369 m_gridWin
->Refresh();
6376 int wxGrid::GetDefaultRowSize()
6378 return m_defaultRowHeight
;
6381 int wxGrid::GetRowSize( int row
)
6383 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6385 return GetRowHeight(row
);
6388 int wxGrid::GetDefaultColSize()
6390 return m_defaultColWidth
;
6393 int wxGrid::GetColSize( int col
)
6395 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6397 return GetColWidth(col
);
6400 // ============================================================================
6401 // access to the grid attributes: each of them has a default value in the grid
6402 // itself and may be overidden on a per-cell basis
6403 // ============================================================================
6405 // ----------------------------------------------------------------------------
6406 // setting default attributes
6407 // ----------------------------------------------------------------------------
6409 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6411 m_defaultCellAttr
->SetBackgroundColour(col
);
6413 m_gridWin
->SetBackgroundColour(col
);
6417 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6419 m_defaultCellAttr
->SetTextColour(col
);
6422 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6424 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6427 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6429 m_defaultCellAttr
->SetFont(font
);
6432 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6434 m_defaultCellAttr
->SetRenderer(renderer
);
6437 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6439 m_defaultCellAttr
->SetEditor(editor
);
6442 // ----------------------------------------------------------------------------
6443 // access to the default attrbiutes
6444 // ----------------------------------------------------------------------------
6446 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6448 return m_defaultCellAttr
->GetBackgroundColour();
6451 wxColour
wxGrid::GetDefaultCellTextColour()
6453 return m_defaultCellAttr
->GetTextColour();
6456 wxFont
wxGrid::GetDefaultCellFont()
6458 return m_defaultCellAttr
->GetFont();
6461 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6463 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6466 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6468 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6471 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6473 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6476 // ----------------------------------------------------------------------------
6477 // access to cell attributes
6478 // ----------------------------------------------------------------------------
6480 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6482 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6483 wxColour colour
= attr
->GetBackgroundColour();
6488 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6490 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6491 wxColour colour
= attr
->GetTextColour();
6496 wxFont
wxGrid::GetCellFont( int row
, int col
)
6498 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6499 wxFont font
= attr
->GetFont();
6504 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6506 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6507 attr
->GetAlignment(horiz
, vert
);
6511 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6513 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6514 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6519 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6521 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6522 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6527 bool wxGrid::IsReadOnly(int row
, int col
) const
6529 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6530 bool isReadOnly
= attr
->IsReadOnly();
6535 // ----------------------------------------------------------------------------
6536 // attribute support: cache, automatic provider creation, ...
6537 // ----------------------------------------------------------------------------
6539 bool wxGrid::CanHaveAttributes()
6546 return m_table
->CanHaveAttributes();
6549 void wxGrid::ClearAttrCache()
6551 if ( m_attrCache
.row
!= -1 )
6553 m_attrCache
.attr
->SafeDecRef();
6554 m_attrCache
.row
= -1;
6558 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6560 wxGrid
*self
= (wxGrid
*)this; // const_cast
6562 self
->ClearAttrCache();
6563 self
->m_attrCache
.row
= row
;
6564 self
->m_attrCache
.col
= col
;
6565 self
->m_attrCache
.attr
= attr
;
6569 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6571 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6573 *attr
= m_attrCache
.attr
;
6574 (*attr
)->SafeIncRef();
6576 #ifdef DEBUG_ATTR_CACHE
6577 gs_nAttrCacheHits
++;
6584 #ifdef DEBUG_ATTR_CACHE
6585 gs_nAttrCacheMisses
++;
6591 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6593 wxGridCellAttr
*attr
;
6594 if ( !LookupAttr(row
, col
, &attr
) )
6596 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6597 CacheAttr(row
, col
, attr
);
6601 attr
->SetDefAttr(m_defaultCellAttr
);
6605 attr
= m_defaultCellAttr
;
6612 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6614 wxGridCellAttr
*attr
;
6615 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6617 wxASSERT_MSG( m_table
,
6618 _T("we may only be called if CanHaveAttributes() "
6619 "returned TRUE and then m_table should be !NULL") );
6621 attr
= m_table
->GetAttr(row
, col
);
6624 attr
= new wxGridCellAttr
;
6626 // artificially inc the ref count to match DecRef() in caller
6629 m_table
->SetAttr(attr
, row
, col
);
6632 CacheAttr(row
, col
, attr
);
6634 attr
->SetDefAttr(m_defaultCellAttr
);
6638 // ----------------------------------------------------------------------------
6639 // setting cell attributes: this is forwarded to the table
6640 // ----------------------------------------------------------------------------
6642 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6644 if ( CanHaveAttributes() )
6646 m_table
->SetRowAttr(attr
, row
);
6654 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6656 if ( CanHaveAttributes() )
6658 m_table
->SetColAttr(attr
, col
);
6666 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6668 if ( CanHaveAttributes() )
6670 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6671 attr
->SetBackgroundColour(colour
);
6676 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6678 if ( CanHaveAttributes() )
6680 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6681 attr
->SetTextColour(colour
);
6686 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6688 if ( CanHaveAttributes() )
6690 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6691 attr
->SetFont(font
);
6696 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6698 if ( CanHaveAttributes() )
6700 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6701 attr
->SetAlignment(horiz
, vert
);
6706 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6708 if ( CanHaveAttributes() )
6710 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6711 attr
->SetRenderer(renderer
);
6716 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6718 if ( CanHaveAttributes() )
6720 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6721 attr
->SetEditor(editor
);
6726 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6728 if ( CanHaveAttributes() )
6730 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6731 attr
->SetReadOnly(isReadOnly
);
6736 // ----------------------------------------------------------------------------
6737 // Data type registration
6738 // ----------------------------------------------------------------------------
6740 void wxGrid::RegisterDataType(const wxString
& typeName
,
6741 wxGridCellRenderer
* renderer
,
6742 wxGridCellEditor
* editor
)
6744 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6748 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6750 wxString typeName
= m_table
->GetTypeName(row
, col
);
6751 return GetDefaultEditorForType(typeName
);
6754 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6756 wxString typeName
= m_table
->GetTypeName(row
, col
);
6757 return GetDefaultRendererForType(typeName
);
6761 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6763 int index
= m_typeRegistry
->FindDataType(typeName
);
6765 // Should we force the failure here or let it fallback to string handling???
6766 // wxFAIL_MSG(wxT("Unknown data type name"));
6769 return m_typeRegistry
->GetEditor(index
);
6773 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6775 int index
= m_typeRegistry
->FindDataType(typeName
);
6777 // Should we force the failure here or let it fallback to string handling???
6778 // wxFAIL_MSG(wxT("Unknown data type name"));
6781 return m_typeRegistry
->GetRenderer(index
);
6785 // ----------------------------------------------------------------------------
6787 // ----------------------------------------------------------------------------
6789 void wxGrid::EnableDragRowSize( bool enable
)
6791 m_canDragRowSize
= enable
;
6795 void wxGrid::EnableDragColSize( bool enable
)
6797 m_canDragColSize
= enable
;
6800 void wxGrid::EnableDragGridSize( bool enable
)
6802 m_canDragGridSize
= enable
;
6806 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6808 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6810 if ( resizeExistingRows
)
6818 void wxGrid::SetRowSize( int row
, int height
)
6820 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6822 if ( m_rowHeights
.IsEmpty() )
6824 // need to really create the array
6828 int h
= wxMax( 0, height
);
6829 int diff
= h
- m_rowHeights
[row
];
6831 m_rowHeights
[row
] = h
;
6833 for ( i
= row
; i
< m_numRows
; i
++ )
6835 m_rowBottoms
[i
] += diff
;
6840 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6842 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6844 if ( resizeExistingCols
)
6852 void wxGrid::SetColSize( int col
, int width
)
6854 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6856 // should we check that it's bigger than GetColMinimalWidth(col) here?
6858 if ( m_colWidths
.IsEmpty() )
6860 // need to really create the array
6864 int w
= wxMax( 0, width
);
6865 int diff
= w
- m_colWidths
[col
];
6866 m_colWidths
[col
] = w
;
6869 for ( i
= col
; i
< m_numCols
; i
++ )
6871 m_colRights
[i
] += diff
;
6877 void wxGrid::SetColMinimalWidth( int col
, int width
)
6879 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6882 int wxGrid::GetColMinimalWidth(int col
) const
6884 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6885 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6888 // ----------------------------------------------------------------------------
6890 // ----------------------------------------------------------------------------
6892 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6894 wxClientDC
dc(m_gridWin
);
6896 wxCoord width
, widthMax
= 0;
6897 for ( int row
= 0; row
< m_numRows
; row
++ )
6899 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6900 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6903 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6904 if ( width
> widthMax
)
6913 // now also compare with the column label width
6914 dc
.SetFont( GetLabelFont() );
6915 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6916 if ( width
> widthMax
)
6923 // empty column - give default width (notice that if widthMax is less
6924 // than default width but != 0, it's ok)
6925 widthMax
= m_defaultColWidth
;
6929 // leave some space around text
6933 SetColSize(col
, widthMax
);
6936 SetColMinimalWidth(col
, widthMax
);
6940 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
6942 int width
= m_rowLabelWidth
;
6944 for ( int col
= 0; col
< m_numCols
; col
++ )
6948 AutoSizeColumn(col
, setAsMin
);
6951 width
+= GetColWidth(col
);
6957 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
6959 int height
= m_colLabelHeight
;
6961 for ( int row
= 0; row
< m_numRows
; row
++ )
6963 // if ( !calcOnly ) AutoSizeRow(row, setAsMin) -- TODO
6965 height
+= GetRowHeight(row
);
6971 void wxGrid::AutoSize()
6974 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
6977 wxSize
wxGrid::DoGetBestSize() const
6979 // don't set sizes, only calculate them
6980 wxGrid
*self
= (wxGrid
*)this; // const_cast
6982 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
6983 self
->SetOrCalcRowSizes(TRUE
));
6991 // ----------------------------------------------------------------------------
6992 // cell value accessor functions
6993 // ----------------------------------------------------------------------------
6995 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6999 m_table
->SetValue( row
, col
, s
.c_str() );
7000 if ( !GetBatchCount() )
7002 wxClientDC
dc( m_gridWin
);
7004 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7007 if ( m_currentCellCoords
.GetRow() == row
&&
7008 m_currentCellCoords
.GetCol() == col
&&
7009 IsCellEditControlEnabled())
7011 HideCellEditControl();
7012 ShowCellEditControl(); // will reread data from table
7019 // ------ Block, row and col selection
7022 void wxGrid::SelectRow( int row
, bool addToSelected
)
7026 if ( IsSelection() && addToSelected
)
7029 bool need_refresh
[4];
7033 need_refresh
[3] = FALSE
;
7037 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7038 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7039 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7040 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7044 need_refresh
[0] = TRUE
;
7045 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
7046 wxGridCellCoords ( oldTop
- 1,
7048 m_selectedTopLeft
.SetRow( row
);
7053 need_refresh
[1] = TRUE
;
7054 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
7055 wxGridCellCoords ( oldBottom
,
7058 m_selectedTopLeft
.SetCol( 0 );
7061 if ( oldBottom
< row
)
7063 need_refresh
[2] = TRUE
;
7064 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
7065 wxGridCellCoords ( row
,
7067 m_selectedBottomRight
.SetRow( row
);
7070 if ( oldRight
< m_numCols
- 1 )
7072 need_refresh
[3] = TRUE
;
7073 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7075 wxGridCellCoords ( oldBottom
,
7077 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
7080 for (i
= 0; i
< 4; i
++ )
7081 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7082 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7086 r
= SelectionToDeviceRect();
7088 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7090 m_selectedTopLeft
.Set( row
, 0 );
7091 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7092 r
= SelectionToDeviceRect();
7093 m_gridWin
->Refresh( FALSE
, &r
);
7096 wxGridRangeSelectEvent
gridEvt( GetId(),
7097 wxEVT_GRID_RANGE_SELECT
,
7100 m_selectedBottomRight
);
7102 GetEventHandler()->ProcessEvent(gridEvt
);
7106 void wxGrid::SelectCol( int col
, bool addToSelected
)
7108 if ( IsSelection() && addToSelected
)
7111 bool need_refresh
[4];
7115 need_refresh
[3] = FALSE
;
7118 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7119 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7120 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7121 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7123 if ( oldLeft
> col
)
7125 need_refresh
[0] = TRUE
;
7126 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7127 wxGridCellCoords ( m_numRows
- 1,
7129 m_selectedTopLeft
.SetCol( col
);
7134 need_refresh
[1] = TRUE
;
7135 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7136 wxGridCellCoords ( oldTop
- 1,
7138 m_selectedTopLeft
.SetRow( 0 );
7141 if ( oldRight
< col
)
7143 need_refresh
[2] = TRUE
;
7144 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7145 wxGridCellCoords ( m_numRows
- 1,
7147 m_selectedBottomRight
.SetCol( col
);
7150 if ( oldBottom
< m_numRows
- 1 )
7152 need_refresh
[3] = TRUE
;
7153 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7155 wxGridCellCoords ( m_numRows
- 1,
7157 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7160 for (i
= 0; i
< 4; i
++ )
7161 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7162 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7168 r
= SelectionToDeviceRect();
7170 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7172 m_selectedTopLeft
.Set( 0, col
);
7173 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7174 r
= SelectionToDeviceRect();
7175 m_gridWin
->Refresh( FALSE
, &r
);
7178 wxGridRangeSelectEvent
gridEvt( GetId(),
7179 wxEVT_GRID_RANGE_SELECT
,
7182 m_selectedBottomRight
);
7184 GetEventHandler()->ProcessEvent(gridEvt
);
7188 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7191 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7193 if ( topRow
> bottomRow
)
7200 if ( leftCol
> rightCol
)
7207 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7208 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7210 if ( m_selectedTopLeft
!= updateTopLeft
||
7211 m_selectedBottomRight
!= updateBottomRight
)
7213 // Compute two optimal update rectangles:
7214 // Either one rectangle is a real subset of the
7215 // other, or they are (almost) disjoint!
7217 bool need_refresh
[4];
7221 need_refresh
[3] = FALSE
;
7224 // Store intermediate values
7225 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7226 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7227 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7228 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7230 // Determine the outer/inner coordinates.
7231 if (oldLeft
> leftCol
)
7237 if (oldTop
> topRow
)
7243 if (oldRight
< rightCol
)
7246 oldRight
= rightCol
;
7249 if (oldBottom
< bottomRow
)
7252 oldBottom
= bottomRow
;
7256 // Now, either the stuff marked old is the outer
7257 // rectangle or we don't have a situation where one
7258 // is contained in the other.
7260 if ( oldLeft
< leftCol
)
7262 need_refresh
[0] = TRUE
;
7263 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7265 wxGridCellCoords ( oldBottom
,
7269 if ( oldTop
< topRow
)
7271 need_refresh
[1] = TRUE
;
7272 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7274 wxGridCellCoords ( topRow
- 1,
7278 if ( oldRight
> rightCol
)
7280 need_refresh
[2] = TRUE
;
7281 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7283 wxGridCellCoords ( oldBottom
,
7287 if ( oldBottom
> bottomRow
)
7289 need_refresh
[3] = TRUE
;
7290 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7292 wxGridCellCoords ( oldBottom
,
7298 m_selectedTopLeft
= updateTopLeft
;
7299 m_selectedBottomRight
= updateBottomRight
;
7301 // various Refresh() calls
7302 for (i
= 0; i
< 4; i
++ )
7303 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7304 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7307 // only generate an event if the block is not being selected by
7308 // dragging the mouse (in which case the event will be generated in
7309 // the mouse event handler)
7310 if ( !m_isDragging
)
7312 wxGridRangeSelectEvent
gridEvt( GetId(),
7313 wxEVT_GRID_RANGE_SELECT
,
7316 m_selectedBottomRight
);
7318 GetEventHandler()->ProcessEvent(gridEvt
);
7322 void wxGrid::SelectAll()
7324 m_selectedTopLeft
.Set( 0, 0 );
7325 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7327 m_gridWin
->Refresh();
7331 void wxGrid::ClearSelection()
7333 m_selectedTopLeft
= wxGridNoCellCoords
;
7334 m_selectedBottomRight
= wxGridNoCellCoords
;
7338 // This function returns the rectangle that encloses the given block
7339 // in device coords clipped to the client size of the grid window.
7341 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7342 const wxGridCellCoords
&bottomRight
)
7344 wxRect
rect( wxGridNoCellRect
);
7347 cellRect
= CellToRect( topLeft
);
7348 if ( cellRect
!= wxGridNoCellRect
)
7354 rect
= wxRect( 0, 0, 0, 0 );
7357 cellRect
= CellToRect( bottomRight
);
7358 if ( cellRect
!= wxGridNoCellRect
)
7364 return wxGridNoCellRect
;
7367 // convert to scrolled coords
7369 int left
, top
, right
, bottom
;
7370 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7371 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7374 m_gridWin
->GetClientSize( &cw
, &ch
);
7376 rect
.SetLeft( wxMax(0, left
) );
7377 rect
.SetTop( wxMax(0, top
) );
7378 rect
.SetRight( wxMin(cw
, right
) );
7379 rect
.SetBottom( wxMin(ch
, bottom
) );
7387 // ------ Grid event classes
7390 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7392 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7393 int row
, int col
, int x
, int y
,
7394 bool control
, bool shift
, bool alt
, bool meta
)
7395 : wxNotifyEvent( type
, id
)
7401 m_control
= control
;
7406 SetEventObject(obj
);
7410 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7412 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7413 int rowOrCol
, int x
, int y
,
7414 bool control
, bool shift
, bool alt
, bool meta
)
7415 : wxNotifyEvent( type
, id
)
7417 m_rowOrCol
= rowOrCol
;
7420 m_control
= control
;
7425 SetEventObject(obj
);
7429 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7431 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7432 const wxGridCellCoords
& topLeft
,
7433 const wxGridCellCoords
& bottomRight
,
7434 bool control
, bool shift
, bool alt
, bool meta
)
7435 : wxNotifyEvent( type
, id
)
7437 m_topLeft
= topLeft
;
7438 m_bottomRight
= bottomRight
;
7439 m_control
= control
;
7444 SetEventObject(obj
);
7448 #endif // ifndef wxUSE_NEW_GRID