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
;
2948 // ----------------------------------------------------------------------------
2949 // the idea is to call these functions only when necessary because they create
2950 // quite big arrays which eat memory mostly unnecessary - in particular, if
2951 // default widths/heights are used for all rows/columns, we may not use these
2954 // with some extra code, it should be possible to only store the
2955 // widths/heights different from default ones but this will be done later...
2956 // ----------------------------------------------------------------------------
2958 void wxGrid::InitRowHeights()
2960 m_rowHeights
.Empty();
2961 m_rowBottoms
.Empty();
2963 m_rowHeights
.Alloc( m_numRows
);
2964 m_rowBottoms
.Alloc( m_numRows
);
2967 for ( int i
= 0; i
< m_numRows
; i
++ )
2969 m_rowHeights
.Add( m_defaultRowHeight
);
2970 rowBottom
+= m_defaultRowHeight
;
2971 m_rowBottoms
.Add( rowBottom
);
2975 void wxGrid::InitColWidths()
2977 m_colWidths
.Empty();
2978 m_colRights
.Empty();
2980 m_colWidths
.Alloc( m_numCols
);
2981 m_colRights
.Alloc( m_numCols
);
2983 for ( int i
= 0; i
< m_numCols
; i
++ )
2985 m_colWidths
.Add( m_defaultColWidth
);
2986 colRight
+= m_defaultColWidth
;
2987 m_colRights
.Add( colRight
);
2991 int wxGrid::GetColWidth(int col
) const
2993 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2996 int wxGrid::GetColLeft(int col
) const
2998 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2999 : m_colRights
[col
] - m_colWidths
[col
];
3002 int wxGrid::GetColRight(int col
) const
3004 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3008 int wxGrid::GetRowHeight(int row
) const
3010 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3013 int wxGrid::GetRowTop(int row
) const
3015 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3016 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3019 int wxGrid::GetRowBottom(int row
) const
3021 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3022 : m_rowBottoms
[row
];
3025 void wxGrid::CalcDimensions()
3028 GetClientSize( &cw
, &ch
);
3030 if ( m_numRows
> 0 && m_numCols
> 0 )
3032 int right
= GetColRight( m_numCols
-1 ) + 50;
3033 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
3035 // TODO: restore the scroll position that we had before sizing
3038 GetViewStart( &x
, &y
);
3039 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3040 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3046 void wxGrid::CalcWindowSizes()
3049 GetClientSize( &cw
, &ch
);
3051 if ( m_cornerLabelWin
->IsShown() )
3052 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3054 if ( m_colLabelWin
->IsShown() )
3055 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3057 if ( m_rowLabelWin
->IsShown() )
3058 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3060 if ( m_gridWin
->IsShown() )
3061 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3065 // this is called when the grid table sends a message to say that it
3066 // has been redimensioned
3068 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3072 // if we were using the default widths/heights so far, we must change them
3074 if ( m_colWidths
.IsEmpty() )
3079 if ( m_rowHeights
.IsEmpty() )
3084 switch ( msg
.GetId() )
3086 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3088 size_t pos
= msg
.GetCommandInt();
3089 int numRows
= msg
.GetCommandInt2();
3090 for ( i
= 0; i
< numRows
; i
++ )
3092 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3093 m_rowBottoms
.Insert( 0, pos
);
3095 m_numRows
+= numRows
;
3098 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3100 for ( i
= pos
; i
< m_numRows
; i
++ )
3102 bottom
+= m_rowHeights
[i
];
3103 m_rowBottoms
[i
] = bottom
;
3109 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3111 int numRows
= msg
.GetCommandInt();
3112 for ( i
= 0; i
< numRows
; i
++ )
3114 m_rowHeights
.Add( m_defaultRowHeight
);
3115 m_rowBottoms
.Add( 0 );
3118 int oldNumRows
= m_numRows
;
3119 m_numRows
+= numRows
;
3122 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3124 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3126 bottom
+= m_rowHeights
[i
];
3127 m_rowBottoms
[i
] = bottom
;
3133 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3135 size_t pos
= msg
.GetCommandInt();
3136 int numRows
= msg
.GetCommandInt2();
3137 for ( i
= 0; i
< numRows
; i
++ )
3139 m_rowHeights
.Remove( pos
);
3140 m_rowBottoms
.Remove( pos
);
3142 m_numRows
-= numRows
;
3147 m_colWidths
.Clear();
3148 m_colRights
.Clear();
3149 m_currentCellCoords
= wxGridNoCellCoords
;
3153 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3154 m_currentCellCoords
.Set( 0, 0 );
3157 for ( i
= 0; i
< m_numRows
; i
++ )
3159 h
+= m_rowHeights
[i
];
3160 m_rowBottoms
[i
] = h
;
3168 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3170 size_t pos
= msg
.GetCommandInt();
3171 int numCols
= msg
.GetCommandInt2();
3172 for ( i
= 0; i
< numCols
; i
++ )
3174 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3175 m_colRights
.Insert( 0, pos
);
3177 m_numCols
+= numCols
;
3180 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3182 for ( i
= pos
; i
< m_numCols
; i
++ )
3184 right
+= m_colWidths
[i
];
3185 m_colRights
[i
] = right
;
3191 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3193 int numCols
= msg
.GetCommandInt();
3194 for ( i
= 0; i
< numCols
; i
++ )
3196 m_colWidths
.Add( m_defaultColWidth
);
3197 m_colRights
.Add( 0 );
3200 int oldNumCols
= m_numCols
;
3201 m_numCols
+= numCols
;
3204 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3206 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3208 right
+= m_colWidths
[i
];
3209 m_colRights
[i
] = right
;
3215 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3217 size_t pos
= msg
.GetCommandInt();
3218 int numCols
= msg
.GetCommandInt2();
3219 for ( i
= 0; i
< numCols
; i
++ )
3221 m_colWidths
.Remove( pos
);
3222 m_colRights
.Remove( pos
);
3224 m_numCols
-= numCols
;
3228 #if 0 // leave the row alone here so that AppendCols will work subsequently
3230 m_rowHeights
.Clear();
3231 m_rowBottoms
.Clear();
3233 m_currentCellCoords
= wxGridNoCellCoords
;
3237 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3238 m_currentCellCoords
.Set( 0, 0 );
3241 for ( i
= 0; i
< m_numCols
; i
++ )
3243 w
+= m_colWidths
[i
];
3256 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3258 wxRegionIterator
iter( reg
);
3261 m_rowLabelsExposed
.Empty();
3268 // TODO: remove this when we can...
3269 // There is a bug in wxMotif that gives garbage update
3270 // rectangles if you jump-scroll a long way by clicking the
3271 // scrollbar with middle button. This is a work-around
3273 #if defined(__WXMOTIF__)
3275 m_gridWin
->GetClientSize( &cw
, &ch
);
3276 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3277 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3280 // logical bounds of update region
3283 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3284 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3286 // find the row labels within these bounds
3289 for ( row
= 0; row
< m_numRows
; row
++ )
3291 if ( GetRowBottom(row
) < top
)
3294 if ( GetRowTop(row
) > bottom
)
3297 m_rowLabelsExposed
.Add( row
);
3305 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3307 wxRegionIterator
iter( reg
);
3310 m_colLabelsExposed
.Empty();
3317 // TODO: remove this when we can...
3318 // There is a bug in wxMotif that gives garbage update
3319 // rectangles if you jump-scroll a long way by clicking the
3320 // scrollbar with middle button. This is a work-around
3322 #if defined(__WXMOTIF__)
3324 m_gridWin
->GetClientSize( &cw
, &ch
);
3325 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3326 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3329 // logical bounds of update region
3332 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3333 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3335 // find the cells within these bounds
3338 for ( col
= 0; col
< m_numCols
; col
++ )
3340 if ( GetColRight(col
) < left
)
3343 if ( GetColLeft(col
) > right
)
3346 m_colLabelsExposed
.Add( col
);
3354 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3356 wxRegionIterator
iter( reg
);
3359 m_cellsExposed
.Empty();
3360 m_rowsExposed
.Empty();
3361 m_colsExposed
.Empty();
3363 int left
, top
, right
, bottom
;
3368 // TODO: remove this when we can...
3369 // There is a bug in wxMotif that gives garbage update
3370 // rectangles if you jump-scroll a long way by clicking the
3371 // scrollbar with middle button. This is a work-around
3373 #if defined(__WXMOTIF__)
3375 m_gridWin
->GetClientSize( &cw
, &ch
);
3376 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3377 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3378 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3379 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3382 // logical bounds of update region
3384 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3385 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3387 // find the cells within these bounds
3390 for ( row
= 0; row
< m_numRows
; row
++ )
3392 if ( GetRowBottom(row
) <= top
)
3395 if ( GetRowTop(row
) > bottom
)
3398 m_rowsExposed
.Add( row
);
3400 for ( col
= 0; col
< m_numCols
; col
++ )
3402 if ( GetColRight(col
) <= left
)
3405 if ( GetColLeft(col
) > right
)
3408 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3409 m_colsExposed
.Add( col
);
3410 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3419 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3422 wxPoint
pos( event
.GetPosition() );
3423 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3425 if ( event
.Dragging() )
3427 m_isDragging
= TRUE
;
3429 if ( event
.LeftIsDown() )
3431 switch( m_cursorMode
)
3433 case WXGRID_CURSOR_RESIZE_ROW
:
3435 int cw
, ch
, left
, dummy
;
3436 m_gridWin
->GetClientSize( &cw
, &ch
);
3437 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3439 wxClientDC
dc( m_gridWin
);
3441 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3442 dc
.SetLogicalFunction(wxINVERT
);
3443 if ( m_dragLastPos
>= 0 )
3445 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3447 dc
.DrawLine( left
, y
, left
+cw
, y
);
3452 case WXGRID_CURSOR_SELECT_ROW
:
3453 if ( (row
= YToRow( y
)) >= 0 &&
3454 !IsInSelection( row
, 0 ) )
3456 SelectRow( row
, TRUE
);
3459 // default label to suppress warnings about "enumeration value
3460 // 'xxx' not handled in switch
3468 m_isDragging
= FALSE
;
3471 // ------------ Entering or leaving the window
3473 if ( event
.Entering() || event
.Leaving() )
3475 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3479 // ------------ Left button pressed
3481 else if ( event
.LeftDown() )
3483 // don't send a label click event for a hit on the
3484 // edge of the row label - this is probably the user
3485 // wanting to resize the row
3487 if ( YToEdgeOfRow(y
) < 0 )
3491 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3493 SelectRow( row
, event
.ShiftDown() );
3494 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3499 // starting to drag-resize a row
3501 if ( CanDragRowSize() )
3502 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3507 // ------------ Left double click
3509 else if (event
.LeftDClick() )
3511 if ( YToEdgeOfRow(y
) < 0 )
3514 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3519 // ------------ Left button released
3521 else if ( event
.LeftUp() )
3523 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3525 DoEndDragResizeRow();
3527 // Note: we are ending the event *after* doing
3528 // default processing in this case
3530 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3533 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3538 // ------------ Right button down
3540 else if ( event
.RightDown() )
3543 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3545 // no default action at the moment
3550 // ------------ Right double click
3552 else if ( event
.RightDClick() )
3555 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3557 // no default action at the moment
3562 // ------------ No buttons down and mouse moving
3564 else if ( event
.Moving() )
3566 m_dragRowOrCol
= YToEdgeOfRow( y
);
3567 if ( m_dragRowOrCol
>= 0 )
3569 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3571 // don't capture the mouse yet
3572 if ( CanDragRowSize() )
3573 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3576 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3578 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3584 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3587 wxPoint
pos( event
.GetPosition() );
3588 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3590 if ( event
.Dragging() )
3592 m_isDragging
= TRUE
;
3594 if ( event
.LeftIsDown() )
3596 switch( m_cursorMode
)
3598 case WXGRID_CURSOR_RESIZE_COL
:
3600 int cw
, ch
, dummy
, top
;
3601 m_gridWin
->GetClientSize( &cw
, &ch
);
3602 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3604 wxClientDC
dc( m_gridWin
);
3607 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3608 GetColMinimalWidth(m_dragRowOrCol
));
3609 dc
.SetLogicalFunction(wxINVERT
);
3610 if ( m_dragLastPos
>= 0 )
3612 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3614 dc
.DrawLine( x
, top
, x
, top
+ch
);
3619 case WXGRID_CURSOR_SELECT_COL
:
3620 if ( (col
= XToCol( x
)) >= 0 &&
3621 !IsInSelection( 0, col
) )
3623 SelectCol( col
, TRUE
);
3626 // default label to suppress warnings about "enumeration value
3627 // 'xxx' not handled in switch
3635 m_isDragging
= FALSE
;
3638 // ------------ Entering or leaving the window
3640 if ( event
.Entering() || event
.Leaving() )
3642 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3646 // ------------ Left button pressed
3648 else if ( event
.LeftDown() )
3650 // don't send a label click event for a hit on the
3651 // edge of the col label - this is probably the user
3652 // wanting to resize the col
3654 if ( XToEdgeOfCol(x
) < 0 )
3658 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3660 SelectCol( col
, event
.ShiftDown() );
3661 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3666 // starting to drag-resize a col
3668 if ( CanDragColSize() )
3669 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3674 // ------------ Left double click
3676 if ( event
.LeftDClick() )
3678 if ( XToEdgeOfCol(x
) < 0 )
3681 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3686 // ------------ Left button released
3688 else if ( event
.LeftUp() )
3690 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3692 DoEndDragResizeCol();
3694 // Note: we are ending the event *after* doing
3695 // default processing in this case
3697 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3700 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3705 // ------------ Right button down
3707 else if ( event
.RightDown() )
3710 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3712 // no default action at the moment
3717 // ------------ Right double click
3719 else if ( event
.RightDClick() )
3722 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3724 // no default action at the moment
3729 // ------------ No buttons down and mouse moving
3731 else if ( event
.Moving() )
3733 m_dragRowOrCol
= XToEdgeOfCol( x
);
3734 if ( m_dragRowOrCol
>= 0 )
3736 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3738 // don't capture the cursor yet
3739 if ( CanDragColSize() )
3740 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3743 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3745 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3751 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3753 if ( event
.LeftDown() )
3755 // indicate corner label by having both row and
3758 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3764 else if ( event
.LeftDClick() )
3766 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3769 else if ( event
.RightDown() )
3771 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3773 // no default action at the moment
3777 else if ( event
.RightDClick() )
3779 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3781 // no default action at the moment
3786 void wxGrid::ChangeCursorMode(CursorMode mode
,
3791 static const wxChar
*cursorModes
[] =
3800 wxLogTrace(_T("grid"),
3801 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3802 win
== m_colLabelWin
? _T("colLabelWin")
3803 : win
? _T("rowLabelWin")
3805 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3806 #endif // __WXDEBUG__
3808 if ( mode
== m_cursorMode
)
3813 // by default use the grid itself
3819 m_winCapture
->ReleaseMouse();
3820 m_winCapture
= (wxWindow
*)NULL
;
3823 m_cursorMode
= mode
;
3825 switch ( m_cursorMode
)
3827 case WXGRID_CURSOR_RESIZE_ROW
:
3828 win
->SetCursor( m_rowResizeCursor
);
3831 case WXGRID_CURSOR_RESIZE_COL
:
3832 win
->SetCursor( m_colResizeCursor
);
3836 win
->SetCursor( *wxSTANDARD_CURSOR
);
3839 // we need to capture mouse when resizing
3840 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3841 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3843 if ( captureMouse
&& resize
)
3845 win
->CaptureMouse();
3850 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3853 wxPoint
pos( event
.GetPosition() );
3854 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3856 wxGridCellCoords coords
;
3857 XYToCell( x
, y
, coords
);
3859 if ( event
.Dragging() )
3861 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3863 // Don't start doing anything until the mouse has been drug at
3864 // least 3 pixels in any direction...
3867 if (m_startDragPos
== wxDefaultPosition
)
3869 m_startDragPos
= pos
;
3872 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3876 m_isDragging
= TRUE
;
3877 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3879 // Hide the edit control, so it
3880 // won't interfer with drag-shrinking.
3881 if ( IsCellEditControlEnabled() )
3883 HideCellEditControl();
3884 SaveEditControlValue();
3887 // Have we captured the mouse yet?
3890 m_winCapture
= m_gridWin
;
3891 m_winCapture
->CaptureMouse();
3894 if ( coords
!= wxGridNoCellCoords
)
3896 if ( !IsSelection() )
3898 SelectBlock( coords
, coords
);
3902 SelectBlock( m_currentCellCoords
, coords
);
3905 if (! IsVisible(coords
))
3907 MakeCellVisible(coords
);
3908 // TODO: need to introduce a delay or something here. The
3909 // scrolling is way to fast, at least on MSW.
3913 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3915 int cw
, ch
, left
, dummy
;
3916 m_gridWin
->GetClientSize( &cw
, &ch
);
3917 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3919 wxClientDC
dc( m_gridWin
);
3921 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3922 dc
.SetLogicalFunction(wxINVERT
);
3923 if ( m_dragLastPos
>= 0 )
3925 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3927 dc
.DrawLine( left
, y
, left
+cw
, y
);
3930 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3932 int cw
, ch
, dummy
, top
;
3933 m_gridWin
->GetClientSize( &cw
, &ch
);
3934 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3936 wxClientDC
dc( m_gridWin
);
3938 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3939 GetColMinimalWidth(m_dragRowOrCol
) );
3940 dc
.SetLogicalFunction(wxINVERT
);
3941 if ( m_dragLastPos
>= 0 )
3943 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3945 dc
.DrawLine( x
, top
, x
, top
+ch
);
3952 m_isDragging
= FALSE
;
3953 m_startDragPos
= wxDefaultPosition
;
3955 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3956 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3959 if ( event
.Entering() || event
.Leaving() )
3961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3962 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3967 // ------------ Left button pressed
3969 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
3971 if ( event
.ShiftDown() )
3973 SelectBlock( m_currentCellCoords
, coords
);
3975 else if ( XToEdgeOfCol(x
) < 0 &&
3976 YToEdgeOfRow(y
) < 0 )
3978 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3983 DisableCellEditControl();
3984 MakeCellVisible( coords
);
3986 // if this is the second click on this cell then start
3988 if ( m_waitForSlowClick
&&
3989 (coords
== m_currentCellCoords
) &&
3990 CanEnableCellControl())
3992 EnableCellEditControl();
3994 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3995 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
3998 m_waitForSlowClick
= FALSE
;
4002 SetCurrentCell( coords
);
4003 m_waitForSlowClick
= TRUE
;
4010 // ------------ Left double click
4012 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4014 DisableCellEditControl();
4016 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4018 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4026 // ------------ Left button released
4028 else if ( event
.LeftUp() )
4030 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4032 if ( IsSelection() )
4036 m_winCapture
->ReleaseMouse();
4037 m_winCapture
= NULL
;
4039 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4042 // Show the edit control, if it has been hidden for
4044 ShowCellEditControl();
4046 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4048 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4049 DoEndDragResizeRow();
4051 // Note: we are ending the event *after* doing
4052 // default processing in this case
4054 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4056 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4058 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4059 DoEndDragResizeCol();
4061 // Note: we are ending the event *after* doing
4062 // default processing in this case
4064 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4071 // ------------ Right button down
4073 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4075 DisableCellEditControl();
4076 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4081 // no default action at the moment
4086 // ------------ Right double click
4088 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4090 DisableCellEditControl();
4091 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4096 // no default action at the moment
4100 // ------------ Moving and no button action
4102 else if ( event
.Moving() && !event
.IsButton() )
4104 int dragRow
= YToEdgeOfRow( y
);
4105 int dragCol
= XToEdgeOfCol( x
);
4107 // Dragging on the corner of a cell to resize in both
4108 // directions is not implemented yet...
4110 if ( dragRow
>= 0 && dragCol
>= 0 )
4112 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4118 m_dragRowOrCol
= dragRow
;
4120 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4122 if ( CanDragRowSize() && CanDragGridSize() )
4123 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4128 m_dragRowOrCol
= dragCol
;
4136 m_dragRowOrCol
= dragCol
;
4138 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4140 if ( CanDragColSize() && CanDragGridSize() )
4141 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4147 // Neither on a row or col edge
4149 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4151 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4157 void wxGrid::DoEndDragResizeRow()
4159 if ( m_dragLastPos
>= 0 )
4161 // erase the last line and resize the row
4163 int cw
, ch
, left
, dummy
;
4164 m_gridWin
->GetClientSize( &cw
, &ch
);
4165 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4167 wxClientDC
dc( m_gridWin
);
4169 dc
.SetLogicalFunction( wxINVERT
);
4170 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4171 HideCellEditControl();
4172 SaveEditControlValue();
4174 int rowTop
= GetRowTop(m_dragRowOrCol
);
4175 SetRowSize( m_dragRowOrCol
,
4176 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4178 if ( !GetBatchCount() )
4180 // Only needed to get the correct rect.y:
4181 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4183 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4184 rect
.width
= m_rowLabelWidth
;
4185 rect
.height
= ch
- rect
.y
;
4186 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4188 m_gridWin
->Refresh( FALSE
, &rect
);
4191 ShowCellEditControl();
4196 void wxGrid::DoEndDragResizeCol()
4198 if ( m_dragLastPos
>= 0 )
4200 // erase the last line and resize the col
4202 int cw
, ch
, dummy
, top
;
4203 m_gridWin
->GetClientSize( &cw
, &ch
);
4204 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4206 wxClientDC
dc( m_gridWin
);
4208 dc
.SetLogicalFunction( wxINVERT
);
4209 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4210 HideCellEditControl();
4211 SaveEditControlValue();
4213 int colLeft
= GetColLeft(m_dragRowOrCol
);
4214 SetColSize( m_dragRowOrCol
,
4215 wxMax( m_dragLastPos
- colLeft
,
4216 GetColMinimalWidth(m_dragRowOrCol
) ) );
4218 if ( !GetBatchCount() )
4220 // Only needed to get the correct rect.x:
4221 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4223 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4224 rect
.width
= cw
- rect
.x
;
4225 rect
.height
= m_colLabelHeight
;
4226 m_colLabelWin
->Refresh( TRUE
, &rect
);
4228 m_gridWin
->Refresh( FALSE
, &rect
);
4231 ShowCellEditControl();
4238 // ------ interaction with data model
4240 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4242 switch ( msg
.GetId() )
4244 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4245 return GetModelValues();
4247 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4248 return SetModelValues();
4250 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4251 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4252 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4253 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4254 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4255 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4256 return Redimension( msg
);
4265 // The behaviour of this function depends on the grid table class
4266 // Clear() function. For the default wxGridStringTable class the
4267 // behavious is to replace all cell contents with wxEmptyString but
4268 // not to change the number of rows or cols.
4270 void wxGrid::ClearGrid()
4274 if (IsCellEditControlEnabled())
4275 DisableCellEditControl();
4278 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4283 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4285 // TODO: something with updateLabels flag
4289 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4295 if (IsCellEditControlEnabled())
4296 DisableCellEditControl();
4298 bool ok
= m_table
->InsertRows( pos
, numRows
);
4300 // the table will have sent the results of the insert row
4301 // operation to this view object as a grid table message
4305 if ( m_numCols
== 0 )
4307 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4309 // TODO: perhaps instead of appending the default number of cols
4310 // we should remember what the last non-zero number of cols was ?
4314 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4316 // if we have just inserted cols into an empty grid the current
4317 // cell will be undefined...
4319 SetCurrentCell( 0, 0 );
4323 if ( !GetBatchCount() ) Refresh();
4335 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4337 // TODO: something with updateLabels flag
4341 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4345 if ( m_table
&& m_table
->AppendRows( numRows
) )
4347 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4349 // if we have just inserted cols into an empty grid the current
4350 // cell will be undefined...
4352 SetCurrentCell( 0, 0 );
4355 // the table will have sent the results of the append row
4356 // operation to this view object as a grid table message
4359 if ( !GetBatchCount() ) Refresh();
4369 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4371 // TODO: something with updateLabels flag
4375 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4381 if (IsCellEditControlEnabled())
4382 DisableCellEditControl();
4384 if (m_table
->DeleteRows( pos
, numRows
))
4387 // the table will have sent the results of the delete row
4388 // operation to this view object as a grid table message
4391 if ( !GetBatchCount() ) Refresh();
4399 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4401 // TODO: something with updateLabels flag
4405 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4411 if (IsCellEditControlEnabled())
4412 DisableCellEditControl();
4414 bool ok
= m_table
->InsertCols( pos
, numCols
);
4416 // the table will have sent the results of the insert col
4417 // operation to this view object as a grid table message
4421 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4423 // if we have just inserted cols into an empty grid the current
4424 // cell will be undefined...
4426 SetCurrentCell( 0, 0 );
4430 if ( !GetBatchCount() ) Refresh();
4442 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4444 // TODO: something with updateLabels flag
4448 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4452 if ( m_table
&& m_table
->AppendCols( numCols
) )
4454 // the table will have sent the results of the append col
4455 // operation to this view object as a grid table message
4457 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4459 // if we have just inserted cols into an empty grid the current
4460 // cell will be undefined...
4462 SetCurrentCell( 0, 0 );
4466 if ( !GetBatchCount() ) Refresh();
4476 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4478 // TODO: something with updateLabels flag
4482 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4488 if (IsCellEditControlEnabled())
4489 DisableCellEditControl();
4491 if ( m_table
->DeleteCols( pos
, numCols
) )
4493 // the table will have sent the results of the delete col
4494 // operation to this view object as a grid table message
4497 if ( !GetBatchCount() ) Refresh();
4507 // ----- event handlers
4510 // Generate a grid event based on a mouse event and
4511 // return the result of ProcessEvent()
4513 bool wxGrid::SendEvent( const wxEventType type
,
4515 wxMouseEvent
& mouseEv
)
4517 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4519 int rowOrCol
= (row
== -1 ? col
: row
);
4521 wxGridSizeEvent
gridEvt( GetId(),
4525 mouseEv
.GetX(), mouseEv
.GetY(),
4526 mouseEv
.ControlDown(),
4527 mouseEv
.ShiftDown(),
4529 mouseEv
.MetaDown() );
4531 return GetEventHandler()->ProcessEvent(gridEvt
);
4533 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4535 wxGridRangeSelectEvent
gridEvt( GetId(),
4539 m_selectedBottomRight
,
4540 mouseEv
.ControlDown(),
4541 mouseEv
.ShiftDown(),
4543 mouseEv
.MetaDown() );
4545 return GetEventHandler()->ProcessEvent(gridEvt
);
4549 wxGridEvent
gridEvt( GetId(),
4553 mouseEv
.GetX(), mouseEv
.GetY(),
4554 mouseEv
.ControlDown(),
4555 mouseEv
.ShiftDown(),
4557 mouseEv
.MetaDown() );
4559 return GetEventHandler()->ProcessEvent(gridEvt
);
4564 // Generate a grid event of specified type and return the result
4565 // of ProcessEvent().
4567 bool wxGrid::SendEvent( const wxEventType type
,
4570 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4572 int rowOrCol
= (row
== -1 ? col
: row
);
4574 wxGridSizeEvent
gridEvt( GetId(),
4579 return GetEventHandler()->ProcessEvent(gridEvt
);
4583 wxGridEvent
gridEvt( GetId(),
4588 return GetEventHandler()->ProcessEvent(gridEvt
);
4593 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4595 wxPaintDC
dc( this );
4597 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4598 m_numRows
&& m_numCols
)
4600 m_currentCellCoords
.Set(0, 0);
4601 ShowCellEditControl();
4608 // This is just here to make sure that CalcDimensions gets called when
4609 // the grid view is resized... then the size event is skipped to allow
4610 // the box sizers to handle everything
4612 void wxGrid::OnSize( wxSizeEvent
& event
)
4619 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4621 if ( m_inOnKeyDown
)
4623 // shouldn't be here - we are going round in circles...
4625 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4628 m_inOnKeyDown
= TRUE
;
4630 // propagate the event up and see if it gets processed
4632 wxWindow
*parent
= GetParent();
4633 wxKeyEvent
keyEvt( event
);
4634 keyEvt
.SetEventObject( parent
);
4636 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4639 // TODO: Should also support Shift-cursor keys for
4640 // extending the selection. Maybe add a flag to
4641 // MoveCursorXXX() and MoveCursorXXXBlock() and
4642 // just send event.ShiftDown().
4644 // try local handlers
4646 switch ( event
.KeyCode() )
4649 if ( event
.ControlDown() )
4651 MoveCursorUpBlock();
4660 if ( event
.ControlDown() )
4662 MoveCursorDownBlock();
4671 if ( event
.ControlDown() )
4673 MoveCursorLeftBlock();
4682 if ( event
.ControlDown() )
4684 MoveCursorRightBlock();
4693 if ( event
.ControlDown() )
4695 event
.Skip(); // to let the edit control have the return
4704 if (event
.ShiftDown())
4711 if ( event
.ControlDown() )
4713 MakeCellVisible( 0, 0 );
4714 SetCurrentCell( 0, 0 );
4723 if ( event
.ControlDown() )
4725 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4726 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4743 if ( !IsEditable() )
4748 // Otherwise fall through to default
4751 // alphanumeric keys enable the cell edit control
4752 if ( !(event
.AltDown() ||
4754 event
.ControlDown()) &&
4755 isalnum(event
.KeyCode()) &&
4756 !IsCellEditControlEnabled() &&
4757 CanEnableCellControl() )
4759 EnableCellEditControl();
4760 int row
= m_currentCellCoords
.GetRow();
4761 int col
= m_currentCellCoords
.GetCol();
4762 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4763 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4768 // let others process char events with modifiers or all
4769 // char events for readonly cells
4776 m_inOnKeyDown
= FALSE
;
4780 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4784 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4786 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4788 // the event has been intercepted - do nothing
4793 m_currentCellCoords
!= wxGridNoCellCoords
)
4795 HideCellEditControl();
4796 DisableCellEditControl();
4798 // Clear the old current cell highlight
4799 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4801 // Otherwise refresh redraws the highlight!
4802 m_currentCellCoords
= coords
;
4804 m_gridWin
->Refresh( FALSE
, &r
);
4807 m_currentCellCoords
= coords
;
4811 wxClientDC
dc(m_gridWin
);
4814 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4815 DrawCellHighlight(dc
, attr
);
4818 if ( IsSelection() )
4820 wxRect
r( SelectionToDeviceRect() );
4822 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4829 // ------ functions to get/send data (see also public functions)
4832 bool wxGrid::GetModelValues()
4836 // all we need to do is repaint the grid
4838 m_gridWin
->Refresh();
4846 bool wxGrid::SetModelValues()
4852 for ( row
= 0; row
< m_numRows
; row
++ )
4854 for ( col
= 0; col
< m_numCols
; col
++ )
4856 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4868 // Note - this function only draws cells that are in the list of
4869 // exposed cells (usually set from the update region by
4870 // CalcExposedCells)
4872 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4874 if ( !m_numRows
|| !m_numCols
) return;
4877 size_t numCells
= m_cellsExposed
.GetCount();
4879 for ( i
= 0; i
< numCells
; i
++ )
4881 DrawCell( dc
, m_cellsExposed
[i
] );
4886 void wxGrid::DrawGridSpace( wxDC
& dc
)
4888 if ( m_numRows
&& m_numCols
)
4891 m_gridWin
->GetClientSize( &cw
, &ch
);
4894 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4896 if ( right
> GetColRight(m_numCols
-1) ||
4897 bottom
> GetRowBottom(m_numRows
-1) )
4900 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4902 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4903 dc
.SetPen( *wxTRANSPARENT_PEN
);
4905 if ( right
> GetColRight(m_numCols
-1) )
4906 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
4907 right
- GetColRight(m_numCols
-1), ch
);
4909 if ( bottom
> GetRowBottom(m_numRows
-1) )
4910 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
4911 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4917 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4919 int row
= coords
.GetRow();
4920 int col
= coords
.GetCol();
4922 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4925 // we draw the cell border ourselves
4926 #if !WXGRID_DRAW_LINES
4927 if ( m_gridLinesEnabled
)
4928 DrawCellBorder( dc
, coords
);
4931 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4933 bool isCurrent
= coords
== m_currentCellCoords
;
4936 rect
.x
= GetColLeft(col
);
4937 rect
.y
= GetRowTop(row
);
4938 rect
.width
= GetColWidth(col
) - 1;
4939 rect
.height
= GetRowHeight(row
) - 1;
4941 // if the editor is shown, we should use it and not the renderer
4942 if ( isCurrent
&& IsCellEditControlEnabled() )
4944 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4948 // but all the rest is drawn by the cell renderer and hence may be
4950 attr
->GetRenderer(this, row
, col
)->
4951 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4958 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4960 int row
= m_currentCellCoords
.GetRow();
4961 int col
= m_currentCellCoords
.GetCol();
4963 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4967 rect
.x
= GetColLeft(col
);
4968 rect
.y
= GetRowTop(row
);
4969 rect
.width
= GetColWidth(col
) - 1;
4970 rect
.height
= GetRowHeight(row
) - 1;
4972 // hmmm... what could we do here to show that the cell is disabled?
4973 // for now, I just draw a thinner border than for the other ones, but
4974 // it doesn't look really good
4975 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4976 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4978 dc
.DrawRectangle(rect
);
4981 // VZ: my experiments with 3d borders...
4983 // how to properly set colours for arbitrary bg?
4984 wxCoord x1
= rect
.x
,
4986 x2
= rect
.x
+ rect
.width
-1,
4987 y2
= rect
.y
+ rect
.height
-1;
4989 dc
.SetPen(*wxWHITE_PEN
);
4990 dc
.DrawLine(x1
, y1
, x2
, y1
);
4991 dc
.DrawLine(x1
, y1
, x1
, y2
);
4993 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4994 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4996 dc
.SetPen(*wxBLACK_PEN
);
4997 dc
.DrawLine(x1
, y2
, x2
, y2
);
4998 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5003 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5005 int row
= coords
.GetRow();
5006 int col
= coords
.GetCol();
5007 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5010 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5012 // right hand border
5014 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5015 GetColRight(col
), GetRowBottom(row
) );
5019 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5020 GetColRight(col
), GetRowBottom(row
) );
5023 void wxGrid::DrawHighlight(wxDC
& dc
)
5025 if ( IsCellEditControlEnabled() )
5027 // don't show highlight when the edit control is shown
5031 // if the active cell was repainted, repaint its highlight too because it
5032 // might have been damaged by the grid lines
5033 size_t count
= m_cellsExposed
.GetCount();
5034 for ( size_t n
= 0; n
< count
; n
++ )
5036 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5038 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5039 DrawCellHighlight(dc
, attr
);
5047 // TODO: remove this ???
5048 // This is used to redraw all grid lines e.g. when the grid line colour
5051 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5053 if ( !m_gridLinesEnabled
||
5055 !m_numCols
) return;
5057 int top
, bottom
, left
, right
;
5063 m_gridWin
->GetClientSize(&cw
, &ch
);
5065 // virtual coords of visible area
5067 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5068 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5073 reg
.GetBox(x
, y
, w
, h
);
5074 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5075 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5079 m_gridWin
->GetClientSize(&cw
, &ch
);
5080 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5081 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5084 // avoid drawing grid lines past the last row and col
5086 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5087 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5089 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5091 // horizontal grid lines
5094 for ( i
= 0; i
< m_numRows
; i
++ )
5096 int bot
= GetRowBottom(i
) - 1;
5105 dc
.DrawLine( left
, bot
, right
, bot
);
5110 // vertical grid lines
5112 for ( i
= 0; i
< m_numCols
; i
++ )
5114 int colRight
= GetColRight(i
) - 1;
5115 if ( colRight
> right
)
5120 if ( colRight
>= left
)
5122 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5128 void wxGrid::DrawRowLabels( wxDC
& dc
)
5130 if ( !m_numRows
|| !m_numCols
) return;
5133 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5135 for ( i
= 0; i
< numLabels
; i
++ )
5137 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5142 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5144 if ( GetRowHeight(row
) <= 0 )
5147 int rowTop
= GetRowTop(row
),
5148 rowBottom
= GetRowBottom(row
) - 1;
5150 dc
.SetPen( *wxBLACK_PEN
);
5151 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5152 m_rowLabelWidth
-1, rowBottom
);
5154 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5156 dc
.SetPen( *wxWHITE_PEN
);
5157 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5158 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5160 dc
.SetBackgroundMode( wxTRANSPARENT
);
5161 dc
.SetTextForeground( GetLabelTextColour() );
5162 dc
.SetFont( GetLabelFont() );
5165 GetRowLabelAlignment( &hAlign
, &vAlign
);
5169 rect
.SetY( GetRowTop(row
) + 2 );
5170 rect
.SetWidth( m_rowLabelWidth
- 4 );
5171 rect
.SetHeight( GetRowHeight(row
) - 4 );
5172 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5176 void wxGrid::DrawColLabels( wxDC
& dc
)
5178 if ( !m_numRows
|| !m_numCols
) return;
5181 size_t numLabels
= m_colLabelsExposed
.GetCount();
5183 for ( i
= 0; i
< numLabels
; i
++ )
5185 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5190 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5192 if ( GetColWidth(col
) <= 0 )
5195 int colLeft
= GetColLeft(col
),
5196 colRight
= GetColRight(col
) - 1;
5198 dc
.SetPen( *wxBLACK_PEN
);
5199 dc
.DrawLine( colRight
, 0,
5200 colRight
, m_colLabelHeight
-1 );
5202 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5203 colRight
, m_colLabelHeight
-1 );
5205 dc
.SetPen( *wxWHITE_PEN
);
5206 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5207 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5209 dc
.SetBackgroundMode( wxTRANSPARENT
);
5210 dc
.SetTextForeground( GetLabelTextColour() );
5211 dc
.SetFont( GetLabelFont() );
5213 dc
.SetBackgroundMode( wxTRANSPARENT
);
5214 dc
.SetTextForeground( GetLabelTextColour() );
5215 dc
.SetFont( GetLabelFont() );
5218 GetColLabelAlignment( &hAlign
, &vAlign
);
5221 rect
.SetX( colLeft
+ 2 );
5223 rect
.SetWidth( GetColWidth(col
) - 4 );
5224 rect
.SetHeight( m_colLabelHeight
- 4 );
5225 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5229 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5230 const wxString
& value
,
5235 long textWidth
, textHeight
;
5236 long lineWidth
, lineHeight
;
5237 wxArrayString lines
;
5239 dc
.SetClippingRegion( rect
);
5240 StringToLines( value
, lines
);
5241 if ( lines
.GetCount() )
5243 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5244 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5247 switch ( horizAlign
)
5250 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5254 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5263 switch ( vertAlign
)
5266 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5270 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5279 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5281 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5286 dc
.DestroyClippingRegion();
5290 // Split multi line text up into an array of strings. Any existing
5291 // contents of the string array are preserved.
5293 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5297 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5298 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5300 while ( startPos
< (int)tVal
.Length() )
5302 pos
= tVal
.Mid(startPos
).Find( eol
);
5307 else if ( pos
== 0 )
5309 lines
.Add( wxEmptyString
);
5313 lines
.Add( value
.Mid(startPos
, pos
) );
5317 if ( startPos
< (int)value
.Length() )
5319 lines
.Add( value
.Mid( startPos
) );
5324 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5325 wxArrayString
& lines
,
5326 long *width
, long *height
)
5333 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5335 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5336 w
= wxMax( w
, lineW
);
5346 // ------ Edit control functions
5350 void wxGrid::EnableEditing( bool edit
)
5352 // TODO: improve this ?
5354 if ( edit
!= m_editable
)
5358 // FIXME IMHO this won't disable the edit control if edit == FALSE
5359 // because of the check in the beginning of
5360 // EnableCellEditControl() just below (VZ)
5361 EnableCellEditControl(m_editable
);
5366 void wxGrid::EnableCellEditControl( bool enable
)
5371 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5372 SetCurrentCell( 0, 0 );
5374 if ( enable
!= m_cellEditCtrlEnabled
)
5376 // TODO allow the app to Veto() this event?
5377 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5381 // this should be checked by the caller!
5382 wxASSERT_MSG( CanEnableCellControl(),
5383 _T("can't enable editing for this cell!") );
5385 // do it before ShowCellEditControl()
5386 m_cellEditCtrlEnabled
= enable
;
5388 ShowCellEditControl();
5392 HideCellEditControl();
5393 SaveEditControlValue();
5395 // do it after HideCellEditControl()
5396 m_cellEditCtrlEnabled
= enable
;
5401 bool wxGrid::IsCurrentCellReadOnly() const
5404 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5405 bool readonly
= attr
->IsReadOnly();
5411 bool wxGrid::CanEnableCellControl() const
5413 return m_editable
&& !IsCurrentCellReadOnly();
5416 bool wxGrid::IsCellEditControlEnabled() const
5418 // the cell edit control might be disable for all cells or just for the
5419 // current one if it's read only
5420 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5423 void wxGrid::ShowCellEditControl()
5425 if ( IsCellEditControlEnabled() )
5427 if ( !IsVisible( m_currentCellCoords
) )
5433 wxRect rect
= CellToRect( m_currentCellCoords
);
5434 int row
= m_currentCellCoords
.GetRow();
5435 int col
= m_currentCellCoords
.GetCol();
5437 // convert to scrolled coords
5439 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5441 // done in PaintBackground()
5443 // erase the highlight and the cell contents because the editor
5444 // might not cover the entire cell
5445 wxClientDC
dc( m_gridWin
);
5447 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5448 dc
.SetPen(*wxTRANSPARENT_PEN
);
5449 dc
.DrawRectangle(rect
);
5452 // cell is shifted by one pixel
5456 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5457 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5458 if ( !editor
->IsCreated() )
5460 editor
->Create(m_gridWin
, -1,
5461 new wxGridCellEditorEvtHandler(this, editor
));
5464 editor
->Show( TRUE
, attr
);
5466 editor
->SetSize( rect
);
5468 editor
->BeginEdit(row
, col
, this);
5475 void wxGrid::HideCellEditControl()
5477 if ( IsCellEditControlEnabled() )
5479 int row
= m_currentCellCoords
.GetRow();
5480 int col
= m_currentCellCoords
.GetCol();
5482 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5483 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5485 m_gridWin
->SetFocus();
5490 void wxGrid::SaveEditControlValue()
5492 if ( IsCellEditControlEnabled() )
5494 int row
= m_currentCellCoords
.GetRow();
5495 int col
= m_currentCellCoords
.GetCol();
5497 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5498 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5499 bool changed
= editor
->EndEdit(row
, col
, this);
5505 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5506 m_currentCellCoords
.GetRow(),
5507 m_currentCellCoords
.GetCol() );
5514 // ------ Grid location functions
5515 // Note that all of these functions work with the logical coordinates of
5516 // grid cells and labels so you will need to convert from device
5517 // coordinates for mouse events etc.
5520 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5522 int row
= YToRow(y
);
5523 int col
= XToCol(x
);
5525 if ( row
== -1 || col
== -1 )
5527 coords
= wxGridNoCellCoords
;
5531 coords
.Set( row
, col
);
5536 int wxGrid::YToRow( int y
)
5540 for ( i
= 0; i
< m_numRows
; i
++ )
5542 if ( y
< GetRowBottom(i
) )
5550 int wxGrid::XToCol( int x
)
5554 for ( i
= 0; i
< m_numCols
; i
++ )
5556 if ( x
< GetColRight(i
) )
5564 // return the row number that that the y coord is near the edge of, or
5565 // -1 if not near an edge
5567 int wxGrid::YToEdgeOfRow( int y
)
5571 for ( i
= 0; i
< m_numRows
; i
++ )
5573 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5575 d
= abs( y
- GetRowBottom(i
) );
5576 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5585 // return the col number that that the x coord is near the edge of, or
5586 // -1 if not near an edge
5588 int wxGrid::XToEdgeOfCol( int x
)
5592 for ( i
= 0; i
< m_numCols
; i
++ )
5594 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5596 d
= abs( x
- GetColRight(i
) );
5597 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5606 wxRect
wxGrid::CellToRect( int row
, int col
)
5608 wxRect
rect( -1, -1, -1, -1 );
5610 if ( row
>= 0 && row
< m_numRows
&&
5611 col
>= 0 && col
< m_numCols
)
5613 rect
.x
= GetColLeft(col
);
5614 rect
.y
= GetRowTop(row
);
5615 rect
.width
= GetColWidth(col
);
5616 rect
.height
= GetRowHeight(row
);
5623 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5625 // get the cell rectangle in logical coords
5627 wxRect
r( CellToRect( row
, col
) );
5629 // convert to device coords
5631 int left
, top
, right
, bottom
;
5632 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5633 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5635 // check against the client area of the grid window
5638 m_gridWin
->GetClientSize( &cw
, &ch
);
5640 if ( wholeCellVisible
)
5642 // is the cell wholly visible ?
5644 return ( left
>= 0 && right
<= cw
&&
5645 top
>= 0 && bottom
<= ch
);
5649 // is the cell partly visible ?
5651 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5652 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5657 // make the specified cell location visible by doing a minimal amount
5660 void wxGrid::MakeCellVisible( int row
, int col
)
5663 int xpos
= -1, ypos
= -1;
5665 if ( row
>= 0 && row
< m_numRows
&&
5666 col
>= 0 && col
< m_numCols
)
5668 // get the cell rectangle in logical coords
5670 wxRect
r( CellToRect( row
, col
) );
5672 // convert to device coords
5674 int left
, top
, right
, bottom
;
5675 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5676 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5679 m_gridWin
->GetClientSize( &cw
, &ch
);
5685 else if ( bottom
> ch
)
5687 int h
= r
.GetHeight();
5689 for ( i
= row
-1; i
>= 0; i
-- )
5691 int rowHeight
= GetRowHeight(i
);
5692 if ( h
+ rowHeight
> ch
)
5699 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5700 // have rounding errors (this is important, because if we do, we
5701 // might not scroll at all and some cells won't be redrawn)
5702 ypos
+= GRID_SCROLL_LINE
/ 2;
5709 else if ( right
> cw
)
5711 int w
= r
.GetWidth();
5713 for ( i
= col
-1; i
>= 0; i
-- )
5715 int colWidth
= GetColWidth(i
);
5716 if ( w
+ colWidth
> cw
)
5723 // see comment for ypos above
5724 xpos
+= GRID_SCROLL_LINE
/ 2;
5727 if ( xpos
!= -1 || ypos
!= -1 )
5729 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5730 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5731 Scroll( xpos
, ypos
);
5739 // ------ Grid cursor movement functions
5742 bool wxGrid::MoveCursorUp()
5744 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5745 m_currentCellCoords
.GetRow() > 0 )
5747 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5748 m_currentCellCoords
.GetCol() );
5750 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5751 m_currentCellCoords
.GetCol() );
5760 bool wxGrid::MoveCursorDown()
5762 // TODO: allow for scrolling
5764 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5765 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5767 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5768 m_currentCellCoords
.GetCol() );
5770 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5771 m_currentCellCoords
.GetCol() );
5780 bool wxGrid::MoveCursorLeft()
5782 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5783 m_currentCellCoords
.GetCol() > 0 )
5785 MakeCellVisible( m_currentCellCoords
.GetRow(),
5786 m_currentCellCoords
.GetCol() - 1 );
5788 SetCurrentCell( m_currentCellCoords
.GetRow(),
5789 m_currentCellCoords
.GetCol() - 1 );
5798 bool wxGrid::MoveCursorRight()
5800 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5801 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5803 MakeCellVisible( m_currentCellCoords
.GetRow(),
5804 m_currentCellCoords
.GetCol() + 1 );
5806 SetCurrentCell( m_currentCellCoords
.GetRow(),
5807 m_currentCellCoords
.GetCol() + 1 );
5816 bool wxGrid::MovePageUp()
5818 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5820 int row
= m_currentCellCoords
.GetRow();
5824 m_gridWin
->GetClientSize( &cw
, &ch
);
5826 int y
= GetRowTop(row
);
5827 int newRow
= YToRow( y
- ch
+ 1 );
5832 else if ( newRow
== row
)
5837 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5838 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5846 bool wxGrid::MovePageDown()
5848 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5850 int row
= m_currentCellCoords
.GetRow();
5851 if ( row
< m_numRows
)
5854 m_gridWin
->GetClientSize( &cw
, &ch
);
5856 int y
= GetRowTop(row
);
5857 int newRow
= YToRow( y
+ ch
);
5860 newRow
= m_numRows
- 1;
5862 else if ( newRow
== row
)
5867 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5868 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5876 bool wxGrid::MoveCursorUpBlock()
5879 m_currentCellCoords
!= wxGridNoCellCoords
&&
5880 m_currentCellCoords
.GetRow() > 0 )
5882 int row
= m_currentCellCoords
.GetRow();
5883 int col
= m_currentCellCoords
.GetCol();
5885 if ( m_table
->IsEmptyCell(row
, col
) )
5887 // starting in an empty cell: find the next block of
5893 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5896 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5898 // starting at the top of a block: find the next block
5904 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5909 // starting within a block: find the top of the block
5914 if ( m_table
->IsEmptyCell(row
, col
) )
5922 MakeCellVisible( row
, col
);
5923 SetCurrentCell( row
, col
);
5931 bool wxGrid::MoveCursorDownBlock()
5934 m_currentCellCoords
!= wxGridNoCellCoords
&&
5935 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5937 int row
= m_currentCellCoords
.GetRow();
5938 int col
= m_currentCellCoords
.GetCol();
5940 if ( m_table
->IsEmptyCell(row
, col
) )
5942 // starting in an empty cell: find the next block of
5945 while ( row
< m_numRows
-1 )
5948 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5951 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5953 // starting at the bottom of a block: find the next block
5956 while ( row
< m_numRows
-1 )
5959 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5964 // starting within a block: find the bottom of the block
5966 while ( row
< m_numRows
-1 )
5969 if ( m_table
->IsEmptyCell(row
, col
) )
5977 MakeCellVisible( row
, col
);
5978 SetCurrentCell( row
, col
);
5986 bool wxGrid::MoveCursorLeftBlock()
5989 m_currentCellCoords
!= wxGridNoCellCoords
&&
5990 m_currentCellCoords
.GetCol() > 0 )
5992 int row
= m_currentCellCoords
.GetRow();
5993 int col
= m_currentCellCoords
.GetCol();
5995 if ( m_table
->IsEmptyCell(row
, col
) )
5997 // starting in an empty cell: find the next block of
6003 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6006 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6008 // starting at the left of a block: find the next block
6014 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6019 // starting within a block: find the left of the block
6024 if ( m_table
->IsEmptyCell(row
, col
) )
6032 MakeCellVisible( row
, col
);
6033 SetCurrentCell( row
, col
);
6041 bool wxGrid::MoveCursorRightBlock()
6044 m_currentCellCoords
!= wxGridNoCellCoords
&&
6045 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6047 int row
= m_currentCellCoords
.GetRow();
6048 int col
= m_currentCellCoords
.GetCol();
6050 if ( m_table
->IsEmptyCell(row
, col
) )
6052 // starting in an empty cell: find the next block of
6055 while ( col
< m_numCols
-1 )
6058 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6061 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6063 // starting at the right of a block: find the next block
6066 while ( col
< m_numCols
-1 )
6069 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6074 // starting within a block: find the right of the block
6076 while ( col
< m_numCols
-1 )
6079 if ( m_table
->IsEmptyCell(row
, col
) )
6087 MakeCellVisible( row
, col
);
6088 SetCurrentCell( row
, col
);
6099 // ------ Label values and formatting
6102 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6104 *horiz
= m_rowLabelHorizAlign
;
6105 *vert
= m_rowLabelVertAlign
;
6108 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6110 *horiz
= m_colLabelHorizAlign
;
6111 *vert
= m_colLabelVertAlign
;
6114 wxString
wxGrid::GetRowLabelValue( int row
)
6118 return m_table
->GetRowLabelValue( row
);
6128 wxString
wxGrid::GetColLabelValue( int col
)
6132 return m_table
->GetColLabelValue( col
);
6143 void wxGrid::SetRowLabelSize( int width
)
6145 width
= wxMax( width
, 0 );
6146 if ( width
!= m_rowLabelWidth
)
6150 m_rowLabelWin
->Show( FALSE
);
6151 m_cornerLabelWin
->Show( FALSE
);
6153 else if ( m_rowLabelWidth
== 0 )
6155 m_rowLabelWin
->Show( TRUE
);
6156 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6159 m_rowLabelWidth
= width
;
6166 void wxGrid::SetColLabelSize( int height
)
6168 height
= wxMax( height
, 0 );
6169 if ( height
!= m_colLabelHeight
)
6173 m_colLabelWin
->Show( FALSE
);
6174 m_cornerLabelWin
->Show( FALSE
);
6176 else if ( m_colLabelHeight
== 0 )
6178 m_colLabelWin
->Show( TRUE
);
6179 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6182 m_colLabelHeight
= height
;
6189 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6191 if ( m_labelBackgroundColour
!= colour
)
6193 m_labelBackgroundColour
= colour
;
6194 m_rowLabelWin
->SetBackgroundColour( colour
);
6195 m_colLabelWin
->SetBackgroundColour( colour
);
6196 m_cornerLabelWin
->SetBackgroundColour( colour
);
6198 if ( !GetBatchCount() )
6200 m_rowLabelWin
->Refresh();
6201 m_colLabelWin
->Refresh();
6202 m_cornerLabelWin
->Refresh();
6207 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6209 if ( m_labelTextColour
!= colour
)
6211 m_labelTextColour
= colour
;
6212 if ( !GetBatchCount() )
6214 m_rowLabelWin
->Refresh();
6215 m_colLabelWin
->Refresh();
6220 void wxGrid::SetLabelFont( const wxFont
& font
)
6223 if ( !GetBatchCount() )
6225 m_rowLabelWin
->Refresh();
6226 m_colLabelWin
->Refresh();
6230 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6232 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6234 m_rowLabelHorizAlign
= horiz
;
6237 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6239 m_rowLabelVertAlign
= vert
;
6242 if ( !GetBatchCount() )
6244 m_rowLabelWin
->Refresh();
6248 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6250 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6252 m_colLabelHorizAlign
= horiz
;
6255 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6257 m_colLabelVertAlign
= vert
;
6260 if ( !GetBatchCount() )
6262 m_colLabelWin
->Refresh();
6266 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6270 m_table
->SetRowLabelValue( row
, s
);
6271 if ( !GetBatchCount() )
6273 wxRect rect
= CellToRect( row
, 0);
6274 if ( rect
.height
> 0 )
6276 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6278 rect
.width
= m_rowLabelWidth
;
6279 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6285 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6289 m_table
->SetColLabelValue( col
, s
);
6290 if ( !GetBatchCount() )
6292 wxRect rect
= CellToRect( 0, col
);
6293 if ( rect
.width
> 0 )
6295 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6297 rect
.height
= m_colLabelHeight
;
6298 m_colLabelWin
->Refresh( TRUE
, &rect
);
6304 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6306 if ( m_gridLineColour
!= colour
)
6308 m_gridLineColour
= colour
;
6310 wxClientDC
dc( m_gridWin
);
6312 DrawAllGridLines( dc
, wxRegion() );
6316 void wxGrid::EnableGridLines( bool enable
)
6318 if ( enable
!= m_gridLinesEnabled
)
6320 m_gridLinesEnabled
= enable
;
6322 if ( !GetBatchCount() )
6326 wxClientDC
dc( m_gridWin
);
6328 DrawAllGridLines( dc
, wxRegion() );
6332 m_gridWin
->Refresh();
6339 int wxGrid::GetDefaultRowSize()
6341 return m_defaultRowHeight
;
6344 int wxGrid::GetRowSize( int row
)
6346 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6348 return GetRowHeight(row
);
6351 int wxGrid::GetDefaultColSize()
6353 return m_defaultColWidth
;
6356 int wxGrid::GetColSize( int col
)
6358 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6360 return GetColWidth(col
);
6363 // ============================================================================
6364 // access to the grid attributes: each of them has a default value in the grid
6365 // itself and may be overidden on a per-cell basis
6366 // ============================================================================
6368 // ----------------------------------------------------------------------------
6369 // setting default attributes
6370 // ----------------------------------------------------------------------------
6372 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6374 m_defaultCellAttr
->SetBackgroundColour(col
);
6376 m_gridWin
->SetBackgroundColour(col
);
6380 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6382 m_defaultCellAttr
->SetTextColour(col
);
6385 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6387 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6390 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6392 m_defaultCellAttr
->SetFont(font
);
6395 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6397 m_defaultCellAttr
->SetRenderer(renderer
);
6400 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6402 m_defaultCellAttr
->SetEditor(editor
);
6405 // ----------------------------------------------------------------------------
6406 // access to the default attrbiutes
6407 // ----------------------------------------------------------------------------
6409 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6411 return m_defaultCellAttr
->GetBackgroundColour();
6414 wxColour
wxGrid::GetDefaultCellTextColour()
6416 return m_defaultCellAttr
->GetTextColour();
6419 wxFont
wxGrid::GetDefaultCellFont()
6421 return m_defaultCellAttr
->GetFont();
6424 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6426 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6429 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6431 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6434 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6436 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6439 // ----------------------------------------------------------------------------
6440 // access to cell attributes
6441 // ----------------------------------------------------------------------------
6443 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6445 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6446 wxColour colour
= attr
->GetBackgroundColour();
6451 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6453 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6454 wxColour colour
= attr
->GetTextColour();
6459 wxFont
wxGrid::GetCellFont( int row
, int col
)
6461 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6462 wxFont font
= attr
->GetFont();
6467 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6469 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6470 attr
->GetAlignment(horiz
, vert
);
6474 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6476 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6477 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6482 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6484 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6485 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6490 bool wxGrid::IsReadOnly(int row
, int col
) const
6492 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6493 bool isReadOnly
= attr
->IsReadOnly();
6498 // ----------------------------------------------------------------------------
6499 // attribute support: cache, automatic provider creation, ...
6500 // ----------------------------------------------------------------------------
6502 bool wxGrid::CanHaveAttributes()
6509 return m_table
->CanHaveAttributes();
6512 void wxGrid::ClearAttrCache()
6514 if ( m_attrCache
.row
!= -1 )
6516 m_attrCache
.attr
->SafeDecRef();
6517 m_attrCache
.row
= -1;
6521 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6523 wxGrid
*self
= (wxGrid
*)this; // const_cast
6525 self
->ClearAttrCache();
6526 self
->m_attrCache
.row
= row
;
6527 self
->m_attrCache
.col
= col
;
6528 self
->m_attrCache
.attr
= attr
;
6532 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6534 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6536 *attr
= m_attrCache
.attr
;
6537 (*attr
)->SafeIncRef();
6539 #ifdef DEBUG_ATTR_CACHE
6540 gs_nAttrCacheHits
++;
6547 #ifdef DEBUG_ATTR_CACHE
6548 gs_nAttrCacheMisses
++;
6554 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6556 wxGridCellAttr
*attr
;
6557 if ( !LookupAttr(row
, col
, &attr
) )
6559 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6560 CacheAttr(row
, col
, attr
);
6564 attr
->SetDefAttr(m_defaultCellAttr
);
6568 attr
= m_defaultCellAttr
;
6575 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6577 wxGridCellAttr
*attr
;
6578 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6580 wxASSERT_MSG( m_table
,
6581 _T("we may only be called if CanHaveAttributes() "
6582 "returned TRUE and then m_table should be !NULL") );
6584 attr
= m_table
->GetAttr(row
, col
);
6587 attr
= new wxGridCellAttr
;
6589 // artificially inc the ref count to match DecRef() in caller
6592 m_table
->SetAttr(attr
, row
, col
);
6595 CacheAttr(row
, col
, attr
);
6597 attr
->SetDefAttr(m_defaultCellAttr
);
6601 // ----------------------------------------------------------------------------
6602 // setting cell attributes: this is forwarded to the table
6603 // ----------------------------------------------------------------------------
6605 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6607 if ( CanHaveAttributes() )
6609 m_table
->SetRowAttr(attr
, row
);
6617 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6619 if ( CanHaveAttributes() )
6621 m_table
->SetColAttr(attr
, col
);
6629 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6631 if ( CanHaveAttributes() )
6633 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6634 attr
->SetBackgroundColour(colour
);
6639 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6641 if ( CanHaveAttributes() )
6643 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6644 attr
->SetTextColour(colour
);
6649 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6651 if ( CanHaveAttributes() )
6653 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6654 attr
->SetFont(font
);
6659 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6661 if ( CanHaveAttributes() )
6663 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6664 attr
->SetAlignment(horiz
, vert
);
6669 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6671 if ( CanHaveAttributes() )
6673 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6674 attr
->SetRenderer(renderer
);
6679 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6681 if ( CanHaveAttributes() )
6683 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6684 attr
->SetEditor(editor
);
6689 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6691 if ( CanHaveAttributes() )
6693 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6694 attr
->SetReadOnly(isReadOnly
);
6699 // ----------------------------------------------------------------------------
6700 // Data type registration
6701 // ----------------------------------------------------------------------------
6703 void wxGrid::RegisterDataType(const wxString
& typeName
,
6704 wxGridCellRenderer
* renderer
,
6705 wxGridCellEditor
* editor
)
6707 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6711 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6713 wxString typeName
= m_table
->GetTypeName(row
, col
);
6714 return GetDefaultEditorForType(typeName
);
6717 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6719 wxString typeName
= m_table
->GetTypeName(row
, col
);
6720 return GetDefaultRendererForType(typeName
);
6724 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6726 int index
= m_typeRegistry
->FindDataType(typeName
);
6728 // Should we force the failure here or let it fallback to string handling???
6729 // wxFAIL_MSG(wxT("Unknown data type name"));
6732 return m_typeRegistry
->GetEditor(index
);
6736 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6738 int index
= m_typeRegistry
->FindDataType(typeName
);
6740 // Should we force the failure here or let it fallback to string handling???
6741 // wxFAIL_MSG(wxT("Unknown data type name"));
6744 return m_typeRegistry
->GetRenderer(index
);
6748 // ----------------------------------------------------------------------------
6750 // ----------------------------------------------------------------------------
6752 void wxGrid::EnableDragRowSize( bool enable
)
6754 m_canDragRowSize
= enable
;
6758 void wxGrid::EnableDragColSize( bool enable
)
6760 m_canDragColSize
= enable
;
6763 void wxGrid::EnableDragGridSize( bool enable
)
6765 m_canDragGridSize
= enable
;
6769 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6771 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6773 if ( resizeExistingRows
)
6781 void wxGrid::SetRowSize( int row
, int height
)
6783 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6785 if ( m_rowHeights
.IsEmpty() )
6787 // need to really create the array
6791 int h
= wxMax( 0, height
);
6792 int diff
= h
- m_rowHeights
[row
];
6794 m_rowHeights
[row
] = h
;
6796 for ( i
= row
; i
< m_numRows
; i
++ )
6798 m_rowBottoms
[i
] += diff
;
6803 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6805 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6807 if ( resizeExistingCols
)
6815 void wxGrid::SetColSize( int col
, int width
)
6817 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6819 // should we check that it's bigger than GetColMinimalWidth(col) here?
6821 if ( m_colWidths
.IsEmpty() )
6823 // need to really create the array
6827 int w
= wxMax( 0, width
);
6828 int diff
= w
- m_colWidths
[col
];
6829 m_colWidths
[col
] = w
;
6832 for ( i
= col
; i
< m_numCols
; i
++ )
6834 m_colRights
[i
] += diff
;
6840 void wxGrid::SetColMinimalWidth( int col
, int width
)
6842 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6845 int wxGrid::GetColMinimalWidth(int col
) const
6847 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6848 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6851 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6853 wxClientDC
dc(m_gridWin
);
6855 wxCoord width
, widthMax
= 0;
6856 for ( int row
= 0; row
< m_numRows
; row
++ )
6858 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6859 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6862 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6863 if ( width
> widthMax
)
6872 // now also compare with the column label width
6873 dc
.SetFont( GetLabelFont() );
6874 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6875 if ( width
> widthMax
)
6882 // empty column - give default width (notice that if widthMax is less
6883 // than default width but != 0, it's ok)
6884 widthMax
= m_defaultColWidth
;
6888 // leave some space around text
6892 SetColSize(col
, widthMax
);
6895 SetColMinimalWidth(col
, widthMax
);
6899 void wxGrid::AutoSizeColumns( bool setAsMin
)
6901 for ( int col
= 0; col
< m_numCols
; col
++ )
6903 AutoSizeColumn(col
, setAsMin
);
6908 // ------ cell value accessor functions
6911 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6915 m_table
->SetValue( row
, col
, s
.c_str() );
6916 if ( !GetBatchCount() )
6918 wxClientDC
dc( m_gridWin
);
6920 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6923 if ( m_currentCellCoords
.GetRow() == row
&&
6924 m_currentCellCoords
.GetCol() == col
&&
6925 IsCellEditControlEnabled())
6927 HideCellEditControl();
6928 ShowCellEditControl(); // will reread data from table
6935 // ------ Block, row and col selection
6938 void wxGrid::SelectRow( int row
, bool addToSelected
)
6942 if ( IsSelection() && addToSelected
)
6945 bool need_refresh
[4];
6949 need_refresh
[3] = FALSE
;
6953 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6954 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6955 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6956 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6960 need_refresh
[0] = TRUE
;
6961 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6962 wxGridCellCoords ( oldTop
- 1,
6964 m_selectedTopLeft
.SetRow( row
);
6969 need_refresh
[1] = TRUE
;
6970 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6971 wxGridCellCoords ( oldBottom
,
6974 m_selectedTopLeft
.SetCol( 0 );
6977 if ( oldBottom
< row
)
6979 need_refresh
[2] = TRUE
;
6980 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6981 wxGridCellCoords ( row
,
6983 m_selectedBottomRight
.SetRow( row
);
6986 if ( oldRight
< m_numCols
- 1 )
6988 need_refresh
[3] = TRUE
;
6989 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6991 wxGridCellCoords ( oldBottom
,
6993 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6996 for (i
= 0; i
< 4; i
++ )
6997 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6998 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7002 r
= SelectionToDeviceRect();
7004 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7006 m_selectedTopLeft
.Set( row
, 0 );
7007 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7008 r
= SelectionToDeviceRect();
7009 m_gridWin
->Refresh( FALSE
, &r
);
7012 wxGridRangeSelectEvent
gridEvt( GetId(),
7013 wxEVT_GRID_RANGE_SELECT
,
7016 m_selectedBottomRight
);
7018 GetEventHandler()->ProcessEvent(gridEvt
);
7022 void wxGrid::SelectCol( int col
, bool addToSelected
)
7024 if ( IsSelection() && addToSelected
)
7027 bool need_refresh
[4];
7031 need_refresh
[3] = FALSE
;
7034 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7035 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7036 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7037 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7039 if ( oldLeft
> col
)
7041 need_refresh
[0] = TRUE
;
7042 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7043 wxGridCellCoords ( m_numRows
- 1,
7045 m_selectedTopLeft
.SetCol( col
);
7050 need_refresh
[1] = TRUE
;
7051 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7052 wxGridCellCoords ( oldTop
- 1,
7054 m_selectedTopLeft
.SetRow( 0 );
7057 if ( oldRight
< col
)
7059 need_refresh
[2] = TRUE
;
7060 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7061 wxGridCellCoords ( m_numRows
- 1,
7063 m_selectedBottomRight
.SetCol( col
);
7066 if ( oldBottom
< m_numRows
- 1 )
7068 need_refresh
[3] = TRUE
;
7069 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7071 wxGridCellCoords ( m_numRows
- 1,
7073 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7076 for (i
= 0; i
< 4; i
++ )
7077 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7078 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7084 r
= SelectionToDeviceRect();
7086 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7088 m_selectedTopLeft
.Set( 0, col
);
7089 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7090 r
= SelectionToDeviceRect();
7091 m_gridWin
->Refresh( FALSE
, &r
);
7094 wxGridRangeSelectEvent
gridEvt( GetId(),
7095 wxEVT_GRID_RANGE_SELECT
,
7098 m_selectedBottomRight
);
7100 GetEventHandler()->ProcessEvent(gridEvt
);
7104 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7107 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7109 if ( topRow
> bottomRow
)
7116 if ( leftCol
> rightCol
)
7123 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7124 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7126 if ( m_selectedTopLeft
!= updateTopLeft
||
7127 m_selectedBottomRight
!= updateBottomRight
)
7129 // Compute two optimal update rectangles:
7130 // Either one rectangle is a real subset of the
7131 // other, or they are (almost) disjoint!
7133 bool need_refresh
[4];
7137 need_refresh
[3] = FALSE
;
7140 // Store intermediate values
7141 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7142 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7143 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7144 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7146 // Determine the outer/inner coordinates.
7147 if (oldLeft
> leftCol
)
7153 if (oldTop
> topRow
)
7159 if (oldRight
< rightCol
)
7162 oldRight
= rightCol
;
7165 if (oldBottom
< bottomRow
)
7168 oldBottom
= bottomRow
;
7172 // Now, either the stuff marked old is the outer
7173 // rectangle or we don't have a situation where one
7174 // is contained in the other.
7176 if ( oldLeft
< leftCol
)
7178 need_refresh
[0] = TRUE
;
7179 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7181 wxGridCellCoords ( oldBottom
,
7185 if ( oldTop
< topRow
)
7187 need_refresh
[1] = TRUE
;
7188 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7190 wxGridCellCoords ( topRow
- 1,
7194 if ( oldRight
> rightCol
)
7196 need_refresh
[2] = TRUE
;
7197 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7199 wxGridCellCoords ( oldBottom
,
7203 if ( oldBottom
> bottomRow
)
7205 need_refresh
[3] = TRUE
;
7206 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7208 wxGridCellCoords ( oldBottom
,
7214 m_selectedTopLeft
= updateTopLeft
;
7215 m_selectedBottomRight
= updateBottomRight
;
7217 // various Refresh() calls
7218 for (i
= 0; i
< 4; i
++ )
7219 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7220 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7223 // only generate an event if the block is not being selected by
7224 // dragging the mouse (in which case the event will be generated in
7225 // the mouse event handler)
7226 if ( !m_isDragging
)
7228 wxGridRangeSelectEvent
gridEvt( GetId(),
7229 wxEVT_GRID_RANGE_SELECT
,
7232 m_selectedBottomRight
);
7234 GetEventHandler()->ProcessEvent(gridEvt
);
7238 void wxGrid::SelectAll()
7240 m_selectedTopLeft
.Set( 0, 0 );
7241 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7243 m_gridWin
->Refresh();
7247 void wxGrid::ClearSelection()
7249 m_selectedTopLeft
= wxGridNoCellCoords
;
7250 m_selectedBottomRight
= wxGridNoCellCoords
;
7254 // This function returns the rectangle that encloses the given block
7255 // in device coords clipped to the client size of the grid window.
7257 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7258 const wxGridCellCoords
&bottomRight
)
7260 wxRect
rect( wxGridNoCellRect
);
7263 cellRect
= CellToRect( topLeft
);
7264 if ( cellRect
!= wxGridNoCellRect
)
7270 rect
= wxRect( 0, 0, 0, 0 );
7273 cellRect
= CellToRect( bottomRight
);
7274 if ( cellRect
!= wxGridNoCellRect
)
7280 return wxGridNoCellRect
;
7283 // convert to scrolled coords
7285 int left
, top
, right
, bottom
;
7286 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7287 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7290 m_gridWin
->GetClientSize( &cw
, &ch
);
7292 rect
.SetLeft( wxMax(0, left
) );
7293 rect
.SetTop( wxMax(0, top
) );
7294 rect
.SetRight( wxMin(cw
, right
) );
7295 rect
.SetBottom( wxMin(ch
, bottom
) );
7303 // ------ Grid event classes
7306 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7308 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7309 int row
, int col
, int x
, int y
,
7310 bool control
, bool shift
, bool alt
, bool meta
)
7311 : wxNotifyEvent( type
, id
)
7317 m_control
= control
;
7322 SetEventObject(obj
);
7326 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7328 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7329 int rowOrCol
, int x
, int y
,
7330 bool control
, bool shift
, bool alt
, bool meta
)
7331 : wxNotifyEvent( type
, id
)
7333 m_rowOrCol
= rowOrCol
;
7336 m_control
= control
;
7341 SetEventObject(obj
);
7345 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7347 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7348 const wxGridCellCoords
& topLeft
,
7349 const wxGridCellCoords
& bottomRight
,
7350 bool control
, bool shift
, bool alt
, bool meta
)
7351 : wxNotifyEvent( type
, id
)
7353 m_topLeft
= topLeft
;
7354 m_bottomRight
= bottomRight
;
7355 m_control
= control
;
7360 SetEventObject(obj
);
7364 #endif // ifndef wxUSE_NEW_GRID