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__)
486 rect
.Inflate(rect
.x
? 1 : 0, rect
.y
? 1 : 0);
488 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
489 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
490 #if defined(__WXMOTIF__)
494 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
495 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
496 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
497 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
500 wxGridCellEditor::SetSize(rect
);
503 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
505 wxASSERT_MSG(m_control
,
506 wxT("The wxGridCellEditor must be Created first!"));
508 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
510 DoBeginEdit(m_startValue
);
513 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
515 Text()->SetValue(startValue
);
516 Text()->SetInsertionPointEnd();
520 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
523 wxASSERT_MSG(m_control
,
524 wxT("The wxGridCellEditor must be Created first!"));
526 bool changed
= FALSE
;
527 wxString value
= Text()->GetValue();
528 if (value
!= m_startValue
)
532 grid
->GetTable()->SetValue(row
, col
, value
);
534 m_startValue
= wxEmptyString
;
535 Text()->SetValue(m_startValue
);
541 void wxGridCellTextEditor::Reset()
543 wxASSERT_MSG(m_control
,
544 wxT("The wxGridCellEditor must be Created first!"));
546 DoReset(m_startValue
);
549 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
551 Text()->SetValue(startValue
);
552 Text()->SetInsertionPointEnd();
555 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
557 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
559 // insert the key in the control
560 long keycode
= event
.KeyCode();
561 if ( isprint(keycode
) )
563 // FIXME this is not going to work for non letters...
564 if ( !event
.ShiftDown() )
566 keycode
= tolower(keycode
);
569 Text()->AppendText((wxChar
)keycode
);
579 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
581 #if defined(__WXMOTIF__) || defined(__WXGTK__)
582 // wxMotif needs a little extra help...
583 int pos
= Text()->GetInsertionPoint();
584 wxString
s( Text()->GetValue() );
585 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
587 Text()->SetInsertionPoint( pos
);
589 // the other ports can handle a Return key press
595 // ----------------------------------------------------------------------------
596 // wxGridCellNumberEditor
597 // ----------------------------------------------------------------------------
599 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
605 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
607 wxEvtHandler
* evtHandler
)
611 // create a spin ctrl
612 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
613 wxDefaultPosition
, wxDefaultSize
,
617 wxGridCellEditor::Create(parent
, id
, evtHandler
);
621 // just a text control
622 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
625 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
626 #endif // wxUSE_VALIDATORS
630 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
632 // first get the value
633 wxGridTableBase
*table
= grid
->GetTable();
634 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
636 m_valueOld
= table
->GetValueAsLong(row
, col
);
640 wxString sValue
= table
->GetValue(row
, col
);
641 if (! sValue
.ToLong(&m_valueOld
))
643 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
650 Spin()->SetValue(m_valueOld
);
654 DoBeginEdit(GetString());
658 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
666 value
= Spin()->GetValue();
667 changed
= value
!= m_valueOld
;
671 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
676 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
677 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
679 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%ld", value
));
685 void wxGridCellNumberEditor::Reset()
689 Spin()->SetValue(m_valueOld
);
693 DoReset(GetString());
697 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
701 long keycode
= event
.KeyCode();
702 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
704 wxGridCellTextEditor::StartingKey(event
);
714 // ----------------------------------------------------------------------------
715 // wxGridCellFloatEditor
716 // ----------------------------------------------------------------------------
718 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
720 wxEvtHandler
* evtHandler
)
722 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
725 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
726 #endif // wxUSE_VALIDATORS
729 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
731 // first get the value
732 wxGridTableBase
*table
= grid
->GetTable();
733 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
735 m_valueOld
= table
->GetValueAsDouble(row
, col
);
739 wxString sValue
= table
->GetValue(row
, col
);
740 if (! sValue
.ToDouble(&m_valueOld
))
742 wxFAIL_MSG( _T("this cell doesn't have float value") );
747 DoBeginEdit(GetString());
750 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
754 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
756 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
757 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
759 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%f", value
));
769 void wxGridCellFloatEditor::Reset()
771 DoReset(GetString());
774 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
776 long keycode
= event
.KeyCode();
777 if ( isdigit(keycode
) ||
778 keycode
== '+' || keycode
== '-' || keycode
== '.' )
780 wxGridCellTextEditor::StartingKey(event
);
789 // ----------------------------------------------------------------------------
790 // wxGridCellBoolEditor
791 // ----------------------------------------------------------------------------
793 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
795 wxEvtHandler
* evtHandler
)
797 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
798 wxDefaultPosition
, wxDefaultSize
,
801 wxGridCellEditor::Create(parent
, id
, evtHandler
);
804 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
807 wxSize size
= m_control
->GetSize();
808 wxCoord minSize
= wxMin(r
.width
, r
.height
);
810 // check if the checkbox is not too big/small for this cell
811 wxSize sizeBest
= m_control
->GetBestSize();
812 if ( !(size
== sizeBest
) )
814 // reset to default size if it had been made smaller
820 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
822 // leave 1 pixel margin
823 size
.x
= size
.y
= minSize
- 2;
830 m_control
->SetSize(size
);
833 // position it in the centre of the rectangle (TODO: support alignment?)
835 #if defined(__WXGTK__) || defined (__WXMOTIF__)
836 // the checkbox without label still has some space to the right in wxGTK,
837 // so shift it to the right
839 #elif defined(__WXMSW__)
845 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
848 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
850 m_control
->Show(show
);
854 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
855 CBox()->SetBackgroundColour(colBg
);
859 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
861 wxASSERT_MSG(m_control
,
862 wxT("The wxGridCellEditor must be Created first!"));
864 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
865 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
867 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
868 CBox()->SetValue(m_startValue
);
872 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
875 wxASSERT_MSG(m_control
,
876 wxT("The wxGridCellEditor must be Created first!"));
878 bool changed
= FALSE
;
879 bool value
= CBox()->GetValue();
880 if ( value
!= m_startValue
)
885 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
886 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
888 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
894 void wxGridCellBoolEditor::Reset()
896 wxASSERT_MSG(m_control
,
897 wxT("The wxGridCellEditor must be Created first!"));
899 CBox()->SetValue(m_startValue
);
902 void wxGridCellBoolEditor::StartingClick()
904 CBox()->SetValue(!CBox()->GetValue());
907 // ----------------------------------------------------------------------------
908 // wxGridCellChoiceEditor
909 // ----------------------------------------------------------------------------
911 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
912 const wxChar
* choices
[],
914 : m_allowOthers(allowOthers
)
916 m_choices
.Alloc(count
);
917 for ( size_t n
= 0; n
< count
; n
++ )
919 m_choices
.Add(choices
[n
]);
923 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
925 wxEvtHandler
* evtHandler
)
927 size_t count
= m_choices
.GetCount();
928 wxString
*choices
= new wxString
[count
];
929 for ( size_t n
= 0; n
< count
; n
++ )
931 choices
[n
] = m_choices
[n
];
934 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
935 wxDefaultPosition
, wxDefaultSize
,
937 m_allowOthers
? 0 : wxCB_READONLY
);
941 wxGridCellEditor::Create(parent
, id
, evtHandler
);
944 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
945 wxGridCellAttr
* attr
)
947 // as we fill the entire client area, don't do anything here to minimize
950 // TODO: It doesn't actually fill the client area since the height of a
951 // combo always defaults to the standard... Until someone has time to
952 // figure out the right rectangle to paint, just do it the normal way...
953 wxGridCellEditor::PaintBackground(rectCell
, attr
);
956 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
958 wxASSERT_MSG(m_control
,
959 wxT("The wxGridCellEditor must be Created first!"));
961 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
963 Combo()->SetValue(m_startValue
);
964 size_t count
= m_choices
.GetCount();
965 for (size_t i
=0; i
<count
; i
++)
967 if (m_startValue
== m_choices
[i
])
969 Combo()->SetSelection(i
);
973 Combo()->SetInsertionPointEnd();
977 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
980 wxString value
= Combo()->GetValue();
981 bool changed
= value
!= m_startValue
;
984 grid
->GetTable()->SetValue(row
, col
, value
);
986 m_startValue
= wxEmptyString
;
987 Combo()->SetValue(m_startValue
);
992 void wxGridCellChoiceEditor::Reset()
994 Combo()->SetValue(m_startValue
);
995 Combo()->SetInsertionPointEnd();
998 // ----------------------------------------------------------------------------
999 // wxGridCellEditorEvtHandler
1000 // ----------------------------------------------------------------------------
1002 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1004 switch ( event
.KeyCode() )
1008 m_grid
->DisableCellEditControl();
1012 event
.Skip( m_grid
->ProcessEvent( event
) );
1016 if (!m_grid
->ProcessEvent(event
))
1017 m_editor
->HandleReturn(event
);
1026 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1028 switch ( event
.KeyCode() )
1040 // ============================================================================
1042 // ============================================================================
1044 // ----------------------------------------------------------------------------
1045 // wxGridCellRenderer
1046 // ----------------------------------------------------------------------------
1048 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1049 wxGridCellAttr
& attr
,
1055 dc
.SetBackgroundMode( wxSOLID
);
1059 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1063 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1066 dc
.SetPen( *wxTRANSPARENT_PEN
);
1067 dc
.DrawRectangle(rect
);
1070 wxGridCellRenderer::~wxGridCellRenderer()
1074 // ----------------------------------------------------------------------------
1075 // wxGridCellStringRenderer
1076 // ----------------------------------------------------------------------------
1078 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1079 wxGridCellAttr
& attr
,
1083 dc
.SetBackgroundMode( wxTRANSPARENT
);
1085 // TODO some special colours for attr.IsReadOnly() case?
1089 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1090 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1094 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1095 dc
.SetTextForeground( attr
.GetTextColour() );
1098 dc
.SetFont( attr
.GetFont() );
1101 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1103 const wxString
& text
)
1106 dc
.SetFont(attr
.GetFont());
1107 dc
.GetTextExtent(text
, &x
, &y
);
1109 return wxSize(x
, y
);
1112 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1113 wxGridCellAttr
& attr
,
1117 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1120 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1121 wxGridCellAttr
& attr
,
1123 const wxRect
& rectCell
,
1127 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1129 // now we only have to draw the text
1130 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1133 attr
.GetAlignment(&hAlign
, &vAlign
);
1135 wxRect rect
= rectCell
;
1138 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1139 rect
, hAlign
, vAlign
);
1142 // ----------------------------------------------------------------------------
1143 // wxGridCellNumberRenderer
1144 // ----------------------------------------------------------------------------
1146 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1148 wxGridTableBase
*table
= grid
.GetTable();
1150 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1152 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1156 text
= table
->GetValue(row
, col
);
1162 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1163 wxGridCellAttr
& attr
,
1165 const wxRect
& rectCell
,
1169 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1171 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1173 // draw the text right aligned by default
1175 attr
.GetAlignment(&hAlign
, &vAlign
);
1178 wxRect rect
= rectCell
;
1181 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1184 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1185 wxGridCellAttr
& attr
,
1189 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1192 // ----------------------------------------------------------------------------
1193 // wxGridCellFloatRenderer
1194 // ----------------------------------------------------------------------------
1196 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1199 SetPrecision(precision
);
1202 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1204 wxGridTableBase
*table
= grid
.GetTable();
1206 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1210 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1213 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1217 text
= table
->GetValue(row
, col
);
1223 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1224 wxGridCellAttr
& attr
,
1226 const wxRect
& rectCell
,
1230 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1232 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1234 // draw the text right aligned by default
1236 attr
.GetAlignment(&hAlign
, &vAlign
);
1239 wxRect rect
= rectCell
;
1242 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1245 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1246 wxGridCellAttr
& attr
,
1250 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1253 // ----------------------------------------------------------------------------
1254 // wxGridCellBoolRenderer
1255 // ----------------------------------------------------------------------------
1257 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1259 // FIXME these checkbox size calculations are really ugly...
1261 // between checkmark and box
1263 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1265 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1268 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1269 wxGridCellAttr
& WXUNUSED(attr
),
1274 // compute it only once (no locks for MT safeness in GUI thread...)
1275 if ( !ms_sizeCheckMark
.x
)
1277 // get checkbox size
1278 wxCoord checkSize
= 0;
1279 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1280 wxSize size
= checkbox
->GetBestSize();
1281 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1283 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1284 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1285 checkSize
-= size
.y
/ 2;
1290 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1293 return ms_sizeCheckMark
;
1296 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1297 wxGridCellAttr
& attr
,
1303 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1305 // draw a check mark in the centre (ignoring alignment - TODO)
1306 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1308 // don't draw outside the cell
1309 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1310 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1312 // and even leave (at least) 1 pixel margin
1313 size
.x
= size
.y
= minSize
- 2;
1316 // draw a border around checkmark
1318 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1319 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1320 rectMark
.width
= size
.x
;
1321 rectMark
.height
= size
.y
;
1323 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1324 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1325 dc
.DrawRectangle(rectMark
);
1327 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1330 // looks nicer under MSW
1335 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1336 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1338 value
= !!grid
.GetTable()->GetValue(row
, col
);
1342 dc
.SetTextForeground(attr
.GetTextColour());
1343 dc
.DrawCheckMark(rectMark
);
1347 // ----------------------------------------------------------------------------
1349 // ----------------------------------------------------------------------------
1351 const wxColour
& wxGridCellAttr::GetTextColour() const
1353 if (HasTextColour())
1357 else if (m_defGridAttr
!= this)
1359 return m_defGridAttr
->GetTextColour();
1363 wxFAIL_MSG(wxT("Missing default cell attribute"));
1364 return wxNullColour
;
1369 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1371 if (HasBackgroundColour())
1373 else if (m_defGridAttr
!= this)
1374 return m_defGridAttr
->GetBackgroundColour();
1377 wxFAIL_MSG(wxT("Missing default cell attribute"));
1378 return wxNullColour
;
1383 const wxFont
& wxGridCellAttr::GetFont() const
1387 else if (m_defGridAttr
!= this)
1388 return m_defGridAttr
->GetFont();
1391 wxFAIL_MSG(wxT("Missing default cell attribute"));
1397 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1401 if ( hAlign
) *hAlign
= m_hAlign
;
1402 if ( vAlign
) *vAlign
= m_vAlign
;
1404 else if (m_defGridAttr
!= this)
1405 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1408 wxFAIL_MSG(wxT("Missing default cell attribute"));
1413 // GetRenderer and GetEditor use a slightly different decision path about
1414 // which attribute to use. If a non-default attr object has one then it is
1415 // used, otherwise the default editor or renderer is fetched from the grid and
1416 // used. It should be the default for the data type of the cell. If it is
1417 // NULL (because the table has a type that the grid does not have in its
1418 // registry,) then the grid's default editor or renderer is used.
1420 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1422 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1423 return m_renderer
; // use local attribute
1425 wxGridCellRenderer
* renderer
= NULL
;
1426 if (grid
) // get renderer for the data type
1427 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1430 // if we still don't have one then use the grid default
1431 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1434 wxFAIL_MSG(wxT("Missing default cell attribute"));
1439 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1441 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1442 return m_editor
; // use local attribute
1444 wxGridCellEditor
* editor
= NULL
;
1445 if (grid
) // get renderer for the data type
1446 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1449 // if we still don't have one then use the grid default
1450 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1453 wxFAIL_MSG(wxT("Missing default cell attribute"));
1458 // ----------------------------------------------------------------------------
1459 // wxGridCellAttrData
1460 // ----------------------------------------------------------------------------
1462 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1464 int n
= FindIndex(row
, col
);
1465 if ( n
== wxNOT_FOUND
)
1467 // add the attribute
1468 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1474 // change the attribute
1475 m_attrs
[(size_t)n
].attr
= attr
;
1479 // remove this attribute
1480 m_attrs
.RemoveAt((size_t)n
);
1485 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1487 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1489 int n
= FindIndex(row
, col
);
1490 if ( n
!= wxNOT_FOUND
)
1492 attr
= m_attrs
[(size_t)n
].attr
;
1499 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1501 size_t count
= m_attrs
.GetCount();
1502 for ( size_t n
= 0; n
< count
; n
++ )
1504 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1505 wxCoord row
= coords
.GetRow();
1506 if ((size_t)row
>= pos
)
1510 // If rows inserted, include row counter where necessary
1511 coords
.SetRow(row
+ numRows
);
1513 else if (numRows
< 0)
1515 // If rows deleted ...
1516 if ((size_t)row
>= pos
- numRows
)
1518 // ...either decrement row counter (if row still exists)...
1519 coords
.SetRow(row
+ numRows
);
1523 // ...or remove the attribute
1524 m_attrs
.RemoveAt((size_t)n
);
1532 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1534 size_t count
= m_attrs
.GetCount();
1535 for ( size_t n
= 0; n
< count
; n
++ )
1537 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1538 wxCoord col
= coords
.GetCol();
1539 if ( (size_t)col
>= pos
)
1543 // If rows inserted, include row counter where necessary
1544 coords
.SetCol(col
+ numCols
);
1546 else if (numCols
< 0)
1548 // If rows deleted ...
1549 if ((size_t)col
>= pos
- numCols
)
1551 // ...either decrement row counter (if row still exists)...
1552 coords
.SetCol(col
+ numCols
);
1556 // ...or remove the attribute
1557 m_attrs
.RemoveAt((size_t)n
);
1565 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1567 size_t count
= m_attrs
.GetCount();
1568 for ( size_t n
= 0; n
< count
; n
++ )
1570 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1571 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1580 // ----------------------------------------------------------------------------
1581 // wxGridRowOrColAttrData
1582 // ----------------------------------------------------------------------------
1584 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1586 size_t count
= m_attrs
.Count();
1587 for ( size_t n
= 0; n
< count
; n
++ )
1589 m_attrs
[n
]->DecRef();
1593 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1595 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1597 int n
= m_rowsOrCols
.Index(rowOrCol
);
1598 if ( n
!= wxNOT_FOUND
)
1600 attr
= m_attrs
[(size_t)n
];
1607 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1609 int n
= m_rowsOrCols
.Index(rowOrCol
);
1610 if ( n
== wxNOT_FOUND
)
1612 // add the attribute
1613 m_rowsOrCols
.Add(rowOrCol
);
1620 // change the attribute
1621 m_attrs
[(size_t)n
] = attr
;
1625 // remove this attribute
1626 m_attrs
[(size_t)n
]->DecRef();
1627 m_rowsOrCols
.RemoveAt((size_t)n
);
1628 m_attrs
.RemoveAt((size_t)n
);
1633 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1635 size_t count
= m_attrs
.GetCount();
1636 for ( size_t n
= 0; n
< count
; n
++ )
1638 int & rowOrCol
= m_rowsOrCols
[n
];
1639 if ( (size_t)rowOrCol
>= pos
)
1641 if ( numRowsOrCols
> 0 )
1643 // If rows inserted, include row counter where necessary
1644 rowOrCol
+= numRowsOrCols
;
1646 else if ( numRowsOrCols
< 0)
1648 // If rows deleted, either decrement row counter (if row still exists)
1649 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1650 rowOrCol
+= numRowsOrCols
;
1653 m_rowsOrCols
.RemoveAt((size_t)n
);
1654 m_attrs
.RemoveAt((size_t)n
);
1662 // ----------------------------------------------------------------------------
1663 // wxGridCellAttrProvider
1664 // ----------------------------------------------------------------------------
1666 wxGridCellAttrProvider::wxGridCellAttrProvider()
1668 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1671 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1676 void wxGridCellAttrProvider::InitData()
1678 m_data
= new wxGridCellAttrProviderData
;
1681 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1683 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1686 // first look for the attribute of this specific cell
1687 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1691 // then look for the col attr (col attributes are more common than
1692 // the row ones, hence they have priority)
1693 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1698 // finally try the row attributes
1699 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1706 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1712 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1715 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1720 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1723 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1728 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1731 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1735 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1737 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1741 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1745 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1747 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1751 // ----------------------------------------------------------------------------
1752 // wxGridTypeRegistry
1753 // ----------------------------------------------------------------------------
1755 wxGridTypeRegistry::~wxGridTypeRegistry()
1757 size_t count
= m_typeinfo
.Count();
1758 for ( size_t i
= 0; i
< count
; i
++ )
1759 delete m_typeinfo
[i
];
1763 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1764 wxGridCellRenderer
* renderer
,
1765 wxGridCellEditor
* editor
)
1768 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1770 // is it already registered?
1771 if ((loc
= FindDataType(typeName
)) != -1) {
1772 delete m_typeinfo
[loc
];
1773 m_typeinfo
[loc
] = info
;
1776 m_typeinfo
.Add(info
);
1780 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1784 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1785 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1794 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1796 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1800 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1802 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1806 // ----------------------------------------------------------------------------
1808 // ----------------------------------------------------------------------------
1810 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1813 wxGridTableBase::wxGridTableBase()
1815 m_view
= (wxGrid
*) NULL
;
1816 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1819 wxGridTableBase::~wxGridTableBase()
1821 delete m_attrProvider
;
1824 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1826 delete m_attrProvider
;
1827 m_attrProvider
= attrProvider
;
1830 bool wxGridTableBase::CanHaveAttributes()
1832 if ( ! GetAttrProvider() )
1834 // use the default attr provider by default
1835 SetAttrProvider(new wxGridCellAttrProvider
);
1840 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1842 if ( m_attrProvider
)
1843 return m_attrProvider
->GetAttr(row
, col
);
1845 return (wxGridCellAttr
*)NULL
;
1848 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1850 if ( m_attrProvider
)
1852 m_attrProvider
->SetAttr(attr
, row
, col
);
1856 // as we take ownership of the pointer and don't store it, we must
1862 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1864 if ( m_attrProvider
)
1866 m_attrProvider
->SetRowAttr(attr
, row
);
1870 // as we take ownership of the pointer and don't store it, we must
1876 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1878 if ( m_attrProvider
)
1880 m_attrProvider
->SetColAttr(attr
, col
);
1884 // as we take ownership of the pointer and don't store it, we must
1890 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1892 if ( m_attrProvider
)
1894 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1898 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1900 if ( m_attrProvider
)
1902 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1906 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1908 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1909 "but your derived table class does not override this function") );
1914 bool wxGridTableBase::AppendRows( size_t numRows
)
1916 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1917 "but your derived table class does not override this function"));
1922 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1924 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1925 "but your derived table class does not override this function"));
1930 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1932 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1933 "but your derived table class does not override this function"));
1938 bool wxGridTableBase::AppendCols( size_t numCols
)
1940 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1941 "but your derived table class does not override this function"));
1946 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1948 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1949 "but your derived table class does not override this function"));
1955 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1958 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1959 // how much it makes sense to us geeks.
1963 wxString
wxGridTableBase::GetColLabelValue( int col
)
1965 // default col labels are:
1966 // cols 0 to 25 : A-Z
1967 // cols 26 to 675 : AA-ZZ
1972 for ( n
= 1; ; n
++ )
1974 s
+= (_T('A') + (wxChar
)( col%26
));
1976 if ( col
< 0 ) break;
1979 // reverse the string...
1981 for ( i
= 0; i
< n
; i
++ )
1990 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1992 return wxGRID_VALUE_STRING
;
1995 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1996 const wxString
& typeName
)
1998 return typeName
== wxGRID_VALUE_STRING
;
2001 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2003 return CanGetValueAs(row
, col
, typeName
);
2006 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2011 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2016 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2021 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2022 long WXUNUSED(value
) )
2026 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2027 double WXUNUSED(value
) )
2031 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2032 bool WXUNUSED(value
) )
2037 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2038 const wxString
& WXUNUSED(typeName
) )
2043 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2044 const wxString
& WXUNUSED(typeName
),
2045 void* WXUNUSED(value
) )
2050 //////////////////////////////////////////////////////////////////////
2052 // Message class for the grid table to send requests and notifications
2056 wxGridTableMessage::wxGridTableMessage()
2058 m_table
= (wxGridTableBase
*) NULL
;
2064 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2065 int commandInt1
, int commandInt2
)
2069 m_comInt1
= commandInt1
;
2070 m_comInt2
= commandInt2
;
2075 //////////////////////////////////////////////////////////////////////
2077 // A basic grid table for string data. An object of this class will
2078 // created by wxGrid if you don't specify an alternative table class.
2081 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2083 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2085 wxGridStringTable::wxGridStringTable()
2090 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2095 m_data
.Alloc( numRows
);
2098 sa
.Alloc( numCols
);
2099 for ( col
= 0; col
< numCols
; col
++ )
2101 sa
.Add( wxEmptyString
);
2104 for ( row
= 0; row
< numRows
; row
++ )
2110 wxGridStringTable::~wxGridStringTable()
2114 long wxGridStringTable::GetNumberRows()
2116 return m_data
.GetCount();
2119 long wxGridStringTable::GetNumberCols()
2121 if ( m_data
.GetCount() > 0 )
2122 return m_data
[0].GetCount();
2127 wxString
wxGridStringTable::GetValue( int row
, int col
)
2129 // TODO: bounds checking
2131 return m_data
[row
][col
];
2134 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2136 // TODO: bounds checking
2138 m_data
[row
][col
] = value
;
2141 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2143 // TODO: bounds checking
2145 return (m_data
[row
][col
] == wxEmptyString
);
2149 void wxGridStringTable::Clear()
2152 int numRows
, numCols
;
2154 numRows
= m_data
.GetCount();
2157 numCols
= m_data
[0].GetCount();
2159 for ( row
= 0; row
< numRows
; row
++ )
2161 for ( col
= 0; col
< numCols
; col
++ )
2163 m_data
[row
][col
] = wxEmptyString
;
2170 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2174 size_t curNumRows
= m_data
.GetCount();
2175 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2177 if ( pos
>= curNumRows
)
2179 return AppendRows( numRows
);
2183 sa
.Alloc( curNumCols
);
2184 for ( col
= 0; col
< curNumCols
; col
++ )
2186 sa
.Add( wxEmptyString
);
2189 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2191 m_data
.Insert( sa
, row
);
2193 UpdateAttrRows( pos
, numRows
);
2196 wxGridTableMessage
msg( this,
2197 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2201 GetView()->ProcessTableMessage( msg
);
2207 bool wxGridStringTable::AppendRows( size_t numRows
)
2211 size_t curNumRows
= m_data
.GetCount();
2212 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2215 if ( curNumCols
> 0 )
2217 sa
.Alloc( curNumCols
);
2218 for ( col
= 0; col
< curNumCols
; col
++ )
2220 sa
.Add( wxEmptyString
);
2224 for ( row
= 0; row
< numRows
; row
++ )
2231 wxGridTableMessage
msg( this,
2232 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2235 GetView()->ProcessTableMessage( msg
);
2241 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2245 size_t curNumRows
= m_data
.GetCount();
2247 if ( pos
>= curNumRows
)
2250 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2251 "Pos value is invalid for present table with %d rows",
2252 pos
, numRows
, curNumRows
);
2253 wxFAIL_MSG( wxT(errmsg
) );
2257 if ( numRows
> curNumRows
- pos
)
2259 numRows
= curNumRows
- pos
;
2262 if ( numRows
>= curNumRows
)
2264 m_data
.Empty(); // don't release memory just yet
2268 for ( n
= 0; n
< numRows
; n
++ )
2270 m_data
.Remove( pos
);
2273 UpdateAttrRows( pos
, -((int)numRows
) );
2276 wxGridTableMessage
msg( this,
2277 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2281 GetView()->ProcessTableMessage( msg
);
2287 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2291 size_t curNumRows
= m_data
.GetCount();
2292 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2294 if ( pos
>= curNumCols
)
2296 return AppendCols( numCols
);
2299 for ( row
= 0; row
< curNumRows
; row
++ )
2301 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2303 m_data
[row
].Insert( wxEmptyString
, col
);
2306 UpdateAttrCols( pos
, numCols
);
2309 wxGridTableMessage
msg( this,
2310 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2314 GetView()->ProcessTableMessage( msg
);
2320 bool wxGridStringTable::AppendCols( size_t numCols
)
2324 size_t curNumRows
= m_data
.GetCount();
2327 // TODO: something better than this ?
2329 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2330 "Call AppendRows() first") );
2334 for ( row
= 0; row
< curNumRows
; row
++ )
2336 for ( n
= 0; n
< numCols
; n
++ )
2338 m_data
[row
].Add( wxEmptyString
);
2344 wxGridTableMessage
msg( this,
2345 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2348 GetView()->ProcessTableMessage( msg
);
2354 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2358 size_t curNumRows
= m_data
.GetCount();
2359 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2361 if ( pos
>= curNumCols
)
2364 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2365 "Pos value is invalid for present table with %d cols",
2366 pos
, numCols
, curNumCols
);
2367 wxFAIL_MSG( wxT( errmsg
) );
2371 if ( numCols
> curNumCols
- pos
)
2373 numCols
= curNumCols
- pos
;
2376 for ( row
= 0; row
< curNumRows
; row
++ )
2378 if ( numCols
>= curNumCols
)
2380 m_data
[row
].Clear();
2384 for ( n
= 0; n
< numCols
; n
++ )
2386 m_data
[row
].Remove( pos
);
2390 UpdateAttrCols( pos
, -((int)numCols
) );
2393 wxGridTableMessage
msg( this,
2394 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2398 GetView()->ProcessTableMessage( msg
);
2404 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2406 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2408 // using default label
2410 return wxGridTableBase::GetRowLabelValue( row
);
2414 return m_rowLabels
[ row
];
2418 wxString
wxGridStringTable::GetColLabelValue( int col
)
2420 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2422 // using default label
2424 return wxGridTableBase::GetColLabelValue( col
);
2428 return m_colLabels
[ col
];
2432 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2434 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2436 int n
= m_rowLabels
.GetCount();
2438 for ( i
= n
; i
<= row
; i
++ )
2440 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2444 m_rowLabels
[row
] = value
;
2447 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2449 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2451 int n
= m_colLabels
.GetCount();
2453 for ( i
= n
; i
<= col
; i
++ )
2455 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2459 m_colLabels
[col
] = value
;
2464 //////////////////////////////////////////////////////////////////////
2465 //////////////////////////////////////////////////////////////////////
2467 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2469 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2470 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2471 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2472 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2475 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2477 const wxPoint
&pos
, const wxSize
&size
)
2478 : wxWindow( parent
, id
, pos
, size
)
2483 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2487 // NO - don't do this because it will set both the x and y origin
2488 // coords to match the parent scrolled window and we just want to
2489 // set the y coord - MB
2491 // m_owner->PrepareDC( dc );
2494 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2495 dc
.SetDeviceOrigin( 0, -y
);
2497 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2498 m_owner
->DrawRowLabels( dc
);
2502 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2504 m_owner
->ProcessRowLabelMouseEvent( event
);
2508 // This seems to be required for wxMotif otherwise the mouse
2509 // cursor must be in the cell edit control to get key events
2511 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2513 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2518 //////////////////////////////////////////////////////////////////////
2520 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2522 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2523 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2524 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2525 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2528 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2530 const wxPoint
&pos
, const wxSize
&size
)
2531 : wxWindow( parent
, id
, pos
, size
)
2536 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2540 // NO - don't do this because it will set both the x and y origin
2541 // coords to match the parent scrolled window and we just want to
2542 // set the x coord - MB
2544 // m_owner->PrepareDC( dc );
2547 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2548 dc
.SetDeviceOrigin( -x
, 0 );
2550 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2551 m_owner
->DrawColLabels( dc
);
2555 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2557 m_owner
->ProcessColLabelMouseEvent( event
);
2561 // This seems to be required for wxMotif otherwise the mouse
2562 // cursor must be in the cell edit control to get key events
2564 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2566 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2571 //////////////////////////////////////////////////////////////////////
2573 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2575 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2576 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2577 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2578 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2581 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2583 const wxPoint
&pos
, const wxSize
&size
)
2584 : wxWindow( parent
, id
, pos
, size
)
2589 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2593 int client_height
= 0;
2594 int client_width
= 0;
2595 GetClientSize( &client_width
, &client_height
);
2597 dc
.SetPen( *wxBLACK_PEN
);
2598 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2599 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2601 dc
.SetPen( *wxWHITE_PEN
);
2602 dc
.DrawLine( 0, 0, client_width
, 0 );
2603 dc
.DrawLine( 0, 0, 0, client_height
);
2607 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2609 m_owner
->ProcessCornerLabelMouseEvent( event
);
2613 // This seems to be required for wxMotif otherwise the mouse
2614 // cursor must be in the cell edit control to get key events
2616 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2618 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2623 //////////////////////////////////////////////////////////////////////
2625 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2627 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2628 EVT_PAINT( wxGridWindow::OnPaint
)
2629 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2630 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2631 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2634 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2635 wxGridRowLabelWindow
*rowLblWin
,
2636 wxGridColLabelWindow
*colLblWin
,
2637 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2638 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
2641 m_rowLabelWin
= rowLblWin
;
2642 m_colLabelWin
= colLblWin
;
2643 SetBackgroundColour( "WHITE" );
2647 wxGridWindow::~wxGridWindow()
2652 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2654 wxPaintDC
dc( this );
2655 m_owner
->PrepareDC( dc
);
2656 wxRegion reg
= GetUpdateRegion();
2657 m_owner
->CalcCellsExposed( reg
);
2658 m_owner
->DrawGridCellArea( dc
);
2659 #if WXGRID_DRAW_LINES
2660 m_owner
->DrawAllGridLines( dc
, reg
);
2662 m_owner
->DrawGridSpace( dc
);
2663 m_owner
->DrawHighlight( dc
);
2667 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2669 wxPanel::ScrollWindow( dx
, dy
, rect
);
2670 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2671 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2675 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2677 m_owner
->ProcessGridCellMouseEvent( event
);
2681 // This seems to be required for wxMotif otherwise the mouse
2682 // cursor must be in the cell edit control to get key events
2684 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2686 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2690 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2695 //////////////////////////////////////////////////////////////////////
2698 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2700 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2701 EVT_PAINT( wxGrid::OnPaint
)
2702 EVT_SIZE( wxGrid::OnSize
)
2703 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2704 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2707 wxGrid::wxGrid( wxWindow
*parent
,
2712 const wxString
& name
)
2713 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2714 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2723 m_defaultCellAttr
->SafeDecRef();
2725 #ifdef DEBUG_ATTR_CACHE
2726 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2727 wxPrintf(_T("wxGrid attribute cache statistics: "
2728 "total: %u, hits: %u (%u%%)\n"),
2729 total
, gs_nAttrCacheHits
,
2730 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2736 delete m_typeRegistry
;
2741 // ----- internal init and update functions
2744 void wxGrid::Create()
2746 m_created
= FALSE
; // set to TRUE by CreateGrid
2747 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2749 m_table
= (wxGridTableBase
*) NULL
;
2752 m_cellEditCtrlEnabled
= FALSE
;
2754 m_defaultCellAttr
= new wxGridCellAttr
;
2755 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2757 // Set default cell attributes
2758 m_defaultCellAttr
->SetFont(GetFont());
2759 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2760 m_defaultCellAttr
->SetTextColour(
2761 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2762 m_defaultCellAttr
->SetBackgroundColour(
2763 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2764 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2765 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2770 m_currentCellCoords
= wxGridNoCellCoords
;
2772 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2773 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2775 // data type registration: register all standard data types
2776 // TODO: may be allow the app to selectively disable some of them?
2777 m_typeRegistry
= new wxGridTypeRegistry
;
2778 RegisterDataType(wxGRID_VALUE_STRING
,
2779 new wxGridCellStringRenderer
,
2780 new wxGridCellTextEditor
);
2781 RegisterDataType(wxGRID_VALUE_BOOL
,
2782 new wxGridCellBoolRenderer
,
2783 new wxGridCellBoolEditor
);
2784 RegisterDataType(wxGRID_VALUE_NUMBER
,
2785 new wxGridCellNumberRenderer
,
2786 new wxGridCellNumberEditor
);
2788 // subwindow components that make up the wxGrid
2789 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2794 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2799 m_colLabelWin
= new wxGridColLabelWindow( this,
2804 m_gridWin
= new wxGridWindow( this,
2811 SetTargetWindow( m_gridWin
);
2815 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2819 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2824 m_numRows
= numRows
;
2825 m_numCols
= numCols
;
2827 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2828 m_table
->SetView( this );
2837 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2841 // RD: Actually, this should probably be allowed. I think it would be
2842 // nice to be able to switch multiple Tables in and out of a single
2843 // View at runtime. Is there anything in the implmentation that would
2846 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2851 m_numRows
= table
->GetNumberRows();
2852 m_numCols
= table
->GetNumberCols();
2855 m_table
->SetView( this );
2868 if ( m_numRows
<= 0 )
2869 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2871 if ( m_numCols
<= 0 )
2872 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2874 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2875 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2877 if ( m_rowLabelWin
)
2879 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2883 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2886 m_labelTextColour
= wxColour( _T("BLACK") );
2889 m_attrCache
.row
= -1;
2891 // TODO: something better than this ?
2893 m_labelFont
= this->GetFont();
2894 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2896 m_rowLabelHorizAlign
= wxLEFT
;
2897 m_rowLabelVertAlign
= wxCENTRE
;
2899 m_colLabelHorizAlign
= wxCENTRE
;
2900 m_colLabelVertAlign
= wxTOP
;
2902 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2903 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2905 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2906 m_defaultRowHeight
+= 8;
2908 m_defaultRowHeight
+= 4;
2911 m_gridLineColour
= wxColour( 128, 128, 255 );
2912 m_gridLinesEnabled
= TRUE
;
2914 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2915 m_winCapture
= (wxWindow
*)NULL
;
2916 m_canDragRowSize
= TRUE
;
2917 m_canDragColSize
= TRUE
;
2918 m_canDragGridSize
= TRUE
;
2920 m_dragRowOrCol
= -1;
2921 m_isDragging
= FALSE
;
2922 m_startDragPos
= wxDefaultPosition
;
2924 m_waitForSlowClick
= FALSE
;
2926 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2927 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2929 m_currentCellCoords
= wxGridNoCellCoords
;
2931 m_selectedTopLeft
= wxGridNoCellCoords
;
2932 m_selectedBottomRight
= wxGridNoCellCoords
;
2933 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2934 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2936 m_editable
= TRUE
; // default for whole grid
2938 m_inOnKeyDown
= FALSE
;
2942 // ----------------------------------------------------------------------------
2943 // the idea is to call these functions only when necessary because they create
2944 // quite big arrays which eat memory mostly unnecessary - in particular, if
2945 // default widths/heights are used for all rows/columns, we may not use these
2948 // with some extra code, it should be possible to only store the
2949 // widths/heights different from default ones but this will be done later...
2950 // ----------------------------------------------------------------------------
2952 void wxGrid::InitRowHeights()
2954 m_rowHeights
.Empty();
2955 m_rowBottoms
.Empty();
2957 m_rowHeights
.Alloc( m_numRows
);
2958 m_rowBottoms
.Alloc( m_numRows
);
2961 for ( int i
= 0; i
< m_numRows
; i
++ )
2963 m_rowHeights
.Add( m_defaultRowHeight
);
2964 rowBottom
+= m_defaultRowHeight
;
2965 m_rowBottoms
.Add( rowBottom
);
2969 void wxGrid::InitColWidths()
2971 m_colWidths
.Empty();
2972 m_colRights
.Empty();
2974 m_colWidths
.Alloc( m_numCols
);
2975 m_colRights
.Alloc( m_numCols
);
2977 for ( int i
= 0; i
< m_numCols
; i
++ )
2979 m_colWidths
.Add( m_defaultColWidth
);
2980 colRight
+= m_defaultColWidth
;
2981 m_colRights
.Add( colRight
);
2985 int wxGrid::GetColWidth(int col
) const
2987 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2990 int wxGrid::GetColLeft(int col
) const
2992 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2993 : m_colRights
[col
] - m_colWidths
[col
];
2996 int wxGrid::GetColRight(int col
) const
2998 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3002 int wxGrid::GetRowHeight(int row
) const
3004 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3007 int wxGrid::GetRowTop(int row
) const
3009 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3010 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3013 int wxGrid::GetRowBottom(int row
) const
3015 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3016 : m_rowBottoms
[row
];
3019 void wxGrid::CalcDimensions()
3022 GetClientSize( &cw
, &ch
);
3024 if ( m_numRows
> 0 && m_numCols
> 0 )
3026 int right
= GetColRight( m_numCols
-1 ) + 50;
3027 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
3029 // TODO: restore the scroll position that we had before sizing
3032 GetViewStart( &x
, &y
);
3033 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3034 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3040 void wxGrid::CalcWindowSizes()
3043 GetClientSize( &cw
, &ch
);
3045 if ( m_cornerLabelWin
->IsShown() )
3046 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3048 if ( m_colLabelWin
->IsShown() )
3049 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3051 if ( m_rowLabelWin
->IsShown() )
3052 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3054 if ( m_gridWin
->IsShown() )
3055 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3059 // this is called when the grid table sends a message to say that it
3060 // has been redimensioned
3062 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3066 // if we were using the default widths/heights so far, we must change them
3068 if ( m_colWidths
.IsEmpty() )
3073 if ( m_rowHeights
.IsEmpty() )
3078 switch ( msg
.GetId() )
3080 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3082 size_t pos
= msg
.GetCommandInt();
3083 int numRows
= msg
.GetCommandInt2();
3084 for ( i
= 0; i
< numRows
; i
++ )
3086 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3087 m_rowBottoms
.Insert( 0, pos
);
3089 m_numRows
+= numRows
;
3092 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3094 for ( i
= pos
; i
< m_numRows
; i
++ )
3096 bottom
+= m_rowHeights
[i
];
3097 m_rowBottoms
[i
] = bottom
;
3103 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3105 int numRows
= msg
.GetCommandInt();
3106 for ( i
= 0; i
< numRows
; i
++ )
3108 m_rowHeights
.Add( m_defaultRowHeight
);
3109 m_rowBottoms
.Add( 0 );
3112 int oldNumRows
= m_numRows
;
3113 m_numRows
+= numRows
;
3116 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3118 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3120 bottom
+= m_rowHeights
[i
];
3121 m_rowBottoms
[i
] = bottom
;
3127 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3129 size_t pos
= msg
.GetCommandInt();
3130 int numRows
= msg
.GetCommandInt2();
3131 for ( i
= 0; i
< numRows
; i
++ )
3133 m_rowHeights
.Remove( pos
);
3134 m_rowBottoms
.Remove( pos
);
3136 m_numRows
-= numRows
;
3141 m_colWidths
.Clear();
3142 m_colRights
.Clear();
3143 m_currentCellCoords
= wxGridNoCellCoords
;
3147 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3148 m_currentCellCoords
.Set( 0, 0 );
3151 for ( i
= 0; i
< m_numRows
; i
++ )
3153 h
+= m_rowHeights
[i
];
3154 m_rowBottoms
[i
] = h
;
3162 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3164 size_t pos
= msg
.GetCommandInt();
3165 int numCols
= msg
.GetCommandInt2();
3166 for ( i
= 0; i
< numCols
; i
++ )
3168 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3169 m_colRights
.Insert( 0, pos
);
3171 m_numCols
+= numCols
;
3174 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3176 for ( i
= pos
; i
< m_numCols
; i
++ )
3178 right
+= m_colWidths
[i
];
3179 m_colRights
[i
] = right
;
3185 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3187 int numCols
= msg
.GetCommandInt();
3188 for ( i
= 0; i
< numCols
; i
++ )
3190 m_colWidths
.Add( m_defaultColWidth
);
3191 m_colRights
.Add( 0 );
3194 int oldNumCols
= m_numCols
;
3195 m_numCols
+= numCols
;
3198 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3200 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3202 right
+= m_colWidths
[i
];
3203 m_colRights
[i
] = right
;
3209 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3211 size_t pos
= msg
.GetCommandInt();
3212 int numCols
= msg
.GetCommandInt2();
3213 for ( i
= 0; i
< numCols
; i
++ )
3215 m_colWidths
.Remove( pos
);
3216 m_colRights
.Remove( pos
);
3218 m_numCols
-= numCols
;
3222 #if 0 // leave the row alone here so that AppendCols will work subsequently
3224 m_rowHeights
.Clear();
3225 m_rowBottoms
.Clear();
3227 m_currentCellCoords
= wxGridNoCellCoords
;
3231 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3232 m_currentCellCoords
.Set( 0, 0 );
3235 for ( i
= 0; i
< m_numCols
; i
++ )
3237 w
+= m_colWidths
[i
];
3250 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3252 wxRegionIterator
iter( reg
);
3255 m_rowLabelsExposed
.Empty();
3262 // TODO: remove this when we can...
3263 // There is a bug in wxMotif that gives garbage update
3264 // rectangles if you jump-scroll a long way by clicking the
3265 // scrollbar with middle button. This is a work-around
3267 #if defined(__WXMOTIF__)
3269 m_gridWin
->GetClientSize( &cw
, &ch
);
3270 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3271 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3274 // logical bounds of update region
3277 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3278 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3280 // find the row labels within these bounds
3283 for ( row
= 0; row
< m_numRows
; row
++ )
3285 if ( GetRowBottom(row
) < top
)
3288 if ( GetRowTop(row
) > bottom
)
3291 m_rowLabelsExposed
.Add( row
);
3299 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3301 wxRegionIterator
iter( reg
);
3304 m_colLabelsExposed
.Empty();
3311 // TODO: remove this when we can...
3312 // There is a bug in wxMotif that gives garbage update
3313 // rectangles if you jump-scroll a long way by clicking the
3314 // scrollbar with middle button. This is a work-around
3316 #if defined(__WXMOTIF__)
3318 m_gridWin
->GetClientSize( &cw
, &ch
);
3319 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3320 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3323 // logical bounds of update region
3326 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3327 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3329 // find the cells within these bounds
3332 for ( col
= 0; col
< m_numCols
; col
++ )
3334 if ( GetColRight(col
) < left
)
3337 if ( GetColLeft(col
) > right
)
3340 m_colLabelsExposed
.Add( col
);
3348 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3350 wxRegionIterator
iter( reg
);
3353 m_cellsExposed
.Empty();
3354 m_rowsExposed
.Empty();
3355 m_colsExposed
.Empty();
3357 int left
, top
, right
, bottom
;
3362 // TODO: remove this when we can...
3363 // There is a bug in wxMotif that gives garbage update
3364 // rectangles if you jump-scroll a long way by clicking the
3365 // scrollbar with middle button. This is a work-around
3367 #if defined(__WXMOTIF__)
3369 m_gridWin
->GetClientSize( &cw
, &ch
);
3370 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3371 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3372 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3373 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3376 // logical bounds of update region
3378 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3379 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3381 // find the cells within these bounds
3384 for ( row
= 0; row
< m_numRows
; row
++ )
3386 if ( GetRowBottom(row
) <= top
)
3389 if ( GetRowTop(row
) > bottom
)
3392 m_rowsExposed
.Add( row
);
3394 for ( col
= 0; col
< m_numCols
; col
++ )
3396 if ( GetColRight(col
) <= left
)
3399 if ( GetColLeft(col
) > right
)
3402 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3403 m_colsExposed
.Add( col
);
3404 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3413 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3416 wxPoint
pos( event
.GetPosition() );
3417 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3419 if ( event
.Dragging() )
3421 m_isDragging
= TRUE
;
3423 if ( event
.LeftIsDown() )
3425 switch( m_cursorMode
)
3427 case WXGRID_CURSOR_RESIZE_ROW
:
3429 int cw
, ch
, left
, dummy
;
3430 m_gridWin
->GetClientSize( &cw
, &ch
);
3431 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3433 wxClientDC
dc( m_gridWin
);
3435 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3436 dc
.SetLogicalFunction(wxINVERT
);
3437 if ( m_dragLastPos
>= 0 )
3439 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3441 dc
.DrawLine( left
, y
, left
+cw
, y
);
3446 case WXGRID_CURSOR_SELECT_ROW
:
3447 if ( (row
= YToRow( y
)) >= 0 &&
3448 !IsInSelection( row
, 0 ) )
3450 SelectRow( row
, TRUE
);
3453 // default label to suppress warnings about "enumeration value
3454 // 'xxx' not handled in switch
3462 m_isDragging
= FALSE
;
3465 // ------------ Entering or leaving the window
3467 if ( event
.Entering() || event
.Leaving() )
3469 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3473 // ------------ Left button pressed
3475 else if ( event
.LeftDown() )
3477 // don't send a label click event for a hit on the
3478 // edge of the row label - this is probably the user
3479 // wanting to resize the row
3481 if ( YToEdgeOfRow(y
) < 0 )
3485 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3487 SelectRow( row
, event
.ShiftDown() );
3488 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3493 // starting to drag-resize a row
3495 if ( CanDragRowSize() )
3496 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3501 // ------------ Left double click
3503 else if (event
.LeftDClick() )
3505 if ( YToEdgeOfRow(y
) < 0 )
3508 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3513 // ------------ Left button released
3515 else if ( event
.LeftUp() )
3517 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3519 DoEndDragResizeRow();
3521 // Note: we are ending the event *after* doing
3522 // default processing in this case
3524 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3527 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3532 // ------------ Right button down
3534 else if ( event
.RightDown() )
3537 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3539 // no default action at the moment
3544 // ------------ Right double click
3546 else if ( event
.RightDClick() )
3549 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3551 // no default action at the moment
3556 // ------------ No buttons down and mouse moving
3558 else if ( event
.Moving() )
3560 m_dragRowOrCol
= YToEdgeOfRow( y
);
3561 if ( m_dragRowOrCol
>= 0 )
3563 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3565 // don't capture the mouse yet
3566 if ( CanDragRowSize() )
3567 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3570 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3572 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3578 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3581 wxPoint
pos( event
.GetPosition() );
3582 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3584 if ( event
.Dragging() )
3586 m_isDragging
= TRUE
;
3588 if ( event
.LeftIsDown() )
3590 switch( m_cursorMode
)
3592 case WXGRID_CURSOR_RESIZE_COL
:
3594 int cw
, ch
, dummy
, top
;
3595 m_gridWin
->GetClientSize( &cw
, &ch
);
3596 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3598 wxClientDC
dc( m_gridWin
);
3601 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3602 GetColMinimalWidth(m_dragRowOrCol
));
3603 dc
.SetLogicalFunction(wxINVERT
);
3604 if ( m_dragLastPos
>= 0 )
3606 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3608 dc
.DrawLine( x
, top
, x
, top
+ch
);
3613 case WXGRID_CURSOR_SELECT_COL
:
3614 if ( (col
= XToCol( x
)) >= 0 &&
3615 !IsInSelection( 0, col
) )
3617 SelectCol( col
, TRUE
);
3620 // default label to suppress warnings about "enumeration value
3621 // 'xxx' not handled in switch
3629 m_isDragging
= FALSE
;
3632 // ------------ Entering or leaving the window
3634 if ( event
.Entering() || event
.Leaving() )
3636 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3640 // ------------ Left button pressed
3642 else if ( event
.LeftDown() )
3644 // don't send a label click event for a hit on the
3645 // edge of the col label - this is probably the user
3646 // wanting to resize the col
3648 if ( XToEdgeOfCol(x
) < 0 )
3652 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3654 SelectCol( col
, event
.ShiftDown() );
3655 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3660 // starting to drag-resize a col
3662 if ( CanDragColSize() )
3663 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3668 // ------------ Left double click
3670 if ( event
.LeftDClick() )
3672 if ( XToEdgeOfCol(x
) < 0 )
3675 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3680 // ------------ Left button released
3682 else if ( event
.LeftUp() )
3684 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3686 DoEndDragResizeCol();
3688 // Note: we are ending the event *after* doing
3689 // default processing in this case
3691 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3694 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3699 // ------------ Right button down
3701 else if ( event
.RightDown() )
3704 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3706 // no default action at the moment
3711 // ------------ Right double click
3713 else if ( event
.RightDClick() )
3716 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3718 // no default action at the moment
3723 // ------------ No buttons down and mouse moving
3725 else if ( event
.Moving() )
3727 m_dragRowOrCol
= XToEdgeOfCol( x
);
3728 if ( m_dragRowOrCol
>= 0 )
3730 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3732 // don't capture the cursor yet
3733 if ( CanDragColSize() )
3734 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3737 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3739 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3745 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3747 if ( event
.LeftDown() )
3749 // indicate corner label by having both row and
3752 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3758 else if ( event
.LeftDClick() )
3760 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3763 else if ( event
.RightDown() )
3765 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3767 // no default action at the moment
3771 else if ( event
.RightDClick() )
3773 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3775 // no default action at the moment
3780 void wxGrid::ChangeCursorMode(CursorMode mode
,
3785 static const wxChar
*cursorModes
[] =
3794 wxLogTrace(_T("grid"),
3795 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3796 win
== m_colLabelWin
? _T("colLabelWin")
3797 : win
? _T("rowLabelWin")
3799 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3800 #endif // __WXDEBUG__
3802 if ( mode
== m_cursorMode
)
3807 // by default use the grid itself
3813 m_winCapture
->ReleaseMouse();
3814 m_winCapture
= (wxWindow
*)NULL
;
3817 m_cursorMode
= mode
;
3819 switch ( m_cursorMode
)
3821 case WXGRID_CURSOR_RESIZE_ROW
:
3822 win
->SetCursor( m_rowResizeCursor
);
3825 case WXGRID_CURSOR_RESIZE_COL
:
3826 win
->SetCursor( m_colResizeCursor
);
3830 win
->SetCursor( *wxSTANDARD_CURSOR
);
3833 // we need to capture mouse when resizing
3834 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3835 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3837 if ( captureMouse
&& resize
)
3839 win
->CaptureMouse();
3844 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3847 wxPoint
pos( event
.GetPosition() );
3848 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3850 wxGridCellCoords coords
;
3851 XYToCell( x
, y
, coords
);
3853 if ( event
.Dragging() )
3855 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3857 // Don't start doing anything until the mouse has been drug at
3858 // least 3 pixels in any direction...
3861 if (m_startDragPos
== wxDefaultPosition
)
3863 m_startDragPos
= pos
;
3866 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3870 m_isDragging
= TRUE
;
3871 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3873 // Hide the edit control, so it
3874 // won't interfer with drag-shrinking.
3875 if ( IsCellEditControlEnabled() )
3877 HideCellEditControl();
3878 SaveEditControlValue();
3881 // Have we captured the mouse yet?
3884 m_winCapture
= m_gridWin
;
3885 m_winCapture
->CaptureMouse();
3888 if ( coords
!= wxGridNoCellCoords
)
3890 if ( !IsSelection() )
3892 SelectBlock( coords
, coords
);
3896 SelectBlock( m_currentCellCoords
, coords
);
3899 if (! IsVisible(coords
))
3901 MakeCellVisible(coords
);
3902 // TODO: need to introduce a delay or something here. The
3903 // scrolling is way to fast, at least on MSW.
3907 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3909 int cw
, ch
, left
, dummy
;
3910 m_gridWin
->GetClientSize( &cw
, &ch
);
3911 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3913 wxClientDC
dc( m_gridWin
);
3915 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3916 dc
.SetLogicalFunction(wxINVERT
);
3917 if ( m_dragLastPos
>= 0 )
3919 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3921 dc
.DrawLine( left
, y
, left
+cw
, y
);
3924 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3926 int cw
, ch
, dummy
, top
;
3927 m_gridWin
->GetClientSize( &cw
, &ch
);
3928 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3930 wxClientDC
dc( m_gridWin
);
3932 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3933 GetColMinimalWidth(m_dragRowOrCol
) );
3934 dc
.SetLogicalFunction(wxINVERT
);
3935 if ( m_dragLastPos
>= 0 )
3937 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3939 dc
.DrawLine( x
, top
, x
, top
+ch
);
3946 m_isDragging
= FALSE
;
3947 m_startDragPos
= wxDefaultPosition
;
3949 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3950 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3953 if ( event
.Entering() || event
.Leaving() )
3955 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3956 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3961 // ------------ Left button pressed
3963 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
3965 if ( event
.ShiftDown() )
3967 SelectBlock( m_currentCellCoords
, coords
);
3969 else if ( XToEdgeOfCol(x
) < 0 &&
3970 YToEdgeOfRow(y
) < 0 )
3972 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3977 DisableCellEditControl();
3978 MakeCellVisible( coords
);
3980 // if this is the second click on this cell then start
3982 if ( m_waitForSlowClick
&&
3983 (coords
== m_currentCellCoords
) &&
3984 CanEnableCellControl())
3986 EnableCellEditControl();
3988 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3989 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
3992 m_waitForSlowClick
= FALSE
;
3996 SetCurrentCell( coords
);
3997 m_waitForSlowClick
= TRUE
;
4004 // ------------ Left double click
4006 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4008 DisableCellEditControl();
4010 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4012 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4020 // ------------ Left button released
4022 else if ( event
.LeftUp() )
4024 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4026 if ( IsSelection() )
4030 m_winCapture
->ReleaseMouse();
4031 m_winCapture
= NULL
;
4033 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4036 // Show the edit control, if it has been hidden for
4038 ShowCellEditControl();
4040 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4042 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4043 DoEndDragResizeRow();
4045 // Note: we are ending the event *after* doing
4046 // default processing in this case
4048 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4050 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4052 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4053 DoEndDragResizeCol();
4055 // Note: we are ending the event *after* doing
4056 // default processing in this case
4058 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4065 // ------------ Right button down
4067 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4069 DisableCellEditControl();
4070 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4075 // no default action at the moment
4080 // ------------ Right double click
4082 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4084 DisableCellEditControl();
4085 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4090 // no default action at the moment
4094 // ------------ Moving and no button action
4096 else if ( event
.Moving() && !event
.IsButton() )
4098 int dragRow
= YToEdgeOfRow( y
);
4099 int dragCol
= XToEdgeOfCol( x
);
4101 // Dragging on the corner of a cell to resize in both
4102 // directions is not implemented yet...
4104 if ( dragRow
>= 0 && dragCol
>= 0 )
4106 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4112 m_dragRowOrCol
= dragRow
;
4114 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4116 if ( CanDragRowSize() && CanDragGridSize() )
4117 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4122 m_dragRowOrCol
= dragCol
;
4129 m_dragRowOrCol
= dragCol
;
4131 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4133 if ( CanDragColSize() && CanDragGridSize() )
4134 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4140 // Neither on a row or col edge
4142 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4144 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4150 void wxGrid::DoEndDragResizeRow()
4152 if ( m_dragLastPos
>= 0 )
4154 // erase the last line and resize the row
4156 int cw
, ch
, left
, dummy
;
4157 m_gridWin
->GetClientSize( &cw
, &ch
);
4158 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4160 wxClientDC
dc( m_gridWin
);
4162 dc
.SetLogicalFunction( wxINVERT
);
4163 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4164 HideCellEditControl();
4165 SaveEditControlValue();
4167 int rowTop
= GetRowTop(m_dragRowOrCol
);
4168 SetRowSize( m_dragRowOrCol
,
4169 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4171 if ( !GetBatchCount() )
4173 // Only needed to get the correct rect.y:
4174 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4176 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4177 rect
.width
= m_rowLabelWidth
;
4178 rect
.height
= ch
- rect
.y
;
4179 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4181 m_gridWin
->Refresh( FALSE
, &rect
);
4184 ShowCellEditControl();
4189 void wxGrid::DoEndDragResizeCol()
4191 if ( m_dragLastPos
>= 0 )
4193 // erase the last line and resize the col
4195 int cw
, ch
, dummy
, top
;
4196 m_gridWin
->GetClientSize( &cw
, &ch
);
4197 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4199 wxClientDC
dc( m_gridWin
);
4201 dc
.SetLogicalFunction( wxINVERT
);
4202 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4203 HideCellEditControl();
4204 SaveEditControlValue();
4206 int colLeft
= GetColLeft(m_dragRowOrCol
);
4207 SetColSize( m_dragRowOrCol
,
4208 wxMax( m_dragLastPos
- colLeft
,
4209 GetColMinimalWidth(m_dragRowOrCol
) ) );
4211 if ( !GetBatchCount() )
4213 // Only needed to get the correct rect.x:
4214 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4216 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4217 rect
.width
= cw
- rect
.x
;
4218 rect
.height
= m_colLabelHeight
;
4219 m_colLabelWin
->Refresh( TRUE
, &rect
);
4221 m_gridWin
->Refresh( FALSE
, &rect
);
4224 ShowCellEditControl();
4231 // ------ interaction with data model
4233 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4235 switch ( msg
.GetId() )
4237 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4238 return GetModelValues();
4240 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4241 return SetModelValues();
4243 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4244 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4245 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4246 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4247 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4248 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4249 return Redimension( msg
);
4258 // The behaviour of this function depends on the grid table class
4259 // Clear() function. For the default wxGridStringTable class the
4260 // behavious is to replace all cell contents with wxEmptyString but
4261 // not to change the number of rows or cols.
4263 void wxGrid::ClearGrid()
4267 if (IsCellEditControlEnabled())
4268 DisableCellEditControl();
4271 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4276 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4278 // TODO: something with updateLabels flag
4282 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4288 if (IsCellEditControlEnabled())
4289 DisableCellEditControl();
4291 bool ok
= m_table
->InsertRows( pos
, numRows
);
4293 // the table will have sent the results of the insert row
4294 // operation to this view object as a grid table message
4298 if ( m_numCols
== 0 )
4300 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4302 // TODO: perhaps instead of appending the default number of cols
4303 // we should remember what the last non-zero number of cols was ?
4307 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4309 // if we have just inserted cols into an empty grid the current
4310 // cell will be undefined...
4312 SetCurrentCell( 0, 0 );
4316 if ( !GetBatchCount() ) Refresh();
4328 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4330 // TODO: something with updateLabels flag
4334 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4338 if ( m_table
&& m_table
->AppendRows( numRows
) )
4340 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4342 // if we have just inserted cols into an empty grid the current
4343 // cell will be undefined...
4345 SetCurrentCell( 0, 0 );
4348 // the table will have sent the results of the append row
4349 // operation to this view object as a grid table message
4352 if ( !GetBatchCount() ) Refresh();
4362 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4364 // TODO: something with updateLabels flag
4368 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4374 if (IsCellEditControlEnabled())
4375 DisableCellEditControl();
4377 if (m_table
->DeleteRows( pos
, numRows
))
4380 // the table will have sent the results of the delete row
4381 // operation to this view object as a grid table message
4384 if ( !GetBatchCount() ) Refresh();
4392 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4394 // TODO: something with updateLabels flag
4398 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4404 if (IsCellEditControlEnabled())
4405 DisableCellEditControl();
4407 bool ok
= m_table
->InsertCols( pos
, numCols
);
4409 // the table will have sent the results of the insert col
4410 // operation to this view object as a grid table message
4414 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4416 // if we have just inserted cols into an empty grid the current
4417 // cell will be undefined...
4419 SetCurrentCell( 0, 0 );
4423 if ( !GetBatchCount() ) Refresh();
4435 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4437 // TODO: something with updateLabels flag
4441 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4445 if ( m_table
&& m_table
->AppendCols( numCols
) )
4447 // the table will have sent the results of the append col
4448 // operation to this view object as a grid table message
4450 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4452 // if we have just inserted cols into an empty grid the current
4453 // cell will be undefined...
4455 SetCurrentCell( 0, 0 );
4459 if ( !GetBatchCount() ) Refresh();
4469 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4471 // TODO: something with updateLabels flag
4475 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4481 if (IsCellEditControlEnabled())
4482 DisableCellEditControl();
4484 if ( m_table
->DeleteCols( pos
, numCols
) )
4486 // the table will have sent the results of the delete col
4487 // operation to this view object as a grid table message
4490 if ( !GetBatchCount() ) Refresh();
4500 // ----- event handlers
4503 // Generate a grid event based on a mouse event and
4504 // return the result of ProcessEvent()
4506 bool wxGrid::SendEvent( const wxEventType type
,
4508 wxMouseEvent
& mouseEv
)
4510 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4512 int rowOrCol
= (row
== -1 ? col
: row
);
4514 wxGridSizeEvent
gridEvt( GetId(),
4518 mouseEv
.GetX(), mouseEv
.GetY(),
4519 mouseEv
.ControlDown(),
4520 mouseEv
.ShiftDown(),
4522 mouseEv
.MetaDown() );
4524 return GetEventHandler()->ProcessEvent(gridEvt
);
4526 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4528 wxGridRangeSelectEvent
gridEvt( GetId(),
4532 m_selectedBottomRight
,
4533 mouseEv
.ControlDown(),
4534 mouseEv
.ShiftDown(),
4536 mouseEv
.MetaDown() );
4538 return GetEventHandler()->ProcessEvent(gridEvt
);
4542 wxGridEvent
gridEvt( GetId(),
4546 mouseEv
.GetX(), mouseEv
.GetY(),
4547 mouseEv
.ControlDown(),
4548 mouseEv
.ShiftDown(),
4550 mouseEv
.MetaDown() );
4552 return GetEventHandler()->ProcessEvent(gridEvt
);
4557 // Generate a grid event of specified type and return the result
4558 // of ProcessEvent().
4560 bool wxGrid::SendEvent( const wxEventType type
,
4563 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4565 int rowOrCol
= (row
== -1 ? col
: row
);
4567 wxGridSizeEvent
gridEvt( GetId(),
4572 return GetEventHandler()->ProcessEvent(gridEvt
);
4576 wxGridEvent
gridEvt( GetId(),
4581 return GetEventHandler()->ProcessEvent(gridEvt
);
4586 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4588 wxPaintDC
dc( this );
4590 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4591 m_numRows
&& m_numCols
)
4593 m_currentCellCoords
.Set(0, 0);
4594 ShowCellEditControl();
4601 // This is just here to make sure that CalcDimensions gets called when
4602 // the grid view is resized... then the size event is skipped to allow
4603 // the box sizers to handle everything
4605 void wxGrid::OnSize( wxSizeEvent
& event
)
4612 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4614 if ( m_inOnKeyDown
)
4616 // shouldn't be here - we are going round in circles...
4618 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4621 m_inOnKeyDown
= TRUE
;
4623 // propagate the event up and see if it gets processed
4625 wxWindow
*parent
= GetParent();
4626 wxKeyEvent
keyEvt( event
);
4627 keyEvt
.SetEventObject( parent
);
4629 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4632 // TODO: Should also support Shift-cursor keys for
4633 // extending the selection. Maybe add a flag to
4634 // MoveCursorXXX() and MoveCursorXXXBlock() and
4635 // just send event.ShiftDown().
4637 // try local handlers
4639 switch ( event
.KeyCode() )
4642 if ( event
.ControlDown() )
4644 MoveCursorUpBlock();
4653 if ( event
.ControlDown() )
4655 MoveCursorDownBlock();
4664 if ( event
.ControlDown() )
4666 MoveCursorLeftBlock();
4675 if ( event
.ControlDown() )
4677 MoveCursorRightBlock();
4686 if ( event
.ControlDown() )
4688 event
.Skip(); // to let the edit control have the return
4697 if (event
.ShiftDown())
4704 if ( event
.ControlDown() )
4706 MakeCellVisible( 0, 0 );
4707 SetCurrentCell( 0, 0 );
4716 if ( event
.ControlDown() )
4718 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4719 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4736 if ( !IsEditable() )
4741 // Otherwise fall through to default
4744 // alphanumeric keys enable the cell edit control
4745 if ( !(event
.AltDown() ||
4747 event
.ControlDown()) &&
4748 isalnum(event
.KeyCode()) &&
4749 !IsCellEditControlEnabled() &&
4750 CanEnableCellControl() )
4752 EnableCellEditControl();
4753 int row
= m_currentCellCoords
.GetRow();
4754 int col
= m_currentCellCoords
.GetCol();
4755 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4756 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4761 // let others process char events with modifiers or all
4762 // char events for readonly cells
4769 m_inOnKeyDown
= FALSE
;
4773 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4777 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4779 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4781 // the event has been intercepted - do nothing
4786 m_currentCellCoords
!= wxGridNoCellCoords
)
4788 HideCellEditControl();
4789 DisableCellEditControl();
4791 // Clear the old current cell highlight
4792 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4794 // Otherwise refresh redraws the highlight!
4795 m_currentCellCoords
= coords
;
4797 m_gridWin
->Refresh( FALSE
, &r
);
4800 m_currentCellCoords
= coords
;
4804 wxClientDC
dc(m_gridWin
);
4807 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4808 DrawCellHighlight(dc
, attr
);
4811 if ( IsSelection() )
4813 wxRect
r( SelectionToDeviceRect() );
4815 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4822 // ------ functions to get/send data (see also public functions)
4825 bool wxGrid::GetModelValues()
4829 // all we need to do is repaint the grid
4831 m_gridWin
->Refresh();
4839 bool wxGrid::SetModelValues()
4845 for ( row
= 0; row
< m_numRows
; row
++ )
4847 for ( col
= 0; col
< m_numCols
; col
++ )
4849 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4861 // Note - this function only draws cells that are in the list of
4862 // exposed cells (usually set from the update region by
4863 // CalcExposedCells)
4865 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4867 if ( !m_numRows
|| !m_numCols
) return;
4870 size_t numCells
= m_cellsExposed
.GetCount();
4872 for ( i
= 0; i
< numCells
; i
++ )
4874 DrawCell( dc
, m_cellsExposed
[i
] );
4879 void wxGrid::DrawGridSpace( wxDC
& dc
)
4881 if ( m_numRows
&& m_numCols
)
4884 m_gridWin
->GetClientSize( &cw
, &ch
);
4887 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4889 if ( right
> GetColRight(m_numCols
-1) ||
4890 bottom
> GetRowBottom(m_numRows
-1) )
4893 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4895 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4896 dc
.SetPen( *wxTRANSPARENT_PEN
);
4898 if ( right
> GetColRight(m_numCols
-1) )
4899 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
4900 right
- GetColRight(m_numCols
-1), ch
);
4902 if ( bottom
> GetRowBottom(m_numRows
-1) )
4903 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
4904 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4910 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4912 int row
= coords
.GetRow();
4913 int col
= coords
.GetCol();
4915 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4918 // we draw the cell border ourselves
4919 #if !WXGRID_DRAW_LINES
4920 if ( m_gridLinesEnabled
)
4921 DrawCellBorder( dc
, coords
);
4924 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4926 bool isCurrent
= coords
== m_currentCellCoords
;
4929 rect
.x
= GetColLeft(col
);
4930 rect
.y
= GetRowTop(row
);
4931 rect
.width
= GetColWidth(col
) - 1;
4932 rect
.height
= GetRowHeight(row
) - 1;
4934 // if the editor is shown, we should use it and not the renderer
4935 if ( isCurrent
&& IsCellEditControlEnabled() )
4937 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4941 // but all the rest is drawn by the cell renderer and hence may be
4943 attr
->GetRenderer(this, row
, col
)->
4944 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4951 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4953 int row
= m_currentCellCoords
.GetRow();
4954 int col
= m_currentCellCoords
.GetCol();
4956 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4960 rect
.x
= GetColLeft(col
);
4961 rect
.y
= GetRowTop(row
);
4962 rect
.width
= GetColWidth(col
) - 1;
4963 rect
.height
= GetRowHeight(row
) - 1;
4965 // hmmm... what could we do here to show that the cell is disabled?
4966 // for now, I just draw a thinner border than for the other ones, but
4967 // it doesn't look really good
4968 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4969 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4971 dc
.DrawRectangle(rect
);
4974 // VZ: my experiments with 3d borders...
4976 // how to properly set colours for arbitrary bg?
4977 wxCoord x1
= rect
.x
,
4979 x2
= rect
.x
+ rect
.width
-1,
4980 y2
= rect
.y
+ rect
.height
-1;
4982 dc
.SetPen(*wxWHITE_PEN
);
4983 dc
.DrawLine(x1
, y1
, x2
, y1
);
4984 dc
.DrawLine(x1
, y1
, x1
, y2
);
4986 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4987 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4989 dc
.SetPen(*wxBLACK_PEN
);
4990 dc
.DrawLine(x1
, y2
, x2
, y2
);
4991 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4996 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4998 int row
= coords
.GetRow();
4999 int col
= coords
.GetCol();
5000 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5003 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5005 // right hand border
5007 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5008 GetColRight(col
), GetRowBottom(row
) );
5012 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5013 GetColRight(col
), GetRowBottom(row
) );
5016 void wxGrid::DrawHighlight(wxDC
& dc
)
5018 if ( IsCellEditControlEnabled() )
5020 // don't show highlight when the edit control is shown
5024 // if the active cell was repainted, repaint its highlight too because it
5025 // might have been damaged by the grid lines
5026 size_t count
= m_cellsExposed
.GetCount();
5027 for ( size_t n
= 0; n
< count
; n
++ )
5029 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5031 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5032 DrawCellHighlight(dc
, attr
);
5040 // TODO: remove this ???
5041 // This is used to redraw all grid lines e.g. when the grid line colour
5044 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5046 if ( !m_gridLinesEnabled
||
5048 !m_numCols
) return;
5050 int top
, bottom
, left
, right
;
5056 m_gridWin
->GetClientSize(&cw
, &ch
);
5058 // virtual coords of visible area
5060 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5061 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5066 reg
.GetBox(x
, y
, w
, h
);
5067 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5068 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5072 m_gridWin
->GetClientSize(&cw
, &ch
);
5073 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5074 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5077 // avoid drawing grid lines past the last row and col
5079 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5080 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5082 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5084 // horizontal grid lines
5087 for ( i
= 0; i
< m_numRows
; i
++ )
5089 int bot
= GetRowBottom(i
) - 1;
5098 dc
.DrawLine( left
, bot
, right
, bot
);
5103 // vertical grid lines
5105 for ( i
= 0; i
< m_numCols
; i
++ )
5107 int colRight
= GetColRight(i
) - 1;
5108 if ( colRight
> right
)
5113 if ( colRight
>= left
)
5115 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5121 void wxGrid::DrawRowLabels( wxDC
& dc
)
5123 if ( !m_numRows
|| !m_numCols
) return;
5126 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5128 for ( i
= 0; i
< numLabels
; i
++ )
5130 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5135 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5137 if ( GetRowHeight(row
) <= 0 )
5140 int rowTop
= GetRowTop(row
),
5141 rowBottom
= GetRowBottom(row
) - 1;
5143 dc
.SetPen( *wxBLACK_PEN
);
5144 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5145 m_rowLabelWidth
-1, rowBottom
);
5147 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5149 dc
.SetPen( *wxWHITE_PEN
);
5150 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5151 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5153 dc
.SetBackgroundMode( wxTRANSPARENT
);
5154 dc
.SetTextForeground( GetLabelTextColour() );
5155 dc
.SetFont( GetLabelFont() );
5158 GetRowLabelAlignment( &hAlign
, &vAlign
);
5162 rect
.SetY( GetRowTop(row
) + 2 );
5163 rect
.SetWidth( m_rowLabelWidth
- 4 );
5164 rect
.SetHeight( GetRowHeight(row
) - 4 );
5165 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5169 void wxGrid::DrawColLabels( wxDC
& dc
)
5171 if ( !m_numRows
|| !m_numCols
) return;
5174 size_t numLabels
= m_colLabelsExposed
.GetCount();
5176 for ( i
= 0; i
< numLabels
; i
++ )
5178 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5183 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5185 if ( GetColWidth(col
) <= 0 )
5188 int colLeft
= GetColLeft(col
),
5189 colRight
= GetColRight(col
) - 1;
5191 dc
.SetPen( *wxBLACK_PEN
);
5192 dc
.DrawLine( colRight
, 0,
5193 colRight
, m_colLabelHeight
-1 );
5195 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5196 colRight
, m_colLabelHeight
-1 );
5198 dc
.SetPen( *wxWHITE_PEN
);
5199 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5200 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5202 dc
.SetBackgroundMode( wxTRANSPARENT
);
5203 dc
.SetTextForeground( GetLabelTextColour() );
5204 dc
.SetFont( GetLabelFont() );
5206 dc
.SetBackgroundMode( wxTRANSPARENT
);
5207 dc
.SetTextForeground( GetLabelTextColour() );
5208 dc
.SetFont( GetLabelFont() );
5211 GetColLabelAlignment( &hAlign
, &vAlign
);
5214 rect
.SetX( colLeft
+ 2 );
5216 rect
.SetWidth( GetColWidth(col
) - 4 );
5217 rect
.SetHeight( m_colLabelHeight
- 4 );
5218 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5222 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5223 const wxString
& value
,
5228 long textWidth
, textHeight
;
5229 long lineWidth
, lineHeight
;
5230 wxArrayString lines
;
5232 dc
.SetClippingRegion( rect
);
5233 StringToLines( value
, lines
);
5234 if ( lines
.GetCount() )
5236 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5237 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5240 switch ( horizAlign
)
5243 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5247 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5256 switch ( vertAlign
)
5259 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5263 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5272 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5274 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5279 dc
.DestroyClippingRegion();
5283 // Split multi line text up into an array of strings. Any existing
5284 // contents of the string array are preserved.
5286 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5290 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5291 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5293 while ( startPos
< (int)tVal
.Length() )
5295 pos
= tVal
.Mid(startPos
).Find( eol
);
5300 else if ( pos
== 0 )
5302 lines
.Add( wxEmptyString
);
5306 lines
.Add( value
.Mid(startPos
, pos
) );
5310 if ( startPos
< (int)value
.Length() )
5312 lines
.Add( value
.Mid( startPos
) );
5317 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5318 wxArrayString
& lines
,
5319 long *width
, long *height
)
5326 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5328 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5329 w
= wxMax( w
, lineW
);
5339 // ------ Edit control functions
5343 void wxGrid::EnableEditing( bool edit
)
5345 // TODO: improve this ?
5347 if ( edit
!= m_editable
)
5351 // FIXME IMHO this won't disable the edit control if edit == FALSE
5352 // because of the check in the beginning of
5353 // EnableCellEditControl() just below (VZ)
5354 EnableCellEditControl(m_editable
);
5359 void wxGrid::EnableCellEditControl( bool enable
)
5364 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5365 SetCurrentCell( 0, 0 );
5367 if ( enable
!= m_cellEditCtrlEnabled
)
5369 // TODO allow the app to Veto() this event?
5370 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5374 // this should be checked by the caller!
5375 wxASSERT_MSG( CanEnableCellControl(),
5376 _T("can't enable editing for this cell!") );
5378 // do it before ShowCellEditControl()
5379 m_cellEditCtrlEnabled
= enable
;
5381 ShowCellEditControl();
5385 HideCellEditControl();
5386 SaveEditControlValue();
5388 // do it after HideCellEditControl()
5389 m_cellEditCtrlEnabled
= enable
;
5394 bool wxGrid::IsCurrentCellReadOnly() const
5397 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5398 bool readonly
= attr
->IsReadOnly();
5404 bool wxGrid::CanEnableCellControl() const
5406 return m_editable
&& !IsCurrentCellReadOnly();
5409 bool wxGrid::IsCellEditControlEnabled() const
5411 // the cell edit control might be disable for all cells or just for the
5412 // current one if it's read only
5413 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5416 void wxGrid::ShowCellEditControl()
5418 if ( IsCellEditControlEnabled() )
5420 if ( !IsVisible( m_currentCellCoords
) )
5426 wxRect rect
= CellToRect( m_currentCellCoords
);
5427 int row
= m_currentCellCoords
.GetRow();
5428 int col
= m_currentCellCoords
.GetCol();
5430 // convert to scrolled coords
5432 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5434 // done in PaintBackground()
5436 // erase the highlight and the cell contents because the editor
5437 // might not cover the entire cell
5438 wxClientDC
dc( m_gridWin
);
5440 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5441 dc
.SetPen(*wxTRANSPARENT_PEN
);
5442 dc
.DrawRectangle(rect
);
5445 // cell is shifted by one pixel
5449 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5450 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5451 if ( !editor
->IsCreated() )
5453 editor
->Create(m_gridWin
, -1,
5454 new wxGridCellEditorEvtHandler(this, editor
));
5457 editor
->SetSize( rect
);
5459 editor
->Show( TRUE
, attr
);
5460 editor
->BeginEdit(row
, col
, this);
5467 void wxGrid::HideCellEditControl()
5469 if ( IsCellEditControlEnabled() )
5471 int row
= m_currentCellCoords
.GetRow();
5472 int col
= m_currentCellCoords
.GetCol();
5474 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5475 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5477 m_gridWin
->SetFocus();
5482 void wxGrid::SaveEditControlValue()
5484 if ( IsCellEditControlEnabled() )
5486 int row
= m_currentCellCoords
.GetRow();
5487 int col
= m_currentCellCoords
.GetCol();
5489 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5490 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5491 bool changed
= editor
->EndEdit(row
, col
, this);
5497 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5498 m_currentCellCoords
.GetRow(),
5499 m_currentCellCoords
.GetCol() );
5506 // ------ Grid location functions
5507 // Note that all of these functions work with the logical coordinates of
5508 // grid cells and labels so you will need to convert from device
5509 // coordinates for mouse events etc.
5512 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5514 int row
= YToRow(y
);
5515 int col
= XToCol(x
);
5517 if ( row
== -1 || col
== -1 )
5519 coords
= wxGridNoCellCoords
;
5523 coords
.Set( row
, col
);
5528 int wxGrid::YToRow( int y
)
5532 for ( i
= 0; i
< m_numRows
; i
++ )
5534 if ( y
< GetRowBottom(i
) )
5542 int wxGrid::XToCol( int x
)
5546 for ( i
= 0; i
< m_numCols
; i
++ )
5548 if ( x
< GetColRight(i
) )
5556 // return the row number that that the y coord is near the edge of, or
5557 // -1 if not near an edge
5559 int wxGrid::YToEdgeOfRow( int y
)
5563 for ( i
= 0; i
< m_numRows
; i
++ )
5565 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5567 d
= abs( y
- GetRowBottom(i
) );
5568 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5577 // return the col number that that the x coord is near the edge of, or
5578 // -1 if not near an edge
5580 int wxGrid::XToEdgeOfCol( int x
)
5584 for ( i
= 0; i
< m_numCols
; i
++ )
5586 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5588 d
= abs( x
- GetColRight(i
) );
5589 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5598 wxRect
wxGrid::CellToRect( int row
, int col
)
5600 wxRect
rect( -1, -1, -1, -1 );
5602 if ( row
>= 0 && row
< m_numRows
&&
5603 col
>= 0 && col
< m_numCols
)
5605 rect
.x
= GetColLeft(col
);
5606 rect
.y
= GetRowTop(row
);
5607 rect
.width
= GetColWidth(col
);
5608 rect
.height
= GetRowHeight(row
);
5615 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5617 // get the cell rectangle in logical coords
5619 wxRect
r( CellToRect( row
, col
) );
5621 // convert to device coords
5623 int left
, top
, right
, bottom
;
5624 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5625 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5627 // check against the client area of the grid window
5630 m_gridWin
->GetClientSize( &cw
, &ch
);
5632 if ( wholeCellVisible
)
5634 // is the cell wholly visible ?
5636 return ( left
>= 0 && right
<= cw
&&
5637 top
>= 0 && bottom
<= ch
);
5641 // is the cell partly visible ?
5643 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5644 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5649 // make the specified cell location visible by doing a minimal amount
5652 void wxGrid::MakeCellVisible( int row
, int col
)
5655 int xpos
= -1, ypos
= -1;
5657 if ( row
>= 0 && row
< m_numRows
&&
5658 col
>= 0 && col
< m_numCols
)
5660 // get the cell rectangle in logical coords
5662 wxRect
r( CellToRect( row
, col
) );
5664 // convert to device coords
5666 int left
, top
, right
, bottom
;
5667 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5668 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5671 m_gridWin
->GetClientSize( &cw
, &ch
);
5677 else if ( bottom
> ch
)
5679 int h
= r
.GetHeight();
5681 for ( i
= row
-1; i
>= 0; i
-- )
5683 int rowHeight
= GetRowHeight(i
);
5684 if ( h
+ rowHeight
> ch
)
5691 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5692 // have rounding errors (this is important, because if we do, we
5693 // might not scroll at all and some cells won't be redrawn)
5694 ypos
+= GRID_SCROLL_LINE
/ 2;
5701 else if ( right
> cw
)
5703 int w
= r
.GetWidth();
5705 for ( i
= col
-1; i
>= 0; i
-- )
5707 int colWidth
= GetColWidth(i
);
5708 if ( w
+ colWidth
> cw
)
5715 // see comment for ypos above
5716 xpos
+= GRID_SCROLL_LINE
/ 2;
5719 if ( xpos
!= -1 || ypos
!= -1 )
5721 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5722 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5723 Scroll( xpos
, ypos
);
5731 // ------ Grid cursor movement functions
5734 bool wxGrid::MoveCursorUp()
5736 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5737 m_currentCellCoords
.GetRow() > 0 )
5739 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5740 m_currentCellCoords
.GetCol() );
5742 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5743 m_currentCellCoords
.GetCol() );
5752 bool wxGrid::MoveCursorDown()
5754 // TODO: allow for scrolling
5756 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5757 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5759 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5760 m_currentCellCoords
.GetCol() );
5762 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5763 m_currentCellCoords
.GetCol() );
5772 bool wxGrid::MoveCursorLeft()
5774 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5775 m_currentCellCoords
.GetCol() > 0 )
5777 MakeCellVisible( m_currentCellCoords
.GetRow(),
5778 m_currentCellCoords
.GetCol() - 1 );
5780 SetCurrentCell( m_currentCellCoords
.GetRow(),
5781 m_currentCellCoords
.GetCol() - 1 );
5790 bool wxGrid::MoveCursorRight()
5792 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5793 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5795 MakeCellVisible( m_currentCellCoords
.GetRow(),
5796 m_currentCellCoords
.GetCol() + 1 );
5798 SetCurrentCell( m_currentCellCoords
.GetRow(),
5799 m_currentCellCoords
.GetCol() + 1 );
5808 bool wxGrid::MovePageUp()
5810 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5812 int row
= m_currentCellCoords
.GetRow();
5816 m_gridWin
->GetClientSize( &cw
, &ch
);
5818 int y
= GetRowTop(row
);
5819 int newRow
= YToRow( y
- ch
+ 1 );
5824 else if ( newRow
== row
)
5829 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5830 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5838 bool wxGrid::MovePageDown()
5840 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5842 int row
= m_currentCellCoords
.GetRow();
5843 if ( row
< m_numRows
)
5846 m_gridWin
->GetClientSize( &cw
, &ch
);
5848 int y
= GetRowTop(row
);
5849 int newRow
= YToRow( y
+ ch
);
5852 newRow
= m_numRows
- 1;
5854 else if ( newRow
== row
)
5859 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5860 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5868 bool wxGrid::MoveCursorUpBlock()
5871 m_currentCellCoords
!= wxGridNoCellCoords
&&
5872 m_currentCellCoords
.GetRow() > 0 )
5874 int row
= m_currentCellCoords
.GetRow();
5875 int col
= m_currentCellCoords
.GetCol();
5877 if ( m_table
->IsEmptyCell(row
, col
) )
5879 // starting in an empty cell: find the next block of
5885 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5888 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5890 // starting at the top of a block: find the next block
5896 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5901 // starting within a block: find the top of the block
5906 if ( m_table
->IsEmptyCell(row
, col
) )
5914 MakeCellVisible( row
, col
);
5915 SetCurrentCell( row
, col
);
5923 bool wxGrid::MoveCursorDownBlock()
5926 m_currentCellCoords
!= wxGridNoCellCoords
&&
5927 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5929 int row
= m_currentCellCoords
.GetRow();
5930 int col
= m_currentCellCoords
.GetCol();
5932 if ( m_table
->IsEmptyCell(row
, col
) )
5934 // starting in an empty cell: find the next block of
5937 while ( row
< m_numRows
-1 )
5940 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5943 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5945 // starting at the bottom of a block: find the next block
5948 while ( row
< m_numRows
-1 )
5951 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5956 // starting within a block: find the bottom of the block
5958 while ( row
< m_numRows
-1 )
5961 if ( m_table
->IsEmptyCell(row
, col
) )
5969 MakeCellVisible( row
, col
);
5970 SetCurrentCell( row
, col
);
5978 bool wxGrid::MoveCursorLeftBlock()
5981 m_currentCellCoords
!= wxGridNoCellCoords
&&
5982 m_currentCellCoords
.GetCol() > 0 )
5984 int row
= m_currentCellCoords
.GetRow();
5985 int col
= m_currentCellCoords
.GetCol();
5987 if ( m_table
->IsEmptyCell(row
, col
) )
5989 // starting in an empty cell: find the next block of
5995 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5998 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6000 // starting at the left of a block: find the next block
6006 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6011 // starting within a block: find the left of the block
6016 if ( m_table
->IsEmptyCell(row
, col
) )
6024 MakeCellVisible( row
, col
);
6025 SetCurrentCell( row
, col
);
6033 bool wxGrid::MoveCursorRightBlock()
6036 m_currentCellCoords
!= wxGridNoCellCoords
&&
6037 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6039 int row
= m_currentCellCoords
.GetRow();
6040 int col
= m_currentCellCoords
.GetCol();
6042 if ( m_table
->IsEmptyCell(row
, col
) )
6044 // starting in an empty cell: find the next block of
6047 while ( col
< m_numCols
-1 )
6050 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6053 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6055 // starting at the right of a block: find the next block
6058 while ( col
< m_numCols
-1 )
6061 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6066 // starting within a block: find the right of the block
6068 while ( col
< m_numCols
-1 )
6071 if ( m_table
->IsEmptyCell(row
, col
) )
6079 MakeCellVisible( row
, col
);
6080 SetCurrentCell( row
, col
);
6091 // ------ Label values and formatting
6094 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6096 *horiz
= m_rowLabelHorizAlign
;
6097 *vert
= m_rowLabelVertAlign
;
6100 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6102 *horiz
= m_colLabelHorizAlign
;
6103 *vert
= m_colLabelVertAlign
;
6106 wxString
wxGrid::GetRowLabelValue( int row
)
6110 return m_table
->GetRowLabelValue( row
);
6120 wxString
wxGrid::GetColLabelValue( int col
)
6124 return m_table
->GetColLabelValue( col
);
6135 void wxGrid::SetRowLabelSize( int width
)
6137 width
= wxMax( width
, 0 );
6138 if ( width
!= m_rowLabelWidth
)
6142 m_rowLabelWin
->Show( FALSE
);
6143 m_cornerLabelWin
->Show( FALSE
);
6145 else if ( m_rowLabelWidth
== 0 )
6147 m_rowLabelWin
->Show( TRUE
);
6148 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6151 m_rowLabelWidth
= width
;
6158 void wxGrid::SetColLabelSize( int height
)
6160 height
= wxMax( height
, 0 );
6161 if ( height
!= m_colLabelHeight
)
6165 m_colLabelWin
->Show( FALSE
);
6166 m_cornerLabelWin
->Show( FALSE
);
6168 else if ( m_colLabelHeight
== 0 )
6170 m_colLabelWin
->Show( TRUE
);
6171 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6174 m_colLabelHeight
= height
;
6181 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6183 if ( m_labelBackgroundColour
!= colour
)
6185 m_labelBackgroundColour
= colour
;
6186 m_rowLabelWin
->SetBackgroundColour( colour
);
6187 m_colLabelWin
->SetBackgroundColour( colour
);
6188 m_cornerLabelWin
->SetBackgroundColour( colour
);
6190 if ( !GetBatchCount() )
6192 m_rowLabelWin
->Refresh();
6193 m_colLabelWin
->Refresh();
6194 m_cornerLabelWin
->Refresh();
6199 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6201 if ( m_labelTextColour
!= colour
)
6203 m_labelTextColour
= colour
;
6204 if ( !GetBatchCount() )
6206 m_rowLabelWin
->Refresh();
6207 m_colLabelWin
->Refresh();
6212 void wxGrid::SetLabelFont( const wxFont
& font
)
6215 if ( !GetBatchCount() )
6217 m_rowLabelWin
->Refresh();
6218 m_colLabelWin
->Refresh();
6222 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6224 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6226 m_rowLabelHorizAlign
= horiz
;
6229 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6231 m_rowLabelVertAlign
= vert
;
6234 if ( !GetBatchCount() )
6236 m_rowLabelWin
->Refresh();
6240 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6242 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6244 m_colLabelHorizAlign
= horiz
;
6247 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6249 m_colLabelVertAlign
= vert
;
6252 if ( !GetBatchCount() )
6254 m_colLabelWin
->Refresh();
6258 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6262 m_table
->SetRowLabelValue( row
, s
);
6263 if ( !GetBatchCount() )
6265 wxRect rect
= CellToRect( row
, 0);
6266 if ( rect
.height
> 0 )
6268 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6270 rect
.width
= m_rowLabelWidth
;
6271 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6277 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6281 m_table
->SetColLabelValue( col
, s
);
6282 if ( !GetBatchCount() )
6284 wxRect rect
= CellToRect( 0, col
);
6285 if ( rect
.width
> 0 )
6287 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6289 rect
.height
= m_colLabelHeight
;
6290 m_colLabelWin
->Refresh( TRUE
, &rect
);
6296 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6298 if ( m_gridLineColour
!= colour
)
6300 m_gridLineColour
= colour
;
6302 wxClientDC
dc( m_gridWin
);
6304 DrawAllGridLines( dc
, wxRegion() );
6308 void wxGrid::EnableGridLines( bool enable
)
6310 if ( enable
!= m_gridLinesEnabled
)
6312 m_gridLinesEnabled
= enable
;
6314 if ( !GetBatchCount() )
6318 wxClientDC
dc( m_gridWin
);
6320 DrawAllGridLines( dc
, wxRegion() );
6324 m_gridWin
->Refresh();
6331 int wxGrid::GetDefaultRowSize()
6333 return m_defaultRowHeight
;
6336 int wxGrid::GetRowSize( int row
)
6338 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6340 return GetRowHeight(row
);
6343 int wxGrid::GetDefaultColSize()
6345 return m_defaultColWidth
;
6348 int wxGrid::GetColSize( int col
)
6350 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6352 return GetColWidth(col
);
6355 // ============================================================================
6356 // access to the grid attributes: each of them has a default value in the grid
6357 // itself and may be overidden on a per-cell basis
6358 // ============================================================================
6360 // ----------------------------------------------------------------------------
6361 // setting default attributes
6362 // ----------------------------------------------------------------------------
6364 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6366 m_defaultCellAttr
->SetBackgroundColour(col
);
6368 m_gridWin
->SetBackgroundColour(col
);
6372 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6374 m_defaultCellAttr
->SetTextColour(col
);
6377 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6379 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6382 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6384 m_defaultCellAttr
->SetFont(font
);
6387 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6389 m_defaultCellAttr
->SetRenderer(renderer
);
6392 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6394 m_defaultCellAttr
->SetEditor(editor
);
6397 // ----------------------------------------------------------------------------
6398 // access to the default attrbiutes
6399 // ----------------------------------------------------------------------------
6401 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6403 return m_defaultCellAttr
->GetBackgroundColour();
6406 wxColour
wxGrid::GetDefaultCellTextColour()
6408 return m_defaultCellAttr
->GetTextColour();
6411 wxFont
wxGrid::GetDefaultCellFont()
6413 return m_defaultCellAttr
->GetFont();
6416 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6418 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6421 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6423 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6426 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6428 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6431 // ----------------------------------------------------------------------------
6432 // access to cell attributes
6433 // ----------------------------------------------------------------------------
6435 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6437 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6438 wxColour colour
= attr
->GetBackgroundColour();
6443 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6445 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6446 wxColour colour
= attr
->GetTextColour();
6451 wxFont
wxGrid::GetCellFont( int row
, int col
)
6453 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6454 wxFont font
= attr
->GetFont();
6459 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6461 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6462 attr
->GetAlignment(horiz
, vert
);
6466 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6468 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6469 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6474 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6476 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6477 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6482 bool wxGrid::IsReadOnly(int row
, int col
) const
6484 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6485 bool isReadOnly
= attr
->IsReadOnly();
6490 // ----------------------------------------------------------------------------
6491 // attribute support: cache, automatic provider creation, ...
6492 // ----------------------------------------------------------------------------
6494 bool wxGrid::CanHaveAttributes()
6501 return m_table
->CanHaveAttributes();
6504 void wxGrid::ClearAttrCache()
6506 if ( m_attrCache
.row
!= -1 )
6508 m_attrCache
.attr
->SafeDecRef();
6509 m_attrCache
.row
= -1;
6513 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6515 wxGrid
*self
= (wxGrid
*)this; // const_cast
6517 self
->ClearAttrCache();
6518 self
->m_attrCache
.row
= row
;
6519 self
->m_attrCache
.col
= col
;
6520 self
->m_attrCache
.attr
= attr
;
6524 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6526 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6528 *attr
= m_attrCache
.attr
;
6529 (*attr
)->SafeIncRef();
6531 #ifdef DEBUG_ATTR_CACHE
6532 gs_nAttrCacheHits
++;
6539 #ifdef DEBUG_ATTR_CACHE
6540 gs_nAttrCacheMisses
++;
6546 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6548 wxGridCellAttr
*attr
;
6549 if ( !LookupAttr(row
, col
, &attr
) )
6551 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6552 CacheAttr(row
, col
, attr
);
6556 attr
->SetDefAttr(m_defaultCellAttr
);
6560 attr
= m_defaultCellAttr
;
6567 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6569 wxGridCellAttr
*attr
;
6570 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6572 wxASSERT_MSG( m_table
,
6573 _T("we may only be called if CanHaveAttributes() "
6574 "returned TRUE and then m_table should be !NULL") );
6576 attr
= m_table
->GetAttr(row
, col
);
6579 attr
= new wxGridCellAttr
;
6581 // artificially inc the ref count to match DecRef() in caller
6584 m_table
->SetAttr(attr
, row
, col
);
6587 CacheAttr(row
, col
, attr
);
6589 attr
->SetDefAttr(m_defaultCellAttr
);
6593 // ----------------------------------------------------------------------------
6594 // setting cell attributes: this is forwarded to the table
6595 // ----------------------------------------------------------------------------
6597 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6599 if ( CanHaveAttributes() )
6601 m_table
->SetRowAttr(attr
, row
);
6609 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6611 if ( CanHaveAttributes() )
6613 m_table
->SetColAttr(attr
, col
);
6621 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6623 if ( CanHaveAttributes() )
6625 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6626 attr
->SetBackgroundColour(colour
);
6631 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6633 if ( CanHaveAttributes() )
6635 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6636 attr
->SetTextColour(colour
);
6641 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6643 if ( CanHaveAttributes() )
6645 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6646 attr
->SetFont(font
);
6651 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6653 if ( CanHaveAttributes() )
6655 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6656 attr
->SetAlignment(horiz
, vert
);
6661 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6663 if ( CanHaveAttributes() )
6665 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6666 attr
->SetRenderer(renderer
);
6671 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6673 if ( CanHaveAttributes() )
6675 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6676 attr
->SetEditor(editor
);
6681 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6683 if ( CanHaveAttributes() )
6685 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6686 attr
->SetReadOnly(isReadOnly
);
6691 // ----------------------------------------------------------------------------
6692 // Data type registration
6693 // ----------------------------------------------------------------------------
6695 void wxGrid::RegisterDataType(const wxString
& typeName
,
6696 wxGridCellRenderer
* renderer
,
6697 wxGridCellEditor
* editor
)
6699 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6703 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6705 wxString typeName
= m_table
->GetTypeName(row
, col
);
6706 return GetDefaultEditorForType(typeName
);
6709 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6711 wxString typeName
= m_table
->GetTypeName(row
, col
);
6712 return GetDefaultRendererForType(typeName
);
6716 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6718 int index
= m_typeRegistry
->FindDataType(typeName
);
6720 // Should we force the failure here or let it fallback to string handling???
6721 // wxFAIL_MSG(wxT("Unknown data type name"));
6724 return m_typeRegistry
->GetEditor(index
);
6728 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6730 int index
= m_typeRegistry
->FindDataType(typeName
);
6732 // Should we force the failure here or let it fallback to string handling???
6733 // wxFAIL_MSG(wxT("Unknown data type name"));
6736 return m_typeRegistry
->GetRenderer(index
);
6740 // ----------------------------------------------------------------------------
6742 // ----------------------------------------------------------------------------
6744 void wxGrid::EnableDragRowSize( bool enable
)
6746 m_canDragRowSize
= enable
;
6750 void wxGrid::EnableDragColSize( bool enable
)
6752 m_canDragColSize
= enable
;
6755 void wxGrid::EnableDragGridSize( bool enable
)
6757 m_canDragGridSize
= enable
;
6761 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6763 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6765 if ( resizeExistingRows
)
6773 void wxGrid::SetRowSize( int row
, int height
)
6775 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6777 if ( m_rowHeights
.IsEmpty() )
6779 // need to really create the array
6783 int h
= wxMax( 0, height
);
6784 int diff
= h
- m_rowHeights
[row
];
6786 m_rowHeights
[row
] = h
;
6788 for ( i
= row
; i
< m_numRows
; i
++ )
6790 m_rowBottoms
[i
] += diff
;
6795 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6797 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6799 if ( resizeExistingCols
)
6807 void wxGrid::SetColSize( int col
, int width
)
6809 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6811 // should we check that it's bigger than GetColMinimalWidth(col) here?
6813 if ( m_colWidths
.IsEmpty() )
6815 // need to really create the array
6819 int w
= wxMax( 0, width
);
6820 int diff
= w
- m_colWidths
[col
];
6821 m_colWidths
[col
] = w
;
6824 for ( i
= col
; i
< m_numCols
; i
++ )
6826 m_colRights
[i
] += diff
;
6832 void wxGrid::SetColMinimalWidth( int col
, int width
)
6834 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6837 int wxGrid::GetColMinimalWidth(int col
) const
6839 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6840 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6843 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6845 wxClientDC
dc(m_gridWin
);
6847 wxCoord width
, widthMax
= 0;
6848 for ( int row
= 0; row
< m_numRows
; row
++ )
6850 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6851 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6854 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6855 if ( width
> widthMax
)
6864 // now also compare with the column label width
6865 dc
.SetFont( GetLabelFont() );
6866 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6867 if ( width
> widthMax
)
6874 // empty column - give default width (notice that if widthMax is less
6875 // than default width but != 0, it's ok)
6876 widthMax
= m_defaultColWidth
;
6880 // leave some space around text
6884 SetColSize(col
, widthMax
);
6887 SetColMinimalWidth(col
, widthMax
);
6891 void wxGrid::AutoSizeColumns( bool setAsMin
)
6893 for ( int col
= 0; col
< m_numCols
; col
++ )
6895 AutoSizeColumn(col
, setAsMin
);
6900 // ------ cell value accessor functions
6903 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6907 m_table
->SetValue( row
, col
, s
.c_str() );
6908 if ( !GetBatchCount() )
6910 wxClientDC
dc( m_gridWin
);
6912 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6915 if ( m_currentCellCoords
.GetRow() == row
&&
6916 m_currentCellCoords
.GetCol() == col
&&
6917 IsCellEditControlEnabled())
6919 HideCellEditControl();
6920 ShowCellEditControl(); // will reread data from table
6927 // ------ Block, row and col selection
6930 void wxGrid::SelectRow( int row
, bool addToSelected
)
6934 if ( IsSelection() && addToSelected
)
6937 bool need_refresh
[4];
6941 need_refresh
[3] = FALSE
;
6945 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6946 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6947 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6948 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6952 need_refresh
[0] = TRUE
;
6953 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6954 wxGridCellCoords ( oldTop
- 1,
6956 m_selectedTopLeft
.SetRow( row
);
6961 need_refresh
[1] = TRUE
;
6962 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6963 wxGridCellCoords ( oldBottom
,
6966 m_selectedTopLeft
.SetCol( 0 );
6969 if ( oldBottom
< row
)
6971 need_refresh
[2] = TRUE
;
6972 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6973 wxGridCellCoords ( row
,
6975 m_selectedBottomRight
.SetRow( row
);
6978 if ( oldRight
< m_numCols
- 1 )
6980 need_refresh
[3] = TRUE
;
6981 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6983 wxGridCellCoords ( oldBottom
,
6985 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6988 for (i
= 0; i
< 4; i
++ )
6989 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6990 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6994 r
= SelectionToDeviceRect();
6996 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6998 m_selectedTopLeft
.Set( row
, 0 );
6999 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7000 r
= SelectionToDeviceRect();
7001 m_gridWin
->Refresh( FALSE
, &r
);
7004 wxGridRangeSelectEvent
gridEvt( GetId(),
7005 wxEVT_GRID_RANGE_SELECT
,
7008 m_selectedBottomRight
);
7010 GetEventHandler()->ProcessEvent(gridEvt
);
7014 void wxGrid::SelectCol( int col
, bool addToSelected
)
7016 if ( IsSelection() && addToSelected
)
7019 bool need_refresh
[4];
7023 need_refresh
[3] = FALSE
;
7026 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7027 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7028 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7029 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7031 if ( oldLeft
> col
)
7033 need_refresh
[0] = TRUE
;
7034 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7035 wxGridCellCoords ( m_numRows
- 1,
7037 m_selectedTopLeft
.SetCol( col
);
7042 need_refresh
[1] = TRUE
;
7043 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7044 wxGridCellCoords ( oldTop
- 1,
7046 m_selectedTopLeft
.SetRow( 0 );
7049 if ( oldRight
< col
)
7051 need_refresh
[2] = TRUE
;
7052 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7053 wxGridCellCoords ( m_numRows
- 1,
7055 m_selectedBottomRight
.SetCol( col
);
7058 if ( oldBottom
< m_numRows
- 1 )
7060 need_refresh
[3] = TRUE
;
7061 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7063 wxGridCellCoords ( m_numRows
- 1,
7065 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7068 for (i
= 0; i
< 4; i
++ )
7069 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7070 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7076 r
= SelectionToDeviceRect();
7078 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7080 m_selectedTopLeft
.Set( 0, col
);
7081 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7082 r
= SelectionToDeviceRect();
7083 m_gridWin
->Refresh( FALSE
, &r
);
7086 wxGridRangeSelectEvent
gridEvt( GetId(),
7087 wxEVT_GRID_RANGE_SELECT
,
7090 m_selectedBottomRight
);
7092 GetEventHandler()->ProcessEvent(gridEvt
);
7096 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7099 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7101 if ( topRow
> bottomRow
)
7108 if ( leftCol
> rightCol
)
7115 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7116 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7118 if ( m_selectedTopLeft
!= updateTopLeft
||
7119 m_selectedBottomRight
!= updateBottomRight
)
7121 // Compute two optimal update rectangles:
7122 // Either one rectangle is a real subset of the
7123 // other, or they are (almost) disjoint!
7125 bool need_refresh
[4];
7129 need_refresh
[3] = FALSE
;
7132 // Store intermediate values
7133 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7134 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7135 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7136 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7138 // Determine the outer/inner coordinates.
7139 if (oldLeft
> leftCol
)
7145 if (oldTop
> topRow
)
7151 if (oldRight
< rightCol
)
7154 oldRight
= rightCol
;
7157 if (oldBottom
< bottomRow
)
7160 oldBottom
= bottomRow
;
7164 // Now, either the stuff marked old is the outer
7165 // rectangle or we don't have a situation where one
7166 // is contained in the other.
7168 if ( oldLeft
< leftCol
)
7170 need_refresh
[0] = TRUE
;
7171 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7173 wxGridCellCoords ( oldBottom
,
7177 if ( oldTop
< topRow
)
7179 need_refresh
[1] = TRUE
;
7180 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7182 wxGridCellCoords ( topRow
- 1,
7186 if ( oldRight
> rightCol
)
7188 need_refresh
[2] = TRUE
;
7189 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7191 wxGridCellCoords ( oldBottom
,
7195 if ( oldBottom
> bottomRow
)
7197 need_refresh
[3] = TRUE
;
7198 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7200 wxGridCellCoords ( oldBottom
,
7206 m_selectedTopLeft
= updateTopLeft
;
7207 m_selectedBottomRight
= updateBottomRight
;
7209 // various Refresh() calls
7210 for (i
= 0; i
< 4; i
++ )
7211 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7212 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7215 // only generate an event if the block is not being selected by
7216 // dragging the mouse (in which case the event will be generated in
7217 // the mouse event handler)
7218 if ( !m_isDragging
)
7220 wxGridRangeSelectEvent
gridEvt( GetId(),
7221 wxEVT_GRID_RANGE_SELECT
,
7224 m_selectedBottomRight
);
7226 GetEventHandler()->ProcessEvent(gridEvt
);
7230 void wxGrid::SelectAll()
7232 m_selectedTopLeft
.Set( 0, 0 );
7233 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7235 m_gridWin
->Refresh();
7239 void wxGrid::ClearSelection()
7241 m_selectedTopLeft
= wxGridNoCellCoords
;
7242 m_selectedBottomRight
= wxGridNoCellCoords
;
7246 // This function returns the rectangle that encloses the given block
7247 // in device coords clipped to the client size of the grid window.
7249 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7250 const wxGridCellCoords
&bottomRight
)
7252 wxRect
rect( wxGridNoCellRect
);
7255 cellRect
= CellToRect( topLeft
);
7256 if ( cellRect
!= wxGridNoCellRect
)
7262 rect
= wxRect( 0, 0, 0, 0 );
7265 cellRect
= CellToRect( bottomRight
);
7266 if ( cellRect
!= wxGridNoCellRect
)
7272 return wxGridNoCellRect
;
7275 // convert to scrolled coords
7277 int left
, top
, right
, bottom
;
7278 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7279 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7282 m_gridWin
->GetClientSize( &cw
, &ch
);
7284 rect
.SetLeft( wxMax(0, left
) );
7285 rect
.SetTop( wxMax(0, top
) );
7286 rect
.SetRight( wxMin(cw
, right
) );
7287 rect
.SetBottom( wxMin(ch
, bottom
) );
7295 // ------ Grid event classes
7298 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7300 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7301 int row
, int col
, int x
, int y
,
7302 bool control
, bool shift
, bool alt
, bool meta
)
7303 : wxNotifyEvent( type
, id
)
7309 m_control
= control
;
7314 SetEventObject(obj
);
7318 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7320 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7321 int rowOrCol
, int x
, int y
,
7322 bool control
, bool shift
, bool alt
, bool meta
)
7323 : wxNotifyEvent( type
, id
)
7325 m_rowOrCol
= rowOrCol
;
7328 m_control
= control
;
7333 SetEventObject(obj
);
7337 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7339 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7340 const wxGridCellCoords
& topLeft
,
7341 const wxGridCellCoords
& bottomRight
,
7342 bool control
, bool shift
, bool alt
, bool meta
)
7343 : wxNotifyEvent( type
, id
)
7345 m_topLeft
= topLeft
;
7346 m_bottomRight
= bottomRight
;
7347 m_control
= control
;
7352 SetEventObject(obj
);
7356 #endif // ifndef wxUSE_NEW_GRID