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
{
259 wxGridDataTypeInfo(const wxString
& typeName
,
260 wxGridCellRenderer
* renderer
,
261 wxGridCellEditor
* editor
)
262 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
265 ~wxGridDataTypeInfo() { delete m_renderer
; delete m_editor
; }
268 wxGridCellRenderer
* m_renderer
;
269 wxGridCellEditor
* m_editor
;
273 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
276 class WXDLLEXPORT wxGridTypeRegistry
{
278 ~wxGridTypeRegistry();
279 void RegisterDataType(const wxString
& typeName
,
280 wxGridCellRenderer
* renderer
,
281 wxGridCellEditor
* editor
);
282 int FindDataType(const wxString
& typeName
);
283 wxGridCellRenderer
* GetRenderer(int index
);
284 wxGridCellEditor
* GetEditor(int index
);
287 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
->Destroy();
375 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
377 wxASSERT_MSG(m_control
,
378 wxT("The wxGridCellEditor must be Created first!"));
379 m_control
->Show(show
);
383 // set the colours/fonts if we have any
386 m_colFgOld
= m_control
->GetForegroundColour();
387 m_control
->SetForegroundColour(attr
->GetTextColour());
389 m_colBgOld
= m_control
->GetBackgroundColour();
390 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
392 m_fontOld
= m_control
->GetFont();
393 m_control
->SetFont(attr
->GetFont());
395 // can't do anything more in the base class version, the other
396 // attributes may only be used by the derived classes
401 // restore the standard colours fonts
402 if ( m_colFgOld
.Ok() )
404 m_control
->SetForegroundColour(m_colFgOld
);
405 m_colFgOld
= wxNullColour
;
408 if ( m_colBgOld
.Ok() )
410 m_control
->SetBackgroundColour(m_colBgOld
);
411 m_colBgOld
= wxNullColour
;
414 if ( m_fontOld
.Ok() )
416 m_control
->SetFont(m_fontOld
);
417 m_fontOld
= wxNullFont
;
422 void wxGridCellEditor::SetSize(const wxRect
& rect
)
424 wxASSERT_MSG(m_control
,
425 wxT("The wxGridCellEditor must be Created first!"));
426 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
429 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
435 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
440 void wxGridCellEditor::StartingClick()
444 // ----------------------------------------------------------------------------
445 // wxGridCellTextEditor
446 // ----------------------------------------------------------------------------
448 wxGridCellTextEditor::wxGridCellTextEditor()
452 void wxGridCellTextEditor::Create(wxWindow
* parent
,
454 wxEvtHandler
* evtHandler
)
456 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
457 wxDefaultPosition
, wxDefaultSize
458 #if defined(__WXMSW__)
459 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
463 wxGridCellEditor::Create(parent
, id
, evtHandler
);
466 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
467 wxGridCellAttr
* WXUNUSED(attr
))
469 // as we fill the entire client area, don't do anything here to minimize
473 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
475 wxRect
rect(rectOrig
);
477 // Make the edit control large enough to allow for internal
480 // TODO: remove this if the text ctrl sizing is improved esp. for
483 #if defined(__WXGTK__)
484 rect
.Inflate(rect
.x
? 1 : 0, rect
.y
? 1 : 0);
486 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
487 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
488 #if defined(__WXMOTIF__)
492 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
493 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
494 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
495 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
498 wxGridCellEditor::SetSize(rect
);
501 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
503 wxASSERT_MSG(m_control
,
504 wxT("The wxGridCellEditor must be Created first!"));
506 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
508 DoBeginEdit(m_startValue
);
511 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
513 Text()->SetValue(startValue
);
514 Text()->SetInsertionPointEnd();
518 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
521 wxASSERT_MSG(m_control
,
522 wxT("The wxGridCellEditor must be Created first!"));
524 bool changed
= FALSE
;
525 wxString value
= Text()->GetValue();
526 if (value
!= m_startValue
)
530 grid
->GetTable()->SetValue(row
, col
, value
);
532 m_startValue
= wxEmptyString
;
533 Text()->SetValue(m_startValue
);
539 void wxGridCellTextEditor::Reset()
541 wxASSERT_MSG(m_control
,
542 wxT("The wxGridCellEditor must be Created first!"));
544 DoReset(m_startValue
);
547 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
549 Text()->SetValue(startValue
);
550 Text()->SetInsertionPointEnd();
553 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
555 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
557 // insert the key in the control
558 long keycode
= event
.KeyCode();
559 if ( isprint(keycode
) )
561 // FIXME this is not going to work for non letters...
562 if ( !event
.ShiftDown() )
564 keycode
= tolower(keycode
);
567 Text()->AppendText((wxChar
)keycode
);
577 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
579 #if defined(__WXMOTIF__) || defined(__WXGTK__)
580 // wxMotif needs a little extra help...
581 int pos
= Text()->GetInsertionPoint();
582 wxString
s( Text()->GetValue() );
583 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
585 Text()->SetInsertionPoint( pos
);
587 // the other ports can handle a Return key press
593 // ----------------------------------------------------------------------------
594 // wxGridCellNumberEditor
595 // ----------------------------------------------------------------------------
597 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
603 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
605 wxEvtHandler
* evtHandler
)
609 // create a spin ctrl
610 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
611 wxDefaultPosition
, wxDefaultSize
,
615 wxGridCellEditor::Create(parent
, id
, evtHandler
);
619 // just a text control
620 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
623 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
624 #endif // wxUSE_VALIDATORS
628 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
630 // first get the value
631 wxGridTableBase
*table
= grid
->GetTable();
632 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
634 m_valueOld
= table
->GetValueAsLong(row
, col
);
638 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
645 Spin()->SetValue(m_valueOld
);
649 DoBeginEdit(GetString());
653 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
661 value
= Spin()->GetValue();
662 changed
= value
!= m_valueOld
;
666 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
671 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
677 void wxGridCellNumberEditor::Reset()
681 Spin()->SetValue(m_valueOld
);
685 DoReset(GetString());
689 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
693 long keycode
= event
.KeyCode();
694 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
696 wxGridCellTextEditor::StartingKey(event
);
706 // ----------------------------------------------------------------------------
707 // wxGridCellFloatEditor
708 // ----------------------------------------------------------------------------
710 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
712 wxEvtHandler
* evtHandler
)
714 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
717 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
718 #endif // wxUSE_VALIDATORS
721 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
723 // first get the value
724 wxGridTableBase
*table
= grid
->GetTable();
725 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
727 m_valueOld
= table
->GetValueAsDouble(row
, col
);
731 wxFAIL_MSG( _T("this cell doesn't have float value") );
736 DoBeginEdit(GetString());
739 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
743 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
745 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
755 void wxGridCellFloatEditor::Reset()
757 DoReset(GetString());
760 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
762 long keycode
= event
.KeyCode();
763 if ( isdigit(keycode
) ||
764 keycode
== '+' || keycode
== '-' || keycode
== '.' )
766 wxGridCellTextEditor::StartingKey(event
);
775 // ----------------------------------------------------------------------------
776 // wxGridCellBoolEditor
777 // ----------------------------------------------------------------------------
779 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
781 wxEvtHandler
* evtHandler
)
783 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
784 wxDefaultPosition
, wxDefaultSize
,
787 wxGridCellEditor::Create(parent
, id
, evtHandler
);
790 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
792 // position it in the centre of the rectangle (TODO: support alignment?)
794 m_control
->GetSize(&w
, &h
);
796 // the checkbox without label still has some space to the right in wxGTK,
797 // so shift it to the right
798 #if defined(__WXGTK__) || defined (__WXMOTIF__)
800 #endif // GTK && Motif
802 m_control
->Move(r
.x
+ r
.width
/2 - w
/2, r
.y
+ r
.height
/2 - h
/2);
805 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
807 m_control
->Show(show
);
811 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
812 CBox()->SetBackgroundColour(colBg
);
816 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
818 wxASSERT_MSG(m_control
,
819 wxT("The wxGridCellEditor must be Created first!"));
821 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
822 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
824 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
825 CBox()->SetValue(m_startValue
);
829 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
832 wxASSERT_MSG(m_control
,
833 wxT("The wxGridCellEditor must be Created first!"));
835 bool changed
= FALSE
;
836 bool value
= CBox()->GetValue();
837 if ( value
!= m_startValue
)
842 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
843 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
845 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
851 void wxGridCellBoolEditor::Reset()
853 wxASSERT_MSG(m_control
,
854 wxT("The wxGridCellEditor must be Created first!"));
856 CBox()->SetValue(m_startValue
);
859 void wxGridCellBoolEditor::StartingClick()
861 CBox()->SetValue(!CBox()->GetValue());
864 // ----------------------------------------------------------------------------
865 // wxGridCellChoiceEditor
866 // ----------------------------------------------------------------------------
868 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
869 const wxChar
* choices
[],
871 : m_allowOthers(allowOthers
)
873 m_choices
.Alloc(count
);
874 for ( size_t n
= 0; n
< count
; n
++ )
876 m_choices
.Add(choices
[n
]);
880 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
882 wxEvtHandler
* evtHandler
)
884 size_t count
= m_choices
.GetCount();
885 wxString
*choices
= new wxString
[count
];
886 for ( size_t n
= 0; n
< count
; n
++ )
888 choices
[n
] = m_choices
[n
];
891 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
892 wxDefaultPosition
, wxDefaultSize
,
894 m_allowOthers
? 0 : wxCB_READONLY
);
898 wxGridCellEditor::Create(parent
, id
, evtHandler
);
901 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
902 wxGridCellAttr
* WXUNUSED(attr
))
904 // as we fill the entire client area, don't do anything here to minimize
908 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
910 wxASSERT_MSG(m_control
,
911 wxT("The wxGridCellEditor must be Created first!"));
913 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
915 Combo()->SetValue(m_startValue
);
916 size_t count
= m_choices
.GetCount();
917 for (size_t i
=0; i
<count
; i
++)
919 if (m_startValue
== m_choices
[i
])
921 Combo()->SetSelection(i
);
925 Combo()->SetInsertionPointEnd();
929 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
932 wxString value
= Combo()->GetValue();
933 bool changed
= value
!= m_startValue
;
936 grid
->GetTable()->SetValue(row
, col
, value
);
938 m_startValue
= wxEmptyString
;
939 Combo()->SetValue(m_startValue
);
944 void wxGridCellChoiceEditor::Reset()
946 Combo()->SetValue(m_startValue
);
947 Combo()->SetInsertionPointEnd();
950 // ----------------------------------------------------------------------------
951 // wxGridCellEditorEvtHandler
952 // ----------------------------------------------------------------------------
954 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
956 switch ( event
.KeyCode() )
960 m_grid
->DisableCellEditControl();
964 event
.Skip( m_grid
->ProcessEvent( event
) );
968 if (!m_grid
->ProcessEvent(event
))
969 m_editor
->HandleReturn(event
);
978 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
980 switch ( event
.KeyCode() )
992 // ============================================================================
994 // ============================================================================
996 // ----------------------------------------------------------------------------
997 // wxGridCellRenderer
998 // ----------------------------------------------------------------------------
1000 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1001 wxGridCellAttr
& attr
,
1007 dc
.SetBackgroundMode( wxSOLID
);
1011 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1015 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1018 dc
.SetPen( *wxTRANSPARENT_PEN
);
1019 dc
.DrawRectangle(rect
);
1022 wxGridCellRenderer::~wxGridCellRenderer()
1026 // ----------------------------------------------------------------------------
1027 // wxGridCellStringRenderer
1028 // ----------------------------------------------------------------------------
1030 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1031 wxGridCellAttr
& attr
,
1035 dc
.SetBackgroundMode( wxTRANSPARENT
);
1037 // TODO some special colours for attr.IsReadOnly() case?
1041 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1042 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1046 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1047 dc
.SetTextForeground( attr
.GetTextColour() );
1050 dc
.SetFont( attr
.GetFont() );
1053 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1055 const wxString
& text
)
1058 dc
.SetFont(attr
.GetFont());
1059 dc
.GetTextExtent(text
, &x
, &y
);
1061 return wxSize(x
, y
);
1064 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1065 wxGridCellAttr
& attr
,
1069 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1072 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1073 wxGridCellAttr
& attr
,
1075 const wxRect
& rectCell
,
1079 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1081 // now we only have to draw the text
1082 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1085 attr
.GetAlignment(&hAlign
, &vAlign
);
1087 wxRect rect
= rectCell
;
1090 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1091 rect
, hAlign
, vAlign
);
1094 // ----------------------------------------------------------------------------
1095 // wxGridCellNumberRenderer
1096 // ----------------------------------------------------------------------------
1098 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1100 wxGridTableBase
*table
= grid
.GetTable();
1102 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1104 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1108 text
= table
->GetValue(row
, col
);
1114 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1115 wxGridCellAttr
& attr
,
1117 const wxRect
& rectCell
,
1121 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1123 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1125 // draw the text right aligned by default
1127 attr
.GetAlignment(&hAlign
, &vAlign
);
1130 wxRect rect
= rectCell
;
1133 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1136 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1137 wxGridCellAttr
& attr
,
1141 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1144 // ----------------------------------------------------------------------------
1145 // wxGridCellFloatRenderer
1146 // ----------------------------------------------------------------------------
1148 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1151 SetPrecision(precision
);
1154 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1156 wxGridTableBase
*table
= grid
.GetTable();
1158 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1162 m_format
.Printf(_T("%%%d.%d%%f"), m_width
, m_precision
);
1165 text
.Printf(m_format
, table
->GetValueAsDouble(row
, col
));
1169 text
= table
->GetValue(row
, col
);
1175 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1176 wxGridCellAttr
& attr
,
1178 const wxRect
& rectCell
,
1182 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1184 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1186 // draw the text right aligned by default
1188 attr
.GetAlignment(&hAlign
, &vAlign
);
1191 wxRect rect
= rectCell
;
1194 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1197 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1198 wxGridCellAttr
& attr
,
1202 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1205 // ----------------------------------------------------------------------------
1206 // wxGridCellBoolRenderer
1207 // ----------------------------------------------------------------------------
1209 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1211 // between checkmark and box
1212 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1214 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1215 wxGridCellAttr
& WXUNUSED(attr
),
1220 // compute it only once (no locks for MT safeness in GUI thread...)
1221 if ( !ms_sizeCheckMark
.x
)
1223 // get checkbox size
1224 wxCoord checkSize
= 0;
1225 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1226 wxSize size
= checkbox
->GetBestSize();
1227 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1229 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1230 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1231 checkSize
-= size
.y
/ 2;
1236 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1239 return ms_sizeCheckMark
;
1242 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1243 wxGridCellAttr
& attr
,
1249 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1251 // draw a check mark in the centre (ignoring alignment - TODO)
1252 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1254 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1255 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1256 rectMark
.width
= size
.x
;
1257 rectMark
.height
= size
.y
;
1259 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1260 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1261 dc
.DrawRectangle(rectMark
);
1263 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1266 if (grid
.GetTable()->CanGetValueAs(row
, col
, wxT("bool")))
1267 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1269 value
= !!grid
.GetTable()->GetValue(row
, col
);
1273 dc
.SetTextForeground(attr
.GetTextColour());
1274 dc
.DrawCheckMark(rectMark
);
1278 // ----------------------------------------------------------------------------
1280 // ----------------------------------------------------------------------------
1282 const wxColour
& wxGridCellAttr::GetTextColour() const
1284 if (HasTextColour())
1288 else if (m_defGridAttr
!= this)
1290 return m_defGridAttr
->GetTextColour();
1294 wxFAIL_MSG(wxT("Missing default cell attribute"));
1295 return wxNullColour
;
1300 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1302 if (HasBackgroundColour())
1304 else if (m_defGridAttr
!= this)
1305 return m_defGridAttr
->GetBackgroundColour();
1308 wxFAIL_MSG(wxT("Missing default cell attribute"));
1309 return wxNullColour
;
1314 const wxFont
& wxGridCellAttr::GetFont() const
1318 else if (m_defGridAttr
!= this)
1319 return m_defGridAttr
->GetFont();
1322 wxFAIL_MSG(wxT("Missing default cell attribute"));
1328 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1332 if ( hAlign
) *hAlign
= m_hAlign
;
1333 if ( vAlign
) *vAlign
= m_vAlign
;
1335 else if (m_defGridAttr
!= this)
1336 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1339 wxFAIL_MSG(wxT("Missing default cell attribute"));
1344 // GetRenderer and GetEditor use a slightly different decision path about
1345 // which attribute to use. If a non-default attr object has one then it is
1346 // used, otherwise the default editor or renderer is fetched from the grid and
1347 // used. It should be the default for the data type of the cell. If it is
1348 // NULL (because the table has a type that the grid does not have in its
1349 // registry,) then the grid's default editor or renderer is used.
1351 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1353 if ((m_defGridAttr
!= this || grid
== NULL
) && HasRenderer())
1354 return m_renderer
; // use local attribute
1356 wxGridCellRenderer
* renderer
= NULL
;
1357 if (grid
) // get renderer for the data type
1358 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1361 // if we still don't have one then use the grid default
1362 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1365 wxFAIL_MSG(wxT("Missing default cell attribute"));
1370 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1372 if ((m_defGridAttr
!= this || grid
== NULL
) && HasEditor())
1373 return m_editor
; // use local attribute
1375 wxGridCellEditor
* editor
= NULL
;
1376 if (grid
) // get renderer for the data type
1377 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1380 // if we still don't have one then use the grid default
1381 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1384 wxFAIL_MSG(wxT("Missing default cell attribute"));
1389 // ----------------------------------------------------------------------------
1390 // wxGridCellAttrData
1391 // ----------------------------------------------------------------------------
1393 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1395 int n
= FindIndex(row
, col
);
1396 if ( n
== wxNOT_FOUND
)
1398 // add the attribute
1399 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1405 // change the attribute
1406 m_attrs
[(size_t)n
].attr
= attr
;
1410 // remove this attribute
1411 m_attrs
.RemoveAt((size_t)n
);
1416 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1418 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1420 int n
= FindIndex(row
, col
);
1421 if ( n
!= wxNOT_FOUND
)
1423 attr
= m_attrs
[(size_t)n
].attr
;
1430 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1432 size_t count
= m_attrs
.GetCount();
1433 for ( size_t n
= 0; n
< count
; n
++ )
1435 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1436 wxCoord row
= coords
.GetRow();
1437 if ((size_t)row
>= pos
)
1441 // If rows inserted, include row counter where necessary
1442 coords
.SetRow(row
+ numRows
);
1444 else if (numRows
< 0)
1446 // If rows deleted ...
1447 if ((size_t)row
>= pos
- numRows
)
1449 // ...either decrement row counter (if row still exists)...
1450 coords
.SetRow(row
+ numRows
);
1454 // ...or remove the attribute
1455 m_attrs
.RemoveAt((size_t)n
);
1463 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1465 size_t count
= m_attrs
.GetCount();
1466 for ( size_t n
= 0; n
< count
; n
++ )
1468 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1469 wxCoord col
= coords
.GetCol();
1470 if ( (size_t)col
>= pos
)
1474 // If rows inserted, include row counter where necessary
1475 coords
.SetCol(col
+ numCols
);
1477 else if (numCols
< 0)
1479 // If rows deleted ...
1480 if ((size_t)col
>= pos
- numCols
)
1482 // ...either decrement row counter (if row still exists)...
1483 coords
.SetCol(col
+ numCols
);
1487 // ...or remove the attribute
1488 m_attrs
.RemoveAt((size_t)n
);
1496 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1498 size_t count
= m_attrs
.GetCount();
1499 for ( size_t n
= 0; n
< count
; n
++ )
1501 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1502 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1511 // ----------------------------------------------------------------------------
1512 // wxGridRowOrColAttrData
1513 // ----------------------------------------------------------------------------
1515 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1517 size_t count
= m_attrs
.Count();
1518 for ( size_t n
= 0; n
< count
; n
++ )
1520 m_attrs
[n
]->DecRef();
1524 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1526 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1528 int n
= m_rowsOrCols
.Index(rowOrCol
);
1529 if ( n
!= wxNOT_FOUND
)
1531 attr
= m_attrs
[(size_t)n
];
1538 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1540 int n
= m_rowsOrCols
.Index(rowOrCol
);
1541 if ( n
== wxNOT_FOUND
)
1543 // add the attribute
1544 m_rowsOrCols
.Add(rowOrCol
);
1551 // change the attribute
1552 m_attrs
[(size_t)n
] = attr
;
1556 // remove this attribute
1557 m_attrs
[(size_t)n
]->DecRef();
1558 m_rowsOrCols
.RemoveAt((size_t)n
);
1559 m_attrs
.RemoveAt((size_t)n
);
1564 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1566 size_t count
= m_attrs
.GetCount();
1567 for ( size_t n
= 0; n
< count
; n
++ )
1569 int & rowOrCol
= m_rowsOrCols
[n
];
1570 if ( (size_t)rowOrCol
>= pos
)
1572 if ( numRowsOrCols
> 0 )
1574 // If rows inserted, include row counter where necessary
1575 rowOrCol
+= numRowsOrCols
;
1577 else if ( numRowsOrCols
< 0)
1579 // If rows deleted, either decrement row counter (if row still exists)
1580 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1581 rowOrCol
+= numRowsOrCols
;
1584 m_rowsOrCols
.RemoveAt((size_t)n
);
1585 m_attrs
.RemoveAt((size_t)n
);
1593 // ----------------------------------------------------------------------------
1594 // wxGridCellAttrProvider
1595 // ----------------------------------------------------------------------------
1597 wxGridCellAttrProvider::wxGridCellAttrProvider()
1599 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1602 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1607 void wxGridCellAttrProvider::InitData()
1609 m_data
= new wxGridCellAttrProviderData
;
1612 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1614 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1617 // first look for the attribute of this specific cell
1618 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1622 // then look for the col attr (col attributes are more common than
1623 // the row ones, hence they have priority)
1624 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1629 // finally try the row attributes
1630 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1637 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1643 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1646 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1651 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1654 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1659 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1662 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1666 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1668 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1672 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1676 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1678 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
1682 // ----------------------------------------------------------------------------
1683 // wxGridTypeRegistry
1684 // ----------------------------------------------------------------------------
1686 wxGridTypeRegistry::~wxGridTypeRegistry()
1688 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++)
1689 delete m_typeinfo
[i
];
1693 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
1694 wxGridCellRenderer
* renderer
,
1695 wxGridCellEditor
* editor
)
1698 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
1700 // is it already registered?
1701 if ((loc
= FindDataType(typeName
)) != -1) {
1702 delete m_typeinfo
[loc
];
1703 m_typeinfo
[loc
] = info
;
1706 m_typeinfo
.Add(info
);
1710 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
1714 for (size_t i
=0; i
<m_typeinfo
.Count(); i
++) {
1715 if (typeName
== m_typeinfo
[i
]->m_typeName
) {
1724 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
1726 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
1730 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
1732 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
1736 // ----------------------------------------------------------------------------
1738 // ----------------------------------------------------------------------------
1740 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1743 wxGridTableBase::wxGridTableBase()
1745 m_view
= (wxGrid
*) NULL
;
1746 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
1749 wxGridTableBase::~wxGridTableBase()
1751 delete m_attrProvider
;
1754 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1756 delete m_attrProvider
;
1757 m_attrProvider
= attrProvider
;
1760 bool wxGridTableBase::CanHaveAttributes()
1762 if ( ! GetAttrProvider() )
1764 // use the default attr provider by default
1765 SetAttrProvider(new wxGridCellAttrProvider
);
1770 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
1772 if ( m_attrProvider
)
1773 return m_attrProvider
->GetAttr(row
, col
);
1775 return (wxGridCellAttr
*)NULL
;
1778 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1780 if ( m_attrProvider
)
1782 m_attrProvider
->SetAttr(attr
, row
, col
);
1786 // as we take ownership of the pointer and don't store it, we must
1792 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1794 if ( m_attrProvider
)
1796 m_attrProvider
->SetRowAttr(attr
, row
);
1800 // as we take ownership of the pointer and don't store it, we must
1806 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1808 if ( m_attrProvider
)
1810 m_attrProvider
->SetColAttr(attr
, col
);
1814 // as we take ownership of the pointer and don't store it, we must
1820 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1822 if ( m_attrProvider
)
1824 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1828 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1830 if ( m_attrProvider
)
1832 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1836 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1838 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1839 "but your derived table class does not override this function") );
1844 bool wxGridTableBase::AppendRows( size_t numRows
)
1846 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1847 "but your derived table class does not override this function"));
1852 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1854 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1855 "but your derived table class does not override this function"));
1860 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1862 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1863 "but your derived table class does not override this function"));
1868 bool wxGridTableBase::AppendCols( size_t numCols
)
1870 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1871 "but your derived table class does not override this function"));
1876 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1878 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1879 "but your derived table class does not override this function"));
1885 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1888 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
1889 // how much it makes sense to us geeks.
1893 wxString
wxGridTableBase::GetColLabelValue( int col
)
1895 // default col labels are:
1896 // cols 0 to 25 : A-Z
1897 // cols 26 to 675 : AA-ZZ
1902 for ( n
= 1; ; n
++ )
1904 s
+= (_T('A') + (wxChar
)( col%26
));
1906 if ( col
< 0 ) break;
1909 // reverse the string...
1911 for ( i
= 0; i
< n
; i
++ )
1920 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1922 return wxGRID_VALUE_STRING
;
1925 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1926 const wxString
& typeName
)
1928 return typeName
== wxGRID_VALUE_STRING
;
1931 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1933 return CanGetValueAs(row
, col
, typeName
);
1936 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1941 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1946 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1951 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1952 long WXUNUSED(value
) )
1956 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1957 double WXUNUSED(value
) )
1961 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1962 bool WXUNUSED(value
) )
1967 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1968 const wxString
& WXUNUSED(typeName
) )
1973 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1974 const wxString
& WXUNUSED(typeName
),
1975 void* WXUNUSED(value
) )
1980 //////////////////////////////////////////////////////////////////////
1982 // Message class for the grid table to send requests and notifications
1986 wxGridTableMessage::wxGridTableMessage()
1988 m_table
= (wxGridTableBase
*) NULL
;
1994 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1995 int commandInt1
, int commandInt2
)
1999 m_comInt1
= commandInt1
;
2000 m_comInt2
= commandInt2
;
2005 //////////////////////////////////////////////////////////////////////
2007 // A basic grid table for string data. An object of this class will
2008 // created by wxGrid if you don't specify an alternative table class.
2011 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2013 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2015 wxGridStringTable::wxGridStringTable()
2020 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2025 m_data
.Alloc( numRows
);
2028 sa
.Alloc( numCols
);
2029 for ( col
= 0; col
< numCols
; col
++ )
2031 sa
.Add( wxEmptyString
);
2034 for ( row
= 0; row
< numRows
; row
++ )
2040 wxGridStringTable::~wxGridStringTable()
2044 long wxGridStringTable::GetNumberRows()
2046 return m_data
.GetCount();
2049 long wxGridStringTable::GetNumberCols()
2051 if ( m_data
.GetCount() > 0 )
2052 return m_data
[0].GetCount();
2057 wxString
wxGridStringTable::GetValue( int row
, int col
)
2059 // TODO: bounds checking
2061 return m_data
[row
][col
];
2064 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2066 // TODO: bounds checking
2068 m_data
[row
][col
] = value
;
2071 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2073 // TODO: bounds checking
2075 return (m_data
[row
][col
] == wxEmptyString
);
2079 void wxGridStringTable::Clear()
2082 int numRows
, numCols
;
2084 numRows
= m_data
.GetCount();
2087 numCols
= m_data
[0].GetCount();
2089 for ( row
= 0; row
< numRows
; row
++ )
2091 for ( col
= 0; col
< numCols
; col
++ )
2093 m_data
[row
][col
] = wxEmptyString
;
2100 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2104 size_t curNumRows
= m_data
.GetCount();
2105 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2107 if ( pos
>= curNumRows
)
2109 return AppendRows( numRows
);
2113 sa
.Alloc( curNumCols
);
2114 for ( col
= 0; col
< curNumCols
; col
++ )
2116 sa
.Add( wxEmptyString
);
2119 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2121 m_data
.Insert( sa
, row
);
2123 UpdateAttrRows( pos
, numRows
);
2126 wxGridTableMessage
msg( this,
2127 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2131 GetView()->ProcessTableMessage( msg
);
2137 bool wxGridStringTable::AppendRows( size_t numRows
)
2141 size_t curNumRows
= m_data
.GetCount();
2142 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2145 if ( curNumCols
> 0 )
2147 sa
.Alloc( curNumCols
);
2148 for ( col
= 0; col
< curNumCols
; col
++ )
2150 sa
.Add( wxEmptyString
);
2154 for ( row
= 0; row
< numRows
; row
++ )
2161 wxGridTableMessage
msg( this,
2162 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2165 GetView()->ProcessTableMessage( msg
);
2171 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2175 size_t curNumRows
= m_data
.GetCount();
2177 if ( pos
>= curNumRows
)
2180 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2181 "Pos value is invalid for present table with %d rows",
2182 pos
, numRows
, curNumRows
);
2183 wxFAIL_MSG( wxT(errmsg
) );
2187 if ( numRows
> curNumRows
- pos
)
2189 numRows
= curNumRows
- pos
;
2192 if ( numRows
>= curNumRows
)
2194 m_data
.Empty(); // don't release memory just yet
2198 for ( n
= 0; n
< numRows
; n
++ )
2200 m_data
.Remove( pos
);
2203 UpdateAttrRows( pos
, -((int)numRows
) );
2206 wxGridTableMessage
msg( this,
2207 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2211 GetView()->ProcessTableMessage( msg
);
2217 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2221 size_t curNumRows
= m_data
.GetCount();
2222 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2224 if ( pos
>= curNumCols
)
2226 return AppendCols( numCols
);
2229 for ( row
= 0; row
< curNumRows
; row
++ )
2231 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2233 m_data
[row
].Insert( wxEmptyString
, col
);
2236 UpdateAttrCols( pos
, numCols
);
2239 wxGridTableMessage
msg( this,
2240 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2244 GetView()->ProcessTableMessage( msg
);
2250 bool wxGridStringTable::AppendCols( size_t numCols
)
2254 size_t curNumRows
= m_data
.GetCount();
2257 // TODO: something better than this ?
2259 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2260 "Call AppendRows() first") );
2264 for ( row
= 0; row
< curNumRows
; row
++ )
2266 for ( n
= 0; n
< numCols
; n
++ )
2268 m_data
[row
].Add( wxEmptyString
);
2274 wxGridTableMessage
msg( this,
2275 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2278 GetView()->ProcessTableMessage( msg
);
2284 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2288 size_t curNumRows
= m_data
.GetCount();
2289 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2291 if ( pos
>= curNumCols
)
2294 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2295 "Pos value is invalid for present table with %d cols",
2296 pos
, numCols
, curNumCols
);
2297 wxFAIL_MSG( wxT( errmsg
) );
2301 if ( numCols
> curNumCols
- pos
)
2303 numCols
= curNumCols
- pos
;
2306 for ( row
= 0; row
< curNumRows
; row
++ )
2308 if ( numCols
>= curNumCols
)
2310 m_data
[row
].Clear();
2314 for ( n
= 0; n
< numCols
; n
++ )
2316 m_data
[row
].Remove( pos
);
2320 UpdateAttrCols( pos
, -((int)numCols
) );
2323 wxGridTableMessage
msg( this,
2324 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2328 GetView()->ProcessTableMessage( msg
);
2334 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2336 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2338 // using default label
2340 return wxGridTableBase::GetRowLabelValue( row
);
2344 return m_rowLabels
[ row
];
2348 wxString
wxGridStringTable::GetColLabelValue( int col
)
2350 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2352 // using default label
2354 return wxGridTableBase::GetColLabelValue( col
);
2358 return m_colLabels
[ col
];
2362 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2364 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2366 int n
= m_rowLabels
.GetCount();
2368 for ( i
= n
; i
<= row
; i
++ )
2370 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2374 m_rowLabels
[row
] = value
;
2377 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2379 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2381 int n
= m_colLabels
.GetCount();
2383 for ( i
= n
; i
<= col
; i
++ )
2385 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2389 m_colLabels
[col
] = value
;
2394 //////////////////////////////////////////////////////////////////////
2395 //////////////////////////////////////////////////////////////////////
2397 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2399 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2400 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2401 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2402 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2405 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2407 const wxPoint
&pos
, const wxSize
&size
)
2408 : wxWindow( parent
, id
, pos
, size
)
2413 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2417 // NO - don't do this because it will set both the x and y origin
2418 // coords to match the parent scrolled window and we just want to
2419 // set the y coord - MB
2421 // m_owner->PrepareDC( dc );
2424 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2425 dc
.SetDeviceOrigin( 0, -y
);
2427 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2428 m_owner
->DrawRowLabels( dc
);
2432 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2434 m_owner
->ProcessRowLabelMouseEvent( event
);
2438 // This seems to be required for wxMotif otherwise the mouse
2439 // cursor must be in the cell edit control to get key events
2441 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2443 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2448 //////////////////////////////////////////////////////////////////////
2450 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2452 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2453 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2454 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2455 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2458 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2460 const wxPoint
&pos
, const wxSize
&size
)
2461 : wxWindow( parent
, id
, pos
, size
)
2466 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2470 // NO - don't do this because it will set both the x and y origin
2471 // coords to match the parent scrolled window and we just want to
2472 // set the x coord - MB
2474 // m_owner->PrepareDC( dc );
2477 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2478 dc
.SetDeviceOrigin( -x
, 0 );
2480 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2481 m_owner
->DrawColLabels( dc
);
2485 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2487 m_owner
->ProcessColLabelMouseEvent( event
);
2491 // This seems to be required for wxMotif otherwise the mouse
2492 // cursor must be in the cell edit control to get key events
2494 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2496 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2501 //////////////////////////////////////////////////////////////////////
2503 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2505 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2506 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2507 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2508 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2511 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2513 const wxPoint
&pos
, const wxSize
&size
)
2514 : wxWindow( parent
, id
, pos
, size
)
2519 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2523 int client_height
= 0;
2524 int client_width
= 0;
2525 GetClientSize( &client_width
, &client_height
);
2527 dc
.SetPen( *wxBLACK_PEN
);
2528 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2529 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2531 dc
.SetPen( *wxWHITE_PEN
);
2532 dc
.DrawLine( 0, 0, client_width
, 0 );
2533 dc
.DrawLine( 0, 0, 0, client_height
);
2537 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2539 m_owner
->ProcessCornerLabelMouseEvent( event
);
2543 // This seems to be required for wxMotif otherwise the mouse
2544 // cursor must be in the cell edit control to get key events
2546 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2548 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2553 //////////////////////////////////////////////////////////////////////
2555 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2557 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2558 EVT_PAINT( wxGridWindow::OnPaint
)
2559 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2560 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2561 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2564 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2565 wxGridRowLabelWindow
*rowLblWin
,
2566 wxGridColLabelWindow
*colLblWin
,
2567 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2568 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
2571 m_rowLabelWin
= rowLblWin
;
2572 m_colLabelWin
= colLblWin
;
2573 SetBackgroundColour( "WHITE" );
2577 wxGridWindow::~wxGridWindow()
2582 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2584 wxPaintDC
dc( this );
2585 m_owner
->PrepareDC( dc
);
2586 wxRegion reg
= GetUpdateRegion();
2587 m_owner
->CalcCellsExposed( reg
);
2588 m_owner
->DrawGridCellArea( dc
);
2589 m_owner
->DrawGridSpace( dc
);
2590 #if WXGRID_DRAW_LINES
2591 m_owner
->DrawAllGridLines( dc
, reg
);
2593 m_owner
->DrawHighlight( dc
);
2597 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2599 wxPanel::ScrollWindow( dx
, dy
, rect
);
2600 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
2601 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
2605 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2607 m_owner
->ProcessGridCellMouseEvent( event
);
2611 // This seems to be required for wxMotif otherwise the mouse
2612 // cursor must be in the cell edit control to get key events
2614 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2616 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2620 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
2625 //////////////////////////////////////////////////////////////////////
2628 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
2630 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2631 EVT_PAINT( wxGrid::OnPaint
)
2632 EVT_SIZE( wxGrid::OnSize
)
2633 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2634 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2637 wxGrid::wxGrid( wxWindow
*parent
,
2642 const wxString
& name
)
2643 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
),
2644 m_colMinWidths(wxKEY_INTEGER
, GRID_HASH_SIZE
)
2653 m_defaultCellAttr
->SafeDecRef();
2655 #ifdef DEBUG_ATTR_CACHE
2656 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2657 wxPrintf(_T("wxGrid attribute cache statistics: "
2658 "total: %u, hits: %u (%u%%)\n"),
2659 total
, gs_nAttrCacheHits
,
2660 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2666 delete m_typeRegistry
;
2671 // ----- internal init and update functions
2674 void wxGrid::Create()
2676 m_created
= FALSE
; // set to TRUE by CreateGrid
2677 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
2679 m_table
= (wxGridTableBase
*) NULL
;
2682 m_cellEditCtrlEnabled
= FALSE
;
2684 m_defaultCellAttr
= new wxGridCellAttr
;
2685 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2687 // Set default cell attributes
2688 m_defaultCellAttr
->SetFont(GetFont());
2689 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
2690 m_defaultCellAttr
->SetTextColour(
2691 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
2692 m_defaultCellAttr
->SetBackgroundColour(
2693 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
2694 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2695 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2700 m_currentCellCoords
= wxGridNoCellCoords
;
2702 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2703 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2705 // data type registration: register all standard data types
2706 // TODO: may be allow the app to selectively disable some of them?
2707 m_typeRegistry
= new wxGridTypeRegistry
;
2708 RegisterDataType(wxGRID_VALUE_STRING
,
2709 new wxGridCellStringRenderer
,
2710 new wxGridCellTextEditor
);
2711 RegisterDataType(wxGRID_VALUE_BOOL
,
2712 new wxGridCellBoolRenderer
,
2713 new wxGridCellBoolEditor
);
2714 RegisterDataType(wxGRID_VALUE_NUMBER
,
2715 new wxGridCellNumberRenderer
,
2716 new wxGridCellNumberEditor
);
2718 // subwindow components that make up the wxGrid
2719 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
2724 m_rowLabelWin
= new wxGridRowLabelWindow( this,
2729 m_colLabelWin
= new wxGridColLabelWindow( this,
2734 m_gridWin
= new wxGridWindow( this,
2741 SetTargetWindow( m_gridWin
);
2745 bool wxGrid::CreateGrid( int numRows
, int numCols
)
2749 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2754 m_numRows
= numRows
;
2755 m_numCols
= numCols
;
2757 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
2758 m_table
->SetView( this );
2767 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
2771 // RD: Actually, this should probably be allowed. I think it would be
2772 // nice to be able to switch multiple Tables in and out of a single
2773 // View at runtime. Is there anything in the implmentation that would
2776 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2781 m_numRows
= table
->GetNumberRows();
2782 m_numCols
= table
->GetNumberCols();
2785 m_table
->SetView( this );
2798 if ( m_numRows
<= 0 )
2799 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
2801 if ( m_numCols
<= 0 )
2802 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
2804 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2805 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2807 if ( m_rowLabelWin
)
2809 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2813 m_labelBackgroundColour
= wxColour( _T("WHITE") );
2816 m_labelTextColour
= wxColour( _T("BLACK") );
2819 m_attrCache
.row
= -1;
2821 // TODO: something better than this ?
2823 m_labelFont
= this->GetFont();
2824 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
2826 m_rowLabelHorizAlign
= wxLEFT
;
2827 m_rowLabelVertAlign
= wxCENTRE
;
2829 m_colLabelHorizAlign
= wxCENTRE
;
2830 m_colLabelVertAlign
= wxTOP
;
2832 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2833 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2835 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2836 m_defaultRowHeight
+= 8;
2838 m_defaultRowHeight
+= 4;
2841 m_gridLineColour
= wxColour( 128, 128, 255 );
2842 m_gridLinesEnabled
= TRUE
;
2844 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2845 m_winCapture
= (wxWindow
*)NULL
;
2846 m_canDragRowSize
= TRUE
;
2847 m_canDragColSize
= TRUE
;
2848 m_canDragGridSize
= TRUE
;
2850 m_dragRowOrCol
= -1;
2851 m_isDragging
= FALSE
;
2852 m_startDragPos
= wxDefaultPosition
;
2854 m_waitForSlowClick
= FALSE
;
2856 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2857 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2859 m_currentCellCoords
= wxGridNoCellCoords
;
2861 m_selectedTopLeft
= wxGridNoCellCoords
;
2862 m_selectedBottomRight
= wxGridNoCellCoords
;
2863 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2864 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2866 m_editable
= TRUE
; // default for whole grid
2868 m_inOnKeyDown
= FALSE
;
2872 // ----------------------------------------------------------------------------
2873 // the idea is to call these functions only when necessary because they create
2874 // quite big arrays which eat memory mostly unnecessary - in particular, if
2875 // default widths/heights are used for all rows/columns, we may not use these
2878 // with some extra code, it should be possible to only store the
2879 // widths/heights different from default ones but this will be done later...
2880 // ----------------------------------------------------------------------------
2882 void wxGrid::InitRowHeights()
2884 m_rowHeights
.Empty();
2885 m_rowBottoms
.Empty();
2887 m_rowHeights
.Alloc( m_numRows
);
2888 m_rowBottoms
.Alloc( m_numRows
);
2891 for ( int i
= 0; i
< m_numRows
; i
++ )
2893 m_rowHeights
.Add( m_defaultRowHeight
);
2894 rowBottom
+= m_defaultRowHeight
;
2895 m_rowBottoms
.Add( rowBottom
);
2899 void wxGrid::InitColWidths()
2901 m_colWidths
.Empty();
2902 m_colRights
.Empty();
2904 m_colWidths
.Alloc( m_numCols
);
2905 m_colRights
.Alloc( m_numCols
);
2907 for ( int i
= 0; i
< m_numCols
; i
++ )
2909 m_colWidths
.Add( m_defaultColWidth
);
2910 colRight
+= m_defaultColWidth
;
2911 m_colRights
.Add( colRight
);
2915 int wxGrid::GetColWidth(int col
) const
2917 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2920 int wxGrid::GetColLeft(int col
) const
2922 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
2923 : m_colRights
[col
] - m_colWidths
[col
];
2926 int wxGrid::GetColRight(int col
) const
2928 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
2932 int wxGrid::GetRowHeight(int row
) const
2934 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2937 int wxGrid::GetRowTop(int row
) const
2939 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2940 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2943 int wxGrid::GetRowBottom(int row
) const
2945 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2946 : m_rowBottoms
[row
];
2949 void wxGrid::CalcDimensions()
2952 GetClientSize( &cw
, &ch
);
2954 if ( m_numRows
> 0 && m_numCols
> 0 )
2956 int right
= GetColRight( m_numCols
-1 ) + 50;
2957 int bottom
= GetRowBottom( m_numRows
-1 ) + 50;
2959 // TODO: restore the scroll position that we had before sizing
2962 GetViewStart( &x
, &y
);
2963 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2964 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2970 void wxGrid::CalcWindowSizes()
2973 GetClientSize( &cw
, &ch
);
2975 if ( m_cornerLabelWin
->IsShown() )
2976 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2978 if ( m_colLabelWin
->IsShown() )
2979 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2981 if ( m_rowLabelWin
->IsShown() )
2982 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2984 if ( m_gridWin
->IsShown() )
2985 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2989 // this is called when the grid table sends a message to say that it
2990 // has been redimensioned
2992 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2996 // if we were using the default widths/heights so far, we must change them
2998 if ( m_colWidths
.IsEmpty() )
3003 if ( m_rowHeights
.IsEmpty() )
3008 switch ( msg
.GetId() )
3010 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3012 size_t pos
= msg
.GetCommandInt();
3013 int numRows
= msg
.GetCommandInt2();
3014 for ( i
= 0; i
< numRows
; i
++ )
3016 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3017 m_rowBottoms
.Insert( 0, pos
);
3019 m_numRows
+= numRows
;
3022 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3024 for ( i
= pos
; i
< m_numRows
; i
++ )
3026 bottom
+= m_rowHeights
[i
];
3027 m_rowBottoms
[i
] = bottom
;
3033 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3035 int numRows
= msg
.GetCommandInt();
3036 for ( i
= 0; i
< numRows
; i
++ )
3038 m_rowHeights
.Add( m_defaultRowHeight
);
3039 m_rowBottoms
.Add( 0 );
3042 int oldNumRows
= m_numRows
;
3043 m_numRows
+= numRows
;
3046 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3048 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3050 bottom
+= m_rowHeights
[i
];
3051 m_rowBottoms
[i
] = bottom
;
3057 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3059 size_t pos
= msg
.GetCommandInt();
3060 int numRows
= msg
.GetCommandInt2();
3061 for ( i
= 0; i
< numRows
; i
++ )
3063 m_rowHeights
.Remove( pos
);
3064 m_rowBottoms
.Remove( pos
);
3066 m_numRows
-= numRows
;
3071 m_colWidths
.Clear();
3072 m_colRights
.Clear();
3073 m_currentCellCoords
= wxGridNoCellCoords
;
3077 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3078 m_currentCellCoords
.Set( 0, 0 );
3081 for ( i
= 0; i
< m_numRows
; i
++ )
3083 h
+= m_rowHeights
[i
];
3084 m_rowBottoms
[i
] = h
;
3092 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3094 size_t pos
= msg
.GetCommandInt();
3095 int numCols
= msg
.GetCommandInt2();
3096 for ( i
= 0; i
< numCols
; i
++ )
3098 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3099 m_colRights
.Insert( 0, pos
);
3101 m_numCols
+= numCols
;
3104 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3106 for ( i
= pos
; i
< m_numCols
; i
++ )
3108 right
+= m_colWidths
[i
];
3109 m_colRights
[i
] = right
;
3115 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3117 int numCols
= msg
.GetCommandInt();
3118 for ( i
= 0; i
< numCols
; i
++ )
3120 m_colWidths
.Add( m_defaultColWidth
);
3121 m_colRights
.Add( 0 );
3124 int oldNumCols
= m_numCols
;
3125 m_numCols
+= numCols
;
3128 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3130 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3132 right
+= m_colWidths
[i
];
3133 m_colRights
[i
] = right
;
3139 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3141 size_t pos
= msg
.GetCommandInt();
3142 int numCols
= msg
.GetCommandInt2();
3143 for ( i
= 0; i
< numCols
; i
++ )
3145 m_colWidths
.Remove( pos
);
3146 m_colRights
.Remove( pos
);
3148 m_numCols
-= numCols
;
3152 #if 0 // leave the row alone here so that AppendCols will work subsequently
3154 m_rowHeights
.Clear();
3155 m_rowBottoms
.Clear();
3157 m_currentCellCoords
= wxGridNoCellCoords
;
3161 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3162 m_currentCellCoords
.Set( 0, 0 );
3165 for ( i
= 0; i
< m_numCols
; i
++ )
3167 w
+= m_colWidths
[i
];
3180 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3182 wxRegionIterator
iter( reg
);
3185 m_rowLabelsExposed
.Empty();
3192 // TODO: remove this when we can...
3193 // There is a bug in wxMotif that gives garbage update
3194 // rectangles if you jump-scroll a long way by clicking the
3195 // scrollbar with middle button. This is a work-around
3197 #if defined(__WXMOTIF__)
3199 m_gridWin
->GetClientSize( &cw
, &ch
);
3200 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3201 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3204 // logical bounds of update region
3207 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3208 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3210 // find the row labels within these bounds
3213 for ( row
= 0; row
< m_numRows
; row
++ )
3215 if ( GetRowBottom(row
) < top
)
3218 if ( GetRowTop(row
) > bottom
)
3221 m_rowLabelsExposed
.Add( row
);
3229 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3231 wxRegionIterator
iter( reg
);
3234 m_colLabelsExposed
.Empty();
3241 // TODO: remove this when we can...
3242 // There is a bug in wxMotif that gives garbage update
3243 // rectangles if you jump-scroll a long way by clicking the
3244 // scrollbar with middle button. This is a work-around
3246 #if defined(__WXMOTIF__)
3248 m_gridWin
->GetClientSize( &cw
, &ch
);
3249 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3250 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3253 // logical bounds of update region
3256 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3257 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3259 // find the cells within these bounds
3262 for ( col
= 0; col
< m_numCols
; col
++ )
3264 if ( GetColRight(col
) < left
)
3267 if ( GetColLeft(col
) > right
)
3270 m_colLabelsExposed
.Add( col
);
3278 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3280 wxRegionIterator
iter( reg
);
3283 m_cellsExposed
.Empty();
3284 m_rowsExposed
.Empty();
3285 m_colsExposed
.Empty();
3287 int left
, top
, right
, bottom
;
3292 // TODO: remove this when we can...
3293 // There is a bug in wxMotif that gives garbage update
3294 // rectangles if you jump-scroll a long way by clicking the
3295 // scrollbar with middle button. This is a work-around
3297 #if defined(__WXMOTIF__)
3299 m_gridWin
->GetClientSize( &cw
, &ch
);
3300 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3301 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3302 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3303 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3306 // logical bounds of update region
3308 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3309 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3311 // find the cells within these bounds
3314 for ( row
= 0; row
< m_numRows
; row
++ )
3316 if ( GetRowBottom(row
) <= top
)
3319 if ( GetRowTop(row
) > bottom
)
3322 m_rowsExposed
.Add( row
);
3324 for ( col
= 0; col
< m_numCols
; col
++ )
3326 if ( GetColRight(col
) <= left
)
3329 if ( GetColLeft(col
) > right
)
3332 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3333 m_colsExposed
.Add( col
);
3334 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3343 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3346 wxPoint
pos( event
.GetPosition() );
3347 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3349 if ( event
.Dragging() )
3351 m_isDragging
= TRUE
;
3353 if ( event
.LeftIsDown() )
3355 switch( m_cursorMode
)
3357 case WXGRID_CURSOR_RESIZE_ROW
:
3359 int cw
, ch
, left
, dummy
;
3360 m_gridWin
->GetClientSize( &cw
, &ch
);
3361 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3363 wxClientDC
dc( m_gridWin
);
3365 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3366 dc
.SetLogicalFunction(wxINVERT
);
3367 if ( m_dragLastPos
>= 0 )
3369 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3371 dc
.DrawLine( left
, y
, left
+cw
, y
);
3376 case WXGRID_CURSOR_SELECT_ROW
:
3377 if ( (row
= YToRow( y
)) >= 0 &&
3378 !IsInSelection( row
, 0 ) )
3380 SelectRow( row
, TRUE
);
3383 // default label to suppress warnings about "enumeration value
3384 // 'xxx' not handled in switch
3392 m_isDragging
= FALSE
;
3395 // ------------ Entering or leaving the window
3397 if ( event
.Entering() || event
.Leaving() )
3399 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3403 // ------------ Left button pressed
3405 else if ( event
.LeftDown() )
3407 // don't send a label click event for a hit on the
3408 // edge of the row label - this is probably the user
3409 // wanting to resize the row
3411 if ( YToEdgeOfRow(y
) < 0 )
3415 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3417 SelectRow( row
, event
.ShiftDown() );
3418 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3423 // starting to drag-resize a row
3425 if ( CanDragRowSize() )
3426 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3431 // ------------ Left double click
3433 else if (event
.LeftDClick() )
3435 if ( YToEdgeOfRow(y
) < 0 )
3438 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3443 // ------------ Left button released
3445 else if ( event
.LeftUp() )
3447 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3449 DoEndDragResizeRow();
3451 // Note: we are ending the event *after* doing
3452 // default processing in this case
3454 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3457 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3462 // ------------ Right button down
3464 else if ( event
.RightDown() )
3467 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3469 // no default action at the moment
3474 // ------------ Right double click
3476 else if ( event
.RightDClick() )
3479 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3481 // no default action at the moment
3486 // ------------ No buttons down and mouse moving
3488 else if ( event
.Moving() )
3490 m_dragRowOrCol
= YToEdgeOfRow( y
);
3491 if ( m_dragRowOrCol
>= 0 )
3493 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3495 // don't capture the mouse yet
3496 if ( CanDragRowSize() )
3497 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3500 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3502 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3508 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3511 wxPoint
pos( event
.GetPosition() );
3512 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3514 if ( event
.Dragging() )
3516 m_isDragging
= TRUE
;
3518 if ( event
.LeftIsDown() )
3520 switch( m_cursorMode
)
3522 case WXGRID_CURSOR_RESIZE_COL
:
3524 int cw
, ch
, dummy
, top
;
3525 m_gridWin
->GetClientSize( &cw
, &ch
);
3526 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3528 wxClientDC
dc( m_gridWin
);
3531 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3532 GetColMinimalWidth(m_dragRowOrCol
));
3533 dc
.SetLogicalFunction(wxINVERT
);
3534 if ( m_dragLastPos
>= 0 )
3536 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3538 dc
.DrawLine( x
, top
, x
, top
+ch
);
3543 case WXGRID_CURSOR_SELECT_COL
:
3544 if ( (col
= XToCol( x
)) >= 0 &&
3545 !IsInSelection( 0, col
) )
3547 SelectCol( col
, TRUE
);
3550 // default label to suppress warnings about "enumeration value
3551 // 'xxx' not handled in switch
3559 m_isDragging
= FALSE
;
3562 // ------------ Entering or leaving the window
3564 if ( event
.Entering() || event
.Leaving() )
3566 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3570 // ------------ Left button pressed
3572 else if ( event
.LeftDown() )
3574 // don't send a label click event for a hit on the
3575 // edge of the col label - this is probably the user
3576 // wanting to resize the col
3578 if ( XToEdgeOfCol(x
) < 0 )
3582 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3584 SelectCol( col
, event
.ShiftDown() );
3585 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
3590 // starting to drag-resize a col
3592 if ( CanDragColSize() )
3593 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
3598 // ------------ Left double click
3600 if ( event
.LeftDClick() )
3602 if ( XToEdgeOfCol(x
) < 0 )
3605 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
3610 // ------------ Left button released
3612 else if ( event
.LeftUp() )
3614 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3616 DoEndDragResizeCol();
3618 // Note: we are ending the event *after* doing
3619 // default processing in this case
3621 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3624 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3629 // ------------ Right button down
3631 else if ( event
.RightDown() )
3634 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3636 // no default action at the moment
3641 // ------------ Right double click
3643 else if ( event
.RightDClick() )
3646 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3648 // no default action at the moment
3653 // ------------ No buttons down and mouse moving
3655 else if ( event
.Moving() )
3657 m_dragRowOrCol
= XToEdgeOfCol( x
);
3658 if ( m_dragRowOrCol
>= 0 )
3660 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3662 // don't capture the cursor yet
3663 if ( CanDragColSize() )
3664 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
3667 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3669 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
3675 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3677 if ( event
.LeftDown() )
3679 // indicate corner label by having both row and
3682 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3688 else if ( event
.LeftDClick() )
3690 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3693 else if ( event
.RightDown() )
3695 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3697 // no default action at the moment
3701 else if ( event
.RightDClick() )
3703 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3705 // no default action at the moment
3710 void wxGrid::ChangeCursorMode(CursorMode mode
,
3715 static const wxChar
*cursorModes
[] =
3724 wxLogTrace(_T("grid"),
3725 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3726 win
== m_colLabelWin
? _T("colLabelWin")
3727 : win
? _T("rowLabelWin")
3729 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3730 #endif // __WXDEBUG__
3732 if ( mode
== m_cursorMode
)
3737 // by default use the grid itself
3743 m_winCapture
->ReleaseMouse();
3744 m_winCapture
= (wxWindow
*)NULL
;
3747 m_cursorMode
= mode
;
3749 switch ( m_cursorMode
)
3751 case WXGRID_CURSOR_RESIZE_ROW
:
3752 win
->SetCursor( m_rowResizeCursor
);
3755 case WXGRID_CURSOR_RESIZE_COL
:
3756 win
->SetCursor( m_colResizeCursor
);
3760 win
->SetCursor( *wxSTANDARD_CURSOR
);
3763 // we need to capture mouse when resizing
3764 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3765 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3767 if ( captureMouse
&& resize
)
3769 win
->CaptureMouse();
3774 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
3777 wxPoint
pos( event
.GetPosition() );
3778 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3780 wxGridCellCoords coords
;
3781 XYToCell( x
, y
, coords
);
3783 if ( event
.Dragging() )
3785 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
3787 // Don't start doing anything until the mouse has been drug at
3788 // least 3 pixels in any direction...
3791 if (m_startDragPos
== wxDefaultPosition
)
3793 m_startDragPos
= pos
;
3796 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
3800 m_isDragging
= TRUE
;
3801 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3803 // Hide the edit control, so it
3804 // won't interfer with drag-shrinking.
3805 if ( IsCellEditControlEnabled() )
3806 HideCellEditControl();
3808 // Have we captured the mouse yet?
3811 m_winCapture
= m_gridWin
;
3812 m_winCapture
->CaptureMouse();
3815 if ( coords
!= wxGridNoCellCoords
)
3817 if ( !IsSelection() )
3819 SelectBlock( coords
, coords
);
3823 SelectBlock( m_currentCellCoords
, coords
);
3826 if (! IsVisible(coords
))
3828 MakeCellVisible(coords
);
3829 // TODO: need to introduce a delay or something here. The
3830 // scrolling is way to fast, at least on MSW.
3834 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3836 int cw
, ch
, left
, dummy
;
3837 m_gridWin
->GetClientSize( &cw
, &ch
);
3838 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3840 wxClientDC
dc( m_gridWin
);
3842 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
3843 dc
.SetLogicalFunction(wxINVERT
);
3844 if ( m_dragLastPos
>= 0 )
3846 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3848 dc
.DrawLine( left
, y
, left
+cw
, y
);
3851 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3853 int cw
, ch
, dummy
, top
;
3854 m_gridWin
->GetClientSize( &cw
, &ch
);
3855 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3857 wxClientDC
dc( m_gridWin
);
3859 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3860 GetColMinimalWidth(m_dragRowOrCol
) );
3861 dc
.SetLogicalFunction(wxINVERT
);
3862 if ( m_dragLastPos
>= 0 )
3864 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3866 dc
.DrawLine( x
, top
, x
, top
+ch
);
3873 m_isDragging
= FALSE
;
3874 m_startDragPos
= wxDefaultPosition
;
3876 // if ( coords == wxGridNoCellCoords && m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
3878 // ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
3881 // if ( coords != wxGridNoCellCoords )
3883 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3884 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3887 if ( event
.Entering() || event
.Leaving() )
3889 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3890 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3895 // ------------ Left button pressed
3897 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
3899 DisableCellEditControl();
3900 if ( event
.ShiftDown() )
3902 SelectBlock( m_currentCellCoords
, coords
);
3904 else if ( XToEdgeOfCol(x
) < 0 &&
3905 YToEdgeOfRow(y
) < 0 )
3907 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
3912 MakeCellVisible( coords
);
3914 // if this is the second click on this cell then start
3916 if ( m_waitForSlowClick
&&
3917 (coords
== m_currentCellCoords
) &&
3918 CanEnableCellControl())
3920 EnableCellEditControl();
3922 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3923 attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol())->StartingClick();
3926 m_waitForSlowClick
= FALSE
;
3930 SetCurrentCell( coords
);
3931 m_waitForSlowClick
= TRUE
;
3938 // ------------ Left double click
3940 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
3942 DisableCellEditControl();
3943 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
3945 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
3953 // ------------ Left button released
3955 else if ( event
.LeftUp() )
3957 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3959 if ( IsSelection() )
3963 m_winCapture
->ReleaseMouse();
3964 m_winCapture
= NULL
;
3966 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
3969 // Show the edit control, if it has been hidden for
3971 ShowCellEditControl();
3973 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3975 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3976 DoEndDragResizeRow();
3978 // Note: we are ending the event *after* doing
3979 // default processing in this case
3981 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3983 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3985 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3986 DoEndDragResizeCol();
3988 // Note: we are ending the event *after* doing
3989 // default processing in this case
3991 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3998 // ------------ Right button down
4000 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4002 DisableCellEditControl();
4003 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4008 // no default action at the moment
4013 // ------------ Right double click
4015 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4017 DisableCellEditControl();
4018 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4023 // no default action at the moment
4027 // ------------ Moving and no button action
4029 else if ( event
.Moving() && !event
.IsButton() )
4031 int dragRow
= YToEdgeOfRow( y
);
4032 int dragCol
= XToEdgeOfCol( x
);
4034 // Dragging on the corner of a cell to resize in both
4035 // directions is not implemented yet...
4037 if ( dragRow
>= 0 && dragCol
>= 0 )
4039 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4045 m_dragRowOrCol
= dragRow
;
4047 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4049 if ( CanDragRowSize() && CanDragGridSize() )
4050 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4058 m_dragRowOrCol
= dragCol
;
4060 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4062 if ( CanDragColSize() && CanDragGridSize() )
4063 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4069 // Neither on a row or col edge
4071 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4073 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4079 void wxGrid::DoEndDragResizeRow()
4081 if ( m_dragLastPos
>= 0 )
4083 // erase the last line and resize the row
4085 int cw
, ch
, left
, dummy
;
4086 m_gridWin
->GetClientSize( &cw
, &ch
);
4087 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4089 wxClientDC
dc( m_gridWin
);
4091 dc
.SetLogicalFunction( wxINVERT
);
4092 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4093 HideCellEditControl();
4095 int rowTop
= GetRowTop(m_dragRowOrCol
);
4096 SetRowSize( m_dragRowOrCol
,
4097 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4099 if ( !GetBatchCount() )
4101 // Only needed to get the correct rect.y:
4102 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4104 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4105 rect
.width
= m_rowLabelWidth
;
4106 rect
.height
= ch
- rect
.y
;
4107 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4109 m_gridWin
->Refresh( FALSE
, &rect
);
4112 ShowCellEditControl();
4117 void wxGrid::DoEndDragResizeCol()
4119 if ( m_dragLastPos
>= 0 )
4121 // erase the last line and resize the col
4123 int cw
, ch
, dummy
, top
;
4124 m_gridWin
->GetClientSize( &cw
, &ch
);
4125 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4127 wxClientDC
dc( m_gridWin
);
4129 dc
.SetLogicalFunction( wxINVERT
);
4130 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4131 HideCellEditControl();
4133 int colLeft
= GetColLeft(m_dragRowOrCol
);
4134 SetColSize( m_dragRowOrCol
,
4135 wxMax( m_dragLastPos
- colLeft
,
4136 GetColMinimalWidth(m_dragRowOrCol
) ) );
4138 if ( !GetBatchCount() )
4140 // Only needed to get the correct rect.x:
4141 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4143 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4144 rect
.width
= cw
- rect
.x
;
4145 rect
.height
= m_colLabelHeight
;
4146 m_colLabelWin
->Refresh( TRUE
, &rect
);
4148 m_gridWin
->Refresh( FALSE
, &rect
);
4151 ShowCellEditControl();
4158 // ------ interaction with data model
4160 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4162 switch ( msg
.GetId() )
4164 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4165 return GetModelValues();
4167 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4168 return SetModelValues();
4170 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4171 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4172 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4173 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4174 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4175 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4176 return Redimension( msg
);
4185 // The behaviour of this function depends on the grid table class
4186 // Clear() function. For the default wxGridStringTable class the
4187 // behavious is to replace all cell contents with wxEmptyString but
4188 // not to change the number of rows or cols.
4190 void wxGrid::ClearGrid()
4194 if (IsCellEditControlEnabled())
4195 DisableCellEditControl();
4198 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4203 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4205 // TODO: something with updateLabels flag
4209 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4215 if (IsCellEditControlEnabled())
4216 DisableCellEditControl();
4218 bool ok
= m_table
->InsertRows( pos
, numRows
);
4220 // the table will have sent the results of the insert row
4221 // operation to this view object as a grid table message
4225 if ( m_numCols
== 0 )
4227 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4229 // TODO: perhaps instead of appending the default number of cols
4230 // we should remember what the last non-zero number of cols was ?
4234 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4236 // if we have just inserted cols into an empty grid the current
4237 // cell will be undefined...
4239 SetCurrentCell( 0, 0 );
4243 if ( !GetBatchCount() ) Refresh();
4255 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4257 // TODO: something with updateLabels flag
4261 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4265 if ( m_table
&& m_table
->AppendRows( numRows
) )
4267 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4269 // if we have just inserted cols into an empty grid the current
4270 // cell will be undefined...
4272 SetCurrentCell( 0, 0 );
4275 // the table will have sent the results of the append row
4276 // operation to this view object as a grid table message
4279 if ( !GetBatchCount() ) Refresh();
4289 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4291 // TODO: something with updateLabels flag
4295 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4301 if (IsCellEditControlEnabled())
4302 DisableCellEditControl();
4304 if (m_table
->DeleteRows( pos
, numRows
))
4307 // the table will have sent the results of the delete row
4308 // operation to this view object as a grid table message
4311 if ( !GetBatchCount() ) Refresh();
4319 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4321 // TODO: something with updateLabels flag
4325 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4331 if (IsCellEditControlEnabled())
4332 DisableCellEditControl();
4334 bool ok
= m_table
->InsertCols( pos
, numCols
);
4336 // the table will have sent the results of the insert col
4337 // operation to this view object as a grid table message
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 );
4350 if ( !GetBatchCount() ) Refresh();
4362 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4364 // TODO: something with updateLabels flag
4368 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4372 if ( m_table
&& m_table
->AppendCols( numCols
) )
4374 // the table will have sent the results of the append col
4375 // operation to this view object as a grid table message
4377 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4379 // if we have just inserted cols into an empty grid the current
4380 // cell will be undefined...
4382 SetCurrentCell( 0, 0 );
4386 if ( !GetBatchCount() ) Refresh();
4396 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4398 // TODO: something with updateLabels flag
4402 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4408 if (IsCellEditControlEnabled())
4409 DisableCellEditControl();
4411 if ( m_table
->DeleteCols( pos
, numCols
) )
4413 // the table will have sent the results of the delete col
4414 // operation to this view object as a grid table message
4417 if ( !GetBatchCount() ) Refresh();
4427 // ----- event handlers
4430 // Generate a grid event based on a mouse event and
4431 // return the result of ProcessEvent()
4433 bool wxGrid::SendEvent( const wxEventType type
,
4435 wxMouseEvent
& mouseEv
)
4437 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4439 int rowOrCol
= (row
== -1 ? col
: row
);
4441 wxGridSizeEvent
gridEvt( GetId(),
4445 mouseEv
.GetX(), mouseEv
.GetY(),
4446 mouseEv
.ControlDown(),
4447 mouseEv
.ShiftDown(),
4449 mouseEv
.MetaDown() );
4451 return GetEventHandler()->ProcessEvent(gridEvt
);
4453 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4455 wxGridRangeSelectEvent
gridEvt( GetId(),
4459 m_selectedBottomRight
,
4460 mouseEv
.ControlDown(),
4461 mouseEv
.ShiftDown(),
4463 mouseEv
.MetaDown() );
4465 return GetEventHandler()->ProcessEvent(gridEvt
);
4469 wxGridEvent
gridEvt( GetId(),
4473 mouseEv
.GetX(), mouseEv
.GetY(),
4474 mouseEv
.ControlDown(),
4475 mouseEv
.ShiftDown(),
4477 mouseEv
.MetaDown() );
4479 return GetEventHandler()->ProcessEvent(gridEvt
);
4484 // Generate a grid event of specified type and return the result
4485 // of ProcessEvent().
4487 bool wxGrid::SendEvent( const wxEventType type
,
4490 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4492 int rowOrCol
= (row
== -1 ? col
: row
);
4494 wxGridSizeEvent
gridEvt( GetId(),
4499 return GetEventHandler()->ProcessEvent(gridEvt
);
4503 wxGridEvent
gridEvt( GetId(),
4508 return GetEventHandler()->ProcessEvent(gridEvt
);
4513 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4515 wxPaintDC
dc( this );
4517 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4518 m_numRows
&& m_numCols
)
4520 m_currentCellCoords
.Set(0, 0);
4521 ShowCellEditControl();
4528 // This is just here to make sure that CalcDimensions gets called when
4529 // the grid view is resized... then the size event is skipped to allow
4530 // the box sizers to handle everything
4532 void wxGrid::OnSize( wxSizeEvent
& event
)
4539 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4541 if ( m_inOnKeyDown
)
4543 // shouldn't be here - we are going round in circles...
4545 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4548 m_inOnKeyDown
= TRUE
;
4550 // propagate the event up and see if it gets processed
4552 wxWindow
*parent
= GetParent();
4553 wxKeyEvent
keyEvt( event
);
4554 keyEvt
.SetEventObject( parent
);
4556 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4559 // TODO: Should also support Shift-cursor keys for
4560 // extending the selection. Maybe add a flag to
4561 // MoveCursorXXX() and MoveCursorXXXBlock() and
4562 // just send event.ShiftDown().
4564 // try local handlers
4566 switch ( event
.KeyCode() )
4569 if ( event
.ControlDown() )
4571 MoveCursorUpBlock();
4580 if ( event
.ControlDown() )
4582 MoveCursorDownBlock();
4591 if ( event
.ControlDown() )
4593 MoveCursorLeftBlock();
4602 if ( event
.ControlDown() )
4604 MoveCursorRightBlock();
4613 if ( event
.ControlDown() )
4615 event
.Skip(); // to let the edit control have the return
4624 if (event
.ShiftDown())
4631 if ( event
.ControlDown() )
4633 MakeCellVisible( 0, 0 );
4634 SetCurrentCell( 0, 0 );
4643 if ( event
.ControlDown() )
4645 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
4646 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
4662 // We don't want these keys to trigger the edit control, any others?
4671 if ( !IsEditable() )
4676 // Otherwise fall through to default
4679 // now try the cell edit control
4681 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4683 EnableCellEditControl();
4684 int row
= m_currentCellCoords
.GetRow();
4685 int col
= m_currentCellCoords
.GetCol();
4686 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4687 attr
->GetEditor(this, row
, col
)->StartingKey(event
);
4692 // let others process char events for readonly cells
4699 m_inOnKeyDown
= FALSE
;
4703 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4707 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4709 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
4711 // the event has been intercepted - do nothing
4716 m_currentCellCoords
!= wxGridNoCellCoords
)
4718 HideCellEditControl();
4719 DisableCellEditControl();
4721 // Clear the old current cell highlight
4722 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
4724 // Otherwise refresh redraws the highlight!
4725 m_currentCellCoords
= coords
;
4727 m_gridWin
->Refresh( FALSE
, &r
);
4730 m_currentCellCoords
= coords
;
4734 wxClientDC
dc(m_gridWin
);
4737 wxGridCellAttr
* attr
= GetCellAttr(coords
);
4738 DrawCellHighlight(dc
, attr
);
4741 if ( IsSelection() )
4743 wxRect
r( SelectionToDeviceRect() );
4745 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
4752 // ------ functions to get/send data (see also public functions)
4755 bool wxGrid::GetModelValues()
4759 // all we need to do is repaint the grid
4761 m_gridWin
->Refresh();
4769 bool wxGrid::SetModelValues()
4775 for ( row
= 0; row
< m_numRows
; row
++ )
4777 for ( col
= 0; col
< m_numCols
; col
++ )
4779 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4791 // Note - this function only draws cells that are in the list of
4792 // exposed cells (usually set from the update region by
4793 // CalcExposedCells)
4795 void wxGrid::DrawGridCellArea( wxDC
& dc
)
4797 if ( !m_numRows
|| !m_numCols
) return;
4800 size_t numCells
= m_cellsExposed
.GetCount();
4802 for ( i
= 0; i
< numCells
; i
++ )
4804 DrawCell( dc
, m_cellsExposed
[i
] );
4809 void wxGrid::DrawGridSpace( wxDC
& dc
)
4811 if ( m_numRows
&& m_numCols
)
4814 m_gridWin
->GetClientSize( &cw
, &ch
);
4817 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4819 if ( right
> GetColRight(m_numCols
-1) ||
4820 bottom
> GetRowBottom(m_numRows
-1) )
4823 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4825 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
4826 dc
.SetPen( *wxTRANSPARENT_PEN
);
4828 if ( right
> GetColRight(m_numCols
-1) )
4829 dc
.DrawRectangle( GetColRight(m_numCols
-1)+1, top
,
4830 right
- GetColRight(m_numCols
-1), ch
);
4832 if ( bottom
> GetRowBottom(m_numRows
-1) )
4833 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1)+1,
4834 cw
, bottom
- GetRowBottom(m_numRows
-1) );
4840 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
4842 int row
= coords
.GetRow();
4843 int col
= coords
.GetCol();
4845 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4848 // we draw the cell border ourselves
4849 #if !WXGRID_DRAW_LINES
4850 if ( m_gridLinesEnabled
)
4851 DrawCellBorder( dc
, coords
);
4854 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4856 bool isCurrent
= coords
== m_currentCellCoords
;
4859 rect
.x
= GetColLeft(col
);
4860 rect
.y
= GetRowTop(row
);
4861 rect
.width
= GetColWidth(col
) - 1;
4862 rect
.height
= GetRowHeight(row
) - 1;
4864 // if the editor is shown, we should use it and not the renderer
4865 if ( isCurrent
&& IsCellEditControlEnabled() )
4867 attr
->GetEditor(this, row
, col
)->PaintBackground(rect
, attr
);
4871 // but all the rest is drawn by the cell renderer and hence may be
4873 attr
->GetRenderer(this, row
, col
)->
4874 Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
4881 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
4883 int row
= m_currentCellCoords
.GetRow();
4884 int col
= m_currentCellCoords
.GetCol();
4886 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4890 rect
.x
= GetColLeft(col
);
4891 rect
.y
= GetRowTop(row
);
4892 rect
.width
= GetColWidth(col
) - 1;
4893 rect
.height
= GetRowHeight(row
) - 1;
4895 // hmmm... what could we do here to show that the cell is disabled?
4896 // for now, I just draw a thinner border than for the other ones, but
4897 // it doesn't look really good
4898 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
4899 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
4901 dc
.DrawRectangle(rect
);
4904 // VZ: my experiments with 3d borders...
4906 // how to properly set colours for arbitrary bg?
4907 wxCoord x1
= rect
.x
,
4909 x2
= rect
.x
+ rect
.width
-1,
4910 y2
= rect
.y
+ rect
.height
-1;
4912 dc
.SetPen(*wxWHITE_PEN
);
4913 dc
.DrawLine(x1
, y1
, x2
, y1
);
4914 dc
.DrawLine(x1
, y1
, x1
, y2
);
4916 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
4917 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
4919 dc
.SetPen(*wxBLACK_PEN
);
4920 dc
.DrawLine(x1
, y2
, x2
, y2
);
4921 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
4926 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
4928 int row
= coords
.GetRow();
4929 int col
= coords
.GetCol();
4930 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
4933 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
4935 // right hand border
4937 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
4938 GetColRight(col
), GetRowBottom(row
) );
4942 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
4943 GetColRight(col
), GetRowBottom(row
) );
4946 void wxGrid::DrawHighlight(wxDC
& dc
)
4948 if ( IsCellEditControlEnabled() )
4950 // don't show highlight when the edit control is shown
4954 // if the active cell was repainted, repaint its highlight too because it
4955 // might have been damaged by the grid lines
4956 size_t count
= m_cellsExposed
.GetCount();
4957 for ( size_t n
= 0; n
< count
; n
++ )
4959 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
4961 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4962 DrawCellHighlight(dc
, attr
);
4970 // TODO: remove this ???
4971 // This is used to redraw all grid lines e.g. when the grid line colour
4974 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
4976 if ( !m_gridLinesEnabled
||
4978 !m_numCols
) return;
4980 int top
, bottom
, left
, right
;
4986 m_gridWin
->GetClientSize(&cw
, &ch
);
4988 // virtual coords of visible area
4990 CalcUnscrolledPosition( 0, 0, &left
, &top
);
4991 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
4996 reg
.GetBox(x
, y
, w
, h
);
4997 CalcUnscrolledPosition( x
, y
, &left
, &top
);
4998 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5002 m_gridWin
->GetClientSize(&cw
, &ch
);
5003 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5004 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5007 // avoid drawing grid lines past the last row and col
5009 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5010 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5012 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5014 // horizontal grid lines
5017 for ( i
= 0; i
< m_numRows
; i
++ )
5019 int bot
= GetRowBottom(i
) - 1;
5028 dc
.DrawLine( left
, bot
, right
, bot
);
5033 // vertical grid lines
5035 for ( i
= 0; i
< m_numCols
; i
++ )
5037 int colRight
= GetColRight(i
) - 1;
5038 if ( colRight
> right
)
5043 if ( colRight
>= left
)
5045 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5051 void wxGrid::DrawRowLabels( wxDC
& dc
)
5053 if ( !m_numRows
|| !m_numCols
) return;
5056 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5058 for ( i
= 0; i
< numLabels
; i
++ )
5060 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5065 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5067 if ( GetRowHeight(row
) <= 0 )
5070 int rowTop
= GetRowTop(row
),
5071 rowBottom
= GetRowBottom(row
) - 1;
5073 dc
.SetPen( *wxBLACK_PEN
);
5074 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5075 m_rowLabelWidth
-1, rowBottom
);
5077 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5079 dc
.SetPen( *wxWHITE_PEN
);
5080 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5081 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5083 dc
.SetBackgroundMode( wxTRANSPARENT
);
5084 dc
.SetTextForeground( GetLabelTextColour() );
5085 dc
.SetFont( GetLabelFont() );
5088 GetRowLabelAlignment( &hAlign
, &vAlign
);
5092 rect
.SetY( GetRowTop(row
) + 2 );
5093 rect
.SetWidth( m_rowLabelWidth
- 4 );
5094 rect
.SetHeight( GetRowHeight(row
) - 4 );
5095 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5099 void wxGrid::DrawColLabels( wxDC
& dc
)
5101 if ( !m_numRows
|| !m_numCols
) return;
5104 size_t numLabels
= m_colLabelsExposed
.GetCount();
5106 for ( i
= 0; i
< numLabels
; i
++ )
5108 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5113 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5115 if ( GetColWidth(col
) <= 0 )
5118 int colLeft
= GetColLeft(col
),
5119 colRight
= GetColRight(col
) - 1;
5121 dc
.SetPen( *wxBLACK_PEN
);
5122 dc
.DrawLine( colRight
, 0,
5123 colRight
, m_colLabelHeight
-1 );
5125 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5126 colRight
, m_colLabelHeight
-1 );
5128 dc
.SetPen( *wxWHITE_PEN
);
5129 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5130 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5132 dc
.SetBackgroundMode( wxTRANSPARENT
);
5133 dc
.SetTextForeground( GetLabelTextColour() );
5134 dc
.SetFont( GetLabelFont() );
5136 dc
.SetBackgroundMode( wxTRANSPARENT
);
5137 dc
.SetTextForeground( GetLabelTextColour() );
5138 dc
.SetFont( GetLabelFont() );
5141 GetColLabelAlignment( &hAlign
, &vAlign
);
5144 rect
.SetX( colLeft
+ 2 );
5146 rect
.SetWidth( GetColWidth(col
) - 4 );
5147 rect
.SetHeight( m_colLabelHeight
- 4 );
5148 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5152 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5153 const wxString
& value
,
5158 long textWidth
, textHeight
;
5159 long lineWidth
, lineHeight
;
5160 wxArrayString lines
;
5162 dc
.SetClippingRegion( rect
);
5163 StringToLines( value
, lines
);
5164 if ( lines
.GetCount() )
5166 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5167 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5170 switch ( horizAlign
)
5173 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5177 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5186 switch ( vertAlign
)
5189 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5193 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5202 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5204 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5209 dc
.DestroyClippingRegion();
5213 // Split multi line text up into an array of strings. Any existing
5214 // contents of the string array are preserved.
5216 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5220 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5221 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5223 while ( startPos
< (int)tVal
.Length() )
5225 pos
= tVal
.Mid(startPos
).Find( eol
);
5230 else if ( pos
== 0 )
5232 lines
.Add( wxEmptyString
);
5236 lines
.Add( value
.Mid(startPos
, pos
) );
5240 if ( startPos
< (int)value
.Length() )
5242 lines
.Add( value
.Mid( startPos
) );
5247 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5248 wxArrayString
& lines
,
5249 long *width
, long *height
)
5256 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5258 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5259 w
= wxMax( w
, lineW
);
5269 // ------ Edit control functions
5273 void wxGrid::EnableEditing( bool edit
)
5275 // TODO: improve this ?
5277 if ( edit
!= m_editable
)
5281 // FIXME IMHO this won't disable the edit control if edit == FALSE
5282 // because of the check in the beginning of
5283 // EnableCellEditControl() just below (VZ)
5284 EnableCellEditControl(m_editable
);
5289 void wxGrid::EnableCellEditControl( bool enable
)
5294 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5295 SetCurrentCell( 0, 0 );
5297 if ( enable
!= m_cellEditCtrlEnabled
)
5299 // TODO allow the app to Veto() this event?
5300 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5304 // this should be checked by the caller!
5305 wxASSERT_MSG( CanEnableCellControl(),
5306 _T("can't enable editing for this cell!") );
5308 // do it before ShowCellEditControl()
5309 m_cellEditCtrlEnabled
= enable
;
5311 ShowCellEditControl();
5315 HideCellEditControl();
5316 SaveEditControlValue();
5318 // do it after HideCellEditControl()
5319 m_cellEditCtrlEnabled
= enable
;
5324 bool wxGrid::IsCurrentCellReadOnly() const
5327 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5328 bool readonly
= attr
->IsReadOnly();
5334 bool wxGrid::CanEnableCellControl() const
5336 return m_editable
&& !IsCurrentCellReadOnly();
5339 bool wxGrid::IsCellEditControlEnabled() const
5341 // the cell edit control might be disable for all cells or just for the
5342 // current one if it's read only
5343 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5346 void wxGrid::ShowCellEditControl()
5348 if ( IsCellEditControlEnabled() )
5350 if ( !IsVisible( m_currentCellCoords
) )
5356 wxRect rect
= CellToRect( m_currentCellCoords
);
5357 int row
= m_currentCellCoords
.GetRow();
5358 int col
= m_currentCellCoords
.GetCol();
5360 // convert to scrolled coords
5362 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5364 // done in PaintBackground()
5366 // erase the highlight and the cell contents because the editor
5367 // might not cover the entire cell
5368 wxClientDC
dc( m_gridWin
);
5370 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5371 dc
.SetPen(*wxTRANSPARENT_PEN
);
5372 dc
.DrawRectangle(rect
);
5375 // cell is shifted by one pixel
5379 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5380 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5381 if ( !editor
->IsCreated() )
5383 editor
->Create(m_gridWin
, -1,
5384 new wxGridCellEditorEvtHandler(this, editor
));
5387 editor
->SetSize( rect
);
5389 editor
->Show( TRUE
, attr
);
5390 editor
->BeginEdit(row
, col
, this);
5397 void wxGrid::HideCellEditControl()
5399 if ( IsCellEditControlEnabled() )
5401 int row
= m_currentCellCoords
.GetRow();
5402 int col
= m_currentCellCoords
.GetCol();
5404 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5405 attr
->GetEditor(this, row
, col
)->Show( FALSE
);
5407 m_gridWin
->SetFocus();
5412 void wxGrid::SaveEditControlValue()
5414 if ( IsCellEditControlEnabled() )
5416 int row
= m_currentCellCoords
.GetRow();
5417 int col
= m_currentCellCoords
.GetCol();
5419 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5420 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5421 bool changed
= editor
->EndEdit(row
, col
, this);
5427 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5428 m_currentCellCoords
.GetRow(),
5429 m_currentCellCoords
.GetCol() );
5436 // ------ Grid location functions
5437 // Note that all of these functions work with the logical coordinates of
5438 // grid cells and labels so you will need to convert from device
5439 // coordinates for mouse events etc.
5442 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5444 int row
= YToRow(y
);
5445 int col
= XToCol(x
);
5447 if ( row
== -1 || col
== -1 )
5449 coords
= wxGridNoCellCoords
;
5453 coords
.Set( row
, col
);
5458 int wxGrid::YToRow( int y
)
5462 for ( i
= 0; i
< m_numRows
; i
++ )
5464 if ( y
< GetRowBottom(i
) )
5472 int wxGrid::XToCol( int x
)
5476 for ( i
= 0; i
< m_numCols
; i
++ )
5478 if ( x
< GetColRight(i
) )
5486 // return the row number that that the y coord is near the edge of, or
5487 // -1 if not near an edge
5489 int wxGrid::YToEdgeOfRow( int y
)
5493 for ( i
= 0; i
< m_numRows
; i
++ )
5495 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5497 d
= abs( y
- GetRowBottom(i
) );
5498 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5507 // return the col number that that the x coord is near the edge of, or
5508 // -1 if not near an edge
5510 int wxGrid::XToEdgeOfCol( int x
)
5514 for ( i
= 0; i
< m_numCols
; i
++ )
5516 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5518 d
= abs( x
- GetColRight(i
) );
5519 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5528 wxRect
wxGrid::CellToRect( int row
, int col
)
5530 wxRect
rect( -1, -1, -1, -1 );
5532 if ( row
>= 0 && row
< m_numRows
&&
5533 col
>= 0 && col
< m_numCols
)
5535 rect
.x
= GetColLeft(col
);
5536 rect
.y
= GetRowTop(row
);
5537 rect
.width
= GetColWidth(col
);
5538 rect
.height
= GetRowHeight(row
);
5545 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5547 // get the cell rectangle in logical coords
5549 wxRect
r( CellToRect( row
, col
) );
5551 // convert to device coords
5553 int left
, top
, right
, bottom
;
5554 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5555 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5557 // check against the client area of the grid window
5560 m_gridWin
->GetClientSize( &cw
, &ch
);
5562 if ( wholeCellVisible
)
5564 // is the cell wholly visible ?
5566 return ( left
>= 0 && right
<= cw
&&
5567 top
>= 0 && bottom
<= ch
);
5571 // is the cell partly visible ?
5573 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
5574 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
5579 // make the specified cell location visible by doing a minimal amount
5582 void wxGrid::MakeCellVisible( int row
, int col
)
5585 int xpos
= -1, ypos
= -1;
5587 if ( row
>= 0 && row
< m_numRows
&&
5588 col
>= 0 && col
< m_numCols
)
5590 // get the cell rectangle in logical coords
5592 wxRect
r( CellToRect( row
, col
) );
5594 // convert to device coords
5596 int left
, top
, right
, bottom
;
5597 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5598 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5601 m_gridWin
->GetClientSize( &cw
, &ch
);
5607 else if ( bottom
> ch
)
5609 int h
= r
.GetHeight();
5611 for ( i
= row
-1; i
>= 0; i
-- )
5613 int rowHeight
= GetRowHeight(i
);
5614 if ( h
+ rowHeight
> ch
)
5621 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
5622 // have rounding errors (this is important, because if we do, we
5623 // might not scroll at all and some cells won't be redrawn)
5624 ypos
+= GRID_SCROLL_LINE
/ 2;
5631 else if ( right
> cw
)
5633 int w
= r
.GetWidth();
5635 for ( i
= col
-1; i
>= 0; i
-- )
5637 int colWidth
= GetColWidth(i
);
5638 if ( w
+ colWidth
> cw
)
5645 // see comment for ypos above
5646 xpos
+= GRID_SCROLL_LINE
/ 2;
5649 if ( xpos
!= -1 || ypos
!= -1 )
5651 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
5652 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
5653 Scroll( xpos
, ypos
);
5661 // ------ Grid cursor movement functions
5664 bool wxGrid::MoveCursorUp()
5666 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5667 m_currentCellCoords
.GetRow() > 0 )
5669 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
5670 m_currentCellCoords
.GetCol() );
5672 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
5673 m_currentCellCoords
.GetCol() );
5682 bool wxGrid::MoveCursorDown()
5684 // TODO: allow for scrolling
5686 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5687 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5689 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
5690 m_currentCellCoords
.GetCol() );
5692 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
5693 m_currentCellCoords
.GetCol() );
5702 bool wxGrid::MoveCursorLeft()
5704 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5705 m_currentCellCoords
.GetCol() > 0 )
5707 MakeCellVisible( m_currentCellCoords
.GetRow(),
5708 m_currentCellCoords
.GetCol() - 1 );
5710 SetCurrentCell( m_currentCellCoords
.GetRow(),
5711 m_currentCellCoords
.GetCol() - 1 );
5720 bool wxGrid::MoveCursorRight()
5722 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
5723 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
5725 MakeCellVisible( m_currentCellCoords
.GetRow(),
5726 m_currentCellCoords
.GetCol() + 1 );
5728 SetCurrentCell( m_currentCellCoords
.GetRow(),
5729 m_currentCellCoords
.GetCol() + 1 );
5738 bool wxGrid::MovePageUp()
5740 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5742 int row
= m_currentCellCoords
.GetRow();
5746 m_gridWin
->GetClientSize( &cw
, &ch
);
5748 int y
= GetRowTop(row
);
5749 int newRow
= YToRow( y
- ch
+ 1 );
5754 else if ( newRow
== row
)
5759 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5760 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5768 bool wxGrid::MovePageDown()
5770 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
5772 int row
= m_currentCellCoords
.GetRow();
5773 if ( row
< m_numRows
)
5776 m_gridWin
->GetClientSize( &cw
, &ch
);
5778 int y
= GetRowTop(row
);
5779 int newRow
= YToRow( y
+ ch
);
5782 newRow
= m_numRows
- 1;
5784 else if ( newRow
== row
)
5789 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
5790 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
5798 bool wxGrid::MoveCursorUpBlock()
5801 m_currentCellCoords
!= wxGridNoCellCoords
&&
5802 m_currentCellCoords
.GetRow() > 0 )
5804 int row
= m_currentCellCoords
.GetRow();
5805 int col
= m_currentCellCoords
.GetCol();
5807 if ( m_table
->IsEmptyCell(row
, col
) )
5809 // starting in an empty cell: find the next block of
5815 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5818 else if ( m_table
->IsEmptyCell(row
-1, col
) )
5820 // starting at the top of a block: find the next block
5826 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5831 // starting within a block: find the top of the block
5836 if ( m_table
->IsEmptyCell(row
, col
) )
5844 MakeCellVisible( row
, col
);
5845 SetCurrentCell( row
, col
);
5853 bool wxGrid::MoveCursorDownBlock()
5856 m_currentCellCoords
!= wxGridNoCellCoords
&&
5857 m_currentCellCoords
.GetRow() < m_numRows
-1 )
5859 int row
= m_currentCellCoords
.GetRow();
5860 int col
= m_currentCellCoords
.GetCol();
5862 if ( m_table
->IsEmptyCell(row
, col
) )
5864 // starting in an empty cell: find the next block of
5867 while ( row
< m_numRows
-1 )
5870 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5873 else if ( m_table
->IsEmptyCell(row
+1, col
) )
5875 // starting at the bottom of a block: find the next block
5878 while ( row
< m_numRows
-1 )
5881 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5886 // starting within a block: find the bottom of the block
5888 while ( row
< m_numRows
-1 )
5891 if ( m_table
->IsEmptyCell(row
, col
) )
5899 MakeCellVisible( row
, col
);
5900 SetCurrentCell( row
, col
);
5908 bool wxGrid::MoveCursorLeftBlock()
5911 m_currentCellCoords
!= wxGridNoCellCoords
&&
5912 m_currentCellCoords
.GetCol() > 0 )
5914 int row
= m_currentCellCoords
.GetRow();
5915 int col
= m_currentCellCoords
.GetCol();
5917 if ( m_table
->IsEmptyCell(row
, col
) )
5919 // starting in an empty cell: find the next block of
5925 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5928 else if ( m_table
->IsEmptyCell(row
, col
-1) )
5930 // starting at the left of a block: find the next block
5936 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5941 // starting within a block: find the left of the block
5946 if ( m_table
->IsEmptyCell(row
, col
) )
5954 MakeCellVisible( row
, col
);
5955 SetCurrentCell( row
, col
);
5963 bool wxGrid::MoveCursorRightBlock()
5966 m_currentCellCoords
!= wxGridNoCellCoords
&&
5967 m_currentCellCoords
.GetCol() < m_numCols
-1 )
5969 int row
= m_currentCellCoords
.GetRow();
5970 int col
= m_currentCellCoords
.GetCol();
5972 if ( m_table
->IsEmptyCell(row
, col
) )
5974 // starting in an empty cell: find the next block of
5977 while ( col
< m_numCols
-1 )
5980 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5983 else if ( m_table
->IsEmptyCell(row
, col
+1) )
5985 // starting at the right of a block: find the next block
5988 while ( col
< m_numCols
-1 )
5991 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
5996 // starting within a block: find the right of the block
5998 while ( col
< m_numCols
-1 )
6001 if ( m_table
->IsEmptyCell(row
, col
) )
6009 MakeCellVisible( row
, col
);
6010 SetCurrentCell( row
, col
);
6021 // ------ Label values and formatting
6024 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6026 *horiz
= m_rowLabelHorizAlign
;
6027 *vert
= m_rowLabelVertAlign
;
6030 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6032 *horiz
= m_colLabelHorizAlign
;
6033 *vert
= m_colLabelVertAlign
;
6036 wxString
wxGrid::GetRowLabelValue( int row
)
6040 return m_table
->GetRowLabelValue( row
);
6050 wxString
wxGrid::GetColLabelValue( int col
)
6054 return m_table
->GetColLabelValue( col
);
6065 void wxGrid::SetRowLabelSize( int width
)
6067 width
= wxMax( width
, 0 );
6068 if ( width
!= m_rowLabelWidth
)
6072 m_rowLabelWin
->Show( FALSE
);
6073 m_cornerLabelWin
->Show( FALSE
);
6075 else if ( m_rowLabelWidth
== 0 )
6077 m_rowLabelWin
->Show( TRUE
);
6078 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6081 m_rowLabelWidth
= width
;
6088 void wxGrid::SetColLabelSize( int height
)
6090 height
= wxMax( height
, 0 );
6091 if ( height
!= m_colLabelHeight
)
6095 m_colLabelWin
->Show( FALSE
);
6096 m_cornerLabelWin
->Show( FALSE
);
6098 else if ( m_colLabelHeight
== 0 )
6100 m_colLabelWin
->Show( TRUE
);
6101 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6104 m_colLabelHeight
= height
;
6111 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6113 if ( m_labelBackgroundColour
!= colour
)
6115 m_labelBackgroundColour
= colour
;
6116 m_rowLabelWin
->SetBackgroundColour( colour
);
6117 m_colLabelWin
->SetBackgroundColour( colour
);
6118 m_cornerLabelWin
->SetBackgroundColour( colour
);
6120 if ( !GetBatchCount() )
6122 m_rowLabelWin
->Refresh();
6123 m_colLabelWin
->Refresh();
6124 m_cornerLabelWin
->Refresh();
6129 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6131 if ( m_labelTextColour
!= colour
)
6133 m_labelTextColour
= colour
;
6134 if ( !GetBatchCount() )
6136 m_rowLabelWin
->Refresh();
6137 m_colLabelWin
->Refresh();
6142 void wxGrid::SetLabelFont( const wxFont
& font
)
6145 if ( !GetBatchCount() )
6147 m_rowLabelWin
->Refresh();
6148 m_colLabelWin
->Refresh();
6152 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6154 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6156 m_rowLabelHorizAlign
= horiz
;
6159 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6161 m_rowLabelVertAlign
= vert
;
6164 if ( !GetBatchCount() )
6166 m_rowLabelWin
->Refresh();
6170 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6172 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6174 m_colLabelHorizAlign
= horiz
;
6177 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6179 m_colLabelVertAlign
= vert
;
6182 if ( !GetBatchCount() )
6184 m_colLabelWin
->Refresh();
6188 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6192 m_table
->SetRowLabelValue( row
, s
);
6193 if ( !GetBatchCount() )
6195 wxRect rect
= CellToRect( row
, 0);
6196 if ( rect
.height
> 0 )
6198 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6200 rect
.width
= m_rowLabelWidth
;
6201 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6207 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6211 m_table
->SetColLabelValue( col
, s
);
6212 if ( !GetBatchCount() )
6214 wxRect rect
= CellToRect( 0, col
);
6215 if ( rect
.width
> 0 )
6217 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6219 rect
.height
= m_colLabelHeight
;
6220 m_colLabelWin
->Refresh( TRUE
, &rect
);
6226 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6228 if ( m_gridLineColour
!= colour
)
6230 m_gridLineColour
= colour
;
6232 wxClientDC
dc( m_gridWin
);
6234 DrawAllGridLines( dc
, wxRegion() );
6238 void wxGrid::EnableGridLines( bool enable
)
6240 if ( enable
!= m_gridLinesEnabled
)
6242 m_gridLinesEnabled
= enable
;
6244 if ( !GetBatchCount() )
6248 wxClientDC
dc( m_gridWin
);
6250 DrawAllGridLines( dc
, wxRegion() );
6254 m_gridWin
->Refresh();
6261 int wxGrid::GetDefaultRowSize()
6263 return m_defaultRowHeight
;
6266 int wxGrid::GetRowSize( int row
)
6268 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6270 return GetRowHeight(row
);
6273 int wxGrid::GetDefaultColSize()
6275 return m_defaultColWidth
;
6278 int wxGrid::GetColSize( int col
)
6280 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6282 return GetColWidth(col
);
6285 // ============================================================================
6286 // access to the grid attributes: each of them has a default value in the grid
6287 // itself and may be overidden on a per-cell basis
6288 // ============================================================================
6290 // ----------------------------------------------------------------------------
6291 // setting default attributes
6292 // ----------------------------------------------------------------------------
6294 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6296 m_defaultCellAttr
->SetBackgroundColour(col
);
6298 m_gridWin
->SetBackgroundColour(col
);
6302 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6304 m_defaultCellAttr
->SetTextColour(col
);
6307 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6309 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6312 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6314 m_defaultCellAttr
->SetFont(font
);
6317 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6319 m_defaultCellAttr
->SetRenderer(renderer
);
6322 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6324 m_defaultCellAttr
->SetEditor(editor
);
6327 // ----------------------------------------------------------------------------
6328 // access to the default attrbiutes
6329 // ----------------------------------------------------------------------------
6331 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6333 return m_defaultCellAttr
->GetBackgroundColour();
6336 wxColour
wxGrid::GetDefaultCellTextColour()
6338 return m_defaultCellAttr
->GetTextColour();
6341 wxFont
wxGrid::GetDefaultCellFont()
6343 return m_defaultCellAttr
->GetFont();
6346 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6348 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6351 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6353 return m_defaultCellAttr
->GetRenderer(NULL
,0,0);
6356 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6358 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6361 // ----------------------------------------------------------------------------
6362 // access to cell attributes
6363 // ----------------------------------------------------------------------------
6365 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6367 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6368 wxColour colour
= attr
->GetBackgroundColour();
6373 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6375 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6376 wxColour colour
= attr
->GetTextColour();
6381 wxFont
wxGrid::GetCellFont( int row
, int col
)
6383 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6384 wxFont font
= attr
->GetFont();
6389 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6391 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6392 attr
->GetAlignment(horiz
, vert
);
6396 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6398 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6399 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6404 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6406 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6407 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6412 bool wxGrid::IsReadOnly(int row
, int col
) const
6414 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6415 bool isReadOnly
= attr
->IsReadOnly();
6420 // ----------------------------------------------------------------------------
6421 // attribute support: cache, automatic provider creation, ...
6422 // ----------------------------------------------------------------------------
6424 bool wxGrid::CanHaveAttributes()
6431 return m_table
->CanHaveAttributes();
6434 void wxGrid::ClearAttrCache()
6436 if ( m_attrCache
.row
!= -1 )
6438 m_attrCache
.attr
->SafeDecRef();
6439 m_attrCache
.row
= -1;
6443 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6445 wxGrid
*self
= (wxGrid
*)this; // const_cast
6447 self
->ClearAttrCache();
6448 self
->m_attrCache
.row
= row
;
6449 self
->m_attrCache
.col
= col
;
6450 self
->m_attrCache
.attr
= attr
;
6454 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6456 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6458 *attr
= m_attrCache
.attr
;
6459 (*attr
)->SafeIncRef();
6461 #ifdef DEBUG_ATTR_CACHE
6462 gs_nAttrCacheHits
++;
6469 #ifdef DEBUG_ATTR_CACHE
6470 gs_nAttrCacheMisses
++;
6476 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6478 wxGridCellAttr
*attr
;
6479 if ( !LookupAttr(row
, col
, &attr
) )
6481 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6482 CacheAttr(row
, col
, attr
);
6486 attr
->SetDefAttr(m_defaultCellAttr
);
6490 attr
= m_defaultCellAttr
;
6497 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6499 wxGridCellAttr
*attr
;
6500 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6502 wxASSERT_MSG( m_table
,
6503 _T("we may only be called if CanHaveAttributes() "
6504 "returned TRUE and then m_table should be !NULL") );
6506 attr
= m_table
->GetAttr(row
, col
);
6509 attr
= new wxGridCellAttr
;
6511 // artificially inc the ref count to match DecRef() in caller
6514 m_table
->SetAttr(attr
, row
, col
);
6517 CacheAttr(row
, col
, attr
);
6519 attr
->SetDefAttr(m_defaultCellAttr
);
6523 // ----------------------------------------------------------------------------
6524 // setting cell attributes: this is forwarded to the table
6525 // ----------------------------------------------------------------------------
6527 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6529 if ( CanHaveAttributes() )
6531 m_table
->SetRowAttr(attr
, row
);
6539 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
6541 if ( CanHaveAttributes() )
6543 m_table
->SetColAttr(attr
, col
);
6551 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
6553 if ( CanHaveAttributes() )
6555 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6556 attr
->SetBackgroundColour(colour
);
6561 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
6563 if ( CanHaveAttributes() )
6565 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6566 attr
->SetTextColour(colour
);
6571 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
6573 if ( CanHaveAttributes() )
6575 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6576 attr
->SetFont(font
);
6581 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
6583 if ( CanHaveAttributes() )
6585 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6586 attr
->SetAlignment(horiz
, vert
);
6591 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
6593 if ( CanHaveAttributes() )
6595 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6596 attr
->SetRenderer(renderer
);
6601 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
6603 if ( CanHaveAttributes() )
6605 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6606 attr
->SetEditor(editor
);
6611 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
6613 if ( CanHaveAttributes() )
6615 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
6616 attr
->SetReadOnly(isReadOnly
);
6621 // ----------------------------------------------------------------------------
6622 // Data type registration
6623 // ----------------------------------------------------------------------------
6625 void wxGrid::RegisterDataType(const wxString
& typeName
,
6626 wxGridCellRenderer
* renderer
,
6627 wxGridCellEditor
* editor
)
6629 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
6633 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
6635 wxString typeName
= m_table
->GetTypeName(row
, col
);
6636 return GetDefaultEditorForType(typeName
);
6639 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
6641 wxString typeName
= m_table
->GetTypeName(row
, col
);
6642 return GetDefaultRendererForType(typeName
);
6646 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
6648 int index
= m_typeRegistry
->FindDataType(typeName
);
6650 // Should we force the failure here or let it fallback to string handling???
6651 // wxFAIL_MSG(wxT("Unknown data type name"));
6654 return m_typeRegistry
->GetEditor(index
);
6658 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
6660 int index
= m_typeRegistry
->FindDataType(typeName
);
6662 // Should we force the failure here or let it fallback to string handling???
6663 // wxFAIL_MSG(wxT("Unknown data type name"));
6666 return m_typeRegistry
->GetRenderer(index
);
6670 // ----------------------------------------------------------------------------
6672 // ----------------------------------------------------------------------------
6674 void wxGrid::EnableDragRowSize( bool enable
)
6676 m_canDragRowSize
= enable
;
6680 void wxGrid::EnableDragColSize( bool enable
)
6682 m_canDragColSize
= enable
;
6685 void wxGrid::EnableDragGridSize( bool enable
)
6687 m_canDragGridSize
= enable
;
6691 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
6693 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
6695 if ( resizeExistingRows
)
6703 void wxGrid::SetRowSize( int row
, int height
)
6705 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
6707 if ( m_rowHeights
.IsEmpty() )
6709 // need to really create the array
6713 int h
= wxMax( 0, height
);
6714 int diff
= h
- m_rowHeights
[row
];
6716 m_rowHeights
[row
] = h
;
6718 for ( i
= row
; i
< m_numRows
; i
++ )
6720 m_rowBottoms
[i
] += diff
;
6725 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
6727 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
6729 if ( resizeExistingCols
)
6737 void wxGrid::SetColSize( int col
, int width
)
6739 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
6741 // should we check that it's bigger than GetColMinimalWidth(col) here?
6743 if ( m_colWidths
.IsEmpty() )
6745 // need to really create the array
6749 int w
= wxMax( 0, width
);
6750 int diff
= w
- m_colWidths
[col
];
6751 m_colWidths
[col
] = w
;
6754 for ( i
= col
; i
< m_numCols
; i
++ )
6756 m_colRights
[i
] += diff
;
6762 void wxGrid::SetColMinimalWidth( int col
, int width
)
6764 m_colMinWidths
.Put(col
, (wxObject
*)width
);
6767 int wxGrid::GetColMinimalWidth(int col
) const
6769 wxObject
*obj
= m_colMinWidths
.Get(m_dragRowOrCol
);
6770 return obj
? (int)obj
: WXGRID_MIN_COL_WIDTH
;
6773 void wxGrid::AutoSizeColumn( int col
, bool setAsMin
)
6775 wxClientDC
dc(m_gridWin
);
6777 wxCoord width
, widthMax
= 0;
6778 for ( int row
= 0; row
< m_numRows
; row
++ )
6780 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6781 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6784 width
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
).x
;
6785 if ( width
> widthMax
)
6794 // now also compare with the column label width
6795 dc
.SetFont( GetLabelFont() );
6796 dc
.GetTextExtent( GetColLabelValue(col
), &width
, NULL
);
6797 if ( width
> widthMax
)
6804 // empty column - give default width (notice that if widthMax is less
6805 // than default width but != 0, it's ok)
6806 widthMax
= m_defaultColWidth
;
6810 // leave some space around text
6814 SetColSize(col
, widthMax
);
6817 SetColMinimalWidth(col
, widthMax
);
6821 void wxGrid::AutoSizeColumns( bool setAsMin
)
6823 for ( int col
= 0; col
< m_numCols
; col
++ )
6825 AutoSizeColumn(col
, setAsMin
);
6830 // ------ cell value accessor functions
6833 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
6837 m_table
->SetValue( row
, col
, s
.c_str() );
6838 if ( !GetBatchCount() )
6840 wxClientDC
dc( m_gridWin
);
6842 DrawCell( dc
, wxGridCellCoords(row
, col
) );
6845 if ( m_currentCellCoords
.GetRow() == row
&&
6846 m_currentCellCoords
.GetCol() == col
&&
6847 IsCellEditControlEnabled())
6849 HideCellEditControl();
6850 ShowCellEditControl(); // will reread data from table
6857 // ------ Block, row and col selection
6860 void wxGrid::SelectRow( int row
, bool addToSelected
)
6864 if ( IsSelection() && addToSelected
)
6867 bool need_refresh
[4];
6871 need_refresh
[3] = FALSE
;
6875 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6876 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6877 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6878 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6882 need_refresh
[0] = TRUE
;
6883 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
6884 wxGridCellCoords ( oldTop
- 1,
6886 m_selectedTopLeft
.SetRow( row
);
6891 need_refresh
[1] = TRUE
;
6892 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
6893 wxGridCellCoords ( oldBottom
,
6896 m_selectedTopLeft
.SetCol( 0 );
6899 if ( oldBottom
< row
)
6901 need_refresh
[2] = TRUE
;
6902 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
6903 wxGridCellCoords ( row
,
6905 m_selectedBottomRight
.SetRow( row
);
6908 if ( oldRight
< m_numCols
- 1 )
6910 need_refresh
[3] = TRUE
;
6911 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6913 wxGridCellCoords ( oldBottom
,
6915 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
6918 for (i
= 0; i
< 4; i
++ )
6919 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6920 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6924 r
= SelectionToDeviceRect();
6926 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
6928 m_selectedTopLeft
.Set( row
, 0 );
6929 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
6930 r
= SelectionToDeviceRect();
6931 m_gridWin
->Refresh( FALSE
, &r
);
6934 wxGridRangeSelectEvent
gridEvt( GetId(),
6935 wxEVT_GRID_RANGE_SELECT
,
6938 m_selectedBottomRight
);
6940 GetEventHandler()->ProcessEvent(gridEvt
);
6944 void wxGrid::SelectCol( int col
, bool addToSelected
)
6946 if ( IsSelection() && addToSelected
)
6949 bool need_refresh
[4];
6953 need_refresh
[3] = FALSE
;
6956 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
6957 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
6958 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
6959 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
6961 if ( oldLeft
> col
)
6963 need_refresh
[0] = TRUE
;
6964 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
6965 wxGridCellCoords ( m_numRows
- 1,
6967 m_selectedTopLeft
.SetCol( col
);
6972 need_refresh
[1] = TRUE
;
6973 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
6974 wxGridCellCoords ( oldTop
- 1,
6976 m_selectedTopLeft
.SetRow( 0 );
6979 if ( oldRight
< col
)
6981 need_refresh
[2] = TRUE
;
6982 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
6983 wxGridCellCoords ( m_numRows
- 1,
6985 m_selectedBottomRight
.SetCol( col
);
6988 if ( oldBottom
< m_numRows
- 1 )
6990 need_refresh
[3] = TRUE
;
6991 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
6993 wxGridCellCoords ( m_numRows
- 1,
6995 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
6998 for (i
= 0; i
< 4; i
++ )
6999 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7000 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7006 r
= SelectionToDeviceRect();
7008 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7010 m_selectedTopLeft
.Set( 0, col
);
7011 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7012 r
= SelectionToDeviceRect();
7013 m_gridWin
->Refresh( FALSE
, &r
);
7016 wxGridRangeSelectEvent
gridEvt( GetId(),
7017 wxEVT_GRID_RANGE_SELECT
,
7020 m_selectedBottomRight
);
7022 GetEventHandler()->ProcessEvent(gridEvt
);
7026 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7029 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7031 if ( topRow
> bottomRow
)
7038 if ( leftCol
> rightCol
)
7045 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7046 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7048 if ( m_selectedTopLeft
!= updateTopLeft
||
7049 m_selectedBottomRight
!= updateBottomRight
)
7051 // Compute two optimal update rectangles:
7052 // Either one rectangle is a real subset of the
7053 // other, or they are (almost) disjoint!
7055 bool need_refresh
[4];
7059 need_refresh
[3] = FALSE
;
7062 // Store intermediate values
7063 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7064 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7065 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7066 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7068 // Determine the outer/inner coordinates.
7069 if (oldLeft
> leftCol
)
7075 if (oldTop
> topRow
)
7081 if (oldRight
< rightCol
)
7084 oldRight
= rightCol
;
7087 if (oldBottom
< bottomRow
)
7090 oldBottom
= bottomRow
;
7094 // Now, either the stuff marked old is the outer
7095 // rectangle or we don't have a situation where one
7096 // is contained in the other.
7098 if ( oldLeft
< leftCol
)
7100 need_refresh
[0] = TRUE
;
7101 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7103 wxGridCellCoords ( oldBottom
,
7107 if ( oldTop
< topRow
)
7109 need_refresh
[1] = TRUE
;
7110 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7112 wxGridCellCoords ( topRow
- 1,
7116 if ( oldRight
> rightCol
)
7118 need_refresh
[2] = TRUE
;
7119 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7121 wxGridCellCoords ( oldBottom
,
7125 if ( oldBottom
> bottomRow
)
7127 need_refresh
[3] = TRUE
;
7128 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7130 wxGridCellCoords ( oldBottom
,
7136 m_selectedTopLeft
= updateTopLeft
;
7137 m_selectedBottomRight
= updateBottomRight
;
7139 // various Refresh() calls
7140 for (i
= 0; i
< 4; i
++ )
7141 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7142 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7145 // only generate an event if the block is not being selected by
7146 // dragging the mouse (in which case the event will be generated in
7147 // the mouse event handler)
7148 if ( !m_isDragging
)
7150 wxGridRangeSelectEvent
gridEvt( GetId(),
7151 wxEVT_GRID_RANGE_SELECT
,
7154 m_selectedBottomRight
);
7156 GetEventHandler()->ProcessEvent(gridEvt
);
7160 void wxGrid::SelectAll()
7162 m_selectedTopLeft
.Set( 0, 0 );
7163 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7165 m_gridWin
->Refresh();
7169 void wxGrid::ClearSelection()
7171 m_selectedTopLeft
= wxGridNoCellCoords
;
7172 m_selectedBottomRight
= wxGridNoCellCoords
;
7176 // This function returns the rectangle that encloses the given block
7177 // in device coords clipped to the client size of the grid window.
7179 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7180 const wxGridCellCoords
&bottomRight
)
7182 wxRect
rect( wxGridNoCellRect
);
7185 cellRect
= CellToRect( topLeft
);
7186 if ( cellRect
!= wxGridNoCellRect
)
7192 rect
= wxRect( 0, 0, 0, 0 );
7195 cellRect
= CellToRect( bottomRight
);
7196 if ( cellRect
!= wxGridNoCellRect
)
7202 return wxGridNoCellRect
;
7205 // convert to scrolled coords
7207 int left
, top
, right
, bottom
;
7208 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7209 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7212 m_gridWin
->GetClientSize( &cw
, &ch
);
7214 rect
.SetLeft( wxMax(0, left
) );
7215 rect
.SetTop( wxMax(0, top
) );
7216 rect
.SetRight( wxMin(cw
, right
) );
7217 rect
.SetBottom( wxMin(ch
, bottom
) );
7225 // ------ Grid event classes
7228 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7230 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7231 int row
, int col
, int x
, int y
,
7232 bool control
, bool shift
, bool alt
, bool meta
)
7233 : wxNotifyEvent( type
, id
)
7239 m_control
= control
;
7244 SetEventObject(obj
);
7248 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7250 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7251 int rowOrCol
, int x
, int y
,
7252 bool control
, bool shift
, bool alt
, bool meta
)
7253 : wxNotifyEvent( type
, id
)
7255 m_rowOrCol
= rowOrCol
;
7258 m_control
= control
;
7263 SetEventObject(obj
);
7267 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7269 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7270 const wxGridCellCoords
& topLeft
,
7271 const wxGridCellCoords
& bottomRight
,
7272 bool control
, bool shift
, bool alt
, bool meta
)
7273 : wxNotifyEvent( type
, id
)
7275 m_topLeft
= topLeft
;
7276 m_bottomRight
= bottomRight
;
7277 m_control
= control
;
7282 SetEventObject(obj
);
7286 #endif // ifndef wxUSE_NEW_GRID