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 const wxColour
& wxGridCellAttr::GetTextColour() const
1359 if (HasTextColour())
1363 else if (m_defGridAttr
!= this)
1365 return m_defGridAttr
->GetTextColour();
1369 wxFAIL_MSG(wxT("Missing default cell attribute"));
1370 return wxNullColour
;
1375 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1377 if (HasBackgroundColour())
1379 else if (m_defGridAttr
!= this)
1380 return m_defGridAttr
->GetBackgroundColour();
1383 wxFAIL_MSG(wxT("Missing default cell attribute"));
1384 return wxNullColour
;
1389 const wxFont
& wxGridCellAttr::GetFont() const
1393 else if (m_defGridAttr
!= this)
1394 return m_defGridAttr
->GetFont();
1397 wxFAIL_MSG(wxT("Missing default cell attribute"));
1403 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1407 if ( hAlign
) *hAlign
= m_hAlign
;
1408 if ( vAlign
) *vAlign
= m_vAlign
;
1410 else if (m_defGridAttr
!= this)
1411 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1414 wxFAIL_MSG(wxT("Missing default cell attribute"));
1419 // GetRenderer and GetEditor use a slightly different decision path about
1420 // which attribute to use. If a non-default attr object has one then it is
1421 // used, otherwise the default editor or renderer is fetched from the grid and
1422 // used. It should be the default for the data type of the cell. If it is
1423 // NULL (because the table has a type that the grid does not have in its
1424 // registry,) then the grid's default editor or renderer is used.
1426 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1428 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1429 return m_renderer
; // use local attribute
1431 wxGridCellRenderer
* renderer
= NULL
;
1432 if (grid
) // get renderer for the data type
1433 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1436 // if we still don't have one then use the grid default
1437 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1440 wxFAIL_MSG(wxT("Missing default cell attribute"));
1445 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1447 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1448 return m_editor
; // use local attribute
1450 wxGridCellEditor
* editor
= NULL
;
1451 if (grid
) // get renderer for the data type
1452 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1455 // if we still don't have one then use the grid default
1456 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1459 wxFAIL_MSG(wxT("Missing default cell attribute"));
1464 // ----------------------------------------------------------------------------
1465 // wxGridCellAttrData
1466 // ----------------------------------------------------------------------------
1468 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1470 int n
= FindIndex(row
, col
);
1471 if ( n
== wxNOT_FOUND
)
1473 // add the attribute
1474 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1480 // change the attribute
1481 m_attrs
[(size_t)n
].attr
= attr
;
1485 // remove this attribute
1486 m_attrs
.RemoveAt((size_t)n
);
1491 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1493 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1495 int n
= FindIndex(row
, col
);
1496 if ( n
!= wxNOT_FOUND
)
1498 attr
= m_attrs
[(size_t)n
].attr
;
1505 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1507 size_t count
= m_attrs
.GetCount();
1508 for ( size_t n
= 0; n
< count
; n
++ )
1510 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1511 wxCoord row
= coords
.GetRow();
1512 if ((size_t)row
>= pos
)
1516 // If rows inserted, include row counter where necessary
1517 coords
.SetRow(row
+ numRows
);
1519 else if (numRows
< 0)
1521 // If rows deleted ...
1522 if ((size_t)row
>= pos
- numRows
)
1524 // ...either decrement row counter (if row still exists)...
1525 coords
.SetRow(row
+ numRows
);
1529 // ...or remove the attribute
1530 m_attrs
.RemoveAt((size_t)n
);
1538 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1540 size_t count
= m_attrs
.GetCount();
1541 for ( size_t n
= 0; n
< count
; n
++ )
1543 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1544 wxCoord col
= coords
.GetCol();
1545 if ( (size_t)col
>= pos
)
1549 // If rows inserted, include row counter where necessary
1550 coords
.SetCol(col
+ numCols
);
1552 else if (numCols
< 0)
1554 // If rows deleted ...
1555 if ((size_t)col
>= pos
- numCols
)
1557 // ...either decrement row counter (if row still exists)...
1558 coords
.SetCol(col
+ numCols
);
1562 // ...or remove the attribute
1563 m_attrs
.RemoveAt((size_t)n
);
1571 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1573 size_t count
= m_attrs
.GetCount();
1574 for ( size_t n
= 0; n
< count
; n
++ )
1576 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1577 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1586 // ----------------------------------------------------------------------------
1587 // wxGridRowOrColAttrData
1588 // ----------------------------------------------------------------------------
1590 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1592 size_t count
= m_attrs
.Count();
1593 for ( size_t n
= 0; n
< count
; n
++ )
1595 m_attrs
[n
]->DecRef();
1599 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1601 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1603 int n
= m_rowsOrCols
.Index(rowOrCol
);
1604 if ( n
!= wxNOT_FOUND
)
1606 attr
= m_attrs
[(size_t)n
];
1613 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1615 int n
= m_rowsOrCols
.Index(rowOrCol
);
1616 if ( n
== wxNOT_FOUND
)
1618 // add the attribute
1619 m_rowsOrCols
.Add(rowOrCol
);
1626 // change the attribute
1627 m_attrs
[(size_t)n
] = attr
;
1631 // remove this attribute
1632 m_attrs
[(size_t)n
]->DecRef();
1633 m_rowsOrCols
.RemoveAt((size_t)n
);
1634 m_attrs
.RemoveAt((size_t)n
);
1639 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1641 size_t count
= m_attrs
.GetCount();
1642 for ( size_t n
= 0; n
< count
; n
++ )
1644 int & rowOrCol
= m_rowsOrCols
[n
];
1645 if ( (size_t)rowOrCol
>= pos
)
1647 if ( numRowsOrCols
> 0 )
1649 // If rows inserted, include row counter where necessary
1650 rowOrCol
+= numRowsOrCols
;
1652 else if ( numRowsOrCols
< 0)
1654 // If rows deleted, either decrement row counter (if row still exists)
1655 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1656 rowOrCol
+= numRowsOrCols
;
1659 m_rowsOrCols
.RemoveAt((size_t)n
);
1660 m_attrs
.RemoveAt((size_t)n
);
1668 // ----------------------------------------------------------------------------
1669 // wxGridCellAttrProvider
1670 // ----------------------------------------------------------------------------
1672 wxGridCellAttrProvider::wxGridCellAttrProvider()
1674 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1677 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1682 void wxGridCellAttrProvider::InitData()
1684 m_data
= new wxGridCellAttrProviderData
;
1687 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1689 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1692 // first look for the attribute of this specific cell
1693 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1697 // then look for the col attr (col attributes are more common than
1698 // the row ones, hence they have priority)
1699 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1704 // finally try the row attributes
1705 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1712 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1718 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1721 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1726 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1729 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1734 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1737 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1741 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1743 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1747 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1751 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1753 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1757 // ----------------------------------------------------------------------------
1758 // wxGridTypeRegistry
1759 // ----------------------------------------------------------------------------
1761 wxGridTypeRegistry::~wxGridTypeRegistry()
1763 size_t count
= m_typeinfo
.Count();
1764 for ( size_t i
= 0; i
< count
; i
++ )
1765 delete m_typeinfo
[i
];
1769 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1770 wxGridCellRenderer
* renderer
,
1771 wxGridCellEditor
* editor
)
1774 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1776 // is it already registered?
1777 if ((loc
= FindDataType(typeName
)) != -1) {
1778 delete m_typeinfo
[loc
];
1779 m_typeinfo
[loc
] = info
;
1782 m_typeinfo
.Add(info
);
1786 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1790 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1791 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1800 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1802 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1806 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1808 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1812 // ----------------------------------------------------------------------------
1814 // ----------------------------------------------------------------------------
1816 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1819 wxGridTableBase::wxGridTableBase()
1821 m_view
= (wxGrid
*) NULL
;
1822 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1825 wxGridTableBase::~wxGridTableBase()
1827 delete m_attrProvider
;
1830 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1832 delete m_attrProvider
;
1833 m_attrProvider
= attrProvider
;
1836 bool wxGridTableBase::CanHaveAttributes()
1838 if ( ! GetAttrProvider() )
1840 // use the default attr provider by default
1841 SetAttrProvider(new wxGridCellAttrProvider
);
1846 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1848 if ( m_attrProvider
)
1849 return m_attrProvider
->GetAttr(row
, col
);
1851 return (wxGridCellAttr
*)NULL
;
1854 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1856 if ( m_attrProvider
)
1858 m_attrProvider
->SetAttr(attr
, row
, col
);
1862 // as we take ownership of the pointer and don't store it, we must
1868 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1870 if ( m_attrProvider
)
1872 m_attrProvider
->SetRowAttr(attr
, row
);
1876 // as we take ownership of the pointer and don't store it, we must
1882 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1884 if ( m_attrProvider
)
1886 m_attrProvider
->SetColAttr(attr
, col
);
1890 // as we take ownership of the pointer and don't store it, we must
1896 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1898 if ( m_attrProvider
)
1900 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1904 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1906 if ( m_attrProvider
)
1908 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1912 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1914 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1915 "but your derived table class does not override this function") );
1920 bool wxGridTableBase::AppendRows( size_t numRows
)
1922 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1923 "but your derived table class does not override this function"));
1928 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1930 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1931 "but your derived table class does not override this function"));
1936 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1938 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1939 "but your derived table class does not override this function"));
1944 bool wxGridTableBase::AppendCols( size_t numCols
)
1946 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1947 "but your derived table class does not override this function"));
1952 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1954 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1955 "but your derived table class does not override this function"));
1961 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1964 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1965 // how much it makes sense to us geeks.
1969 wxString
wxGridTableBase::GetColLabelValue( int col
)
1971 // default col labels are:
1972 // cols 0 to 25 : A-Z
1973 // cols 26 to 675 : AA-ZZ
1978 for ( n
= 1; ; n
++ )
1980 s
+= (_T('A') + (wxChar
)( col%26
));
1982 if ( col
< 0 ) break;
1985 // reverse the string...
1987 for ( i
= 0; i
< n
; i
++ )
1996 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1998 return wxGRID_VALUE_STRING
;
2001 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2002 const wxString
& typeName
)
2004 return typeName
== wxGRID_VALUE_STRING
;
2007 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2009 return CanGetValueAs(row
, col
, typeName
);
2012 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2017 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2022 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2027 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2028 long WXUNUSED(value
) )
2032 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2033 double WXUNUSED(value
) )
2037 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2038 bool WXUNUSED(value
) )
2043 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2044 const wxString
& WXUNUSED(typeName
) )
2049 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2050 const wxString
& WXUNUSED(typeName
),
2051 void* WXUNUSED(value
) )
2056 //////////////////////////////////////////////////////////////////////
2058 // Message class for the grid table to send requests and notifications
2062 wxGridTableMessage::wxGridTableMessage()
2064 m_table
= (wxGridTableBase
*) NULL
;
2070 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2071 int commandInt1
, int commandInt2
)
2075 m_comInt1
= commandInt1
;
2076 m_comInt2
= commandInt2
;
2081 //////////////////////////////////////////////////////////////////////
2083 // A basic grid table for string data. An object of this class will
2084 // created by wxGrid if you don't specify an alternative table class.
2087 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2089 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2091 wxGridStringTable::wxGridStringTable()
2096 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2101 m_data
.Alloc( numRows
);
2104 sa
.Alloc( numCols
);
2105 for ( col
= 0; col
< numCols
; col
++ )
2107 sa
.Add( wxEmptyString
);
2110 for ( row
= 0; row
< numRows
; row
++ )
2116 wxGridStringTable::~wxGridStringTable()
2120 long wxGridStringTable::GetNumberRows()
2122 return m_data
.GetCount();
2125 long wxGridStringTable::GetNumberCols()
2127 if ( m_data
.GetCount() > 0 )
2128 return m_data
[0].GetCount();
2133 wxString
wxGridStringTable::GetValue( int row
, int col
)
2135 // TODO: bounds checking
2137 return m_data
[row
][col
];
2140 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2142 // TODO: bounds checking
2144 m_data
[row
][col
] = value
;
2147 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2149 // TODO: bounds checking
2151 return (m_data
[row
][col
] == wxEmptyString
);
2155 void wxGridStringTable::Clear()
2158 int numRows
, numCols
;
2160 numRows
= m_data
.GetCount();
2163 numCols
= m_data
[0].GetCount();
2165 for ( row
= 0; row
< numRows
; row
++ )
2167 for ( col
= 0; col
< numCols
; col
++ )
2169 m_data
[row
][col
] = wxEmptyString
;
2176 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2180 size_t curNumRows
= m_data
.GetCount();
2181 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2183 if ( pos
>= curNumRows
)
2185 return AppendRows( numRows
);
2189 sa
.Alloc( curNumCols
);
2190 for ( col
= 0; col
< curNumCols
; col
++ )
2192 sa
.Add( wxEmptyString
);
2195 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2197 m_data
.Insert( sa
, row
);
2199 UpdateAttrRows( pos
, numRows
);
2202 wxGridTableMessage
msg( this,
2203 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2207 GetView()->ProcessTableMessage( msg
);
2213 bool wxGridStringTable::AppendRows( size_t numRows
)
2217 size_t curNumRows
= m_data
.GetCount();
2218 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2221 if ( curNumCols
> 0 )
2223 sa
.Alloc( curNumCols
);
2224 for ( col
= 0; col
< curNumCols
; col
++ )
2226 sa
.Add( wxEmptyString
);
2230 for ( row
= 0; row
< numRows
; row
++ )
2237 wxGridTableMessage
msg( this,
2238 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2241 GetView()->ProcessTableMessage( msg
);
2247 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2251 size_t curNumRows
= m_data
.GetCount();
2253 if ( pos
>= curNumRows
)
2256 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2257 "Pos value is invalid for present table with %d rows",
2258 pos
, numRows
, curNumRows
);
2259 wxFAIL_MSG( wxT(errmsg
) );
2263 if ( numRows
> curNumRows
- pos
)
2265 numRows
= curNumRows
- pos
;
2268 if ( numRows
>= curNumRows
)
2270 m_data
.Empty(); // don't release memory just yet
2274 for ( n
= 0; n
< numRows
; n
++ )
2276 m_data
.Remove( pos
);
2279 UpdateAttrRows( pos
, -((int)numRows
) );
2282 wxGridTableMessage
msg( this,
2283 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2287 GetView()->ProcessTableMessage( msg
);
2293 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2297 size_t curNumRows
= m_data
.GetCount();
2298 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2300 if ( pos
>= curNumCols
)
2302 return AppendCols( numCols
);
2305 for ( row
= 0; row
< curNumRows
; row
++ )
2307 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2309 m_data
[row
].Insert( wxEmptyString
, col
);
2312 UpdateAttrCols( pos
, numCols
);
2315 wxGridTableMessage
msg( this,
2316 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2320 GetView()->ProcessTableMessage( msg
);
2326 bool wxGridStringTable::AppendCols( size_t numCols
)
2330 size_t curNumRows
= m_data
.GetCount();
2333 // TODO: something better than this ?
2335 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2336 "Call AppendRows() first") );
2340 for ( row
= 0; row
< curNumRows
; row
++ )
2342 for ( n
= 0; n
< numCols
; n
++ )
2344 m_data
[row
].Add( wxEmptyString
);
2350 wxGridTableMessage
msg( this,
2351 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2354 GetView()->ProcessTableMessage( msg
);
2360 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2364 size_t curNumRows
= m_data
.GetCount();
2365 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2367 if ( pos
>= curNumCols
)
2370 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2371 "Pos value is invalid for present table with %d cols",
2372 pos
, numCols
, curNumCols
);
2373 wxFAIL_MSG( wxT( errmsg
) );
2377 if ( numCols
> curNumCols
- pos
)
2379 numCols
= curNumCols
- pos
;
2382 for ( row
= 0; row
< curNumRows
; row
++ )
2384 if ( numCols
>= curNumCols
)
2386 m_data
[row
].Clear();
2390 for ( n
= 0; n
< numCols
; n
++ )
2392 m_data
[row
].Remove( pos
);
2396 UpdateAttrCols( pos
, -((int)numCols
) );
2399 wxGridTableMessage
msg( this,
2400 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2404 GetView()->ProcessTableMessage( msg
);
2410 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2412 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2414 // using default label
2416 return wxGridTableBase::GetRowLabelValue( row
);
2420 return m_rowLabels
[ row
];
2424 wxString
wxGridStringTable::GetColLabelValue( int col
)
2426 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2428 // using default label
2430 return wxGridTableBase::GetColLabelValue( col
);
2434 return m_colLabels
[ col
];
2438 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2440 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2442 int n
= m_rowLabels
.GetCount();
2444 for ( i
= n
; i
<= row
; i
++ )
2446 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2450 m_rowLabels
[row
] = value
;
2453 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2455 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2457 int n
= m_colLabels
.GetCount();
2459 for ( i
= n
; i
<= col
; i
++ )
2461 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2465 m_colLabels
[col
] = value
;
2470 //////////////////////////////////////////////////////////////////////
2471 //////////////////////////////////////////////////////////////////////
2473 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2475 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2476 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2477 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2478 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2481 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2483 const wxPoint
&pos
, const wxSize
&size
)
2484 : wxWindow( parent
, id
, pos
, size
)
2489 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2493 // NO - don't do this because it will set both the x and y origin
2494 // coords to match the parent scrolled window and we just want to
2495 // set the y coord - MB
2497 // m_owner->PrepareDC( dc );
2500 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2501 dc
.SetDeviceOrigin( 0, -y
);
2503 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2504 m_owner
->DrawRowLabels( dc
);
2508 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2510 m_owner
->ProcessRowLabelMouseEvent( event
);
2514 // This seems to be required for wxMotif otherwise the mouse
2515 // cursor must be in the cell edit control to get key events
2517 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2519 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2524 //////////////////////////////////////////////////////////////////////
2526 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2528 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2529 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2530 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2531 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2534 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2536 const wxPoint
&pos
, const wxSize
&size
)
2537 : wxWindow( parent
, id
, pos
, size
)
2542 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2546 // NO - don't do this because it will set both the x and y origin
2547 // coords to match the parent scrolled window and we just want to
2548 // set the x coord - MB
2550 // m_owner->PrepareDC( dc );
2553 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2554 dc
.SetDeviceOrigin( -x
, 0 );
2556 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2557 m_owner
->DrawColLabels( dc
);
2561 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2563 m_owner
->ProcessColLabelMouseEvent( event
);
2567 // This seems to be required for wxMotif otherwise the mouse
2568 // cursor must be in the cell edit control to get key events
2570 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2572 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2577 //////////////////////////////////////////////////////////////////////
2579 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2581 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2582 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2583 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2584 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2587 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2589 const wxPoint
&pos
, const wxSize
&size
)
2590 : wxWindow( parent
, id
, pos
, size
)
2595 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2599 int client_height
= 0;
2600 int client_width
= 0;
2601 GetClientSize( &client_width
, &client_height
);
2603 dc
.SetPen( *wxBLACK_PEN
);
2604 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2605 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2607 dc
.SetPen( *wxWHITE_PEN
);
2608 dc
.DrawLine( 0, 0, client_width
, 0 );
2609 dc
.DrawLine( 0, 0, 0, client_height
);
2613 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2615 m_owner
->ProcessCornerLabelMouseEvent( event
);
2619 // This seems to be required for wxMotif otherwise the mouse
2620 // cursor must be in the cell edit control to get key events
2622 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2624 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2629 //////////////////////////////////////////////////////////////////////
2631 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2633 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2634 EVT_PAINT( wxGridWindow::OnPaint
)
2635 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2636 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2637 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2640 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2641 wxGridRowLabelWindow
*rowLblWin
,
2642 wxGridColLabelWindow
*colLblWin
,
2643 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2644 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
2647 m_rowLabelWin
= rowLblWin
;
2648 m_colLabelWin
= colLblWin
;
2649 SetBackgroundColour( "WHITE" );
2653 wxGridWindow::~wxGridWindow()
2658 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2660 wxPaintDC
dc( this );
2661 m_owner
->PrepareDC( dc
);
2662 wxRegion reg
= GetUpdateRegion();
2663 m_owner
->CalcCellsExposed( reg
);
2664 m_owner
->DrawGridCellArea( dc
);
2665 #if WXGRID_DRAW_LINES
2666 m_owner
->DrawAllGridLines( dc
, reg
);
2668 m_owner
->DrawGridSpace( dc
);
2669 m_owner
->DrawHighlight( dc
);
2673 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2675 wxPanel::ScrollWindow( dx
, dy
, rect
);
2676 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2677 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2681 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2683 m_owner
->ProcessGridCellMouseEvent( event
);
2687 // This seems to be required for wxMotif otherwise the mouse
2688 // cursor must be in the cell edit control to get key events
2690 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2692 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2696 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2701 //////////////////////////////////////////////////////////////////////
2704 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2706 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2707 EVT_PAINT( wxGrid::OnPaint
)
2708 EVT_SIZE( wxGrid::OnSize
)
2709 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2710 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2713 wxGrid::wxGrid( wxWindow
*parent
,
2718 const wxString
& name
)
2719 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2720 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2729 m_defaultCellAttr
->SafeDecRef();
2731 #ifdef DEBUG_ATTR_CACHE
2732 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2733 wxPrintf(_T("wxGrid attribute cache statistics: "
2734 "total: %u, hits: %u (%u%%)\n"),
2735 total
, gs_nAttrCacheHits
,
2736 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2742 delete m_typeRegistry
;
2747 // ----- internal init and update functions
2750 void wxGrid::Create()
2752 m_created
= FALSE
; // set to TRUE by CreateGrid
2753 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2755 m_table
= (wxGridTableBase
*) NULL
;
2758 m_cellEditCtrlEnabled
= FALSE
;
2760 m_defaultCellAttr
= new wxGridCellAttr
;
2761 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2763 // Set default cell attributes
2764 m_defaultCellAttr
->SetFont(GetFont());
2765 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2766 m_defaultCellAttr
->SetTextColour(
2767 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2768 m_defaultCellAttr
->SetBackgroundColour(
2769 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2770 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2771 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2776 m_currentCellCoords
= wxGridNoCellCoords
;
2778 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2779 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2781 // data type registration: register all standard data types
2782 // TODO: may be allow the app to selectively disable some of them?
2783 m_typeRegistry
= new wxGridTypeRegistry
;
2784 RegisterDataType(wxGRID_VALUE_STRING
,
2785 new wxGridCellStringRenderer
,
2786 new wxGridCellTextEditor
);
2787 RegisterDataType(wxGRID_VALUE_BOOL
,
2788 new wxGridCellBoolRenderer
,
2789 new wxGridCellBoolEditor
);
2790 RegisterDataType(wxGRID_VALUE_NUMBER
,
2791 new wxGridCellNumberRenderer
,
2792 new wxGridCellNumberEditor
);
2794 // subwindow components that make up the wxGrid
2795 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2800 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2805 m_colLabelWin
= new wxGridColLabelWindow( this,
2810 m_gridWin
= new wxGridWindow( this,
2817 SetTargetWindow( m_gridWin
);
2821 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2825 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2830 m_numRows
= numRows
;
2831 m_numCols
= numCols
;
2833 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2834 m_table
->SetView( this );
2843 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2847 // RD: Actually, this should probably be allowed. I think it would be
2848 // nice to be able to switch multiple Tables in and out of a single
2849 // View at runtime. Is there anything in the implmentation that would
2852 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2857 m_numRows
= table
->GetNumberRows();
2858 m_numCols
= table
->GetNumberCols();
2861 m_table
->SetView( this );
2874 if ( m_numRows
<= 0 )
2875 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2877 if ( m_numCols
<= 0 )
2878 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2880 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2881 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2883 if ( m_rowLabelWin
)
2885 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2889 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2892 m_labelTextColour
= wxColour( _T("BLACK") );
2895 m_attrCache
.row
= -1;
2897 // TODO: something better than this ?
2899 m_labelFont
= this->GetFont();
2900 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2902 m_rowLabelHorizAlign
= wxLEFT
;
2903 m_rowLabelVertAlign
= wxCENTRE
;
2905 m_colLabelHorizAlign
= wxCENTRE
;
2906 m_colLabelVertAlign
= wxTOP
;
2908 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2909 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2911 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2912 m_defaultRowHeight
+= 8;
2914 m_defaultRowHeight
+= 4;
2917 m_gridLineColour
= wxColour( 128, 128, 255 );
2918 m_gridLinesEnabled
= TRUE
;
2920 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2921 m_winCapture
= (wxWindow
*)NULL
;
2922 m_canDragRowSize
= TRUE
;
2923 m_canDragColSize
= TRUE
;
2924 m_canDragGridSize
= TRUE
;
2926 m_dragRowOrCol
= -1;
2927 m_isDragging
= FALSE
;
2928 m_startDragPos
= wxDefaultPosition
;
2930 m_waitForSlowClick
= FALSE
;
2932 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2933 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2935 m_currentCellCoords
= wxGridNoCellCoords
;
2937 m_selectedTopLeft
= wxGridNoCellCoords
;
2938 m_selectedBottomRight
= wxGridNoCellCoords
;
2939 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2940 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2942 m_editable
= TRUE
; // default for whole grid
2944 m_inOnKeyDown
= FALSE
;
2953 // ----------------------------------------------------------------------------
2954 // the idea is to call these functions only when necessary because they create
2955 // quite big arrays which eat memory mostly unnecessary - in particular, if
2956 // default widths/heights are used for all rows/columns, we may not use these
2959 // with some extra code, it should be possible to only store the
2960 // widths/heights different from default ones but this will be done later...
2961 // ----------------------------------------------------------------------------
2963 void wxGrid::InitRowHeights()
2965 m_rowHeights
.Empty();
2966 m_rowBottoms
.Empty();
2968 m_rowHeights
.Alloc( m_numRows
);
2969 m_rowBottoms
.Alloc( m_numRows
);
2972 for ( int i
= 0; i
< m_numRows
; i
++ )
2974 m_rowHeights
.Add( m_defaultRowHeight
);
2975 rowBottom
+= m_defaultRowHeight
;
2976 m_rowBottoms
.Add( rowBottom
);
2980 void wxGrid::InitColWidths()
2982 m_colWidths
.Empty();
2983 m_colRights
.Empty();
2985 m_colWidths
.Alloc( m_numCols
);
2986 m_colRights
.Alloc( m_numCols
);
2988 for ( int i
= 0; i
< m_numCols
; i
++ )
2990 m_colWidths
.Add( m_defaultColWidth
);
2991 colRight
+= m_defaultColWidth
;
2992 m_colRights
.Add( colRight
);
2996 int wxGrid::GetColWidth(int col
) const
2998 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3001 int wxGrid::GetColLeft(int col
) const
3003 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3004 : m_colRights
[col
] - m_colWidths
[col
];
3007 int wxGrid::GetColRight(int col
) const
3009 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3013 int wxGrid::GetRowHeight(int row
) const
3015 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3018 int wxGrid::GetRowTop(int row
) const
3020 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3021 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3024 int wxGrid::GetRowBottom(int row
) const
3026 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3027 : m_rowBottoms
[row
];
3030 void wxGrid::CalcDimensions()
3033 GetClientSize( &cw
, &ch
);
3035 if ( m_numRows
> 0 && m_numCols
> 0 )
3037 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3038 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3040 // TODO: restore the scroll position that we had before sizing
3043 GetViewStart( &x
, &y
);
3044 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3045 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3051 void wxGrid::CalcWindowSizes()
3054 GetClientSize( &cw
, &ch
);
3056 if ( m_cornerLabelWin
->IsShown() )
3057 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3059 if ( m_colLabelWin
->IsShown() )
3060 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3062 if ( m_rowLabelWin
->IsShown() )
3063 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3065 if ( m_gridWin
->IsShown() )
3066 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3070 // this is called when the grid table sends a message to say that it
3071 // has been redimensioned
3073 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3077 // if we were using the default widths/heights so far, we must change them
3079 if ( m_colWidths
.IsEmpty() )
3084 if ( m_rowHeights
.IsEmpty() )
3089 switch ( msg
.GetId() )
3091 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3093 size_t pos
= msg
.GetCommandInt();
3094 int numRows
= msg
.GetCommandInt2();
3095 for ( i
= 0; i
< numRows
; i
++ )
3097 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3098 m_rowBottoms
.Insert( 0, pos
);
3100 m_numRows
+= numRows
;
3103 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3105 for ( i
= pos
; i
< m_numRows
; i
++ )
3107 bottom
+= m_rowHeights
[i
];
3108 m_rowBottoms
[i
] = bottom
;
3114 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3116 int numRows
= msg
.GetCommandInt();
3117 for ( i
= 0; i
< numRows
; i
++ )
3119 m_rowHeights
.Add( m_defaultRowHeight
);
3120 m_rowBottoms
.Add( 0 );
3123 int oldNumRows
= m_numRows
;
3124 m_numRows
+= numRows
;
3127 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3129 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3131 bottom
+= m_rowHeights
[i
];
3132 m_rowBottoms
[i
] = bottom
;
3138 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3140 size_t pos
= msg
.GetCommandInt();
3141 int numRows
= msg
.GetCommandInt2();
3142 for ( i
= 0; i
< numRows
; i
++ )
3144 m_rowHeights
.Remove( pos
);
3145 m_rowBottoms
.Remove( pos
);
3147 m_numRows
-= numRows
;
3152 m_colWidths
.Clear();
3153 m_colRights
.Clear();
3154 m_currentCellCoords
= wxGridNoCellCoords
;
3158 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3159 m_currentCellCoords
.Set( 0, 0 );
3162 for ( i
= 0; i
< m_numRows
; i
++ )
3164 h
+= m_rowHeights
[i
];
3165 m_rowBottoms
[i
] = h
;
3173 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3175 size_t pos
= msg
.GetCommandInt();
3176 int numCols
= msg
.GetCommandInt2();
3177 for ( i
= 0; i
< numCols
; i
++ )
3179 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3180 m_colRights
.Insert( 0, pos
);
3182 m_numCols
+= numCols
;
3185 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3187 for ( i
= pos
; i
< m_numCols
; i
++ )
3189 right
+= m_colWidths
[i
];
3190 m_colRights
[i
] = right
;
3196 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3198 int numCols
= msg
.GetCommandInt();
3199 for ( i
= 0; i
< numCols
; i
++ )
3201 m_colWidths
.Add( m_defaultColWidth
);
3202 m_colRights
.Add( 0 );
3205 int oldNumCols
= m_numCols
;
3206 m_numCols
+= numCols
;
3209 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3211 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3213 right
+= m_colWidths
[i
];
3214 m_colRights
[i
] = right
;
3220 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3222 size_t pos
= msg
.GetCommandInt();
3223 int numCols
= msg
.GetCommandInt2();
3224 for ( i
= 0; i
< numCols
; i
++ )
3226 m_colWidths
.Remove( pos
);
3227 m_colRights
.Remove( pos
);
3229 m_numCols
-= numCols
;
3233 #if 0 // leave the row alone here so that AppendCols will work subsequently
3235 m_rowHeights
.Clear();
3236 m_rowBottoms
.Clear();
3238 m_currentCellCoords
= wxGridNoCellCoords
;
3242 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3243 m_currentCellCoords
.Set( 0, 0 );
3246 for ( i
= 0; i
< m_numCols
; i
++ )
3248 w
+= m_colWidths
[i
];
3261 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3263 wxRegionIterator
iter( reg
);
3266 m_rowLabelsExposed
.Empty();
3273 // TODO: remove this when we can...
3274 // There is a bug in wxMotif that gives garbage update
3275 // rectangles if you jump-scroll a long way by clicking the
3276 // scrollbar with middle button. This is a work-around
3278 #if defined(__WXMOTIF__)
3280 m_gridWin
->GetClientSize( &cw
, &ch
);
3281 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3282 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3285 // logical bounds of update region
3288 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3289 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3291 // find the row labels within these bounds
3294 for ( row
= 0; row
< m_numRows
; row
++ )
3296 if ( GetRowBottom(row
) < top
)
3299 if ( GetRowTop(row
) > bottom
)
3302 m_rowLabelsExposed
.Add( row
);
3310 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3312 wxRegionIterator
iter( reg
);
3315 m_colLabelsExposed
.Empty();
3322 // TODO: remove this when we can...
3323 // There is a bug in wxMotif that gives garbage update
3324 // rectangles if you jump-scroll a long way by clicking the
3325 // scrollbar with middle button. This is a work-around
3327 #if defined(__WXMOTIF__)
3329 m_gridWin
->GetClientSize( &cw
, &ch
);
3330 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3331 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3334 // logical bounds of update region
3337 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3338 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3340 // find the cells within these bounds
3343 for ( col
= 0; col
< m_numCols
; col
++ )
3345 if ( GetColRight(col
) < left
)
3348 if ( GetColLeft(col
) > right
)
3351 m_colLabelsExposed
.Add( col
);
3359 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3361 wxRegionIterator
iter( reg
);
3364 m_cellsExposed
.Empty();
3365 m_rowsExposed
.Empty();
3366 m_colsExposed
.Empty();
3368 int left
, top
, right
, bottom
;
3373 // TODO: remove this when we can...
3374 // There is a bug in wxMotif that gives garbage update
3375 // rectangles if you jump-scroll a long way by clicking the
3376 // scrollbar with middle button. This is a work-around
3378 #if defined(__WXMOTIF__)
3380 m_gridWin
->GetClientSize( &cw
, &ch
);
3381 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3382 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3383 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3384 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3387 // logical bounds of update region
3389 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3390 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3392 // find the cells within these bounds
3395 for ( row
= 0; row
< m_numRows
; row
++ )
3397 if ( GetRowBottom(row
) <= top
)
3400 if ( GetRowTop(row
) > bottom
)
3403 m_rowsExposed
.Add( row
);
3405 for ( col
= 0; col
< m_numCols
; col
++ )
3407 if ( GetColRight(col
) <= left
)
3410 if ( GetColLeft(col
) > right
)
3413 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3414 m_colsExposed
.Add( col
);
3415 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3424 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3427 wxPoint
pos( event
.GetPosition() );
3428 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3430 if ( event
.Dragging() )
3432 m_isDragging
= TRUE
;
3434 if ( event
.LeftIsDown() )
3436 switch( m_cursorMode
)
3438 case WXGRID_CURSOR_RESIZE_ROW
:
3440 int cw
, ch
, left
, dummy
;
3441 m_gridWin
->GetClientSize( &cw
, &ch
);
3442 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3444 wxClientDC
dc( m_gridWin
);
3446 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3447 dc
.SetLogicalFunction(wxINVERT
);
3448 if ( m_dragLastPos
>= 0 )
3450 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3452 dc
.DrawLine( left
, y
, left
+cw
, y
);
3457 case WXGRID_CURSOR_SELECT_ROW
:
3458 if ( (row
= YToRow( y
)) >= 0 &&
3459 !IsInSelection( row
, 0 ) )
3461 SelectRow( row
, TRUE
);
3464 // default label to suppress warnings about "enumeration value
3465 // 'xxx' not handled in switch
3473 m_isDragging
= FALSE
;
3476 // ------------ Entering or leaving the window
3478 if ( event
.Entering() || event
.Leaving() )
3480 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3484 // ------------ Left button pressed
3486 else if ( event
.LeftDown() )
3488 // don't send a label click event for a hit on the
3489 // edge of the row label - this is probably the user
3490 // wanting to resize the row
3492 if ( YToEdgeOfRow(y
) < 0 )
3496 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3498 SelectRow( row
, event
.ShiftDown() );
3499 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3504 // starting to drag-resize a row
3506 if ( CanDragRowSize() )
3507 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3512 // ------------ Left double click
3514 else if (event
.LeftDClick() )
3516 if ( YToEdgeOfRow(y
) < 0 )
3519 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3524 // ------------ Left button released
3526 else if ( event
.LeftUp() )
3528 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3530 DoEndDragResizeRow();
3532 // Note: we are ending the event *after* doing
3533 // default processing in this case
3535 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3538 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3543 // ------------ Right button down
3545 else if ( event
.RightDown() )
3548 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3550 // no default action at the moment
3555 // ------------ Right double click
3557 else if ( event
.RightDClick() )
3560 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3562 // no default action at the moment
3567 // ------------ No buttons down and mouse moving
3569 else if ( event
.Moving() )
3571 m_dragRowOrCol
= YToEdgeOfRow( y
);
3572 if ( m_dragRowOrCol
>= 0 )
3574 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3576 // don't capture the mouse yet
3577 if ( CanDragRowSize() )
3578 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3581 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3583 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3589 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3592 wxPoint
pos( event
.GetPosition() );
3593 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3595 if ( event
.Dragging() )
3597 m_isDragging
= TRUE
;
3599 if ( event
.LeftIsDown() )
3601 switch( m_cursorMode
)
3603 case WXGRID_CURSOR_RESIZE_COL
:
3605 int cw
, ch
, dummy
, top
;
3606 m_gridWin
->GetClientSize( &cw
, &ch
);
3607 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3609 wxClientDC
dc( m_gridWin
);
3612 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3613 GetColMinimalWidth(m_dragRowOrCol
));
3614 dc
.SetLogicalFunction(wxINVERT
);
3615 if ( m_dragLastPos
>= 0 )
3617 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3619 dc
.DrawLine( x
, top
, x
, top
+ch
);
3624 case WXGRID_CURSOR_SELECT_COL
:
3625 if ( (col
= XToCol( x
)) >= 0 &&
3626 !IsInSelection( 0, col
) )
3628 SelectCol( col
, TRUE
);
3631 // default label to suppress warnings about "enumeration value
3632 // 'xxx' not handled in switch
3640 m_isDragging
= FALSE
;
3643 // ------------ Entering or leaving the window
3645 if ( event
.Entering() || event
.Leaving() )
3647 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3651 // ------------ Left button pressed
3653 else if ( event
.LeftDown() )
3655 // don't send a label click event for a hit on the
3656 // edge of the col label - this is probably the user
3657 // wanting to resize the col
3659 if ( XToEdgeOfCol(x
) < 0 )
3663 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3665 SelectCol( col
, event
.ShiftDown() );
3666 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3671 // starting to drag-resize a col
3673 if ( CanDragColSize() )
3674 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3679 // ------------ Left double click
3681 if ( event
.LeftDClick() )
3683 if ( XToEdgeOfCol(x
) < 0 )
3686 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3691 // ------------ Left button released
3693 else if ( event
.LeftUp() )
3695 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3697 DoEndDragResizeCol();
3699 // Note: we are ending the event *after* doing
3700 // default processing in this case
3702 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3705 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3710 // ------------ Right button down
3712 else if ( event
.RightDown() )
3715 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3717 // no default action at the moment
3722 // ------------ Right double click
3724 else if ( event
.RightDClick() )
3727 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3729 // no default action at the moment
3734 // ------------ No buttons down and mouse moving
3736 else if ( event
.Moving() )
3738 m_dragRowOrCol
= XToEdgeOfCol( x
);
3739 if ( m_dragRowOrCol
>= 0 )
3741 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3743 // don't capture the cursor yet
3744 if ( CanDragColSize() )
3745 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3748 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3750 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3756 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3758 if ( event
.LeftDown() )
3760 // indicate corner label by having both row and
3763 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3769 else if ( event
.LeftDClick() )
3771 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3774 else if ( event
.RightDown() )
3776 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3778 // no default action at the moment
3782 else if ( event
.RightDClick() )
3784 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3786 // no default action at the moment
3791 void wxGrid::ChangeCursorMode(CursorMode mode
,
3796 static const wxChar
*cursorModes
[] =
3805 wxLogTrace(_T("grid"),
3806 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3807 win
== m_colLabelWin
? _T("colLabelWin")
3808 : win
? _T("rowLabelWin")
3810 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3811 #endif // __WXDEBUG__
3813 if ( mode
== m_cursorMode
)
3818 // by default use the grid itself
3824 m_winCapture
->ReleaseMouse();
3825 m_winCapture
= (wxWindow
*)NULL
;
3828 m_cursorMode
= mode
;
3830 switch ( m_cursorMode
)
3832 case WXGRID_CURSOR_RESIZE_ROW
:
3833 win
->SetCursor( m_rowResizeCursor
);
3836 case WXGRID_CURSOR_RESIZE_COL
:
3837 win
->SetCursor( m_colResizeCursor
);
3841 win
->SetCursor( *wxSTANDARD_CURSOR
);
3844 // we need to capture mouse when resizing
3845 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3846 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3848 if ( captureMouse
&& resize
)
3850 win
->CaptureMouse();
3855 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3858 wxPoint
pos( event
.GetPosition() );
3859 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3861 wxGridCellCoords coords
;
3862 XYToCell( x
, y
, coords
);
3864 if ( event
.Dragging() )
3866 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3868 // Don't start doing anything until the mouse has been drug at
3869 // least 3 pixels in any direction...
3872 if (m_startDragPos
== wxDefaultPosition
)
3874 m_startDragPos
= pos
;
3877 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3881 m_isDragging
= TRUE
;
3882 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3884 // Hide the edit control, so it
3885 // won't interfer with drag-shrinking.
3886 if ( IsCellEditControlEnabled() )
3888 HideCellEditControl();
3889 SaveEditControlValue();
3892 // Have we captured the mouse yet?
3895 m_winCapture
= m_gridWin
;
3896 m_winCapture
->CaptureMouse();
3899 if ( coords
!= wxGridNoCellCoords
)
3901 if ( !IsSelection() )
3903 SelectBlock( coords
, coords
);
3907 SelectBlock( m_currentCellCoords
, coords
);
3910 if (! IsVisible(coords
))
3912 MakeCellVisible(coords
);
3913 // TODO: need to introduce a delay or something here. The
3914 // scrolling is way to fast, at least on MSW.
3918 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3920 int cw
, ch
, left
, dummy
;
3921 m_gridWin
->GetClientSize( &cw
, &ch
);
3922 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3924 wxClientDC
dc( m_gridWin
);
3926 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3927 dc
.SetLogicalFunction(wxINVERT
);
3928 if ( m_dragLastPos
>= 0 )
3930 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3932 dc
.DrawLine( left
, y
, left
+cw
, y
);
3935 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3937 int cw
, ch
, dummy
, top
;
3938 m_gridWin
->GetClientSize( &cw
, &ch
);
3939 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3941 wxClientDC
dc( m_gridWin
);
3943 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3944 GetColMinimalWidth(m_dragRowOrCol
) );
3945 dc
.SetLogicalFunction(wxINVERT
);
3946 if ( m_dragLastPos
>= 0 )
3948 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3950 dc
.DrawLine( x
, top
, x
, top
+ch
);
3957 m_isDragging
= FALSE
;
3958 m_startDragPos
= wxDefaultPosition
;
3960 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3961 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3964 if ( event
.Entering() || event
.Leaving() )
3966 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3967 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3972 // ------------ Left button pressed
3974 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
3976 if ( event
.ShiftDown() )
3978 SelectBlock( m_currentCellCoords
, coords
);
3980 else if ( XToEdgeOfCol(x
) < 0 &&
3981 YToEdgeOfRow(y
) < 0 )
3983 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3988 DisableCellEditControl();
3989 MakeCellVisible( coords
);
3991 // if this is the second click on this cell then start
3993 if ( m_waitForSlowClick
&&
3994 (coords
== m_currentCellCoords
) &&
3995 CanEnableCellControl())
3997 EnableCellEditControl();
3999 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4000 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
4003 m_waitForSlowClick
= FALSE
;
4007 SetCurrentCell( coords
);
4008 m_waitForSlowClick
= TRUE
;
4015 // ------------ Left double click
4017 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4019 DisableCellEditControl();
4021 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4023 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4031 // ------------ Left button released
4033 else if ( event
.LeftUp() )
4035 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4037 if ( IsSelection() )
4041 m_winCapture
->ReleaseMouse();
4042 m_winCapture
= NULL
;
4044 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4047 // Show the edit control, if it has been hidden for
4049 ShowCellEditControl();
4051 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4053 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4054 DoEndDragResizeRow();
4056 // Note: we are ending the event *after* doing
4057 // default processing in this case
4059 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4061 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4063 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4064 DoEndDragResizeCol();
4066 // Note: we are ending the event *after* doing
4067 // default processing in this case
4069 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4076 // ------------ Right button down
4078 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4080 DisableCellEditControl();
4081 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4086 // no default action at the moment
4091 // ------------ Right double click
4093 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4095 DisableCellEditControl();
4096 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4101 // no default action at the moment
4105 // ------------ Moving and no button action
4107 else if ( event
.Moving() && !event
.IsButton() )
4109 int dragRow
= YToEdgeOfRow( y
);
4110 int dragCol
= XToEdgeOfCol( x
);
4112 // Dragging on the corner of a cell to resize in both
4113 // directions is not implemented yet...
4115 if ( dragRow
>= 0 && dragCol
>= 0 )
4117 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4123 m_dragRowOrCol
= dragRow
;
4125 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4127 if ( CanDragRowSize() && CanDragGridSize() )
4128 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4133 m_dragRowOrCol
= dragCol
;
4141 m_dragRowOrCol
= dragCol
;
4143 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4145 if ( CanDragColSize() && CanDragGridSize() )
4146 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4152 // Neither on a row or col edge
4154 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4156 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4162 void wxGrid::DoEndDragResizeRow()
4164 if ( m_dragLastPos
>= 0 )
4166 // erase the last line and resize the row
4168 int cw
, ch
, left
, dummy
;
4169 m_gridWin
->GetClientSize( &cw
, &ch
);
4170 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4172 wxClientDC
dc( m_gridWin
);
4174 dc
.SetLogicalFunction( wxINVERT
);
4175 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4176 HideCellEditControl();
4177 SaveEditControlValue();
4179 int rowTop
= GetRowTop(m_dragRowOrCol
);
4180 SetRowSize( m_dragRowOrCol
,
4181 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4183 if ( !GetBatchCount() )
4185 // Only needed to get the correct rect.y:
4186 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4188 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4189 rect
.width
= m_rowLabelWidth
;
4190 rect
.height
= ch
- rect
.y
;
4191 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4193 m_gridWin
->Refresh( FALSE
, &rect
);
4196 ShowCellEditControl();
4201 void wxGrid::DoEndDragResizeCol()
4203 if ( m_dragLastPos
>= 0 )
4205 // erase the last line and resize the col
4207 int cw
, ch
, dummy
, top
;
4208 m_gridWin
->GetClientSize( &cw
, &ch
);
4209 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4211 wxClientDC
dc( m_gridWin
);
4213 dc
.SetLogicalFunction( wxINVERT
);
4214 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4215 HideCellEditControl();
4216 SaveEditControlValue();
4218 int colLeft
= GetColLeft(m_dragRowOrCol
);
4219 SetColSize( m_dragRowOrCol
,
4220 wxMax( m_dragLastPos
- colLeft
,
4221 GetColMinimalWidth(m_dragRowOrCol
) ) );
4223 if ( !GetBatchCount() )
4225 // Only needed to get the correct rect.x:
4226 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4228 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4229 rect
.width
= cw
- rect
.x
;
4230 rect
.height
= m_colLabelHeight
;
4231 m_colLabelWin
->Refresh( TRUE
, &rect
);
4233 m_gridWin
->Refresh( FALSE
, &rect
);
4236 ShowCellEditControl();
4243 // ------ interaction with data model
4245 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4247 switch ( msg
.GetId() )
4249 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4250 return GetModelValues();
4252 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4253 return SetModelValues();
4255 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4256 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4257 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4258 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4259 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4260 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4261 return Redimension( msg
);
4270 // The behaviour of this function depends on the grid table class
4271 // Clear() function. For the default wxGridStringTable class the
4272 // behavious is to replace all cell contents with wxEmptyString but
4273 // not to change the number of rows or cols.
4275 void wxGrid::ClearGrid()
4279 if (IsCellEditControlEnabled())
4280 DisableCellEditControl();
4283 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4288 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4290 // TODO: something with updateLabels flag
4294 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4300 if (IsCellEditControlEnabled())
4301 DisableCellEditControl();
4303 bool ok
= m_table
->InsertRows( pos
, numRows
);
4305 // the table will have sent the results of the insert row
4306 // operation to this view object as a grid table message
4310 if ( m_numCols
== 0 )
4312 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4314 // TODO: perhaps instead of appending the default number of cols
4315 // we should remember what the last non-zero number of cols was ?
4319 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4321 // if we have just inserted cols into an empty grid the current
4322 // cell will be undefined...
4324 SetCurrentCell( 0, 0 );
4328 if ( !GetBatchCount() ) Refresh();
4340 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4342 // TODO: something with updateLabels flag
4346 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4350 if ( m_table
&& m_table
->AppendRows( numRows
) )
4352 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4354 // if we have just inserted cols into an empty grid the current
4355 // cell will be undefined...
4357 SetCurrentCell( 0, 0 );
4360 // the table will have sent the results of the append row
4361 // operation to this view object as a grid table message
4364 if ( !GetBatchCount() ) Refresh();
4374 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4376 // TODO: something with updateLabels flag
4380 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4386 if (IsCellEditControlEnabled())
4387 DisableCellEditControl();
4389 if (m_table
->DeleteRows( pos
, numRows
))
4392 // the table will have sent the results of the delete row
4393 // operation to this view object as a grid table message
4396 if ( !GetBatchCount() ) Refresh();
4404 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4406 // TODO: something with updateLabels flag
4410 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4416 if (IsCellEditControlEnabled())
4417 DisableCellEditControl();
4419 bool ok
= m_table
->InsertCols( pos
, numCols
);
4421 // the table will have sent the results of the insert col
4422 // operation to this view object as a grid table message
4426 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4428 // if we have just inserted cols into an empty grid the current
4429 // cell will be undefined...
4431 SetCurrentCell( 0, 0 );
4435 if ( !GetBatchCount() ) Refresh();
4447 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4449 // TODO: something with updateLabels flag
4453 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4457 if ( m_table
&& m_table
->AppendCols( numCols
) )
4459 // the table will have sent the results of the append col
4460 // operation to this view object as a grid table message
4462 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4464 // if we have just inserted cols into an empty grid the current
4465 // cell will be undefined...
4467 SetCurrentCell( 0, 0 );
4471 if ( !GetBatchCount() ) Refresh();
4481 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4483 // TODO: something with updateLabels flag
4487 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4493 if (IsCellEditControlEnabled())
4494 DisableCellEditControl();
4496 if ( m_table
->DeleteCols( pos
, numCols
) )
4498 // the table will have sent the results of the delete col
4499 // operation to this view object as a grid table message
4502 if ( !GetBatchCount() ) Refresh();
4512 // ----- event handlers
4515 // Generate a grid event based on a mouse event and
4516 // return the result of ProcessEvent()
4518 bool wxGrid::SendEvent( const wxEventType type
,
4520 wxMouseEvent
& mouseEv
)
4522 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4524 int rowOrCol
= (row
== -1 ? col
: row
);
4526 wxGridSizeEvent
gridEvt( GetId(),
4530 mouseEv
.GetX(), mouseEv
.GetY(),
4531 mouseEv
.ControlDown(),
4532 mouseEv
.ShiftDown(),
4534 mouseEv
.MetaDown() );
4536 return GetEventHandler()->ProcessEvent(gridEvt
);
4538 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4540 wxGridRangeSelectEvent
gridEvt( GetId(),
4544 m_selectedBottomRight
,
4545 mouseEv
.ControlDown(),
4546 mouseEv
.ShiftDown(),
4548 mouseEv
.MetaDown() );
4550 return GetEventHandler()->ProcessEvent(gridEvt
);
4554 wxGridEvent
gridEvt( GetId(),
4558 mouseEv
.GetX(), mouseEv
.GetY(),
4559 mouseEv
.ControlDown(),
4560 mouseEv
.ShiftDown(),
4562 mouseEv
.MetaDown() );
4564 return GetEventHandler()->ProcessEvent(gridEvt
);
4569 // Generate a grid event of specified type and return the result
4570 // of ProcessEvent().
4572 bool wxGrid::SendEvent( const wxEventType type
,
4575 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4577 int rowOrCol
= (row
== -1 ? col
: row
);
4579 wxGridSizeEvent
gridEvt( GetId(),
4584 return GetEventHandler()->ProcessEvent(gridEvt
);
4588 wxGridEvent
gridEvt( GetId(),
4593 return GetEventHandler()->ProcessEvent(gridEvt
);
4598 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4600 wxPaintDC
dc( this );
4602 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4603 m_numRows
&& m_numCols
)
4605 m_currentCellCoords
.Set(0, 0);
4606 ShowCellEditControl();
4613 // This is just here to make sure that CalcDimensions gets called when
4614 // the grid view is resized... then the size event is skipped to allow
4615 // the box sizers to handle everything
4617 void wxGrid::OnSize( wxSizeEvent
& event
)
4624 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4626 if ( m_inOnKeyDown
)
4628 // shouldn't be here - we are going round in circles...
4630 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4633 m_inOnKeyDown
= TRUE
;
4635 // propagate the event up and see if it gets processed
4637 wxWindow
*parent
= GetParent();
4638 wxKeyEvent
keyEvt( event
);
4639 keyEvt
.SetEventObject( parent
);
4641 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4644 // TODO: Should also support Shift-cursor keys for
4645 // extending the selection. Maybe add a flag to
4646 // MoveCursorXXX() and MoveCursorXXXBlock() and
4647 // just send event.ShiftDown().
4649 // try local handlers
4651 switch ( event
.KeyCode() )
4654 if ( event
.ControlDown() )
4656 MoveCursorUpBlock();
4665 if ( event
.ControlDown() )
4667 MoveCursorDownBlock();
4676 if ( event
.ControlDown() )
4678 MoveCursorLeftBlock();
4687 if ( event
.ControlDown() )
4689 MoveCursorRightBlock();
4698 if ( event
.ControlDown() )
4700 event
.Skip(); // to let the edit control have the return
4709 if (event
.ShiftDown())
4716 if ( event
.ControlDown() )
4718 MakeCellVisible( 0, 0 );
4719 SetCurrentCell( 0, 0 );
4728 if ( event
.ControlDown() )
4730 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4731 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4748 if ( !IsEditable() )
4753 // Otherwise fall through to default
4756 // alphanumeric keys or F2 (special key just for this) enable
4757 // the cell edit control
4758 if ( !(event
.AltDown() ||
4760 event
.ControlDown()) &&
4761 (isalnum(event
.KeyCode()) || event
.KeyCode() == WXK_F2
) &&
4762 !IsCellEditControlEnabled() &&
4763 CanEnableCellControl() )
4765 EnableCellEditControl();
4766 int row
= m_currentCellCoords
.GetRow();
4767 int col
= m_currentCellCoords
.GetCol();
4768 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4769 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4774 // let others process char events with modifiers or all
4775 // char events for readonly cells
4782 m_inOnKeyDown
= FALSE
;
4786 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4790 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4792 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4794 // the event has been intercepted - do nothing
4799 m_currentCellCoords
!= wxGridNoCellCoords
)
4801 HideCellEditControl();
4802 DisableCellEditControl();
4804 // Clear the old current cell highlight
4805 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4807 // Otherwise refresh redraws the highlight!
4808 m_currentCellCoords
= coords
;
4810 m_gridWin
->Refresh( FALSE
, &r
);
4813 m_currentCellCoords
= coords
;
4817 wxClientDC
dc(m_gridWin
);
4820 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4821 DrawCellHighlight(dc
, attr
);
4824 if ( IsSelection() )
4826 wxRect
r( SelectionToDeviceRect() );
4828 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4835 // ------ functions to get/send data (see also public functions)
4838 bool wxGrid::GetModelValues()
4842 // all we need to do is repaint the grid
4844 m_gridWin
->Refresh();
4852 bool wxGrid::SetModelValues()
4858 for ( row
= 0; row
< m_numRows
; row
++ )
4860 for ( col
= 0; col
< m_numCols
; col
++ )
4862 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4874 // Note - this function only draws cells that are in the list of
4875 // exposed cells (usually set from the update region by
4876 // CalcExposedCells)
4878 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4880 if ( !m_numRows
|| !m_numCols
) return;
4883 size_t numCells
= m_cellsExposed
.GetCount();
4885 for ( i
= 0; i
< numCells
; i
++ )
4887 DrawCell( dc
, m_cellsExposed
[i
] );
4892 void wxGrid::DrawGridSpace( wxDC
& dc
)
4894 if ( m_numRows
&& m_numCols
)
4897 m_gridWin
->GetClientSize( &cw
, &ch
);
4900 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4902 if ( right
> GetColRight(m_numCols
-1) ||
4903 bottom
> GetRowBottom(m_numRows
-1) )
4906 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4908 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4909 dc
.SetPen( *wxTRANSPARENT_PEN
);
4911 if ( right
> GetColRight(m_numCols
-1) )
4912 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
4913 right
- GetColRight(m_numCols
-1), ch
);
4915 if ( bottom
> GetRowBottom(m_numRows
-1) )
4916 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
4917 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4923 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4925 int row
= coords
.GetRow();
4926 int col
= coords
.GetCol();
4928 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4931 // we draw the cell border ourselves
4932 #if !WXGRID_DRAW_LINES
4933 if ( m_gridLinesEnabled
)
4934 DrawCellBorder( dc
, coords
);
4937 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4939 bool isCurrent
= coords
== m_currentCellCoords
;
4942 rect
.x
= GetColLeft(col
);
4943 rect
.y
= GetRowTop(row
);
4944 rect
.width
= GetColWidth(col
) - 1;
4945 rect
.height
= GetRowHeight(row
) - 1;
4947 // if the editor is shown, we should use it and not the renderer
4948 if ( isCurrent
&& IsCellEditControlEnabled() )
4950 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4954 // but all the rest is drawn by the cell renderer and hence may be
4956 attr
->GetRenderer(this, row
, col
)->
4957 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4964 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4966 int row
= m_currentCellCoords
.GetRow();
4967 int col
= m_currentCellCoords
.GetCol();
4969 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4973 rect
.x
= GetColLeft(col
);
4974 rect
.y
= GetRowTop(row
);
4975 rect
.width
= GetColWidth(col
) - 1;
4976 rect
.height
= GetRowHeight(row
) - 1;
4978 // hmmm... what could we do here to show that the cell is disabled?
4979 // for now, I just draw a thinner border than for the other ones, but
4980 // it doesn't look really good
4981 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4982 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4984 dc
.DrawRectangle(rect
);
4987 // VZ: my experiments with 3d borders...
4989 // how to properly set colours for arbitrary bg?
4990 wxCoord x1
= rect
.x
,
4992 x2
= rect
.x
+ rect
.width
-1,
4993 y2
= rect
.y
+ rect
.height
-1;
4995 dc
.SetPen(*wxWHITE_PEN
);
4996 dc
.DrawLine(x1
, y1
, x2
, y1
);
4997 dc
.DrawLine(x1
, y1
, x1
, y2
);
4999 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5000 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5002 dc
.SetPen(*wxBLACK_PEN
);
5003 dc
.DrawLine(x1
, y2
, x2
, y2
);
5004 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5009 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5011 int row
= coords
.GetRow();
5012 int col
= coords
.GetCol();
5013 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5016 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5018 // right hand border
5020 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5021 GetColRight(col
), GetRowBottom(row
) );
5025 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5026 GetColRight(col
), GetRowBottom(row
) );
5029 void wxGrid::DrawHighlight(wxDC
& dc
)
5031 if ( IsCellEditControlEnabled() )
5033 // don't show highlight when the edit control is shown
5037 // if the active cell was repainted, repaint its highlight too because it
5038 // might have been damaged by the grid lines
5039 size_t count
= m_cellsExposed
.GetCount();
5040 for ( size_t n
= 0; n
< count
; n
++ )
5042 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5044 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5045 DrawCellHighlight(dc
, attr
);
5053 // TODO: remove this ???
5054 // This is used to redraw all grid lines e.g. when the grid line colour
5057 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5059 if ( !m_gridLinesEnabled
||
5061 !m_numCols
) return;
5063 int top
, bottom
, left
, right
;
5069 m_gridWin
->GetClientSize(&cw
, &ch
);
5071 // virtual coords of visible area
5073 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5074 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5079 reg
.GetBox(x
, y
, w
, h
);
5080 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5081 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5085 m_gridWin
->GetClientSize(&cw
, &ch
);
5086 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5087 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5090 // avoid drawing grid lines past the last row and col
5092 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5093 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5095 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5097 // horizontal grid lines
5100 for ( i
= 0; i
< m_numRows
; i
++ )
5102 int bot
= GetRowBottom(i
) - 1;
5111 dc
.DrawLine( left
, bot
, right
, bot
);
5116 // vertical grid lines
5118 for ( i
= 0; i
< m_numCols
; i
++ )
5120 int colRight
= GetColRight(i
) - 1;
5121 if ( colRight
> right
)
5126 if ( colRight
>= left
)
5128 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5134 void wxGrid::DrawRowLabels( wxDC
& dc
)
5136 if ( !m_numRows
|| !m_numCols
) return;
5139 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5141 for ( i
= 0; i
< numLabels
; i
++ )
5143 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5148 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5150 if ( GetRowHeight(row
) <= 0 )
5153 int rowTop
= GetRowTop(row
),
5154 rowBottom
= GetRowBottom(row
) - 1;
5156 dc
.SetPen( *wxBLACK_PEN
);
5157 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5158 m_rowLabelWidth
-1, rowBottom
);
5160 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5162 dc
.SetPen( *wxWHITE_PEN
);
5163 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5164 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5166 dc
.SetBackgroundMode( wxTRANSPARENT
);
5167 dc
.SetTextForeground( GetLabelTextColour() );
5168 dc
.SetFont( GetLabelFont() );
5171 GetRowLabelAlignment( &hAlign
, &vAlign
);
5175 rect
.SetY( GetRowTop(row
) + 2 );
5176 rect
.SetWidth( m_rowLabelWidth
- 4 );
5177 rect
.SetHeight( GetRowHeight(row
) - 4 );
5178 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5182 void wxGrid::DrawColLabels( wxDC
& dc
)
5184 if ( !m_numRows
|| !m_numCols
) return;
5187 size_t numLabels
= m_colLabelsExposed
.GetCount();
5189 for ( i
= 0; i
< numLabels
; i
++ )
5191 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5196 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5198 if ( GetColWidth(col
) <= 0 )
5201 int colLeft
= GetColLeft(col
),
5202 colRight
= GetColRight(col
) - 1;
5204 dc
.SetPen( *wxBLACK_PEN
);
5205 dc
.DrawLine( colRight
, 0,
5206 colRight
, m_colLabelHeight
-1 );
5208 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5209 colRight
, m_colLabelHeight
-1 );
5211 dc
.SetPen( *wxWHITE_PEN
);
5212 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5213 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5215 dc
.SetBackgroundMode( wxTRANSPARENT
);
5216 dc
.SetTextForeground( GetLabelTextColour() );
5217 dc
.SetFont( GetLabelFont() );
5219 dc
.SetBackgroundMode( wxTRANSPARENT
);
5220 dc
.SetTextForeground( GetLabelTextColour() );
5221 dc
.SetFont( GetLabelFont() );
5224 GetColLabelAlignment( &hAlign
, &vAlign
);
5227 rect
.SetX( colLeft
+ 2 );
5229 rect
.SetWidth( GetColWidth(col
) - 4 );
5230 rect
.SetHeight( m_colLabelHeight
- 4 );
5231 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5235 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5236 const wxString
& value
,
5241 long textWidth
, textHeight
;
5242 long lineWidth
, lineHeight
;
5243 wxArrayString lines
;
5245 dc
.SetClippingRegion( rect
);
5246 StringToLines( value
, lines
);
5247 if ( lines
.GetCount() )
5249 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5250 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5253 switch ( horizAlign
)
5256 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5260 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5269 switch ( vertAlign
)
5272 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5276 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5285 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5287 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5292 dc
.DestroyClippingRegion();
5296 // Split multi line text up into an array of strings. Any existing
5297 // contents of the string array are preserved.
5299 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5303 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5304 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5306 while ( startPos
< (int)tVal
.Length() )
5308 pos
= tVal
.Mid(startPos
).Find( eol
);
5313 else if ( pos
== 0 )
5315 lines
.Add( wxEmptyString
);
5319 lines
.Add( value
.Mid(startPos
, pos
) );
5323 if ( startPos
< (int)value
.Length() )
5325 lines
.Add( value
.Mid( startPos
) );
5330 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5331 wxArrayString
& lines
,
5332 long *width
, long *height
)
5339 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5341 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5342 w
= wxMax( w
, lineW
);
5352 // ------ Edit control functions
5356 void wxGrid::EnableEditing( bool edit
)
5358 // TODO: improve this ?
5360 if ( edit
!= m_editable
)
5364 // FIXME IMHO this won't disable the edit control if edit == FALSE
5365 // because of the check in the beginning of
5366 // EnableCellEditControl() just below (VZ)
5367 EnableCellEditControl(m_editable
);
5372 void wxGrid::EnableCellEditControl( bool enable
)
5377 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5378 SetCurrentCell( 0, 0 );
5380 if ( enable
!= m_cellEditCtrlEnabled
)
5382 // TODO allow the app to Veto() this event?
5383 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5387 // this should be checked by the caller!
5388 wxASSERT_MSG( CanEnableCellControl(),
5389 _T("can't enable editing for this cell!") );
5391 // do it before ShowCellEditControl()
5392 m_cellEditCtrlEnabled
= enable
;
5394 ShowCellEditControl();
5398 HideCellEditControl();
5399 SaveEditControlValue();
5401 // do it after HideCellEditControl()
5402 m_cellEditCtrlEnabled
= enable
;
5407 bool wxGrid::IsCurrentCellReadOnly() const
5410 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5411 bool readonly
= attr
->IsReadOnly();
5417 bool wxGrid::CanEnableCellControl() const
5419 return m_editable
&& !IsCurrentCellReadOnly();
5422 bool wxGrid::IsCellEditControlEnabled() const
5424 // the cell edit control might be disable for all cells or just for the
5425 // current one if it's read only
5426 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5429 void wxGrid::ShowCellEditControl()
5431 if ( IsCellEditControlEnabled() )
5433 if ( !IsVisible( m_currentCellCoords
) )
5439 wxRect rect
= CellToRect( m_currentCellCoords
);
5440 int row
= m_currentCellCoords
.GetRow();
5441 int col
= m_currentCellCoords
.GetCol();
5443 // convert to scrolled coords
5445 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5447 // done in PaintBackground()
5449 // erase the highlight and the cell contents because the editor
5450 // might not cover the entire cell
5451 wxClientDC
dc( m_gridWin
);
5453 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5454 dc
.SetPen(*wxTRANSPARENT_PEN
);
5455 dc
.DrawRectangle(rect
);
5458 // cell is shifted by one pixel
5462 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5463 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5464 if ( !editor
->IsCreated() )
5466 editor
->Create(m_gridWin
, -1,
5467 new wxGridCellEditorEvtHandler(this, editor
));
5470 editor
->Show( TRUE
, attr
);
5472 editor
->SetSize( rect
);
5474 editor
->BeginEdit(row
, col
, this);
5481 void wxGrid::HideCellEditControl()
5483 if ( IsCellEditControlEnabled() )
5485 int row
= m_currentCellCoords
.GetRow();
5486 int col
= m_currentCellCoords
.GetCol();
5488 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5489 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5491 m_gridWin
->SetFocus();
5496 void wxGrid::SaveEditControlValue()
5498 if ( IsCellEditControlEnabled() )
5500 int row
= m_currentCellCoords
.GetRow();
5501 int col
= m_currentCellCoords
.GetCol();
5503 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5504 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5505 bool changed
= editor
->EndEdit(row
, col
, this);
5511 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5512 m_currentCellCoords
.GetRow(),
5513 m_currentCellCoords
.GetCol() );
5520 // ------ Grid location functions
5521 // Note that all of these functions work with the logical coordinates of
5522 // grid cells and labels so you will need to convert from device
5523 // coordinates for mouse events etc.
5526 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5528 int row
= YToRow(y
);
5529 int col
= XToCol(x
);
5531 if ( row
== -1 || col
== -1 )
5533 coords
= wxGridNoCellCoords
;
5537 coords
.Set( row
, col
);
5542 int wxGrid::YToRow( int y
)
5546 for ( i
= 0; i
< m_numRows
; i
++ )
5548 if ( y
< GetRowBottom(i
) )
5556 int wxGrid::XToCol( int x
)
5560 for ( i
= 0; i
< m_numCols
; i
++ )
5562 if ( x
< GetColRight(i
) )
5570 // return the row number that that the y coord is near the edge of, or
5571 // -1 if not near an edge
5573 int wxGrid::YToEdgeOfRow( int y
)
5577 for ( i
= 0; i
< m_numRows
; i
++ )
5579 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5581 d
= abs( y
- GetRowBottom(i
) );
5582 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5591 // return the col number that that the x coord is near the edge of, or
5592 // -1 if not near an edge
5594 int wxGrid::XToEdgeOfCol( int x
)
5598 for ( i
= 0; i
< m_numCols
; i
++ )
5600 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5602 d
= abs( x
- GetColRight(i
) );
5603 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5612 wxRect
wxGrid::CellToRect( int row
, int col
)
5614 wxRect
rect( -1, -1, -1, -1 );
5616 if ( row
>= 0 && row
< m_numRows
&&
5617 col
>= 0 && col
< m_numCols
)
5619 rect
.x
= GetColLeft(col
);
5620 rect
.y
= GetRowTop(row
);
5621 rect
.width
= GetColWidth(col
);
5622 rect
.height
= GetRowHeight(row
);
5629 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5631 // get the cell rectangle in logical coords
5633 wxRect
r( CellToRect( row
, col
) );
5635 // convert to device coords
5637 int left
, top
, right
, bottom
;
5638 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5639 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5641 // check against the client area of the grid window
5644 m_gridWin
->GetClientSize( &cw
, &ch
);
5646 if ( wholeCellVisible
)
5648 // is the cell wholly visible ?
5650 return ( left
>= 0 && right
<= cw
&&
5651 top
>= 0 && bottom
<= ch
);
5655 // is the cell partly visible ?
5657 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5658 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5663 // make the specified cell location visible by doing a minimal amount
5666 void wxGrid::MakeCellVisible( int row
, int col
)
5669 int xpos
= -1, ypos
= -1;
5671 if ( row
>= 0 && row
< m_numRows
&&
5672 col
>= 0 && col
< m_numCols
)
5674 // get the cell rectangle in logical coords
5676 wxRect
r( CellToRect( row
, col
) );
5678 // convert to device coords
5680 int left
, top
, right
, bottom
;
5681 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5682 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5685 m_gridWin
->GetClientSize( &cw
, &ch
);
5691 else if ( bottom
> ch
)
5693 int h
= r
.GetHeight();
5695 for ( i
= row
-1; i
>= 0; i
-- )
5697 int rowHeight
= GetRowHeight(i
);
5698 if ( h
+ rowHeight
> ch
)
5705 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5706 // have rounding errors (this is important, because if we do, we
5707 // might not scroll at all and some cells won't be redrawn)
5708 ypos
+= GRID_SCROLL_LINE
/ 2;
5715 else if ( right
> cw
)
5717 int w
= r
.GetWidth();
5719 for ( i
= col
-1; i
>= 0; i
-- )
5721 int colWidth
= GetColWidth(i
);
5722 if ( w
+ colWidth
> cw
)
5729 // see comment for ypos above
5730 xpos
+= GRID_SCROLL_LINE
/ 2;
5733 if ( xpos
!= -1 || ypos
!= -1 )
5735 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5736 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5737 Scroll( xpos
, ypos
);
5745 // ------ Grid cursor movement functions
5748 bool wxGrid::MoveCursorUp()
5750 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5751 m_currentCellCoords
.GetRow() > 0 )
5753 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5754 m_currentCellCoords
.GetCol() );
5756 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5757 m_currentCellCoords
.GetCol() );
5766 bool wxGrid::MoveCursorDown()
5768 // TODO: allow for scrolling
5770 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5771 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5773 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5774 m_currentCellCoords
.GetCol() );
5776 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5777 m_currentCellCoords
.GetCol() );
5786 bool wxGrid::MoveCursorLeft()
5788 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5789 m_currentCellCoords
.GetCol() > 0 )
5791 MakeCellVisible( m_currentCellCoords
.GetRow(),
5792 m_currentCellCoords
.GetCol() - 1 );
5794 SetCurrentCell( m_currentCellCoords
.GetRow(),
5795 m_currentCellCoords
.GetCol() - 1 );
5804 bool wxGrid::MoveCursorRight()
5806 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5807 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5809 MakeCellVisible( m_currentCellCoords
.GetRow(),
5810 m_currentCellCoords
.GetCol() + 1 );
5812 SetCurrentCell( m_currentCellCoords
.GetRow(),
5813 m_currentCellCoords
.GetCol() + 1 );
5822 bool wxGrid::MovePageUp()
5824 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5826 int row
= m_currentCellCoords
.GetRow();
5830 m_gridWin
->GetClientSize( &cw
, &ch
);
5832 int y
= GetRowTop(row
);
5833 int newRow
= YToRow( y
- ch
+ 1 );
5838 else if ( newRow
== row
)
5843 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5844 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5852 bool wxGrid::MovePageDown()
5854 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5856 int row
= m_currentCellCoords
.GetRow();
5857 if ( row
< m_numRows
)
5860 m_gridWin
->GetClientSize( &cw
, &ch
);
5862 int y
= GetRowTop(row
);
5863 int newRow
= YToRow( y
+ ch
);
5866 newRow
= m_numRows
- 1;
5868 else if ( newRow
== row
)
5873 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5874 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5882 bool wxGrid::MoveCursorUpBlock()
5885 m_currentCellCoords
!= wxGridNoCellCoords
&&
5886 m_currentCellCoords
.GetRow() > 0 )
5888 int row
= m_currentCellCoords
.GetRow();
5889 int col
= m_currentCellCoords
.GetCol();
5891 if ( m_table
->IsEmptyCell(row
, col
) )
5893 // starting in an empty cell: find the next block of
5899 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5902 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5904 // starting at the top of a block: find the next block
5910 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5915 // starting within a block: find the top of the block
5920 if ( m_table
->IsEmptyCell(row
, col
) )
5928 MakeCellVisible( row
, col
);
5929 SetCurrentCell( row
, col
);
5937 bool wxGrid::MoveCursorDownBlock()
5940 m_currentCellCoords
!= wxGridNoCellCoords
&&
5941 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5943 int row
= m_currentCellCoords
.GetRow();
5944 int col
= m_currentCellCoords
.GetCol();
5946 if ( m_table
->IsEmptyCell(row
, col
) )
5948 // starting in an empty cell: find the next block of
5951 while ( row
< m_numRows
-1 )
5954 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5957 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5959 // starting at the bottom of a block: find the next block
5962 while ( row
< m_numRows
-1 )
5965 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5970 // starting within a block: find the bottom of the block
5972 while ( row
< m_numRows
-1 )
5975 if ( m_table
->IsEmptyCell(row
, col
) )
5983 MakeCellVisible( row
, col
);
5984 SetCurrentCell( row
, col
);
5992 bool wxGrid::MoveCursorLeftBlock()
5995 m_currentCellCoords
!= wxGridNoCellCoords
&&
5996 m_currentCellCoords
.GetCol() > 0 )
5998 int row
= m_currentCellCoords
.GetRow();
5999 int col
= m_currentCellCoords
.GetCol();
6001 if ( m_table
->IsEmptyCell(row
, col
) )
6003 // starting in an empty cell: find the next block of
6009 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6012 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6014 // starting at the left of a block: find the next block
6020 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6025 // starting within a block: find the left of the block
6030 if ( m_table
->IsEmptyCell(row
, col
) )
6038 MakeCellVisible( row
, col
);
6039 SetCurrentCell( row
, col
);
6047 bool wxGrid::MoveCursorRightBlock()
6050 m_currentCellCoords
!= wxGridNoCellCoords
&&
6051 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6053 int row
= m_currentCellCoords
.GetRow();
6054 int col
= m_currentCellCoords
.GetCol();
6056 if ( m_table
->IsEmptyCell(row
, col
) )
6058 // starting in an empty cell: find the next block of
6061 while ( col
< m_numCols
-1 )
6064 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6067 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6069 // starting at the right of a block: find the next block
6072 while ( col
< m_numCols
-1 )
6075 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6080 // starting within a block: find the right of the block
6082 while ( col
< m_numCols
-1 )
6085 if ( m_table
->IsEmptyCell(row
, col
) )
6093 MakeCellVisible( row
, col
);
6094 SetCurrentCell( row
, col
);
6105 // ------ Label values and formatting
6108 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6110 *horiz
= m_rowLabelHorizAlign
;
6111 *vert
= m_rowLabelVertAlign
;
6114 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6116 *horiz
= m_colLabelHorizAlign
;
6117 *vert
= m_colLabelVertAlign
;
6120 wxString
wxGrid::GetRowLabelValue( int row
)
6124 return m_table
->GetRowLabelValue( row
);
6134 wxString
wxGrid::GetColLabelValue( int col
)
6138 return m_table
->GetColLabelValue( col
);
6149 void wxGrid::SetRowLabelSize( int width
)
6151 width
= wxMax( width
, 0 );
6152 if ( width
!= m_rowLabelWidth
)
6156 m_rowLabelWin
->Show( FALSE
);
6157 m_cornerLabelWin
->Show( FALSE
);
6159 else if ( m_rowLabelWidth
== 0 )
6161 m_rowLabelWin
->Show( TRUE
);
6162 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6165 m_rowLabelWidth
= width
;
6172 void wxGrid::SetColLabelSize( int height
)
6174 height
= wxMax( height
, 0 );
6175 if ( height
!= m_colLabelHeight
)
6179 m_colLabelWin
->Show( FALSE
);
6180 m_cornerLabelWin
->Show( FALSE
);
6182 else if ( m_colLabelHeight
== 0 )
6184 m_colLabelWin
->Show( TRUE
);
6185 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6188 m_colLabelHeight
= height
;
6195 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6197 if ( m_labelBackgroundColour
!= colour
)
6199 m_labelBackgroundColour
= colour
;
6200 m_rowLabelWin
->SetBackgroundColour( colour
);
6201 m_colLabelWin
->SetBackgroundColour( colour
);
6202 m_cornerLabelWin
->SetBackgroundColour( colour
);
6204 if ( !GetBatchCount() )
6206 m_rowLabelWin
->Refresh();
6207 m_colLabelWin
->Refresh();
6208 m_cornerLabelWin
->Refresh();
6213 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6215 if ( m_labelTextColour
!= colour
)
6217 m_labelTextColour
= colour
;
6218 if ( !GetBatchCount() )
6220 m_rowLabelWin
->Refresh();
6221 m_colLabelWin
->Refresh();
6226 void wxGrid::SetLabelFont( const wxFont
& font
)
6229 if ( !GetBatchCount() )
6231 m_rowLabelWin
->Refresh();
6232 m_colLabelWin
->Refresh();
6236 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6238 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6240 m_rowLabelHorizAlign
= horiz
;
6243 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6245 m_rowLabelVertAlign
= vert
;
6248 if ( !GetBatchCount() )
6250 m_rowLabelWin
->Refresh();
6254 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6256 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6258 m_colLabelHorizAlign
= horiz
;
6261 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6263 m_colLabelVertAlign
= vert
;
6266 if ( !GetBatchCount() )
6268 m_colLabelWin
->Refresh();
6272 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6276 m_table
->SetRowLabelValue( row
, s
);
6277 if ( !GetBatchCount() )
6279 wxRect rect
= CellToRect( row
, 0);
6280 if ( rect
.height
> 0 )
6282 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6284 rect
.width
= m_rowLabelWidth
;
6285 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6291 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6295 m_table
->SetColLabelValue( col
, s
);
6296 if ( !GetBatchCount() )
6298 wxRect rect
= CellToRect( 0, col
);
6299 if ( rect
.width
> 0 )
6301 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6303 rect
.height
= m_colLabelHeight
;
6304 m_colLabelWin
->Refresh( TRUE
, &rect
);
6310 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6312 if ( m_gridLineColour
!= colour
)
6314 m_gridLineColour
= colour
;
6316 wxClientDC
dc( m_gridWin
);
6318 DrawAllGridLines( dc
, wxRegion() );
6322 void wxGrid::EnableGridLines( bool enable
)
6324 if ( enable
!= m_gridLinesEnabled
)
6326 m_gridLinesEnabled
= enable
;
6328 if ( !GetBatchCount() )
6332 wxClientDC
dc( m_gridWin
);
6334 DrawAllGridLines( dc
, wxRegion() );
6338 m_gridWin
->Refresh();
6345 int wxGrid::GetDefaultRowSize()
6347 return m_defaultRowHeight
;
6350 int wxGrid::GetRowSize( int row
)
6352 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6354 return GetRowHeight(row
);
6357 int wxGrid::GetDefaultColSize()
6359 return m_defaultColWidth
;
6362 int wxGrid::GetColSize( int col
)
6364 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6366 return GetColWidth(col
);
6369 // ============================================================================
6370 // access to the grid attributes: each of them has a default value in the grid
6371 // itself and may be overidden on a per-cell basis
6372 // ============================================================================
6374 // ----------------------------------------------------------------------------
6375 // setting default attributes
6376 // ----------------------------------------------------------------------------
6378 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6380 m_defaultCellAttr
->SetBackgroundColour(col
);
6382 m_gridWin
->SetBackgroundColour(col
);
6386 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6388 m_defaultCellAttr
->SetTextColour(col
);
6391 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6393 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6396 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6398 m_defaultCellAttr
->SetFont(font
);
6401 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6403 m_defaultCellAttr
->SetRenderer(renderer
);
6406 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6408 m_defaultCellAttr
->SetEditor(editor
);
6411 // ----------------------------------------------------------------------------
6412 // access to the default attrbiutes
6413 // ----------------------------------------------------------------------------
6415 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6417 return m_defaultCellAttr
->GetBackgroundColour();
6420 wxColour
wxGrid::GetDefaultCellTextColour()
6422 return m_defaultCellAttr
->GetTextColour();
6425 wxFont
wxGrid::GetDefaultCellFont()
6427 return m_defaultCellAttr
->GetFont();
6430 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6432 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6435 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6437 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6440 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6442 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6445 // ----------------------------------------------------------------------------
6446 // access to cell attributes
6447 // ----------------------------------------------------------------------------
6449 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6451 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6452 wxColour colour
= attr
->GetBackgroundColour();
6457 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6459 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6460 wxColour colour
= attr
->GetTextColour();
6465 wxFont
wxGrid::GetCellFont( int row
, int col
)
6467 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6468 wxFont font
= attr
->GetFont();
6473 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6475 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6476 attr
->GetAlignment(horiz
, vert
);
6480 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6482 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6483 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6488 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6490 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6491 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6496 bool wxGrid::IsReadOnly(int row
, int col
) const
6498 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6499 bool isReadOnly
= attr
->IsReadOnly();
6504 // ----------------------------------------------------------------------------
6505 // attribute support: cache, automatic provider creation, ...
6506 // ----------------------------------------------------------------------------
6508 bool wxGrid::CanHaveAttributes()
6515 return m_table
->CanHaveAttributes();
6518 void wxGrid::ClearAttrCache()
6520 if ( m_attrCache
.row
!= -1 )
6522 m_attrCache
.attr
->SafeDecRef();
6523 m_attrCache
.row
= -1;
6527 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6529 wxGrid
*self
= (wxGrid
*)this; // const_cast
6531 self
->ClearAttrCache();
6532 self
->m_attrCache
.row
= row
;
6533 self
->m_attrCache
.col
= col
;
6534 self
->m_attrCache
.attr
= attr
;
6538 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6540 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6542 *attr
= m_attrCache
.attr
;
6543 (*attr
)->SafeIncRef();
6545 #ifdef DEBUG_ATTR_CACHE
6546 gs_nAttrCacheHits
++;
6553 #ifdef DEBUG_ATTR_CACHE
6554 gs_nAttrCacheMisses
++;
6560 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6562 wxGridCellAttr
*attr
;
6563 if ( !LookupAttr(row
, col
, &attr
) )
6565 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6566 CacheAttr(row
, col
, attr
);
6570 attr
->SetDefAttr(m_defaultCellAttr
);
6574 attr
= m_defaultCellAttr
;
6581 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6583 wxGridCellAttr
*attr
;
6584 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6586 wxASSERT_MSG( m_table
,
6587 _T("we may only be called if CanHaveAttributes() "
6588 "returned TRUE and then m_table should be !NULL") );
6590 attr
= m_table
->GetAttr(row
, col
);
6593 attr
= new wxGridCellAttr
;
6595 // artificially inc the ref count to match DecRef() in caller
6598 m_table
->SetAttr(attr
, row
, col
);
6601 CacheAttr(row
, col
, attr
);
6603 attr
->SetDefAttr(m_defaultCellAttr
);
6607 // ----------------------------------------------------------------------------
6608 // setting cell attributes: this is forwarded to the table
6609 // ----------------------------------------------------------------------------
6611 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6613 if ( CanHaveAttributes() )
6615 m_table
->SetRowAttr(attr
, row
);
6623 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6625 if ( CanHaveAttributes() )
6627 m_table
->SetColAttr(attr
, col
);
6635 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6637 if ( CanHaveAttributes() )
6639 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6640 attr
->SetBackgroundColour(colour
);
6645 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6647 if ( CanHaveAttributes() )
6649 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6650 attr
->SetTextColour(colour
);
6655 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6657 if ( CanHaveAttributes() )
6659 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6660 attr
->SetFont(font
);
6665 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6667 if ( CanHaveAttributes() )
6669 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6670 attr
->SetAlignment(horiz
, vert
);
6675 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6677 if ( CanHaveAttributes() )
6679 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6680 attr
->SetRenderer(renderer
);
6685 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6687 if ( CanHaveAttributes() )
6689 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6690 attr
->SetEditor(editor
);
6695 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6697 if ( CanHaveAttributes() )
6699 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6700 attr
->SetReadOnly(isReadOnly
);
6705 // ----------------------------------------------------------------------------
6706 // Data type registration
6707 // ----------------------------------------------------------------------------
6709 void wxGrid::RegisterDataType(const wxString
& typeName
,
6710 wxGridCellRenderer
* renderer
,
6711 wxGridCellEditor
* editor
)
6713 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6717 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6719 wxString typeName
= m_table
->GetTypeName(row
, col
);
6720 return GetDefaultEditorForType(typeName
);
6723 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6725 wxString typeName
= m_table
->GetTypeName(row
, col
);
6726 return GetDefaultRendererForType(typeName
);
6730 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6732 int index
= m_typeRegistry
->FindDataType(typeName
);
6734 // Should we force the failure here or let it fallback to string handling???
6735 // wxFAIL_MSG(wxT("Unknown data type name"));
6738 return m_typeRegistry
->GetEditor(index
);
6742 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6744 int index
= m_typeRegistry
->FindDataType(typeName
);
6746 // Should we force the failure here or let it fallback to string handling???
6747 // wxFAIL_MSG(wxT("Unknown data type name"));
6750 return m_typeRegistry
->GetRenderer(index
);
6754 // ----------------------------------------------------------------------------
6756 // ----------------------------------------------------------------------------
6758 void wxGrid::EnableDragRowSize( bool enable
)
6760 m_canDragRowSize
= enable
;
6764 void wxGrid::EnableDragColSize( bool enable
)
6766 m_canDragColSize
= enable
;
6769 void wxGrid::EnableDragGridSize( bool enable
)
6771 m_canDragGridSize
= enable
;
6775 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6777 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6779 if ( resizeExistingRows
)
6787 void wxGrid::SetRowSize( int row
, int height
)
6789 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6791 if ( m_rowHeights
.IsEmpty() )
6793 // need to really create the array
6797 int h
= wxMax( 0, height
);
6798 int diff
= h
- m_rowHeights
[row
];
6800 m_rowHeights
[row
] = h
;
6802 for ( i
= row
; i
< m_numRows
; i
++ )
6804 m_rowBottoms
[i
] += diff
;
6809 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6811 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6813 if ( resizeExistingCols
)
6821 void wxGrid::SetColSize( int col
, int width
)
6823 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6825 // should we check that it's bigger than GetColMinimalWidth(col) here?
6827 if ( m_colWidths
.IsEmpty() )
6829 // need to really create the array
6833 int w
= wxMax( 0, width
);
6834 int diff
= w
- m_colWidths
[col
];
6835 m_colWidths
[col
] = w
;
6838 for ( i
= col
; i
< m_numCols
; i
++ )
6840 m_colRights
[i
] += diff
;
6846 void wxGrid::SetColMinimalWidth( int col
, int width
)
6848 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6851 int wxGrid::GetColMinimalWidth(int col
) const
6853 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6854 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6857 // ----------------------------------------------------------------------------
6859 // ----------------------------------------------------------------------------
6861 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6863 wxClientDC
dc(m_gridWin
);
6865 wxCoord width
, widthMax
= 0;
6866 for ( int row
= 0; row
< m_numRows
; row
++ )
6868 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6869 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6872 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6873 if ( width
> widthMax
)
6882 // now also compare with the column label width
6883 dc
.SetFont( GetLabelFont() );
6884 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6885 if ( width
> widthMax
)
6892 // empty column - give default width (notice that if widthMax is less
6893 // than default width but != 0, it's ok)
6894 widthMax
= m_defaultColWidth
;
6898 // leave some space around text
6902 SetColSize(col
, widthMax
);
6905 SetColMinimalWidth(col
, widthMax
);
6909 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
6911 int width
= m_rowLabelWidth
;
6913 for ( int col
= 0; col
< m_numCols
; col
++ )
6917 AutoSizeColumn(col
, setAsMin
);
6920 width
+= GetColWidth(col
);
6926 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
6928 int height
= m_colLabelHeight
;
6930 for ( int row
= 0; row
< m_numRows
; row
++ )
6932 // if ( !calcOnly ) AutoSizeRow(row, setAsMin) -- TODO
6934 height
+= GetRowHeight(row
);
6940 void wxGrid::AutoSize()
6943 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
6946 wxSize
wxGrid::DoGetBestSize() const
6948 // don't set sizes, only calculate them
6949 wxGrid
*self
= (wxGrid
*)this; // const_cast
6951 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
6952 self
->SetOrCalcRowSizes(TRUE
));
6960 // ----------------------------------------------------------------------------
6961 // cell value accessor functions
6962 // ----------------------------------------------------------------------------
6964 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6968 m_table
->SetValue( row
, col
, s
.c_str() );
6969 if ( !GetBatchCount() )
6971 wxClientDC
dc( m_gridWin
);
6973 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6976 if ( m_currentCellCoords
.GetRow() == row
&&
6977 m_currentCellCoords
.GetCol() == col
&&
6978 IsCellEditControlEnabled())
6980 HideCellEditControl();
6981 ShowCellEditControl(); // will reread data from table
6988 // ------ Block, row and col selection
6991 void wxGrid::SelectRow( int row
, bool addToSelected
)
6995 if ( IsSelection() && addToSelected
)
6998 bool need_refresh
[4];
7002 need_refresh
[3] = FALSE
;
7006 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7007 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7008 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7009 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7013 need_refresh
[0] = TRUE
;
7014 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
7015 wxGridCellCoords ( oldTop
- 1,
7017 m_selectedTopLeft
.SetRow( row
);
7022 need_refresh
[1] = TRUE
;
7023 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
7024 wxGridCellCoords ( oldBottom
,
7027 m_selectedTopLeft
.SetCol( 0 );
7030 if ( oldBottom
< row
)
7032 need_refresh
[2] = TRUE
;
7033 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
7034 wxGridCellCoords ( row
,
7036 m_selectedBottomRight
.SetRow( row
);
7039 if ( oldRight
< m_numCols
- 1 )
7041 need_refresh
[3] = TRUE
;
7042 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7044 wxGridCellCoords ( oldBottom
,
7046 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
7049 for (i
= 0; i
< 4; i
++ )
7050 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7051 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7055 r
= SelectionToDeviceRect();
7057 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7059 m_selectedTopLeft
.Set( row
, 0 );
7060 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7061 r
= SelectionToDeviceRect();
7062 m_gridWin
->Refresh( FALSE
, &r
);
7065 wxGridRangeSelectEvent
gridEvt( GetId(),
7066 wxEVT_GRID_RANGE_SELECT
,
7069 m_selectedBottomRight
);
7071 GetEventHandler()->ProcessEvent(gridEvt
);
7075 void wxGrid::SelectCol( int col
, bool addToSelected
)
7077 if ( IsSelection() && addToSelected
)
7080 bool need_refresh
[4];
7084 need_refresh
[3] = FALSE
;
7087 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7088 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7089 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7090 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7092 if ( oldLeft
> col
)
7094 need_refresh
[0] = TRUE
;
7095 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7096 wxGridCellCoords ( m_numRows
- 1,
7098 m_selectedTopLeft
.SetCol( col
);
7103 need_refresh
[1] = TRUE
;
7104 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7105 wxGridCellCoords ( oldTop
- 1,
7107 m_selectedTopLeft
.SetRow( 0 );
7110 if ( oldRight
< col
)
7112 need_refresh
[2] = TRUE
;
7113 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7114 wxGridCellCoords ( m_numRows
- 1,
7116 m_selectedBottomRight
.SetCol( col
);
7119 if ( oldBottom
< m_numRows
- 1 )
7121 need_refresh
[3] = TRUE
;
7122 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7124 wxGridCellCoords ( m_numRows
- 1,
7126 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7129 for (i
= 0; i
< 4; i
++ )
7130 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7131 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7137 r
= SelectionToDeviceRect();
7139 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7141 m_selectedTopLeft
.Set( 0, col
);
7142 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7143 r
= SelectionToDeviceRect();
7144 m_gridWin
->Refresh( FALSE
, &r
);
7147 wxGridRangeSelectEvent
gridEvt( GetId(),
7148 wxEVT_GRID_RANGE_SELECT
,
7151 m_selectedBottomRight
);
7153 GetEventHandler()->ProcessEvent(gridEvt
);
7157 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7160 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7162 if ( topRow
> bottomRow
)
7169 if ( leftCol
> rightCol
)
7176 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7177 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7179 if ( m_selectedTopLeft
!= updateTopLeft
||
7180 m_selectedBottomRight
!= updateBottomRight
)
7182 // Compute two optimal update rectangles:
7183 // Either one rectangle is a real subset of the
7184 // other, or they are (almost) disjoint!
7186 bool need_refresh
[4];
7190 need_refresh
[3] = FALSE
;
7193 // Store intermediate values
7194 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7195 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7196 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7197 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7199 // Determine the outer/inner coordinates.
7200 if (oldLeft
> leftCol
)
7206 if (oldTop
> topRow
)
7212 if (oldRight
< rightCol
)
7215 oldRight
= rightCol
;
7218 if (oldBottom
< bottomRow
)
7221 oldBottom
= bottomRow
;
7225 // Now, either the stuff marked old is the outer
7226 // rectangle or we don't have a situation where one
7227 // is contained in the other.
7229 if ( oldLeft
< leftCol
)
7231 need_refresh
[0] = TRUE
;
7232 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7234 wxGridCellCoords ( oldBottom
,
7238 if ( oldTop
< topRow
)
7240 need_refresh
[1] = TRUE
;
7241 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7243 wxGridCellCoords ( topRow
- 1,
7247 if ( oldRight
> rightCol
)
7249 need_refresh
[2] = TRUE
;
7250 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7252 wxGridCellCoords ( oldBottom
,
7256 if ( oldBottom
> bottomRow
)
7258 need_refresh
[3] = TRUE
;
7259 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7261 wxGridCellCoords ( oldBottom
,
7267 m_selectedTopLeft
= updateTopLeft
;
7268 m_selectedBottomRight
= updateBottomRight
;
7270 // various Refresh() calls
7271 for (i
= 0; i
< 4; i
++ )
7272 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7273 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7276 // only generate an event if the block is not being selected by
7277 // dragging the mouse (in which case the event will be generated in
7278 // the mouse event handler)
7279 if ( !m_isDragging
)
7281 wxGridRangeSelectEvent
gridEvt( GetId(),
7282 wxEVT_GRID_RANGE_SELECT
,
7285 m_selectedBottomRight
);
7287 GetEventHandler()->ProcessEvent(gridEvt
);
7291 void wxGrid::SelectAll()
7293 m_selectedTopLeft
.Set( 0, 0 );
7294 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7296 m_gridWin
->Refresh();
7300 void wxGrid::ClearSelection()
7302 m_selectedTopLeft
= wxGridNoCellCoords
;
7303 m_selectedBottomRight
= wxGridNoCellCoords
;
7307 // This function returns the rectangle that encloses the given block
7308 // in device coords clipped to the client size of the grid window.
7310 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7311 const wxGridCellCoords
&bottomRight
)
7313 wxRect
rect( wxGridNoCellRect
);
7316 cellRect
= CellToRect( topLeft
);
7317 if ( cellRect
!= wxGridNoCellRect
)
7323 rect
= wxRect( 0, 0, 0, 0 );
7326 cellRect
= CellToRect( bottomRight
);
7327 if ( cellRect
!= wxGridNoCellRect
)
7333 return wxGridNoCellRect
;
7336 // convert to scrolled coords
7338 int left
, top
, right
, bottom
;
7339 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7340 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7343 m_gridWin
->GetClientSize( &cw
, &ch
);
7345 rect
.SetLeft( wxMax(0, left
) );
7346 rect
.SetTop( wxMax(0, top
) );
7347 rect
.SetRight( wxMin(cw
, right
) );
7348 rect
.SetBottom( wxMin(ch
, bottom
) );
7356 // ------ Grid event classes
7359 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7361 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7362 int row
, int col
, int x
, int y
,
7363 bool control
, bool shift
, bool alt
, bool meta
)
7364 : wxNotifyEvent( type
, id
)
7370 m_control
= control
;
7375 SetEventObject(obj
);
7379 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7381 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7382 int rowOrCol
, int x
, int y
,
7383 bool control
, bool shift
, bool alt
, bool meta
)
7384 : wxNotifyEvent( type
, id
)
7386 m_rowOrCol
= rowOrCol
;
7389 m_control
= control
;
7394 SetEventObject(obj
);
7398 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7400 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7401 const wxGridCellCoords
& topLeft
,
7402 const wxGridCellCoords
& bottomRight
,
7403 bool control
, bool shift
, bool alt
, bool meta
)
7404 : wxNotifyEvent( type
, id
)
7406 m_topLeft
= topLeft
;
7407 m_bottomRight
= bottomRight
;
7408 m_control
= control
;
7413 SetEventObject(obj
);
7417 #endif // ifndef wxUSE_NEW_GRID