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
;
2950 // ----------------------------------------------------------------------------
2951 // the idea is to call these functions only when necessary because they create
2952 // quite big arrays which eat memory mostly unnecessary - in particular, if
2953 // default widths/heights are used for all rows/columns, we may not use these
2956 // with some extra code, it should be possible to only store the
2957 // widths/heights different from default ones but this will be done later...
2958 // ----------------------------------------------------------------------------
2960 void wxGrid::InitRowHeights()
2962 m_rowHeights
.Empty();
2963 m_rowBottoms
.Empty();
2965 m_rowHeights
.Alloc( m_numRows
);
2966 m_rowBottoms
.Alloc( m_numRows
);
2969 for ( int i
= 0; i
< m_numRows
; i
++ )
2971 m_rowHeights
.Add( m_defaultRowHeight
);
2972 rowBottom
+= m_defaultRowHeight
;
2973 m_rowBottoms
.Add( rowBottom
);
2977 void wxGrid::InitColWidths()
2979 m_colWidths
.Empty();
2980 m_colRights
.Empty();
2982 m_colWidths
.Alloc( m_numCols
);
2983 m_colRights
.Alloc( m_numCols
);
2985 for ( int i
= 0; i
< m_numCols
; i
++ )
2987 m_colWidths
.Add( m_defaultColWidth
);
2988 colRight
+= m_defaultColWidth
;
2989 m_colRights
.Add( colRight
);
2993 int wxGrid::GetColWidth(int col
) const
2995 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2998 int wxGrid::GetColLeft(int col
) const
3000 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3001 : m_colRights
[col
] - m_colWidths
[col
];
3004 int wxGrid::GetColRight(int col
) const
3006 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3010 int wxGrid::GetRowHeight(int row
) const
3012 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3015 int wxGrid::GetRowTop(int row
) const
3017 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3018 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3021 int wxGrid::GetRowBottom(int row
) const
3023 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3024 : m_rowBottoms
[row
];
3027 void wxGrid::CalcDimensions()
3030 GetClientSize( &cw
, &ch
);
3032 if ( m_numRows
> 0 && m_numCols
> 0 )
3034 int right
= GetColRight( m_numCols
-1 ) + 50;
3035 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
3037 // TODO: restore the scroll position that we had before sizing
3040 GetViewStart( &x
, &y
);
3041 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3042 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3048 void wxGrid::CalcWindowSizes()
3051 GetClientSize( &cw
, &ch
);
3053 if ( m_cornerLabelWin
->IsShown() )
3054 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3056 if ( m_colLabelWin
->IsShown() )
3057 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3059 if ( m_rowLabelWin
->IsShown() )
3060 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3062 if ( m_gridWin
->IsShown() )
3063 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3067 // this is called when the grid table sends a message to say that it
3068 // has been redimensioned
3070 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3074 // if we were using the default widths/heights so far, we must change them
3076 if ( m_colWidths
.IsEmpty() )
3081 if ( m_rowHeights
.IsEmpty() )
3086 switch ( msg
.GetId() )
3088 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3090 size_t pos
= msg
.GetCommandInt();
3091 int numRows
= msg
.GetCommandInt2();
3092 for ( i
= 0; i
< numRows
; i
++ )
3094 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3095 m_rowBottoms
.Insert( 0, pos
);
3097 m_numRows
+= numRows
;
3100 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3102 for ( i
= pos
; i
< m_numRows
; i
++ )
3104 bottom
+= m_rowHeights
[i
];
3105 m_rowBottoms
[i
] = bottom
;
3111 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3113 int numRows
= msg
.GetCommandInt();
3114 for ( i
= 0; i
< numRows
; i
++ )
3116 m_rowHeights
.Add( m_defaultRowHeight
);
3117 m_rowBottoms
.Add( 0 );
3120 int oldNumRows
= m_numRows
;
3121 m_numRows
+= numRows
;
3124 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3126 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3128 bottom
+= m_rowHeights
[i
];
3129 m_rowBottoms
[i
] = bottom
;
3135 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3137 size_t pos
= msg
.GetCommandInt();
3138 int numRows
= msg
.GetCommandInt2();
3139 for ( i
= 0; i
< numRows
; i
++ )
3141 m_rowHeights
.Remove( pos
);
3142 m_rowBottoms
.Remove( pos
);
3144 m_numRows
-= numRows
;
3149 m_colWidths
.Clear();
3150 m_colRights
.Clear();
3151 m_currentCellCoords
= wxGridNoCellCoords
;
3155 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3156 m_currentCellCoords
.Set( 0, 0 );
3159 for ( i
= 0; i
< m_numRows
; i
++ )
3161 h
+= m_rowHeights
[i
];
3162 m_rowBottoms
[i
] = h
;
3170 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3172 size_t pos
= msg
.GetCommandInt();
3173 int numCols
= msg
.GetCommandInt2();
3174 for ( i
= 0; i
< numCols
; i
++ )
3176 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3177 m_colRights
.Insert( 0, pos
);
3179 m_numCols
+= numCols
;
3182 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3184 for ( i
= pos
; i
< m_numCols
; i
++ )
3186 right
+= m_colWidths
[i
];
3187 m_colRights
[i
] = right
;
3193 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3195 int numCols
= msg
.GetCommandInt();
3196 for ( i
= 0; i
< numCols
; i
++ )
3198 m_colWidths
.Add( m_defaultColWidth
);
3199 m_colRights
.Add( 0 );
3202 int oldNumCols
= m_numCols
;
3203 m_numCols
+= numCols
;
3206 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3208 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3210 right
+= m_colWidths
[i
];
3211 m_colRights
[i
] = right
;
3217 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3219 size_t pos
= msg
.GetCommandInt();
3220 int numCols
= msg
.GetCommandInt2();
3221 for ( i
= 0; i
< numCols
; i
++ )
3223 m_colWidths
.Remove( pos
);
3224 m_colRights
.Remove( pos
);
3226 m_numCols
-= numCols
;
3230 #if 0 // leave the row alone here so that AppendCols will work subsequently
3232 m_rowHeights
.Clear();
3233 m_rowBottoms
.Clear();
3235 m_currentCellCoords
= wxGridNoCellCoords
;
3239 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3240 m_currentCellCoords
.Set( 0, 0 );
3243 for ( i
= 0; i
< m_numCols
; i
++ )
3245 w
+= m_colWidths
[i
];
3258 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3260 wxRegionIterator
iter( reg
);
3263 m_rowLabelsExposed
.Empty();
3270 // TODO: remove this when we can...
3271 // There is a bug in wxMotif that gives garbage update
3272 // rectangles if you jump-scroll a long way by clicking the
3273 // scrollbar with middle button. This is a work-around
3275 #if defined(__WXMOTIF__)
3277 m_gridWin
->GetClientSize( &cw
, &ch
);
3278 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3279 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3282 // logical bounds of update region
3285 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3286 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3288 // find the row labels within these bounds
3291 for ( row
= 0; row
< m_numRows
; row
++ )
3293 if ( GetRowBottom(row
) < top
)
3296 if ( GetRowTop(row
) > bottom
)
3299 m_rowLabelsExposed
.Add( row
);
3307 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3309 wxRegionIterator
iter( reg
);
3312 m_colLabelsExposed
.Empty();
3319 // TODO: remove this when we can...
3320 // There is a bug in wxMotif that gives garbage update
3321 // rectangles if you jump-scroll a long way by clicking the
3322 // scrollbar with middle button. This is a work-around
3324 #if defined(__WXMOTIF__)
3326 m_gridWin
->GetClientSize( &cw
, &ch
);
3327 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3328 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3331 // logical bounds of update region
3334 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3335 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3337 // find the cells within these bounds
3340 for ( col
= 0; col
< m_numCols
; col
++ )
3342 if ( GetColRight(col
) < left
)
3345 if ( GetColLeft(col
) > right
)
3348 m_colLabelsExposed
.Add( col
);
3356 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3358 wxRegionIterator
iter( reg
);
3361 m_cellsExposed
.Empty();
3362 m_rowsExposed
.Empty();
3363 m_colsExposed
.Empty();
3365 int left
, top
, right
, bottom
;
3370 // TODO: remove this when we can...
3371 // There is a bug in wxMotif that gives garbage update
3372 // rectangles if you jump-scroll a long way by clicking the
3373 // scrollbar with middle button. This is a work-around
3375 #if defined(__WXMOTIF__)
3377 m_gridWin
->GetClientSize( &cw
, &ch
);
3378 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3379 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3380 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3381 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3384 // logical bounds of update region
3386 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3387 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3389 // find the cells within these bounds
3392 for ( row
= 0; row
< m_numRows
; row
++ )
3394 if ( GetRowBottom(row
) <= top
)
3397 if ( GetRowTop(row
) > bottom
)
3400 m_rowsExposed
.Add( row
);
3402 for ( col
= 0; col
< m_numCols
; col
++ )
3404 if ( GetColRight(col
) <= left
)
3407 if ( GetColLeft(col
) > right
)
3410 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3411 m_colsExposed
.Add( col
);
3412 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3421 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3424 wxPoint
pos( event
.GetPosition() );
3425 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3427 if ( event
.Dragging() )
3429 m_isDragging
= TRUE
;
3431 if ( event
.LeftIsDown() )
3433 switch( m_cursorMode
)
3435 case WXGRID_CURSOR_RESIZE_ROW
:
3437 int cw
, ch
, left
, dummy
;
3438 m_gridWin
->GetClientSize( &cw
, &ch
);
3439 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3441 wxClientDC
dc( m_gridWin
);
3443 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3444 dc
.SetLogicalFunction(wxINVERT
);
3445 if ( m_dragLastPos
>= 0 )
3447 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3449 dc
.DrawLine( left
, y
, left
+cw
, y
);
3454 case WXGRID_CURSOR_SELECT_ROW
:
3455 if ( (row
= YToRow( y
)) >= 0 &&
3456 !IsInSelection( row
, 0 ) )
3458 SelectRow( row
, TRUE
);
3461 // default label to suppress warnings about "enumeration value
3462 // 'xxx' not handled in switch
3470 m_isDragging
= FALSE
;
3473 // ------------ Entering or leaving the window
3475 if ( event
.Entering() || event
.Leaving() )
3477 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3481 // ------------ Left button pressed
3483 else if ( event
.LeftDown() )
3485 // don't send a label click event for a hit on the
3486 // edge of the row label - this is probably the user
3487 // wanting to resize the row
3489 if ( YToEdgeOfRow(y
) < 0 )
3493 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3495 SelectRow( row
, event
.ShiftDown() );
3496 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3501 // starting to drag-resize a row
3503 if ( CanDragRowSize() )
3504 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3509 // ------------ Left double click
3511 else if (event
.LeftDClick() )
3513 if ( YToEdgeOfRow(y
) < 0 )
3516 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3521 // ------------ Left button released
3523 else if ( event
.LeftUp() )
3525 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3527 DoEndDragResizeRow();
3529 // Note: we are ending the event *after* doing
3530 // default processing in this case
3532 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3535 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3540 // ------------ Right button down
3542 else if ( event
.RightDown() )
3545 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3547 // no default action at the moment
3552 // ------------ Right double click
3554 else if ( event
.RightDClick() )
3557 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3559 // no default action at the moment
3564 // ------------ No buttons down and mouse moving
3566 else if ( event
.Moving() )
3568 m_dragRowOrCol
= YToEdgeOfRow( y
);
3569 if ( m_dragRowOrCol
>= 0 )
3571 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3573 // don't capture the mouse yet
3574 if ( CanDragRowSize() )
3575 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3578 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3580 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3586 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3589 wxPoint
pos( event
.GetPosition() );
3590 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3592 if ( event
.Dragging() )
3594 m_isDragging
= TRUE
;
3596 if ( event
.LeftIsDown() )
3598 switch( m_cursorMode
)
3600 case WXGRID_CURSOR_RESIZE_COL
:
3602 int cw
, ch
, dummy
, top
;
3603 m_gridWin
->GetClientSize( &cw
, &ch
);
3604 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3606 wxClientDC
dc( m_gridWin
);
3609 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3610 GetColMinimalWidth(m_dragRowOrCol
));
3611 dc
.SetLogicalFunction(wxINVERT
);
3612 if ( m_dragLastPos
>= 0 )
3614 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3616 dc
.DrawLine( x
, top
, x
, top
+ch
);
3621 case WXGRID_CURSOR_SELECT_COL
:
3622 if ( (col
= XToCol( x
)) >= 0 &&
3623 !IsInSelection( 0, col
) )
3625 SelectCol( col
, TRUE
);
3628 // default label to suppress warnings about "enumeration value
3629 // 'xxx' not handled in switch
3637 m_isDragging
= FALSE
;
3640 // ------------ Entering or leaving the window
3642 if ( event
.Entering() || event
.Leaving() )
3644 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3648 // ------------ Left button pressed
3650 else if ( event
.LeftDown() )
3652 // don't send a label click event for a hit on the
3653 // edge of the col label - this is probably the user
3654 // wanting to resize the col
3656 if ( XToEdgeOfCol(x
) < 0 )
3660 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3662 SelectCol( col
, event
.ShiftDown() );
3663 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3668 // starting to drag-resize a col
3670 if ( CanDragColSize() )
3671 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3676 // ------------ Left double click
3678 if ( event
.LeftDClick() )
3680 if ( XToEdgeOfCol(x
) < 0 )
3683 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3688 // ------------ Left button released
3690 else if ( event
.LeftUp() )
3692 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3694 DoEndDragResizeCol();
3696 // Note: we are ending the event *after* doing
3697 // default processing in this case
3699 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3702 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3707 // ------------ Right button down
3709 else if ( event
.RightDown() )
3712 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3714 // no default action at the moment
3719 // ------------ Right double click
3721 else if ( event
.RightDClick() )
3724 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3726 // no default action at the moment
3731 // ------------ No buttons down and mouse moving
3733 else if ( event
.Moving() )
3735 m_dragRowOrCol
= XToEdgeOfCol( x
);
3736 if ( m_dragRowOrCol
>= 0 )
3738 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3740 // don't capture the cursor yet
3741 if ( CanDragColSize() )
3742 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3745 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3747 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3753 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3755 if ( event
.LeftDown() )
3757 // indicate corner label by having both row and
3760 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3766 else if ( event
.LeftDClick() )
3768 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3771 else if ( event
.RightDown() )
3773 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3775 // no default action at the moment
3779 else if ( event
.RightDClick() )
3781 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3783 // no default action at the moment
3788 void wxGrid::ChangeCursorMode(CursorMode mode
,
3793 static const wxChar
*cursorModes
[] =
3802 wxLogTrace(_T("grid"),
3803 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3804 win
== m_colLabelWin
? _T("colLabelWin")
3805 : win
? _T("rowLabelWin")
3807 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3808 #endif // __WXDEBUG__
3810 if ( mode
== m_cursorMode
)
3815 // by default use the grid itself
3821 m_winCapture
->ReleaseMouse();
3822 m_winCapture
= (wxWindow
*)NULL
;
3825 m_cursorMode
= mode
;
3827 switch ( m_cursorMode
)
3829 case WXGRID_CURSOR_RESIZE_ROW
:
3830 win
->SetCursor( m_rowResizeCursor
);
3833 case WXGRID_CURSOR_RESIZE_COL
:
3834 win
->SetCursor( m_colResizeCursor
);
3838 win
->SetCursor( *wxSTANDARD_CURSOR
);
3841 // we need to capture mouse when resizing
3842 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3843 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3845 if ( captureMouse
&& resize
)
3847 win
->CaptureMouse();
3852 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3855 wxPoint
pos( event
.GetPosition() );
3856 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3858 wxGridCellCoords coords
;
3859 XYToCell( x
, y
, coords
);
3861 if ( event
.Dragging() )
3863 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3865 // Don't start doing anything until the mouse has been drug at
3866 // least 3 pixels in any direction...
3869 if (m_startDragPos
== wxDefaultPosition
)
3871 m_startDragPos
= pos
;
3874 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3878 m_isDragging
= TRUE
;
3879 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3881 // Hide the edit control, so it
3882 // won't interfer with drag-shrinking.
3883 if ( IsCellEditControlEnabled() )
3885 HideCellEditControl();
3886 SaveEditControlValue();
3889 // Have we captured the mouse yet?
3892 m_winCapture
= m_gridWin
;
3893 m_winCapture
->CaptureMouse();
3896 if ( coords
!= wxGridNoCellCoords
)
3898 if ( !IsSelection() )
3900 SelectBlock( coords
, coords
);
3904 SelectBlock( m_currentCellCoords
, coords
);
3907 if (! IsVisible(coords
))
3909 MakeCellVisible(coords
);
3910 // TODO: need to introduce a delay or something here. The
3911 // scrolling is way to fast, at least on MSW.
3915 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3917 int cw
, ch
, left
, dummy
;
3918 m_gridWin
->GetClientSize( &cw
, &ch
);
3919 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3921 wxClientDC
dc( m_gridWin
);
3923 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3924 dc
.SetLogicalFunction(wxINVERT
);
3925 if ( m_dragLastPos
>= 0 )
3927 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3929 dc
.DrawLine( left
, y
, left
+cw
, y
);
3932 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3934 int cw
, ch
, dummy
, top
;
3935 m_gridWin
->GetClientSize( &cw
, &ch
);
3936 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3938 wxClientDC
dc( m_gridWin
);
3940 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3941 GetColMinimalWidth(m_dragRowOrCol
) );
3942 dc
.SetLogicalFunction(wxINVERT
);
3943 if ( m_dragLastPos
>= 0 )
3945 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3947 dc
.DrawLine( x
, top
, x
, top
+ch
);
3954 m_isDragging
= FALSE
;
3955 m_startDragPos
= wxDefaultPosition
;
3957 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3958 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3961 if ( event
.Entering() || event
.Leaving() )
3963 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3964 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3969 // ------------ Left button pressed
3971 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
3973 if ( event
.ShiftDown() )
3975 SelectBlock( m_currentCellCoords
, coords
);
3977 else if ( XToEdgeOfCol(x
) < 0 &&
3978 YToEdgeOfRow(y
) < 0 )
3980 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3985 DisableCellEditControl();
3986 MakeCellVisible( coords
);
3988 // if this is the second click on this cell then start
3990 if ( m_waitForSlowClick
&&
3991 (coords
== m_currentCellCoords
) &&
3992 CanEnableCellControl())
3994 EnableCellEditControl();
3996 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3997 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
4000 m_waitForSlowClick
= FALSE
;
4004 SetCurrentCell( coords
);
4005 m_waitForSlowClick
= TRUE
;
4012 // ------------ Left double click
4014 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4016 DisableCellEditControl();
4018 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4020 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4028 // ------------ Left button released
4030 else if ( event
.LeftUp() )
4032 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4034 if ( IsSelection() )
4038 m_winCapture
->ReleaseMouse();
4039 m_winCapture
= NULL
;
4041 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4044 // Show the edit control, if it has been hidden for
4046 ShowCellEditControl();
4048 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4050 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4051 DoEndDragResizeRow();
4053 // Note: we are ending the event *after* doing
4054 // default processing in this case
4056 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4058 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4060 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4061 DoEndDragResizeCol();
4063 // Note: we are ending the event *after* doing
4064 // default processing in this case
4066 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4073 // ------------ Right button down
4075 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4077 DisableCellEditControl();
4078 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4083 // no default action at the moment
4088 // ------------ Right double click
4090 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4092 DisableCellEditControl();
4093 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4098 // no default action at the moment
4102 // ------------ Moving and no button action
4104 else if ( event
.Moving() && !event
.IsButton() )
4106 int dragRow
= YToEdgeOfRow( y
);
4107 int dragCol
= XToEdgeOfCol( x
);
4109 // Dragging on the corner of a cell to resize in both
4110 // directions is not implemented yet...
4112 if ( dragRow
>= 0 && dragCol
>= 0 )
4114 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4120 m_dragRowOrCol
= dragRow
;
4122 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4124 if ( CanDragRowSize() && CanDragGridSize() )
4125 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4130 m_dragRowOrCol
= dragCol
;
4138 m_dragRowOrCol
= dragCol
;
4140 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4142 if ( CanDragColSize() && CanDragGridSize() )
4143 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4149 // Neither on a row or col edge
4151 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4153 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4159 void wxGrid::DoEndDragResizeRow()
4161 if ( m_dragLastPos
>= 0 )
4163 // erase the last line and resize the row
4165 int cw
, ch
, left
, dummy
;
4166 m_gridWin
->GetClientSize( &cw
, &ch
);
4167 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4169 wxClientDC
dc( m_gridWin
);
4171 dc
.SetLogicalFunction( wxINVERT
);
4172 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4173 HideCellEditControl();
4174 SaveEditControlValue();
4176 int rowTop
= GetRowTop(m_dragRowOrCol
);
4177 SetRowSize( m_dragRowOrCol
,
4178 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4180 if ( !GetBatchCount() )
4182 // Only needed to get the correct rect.y:
4183 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4185 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4186 rect
.width
= m_rowLabelWidth
;
4187 rect
.height
= ch
- rect
.y
;
4188 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4190 m_gridWin
->Refresh( FALSE
, &rect
);
4193 ShowCellEditControl();
4198 void wxGrid::DoEndDragResizeCol()
4200 if ( m_dragLastPos
>= 0 )
4202 // erase the last line and resize the col
4204 int cw
, ch
, dummy
, top
;
4205 m_gridWin
->GetClientSize( &cw
, &ch
);
4206 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4208 wxClientDC
dc( m_gridWin
);
4210 dc
.SetLogicalFunction( wxINVERT
);
4211 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4212 HideCellEditControl();
4213 SaveEditControlValue();
4215 int colLeft
= GetColLeft(m_dragRowOrCol
);
4216 SetColSize( m_dragRowOrCol
,
4217 wxMax( m_dragLastPos
- colLeft
,
4218 GetColMinimalWidth(m_dragRowOrCol
) ) );
4220 if ( !GetBatchCount() )
4222 // Only needed to get the correct rect.x:
4223 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4225 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4226 rect
.width
= cw
- rect
.x
;
4227 rect
.height
= m_colLabelHeight
;
4228 m_colLabelWin
->Refresh( TRUE
, &rect
);
4230 m_gridWin
->Refresh( FALSE
, &rect
);
4233 ShowCellEditControl();
4240 // ------ interaction with data model
4242 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4244 switch ( msg
.GetId() )
4246 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4247 return GetModelValues();
4249 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4250 return SetModelValues();
4252 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4253 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4254 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4255 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4256 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4257 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4258 return Redimension( msg
);
4267 // The behaviour of this function depends on the grid table class
4268 // Clear() function. For the default wxGridStringTable class the
4269 // behavious is to replace all cell contents with wxEmptyString but
4270 // not to change the number of rows or cols.
4272 void wxGrid::ClearGrid()
4276 if (IsCellEditControlEnabled())
4277 DisableCellEditControl();
4280 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4285 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4287 // TODO: something with updateLabels flag
4291 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4297 if (IsCellEditControlEnabled())
4298 DisableCellEditControl();
4300 bool ok
= m_table
->InsertRows( pos
, numRows
);
4302 // the table will have sent the results of the insert row
4303 // operation to this view object as a grid table message
4307 if ( m_numCols
== 0 )
4309 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4311 // TODO: perhaps instead of appending the default number of cols
4312 // we should remember what the last non-zero number of cols was ?
4316 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4318 // if we have just inserted cols into an empty grid the current
4319 // cell will be undefined...
4321 SetCurrentCell( 0, 0 );
4325 if ( !GetBatchCount() ) Refresh();
4337 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4339 // TODO: something with updateLabels flag
4343 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4347 if ( m_table
&& m_table
->AppendRows( numRows
) )
4349 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4351 // if we have just inserted cols into an empty grid the current
4352 // cell will be undefined...
4354 SetCurrentCell( 0, 0 );
4357 // the table will have sent the results of the append row
4358 // operation to this view object as a grid table message
4361 if ( !GetBatchCount() ) Refresh();
4371 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4373 // TODO: something with updateLabels flag
4377 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4383 if (IsCellEditControlEnabled())
4384 DisableCellEditControl();
4386 if (m_table
->DeleteRows( pos
, numRows
))
4389 // the table will have sent the results of the delete row
4390 // operation to this view object as a grid table message
4393 if ( !GetBatchCount() ) Refresh();
4401 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4403 // TODO: something with updateLabels flag
4407 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4413 if (IsCellEditControlEnabled())
4414 DisableCellEditControl();
4416 bool ok
= m_table
->InsertCols( pos
, numCols
);
4418 // the table will have sent the results of the insert col
4419 // operation to this view object as a grid table message
4423 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4425 // if we have just inserted cols into an empty grid the current
4426 // cell will be undefined...
4428 SetCurrentCell( 0, 0 );
4432 if ( !GetBatchCount() ) Refresh();
4444 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4446 // TODO: something with updateLabels flag
4450 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4454 if ( m_table
&& m_table
->AppendCols( numCols
) )
4456 // the table will have sent the results of the append col
4457 // operation to this view object as a grid table message
4459 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4461 // if we have just inserted cols into an empty grid the current
4462 // cell will be undefined...
4464 SetCurrentCell( 0, 0 );
4468 if ( !GetBatchCount() ) Refresh();
4478 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4480 // TODO: something with updateLabels flag
4484 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4490 if (IsCellEditControlEnabled())
4491 DisableCellEditControl();
4493 if ( m_table
->DeleteCols( pos
, numCols
) )
4495 // the table will have sent the results of the delete col
4496 // operation to this view object as a grid table message
4499 if ( !GetBatchCount() ) Refresh();
4509 // ----- event handlers
4512 // Generate a grid event based on a mouse event and
4513 // return the result of ProcessEvent()
4515 bool wxGrid::SendEvent( const wxEventType type
,
4517 wxMouseEvent
& mouseEv
)
4519 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4521 int rowOrCol
= (row
== -1 ? col
: row
);
4523 wxGridSizeEvent
gridEvt( GetId(),
4527 mouseEv
.GetX(), mouseEv
.GetY(),
4528 mouseEv
.ControlDown(),
4529 mouseEv
.ShiftDown(),
4531 mouseEv
.MetaDown() );
4533 return GetEventHandler()->ProcessEvent(gridEvt
);
4535 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4537 wxGridRangeSelectEvent
gridEvt( GetId(),
4541 m_selectedBottomRight
,
4542 mouseEv
.ControlDown(),
4543 mouseEv
.ShiftDown(),
4545 mouseEv
.MetaDown() );
4547 return GetEventHandler()->ProcessEvent(gridEvt
);
4551 wxGridEvent
gridEvt( GetId(),
4555 mouseEv
.GetX(), mouseEv
.GetY(),
4556 mouseEv
.ControlDown(),
4557 mouseEv
.ShiftDown(),
4559 mouseEv
.MetaDown() );
4561 return GetEventHandler()->ProcessEvent(gridEvt
);
4566 // Generate a grid event of specified type and return the result
4567 // of ProcessEvent().
4569 bool wxGrid::SendEvent( const wxEventType type
,
4572 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4574 int rowOrCol
= (row
== -1 ? col
: row
);
4576 wxGridSizeEvent
gridEvt( GetId(),
4581 return GetEventHandler()->ProcessEvent(gridEvt
);
4585 wxGridEvent
gridEvt( GetId(),
4590 return GetEventHandler()->ProcessEvent(gridEvt
);
4595 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4597 wxPaintDC
dc( this );
4599 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4600 m_numRows
&& m_numCols
)
4602 m_currentCellCoords
.Set(0, 0);
4603 ShowCellEditControl();
4610 // This is just here to make sure that CalcDimensions gets called when
4611 // the grid view is resized... then the size event is skipped to allow
4612 // the box sizers to handle everything
4614 void wxGrid::OnSize( wxSizeEvent
& event
)
4621 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4623 if ( m_inOnKeyDown
)
4625 // shouldn't be here - we are going round in circles...
4627 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4630 m_inOnKeyDown
= TRUE
;
4632 // propagate the event up and see if it gets processed
4634 wxWindow
*parent
= GetParent();
4635 wxKeyEvent
keyEvt( event
);
4636 keyEvt
.SetEventObject( parent
);
4638 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4641 // TODO: Should also support Shift-cursor keys for
4642 // extending the selection. Maybe add a flag to
4643 // MoveCursorXXX() and MoveCursorXXXBlock() and
4644 // just send event.ShiftDown().
4646 // try local handlers
4648 switch ( event
.KeyCode() )
4651 if ( event
.ControlDown() )
4653 MoveCursorUpBlock();
4662 if ( event
.ControlDown() )
4664 MoveCursorDownBlock();
4673 if ( event
.ControlDown() )
4675 MoveCursorLeftBlock();
4684 if ( event
.ControlDown() )
4686 MoveCursorRightBlock();
4695 if ( event
.ControlDown() )
4697 event
.Skip(); // to let the edit control have the return
4706 if (event
.ShiftDown())
4713 if ( event
.ControlDown() )
4715 MakeCellVisible( 0, 0 );
4716 SetCurrentCell( 0, 0 );
4725 if ( event
.ControlDown() )
4727 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4728 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4745 if ( !IsEditable() )
4750 // Otherwise fall through to default
4753 // alphanumeric keys or F2 (special key just for this) enable
4754 // the cell edit control
4755 if ( !(event
.AltDown() ||
4757 event
.ControlDown()) &&
4758 (isalnum(event
.KeyCode()) || event
.KeyCode() == WXK_F2
) &&
4759 !IsCellEditControlEnabled() &&
4760 CanEnableCellControl() )
4762 EnableCellEditControl();
4763 int row
= m_currentCellCoords
.GetRow();
4764 int col
= m_currentCellCoords
.GetCol();
4765 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4766 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4771 // let others process char events with modifiers or all
4772 // char events for readonly cells
4779 m_inOnKeyDown
= FALSE
;
4783 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4787 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4789 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4791 // the event has been intercepted - do nothing
4796 m_currentCellCoords
!= wxGridNoCellCoords
)
4798 HideCellEditControl();
4799 DisableCellEditControl();
4801 // Clear the old current cell highlight
4802 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4804 // Otherwise refresh redraws the highlight!
4805 m_currentCellCoords
= coords
;
4807 m_gridWin
->Refresh( FALSE
, &r
);
4810 m_currentCellCoords
= coords
;
4814 wxClientDC
dc(m_gridWin
);
4817 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4818 DrawCellHighlight(dc
, attr
);
4821 if ( IsSelection() )
4823 wxRect
r( SelectionToDeviceRect() );
4825 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4832 // ------ functions to get/send data (see also public functions)
4835 bool wxGrid::GetModelValues()
4839 // all we need to do is repaint the grid
4841 m_gridWin
->Refresh();
4849 bool wxGrid::SetModelValues()
4855 for ( row
= 0; row
< m_numRows
; row
++ )
4857 for ( col
= 0; col
< m_numCols
; col
++ )
4859 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4871 // Note - this function only draws cells that are in the list of
4872 // exposed cells (usually set from the update region by
4873 // CalcExposedCells)
4875 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4877 if ( !m_numRows
|| !m_numCols
) return;
4880 size_t numCells
= m_cellsExposed
.GetCount();
4882 for ( i
= 0; i
< numCells
; i
++ )
4884 DrawCell( dc
, m_cellsExposed
[i
] );
4889 void wxGrid::DrawGridSpace( wxDC
& dc
)
4891 if ( m_numRows
&& m_numCols
)
4894 m_gridWin
->GetClientSize( &cw
, &ch
);
4897 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4899 if ( right
> GetColRight(m_numCols
-1) ||
4900 bottom
> GetRowBottom(m_numRows
-1) )
4903 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4905 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4906 dc
.SetPen( *wxTRANSPARENT_PEN
);
4908 if ( right
> GetColRight(m_numCols
-1) )
4909 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
4910 right
- GetColRight(m_numCols
-1), ch
);
4912 if ( bottom
> GetRowBottom(m_numRows
-1) )
4913 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
4914 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4920 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4922 int row
= coords
.GetRow();
4923 int col
= coords
.GetCol();
4925 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4928 // we draw the cell border ourselves
4929 #if !WXGRID_DRAW_LINES
4930 if ( m_gridLinesEnabled
)
4931 DrawCellBorder( dc
, coords
);
4934 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4936 bool isCurrent
= coords
== m_currentCellCoords
;
4939 rect
.x
= GetColLeft(col
);
4940 rect
.y
= GetRowTop(row
);
4941 rect
.width
= GetColWidth(col
) - 1;
4942 rect
.height
= GetRowHeight(row
) - 1;
4944 // if the editor is shown, we should use it and not the renderer
4945 if ( isCurrent
&& IsCellEditControlEnabled() )
4947 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4951 // but all the rest is drawn by the cell renderer and hence may be
4953 attr
->GetRenderer(this, row
, col
)->
4954 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4961 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4963 int row
= m_currentCellCoords
.GetRow();
4964 int col
= m_currentCellCoords
.GetCol();
4966 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4970 rect
.x
= GetColLeft(col
);
4971 rect
.y
= GetRowTop(row
);
4972 rect
.width
= GetColWidth(col
) - 1;
4973 rect
.height
= GetRowHeight(row
) - 1;
4975 // hmmm... what could we do here to show that the cell is disabled?
4976 // for now, I just draw a thinner border than for the other ones, but
4977 // it doesn't look really good
4978 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4979 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4981 dc
.DrawRectangle(rect
);
4984 // VZ: my experiments with 3d borders...
4986 // how to properly set colours for arbitrary bg?
4987 wxCoord x1
= rect
.x
,
4989 x2
= rect
.x
+ rect
.width
-1,
4990 y2
= rect
.y
+ rect
.height
-1;
4992 dc
.SetPen(*wxWHITE_PEN
);
4993 dc
.DrawLine(x1
, y1
, x2
, y1
);
4994 dc
.DrawLine(x1
, y1
, x1
, y2
);
4996 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4997 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4999 dc
.SetPen(*wxBLACK_PEN
);
5000 dc
.DrawLine(x1
, y2
, x2
, y2
);
5001 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5006 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5008 int row
= coords
.GetRow();
5009 int col
= coords
.GetCol();
5010 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5013 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5015 // right hand border
5017 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5018 GetColRight(col
), GetRowBottom(row
) );
5022 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5023 GetColRight(col
), GetRowBottom(row
) );
5026 void wxGrid::DrawHighlight(wxDC
& dc
)
5028 if ( IsCellEditControlEnabled() )
5030 // don't show highlight when the edit control is shown
5034 // if the active cell was repainted, repaint its highlight too because it
5035 // might have been damaged by the grid lines
5036 size_t count
= m_cellsExposed
.GetCount();
5037 for ( size_t n
= 0; n
< count
; n
++ )
5039 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5041 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5042 DrawCellHighlight(dc
, attr
);
5050 // TODO: remove this ???
5051 // This is used to redraw all grid lines e.g. when the grid line colour
5054 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5056 if ( !m_gridLinesEnabled
||
5058 !m_numCols
) return;
5060 int top
, bottom
, left
, right
;
5066 m_gridWin
->GetClientSize(&cw
, &ch
);
5068 // virtual coords of visible area
5070 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5071 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5076 reg
.GetBox(x
, y
, w
, h
);
5077 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5078 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5082 m_gridWin
->GetClientSize(&cw
, &ch
);
5083 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5084 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5087 // avoid drawing grid lines past the last row and col
5089 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5090 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5092 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5094 // horizontal grid lines
5097 for ( i
= 0; i
< m_numRows
; i
++ )
5099 int bot
= GetRowBottom(i
) - 1;
5108 dc
.DrawLine( left
, bot
, right
, bot
);
5113 // vertical grid lines
5115 for ( i
= 0; i
< m_numCols
; i
++ )
5117 int colRight
= GetColRight(i
) - 1;
5118 if ( colRight
> right
)
5123 if ( colRight
>= left
)
5125 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5131 void wxGrid::DrawRowLabels( wxDC
& dc
)
5133 if ( !m_numRows
|| !m_numCols
) return;
5136 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5138 for ( i
= 0; i
< numLabels
; i
++ )
5140 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5145 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5147 if ( GetRowHeight(row
) <= 0 )
5150 int rowTop
= GetRowTop(row
),
5151 rowBottom
= GetRowBottom(row
) - 1;
5153 dc
.SetPen( *wxBLACK_PEN
);
5154 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5155 m_rowLabelWidth
-1, rowBottom
);
5157 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5159 dc
.SetPen( *wxWHITE_PEN
);
5160 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5161 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5163 dc
.SetBackgroundMode( wxTRANSPARENT
);
5164 dc
.SetTextForeground( GetLabelTextColour() );
5165 dc
.SetFont( GetLabelFont() );
5168 GetRowLabelAlignment( &hAlign
, &vAlign
);
5172 rect
.SetY( GetRowTop(row
) + 2 );
5173 rect
.SetWidth( m_rowLabelWidth
- 4 );
5174 rect
.SetHeight( GetRowHeight(row
) - 4 );
5175 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5179 void wxGrid::DrawColLabels( wxDC
& dc
)
5181 if ( !m_numRows
|| !m_numCols
) return;
5184 size_t numLabels
= m_colLabelsExposed
.GetCount();
5186 for ( i
= 0; i
< numLabels
; i
++ )
5188 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5193 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5195 if ( GetColWidth(col
) <= 0 )
5198 int colLeft
= GetColLeft(col
),
5199 colRight
= GetColRight(col
) - 1;
5201 dc
.SetPen( *wxBLACK_PEN
);
5202 dc
.DrawLine( colRight
, 0,
5203 colRight
, m_colLabelHeight
-1 );
5205 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5206 colRight
, m_colLabelHeight
-1 );
5208 dc
.SetPen( *wxWHITE_PEN
);
5209 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5210 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5212 dc
.SetBackgroundMode( wxTRANSPARENT
);
5213 dc
.SetTextForeground( GetLabelTextColour() );
5214 dc
.SetFont( GetLabelFont() );
5216 dc
.SetBackgroundMode( wxTRANSPARENT
);
5217 dc
.SetTextForeground( GetLabelTextColour() );
5218 dc
.SetFont( GetLabelFont() );
5221 GetColLabelAlignment( &hAlign
, &vAlign
);
5224 rect
.SetX( colLeft
+ 2 );
5226 rect
.SetWidth( GetColWidth(col
) - 4 );
5227 rect
.SetHeight( m_colLabelHeight
- 4 );
5228 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5232 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5233 const wxString
& value
,
5238 long textWidth
, textHeight
;
5239 long lineWidth
, lineHeight
;
5240 wxArrayString lines
;
5242 dc
.SetClippingRegion( rect
);
5243 StringToLines( value
, lines
);
5244 if ( lines
.GetCount() )
5246 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5247 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5250 switch ( horizAlign
)
5253 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5257 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5266 switch ( vertAlign
)
5269 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5273 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5282 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5284 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5289 dc
.DestroyClippingRegion();
5293 // Split multi line text up into an array of strings. Any existing
5294 // contents of the string array are preserved.
5296 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5300 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5301 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5303 while ( startPos
< (int)tVal
.Length() )
5305 pos
= tVal
.Mid(startPos
).Find( eol
);
5310 else if ( pos
== 0 )
5312 lines
.Add( wxEmptyString
);
5316 lines
.Add( value
.Mid(startPos
, pos
) );
5320 if ( startPos
< (int)value
.Length() )
5322 lines
.Add( value
.Mid( startPos
) );
5327 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5328 wxArrayString
& lines
,
5329 long *width
, long *height
)
5336 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5338 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5339 w
= wxMax( w
, lineW
);
5349 // ------ Edit control functions
5353 void wxGrid::EnableEditing( bool edit
)
5355 // TODO: improve this ?
5357 if ( edit
!= m_editable
)
5361 // FIXME IMHO this won't disable the edit control if edit == FALSE
5362 // because of the check in the beginning of
5363 // EnableCellEditControl() just below (VZ)
5364 EnableCellEditControl(m_editable
);
5369 void wxGrid::EnableCellEditControl( bool enable
)
5374 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5375 SetCurrentCell( 0, 0 );
5377 if ( enable
!= m_cellEditCtrlEnabled
)
5379 // TODO allow the app to Veto() this event?
5380 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5384 // this should be checked by the caller!
5385 wxASSERT_MSG( CanEnableCellControl(),
5386 _T("can't enable editing for this cell!") );
5388 // do it before ShowCellEditControl()
5389 m_cellEditCtrlEnabled
= enable
;
5391 ShowCellEditControl();
5395 HideCellEditControl();
5396 SaveEditControlValue();
5398 // do it after HideCellEditControl()
5399 m_cellEditCtrlEnabled
= enable
;
5404 bool wxGrid::IsCurrentCellReadOnly() const
5407 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5408 bool readonly
= attr
->IsReadOnly();
5414 bool wxGrid::CanEnableCellControl() const
5416 return m_editable
&& !IsCurrentCellReadOnly();
5419 bool wxGrid::IsCellEditControlEnabled() const
5421 // the cell edit control might be disable for all cells or just for the
5422 // current one if it's read only
5423 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5426 void wxGrid::ShowCellEditControl()
5428 if ( IsCellEditControlEnabled() )
5430 if ( !IsVisible( m_currentCellCoords
) )
5436 wxRect rect
= CellToRect( m_currentCellCoords
);
5437 int row
= m_currentCellCoords
.GetRow();
5438 int col
= m_currentCellCoords
.GetCol();
5440 // convert to scrolled coords
5442 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5444 // done in PaintBackground()
5446 // erase the highlight and the cell contents because the editor
5447 // might not cover the entire cell
5448 wxClientDC
dc( m_gridWin
);
5450 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5451 dc
.SetPen(*wxTRANSPARENT_PEN
);
5452 dc
.DrawRectangle(rect
);
5455 // cell is shifted by one pixel
5459 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5460 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5461 if ( !editor
->IsCreated() )
5463 editor
->Create(m_gridWin
, -1,
5464 new wxGridCellEditorEvtHandler(this, editor
));
5467 editor
->Show( TRUE
, attr
);
5469 editor
->SetSize( rect
);
5471 editor
->BeginEdit(row
, col
, this);
5478 void wxGrid::HideCellEditControl()
5480 if ( IsCellEditControlEnabled() )
5482 int row
= m_currentCellCoords
.GetRow();
5483 int col
= m_currentCellCoords
.GetCol();
5485 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5486 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5488 m_gridWin
->SetFocus();
5493 void wxGrid::SaveEditControlValue()
5495 if ( IsCellEditControlEnabled() )
5497 int row
= m_currentCellCoords
.GetRow();
5498 int col
= m_currentCellCoords
.GetCol();
5500 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5501 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5502 bool changed
= editor
->EndEdit(row
, col
, this);
5508 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5509 m_currentCellCoords
.GetRow(),
5510 m_currentCellCoords
.GetCol() );
5517 // ------ Grid location functions
5518 // Note that all of these functions work with the logical coordinates of
5519 // grid cells and labels so you will need to convert from device
5520 // coordinates for mouse events etc.
5523 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5525 int row
= YToRow(y
);
5526 int col
= XToCol(x
);
5528 if ( row
== -1 || col
== -1 )
5530 coords
= wxGridNoCellCoords
;
5534 coords
.Set( row
, col
);
5539 int wxGrid::YToRow( int y
)
5543 for ( i
= 0; i
< m_numRows
; i
++ )
5545 if ( y
< GetRowBottom(i
) )
5553 int wxGrid::XToCol( int x
)
5557 for ( i
= 0; i
< m_numCols
; i
++ )
5559 if ( x
< GetColRight(i
) )
5567 // return the row number that that the y coord is near the edge of, or
5568 // -1 if not near an edge
5570 int wxGrid::YToEdgeOfRow( int y
)
5574 for ( i
= 0; i
< m_numRows
; i
++ )
5576 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5578 d
= abs( y
- GetRowBottom(i
) );
5579 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5588 // return the col number that that the x coord is near the edge of, or
5589 // -1 if not near an edge
5591 int wxGrid::XToEdgeOfCol( int x
)
5595 for ( i
= 0; i
< m_numCols
; i
++ )
5597 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5599 d
= abs( x
- GetColRight(i
) );
5600 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5609 wxRect
wxGrid::CellToRect( int row
, int col
)
5611 wxRect
rect( -1, -1, -1, -1 );
5613 if ( row
>= 0 && row
< m_numRows
&&
5614 col
>= 0 && col
< m_numCols
)
5616 rect
.x
= GetColLeft(col
);
5617 rect
.y
= GetRowTop(row
);
5618 rect
.width
= GetColWidth(col
);
5619 rect
.height
= GetRowHeight(row
);
5626 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5628 // get the cell rectangle in logical coords
5630 wxRect
r( CellToRect( row
, col
) );
5632 // convert to device coords
5634 int left
, top
, right
, bottom
;
5635 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5636 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5638 // check against the client area of the grid window
5641 m_gridWin
->GetClientSize( &cw
, &ch
);
5643 if ( wholeCellVisible
)
5645 // is the cell wholly visible ?
5647 return ( left
>= 0 && right
<= cw
&&
5648 top
>= 0 && bottom
<= ch
);
5652 // is the cell partly visible ?
5654 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5655 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5660 // make the specified cell location visible by doing a minimal amount
5663 void wxGrid::MakeCellVisible( int row
, int col
)
5666 int xpos
= -1, ypos
= -1;
5668 if ( row
>= 0 && row
< m_numRows
&&
5669 col
>= 0 && col
< m_numCols
)
5671 // get the cell rectangle in logical coords
5673 wxRect
r( CellToRect( row
, col
) );
5675 // convert to device coords
5677 int left
, top
, right
, bottom
;
5678 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5679 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5682 m_gridWin
->GetClientSize( &cw
, &ch
);
5688 else if ( bottom
> ch
)
5690 int h
= r
.GetHeight();
5692 for ( i
= row
-1; i
>= 0; i
-- )
5694 int rowHeight
= GetRowHeight(i
);
5695 if ( h
+ rowHeight
> ch
)
5702 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5703 // have rounding errors (this is important, because if we do, we
5704 // might not scroll at all and some cells won't be redrawn)
5705 ypos
+= GRID_SCROLL_LINE
/ 2;
5712 else if ( right
> cw
)
5714 int w
= r
.GetWidth();
5716 for ( i
= col
-1; i
>= 0; i
-- )
5718 int colWidth
= GetColWidth(i
);
5719 if ( w
+ colWidth
> cw
)
5726 // see comment for ypos above
5727 xpos
+= GRID_SCROLL_LINE
/ 2;
5730 if ( xpos
!= -1 || ypos
!= -1 )
5732 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5733 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5734 Scroll( xpos
, ypos
);
5742 // ------ Grid cursor movement functions
5745 bool wxGrid::MoveCursorUp()
5747 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5748 m_currentCellCoords
.GetRow() > 0 )
5750 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5751 m_currentCellCoords
.GetCol() );
5753 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5754 m_currentCellCoords
.GetCol() );
5763 bool wxGrid::MoveCursorDown()
5765 // TODO: allow for scrolling
5767 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5768 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5770 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5771 m_currentCellCoords
.GetCol() );
5773 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5774 m_currentCellCoords
.GetCol() );
5783 bool wxGrid::MoveCursorLeft()
5785 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5786 m_currentCellCoords
.GetCol() > 0 )
5788 MakeCellVisible( m_currentCellCoords
.GetRow(),
5789 m_currentCellCoords
.GetCol() - 1 );
5791 SetCurrentCell( m_currentCellCoords
.GetRow(),
5792 m_currentCellCoords
.GetCol() - 1 );
5801 bool wxGrid::MoveCursorRight()
5803 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5804 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5806 MakeCellVisible( m_currentCellCoords
.GetRow(),
5807 m_currentCellCoords
.GetCol() + 1 );
5809 SetCurrentCell( m_currentCellCoords
.GetRow(),
5810 m_currentCellCoords
.GetCol() + 1 );
5819 bool wxGrid::MovePageUp()
5821 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5823 int row
= m_currentCellCoords
.GetRow();
5827 m_gridWin
->GetClientSize( &cw
, &ch
);
5829 int y
= GetRowTop(row
);
5830 int newRow
= YToRow( y
- ch
+ 1 );
5835 else if ( newRow
== row
)
5840 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5841 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5849 bool wxGrid::MovePageDown()
5851 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5853 int row
= m_currentCellCoords
.GetRow();
5854 if ( row
< m_numRows
)
5857 m_gridWin
->GetClientSize( &cw
, &ch
);
5859 int y
= GetRowTop(row
);
5860 int newRow
= YToRow( y
+ ch
);
5863 newRow
= m_numRows
- 1;
5865 else if ( newRow
== row
)
5870 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5871 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5879 bool wxGrid::MoveCursorUpBlock()
5882 m_currentCellCoords
!= wxGridNoCellCoords
&&
5883 m_currentCellCoords
.GetRow() > 0 )
5885 int row
= m_currentCellCoords
.GetRow();
5886 int col
= m_currentCellCoords
.GetCol();
5888 if ( m_table
->IsEmptyCell(row
, col
) )
5890 // starting in an empty cell: find the next block of
5896 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5899 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5901 // starting at the top of a block: find the next block
5907 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5912 // starting within a block: find the top of the block
5917 if ( m_table
->IsEmptyCell(row
, col
) )
5925 MakeCellVisible( row
, col
);
5926 SetCurrentCell( row
, col
);
5934 bool wxGrid::MoveCursorDownBlock()
5937 m_currentCellCoords
!= wxGridNoCellCoords
&&
5938 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5940 int row
= m_currentCellCoords
.GetRow();
5941 int col
= m_currentCellCoords
.GetCol();
5943 if ( m_table
->IsEmptyCell(row
, col
) )
5945 // starting in an empty cell: find the next block of
5948 while ( row
< m_numRows
-1 )
5951 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5954 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5956 // starting at the bottom of a block: find the next block
5959 while ( row
< m_numRows
-1 )
5962 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5967 // starting within a block: find the bottom of the block
5969 while ( row
< m_numRows
-1 )
5972 if ( m_table
->IsEmptyCell(row
, col
) )
5980 MakeCellVisible( row
, col
);
5981 SetCurrentCell( row
, col
);
5989 bool wxGrid::MoveCursorLeftBlock()
5992 m_currentCellCoords
!= wxGridNoCellCoords
&&
5993 m_currentCellCoords
.GetCol() > 0 )
5995 int row
= m_currentCellCoords
.GetRow();
5996 int col
= m_currentCellCoords
.GetCol();
5998 if ( m_table
->IsEmptyCell(row
, col
) )
6000 // starting in an empty cell: find the next block of
6006 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6009 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6011 // starting at the left of a block: find the next block
6017 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6022 // starting within a block: find the left of the block
6027 if ( m_table
->IsEmptyCell(row
, col
) )
6035 MakeCellVisible( row
, col
);
6036 SetCurrentCell( row
, col
);
6044 bool wxGrid::MoveCursorRightBlock()
6047 m_currentCellCoords
!= wxGridNoCellCoords
&&
6048 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6050 int row
= m_currentCellCoords
.GetRow();
6051 int col
= m_currentCellCoords
.GetCol();
6053 if ( m_table
->IsEmptyCell(row
, col
) )
6055 // starting in an empty cell: find the next block of
6058 while ( col
< m_numCols
-1 )
6061 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6064 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6066 // starting at the right of a block: find the next block
6069 while ( col
< m_numCols
-1 )
6072 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6077 // starting within a block: find the right of the block
6079 while ( col
< m_numCols
-1 )
6082 if ( m_table
->IsEmptyCell(row
, col
) )
6090 MakeCellVisible( row
, col
);
6091 SetCurrentCell( row
, col
);
6102 // ------ Label values and formatting
6105 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6107 *horiz
= m_rowLabelHorizAlign
;
6108 *vert
= m_rowLabelVertAlign
;
6111 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6113 *horiz
= m_colLabelHorizAlign
;
6114 *vert
= m_colLabelVertAlign
;
6117 wxString
wxGrid::GetRowLabelValue( int row
)
6121 return m_table
->GetRowLabelValue( row
);
6131 wxString
wxGrid::GetColLabelValue( int col
)
6135 return m_table
->GetColLabelValue( col
);
6146 void wxGrid::SetRowLabelSize( int width
)
6148 width
= wxMax( width
, 0 );
6149 if ( width
!= m_rowLabelWidth
)
6153 m_rowLabelWin
->Show( FALSE
);
6154 m_cornerLabelWin
->Show( FALSE
);
6156 else if ( m_rowLabelWidth
== 0 )
6158 m_rowLabelWin
->Show( TRUE
);
6159 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6162 m_rowLabelWidth
= width
;
6169 void wxGrid::SetColLabelSize( int height
)
6171 height
= wxMax( height
, 0 );
6172 if ( height
!= m_colLabelHeight
)
6176 m_colLabelWin
->Show( FALSE
);
6177 m_cornerLabelWin
->Show( FALSE
);
6179 else if ( m_colLabelHeight
== 0 )
6181 m_colLabelWin
->Show( TRUE
);
6182 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6185 m_colLabelHeight
= height
;
6192 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6194 if ( m_labelBackgroundColour
!= colour
)
6196 m_labelBackgroundColour
= colour
;
6197 m_rowLabelWin
->SetBackgroundColour( colour
);
6198 m_colLabelWin
->SetBackgroundColour( colour
);
6199 m_cornerLabelWin
->SetBackgroundColour( colour
);
6201 if ( !GetBatchCount() )
6203 m_rowLabelWin
->Refresh();
6204 m_colLabelWin
->Refresh();
6205 m_cornerLabelWin
->Refresh();
6210 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6212 if ( m_labelTextColour
!= colour
)
6214 m_labelTextColour
= colour
;
6215 if ( !GetBatchCount() )
6217 m_rowLabelWin
->Refresh();
6218 m_colLabelWin
->Refresh();
6223 void wxGrid::SetLabelFont( const wxFont
& font
)
6226 if ( !GetBatchCount() )
6228 m_rowLabelWin
->Refresh();
6229 m_colLabelWin
->Refresh();
6233 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6235 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6237 m_rowLabelHorizAlign
= horiz
;
6240 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6242 m_rowLabelVertAlign
= vert
;
6245 if ( !GetBatchCount() )
6247 m_rowLabelWin
->Refresh();
6251 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6253 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6255 m_colLabelHorizAlign
= horiz
;
6258 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6260 m_colLabelVertAlign
= vert
;
6263 if ( !GetBatchCount() )
6265 m_colLabelWin
->Refresh();
6269 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6273 m_table
->SetRowLabelValue( row
, s
);
6274 if ( !GetBatchCount() )
6276 wxRect rect
= CellToRect( row
, 0);
6277 if ( rect
.height
> 0 )
6279 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6281 rect
.width
= m_rowLabelWidth
;
6282 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6288 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6292 m_table
->SetColLabelValue( col
, s
);
6293 if ( !GetBatchCount() )
6295 wxRect rect
= CellToRect( 0, col
);
6296 if ( rect
.width
> 0 )
6298 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6300 rect
.height
= m_colLabelHeight
;
6301 m_colLabelWin
->Refresh( TRUE
, &rect
);
6307 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6309 if ( m_gridLineColour
!= colour
)
6311 m_gridLineColour
= colour
;
6313 wxClientDC
dc( m_gridWin
);
6315 DrawAllGridLines( dc
, wxRegion() );
6319 void wxGrid::EnableGridLines( bool enable
)
6321 if ( enable
!= m_gridLinesEnabled
)
6323 m_gridLinesEnabled
= enable
;
6325 if ( !GetBatchCount() )
6329 wxClientDC
dc( m_gridWin
);
6331 DrawAllGridLines( dc
, wxRegion() );
6335 m_gridWin
->Refresh();
6342 int wxGrid::GetDefaultRowSize()
6344 return m_defaultRowHeight
;
6347 int wxGrid::GetRowSize( int row
)
6349 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6351 return GetRowHeight(row
);
6354 int wxGrid::GetDefaultColSize()
6356 return m_defaultColWidth
;
6359 int wxGrid::GetColSize( int col
)
6361 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6363 return GetColWidth(col
);
6366 // ============================================================================
6367 // access to the grid attributes: each of them has a default value in the grid
6368 // itself and may be overidden on a per-cell basis
6369 // ============================================================================
6371 // ----------------------------------------------------------------------------
6372 // setting default attributes
6373 // ----------------------------------------------------------------------------
6375 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6377 m_defaultCellAttr
->SetBackgroundColour(col
);
6379 m_gridWin
->SetBackgroundColour(col
);
6383 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6385 m_defaultCellAttr
->SetTextColour(col
);
6388 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6390 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6393 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6395 m_defaultCellAttr
->SetFont(font
);
6398 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6400 m_defaultCellAttr
->SetRenderer(renderer
);
6403 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6405 m_defaultCellAttr
->SetEditor(editor
);
6408 // ----------------------------------------------------------------------------
6409 // access to the default attrbiutes
6410 // ----------------------------------------------------------------------------
6412 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6414 return m_defaultCellAttr
->GetBackgroundColour();
6417 wxColour
wxGrid::GetDefaultCellTextColour()
6419 return m_defaultCellAttr
->GetTextColour();
6422 wxFont
wxGrid::GetDefaultCellFont()
6424 return m_defaultCellAttr
->GetFont();
6427 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6429 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6432 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6434 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6437 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6439 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6442 // ----------------------------------------------------------------------------
6443 // access to cell attributes
6444 // ----------------------------------------------------------------------------
6446 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6448 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6449 wxColour colour
= attr
->GetBackgroundColour();
6454 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6456 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6457 wxColour colour
= attr
->GetTextColour();
6462 wxFont
wxGrid::GetCellFont( int row
, int col
)
6464 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6465 wxFont font
= attr
->GetFont();
6470 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6472 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6473 attr
->GetAlignment(horiz
, vert
);
6477 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6479 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6480 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6485 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6487 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6488 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6493 bool wxGrid::IsReadOnly(int row
, int col
) const
6495 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6496 bool isReadOnly
= attr
->IsReadOnly();
6501 // ----------------------------------------------------------------------------
6502 // attribute support: cache, automatic provider creation, ...
6503 // ----------------------------------------------------------------------------
6505 bool wxGrid::CanHaveAttributes()
6512 return m_table
->CanHaveAttributes();
6515 void wxGrid::ClearAttrCache()
6517 if ( m_attrCache
.row
!= -1 )
6519 m_attrCache
.attr
->SafeDecRef();
6520 m_attrCache
.row
= -1;
6524 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6526 wxGrid
*self
= (wxGrid
*)this; // const_cast
6528 self
->ClearAttrCache();
6529 self
->m_attrCache
.row
= row
;
6530 self
->m_attrCache
.col
= col
;
6531 self
->m_attrCache
.attr
= attr
;
6535 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6537 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6539 *attr
= m_attrCache
.attr
;
6540 (*attr
)->SafeIncRef();
6542 #ifdef DEBUG_ATTR_CACHE
6543 gs_nAttrCacheHits
++;
6550 #ifdef DEBUG_ATTR_CACHE
6551 gs_nAttrCacheMisses
++;
6557 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6559 wxGridCellAttr
*attr
;
6560 if ( !LookupAttr(row
, col
, &attr
) )
6562 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6563 CacheAttr(row
, col
, attr
);
6567 attr
->SetDefAttr(m_defaultCellAttr
);
6571 attr
= m_defaultCellAttr
;
6578 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6580 wxGridCellAttr
*attr
;
6581 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6583 wxASSERT_MSG( m_table
,
6584 _T("we may only be called if CanHaveAttributes() "
6585 "returned TRUE and then m_table should be !NULL") );
6587 attr
= m_table
->GetAttr(row
, col
);
6590 attr
= new wxGridCellAttr
;
6592 // artificially inc the ref count to match DecRef() in caller
6595 m_table
->SetAttr(attr
, row
, col
);
6598 CacheAttr(row
, col
, attr
);
6600 attr
->SetDefAttr(m_defaultCellAttr
);
6604 // ----------------------------------------------------------------------------
6605 // setting cell attributes: this is forwarded to the table
6606 // ----------------------------------------------------------------------------
6608 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6610 if ( CanHaveAttributes() )
6612 m_table
->SetRowAttr(attr
, row
);
6620 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6622 if ( CanHaveAttributes() )
6624 m_table
->SetColAttr(attr
, col
);
6632 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6634 if ( CanHaveAttributes() )
6636 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6637 attr
->SetBackgroundColour(colour
);
6642 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6644 if ( CanHaveAttributes() )
6646 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6647 attr
->SetTextColour(colour
);
6652 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6654 if ( CanHaveAttributes() )
6656 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6657 attr
->SetFont(font
);
6662 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6664 if ( CanHaveAttributes() )
6666 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6667 attr
->SetAlignment(horiz
, vert
);
6672 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6674 if ( CanHaveAttributes() )
6676 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6677 attr
->SetRenderer(renderer
);
6682 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6684 if ( CanHaveAttributes() )
6686 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6687 attr
->SetEditor(editor
);
6692 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6694 if ( CanHaveAttributes() )
6696 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6697 attr
->SetReadOnly(isReadOnly
);
6702 // ----------------------------------------------------------------------------
6703 // Data type registration
6704 // ----------------------------------------------------------------------------
6706 void wxGrid::RegisterDataType(const wxString
& typeName
,
6707 wxGridCellRenderer
* renderer
,
6708 wxGridCellEditor
* editor
)
6710 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6714 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6716 wxString typeName
= m_table
->GetTypeName(row
, col
);
6717 return GetDefaultEditorForType(typeName
);
6720 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6722 wxString typeName
= m_table
->GetTypeName(row
, col
);
6723 return GetDefaultRendererForType(typeName
);
6727 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6729 int index
= m_typeRegistry
->FindDataType(typeName
);
6731 // Should we force the failure here or let it fallback to string handling???
6732 // wxFAIL_MSG(wxT("Unknown data type name"));
6735 return m_typeRegistry
->GetEditor(index
);
6739 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6741 int index
= m_typeRegistry
->FindDataType(typeName
);
6743 // Should we force the failure here or let it fallback to string handling???
6744 // wxFAIL_MSG(wxT("Unknown data type name"));
6747 return m_typeRegistry
->GetRenderer(index
);
6751 // ----------------------------------------------------------------------------
6753 // ----------------------------------------------------------------------------
6755 void wxGrid::EnableDragRowSize( bool enable
)
6757 m_canDragRowSize
= enable
;
6761 void wxGrid::EnableDragColSize( bool enable
)
6763 m_canDragColSize
= enable
;
6766 void wxGrid::EnableDragGridSize( bool enable
)
6768 m_canDragGridSize
= enable
;
6772 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6774 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6776 if ( resizeExistingRows
)
6784 void wxGrid::SetRowSize( int row
, int height
)
6786 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6788 if ( m_rowHeights
.IsEmpty() )
6790 // need to really create the array
6794 int h
= wxMax( 0, height
);
6795 int diff
= h
- m_rowHeights
[row
];
6797 m_rowHeights
[row
] = h
;
6799 for ( i
= row
; i
< m_numRows
; i
++ )
6801 m_rowBottoms
[i
] += diff
;
6806 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6808 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6810 if ( resizeExistingCols
)
6818 void wxGrid::SetColSize( int col
, int width
)
6820 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6822 // should we check that it's bigger than GetColMinimalWidth(col) here?
6824 if ( m_colWidths
.IsEmpty() )
6826 // need to really create the array
6830 int w
= wxMax( 0, width
);
6831 int diff
= w
- m_colWidths
[col
];
6832 m_colWidths
[col
] = w
;
6835 for ( i
= col
; i
< m_numCols
; i
++ )
6837 m_colRights
[i
] += diff
;
6843 void wxGrid::SetColMinimalWidth( int col
, int width
)
6845 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6848 int wxGrid::GetColMinimalWidth(int col
) const
6850 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6851 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6854 // ----------------------------------------------------------------------------
6856 // ----------------------------------------------------------------------------
6858 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6860 wxClientDC
dc(m_gridWin
);
6862 wxCoord width
, widthMax
= 0;
6863 for ( int row
= 0; row
< m_numRows
; row
++ )
6865 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6866 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6869 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6870 if ( width
> widthMax
)
6879 // now also compare with the column label width
6880 dc
.SetFont( GetLabelFont() );
6881 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6882 if ( width
> widthMax
)
6889 // empty column - give default width (notice that if widthMax is less
6890 // than default width but != 0, it's ok)
6891 widthMax
= m_defaultColWidth
;
6895 // leave some space around text
6899 SetColSize(col
, widthMax
);
6902 SetColMinimalWidth(col
, widthMax
);
6906 void wxGrid::AutoSizeColumns( bool setAsMin
)
6908 int width
= m_rowLabelWidth
;
6910 for ( int col
= 0; col
< m_numCols
; col
++ )
6912 AutoSizeColumn(col
, setAsMin
);
6914 width
+= GetColWidth(col
);
6917 // also set the grid size to just fit the columns
6921 void wxGrid::AutoSizeRows(bool WXUNUSED(setAsMin
))
6923 int height
= m_colLabelHeight
;
6925 for ( int row
= 0; row
< m_numRows
; row
++ )
6927 // AutoSizeRow(row, setAsMin) -- TODO
6929 height
+= GetRowHeight(row
);
6932 SetSize(-1, height
);
6935 void wxGrid::AutoSize()
6941 // ----------------------------------------------------------------------------
6942 // cell value accessor functions
6943 // ----------------------------------------------------------------------------
6945 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6949 m_table
->SetValue( row
, col
, s
.c_str() );
6950 if ( !GetBatchCount() )
6952 wxClientDC
dc( m_gridWin
);
6954 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6957 if ( m_currentCellCoords
.GetRow() == row
&&
6958 m_currentCellCoords
.GetCol() == col
&&
6959 IsCellEditControlEnabled())
6961 HideCellEditControl();
6962 ShowCellEditControl(); // will reread data from table
6969 // ------ Block, row and col selection
6972 void wxGrid::SelectRow( int row
, bool addToSelected
)
6976 if ( IsSelection() && addToSelected
)
6979 bool need_refresh
[4];
6983 need_refresh
[3] = FALSE
;
6987 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6988 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6989 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6990 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6994 need_refresh
[0] = TRUE
;
6995 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6996 wxGridCellCoords ( oldTop
- 1,
6998 m_selectedTopLeft
.SetRow( row
);
7003 need_refresh
[1] = TRUE
;
7004 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
7005 wxGridCellCoords ( oldBottom
,
7008 m_selectedTopLeft
.SetCol( 0 );
7011 if ( oldBottom
< row
)
7013 need_refresh
[2] = TRUE
;
7014 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
7015 wxGridCellCoords ( row
,
7017 m_selectedBottomRight
.SetRow( row
);
7020 if ( oldRight
< m_numCols
- 1 )
7022 need_refresh
[3] = TRUE
;
7023 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7025 wxGridCellCoords ( oldBottom
,
7027 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
7030 for (i
= 0; i
< 4; i
++ )
7031 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7032 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7036 r
= SelectionToDeviceRect();
7038 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7040 m_selectedTopLeft
.Set( row
, 0 );
7041 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7042 r
= SelectionToDeviceRect();
7043 m_gridWin
->Refresh( FALSE
, &r
);
7046 wxGridRangeSelectEvent
gridEvt( GetId(),
7047 wxEVT_GRID_RANGE_SELECT
,
7050 m_selectedBottomRight
);
7052 GetEventHandler()->ProcessEvent(gridEvt
);
7056 void wxGrid::SelectCol( int col
, bool addToSelected
)
7058 if ( IsSelection() && addToSelected
)
7061 bool need_refresh
[4];
7065 need_refresh
[3] = FALSE
;
7068 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7069 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7070 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7071 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7073 if ( oldLeft
> col
)
7075 need_refresh
[0] = TRUE
;
7076 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7077 wxGridCellCoords ( m_numRows
- 1,
7079 m_selectedTopLeft
.SetCol( col
);
7084 need_refresh
[1] = TRUE
;
7085 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7086 wxGridCellCoords ( oldTop
- 1,
7088 m_selectedTopLeft
.SetRow( 0 );
7091 if ( oldRight
< col
)
7093 need_refresh
[2] = TRUE
;
7094 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7095 wxGridCellCoords ( m_numRows
- 1,
7097 m_selectedBottomRight
.SetCol( col
);
7100 if ( oldBottom
< m_numRows
- 1 )
7102 need_refresh
[3] = TRUE
;
7103 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7105 wxGridCellCoords ( m_numRows
- 1,
7107 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7110 for (i
= 0; i
< 4; i
++ )
7111 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7112 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7118 r
= SelectionToDeviceRect();
7120 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7122 m_selectedTopLeft
.Set( 0, col
);
7123 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7124 r
= SelectionToDeviceRect();
7125 m_gridWin
->Refresh( FALSE
, &r
);
7128 wxGridRangeSelectEvent
gridEvt( GetId(),
7129 wxEVT_GRID_RANGE_SELECT
,
7132 m_selectedBottomRight
);
7134 GetEventHandler()->ProcessEvent(gridEvt
);
7138 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7141 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7143 if ( topRow
> bottomRow
)
7150 if ( leftCol
> rightCol
)
7157 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7158 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7160 if ( m_selectedTopLeft
!= updateTopLeft
||
7161 m_selectedBottomRight
!= updateBottomRight
)
7163 // Compute two optimal update rectangles:
7164 // Either one rectangle is a real subset of the
7165 // other, or they are (almost) disjoint!
7167 bool need_refresh
[4];
7171 need_refresh
[3] = FALSE
;
7174 // Store intermediate values
7175 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7176 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7177 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7178 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7180 // Determine the outer/inner coordinates.
7181 if (oldLeft
> leftCol
)
7187 if (oldTop
> topRow
)
7193 if (oldRight
< rightCol
)
7196 oldRight
= rightCol
;
7199 if (oldBottom
< bottomRow
)
7202 oldBottom
= bottomRow
;
7206 // Now, either the stuff marked old is the outer
7207 // rectangle or we don't have a situation where one
7208 // is contained in the other.
7210 if ( oldLeft
< leftCol
)
7212 need_refresh
[0] = TRUE
;
7213 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7215 wxGridCellCoords ( oldBottom
,
7219 if ( oldTop
< topRow
)
7221 need_refresh
[1] = TRUE
;
7222 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7224 wxGridCellCoords ( topRow
- 1,
7228 if ( oldRight
> rightCol
)
7230 need_refresh
[2] = TRUE
;
7231 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7233 wxGridCellCoords ( oldBottom
,
7237 if ( oldBottom
> bottomRow
)
7239 need_refresh
[3] = TRUE
;
7240 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7242 wxGridCellCoords ( oldBottom
,
7248 m_selectedTopLeft
= updateTopLeft
;
7249 m_selectedBottomRight
= updateBottomRight
;
7251 // various Refresh() calls
7252 for (i
= 0; i
< 4; i
++ )
7253 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7254 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7257 // only generate an event if the block is not being selected by
7258 // dragging the mouse (in which case the event will be generated in
7259 // the mouse event handler)
7260 if ( !m_isDragging
)
7262 wxGridRangeSelectEvent
gridEvt( GetId(),
7263 wxEVT_GRID_RANGE_SELECT
,
7266 m_selectedBottomRight
);
7268 GetEventHandler()->ProcessEvent(gridEvt
);
7272 void wxGrid::SelectAll()
7274 m_selectedTopLeft
.Set( 0, 0 );
7275 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7277 m_gridWin
->Refresh();
7281 void wxGrid::ClearSelection()
7283 m_selectedTopLeft
= wxGridNoCellCoords
;
7284 m_selectedBottomRight
= wxGridNoCellCoords
;
7288 // This function returns the rectangle that encloses the given block
7289 // in device coords clipped to the client size of the grid window.
7291 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7292 const wxGridCellCoords
&bottomRight
)
7294 wxRect
rect( wxGridNoCellRect
);
7297 cellRect
= CellToRect( topLeft
);
7298 if ( cellRect
!= wxGridNoCellRect
)
7304 rect
= wxRect( 0, 0, 0, 0 );
7307 cellRect
= CellToRect( bottomRight
);
7308 if ( cellRect
!= wxGridNoCellRect
)
7314 return wxGridNoCellRect
;
7317 // convert to scrolled coords
7319 int left
, top
, right
, bottom
;
7320 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7321 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7324 m_gridWin
->GetClientSize( &cw
, &ch
);
7326 rect
.SetLeft( wxMax(0, left
) );
7327 rect
.SetTop( wxMax(0, top
) );
7328 rect
.SetRight( wxMin(cw
, right
) );
7329 rect
.SetBottom( wxMin(ch
, bottom
) );
7337 // ------ Grid event classes
7340 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7342 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7343 int row
, int col
, int x
, int y
,
7344 bool control
, bool shift
, bool alt
, bool meta
)
7345 : wxNotifyEvent( type
, id
)
7351 m_control
= control
;
7356 SetEventObject(obj
);
7360 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7362 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7363 int rowOrCol
, int x
, int y
,
7364 bool control
, bool shift
, bool alt
, bool meta
)
7365 : wxNotifyEvent( type
, id
)
7367 m_rowOrCol
= rowOrCol
;
7370 m_control
= control
;
7375 SetEventObject(obj
);
7379 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7381 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7382 const wxGridCellCoords
& topLeft
,
7383 const wxGridCellCoords
& bottomRight
,
7384 bool control
, bool shift
, bool alt
, bool meta
)
7385 : wxNotifyEvent( type
, id
)
7387 m_topLeft
= topLeft
;
7388 m_bottomRight
= bottomRight
;
7389 m_control
= control
;
7394 SetEventObject(obj
);
7398 #endif // ifndef wxUSE_NEW_GRID