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
;
4130 m_dragRowOrCol
= dragCol
;
4132 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4134 if ( CanDragColSize() && CanDragGridSize() )
4135 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4141 // Neither on a row or col edge
4143 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4145 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4151 void wxGrid::DoEndDragResizeRow()
4153 if ( m_dragLastPos
>= 0 )
4155 // erase the last line and resize the row
4157 int cw
, ch
, left
, dummy
;
4158 m_gridWin
->GetClientSize( &cw
, &ch
);
4159 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4161 wxClientDC
dc( m_gridWin
);
4163 dc
.SetLogicalFunction( wxINVERT
);
4164 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4165 HideCellEditControl();
4166 SaveEditControlValue();
4168 int rowTop
= GetRowTop(m_dragRowOrCol
);
4169 SetRowSize( m_dragRowOrCol
,
4170 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4172 if ( !GetBatchCount() )
4174 // Only needed to get the correct rect.y:
4175 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4177 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4178 rect
.width
= m_rowLabelWidth
;
4179 rect
.height
= ch
- rect
.y
;
4180 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4182 m_gridWin
->Refresh( FALSE
, &rect
);
4185 ShowCellEditControl();
4190 void wxGrid::DoEndDragResizeCol()
4192 if ( m_dragLastPos
>= 0 )
4194 // erase the last line and resize the col
4196 int cw
, ch
, dummy
, top
;
4197 m_gridWin
->GetClientSize( &cw
, &ch
);
4198 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4200 wxClientDC
dc( m_gridWin
);
4202 dc
.SetLogicalFunction( wxINVERT
);
4203 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4204 HideCellEditControl();
4205 SaveEditControlValue();
4207 int colLeft
= GetColLeft(m_dragRowOrCol
);
4208 SetColSize( m_dragRowOrCol
,
4209 wxMax( m_dragLastPos
- colLeft
,
4210 GetColMinimalWidth(m_dragRowOrCol
) ) );
4212 if ( !GetBatchCount() )
4214 // Only needed to get the correct rect.x:
4215 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4217 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4218 rect
.width
= cw
- rect
.x
;
4219 rect
.height
= m_colLabelHeight
;
4220 m_colLabelWin
->Refresh( TRUE
, &rect
);
4222 m_gridWin
->Refresh( FALSE
, &rect
);
4225 ShowCellEditControl();
4232 // ------ interaction with data model
4234 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4236 switch ( msg
.GetId() )
4238 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4239 return GetModelValues();
4241 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4242 return SetModelValues();
4244 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4245 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4246 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4247 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4248 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4249 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4250 return Redimension( msg
);
4259 // The behaviour of this function depends on the grid table class
4260 // Clear() function. For the default wxGridStringTable class the
4261 // behavious is to replace all cell contents with wxEmptyString but
4262 // not to change the number of rows or cols.
4264 void wxGrid::ClearGrid()
4268 if (IsCellEditControlEnabled())
4269 DisableCellEditControl();
4272 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4277 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4279 // TODO: something with updateLabels flag
4283 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4289 if (IsCellEditControlEnabled())
4290 DisableCellEditControl();
4292 bool ok
= m_table
->InsertRows( pos
, numRows
);
4294 // the table will have sent the results of the insert row
4295 // operation to this view object as a grid table message
4299 if ( m_numCols
== 0 )
4301 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4303 // TODO: perhaps instead of appending the default number of cols
4304 // we should remember what the last non-zero number of cols was ?
4308 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4310 // if we have just inserted cols into an empty grid the current
4311 // cell will be undefined...
4313 SetCurrentCell( 0, 0 );
4317 if ( !GetBatchCount() ) Refresh();
4329 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4331 // TODO: something with updateLabels flag
4335 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4339 if ( m_table
&& m_table
->AppendRows( numRows
) )
4341 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4343 // if we have just inserted cols into an empty grid the current
4344 // cell will be undefined...
4346 SetCurrentCell( 0, 0 );
4349 // the table will have sent the results of the append row
4350 // operation to this view object as a grid table message
4353 if ( !GetBatchCount() ) Refresh();
4363 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4365 // TODO: something with updateLabels flag
4369 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4375 if (IsCellEditControlEnabled())
4376 DisableCellEditControl();
4378 if (m_table
->DeleteRows( pos
, numRows
))
4381 // the table will have sent the results of the delete row
4382 // operation to this view object as a grid table message
4385 if ( !GetBatchCount() ) Refresh();
4393 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4395 // TODO: something with updateLabels flag
4399 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4405 if (IsCellEditControlEnabled())
4406 DisableCellEditControl();
4408 bool ok
= m_table
->InsertCols( pos
, numCols
);
4410 // the table will have sent the results of the insert col
4411 // operation to this view object as a grid table message
4415 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4417 // if we have just inserted cols into an empty grid the current
4418 // cell will be undefined...
4420 SetCurrentCell( 0, 0 );
4424 if ( !GetBatchCount() ) Refresh();
4436 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4438 // TODO: something with updateLabels flag
4442 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4446 if ( m_table
&& m_table
->AppendCols( numCols
) )
4448 // the table will have sent the results of the append col
4449 // operation to this view object as a grid table message
4451 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4453 // if we have just inserted cols into an empty grid the current
4454 // cell will be undefined...
4456 SetCurrentCell( 0, 0 );
4460 if ( !GetBatchCount() ) Refresh();
4470 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4472 // TODO: something with updateLabels flag
4476 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4482 if (IsCellEditControlEnabled())
4483 DisableCellEditControl();
4485 if ( m_table
->DeleteCols( pos
, numCols
) )
4487 // the table will have sent the results of the delete col
4488 // operation to this view object as a grid table message
4491 if ( !GetBatchCount() ) Refresh();
4501 // ----- event handlers
4504 // Generate a grid event based on a mouse event and
4505 // return the result of ProcessEvent()
4507 bool wxGrid::SendEvent( const wxEventType type
,
4509 wxMouseEvent
& mouseEv
)
4511 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4513 int rowOrCol
= (row
== -1 ? col
: row
);
4515 wxGridSizeEvent
gridEvt( GetId(),
4519 mouseEv
.GetX(), mouseEv
.GetY(),
4520 mouseEv
.ControlDown(),
4521 mouseEv
.ShiftDown(),
4523 mouseEv
.MetaDown() );
4525 return GetEventHandler()->ProcessEvent(gridEvt
);
4527 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4529 wxGridRangeSelectEvent
gridEvt( GetId(),
4533 m_selectedBottomRight
,
4534 mouseEv
.ControlDown(),
4535 mouseEv
.ShiftDown(),
4537 mouseEv
.MetaDown() );
4539 return GetEventHandler()->ProcessEvent(gridEvt
);
4543 wxGridEvent
gridEvt( GetId(),
4547 mouseEv
.GetX(), mouseEv
.GetY(),
4548 mouseEv
.ControlDown(),
4549 mouseEv
.ShiftDown(),
4551 mouseEv
.MetaDown() );
4553 return GetEventHandler()->ProcessEvent(gridEvt
);
4558 // Generate a grid event of specified type and return the result
4559 // of ProcessEvent().
4561 bool wxGrid::SendEvent( const wxEventType type
,
4564 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4566 int rowOrCol
= (row
== -1 ? col
: row
);
4568 wxGridSizeEvent
gridEvt( GetId(),
4573 return GetEventHandler()->ProcessEvent(gridEvt
);
4577 wxGridEvent
gridEvt( GetId(),
4582 return GetEventHandler()->ProcessEvent(gridEvt
);
4587 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4589 wxPaintDC
dc( this );
4591 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4592 m_numRows
&& m_numCols
)
4594 m_currentCellCoords
.Set(0, 0);
4595 ShowCellEditControl();
4602 // This is just here to make sure that CalcDimensions gets called when
4603 // the grid view is resized... then the size event is skipped to allow
4604 // the box sizers to handle everything
4606 void wxGrid::OnSize( wxSizeEvent
& event
)
4613 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4615 if ( m_inOnKeyDown
)
4617 // shouldn't be here - we are going round in circles...
4619 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4622 m_inOnKeyDown
= TRUE
;
4624 // propagate the event up and see if it gets processed
4626 wxWindow
*parent
= GetParent();
4627 wxKeyEvent
keyEvt( event
);
4628 keyEvt
.SetEventObject( parent
);
4630 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4633 // TODO: Should also support Shift-cursor keys for
4634 // extending the selection. Maybe add a flag to
4635 // MoveCursorXXX() and MoveCursorXXXBlock() and
4636 // just send event.ShiftDown().
4638 // try local handlers
4640 switch ( event
.KeyCode() )
4643 if ( event
.ControlDown() )
4645 MoveCursorUpBlock();
4654 if ( event
.ControlDown() )
4656 MoveCursorDownBlock();
4665 if ( event
.ControlDown() )
4667 MoveCursorLeftBlock();
4676 if ( event
.ControlDown() )
4678 MoveCursorRightBlock();
4687 if ( event
.ControlDown() )
4689 event
.Skip(); // to let the edit control have the return
4698 if (event
.ShiftDown())
4705 if ( event
.ControlDown() )
4707 MakeCellVisible( 0, 0 );
4708 SetCurrentCell( 0, 0 );
4717 if ( event
.ControlDown() )
4719 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4720 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4737 if ( !IsEditable() )
4742 // Otherwise fall through to default
4745 // alphanumeric keys enable the cell edit control
4746 if ( !(event
.AltDown() ||
4748 event
.ControlDown()) &&
4749 isalnum(event
.KeyCode()) &&
4750 !IsCellEditControlEnabled() &&
4751 CanEnableCellControl() )
4753 EnableCellEditControl();
4754 int row
= m_currentCellCoords
.GetRow();
4755 int col
= m_currentCellCoords
.GetCol();
4756 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4757 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4762 // let others process char events with modifiers or all
4763 // char events for readonly cells
4770 m_inOnKeyDown
= FALSE
;
4774 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4778 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4780 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4782 // the event has been intercepted - do nothing
4787 m_currentCellCoords
!= wxGridNoCellCoords
)
4789 HideCellEditControl();
4790 DisableCellEditControl();
4792 // Clear the old current cell highlight
4793 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4795 // Otherwise refresh redraws the highlight!
4796 m_currentCellCoords
= coords
;
4798 m_gridWin
->Refresh( FALSE
, &r
);
4801 m_currentCellCoords
= coords
;
4805 wxClientDC
dc(m_gridWin
);
4808 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4809 DrawCellHighlight(dc
, attr
);
4812 if ( IsSelection() )
4814 wxRect
r( SelectionToDeviceRect() );
4816 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4823 // ------ functions to get/send data (see also public functions)
4826 bool wxGrid::GetModelValues()
4830 // all we need to do is repaint the grid
4832 m_gridWin
->Refresh();
4840 bool wxGrid::SetModelValues()
4846 for ( row
= 0; row
< m_numRows
; row
++ )
4848 for ( col
= 0; col
< m_numCols
; col
++ )
4850 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4862 // Note - this function only draws cells that are in the list of
4863 // exposed cells (usually set from the update region by
4864 // CalcExposedCells)
4866 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4868 if ( !m_numRows
|| !m_numCols
) return;
4871 size_t numCells
= m_cellsExposed
.GetCount();
4873 for ( i
= 0; i
< numCells
; i
++ )
4875 DrawCell( dc
, m_cellsExposed
[i
] );
4880 void wxGrid::DrawGridSpace( wxDC
& dc
)
4882 if ( m_numRows
&& m_numCols
)
4885 m_gridWin
->GetClientSize( &cw
, &ch
);
4888 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4890 if ( right
> GetColRight(m_numCols
-1) ||
4891 bottom
> GetRowBottom(m_numRows
-1) )
4894 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4896 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4897 dc
.SetPen( *wxTRANSPARENT_PEN
);
4899 if ( right
> GetColRight(m_numCols
-1) )
4900 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
4901 right
- GetColRight(m_numCols
-1), ch
);
4903 if ( bottom
> GetRowBottom(m_numRows
-1) )
4904 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
4905 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4911 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4913 int row
= coords
.GetRow();
4914 int col
= coords
.GetCol();
4916 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4919 // we draw the cell border ourselves
4920 #if !WXGRID_DRAW_LINES
4921 if ( m_gridLinesEnabled
)
4922 DrawCellBorder( dc
, coords
);
4925 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4927 bool isCurrent
= coords
== m_currentCellCoords
;
4930 rect
.x
= GetColLeft(col
);
4931 rect
.y
= GetRowTop(row
);
4932 rect
.width
= GetColWidth(col
) - 1;
4933 rect
.height
= GetRowHeight(row
) - 1;
4935 // if the editor is shown, we should use it and not the renderer
4936 if ( isCurrent
&& IsCellEditControlEnabled() )
4938 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4942 // but all the rest is drawn by the cell renderer and hence may be
4944 attr
->GetRenderer(this, row
, col
)->
4945 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4952 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4954 int row
= m_currentCellCoords
.GetRow();
4955 int col
= m_currentCellCoords
.GetCol();
4957 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4961 rect
.x
= GetColLeft(col
);
4962 rect
.y
= GetRowTop(row
);
4963 rect
.width
= GetColWidth(col
) - 1;
4964 rect
.height
= GetRowHeight(row
) - 1;
4966 // hmmm... what could we do here to show that the cell is disabled?
4967 // for now, I just draw a thinner border than for the other ones, but
4968 // it doesn't look really good
4969 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4970 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4972 dc
.DrawRectangle(rect
);
4975 // VZ: my experiments with 3d borders...
4977 // how to properly set colours for arbitrary bg?
4978 wxCoord x1
= rect
.x
,
4980 x2
= rect
.x
+ rect
.width
-1,
4981 y2
= rect
.y
+ rect
.height
-1;
4983 dc
.SetPen(*wxWHITE_PEN
);
4984 dc
.DrawLine(x1
, y1
, x2
, y1
);
4985 dc
.DrawLine(x1
, y1
, x1
, y2
);
4987 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4988 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4990 dc
.SetPen(*wxBLACK_PEN
);
4991 dc
.DrawLine(x1
, y2
, x2
, y2
);
4992 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4997 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4999 int row
= coords
.GetRow();
5000 int col
= coords
.GetCol();
5001 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5004 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5006 // right hand border
5008 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5009 GetColRight(col
), GetRowBottom(row
) );
5013 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5014 GetColRight(col
), GetRowBottom(row
) );
5017 void wxGrid::DrawHighlight(wxDC
& dc
)
5019 if ( IsCellEditControlEnabled() )
5021 // don't show highlight when the edit control is shown
5025 // if the active cell was repainted, repaint its highlight too because it
5026 // might have been damaged by the grid lines
5027 size_t count
= m_cellsExposed
.GetCount();
5028 for ( size_t n
= 0; n
< count
; n
++ )
5030 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5032 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5033 DrawCellHighlight(dc
, attr
);
5041 // TODO: remove this ???
5042 // This is used to redraw all grid lines e.g. when the grid line colour
5045 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5047 if ( !m_gridLinesEnabled
||
5049 !m_numCols
) return;
5051 int top
, bottom
, left
, right
;
5057 m_gridWin
->GetClientSize(&cw
, &ch
);
5059 // virtual coords of visible area
5061 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5062 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5067 reg
.GetBox(x
, y
, w
, h
);
5068 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5069 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5073 m_gridWin
->GetClientSize(&cw
, &ch
);
5074 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5075 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5078 // avoid drawing grid lines past the last row and col
5080 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5081 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5083 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5085 // horizontal grid lines
5088 for ( i
= 0; i
< m_numRows
; i
++ )
5090 int bot
= GetRowBottom(i
) - 1;
5099 dc
.DrawLine( left
, bot
, right
, bot
);
5104 // vertical grid lines
5106 for ( i
= 0; i
< m_numCols
; i
++ )
5108 int colRight
= GetColRight(i
) - 1;
5109 if ( colRight
> right
)
5114 if ( colRight
>= left
)
5116 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5122 void wxGrid::DrawRowLabels( wxDC
& dc
)
5124 if ( !m_numRows
|| !m_numCols
) return;
5127 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5129 for ( i
= 0; i
< numLabels
; i
++ )
5131 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5136 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5138 if ( GetRowHeight(row
) <= 0 )
5141 int rowTop
= GetRowTop(row
),
5142 rowBottom
= GetRowBottom(row
) - 1;
5144 dc
.SetPen( *wxBLACK_PEN
);
5145 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5146 m_rowLabelWidth
-1, rowBottom
);
5148 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5150 dc
.SetPen( *wxWHITE_PEN
);
5151 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5152 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5154 dc
.SetBackgroundMode( wxTRANSPARENT
);
5155 dc
.SetTextForeground( GetLabelTextColour() );
5156 dc
.SetFont( GetLabelFont() );
5159 GetRowLabelAlignment( &hAlign
, &vAlign
);
5163 rect
.SetY( GetRowTop(row
) + 2 );
5164 rect
.SetWidth( m_rowLabelWidth
- 4 );
5165 rect
.SetHeight( GetRowHeight(row
) - 4 );
5166 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5170 void wxGrid::DrawColLabels( wxDC
& dc
)
5172 if ( !m_numRows
|| !m_numCols
) return;
5175 size_t numLabels
= m_colLabelsExposed
.GetCount();
5177 for ( i
= 0; i
< numLabels
; i
++ )
5179 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5184 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5186 if ( GetColWidth(col
) <= 0 )
5189 int colLeft
= GetColLeft(col
),
5190 colRight
= GetColRight(col
) - 1;
5192 dc
.SetPen( *wxBLACK_PEN
);
5193 dc
.DrawLine( colRight
, 0,
5194 colRight
, m_colLabelHeight
-1 );
5196 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5197 colRight
, m_colLabelHeight
-1 );
5199 dc
.SetPen( *wxWHITE_PEN
);
5200 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5201 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5203 dc
.SetBackgroundMode( wxTRANSPARENT
);
5204 dc
.SetTextForeground( GetLabelTextColour() );
5205 dc
.SetFont( GetLabelFont() );
5207 dc
.SetBackgroundMode( wxTRANSPARENT
);
5208 dc
.SetTextForeground( GetLabelTextColour() );
5209 dc
.SetFont( GetLabelFont() );
5212 GetColLabelAlignment( &hAlign
, &vAlign
);
5215 rect
.SetX( colLeft
+ 2 );
5217 rect
.SetWidth( GetColWidth(col
) - 4 );
5218 rect
.SetHeight( m_colLabelHeight
- 4 );
5219 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5223 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5224 const wxString
& value
,
5229 long textWidth
, textHeight
;
5230 long lineWidth
, lineHeight
;
5231 wxArrayString lines
;
5233 dc
.SetClippingRegion( rect
);
5234 StringToLines( value
, lines
);
5235 if ( lines
.GetCount() )
5237 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5238 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5241 switch ( horizAlign
)
5244 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5248 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5257 switch ( vertAlign
)
5260 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5264 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5273 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5275 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5280 dc
.DestroyClippingRegion();
5284 // Split multi line text up into an array of strings. Any existing
5285 // contents of the string array are preserved.
5287 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5291 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5292 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5294 while ( startPos
< (int)tVal
.Length() )
5296 pos
= tVal
.Mid(startPos
).Find( eol
);
5301 else if ( pos
== 0 )
5303 lines
.Add( wxEmptyString
);
5307 lines
.Add( value
.Mid(startPos
, pos
) );
5311 if ( startPos
< (int)value
.Length() )
5313 lines
.Add( value
.Mid( startPos
) );
5318 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5319 wxArrayString
& lines
,
5320 long *width
, long *height
)
5327 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5329 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5330 w
= wxMax( w
, lineW
);
5340 // ------ Edit control functions
5344 void wxGrid::EnableEditing( bool edit
)
5346 // TODO: improve this ?
5348 if ( edit
!= m_editable
)
5352 // FIXME IMHO this won't disable the edit control if edit == FALSE
5353 // because of the check in the beginning of
5354 // EnableCellEditControl() just below (VZ)
5355 EnableCellEditControl(m_editable
);
5360 void wxGrid::EnableCellEditControl( bool enable
)
5365 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5366 SetCurrentCell( 0, 0 );
5368 if ( enable
!= m_cellEditCtrlEnabled
)
5370 // TODO allow the app to Veto() this event?
5371 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5375 // this should be checked by the caller!
5376 wxASSERT_MSG( CanEnableCellControl(),
5377 _T("can't enable editing for this cell!") );
5379 // do it before ShowCellEditControl()
5380 m_cellEditCtrlEnabled
= enable
;
5382 ShowCellEditControl();
5386 HideCellEditControl();
5387 SaveEditControlValue();
5389 // do it after HideCellEditControl()
5390 m_cellEditCtrlEnabled
= enable
;
5395 bool wxGrid::IsCurrentCellReadOnly() const
5398 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5399 bool readonly
= attr
->IsReadOnly();
5405 bool wxGrid::CanEnableCellControl() const
5407 return m_editable
&& !IsCurrentCellReadOnly();
5410 bool wxGrid::IsCellEditControlEnabled() const
5412 // the cell edit control might be disable for all cells or just for the
5413 // current one if it's read only
5414 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5417 void wxGrid::ShowCellEditControl()
5419 if ( IsCellEditControlEnabled() )
5421 if ( !IsVisible( m_currentCellCoords
) )
5427 wxRect rect
= CellToRect( m_currentCellCoords
);
5428 int row
= m_currentCellCoords
.GetRow();
5429 int col
= m_currentCellCoords
.GetCol();
5431 // convert to scrolled coords
5433 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5435 // done in PaintBackground()
5437 // erase the highlight and the cell contents because the editor
5438 // might not cover the entire cell
5439 wxClientDC
dc( m_gridWin
);
5441 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5442 dc
.SetPen(*wxTRANSPARENT_PEN
);
5443 dc
.DrawRectangle(rect
);
5446 // cell is shifted by one pixel
5450 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5451 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5452 if ( !editor
->IsCreated() )
5454 editor
->Create(m_gridWin
, -1,
5455 new wxGridCellEditorEvtHandler(this, editor
));
5458 editor
->SetSize( rect
);
5460 editor
->Show( TRUE
, attr
);
5461 editor
->BeginEdit(row
, col
, this);
5468 void wxGrid::HideCellEditControl()
5470 if ( IsCellEditControlEnabled() )
5472 int row
= m_currentCellCoords
.GetRow();
5473 int col
= m_currentCellCoords
.GetCol();
5475 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5476 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5478 m_gridWin
->SetFocus();
5483 void wxGrid::SaveEditControlValue()
5485 if ( IsCellEditControlEnabled() )
5487 int row
= m_currentCellCoords
.GetRow();
5488 int col
= m_currentCellCoords
.GetCol();
5490 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5491 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5492 bool changed
= editor
->EndEdit(row
, col
, this);
5498 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5499 m_currentCellCoords
.GetRow(),
5500 m_currentCellCoords
.GetCol() );
5507 // ------ Grid location functions
5508 // Note that all of these functions work with the logical coordinates of
5509 // grid cells and labels so you will need to convert from device
5510 // coordinates for mouse events etc.
5513 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5515 int row
= YToRow(y
);
5516 int col
= XToCol(x
);
5518 if ( row
== -1 || col
== -1 )
5520 coords
= wxGridNoCellCoords
;
5524 coords
.Set( row
, col
);
5529 int wxGrid::YToRow( int y
)
5533 for ( i
= 0; i
< m_numRows
; i
++ )
5535 if ( y
< GetRowBottom(i
) )
5543 int wxGrid::XToCol( int x
)
5547 for ( i
= 0; i
< m_numCols
; i
++ )
5549 if ( x
< GetColRight(i
) )
5557 // return the row number that that the y coord is near the edge of, or
5558 // -1 if not near an edge
5560 int wxGrid::YToEdgeOfRow( int y
)
5564 for ( i
= 0; i
< m_numRows
; i
++ )
5566 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5568 d
= abs( y
- GetRowBottom(i
) );
5569 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5578 // return the col number that that the x coord is near the edge of, or
5579 // -1 if not near an edge
5581 int wxGrid::XToEdgeOfCol( int x
)
5585 for ( i
= 0; i
< m_numCols
; i
++ )
5587 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5589 d
= abs( x
- GetColRight(i
) );
5590 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5599 wxRect
wxGrid::CellToRect( int row
, int col
)
5601 wxRect
rect( -1, -1, -1, -1 );
5603 if ( row
>= 0 && row
< m_numRows
&&
5604 col
>= 0 && col
< m_numCols
)
5606 rect
.x
= GetColLeft(col
);
5607 rect
.y
= GetRowTop(row
);
5608 rect
.width
= GetColWidth(col
);
5609 rect
.height
= GetRowHeight(row
);
5616 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5618 // get the cell rectangle in logical coords
5620 wxRect
r( CellToRect( row
, col
) );
5622 // convert to device coords
5624 int left
, top
, right
, bottom
;
5625 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5626 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5628 // check against the client area of the grid window
5631 m_gridWin
->GetClientSize( &cw
, &ch
);
5633 if ( wholeCellVisible
)
5635 // is the cell wholly visible ?
5637 return ( left
>= 0 && right
<= cw
&&
5638 top
>= 0 && bottom
<= ch
);
5642 // is the cell partly visible ?
5644 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5645 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5650 // make the specified cell location visible by doing a minimal amount
5653 void wxGrid::MakeCellVisible( int row
, int col
)
5656 int xpos
= -1, ypos
= -1;
5658 if ( row
>= 0 && row
< m_numRows
&&
5659 col
>= 0 && col
< m_numCols
)
5661 // get the cell rectangle in logical coords
5663 wxRect
r( CellToRect( row
, col
) );
5665 // convert to device coords
5667 int left
, top
, right
, bottom
;
5668 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5669 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5672 m_gridWin
->GetClientSize( &cw
, &ch
);
5678 else if ( bottom
> ch
)
5680 int h
= r
.GetHeight();
5682 for ( i
= row
-1; i
>= 0; i
-- )
5684 int rowHeight
= GetRowHeight(i
);
5685 if ( h
+ rowHeight
> ch
)
5692 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5693 // have rounding errors (this is important, because if we do, we
5694 // might not scroll at all and some cells won't be redrawn)
5695 ypos
+= GRID_SCROLL_LINE
/ 2;
5702 else if ( right
> cw
)
5704 int w
= r
.GetWidth();
5706 for ( i
= col
-1; i
>= 0; i
-- )
5708 int colWidth
= GetColWidth(i
);
5709 if ( w
+ colWidth
> cw
)
5716 // see comment for ypos above
5717 xpos
+= GRID_SCROLL_LINE
/ 2;
5720 if ( xpos
!= -1 || ypos
!= -1 )
5722 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5723 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5724 Scroll( xpos
, ypos
);
5732 // ------ Grid cursor movement functions
5735 bool wxGrid::MoveCursorUp()
5737 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5738 m_currentCellCoords
.GetRow() > 0 )
5740 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5741 m_currentCellCoords
.GetCol() );
5743 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5744 m_currentCellCoords
.GetCol() );
5753 bool wxGrid::MoveCursorDown()
5755 // TODO: allow for scrolling
5757 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5758 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5760 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5761 m_currentCellCoords
.GetCol() );
5763 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5764 m_currentCellCoords
.GetCol() );
5773 bool wxGrid::MoveCursorLeft()
5775 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5776 m_currentCellCoords
.GetCol() > 0 )
5778 MakeCellVisible( m_currentCellCoords
.GetRow(),
5779 m_currentCellCoords
.GetCol() - 1 );
5781 SetCurrentCell( m_currentCellCoords
.GetRow(),
5782 m_currentCellCoords
.GetCol() - 1 );
5791 bool wxGrid::MoveCursorRight()
5793 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5794 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5796 MakeCellVisible( m_currentCellCoords
.GetRow(),
5797 m_currentCellCoords
.GetCol() + 1 );
5799 SetCurrentCell( m_currentCellCoords
.GetRow(),
5800 m_currentCellCoords
.GetCol() + 1 );
5809 bool wxGrid::MovePageUp()
5811 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5813 int row
= m_currentCellCoords
.GetRow();
5817 m_gridWin
->GetClientSize( &cw
, &ch
);
5819 int y
= GetRowTop(row
);
5820 int newRow
= YToRow( y
- ch
+ 1 );
5825 else if ( newRow
== row
)
5830 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5831 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5839 bool wxGrid::MovePageDown()
5841 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5843 int row
= m_currentCellCoords
.GetRow();
5844 if ( row
< m_numRows
)
5847 m_gridWin
->GetClientSize( &cw
, &ch
);
5849 int y
= GetRowTop(row
);
5850 int newRow
= YToRow( y
+ ch
);
5853 newRow
= m_numRows
- 1;
5855 else if ( newRow
== row
)
5860 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5861 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5869 bool wxGrid::MoveCursorUpBlock()
5872 m_currentCellCoords
!= wxGridNoCellCoords
&&
5873 m_currentCellCoords
.GetRow() > 0 )
5875 int row
= m_currentCellCoords
.GetRow();
5876 int col
= m_currentCellCoords
.GetCol();
5878 if ( m_table
->IsEmptyCell(row
, col
) )
5880 // starting in an empty cell: find the next block of
5886 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5889 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5891 // starting at the top of a block: find the next block
5897 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5902 // starting within a block: find the top of the block
5907 if ( m_table
->IsEmptyCell(row
, col
) )
5915 MakeCellVisible( row
, col
);
5916 SetCurrentCell( row
, col
);
5924 bool wxGrid::MoveCursorDownBlock()
5927 m_currentCellCoords
!= wxGridNoCellCoords
&&
5928 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5930 int row
= m_currentCellCoords
.GetRow();
5931 int col
= m_currentCellCoords
.GetCol();
5933 if ( m_table
->IsEmptyCell(row
, col
) )
5935 // starting in an empty cell: find the next block of
5938 while ( row
< m_numRows
-1 )
5941 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5944 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5946 // starting at the bottom of a block: find the next block
5949 while ( row
< m_numRows
-1 )
5952 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5957 // starting within a block: find the bottom of the block
5959 while ( row
< m_numRows
-1 )
5962 if ( m_table
->IsEmptyCell(row
, col
) )
5970 MakeCellVisible( row
, col
);
5971 SetCurrentCell( row
, col
);
5979 bool wxGrid::MoveCursorLeftBlock()
5982 m_currentCellCoords
!= wxGridNoCellCoords
&&
5983 m_currentCellCoords
.GetCol() > 0 )
5985 int row
= m_currentCellCoords
.GetRow();
5986 int col
= m_currentCellCoords
.GetCol();
5988 if ( m_table
->IsEmptyCell(row
, col
) )
5990 // starting in an empty cell: find the next block of
5996 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5999 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6001 // starting at the left of a block: find the next block
6007 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6012 // starting within a block: find the left of the block
6017 if ( m_table
->IsEmptyCell(row
, col
) )
6025 MakeCellVisible( row
, col
);
6026 SetCurrentCell( row
, col
);
6034 bool wxGrid::MoveCursorRightBlock()
6037 m_currentCellCoords
!= wxGridNoCellCoords
&&
6038 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6040 int row
= m_currentCellCoords
.GetRow();
6041 int col
= m_currentCellCoords
.GetCol();
6043 if ( m_table
->IsEmptyCell(row
, col
) )
6045 // starting in an empty cell: find the next block of
6048 while ( col
< m_numCols
-1 )
6051 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6054 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6056 // starting at the right of a block: find the next block
6059 while ( col
< m_numCols
-1 )
6062 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6067 // starting within a block: find the right of the block
6069 while ( col
< m_numCols
-1 )
6072 if ( m_table
->IsEmptyCell(row
, col
) )
6080 MakeCellVisible( row
, col
);
6081 SetCurrentCell( row
, col
);
6092 // ------ Label values and formatting
6095 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6097 *horiz
= m_rowLabelHorizAlign
;
6098 *vert
= m_rowLabelVertAlign
;
6101 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6103 *horiz
= m_colLabelHorizAlign
;
6104 *vert
= m_colLabelVertAlign
;
6107 wxString
wxGrid::GetRowLabelValue( int row
)
6111 return m_table
->GetRowLabelValue( row
);
6121 wxString
wxGrid::GetColLabelValue( int col
)
6125 return m_table
->GetColLabelValue( col
);
6136 void wxGrid::SetRowLabelSize( int width
)
6138 width
= wxMax( width
, 0 );
6139 if ( width
!= m_rowLabelWidth
)
6143 m_rowLabelWin
->Show( FALSE
);
6144 m_cornerLabelWin
->Show( FALSE
);
6146 else if ( m_rowLabelWidth
== 0 )
6148 m_rowLabelWin
->Show( TRUE
);
6149 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6152 m_rowLabelWidth
= width
;
6159 void wxGrid::SetColLabelSize( int height
)
6161 height
= wxMax( height
, 0 );
6162 if ( height
!= m_colLabelHeight
)
6166 m_colLabelWin
->Show( FALSE
);
6167 m_cornerLabelWin
->Show( FALSE
);
6169 else if ( m_colLabelHeight
== 0 )
6171 m_colLabelWin
->Show( TRUE
);
6172 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6175 m_colLabelHeight
= height
;
6182 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6184 if ( m_labelBackgroundColour
!= colour
)
6186 m_labelBackgroundColour
= colour
;
6187 m_rowLabelWin
->SetBackgroundColour( colour
);
6188 m_colLabelWin
->SetBackgroundColour( colour
);
6189 m_cornerLabelWin
->SetBackgroundColour( colour
);
6191 if ( !GetBatchCount() )
6193 m_rowLabelWin
->Refresh();
6194 m_colLabelWin
->Refresh();
6195 m_cornerLabelWin
->Refresh();
6200 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6202 if ( m_labelTextColour
!= colour
)
6204 m_labelTextColour
= colour
;
6205 if ( !GetBatchCount() )
6207 m_rowLabelWin
->Refresh();
6208 m_colLabelWin
->Refresh();
6213 void wxGrid::SetLabelFont( const wxFont
& font
)
6216 if ( !GetBatchCount() )
6218 m_rowLabelWin
->Refresh();
6219 m_colLabelWin
->Refresh();
6223 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6225 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6227 m_rowLabelHorizAlign
= horiz
;
6230 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6232 m_rowLabelVertAlign
= vert
;
6235 if ( !GetBatchCount() )
6237 m_rowLabelWin
->Refresh();
6241 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6243 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6245 m_colLabelHorizAlign
= horiz
;
6248 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6250 m_colLabelVertAlign
= vert
;
6253 if ( !GetBatchCount() )
6255 m_colLabelWin
->Refresh();
6259 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6263 m_table
->SetRowLabelValue( row
, s
);
6264 if ( !GetBatchCount() )
6266 wxRect rect
= CellToRect( row
, 0);
6267 if ( rect
.height
> 0 )
6269 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6271 rect
.width
= m_rowLabelWidth
;
6272 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6278 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6282 m_table
->SetColLabelValue( col
, s
);
6283 if ( !GetBatchCount() )
6285 wxRect rect
= CellToRect( 0, col
);
6286 if ( rect
.width
> 0 )
6288 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6290 rect
.height
= m_colLabelHeight
;
6291 m_colLabelWin
->Refresh( TRUE
, &rect
);
6297 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6299 if ( m_gridLineColour
!= colour
)
6301 m_gridLineColour
= colour
;
6303 wxClientDC
dc( m_gridWin
);
6305 DrawAllGridLines( dc
, wxRegion() );
6309 void wxGrid::EnableGridLines( bool enable
)
6311 if ( enable
!= m_gridLinesEnabled
)
6313 m_gridLinesEnabled
= enable
;
6315 if ( !GetBatchCount() )
6319 wxClientDC
dc( m_gridWin
);
6321 DrawAllGridLines( dc
, wxRegion() );
6325 m_gridWin
->Refresh();
6332 int wxGrid::GetDefaultRowSize()
6334 return m_defaultRowHeight
;
6337 int wxGrid::GetRowSize( int row
)
6339 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6341 return GetRowHeight(row
);
6344 int wxGrid::GetDefaultColSize()
6346 return m_defaultColWidth
;
6349 int wxGrid::GetColSize( int col
)
6351 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6353 return GetColWidth(col
);
6356 // ============================================================================
6357 // access to the grid attributes: each of them has a default value in the grid
6358 // itself and may be overidden on a per-cell basis
6359 // ============================================================================
6361 // ----------------------------------------------------------------------------
6362 // setting default attributes
6363 // ----------------------------------------------------------------------------
6365 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6367 m_defaultCellAttr
->SetBackgroundColour(col
);
6369 m_gridWin
->SetBackgroundColour(col
);
6373 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6375 m_defaultCellAttr
->SetTextColour(col
);
6378 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6380 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6383 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6385 m_defaultCellAttr
->SetFont(font
);
6388 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6390 m_defaultCellAttr
->SetRenderer(renderer
);
6393 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6395 m_defaultCellAttr
->SetEditor(editor
);
6398 // ----------------------------------------------------------------------------
6399 // access to the default attrbiutes
6400 // ----------------------------------------------------------------------------
6402 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6404 return m_defaultCellAttr
->GetBackgroundColour();
6407 wxColour
wxGrid::GetDefaultCellTextColour()
6409 return m_defaultCellAttr
->GetTextColour();
6412 wxFont
wxGrid::GetDefaultCellFont()
6414 return m_defaultCellAttr
->GetFont();
6417 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6419 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6422 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6424 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6427 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6429 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6432 // ----------------------------------------------------------------------------
6433 // access to cell attributes
6434 // ----------------------------------------------------------------------------
6436 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6438 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6439 wxColour colour
= attr
->GetBackgroundColour();
6444 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6446 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6447 wxColour colour
= attr
->GetTextColour();
6452 wxFont
wxGrid::GetCellFont( int row
, int col
)
6454 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6455 wxFont font
= attr
->GetFont();
6460 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6462 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6463 attr
->GetAlignment(horiz
, vert
);
6467 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6469 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6470 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6475 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6477 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6478 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6483 bool wxGrid::IsReadOnly(int row
, int col
) const
6485 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6486 bool isReadOnly
= attr
->IsReadOnly();
6491 // ----------------------------------------------------------------------------
6492 // attribute support: cache, automatic provider creation, ...
6493 // ----------------------------------------------------------------------------
6495 bool wxGrid::CanHaveAttributes()
6502 return m_table
->CanHaveAttributes();
6505 void wxGrid::ClearAttrCache()
6507 if ( m_attrCache
.row
!= -1 )
6509 m_attrCache
.attr
->SafeDecRef();
6510 m_attrCache
.row
= -1;
6514 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6516 wxGrid
*self
= (wxGrid
*)this; // const_cast
6518 self
->ClearAttrCache();
6519 self
->m_attrCache
.row
= row
;
6520 self
->m_attrCache
.col
= col
;
6521 self
->m_attrCache
.attr
= attr
;
6525 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6527 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6529 *attr
= m_attrCache
.attr
;
6530 (*attr
)->SafeIncRef();
6532 #ifdef DEBUG_ATTR_CACHE
6533 gs_nAttrCacheHits
++;
6540 #ifdef DEBUG_ATTR_CACHE
6541 gs_nAttrCacheMisses
++;
6547 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6549 wxGridCellAttr
*attr
;
6550 if ( !LookupAttr(row
, col
, &attr
) )
6552 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6553 CacheAttr(row
, col
, attr
);
6557 attr
->SetDefAttr(m_defaultCellAttr
);
6561 attr
= m_defaultCellAttr
;
6568 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6570 wxGridCellAttr
*attr
;
6571 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6573 wxASSERT_MSG( m_table
,
6574 _T("we may only be called if CanHaveAttributes() "
6575 "returned TRUE and then m_table should be !NULL") );
6577 attr
= m_table
->GetAttr(row
, col
);
6580 attr
= new wxGridCellAttr
;
6582 // artificially inc the ref count to match DecRef() in caller
6585 m_table
->SetAttr(attr
, row
, col
);
6588 CacheAttr(row
, col
, attr
);
6590 attr
->SetDefAttr(m_defaultCellAttr
);
6594 // ----------------------------------------------------------------------------
6595 // setting cell attributes: this is forwarded to the table
6596 // ----------------------------------------------------------------------------
6598 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6600 if ( CanHaveAttributes() )
6602 m_table
->SetRowAttr(attr
, row
);
6610 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6612 if ( CanHaveAttributes() )
6614 m_table
->SetColAttr(attr
, col
);
6622 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6624 if ( CanHaveAttributes() )
6626 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6627 attr
->SetBackgroundColour(colour
);
6632 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6634 if ( CanHaveAttributes() )
6636 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6637 attr
->SetTextColour(colour
);
6642 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6644 if ( CanHaveAttributes() )
6646 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6647 attr
->SetFont(font
);
6652 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6654 if ( CanHaveAttributes() )
6656 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6657 attr
->SetAlignment(horiz
, vert
);
6662 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6664 if ( CanHaveAttributes() )
6666 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6667 attr
->SetRenderer(renderer
);
6672 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6674 if ( CanHaveAttributes() )
6676 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6677 attr
->SetEditor(editor
);
6682 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6684 if ( CanHaveAttributes() )
6686 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6687 attr
->SetReadOnly(isReadOnly
);
6692 // ----------------------------------------------------------------------------
6693 // Data type registration
6694 // ----------------------------------------------------------------------------
6696 void wxGrid::RegisterDataType(const wxString
& typeName
,
6697 wxGridCellRenderer
* renderer
,
6698 wxGridCellEditor
* editor
)
6700 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6704 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6706 wxString typeName
= m_table
->GetTypeName(row
, col
);
6707 return GetDefaultEditorForType(typeName
);
6710 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6712 wxString typeName
= m_table
->GetTypeName(row
, col
);
6713 return GetDefaultRendererForType(typeName
);
6717 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6719 int index
= m_typeRegistry
->FindDataType(typeName
);
6721 // Should we force the failure here or let it fallback to string handling???
6722 // wxFAIL_MSG(wxT("Unknown data type name"));
6725 return m_typeRegistry
->GetEditor(index
);
6729 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6731 int index
= m_typeRegistry
->FindDataType(typeName
);
6733 // Should we force the failure here or let it fallback to string handling???
6734 // wxFAIL_MSG(wxT("Unknown data type name"));
6737 return m_typeRegistry
->GetRenderer(index
);
6741 // ----------------------------------------------------------------------------
6743 // ----------------------------------------------------------------------------
6745 void wxGrid::EnableDragRowSize( bool enable
)
6747 m_canDragRowSize
= enable
;
6751 void wxGrid::EnableDragColSize( bool enable
)
6753 m_canDragColSize
= enable
;
6756 void wxGrid::EnableDragGridSize( bool enable
)
6758 m_canDragGridSize
= enable
;
6762 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6764 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6766 if ( resizeExistingRows
)
6774 void wxGrid::SetRowSize( int row
, int height
)
6776 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6778 if ( m_rowHeights
.IsEmpty() )
6780 // need to really create the array
6784 int h
= wxMax( 0, height
);
6785 int diff
= h
- m_rowHeights
[row
];
6787 m_rowHeights
[row
] = h
;
6789 for ( i
= row
; i
< m_numRows
; i
++ )
6791 m_rowBottoms
[i
] += diff
;
6796 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6798 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6800 if ( resizeExistingCols
)
6808 void wxGrid::SetColSize( int col
, int width
)
6810 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6812 // should we check that it's bigger than GetColMinimalWidth(col) here?
6814 if ( m_colWidths
.IsEmpty() )
6816 // need to really create the array
6820 int w
= wxMax( 0, width
);
6821 int diff
= w
- m_colWidths
[col
];
6822 m_colWidths
[col
] = w
;
6825 for ( i
= col
; i
< m_numCols
; i
++ )
6827 m_colRights
[i
] += diff
;
6833 void wxGrid::SetColMinimalWidth( int col
, int width
)
6835 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6838 int wxGrid::GetColMinimalWidth(int col
) const
6840 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6841 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6844 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6846 wxClientDC
dc(m_gridWin
);
6848 wxCoord width
, widthMax
= 0;
6849 for ( int row
= 0; row
< m_numRows
; row
++ )
6851 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6852 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6855 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6856 if ( width
> widthMax
)
6865 // now also compare with the column label width
6866 dc
.SetFont( GetLabelFont() );
6867 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6868 if ( width
> widthMax
)
6875 // empty column - give default width (notice that if widthMax is less
6876 // than default width but != 0, it's ok)
6877 widthMax
= m_defaultColWidth
;
6881 // leave some space around text
6885 SetColSize(col
, widthMax
);
6888 SetColMinimalWidth(col
, widthMax
);
6892 void wxGrid::AutoSizeColumns( bool setAsMin
)
6894 for ( int col
= 0; col
< m_numCols
; col
++ )
6896 AutoSizeColumn(col
, setAsMin
);
6901 // ------ cell value accessor functions
6904 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6908 m_table
->SetValue( row
, col
, s
.c_str() );
6909 if ( !GetBatchCount() )
6911 wxClientDC
dc( m_gridWin
);
6913 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6916 if ( m_currentCellCoords
.GetRow() == row
&&
6917 m_currentCellCoords
.GetCol() == col
&&
6918 IsCellEditControlEnabled())
6920 HideCellEditControl();
6921 ShowCellEditControl(); // will reread data from table
6928 // ------ Block, row and col selection
6931 void wxGrid::SelectRow( int row
, bool addToSelected
)
6935 if ( IsSelection() && addToSelected
)
6938 bool need_refresh
[4];
6942 need_refresh
[3] = FALSE
;
6946 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6947 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6948 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6949 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6953 need_refresh
[0] = TRUE
;
6954 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6955 wxGridCellCoords ( oldTop
- 1,
6957 m_selectedTopLeft
.SetRow( row
);
6962 need_refresh
[1] = TRUE
;
6963 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6964 wxGridCellCoords ( oldBottom
,
6967 m_selectedTopLeft
.SetCol( 0 );
6970 if ( oldBottom
< row
)
6972 need_refresh
[2] = TRUE
;
6973 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6974 wxGridCellCoords ( row
,
6976 m_selectedBottomRight
.SetRow( row
);
6979 if ( oldRight
< m_numCols
- 1 )
6981 need_refresh
[3] = TRUE
;
6982 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6984 wxGridCellCoords ( oldBottom
,
6986 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6989 for (i
= 0; i
< 4; i
++ )
6990 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6991 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6995 r
= SelectionToDeviceRect();
6997 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6999 m_selectedTopLeft
.Set( row
, 0 );
7000 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7001 r
= SelectionToDeviceRect();
7002 m_gridWin
->Refresh( FALSE
, &r
);
7005 wxGridRangeSelectEvent
gridEvt( GetId(),
7006 wxEVT_GRID_RANGE_SELECT
,
7009 m_selectedBottomRight
);
7011 GetEventHandler()->ProcessEvent(gridEvt
);
7015 void wxGrid::SelectCol( int col
, bool addToSelected
)
7017 if ( IsSelection() && addToSelected
)
7020 bool need_refresh
[4];
7024 need_refresh
[3] = FALSE
;
7027 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7028 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7029 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7030 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7032 if ( oldLeft
> col
)
7034 need_refresh
[0] = TRUE
;
7035 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7036 wxGridCellCoords ( m_numRows
- 1,
7038 m_selectedTopLeft
.SetCol( col
);
7043 need_refresh
[1] = TRUE
;
7044 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7045 wxGridCellCoords ( oldTop
- 1,
7047 m_selectedTopLeft
.SetRow( 0 );
7050 if ( oldRight
< col
)
7052 need_refresh
[2] = TRUE
;
7053 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7054 wxGridCellCoords ( m_numRows
- 1,
7056 m_selectedBottomRight
.SetCol( col
);
7059 if ( oldBottom
< m_numRows
- 1 )
7061 need_refresh
[3] = TRUE
;
7062 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7064 wxGridCellCoords ( m_numRows
- 1,
7066 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7069 for (i
= 0; i
< 4; i
++ )
7070 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7071 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7077 r
= SelectionToDeviceRect();
7079 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7081 m_selectedTopLeft
.Set( 0, col
);
7082 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7083 r
= SelectionToDeviceRect();
7084 m_gridWin
->Refresh( FALSE
, &r
);
7087 wxGridRangeSelectEvent
gridEvt( GetId(),
7088 wxEVT_GRID_RANGE_SELECT
,
7091 m_selectedBottomRight
);
7093 GetEventHandler()->ProcessEvent(gridEvt
);
7097 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7100 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7102 if ( topRow
> bottomRow
)
7109 if ( leftCol
> rightCol
)
7116 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7117 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7119 if ( m_selectedTopLeft
!= updateTopLeft
||
7120 m_selectedBottomRight
!= updateBottomRight
)
7122 // Compute two optimal update rectangles:
7123 // Either one rectangle is a real subset of the
7124 // other, or they are (almost) disjoint!
7126 bool need_refresh
[4];
7130 need_refresh
[3] = FALSE
;
7133 // Store intermediate values
7134 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7135 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7136 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7137 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7139 // Determine the outer/inner coordinates.
7140 if (oldLeft
> leftCol
)
7146 if (oldTop
> topRow
)
7152 if (oldRight
< rightCol
)
7155 oldRight
= rightCol
;
7158 if (oldBottom
< bottomRow
)
7161 oldBottom
= bottomRow
;
7165 // Now, either the stuff marked old is the outer
7166 // rectangle or we don't have a situation where one
7167 // is contained in the other.
7169 if ( oldLeft
< leftCol
)
7171 need_refresh
[0] = TRUE
;
7172 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7174 wxGridCellCoords ( oldBottom
,
7178 if ( oldTop
< topRow
)
7180 need_refresh
[1] = TRUE
;
7181 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7183 wxGridCellCoords ( topRow
- 1,
7187 if ( oldRight
> rightCol
)
7189 need_refresh
[2] = TRUE
;
7190 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7192 wxGridCellCoords ( oldBottom
,
7196 if ( oldBottom
> bottomRow
)
7198 need_refresh
[3] = TRUE
;
7199 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7201 wxGridCellCoords ( oldBottom
,
7207 m_selectedTopLeft
= updateTopLeft
;
7208 m_selectedBottomRight
= updateBottomRight
;
7210 // various Refresh() calls
7211 for (i
= 0; i
< 4; i
++ )
7212 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7213 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7216 // only generate an event if the block is not being selected by
7217 // dragging the mouse (in which case the event will be generated in
7218 // the mouse event handler)
7219 if ( !m_isDragging
)
7221 wxGridRangeSelectEvent
gridEvt( GetId(),
7222 wxEVT_GRID_RANGE_SELECT
,
7225 m_selectedBottomRight
);
7227 GetEventHandler()->ProcessEvent(gridEvt
);
7231 void wxGrid::SelectAll()
7233 m_selectedTopLeft
.Set( 0, 0 );
7234 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7236 m_gridWin
->Refresh();
7240 void wxGrid::ClearSelection()
7242 m_selectedTopLeft
= wxGridNoCellCoords
;
7243 m_selectedBottomRight
= wxGridNoCellCoords
;
7247 // This function returns the rectangle that encloses the given block
7248 // in device coords clipped to the client size of the grid window.
7250 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7251 const wxGridCellCoords
&bottomRight
)
7253 wxRect
rect( wxGridNoCellRect
);
7256 cellRect
= CellToRect( topLeft
);
7257 if ( cellRect
!= wxGridNoCellRect
)
7263 rect
= wxRect( 0, 0, 0, 0 );
7266 cellRect
= CellToRect( bottomRight
);
7267 if ( cellRect
!= wxGridNoCellRect
)
7273 return wxGridNoCellRect
;
7276 // convert to scrolled coords
7278 int left
, top
, right
, bottom
;
7279 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7280 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7283 m_gridWin
->GetClientSize( &cw
, &ch
);
7285 rect
.SetLeft( wxMax(0, left
) );
7286 rect
.SetTop( wxMax(0, top
) );
7287 rect
.SetRight( wxMin(cw
, right
) );
7288 rect
.SetBottom( wxMin(ch
, bottom
) );
7296 // ------ Grid event classes
7299 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7301 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7302 int row
, int col
, int x
, int y
,
7303 bool control
, bool shift
, bool alt
, bool meta
)
7304 : wxNotifyEvent( type
, id
)
7310 m_control
= control
;
7315 SetEventObject(obj
);
7319 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7321 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7322 int rowOrCol
, int x
, int y
,
7323 bool control
, bool shift
, bool alt
, bool meta
)
7324 : wxNotifyEvent( type
, id
)
7326 m_rowOrCol
= rowOrCol
;
7329 m_control
= control
;
7334 SetEventObject(obj
);
7338 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7340 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7341 const wxGridCellCoords
& topLeft
,
7342 const wxGridCellCoords
& bottomRight
,
7343 bool control
, bool shift
, bool alt
, bool meta
)
7344 : wxNotifyEvent( type
, id
)
7346 m_topLeft
= topLeft
;
7347 m_bottomRight
= bottomRight
;
7348 m_control
= control
;
7353 SetEventObject(obj
);
7357 #endif // ifndef wxUSE_NEW_GRID