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"
50 #include "wx/tokenzr.h"
53 #include "wx/generic/gridsel.h"
55 // Required for wxIs... functions
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
64 struct wxGridCellWithAttr
66 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
67 : coords(row
, col
), attr(attr_
)
76 wxGridCellCoords coords
;
80 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
82 #include "wx/arrimpl.cpp"
84 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
85 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
94 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
95 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
96 const wxPoint
&pos
, const wxSize
&size
);
101 void OnPaint( wxPaintEvent
& event
);
102 void OnMouseEvent( wxMouseEvent
& event
);
103 void OnKeyDown( wxKeyEvent
& event
);
105 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
106 DECLARE_EVENT_TABLE()
110 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
113 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
114 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
115 const wxPoint
&pos
, const wxSize
&size
);
120 void OnPaint( wxPaintEvent
&event
);
121 void OnMouseEvent( wxMouseEvent
& event
);
122 void OnKeyDown( wxKeyEvent
& event
);
124 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
125 DECLARE_EVENT_TABLE()
129 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
132 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
133 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
134 const wxPoint
&pos
, const wxSize
&size
);
139 void OnMouseEvent( wxMouseEvent
& event
);
140 void OnKeyDown( wxKeyEvent
& event
);
141 void OnPaint( wxPaintEvent
& event
);
143 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
144 DECLARE_EVENT_TABLE()
147 class WXDLLEXPORT wxGridWindow
: public wxPanel
152 m_owner
= (wxGrid
*)NULL
;
153 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
154 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
157 wxGridWindow( wxGrid
*parent
,
158 wxGridRowLabelWindow
*rowLblWin
,
159 wxGridColLabelWindow
*colLblWin
,
160 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
163 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
167 wxGridRowLabelWindow
*m_rowLabelWin
;
168 wxGridColLabelWindow
*m_colLabelWin
;
170 void OnPaint( wxPaintEvent
&event
);
171 void OnMouseEvent( wxMouseEvent
& event
);
172 void OnKeyDown( wxKeyEvent
& );
173 void OnEraseBackground( wxEraseEvent
& );
176 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
177 DECLARE_EVENT_TABLE()
182 class wxGridCellEditorEvtHandler
: public wxEvtHandler
185 wxGridCellEditorEvtHandler()
186 : m_grid(0), m_editor(0)
188 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
189 : m_grid(grid
), m_editor(editor
)
192 void OnKeyDown(wxKeyEvent
& event
);
193 void OnChar(wxKeyEvent
& event
);
197 wxGridCellEditor
* m_editor
;
198 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
199 DECLARE_EVENT_TABLE()
203 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
204 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
205 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
206 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
211 // ----------------------------------------------------------------------------
212 // the internal data representation used by wxGridCellAttrProvider
213 // ----------------------------------------------------------------------------
215 // this class stores attributes set for cells
216 class WXDLLEXPORT wxGridCellAttrData
219 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
220 wxGridCellAttr
*GetAttr(int row
, int col
) const;
221 void UpdateAttrRows( size_t pos
, int numRows
);
222 void UpdateAttrCols( size_t pos
, int numCols
);
225 // searches for the attr for given cell, returns wxNOT_FOUND if not found
226 int FindIndex(int row
, int col
) const;
228 wxGridCellWithAttrArray m_attrs
;
231 // this class stores attributes set for rows or columns
232 class WXDLLEXPORT wxGridRowOrColAttrData
235 // empty ctor to suppress warnings
236 wxGridRowOrColAttrData() { }
237 ~wxGridRowOrColAttrData();
239 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
240 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
241 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
244 wxArrayInt m_rowsOrCols
;
245 wxArrayAttrs m_attrs
;
248 // NB: this is just a wrapper around 3 objects: one which stores cell
249 // attributes, and 2 others for row/col ones
250 class WXDLLEXPORT wxGridCellAttrProviderData
253 wxGridCellAttrData m_cellAttrs
;
254 wxGridRowOrColAttrData m_rowAttrs
,
259 // ----------------------------------------------------------------------------
260 // data structures used for the data type registry
261 // ----------------------------------------------------------------------------
263 struct wxGridDataTypeInfo
265 wxGridDataTypeInfo(const wxString
& typeName
,
266 wxGridCellRenderer
* renderer
,
267 wxGridCellEditor
* editor
)
268 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
271 ~wxGridDataTypeInfo()
273 wxSafeDecRef(m_renderer
);
274 wxSafeDecRef(m_editor
);
278 wxGridCellRenderer
* m_renderer
;
279 wxGridCellEditor
* m_editor
;
283 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
286 class WXDLLEXPORT wxGridTypeRegistry
289 ~wxGridTypeRegistry();
291 void RegisterDataType(const wxString
& typeName
,
292 wxGridCellRenderer
* renderer
,
293 wxGridCellEditor
* editor
);
295 // find one of already registered data types
296 int FindRegisteredDataType(const wxString
& typeName
);
298 // try to FindRegisteredDataType(), if this fails and typeName is one of
299 // standard typenames, register it and return its index
300 int FindDataType(const wxString
& typeName
);
302 // try to FindDataType(), if it fails see if it is not one of already
303 // registered data types with some params in which case clone the
304 // registered data type and set params for it
305 int FindOrCloneDataType(const wxString
& typeName
);
307 wxGridCellRenderer
* GetRenderer(int index
);
308 wxGridCellEditor
* GetEditor(int index
);
311 wxGridDataTypeInfoArray m_typeinfo
;
314 // ----------------------------------------------------------------------------
315 // conditional compilation
316 // ----------------------------------------------------------------------------
318 #ifndef WXGRID_DRAW_LINES
319 #define WXGRID_DRAW_LINES 1
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 //#define DEBUG_ATTR_CACHE
327 #ifdef DEBUG_ATTR_CACHE
328 static size_t gs_nAttrCacheHits
= 0;
329 static size_t gs_nAttrCacheMisses
= 0;
330 #endif // DEBUG_ATTR_CACHE
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
337 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
340 // TODO: fixed so far - make configurable later (and also different for x/y)
341 static const size_t GRID_SCROLL_LINE
= 10;
343 // the size of hash tables used a bit everywhere (the max number of elements
344 // in these hash tables is the number of rows/columns)
345 static const int GRID_HASH_SIZE
= 100;
347 // ============================================================================
349 // ============================================================================
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
355 wxGridCellEditor::wxGridCellEditor()
361 wxGridCellEditor::~wxGridCellEditor()
366 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
367 wxWindowID
WXUNUSED(id
),
368 wxEvtHandler
* evtHandler
)
371 m_control
->PushEventHandler(evtHandler
);
374 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
375 wxGridCellAttr
*attr
)
377 // erase the background because we might not fill the cell
378 wxClientDC
dc(m_control
->GetParent());
379 dc
.SetPen(*wxTRANSPARENT_PEN
);
380 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
381 dc
.DrawRectangle(rectCell
);
383 // redraw the control we just painted over
384 m_control
->Refresh();
387 void wxGridCellEditor::Destroy()
391 m_control
->PopEventHandler(TRUE
/* delete it*/);
393 m_control
->Destroy();
398 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
400 wxASSERT_MSG(m_control
,
401 wxT("The wxGridCellEditor must be Created first!"));
402 m_control
->Show(show
);
406 // set the colours/fonts if we have any
409 m_colFgOld
= m_control
->GetForegroundColour();
410 m_control
->SetForegroundColour(attr
->GetTextColour());
412 m_colBgOld
= m_control
->GetBackgroundColour();
413 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
415 m_fontOld
= m_control
->GetFont();
416 m_control
->SetFont(attr
->GetFont());
418 // can't do anything more in the base class version, the other
419 // attributes may only be used by the derived classes
424 // restore the standard colours fonts
425 if ( m_colFgOld
.Ok() )
427 m_control
->SetForegroundColour(m_colFgOld
);
428 m_colFgOld
= wxNullColour
;
431 if ( m_colBgOld
.Ok() )
433 m_control
->SetBackgroundColour(m_colBgOld
);
434 m_colBgOld
= wxNullColour
;
437 if ( m_fontOld
.Ok() )
439 m_control
->SetFont(m_fontOld
);
440 m_fontOld
= wxNullFont
;
445 void wxGridCellEditor::SetSize(const wxRect
& rect
)
447 wxASSERT_MSG(m_control
,
448 wxT("The wxGridCellEditor must be Created first!"));
449 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
452 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
458 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
463 void wxGridCellEditor::StartingClick()
467 // ----------------------------------------------------------------------------
468 // wxGridCellTextEditor
469 // ----------------------------------------------------------------------------
471 wxGridCellTextEditor::wxGridCellTextEditor()
476 void wxGridCellTextEditor::Create(wxWindow
* parent
,
478 wxEvtHandler
* evtHandler
)
480 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
481 wxDefaultPosition
, wxDefaultSize
482 #if defined(__WXMSW__)
483 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
487 // TODO: use m_maxChars
489 wxGridCellEditor::Create(parent
, id
, evtHandler
);
492 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
493 wxGridCellAttr
* WXUNUSED(attr
))
495 // as we fill the entire client area, don't do anything here to minimize
499 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
501 wxRect
rect(rectOrig
);
503 // Make the edit control large enough to allow for internal
506 // TODO: remove this if the text ctrl sizing is improved esp. for
509 #if defined(__WXGTK__)
518 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
519 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
520 #if defined(__WXMOTIF__)
524 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
525 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
526 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
527 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
530 wxGridCellEditor::SetSize(rect
);
533 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
535 wxASSERT_MSG(m_control
,
536 wxT("The wxGridCellEditor must be Created first!"));
538 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
540 DoBeginEdit(m_startValue
);
543 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
545 Text()->SetValue(startValue
);
546 Text()->SetInsertionPointEnd();
550 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
553 wxASSERT_MSG(m_control
,
554 wxT("The wxGridCellEditor must be Created first!"));
556 bool changed
= FALSE
;
557 wxString value
= Text()->GetValue();
558 if (value
!= m_startValue
)
562 grid
->GetTable()->SetValue(row
, col
, value
);
564 m_startValue
= wxEmptyString
;
565 Text()->SetValue(m_startValue
);
571 void wxGridCellTextEditor::Reset()
573 wxASSERT_MSG(m_control
,
574 wxT("The wxGridCellEditor must be Created first!"));
576 DoReset(m_startValue
);
579 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
581 Text()->SetValue(startValue
);
582 Text()->SetInsertionPointEnd();
585 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
587 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
589 // insert the key in the control
590 int keycode
= (int)event
.KeyCode();
591 if ( isprint(keycode
) )
593 // FIXME this is not going to work for non letters...
594 if ( !event
.ShiftDown() )
596 keycode
= tolower(keycode
);
599 Text()->AppendText((wxChar
)keycode
);
609 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
611 #if defined(__WXMOTIF__) || defined(__WXGTK__)
612 // wxMotif needs a little extra help...
613 long pos
= Text()->GetInsertionPoint();
614 wxString
s( Text()->GetValue() );
615 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
617 Text()->SetInsertionPoint( pos
);
619 // the other ports can handle a Return key press
625 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
635 if ( !params
.ToLong(&tmp
) )
637 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string "
638 "'%s' ignored"), params
.c_str());
642 m_maxChars
= (size_t)tmp
;
647 // ----------------------------------------------------------------------------
648 // wxGridCellNumberEditor
649 // ----------------------------------------------------------------------------
651 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
657 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
659 wxEvtHandler
* evtHandler
)
663 // create a spin ctrl
664 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
665 wxDefaultPosition
, wxDefaultSize
,
669 wxGridCellEditor::Create(parent
, id
, evtHandler
);
673 // just a text control
674 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
677 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
678 #endif // wxUSE_VALIDATORS
682 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
684 // first get the value
685 wxGridTableBase
*table
= grid
->GetTable();
686 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
688 m_valueOld
= table
->GetValueAsLong(row
, col
);
692 wxString sValue
= table
->GetValue(row
, col
);
693 if (! sValue
.ToLong(&m_valueOld
))
695 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
702 Spin()->SetValue((int)m_valueOld
);
706 DoBeginEdit(GetString());
710 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
718 value
= Spin()->GetValue();
719 changed
= value
!= m_valueOld
;
723 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
728 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
729 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
731 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%ld", value
));
737 void wxGridCellNumberEditor::Reset()
741 Spin()->SetValue((int)m_valueOld
);
745 DoReset(GetString());
749 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
753 int keycode
= (int) event
.KeyCode();
754 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
756 wxGridCellTextEditor::StartingKey(event
);
766 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
777 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
781 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
785 // skip the error message below
790 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string "
791 "'%s' ignored"), params
.c_str());
795 // ----------------------------------------------------------------------------
796 // wxGridCellFloatEditor
797 // ----------------------------------------------------------------------------
799 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
801 wxEvtHandler
* evtHandler
)
803 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
806 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
807 #endif // wxUSE_VALIDATORS
810 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
812 // first get the value
813 wxGridTableBase
*table
= grid
->GetTable();
814 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
816 m_valueOld
= table
->GetValueAsDouble(row
, col
);
820 wxString sValue
= table
->GetValue(row
, col
);
821 if (! sValue
.ToDouble(&m_valueOld
))
823 wxFAIL_MSG( _T("this cell doesn't have float value") );
828 DoBeginEdit(GetString());
831 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
835 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
837 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
838 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
840 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%f", value
));
850 void wxGridCellFloatEditor::Reset()
852 DoReset(GetString());
855 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
857 int keycode
= (int)event
.KeyCode();
858 if ( isdigit(keycode
) ||
859 keycode
== '+' || keycode
== '-' || keycode
== '.' )
861 wxGridCellTextEditor::StartingKey(event
);
870 // ----------------------------------------------------------------------------
871 // wxGridCellBoolEditor
872 // ----------------------------------------------------------------------------
874 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
876 wxEvtHandler
* evtHandler
)
878 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
879 wxDefaultPosition
, wxDefaultSize
,
882 wxGridCellEditor::Create(parent
, id
, evtHandler
);
885 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
888 wxSize size
= m_control
->GetSize();
889 wxCoord minSize
= wxMin(r
.width
, r
.height
);
891 // check if the checkbox is not too big/small for this cell
892 wxSize sizeBest
= m_control
->GetBestSize();
893 if ( !(size
== sizeBest
) )
895 // reset to default size if it had been made smaller
901 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
903 // leave 1 pixel margin
904 size
.x
= size
.y
= minSize
- 2;
911 m_control
->SetSize(size
);
914 // position it in the centre of the rectangle (TODO: support alignment?)
916 #if defined(__WXGTK__) || defined (__WXMOTIF__)
917 // the checkbox without label still has some space to the right in wxGTK,
918 // so shift it to the right
920 #elif defined(__WXMSW__)
921 // here too, but in other way
926 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
929 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
931 m_control
->Show(show
);
935 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
936 CBox()->SetBackgroundColour(colBg
);
940 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
942 wxASSERT_MSG(m_control
,
943 wxT("The wxGridCellEditor must be Created first!"));
945 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
946 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
948 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
949 CBox()->SetValue(m_startValue
);
953 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
956 wxASSERT_MSG(m_control
,
957 wxT("The wxGridCellEditor must be Created first!"));
959 bool changed
= FALSE
;
960 bool value
= CBox()->GetValue();
961 if ( value
!= m_startValue
)
966 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
967 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
969 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
975 void wxGridCellBoolEditor::Reset()
977 wxASSERT_MSG(m_control
,
978 wxT("The wxGridCellEditor must be Created first!"));
980 CBox()->SetValue(m_startValue
);
983 void wxGridCellBoolEditor::StartingClick()
985 CBox()->SetValue(!CBox()->GetValue());
988 // ----------------------------------------------------------------------------
989 // wxGridCellChoiceEditor
990 // ----------------------------------------------------------------------------
992 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
993 const wxChar
* choices
[],
995 : m_allowOthers(allowOthers
)
999 m_choices
.Alloc(count
);
1000 for ( size_t n
= 0; n
< count
; n
++ )
1002 m_choices
.Add(choices
[n
]);
1007 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1009 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1010 editor
->m_allowOthers
= m_allowOthers
;
1011 editor
->m_choices
= m_choices
;
1016 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1018 wxEvtHandler
* evtHandler
)
1020 size_t count
= m_choices
.GetCount();
1021 wxString
*choices
= new wxString
[count
];
1022 for ( size_t n
= 0; n
< count
; n
++ )
1024 choices
[n
] = m_choices
[n
];
1027 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1028 wxDefaultPosition
, wxDefaultSize
,
1030 m_allowOthers
? 0 : wxCB_READONLY
);
1034 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1037 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1038 wxGridCellAttr
* attr
)
1040 // as we fill the entire client area, don't do anything here to minimize
1043 // TODO: It doesn't actually fill the client area since the height of a
1044 // combo always defaults to the standard... Until someone has time to
1045 // figure out the right rectangle to paint, just do it the normal way...
1046 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1049 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1051 wxASSERT_MSG(m_control
,
1052 wxT("The wxGridCellEditor must be Created first!"));
1054 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1056 Combo()->SetValue(m_startValue
);
1057 size_t count
= m_choices
.GetCount();
1058 for (size_t i
=0; i
<count
; i
++)
1060 if (m_startValue
== m_choices
[i
])
1062 Combo()->SetSelection(i
);
1066 Combo()->SetInsertionPointEnd();
1067 Combo()->SetFocus();
1070 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1073 wxString value
= Combo()->GetValue();
1074 bool changed
= value
!= m_startValue
;
1077 grid
->GetTable()->SetValue(row
, col
, value
);
1079 m_startValue
= wxEmptyString
;
1080 Combo()->SetValue(m_startValue
);
1085 void wxGridCellChoiceEditor::Reset()
1087 Combo()->SetValue(m_startValue
);
1088 Combo()->SetInsertionPointEnd();
1091 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1101 wxStringTokenizer
tk(params
, _T(','));
1102 while ( tk
.HasMoreTokens() )
1104 m_choices
.Add(tk
.GetNextToken());
1108 // ----------------------------------------------------------------------------
1109 // wxGridCellEditorEvtHandler
1110 // ----------------------------------------------------------------------------
1112 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1114 switch ( event
.KeyCode() )
1118 m_grid
->DisableCellEditControl();
1122 event
.Skip( m_grid
->ProcessEvent( event
) );
1126 if (!m_grid
->ProcessEvent(event
))
1127 m_editor
->HandleReturn(event
);
1136 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1138 switch ( event
.KeyCode() )
1150 // ----------------------------------------------------------------------------
1151 // wxGridCellWorker is an (almost) empty common base class for
1152 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1153 // ----------------------------------------------------------------------------
1155 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1160 wxGridCellWorker::~wxGridCellWorker()
1164 // ============================================================================
1166 // ============================================================================
1168 // ----------------------------------------------------------------------------
1169 // wxGridCellRenderer
1170 // ----------------------------------------------------------------------------
1172 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1173 wxGridCellAttr
& attr
,
1179 dc
.SetBackgroundMode( wxSOLID
);
1183 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1187 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1190 dc
.SetPen( *wxTRANSPARENT_PEN
);
1191 dc
.DrawRectangle(rect
);
1194 // ----------------------------------------------------------------------------
1195 // wxGridCellStringRenderer
1196 // ----------------------------------------------------------------------------
1198 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1199 wxGridCellAttr
& attr
,
1203 dc
.SetBackgroundMode( wxTRANSPARENT
);
1205 // TODO some special colours for attr.IsReadOnly() case?
1209 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1210 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1214 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1215 dc
.SetTextForeground( attr
.GetTextColour() );
1218 dc
.SetFont( attr
.GetFont() );
1221 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1223 const wxString
& text
)
1226 dc
.SetFont(attr
.GetFont());
1227 dc
.GetTextExtent(text
, &x
, &y
);
1229 return wxSize(x
, y
);
1232 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1233 wxGridCellAttr
& attr
,
1237 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1240 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1241 wxGridCellAttr
& attr
,
1243 const wxRect
& rectCell
,
1247 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1249 // now we only have to draw the text
1250 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1253 attr
.GetAlignment(&hAlign
, &vAlign
);
1255 wxRect rect
= rectCell
;
1258 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1259 rect
, hAlign
, vAlign
);
1262 // ----------------------------------------------------------------------------
1263 // wxGridCellNumberRenderer
1264 // ----------------------------------------------------------------------------
1266 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1268 wxGridTableBase
*table
= grid
.GetTable();
1270 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1272 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1276 text
= table
->GetValue(row
, col
);
1282 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1283 wxGridCellAttr
& attr
,
1285 const wxRect
& rectCell
,
1289 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1291 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1293 // draw the text right aligned by default
1295 attr
.GetAlignment(&hAlign
, &vAlign
);
1298 wxRect rect
= rectCell
;
1301 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1304 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1305 wxGridCellAttr
& attr
,
1309 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1312 // ----------------------------------------------------------------------------
1313 // wxGridCellFloatRenderer
1314 // ----------------------------------------------------------------------------
1316 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1319 SetPrecision(precision
);
1322 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1324 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1325 renderer
->m_width
= m_width
;
1326 renderer
->m_precision
= m_precision
;
1327 renderer
->m_format
= m_format
;
1332 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1334 wxGridTableBase
*table
= grid
.GetTable();
1339 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1341 val
= table
->GetValueAsDouble(row
, col
);
1346 text
= table
->GetValue(row
, col
);
1347 hasDouble
= text
.ToDouble(&val
);
1354 if ( m_width
== -1 )
1356 // default width/precision
1357 m_format
= _T("%f");
1359 else if ( m_precision
== -1 )
1361 // default precision
1362 m_format
.Printf(_T("%%%d.f"), m_width
);
1366 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1370 text
.Printf(m_format
, val
);
1372 //else: text already contains the string
1377 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1378 wxGridCellAttr
& attr
,
1380 const wxRect
& rectCell
,
1384 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1386 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1388 // draw the text right aligned by default
1390 attr
.GetAlignment(&hAlign
, &vAlign
);
1393 wxRect rect
= rectCell
;
1396 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1399 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1400 wxGridCellAttr
& attr
,
1404 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1407 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1413 // reset to defaults
1419 wxString tmp
= params
.BeforeFirst(_T(','));
1423 if ( !tmp
.ToLong(&width
) )
1429 SetWidth((int)width
);
1431 tmp
= params
.AfterFirst(_T(','));
1435 if ( !tmp
.ToLong(&precision
) )
1441 SetPrecision((int)precision
);
1449 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string "
1450 "'%s ignored"), params
.c_str());
1455 // ----------------------------------------------------------------------------
1456 // wxGridCellBoolRenderer
1457 // ----------------------------------------------------------------------------
1459 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1461 // FIXME these checkbox size calculations are really ugly...
1463 // between checkmark and box
1464 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1466 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1467 wxGridCellAttr
& WXUNUSED(attr
),
1472 // compute it only once (no locks for MT safeness in GUI thread...)
1473 if ( !ms_sizeCheckMark
.x
)
1475 // get checkbox size
1476 wxCoord checkSize
= 0;
1477 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1478 wxSize size
= checkbox
->GetBestSize();
1479 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1481 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1482 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1483 checkSize
-= size
.y
/ 2;
1488 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1491 return ms_sizeCheckMark
;
1494 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1495 wxGridCellAttr
& attr
,
1501 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1503 // draw a check mark in the centre (ignoring alignment - TODO)
1504 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1506 // don't draw outside the cell
1507 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1508 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1510 // and even leave (at least) 1 pixel margin
1511 size
.x
= size
.y
= minSize
- 2;
1514 // draw a border around checkmark
1516 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1517 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1518 rectBorder
.width
= size
.x
;
1519 rectBorder
.height
= size
.y
;
1522 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1523 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1525 value
= !!grid
.GetTable()->GetValue(row
, col
);
1529 wxRect rectMark
= rectBorder
;
1531 // MSW DrawCheckMark() is weird (and should probably be changed...)
1532 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1536 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1539 dc
.SetTextForeground(attr
.GetTextColour());
1540 dc
.DrawCheckMark(rectMark
);
1543 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1544 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1545 dc
.DrawRectangle(rectBorder
);
1548 // ----------------------------------------------------------------------------
1550 // ----------------------------------------------------------------------------
1552 wxGridCellAttr
*wxGridCellAttr::Clone() const
1554 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1555 if ( HasTextColour() )
1556 attr
->SetTextColour(GetTextColour());
1557 if ( HasBackgroundColour() )
1558 attr
->SetBackgroundColour(GetBackgroundColour());
1560 attr
->SetFont(GetFont());
1561 if ( HasAlignment() )
1562 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1566 attr
->SetRenderer(m_renderer
);
1567 m_renderer
->IncRef();
1571 attr
->SetEditor(m_editor
);
1576 attr
->SetReadOnly();
1578 attr
->SetDefAttr(m_defGridAttr
);
1583 const wxColour
& wxGridCellAttr::GetTextColour() const
1585 if (HasTextColour())
1589 else if (m_defGridAttr
!= this)
1591 return m_defGridAttr
->GetTextColour();
1595 wxFAIL_MSG(wxT("Missing default cell attribute"));
1596 return wxNullColour
;
1601 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1603 if (HasBackgroundColour())
1605 else if (m_defGridAttr
!= this)
1606 return m_defGridAttr
->GetBackgroundColour();
1609 wxFAIL_MSG(wxT("Missing default cell attribute"));
1610 return wxNullColour
;
1615 const wxFont
& wxGridCellAttr::GetFont() const
1619 else if (m_defGridAttr
!= this)
1620 return m_defGridAttr
->GetFont();
1623 wxFAIL_MSG(wxT("Missing default cell attribute"));
1629 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1633 if ( hAlign
) *hAlign
= m_hAlign
;
1634 if ( vAlign
) *vAlign
= m_vAlign
;
1636 else if (m_defGridAttr
!= this)
1637 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1640 wxFAIL_MSG(wxT("Missing default cell attribute"));
1645 // GetRenderer and GetEditor use a slightly different decision path about
1646 // which attribute to use. If a non-default attr object has one then it is
1647 // used, otherwise the default editor or renderer is fetched from the grid and
1648 // used. It should be the default for the data type of the cell. If it is
1649 // NULL (because the table has a type that the grid does not have in its
1650 // registry,) then the grid's default editor or renderer is used.
1652 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1654 wxGridCellRenderer
* renderer
= NULL
;
1656 if ( m_defGridAttr
!= this || grid
== NULL
)
1658 renderer
= m_renderer
; // use local attribute
1663 if ( !renderer
&& grid
) // get renderer for the data type
1665 // GetDefaultRendererForCell() will do IncRef() for us
1666 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1671 // if we still don't have one then use the grid default
1672 // (no need for IncRef() here neither)
1673 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1678 wxFAIL_MSG(wxT("Missing default cell attribute"));
1684 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1686 wxGridCellEditor
* editor
= NULL
;
1688 if ( m_defGridAttr
!= this || grid
== NULL
)
1690 editor
= m_editor
; // use local attribute
1695 if ( !editor
&& grid
) // get renderer for the data type
1696 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1699 // if we still don't have one then use the grid default
1700 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1704 wxFAIL_MSG(wxT("Missing default cell attribute"));
1710 // ----------------------------------------------------------------------------
1711 // wxGridCellAttrData
1712 // ----------------------------------------------------------------------------
1714 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1716 int n
= FindIndex(row
, col
);
1717 if ( n
== wxNOT_FOUND
)
1719 // add the attribute
1720 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1726 // change the attribute
1727 m_attrs
[(size_t)n
].attr
= attr
;
1731 // remove this attribute
1732 m_attrs
.RemoveAt((size_t)n
);
1737 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1739 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1741 int n
= FindIndex(row
, col
);
1742 if ( n
!= wxNOT_FOUND
)
1744 attr
= m_attrs
[(size_t)n
].attr
;
1751 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1753 size_t count
= m_attrs
.GetCount();
1754 for ( size_t n
= 0; n
< count
; n
++ )
1756 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1757 wxCoord row
= coords
.GetRow();
1758 if ((size_t)row
>= pos
)
1762 // If rows inserted, include row counter where necessary
1763 coords
.SetRow(row
+ numRows
);
1765 else if (numRows
< 0)
1767 // If rows deleted ...
1768 if ((size_t)row
>= pos
- numRows
)
1770 // ...either decrement row counter (if row still exists)...
1771 coords
.SetRow(row
+ numRows
);
1775 // ...or remove the attribute
1776 m_attrs
.RemoveAt((size_t)n
);
1784 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1786 size_t count
= m_attrs
.GetCount();
1787 for ( size_t n
= 0; n
< count
; n
++ )
1789 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1790 wxCoord col
= coords
.GetCol();
1791 if ( (size_t)col
>= pos
)
1795 // If rows inserted, include row counter where necessary
1796 coords
.SetCol(col
+ numCols
);
1798 else if (numCols
< 0)
1800 // If rows deleted ...
1801 if ((size_t)col
>= pos
- numCols
)
1803 // ...either decrement row counter (if row still exists)...
1804 coords
.SetCol(col
+ numCols
);
1808 // ...or remove the attribute
1809 m_attrs
.RemoveAt((size_t)n
);
1817 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1819 size_t count
= m_attrs
.GetCount();
1820 for ( size_t n
= 0; n
< count
; n
++ )
1822 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1823 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1832 // ----------------------------------------------------------------------------
1833 // wxGridRowOrColAttrData
1834 // ----------------------------------------------------------------------------
1836 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1838 size_t count
= m_attrs
.Count();
1839 for ( size_t n
= 0; n
< count
; n
++ )
1841 m_attrs
[n
]->DecRef();
1845 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1847 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1849 int n
= m_rowsOrCols
.Index(rowOrCol
);
1850 if ( n
!= wxNOT_FOUND
)
1852 attr
= m_attrs
[(size_t)n
];
1859 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1861 int i
= m_rowsOrCols
.Index(rowOrCol
);
1862 if ( i
== wxNOT_FOUND
)
1864 // add the attribute
1865 m_rowsOrCols
.Add(rowOrCol
);
1870 size_t n
= (size_t)i
;
1873 // change the attribute
1874 m_attrs
[n
]->DecRef();
1879 // remove this attribute
1880 m_attrs
[n
]->DecRef();
1881 m_rowsOrCols
.RemoveAt(n
);
1882 m_attrs
.RemoveAt(n
);
1887 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1889 size_t count
= m_attrs
.GetCount();
1890 for ( size_t n
= 0; n
< count
; n
++ )
1892 int & rowOrCol
= m_rowsOrCols
[n
];
1893 if ( (size_t)rowOrCol
>= pos
)
1895 if ( numRowsOrCols
> 0 )
1897 // If rows inserted, include row counter where necessary
1898 rowOrCol
+= numRowsOrCols
;
1900 else if ( numRowsOrCols
< 0)
1902 // If rows deleted, either decrement row counter (if row still exists)
1903 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1904 rowOrCol
+= numRowsOrCols
;
1907 m_rowsOrCols
.RemoveAt((size_t)n
);
1908 m_attrs
.RemoveAt((size_t)n
);
1916 // ----------------------------------------------------------------------------
1917 // wxGridCellAttrProvider
1918 // ----------------------------------------------------------------------------
1920 wxGridCellAttrProvider::wxGridCellAttrProvider()
1922 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1925 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1930 void wxGridCellAttrProvider::InitData()
1932 m_data
= new wxGridCellAttrProviderData
;
1935 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1937 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1940 // first look for the attribute of this specific cell
1941 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1945 // then look for the col attr (col attributes are more common than
1946 // the row ones, hence they have priority)
1947 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1952 // finally try the row attributes
1953 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1960 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1966 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1969 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1974 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1977 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1982 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1985 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1989 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1991 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1995 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1999 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2001 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2005 // ----------------------------------------------------------------------------
2006 // wxGridTypeRegistry
2007 // ----------------------------------------------------------------------------
2009 wxGridTypeRegistry::~wxGridTypeRegistry()
2011 size_t count
= m_typeinfo
.Count();
2012 for ( size_t i
= 0; i
< count
; i
++ )
2013 delete m_typeinfo
[i
];
2017 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2018 wxGridCellRenderer
* renderer
,
2019 wxGridCellEditor
* editor
)
2021 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2023 // is it already registered?
2024 int loc
= FindRegisteredDataType(typeName
);
2025 if ( loc
!= wxNOT_FOUND
)
2027 delete m_typeinfo
[loc
];
2028 m_typeinfo
[loc
] = info
;
2032 m_typeinfo
.Add(info
);
2036 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2038 size_t count
= m_typeinfo
.GetCount();
2039 for ( size_t i
= 0; i
< count
; i
++ )
2041 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2050 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2052 int index
= FindRegisteredDataType(typeName
);
2053 if ( index
== wxNOT_FOUND
)
2055 // check whether this is one of the standard ones, in which case
2056 // register it "on the fly"
2057 if ( typeName
== wxGRID_VALUE_STRING
)
2059 RegisterDataType(wxGRID_VALUE_STRING
,
2060 new wxGridCellStringRenderer
,
2061 new wxGridCellTextEditor
);
2063 else if ( typeName
== wxGRID_VALUE_BOOL
)
2065 RegisterDataType(wxGRID_VALUE_BOOL
,
2066 new wxGridCellBoolRenderer
,
2067 new wxGridCellBoolEditor
);
2069 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2071 RegisterDataType(wxGRID_VALUE_NUMBER
,
2072 new wxGridCellNumberRenderer
,
2073 new wxGridCellNumberEditor
);
2075 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2077 RegisterDataType(wxGRID_VALUE_FLOAT
,
2078 new wxGridCellFloatRenderer
,
2079 new wxGridCellFloatEditor
);
2081 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2083 RegisterDataType(wxGRID_VALUE_CHOICE
,
2084 new wxGridCellStringRenderer
,
2085 new wxGridCellChoiceEditor
);
2092 // we get here only if just added the entry for this type, so return
2094 index
= m_typeinfo
.GetCount() - 1;
2100 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2102 int index
= FindDataType(typeName
);
2103 if ( index
== wxNOT_FOUND
)
2105 // the first part of the typename is the "real" type, anything after ':'
2106 // are the parameters for the renderer
2107 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2108 if ( index
== wxNOT_FOUND
)
2113 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2114 wxGridCellRenderer
*rendererOld
= renderer
;
2115 renderer
= renderer
->Clone();
2116 rendererOld
->DecRef();
2118 wxGridCellEditor
*editor
= GetEditor(index
);
2119 wxGridCellEditor
*editorOld
= editor
;
2120 editor
= editor
->Clone();
2121 editorOld
->DecRef();
2123 // do it even if there are no parameters to reset them to defaults
2124 wxString params
= typeName
.AfterFirst(_T(':'));
2125 renderer
->SetParameters(params
);
2126 editor
->SetParameters(params
);
2128 // register the new typename
2129 RegisterDataType(typeName
, renderer
, editor
);
2131 // we just registered it, it's the last one
2132 index
= m_typeinfo
.GetCount() - 1;
2138 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2140 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2145 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2147 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2152 // ----------------------------------------------------------------------------
2154 // ----------------------------------------------------------------------------
2156 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2159 wxGridTableBase::wxGridTableBase()
2161 m_view
= (wxGrid
*) NULL
;
2162 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2165 wxGridTableBase::~wxGridTableBase()
2167 delete m_attrProvider
;
2170 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2172 delete m_attrProvider
;
2173 m_attrProvider
= attrProvider
;
2176 bool wxGridTableBase::CanHaveAttributes()
2178 if ( ! GetAttrProvider() )
2180 // use the default attr provider by default
2181 SetAttrProvider(new wxGridCellAttrProvider
);
2186 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2188 if ( m_attrProvider
)
2189 return m_attrProvider
->GetAttr(row
, col
);
2191 return (wxGridCellAttr
*)NULL
;
2194 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2196 if ( m_attrProvider
)
2198 m_attrProvider
->SetAttr(attr
, row
, col
);
2202 // as we take ownership of the pointer and don't store it, we must
2208 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2210 if ( m_attrProvider
)
2212 m_attrProvider
->SetRowAttr(attr
, row
);
2216 // as we take ownership of the pointer and don't store it, we must
2222 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2224 if ( m_attrProvider
)
2226 m_attrProvider
->SetColAttr(attr
, col
);
2230 // as we take ownership of the pointer and don't store it, we must
2236 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
2238 if ( m_attrProvider
)
2240 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
2244 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
2246 if ( m_attrProvider
)
2248 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
2252 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2253 size_t WXUNUSED(numRows
) )
2255 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
2256 "but your derived table class does not override this function") );
2261 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2263 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
2264 "but your derived table class does not override this function"));
2269 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2270 size_t WXUNUSED(numRows
) )
2272 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
2273 "but your derived table class does not override this function"));
2278 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2279 size_t WXUNUSED(numCols
) )
2281 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
2282 "but your derived table class does not override this function"));
2287 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2289 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
2290 "but your derived table class does not override this function"));
2295 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2296 size_t WXUNUSED(numCols
) )
2298 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
2299 "but your derived table class does not override this function"));
2305 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2308 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2309 // how much it makes sense to us geeks.
2313 wxString
wxGridTableBase::GetColLabelValue( int col
)
2315 // default col labels are:
2316 // cols 0 to 25 : A-Z
2317 // cols 26 to 675 : AA-ZZ
2322 for ( n
= 1; ; n
++ )
2324 s
+= (_T('A') + (wxChar
)( col%26
));
2326 if ( col
< 0 ) break;
2329 // reverse the string...
2331 for ( i
= 0; i
< n
; i
++ )
2340 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2342 return wxGRID_VALUE_STRING
;
2345 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2346 const wxString
& typeName
)
2348 return typeName
== wxGRID_VALUE_STRING
;
2351 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2353 return CanGetValueAs(row
, col
, typeName
);
2356 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2361 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2366 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2371 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2372 long WXUNUSED(value
) )
2376 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2377 double WXUNUSED(value
) )
2381 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2382 bool WXUNUSED(value
) )
2387 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2388 const wxString
& WXUNUSED(typeName
) )
2393 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2394 const wxString
& WXUNUSED(typeName
),
2395 void* WXUNUSED(value
) )
2399 //////////////////////////////////////////////////////////////////////
2401 // Message class for the grid table to send requests and notifications
2405 wxGridTableMessage::wxGridTableMessage()
2407 m_table
= (wxGridTableBase
*) NULL
;
2413 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2414 int commandInt1
, int commandInt2
)
2418 m_comInt1
= commandInt1
;
2419 m_comInt2
= commandInt2
;
2424 //////////////////////////////////////////////////////////////////////
2426 // A basic grid table for string data. An object of this class will
2427 // created by wxGrid if you don't specify an alternative table class.
2430 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2432 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2434 wxGridStringTable::wxGridStringTable()
2439 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2444 m_data
.Alloc( numRows
);
2447 sa
.Alloc( numCols
);
2448 for ( col
= 0; col
< numCols
; col
++ )
2450 sa
.Add( wxEmptyString
);
2453 for ( row
= 0; row
< numRows
; row
++ )
2459 wxGridStringTable::~wxGridStringTable()
2463 int wxGridStringTable::GetNumberRows()
2465 return m_data
.GetCount();
2468 int wxGridStringTable::GetNumberCols()
2470 if ( m_data
.GetCount() > 0 )
2471 return m_data
[0].GetCount();
2476 wxString
wxGridStringTable::GetValue( int row
, int col
)
2478 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2479 _T("invalid row or column index in wxGridStringTable") );
2481 return m_data
[row
][col
];
2484 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2486 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2487 _T("invalid row or column index in wxGridStringTable") );
2489 m_data
[row
][col
] = value
;
2492 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2494 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2495 _T("invalid row or column index in wxGridStringTable") );
2497 return (m_data
[row
][col
] == wxEmptyString
);
2500 void wxGridStringTable::Clear()
2503 int numRows
, numCols
;
2505 numRows
= m_data
.GetCount();
2508 numCols
= m_data
[0].GetCount();
2510 for ( row
= 0; row
< numRows
; row
++ )
2512 for ( col
= 0; col
< numCols
; col
++ )
2514 m_data
[row
][col
] = wxEmptyString
;
2521 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2525 size_t curNumRows
= m_data
.GetCount();
2526 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2528 if ( pos
>= curNumRows
)
2530 return AppendRows( numRows
);
2534 sa
.Alloc( curNumCols
);
2535 for ( col
= 0; col
< curNumCols
; col
++ )
2537 sa
.Add( wxEmptyString
);
2540 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2542 m_data
.Insert( sa
, row
);
2544 UpdateAttrRows( pos
, numRows
);
2547 wxGridTableMessage
msg( this,
2548 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2552 GetView()->ProcessTableMessage( msg
);
2558 bool wxGridStringTable::AppendRows( size_t numRows
)
2562 size_t curNumRows
= m_data
.GetCount();
2563 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2566 if ( curNumCols
> 0 )
2568 sa
.Alloc( curNumCols
);
2569 for ( col
= 0; col
< curNumCols
; col
++ )
2571 sa
.Add( wxEmptyString
);
2575 for ( row
= 0; row
< numRows
; row
++ )
2582 wxGridTableMessage
msg( this,
2583 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2586 GetView()->ProcessTableMessage( msg
);
2592 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2596 size_t curNumRows
= m_data
.GetCount();
2598 if ( pos
>= curNumRows
)
2601 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2602 "Pos value is invalid for present table with %d rows",
2603 pos
, numRows
, curNumRows
);
2604 wxFAIL_MSG( wxT(errmsg
) );
2608 if ( numRows
> curNumRows
- pos
)
2610 numRows
= curNumRows
- pos
;
2613 if ( numRows
>= curNumRows
)
2615 m_data
.Empty(); // don't release memory just yet
2619 for ( n
= 0; n
< numRows
; n
++ )
2621 m_data
.Remove( pos
);
2624 UpdateAttrRows( pos
, -((int)numRows
) );
2627 wxGridTableMessage
msg( this,
2628 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2632 GetView()->ProcessTableMessage( msg
);
2638 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2642 size_t curNumRows
= m_data
.GetCount();
2643 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2645 if ( pos
>= curNumCols
)
2647 return AppendCols( numCols
);
2650 for ( row
= 0; row
< curNumRows
; row
++ )
2652 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2654 m_data
[row
].Insert( wxEmptyString
, col
);
2657 UpdateAttrCols( pos
, numCols
);
2660 wxGridTableMessage
msg( this,
2661 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2665 GetView()->ProcessTableMessage( msg
);
2671 bool wxGridStringTable::AppendCols( size_t numCols
)
2675 size_t curNumRows
= m_data
.GetCount();
2678 // TODO: something better than this ?
2680 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2681 "Call AppendRows() first") );
2685 for ( row
= 0; row
< curNumRows
; row
++ )
2687 for ( n
= 0; n
< numCols
; n
++ )
2689 m_data
[row
].Add( wxEmptyString
);
2695 wxGridTableMessage
msg( this,
2696 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2699 GetView()->ProcessTableMessage( msg
);
2705 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2709 size_t curNumRows
= m_data
.GetCount();
2710 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2712 if ( pos
>= curNumCols
)
2715 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2716 "Pos value is invalid for present table with %d cols",
2717 pos
, numCols
, curNumCols
);
2718 wxFAIL_MSG( wxT( errmsg
) );
2722 if ( numCols
> curNumCols
- pos
)
2724 numCols
= curNumCols
- pos
;
2727 for ( row
= 0; row
< curNumRows
; row
++ )
2729 if ( numCols
>= curNumCols
)
2731 m_data
[row
].Clear();
2735 for ( n
= 0; n
< numCols
; n
++ )
2737 m_data
[row
].Remove( pos
);
2741 UpdateAttrCols( pos
, -((int)numCols
) );
2744 wxGridTableMessage
msg( this,
2745 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2749 GetView()->ProcessTableMessage( msg
);
2755 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2757 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2759 // using default label
2761 return wxGridTableBase::GetRowLabelValue( row
);
2765 return m_rowLabels
[ row
];
2769 wxString
wxGridStringTable::GetColLabelValue( int col
)
2771 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2773 // using default label
2775 return wxGridTableBase::GetColLabelValue( col
);
2779 return m_colLabels
[ col
];
2783 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2785 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2787 int n
= m_rowLabels
.GetCount();
2789 for ( i
= n
; i
<= row
; i
++ )
2791 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2795 m_rowLabels
[row
] = value
;
2798 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2800 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2802 int n
= m_colLabels
.GetCount();
2804 for ( i
= n
; i
<= col
; i
++ )
2806 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2810 m_colLabels
[col
] = value
;
2815 //////////////////////////////////////////////////////////////////////
2816 //////////////////////////////////////////////////////////////////////
2818 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2820 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2821 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2822 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2823 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2826 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2828 const wxPoint
&pos
, const wxSize
&size
)
2829 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2834 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2838 // NO - don't do this because it will set both the x and y origin
2839 // coords to match the parent scrolled window and we just want to
2840 // set the y coord - MB
2842 // m_owner->PrepareDC( dc );
2845 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2846 dc
.SetDeviceOrigin( 0, -y
);
2848 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2849 m_owner
->DrawRowLabels( dc
);
2853 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2855 m_owner
->ProcessRowLabelMouseEvent( event
);
2859 // This seems to be required for wxMotif otherwise the mouse
2860 // cursor must be in the cell edit control to get key events
2862 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2864 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2869 //////////////////////////////////////////////////////////////////////
2871 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2873 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2874 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2875 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2876 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2879 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2881 const wxPoint
&pos
, const wxSize
&size
)
2882 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2887 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2891 // NO - don't do this because it will set both the x and y origin
2892 // coords to match the parent scrolled window and we just want to
2893 // set the x coord - MB
2895 // m_owner->PrepareDC( dc );
2898 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2899 dc
.SetDeviceOrigin( -x
, 0 );
2901 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2902 m_owner
->DrawColLabels( dc
);
2906 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2908 m_owner
->ProcessColLabelMouseEvent( event
);
2912 // This seems to be required for wxMotif otherwise the mouse
2913 // cursor must be in the cell edit control to get key events
2915 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2917 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2922 //////////////////////////////////////////////////////////////////////
2924 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2926 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2927 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2928 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2929 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2932 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2934 const wxPoint
&pos
, const wxSize
&size
)
2935 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2940 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2944 int client_height
= 0;
2945 int client_width
= 0;
2946 GetClientSize( &client_width
, &client_height
);
2948 dc
.SetPen( *wxBLACK_PEN
);
2949 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2950 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2952 dc
.SetPen( *wxWHITE_PEN
);
2953 dc
.DrawLine( 0, 0, client_width
, 0 );
2954 dc
.DrawLine( 0, 0, 0, client_height
);
2958 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2960 m_owner
->ProcessCornerLabelMouseEvent( event
);
2964 // This seems to be required for wxMotif otherwise the mouse
2965 // cursor must be in the cell edit control to get key events
2967 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2969 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2974 //////////////////////////////////////////////////////////////////////
2976 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2978 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2979 EVT_PAINT( wxGridWindow::OnPaint
)
2980 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2981 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2982 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2985 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2986 wxGridRowLabelWindow
*rowLblWin
,
2987 wxGridColLabelWindow
*colLblWin
,
2988 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2989 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
2992 m_rowLabelWin
= rowLblWin
;
2993 m_colLabelWin
= colLblWin
;
2994 SetBackgroundColour( "WHITE" );
2998 wxGridWindow::~wxGridWindow()
3003 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3005 wxPaintDC
dc( this );
3006 m_owner
->PrepareDC( dc
);
3007 wxRegion reg
= GetUpdateRegion();
3008 m_owner
->CalcCellsExposed( reg
);
3009 m_owner
->DrawGridCellArea( dc
);
3010 #if WXGRID_DRAW_LINES
3011 m_owner
->DrawAllGridLines( dc
, reg
);
3013 m_owner
->DrawGridSpace( dc
);
3014 m_owner
->DrawHighlight( dc
);
3018 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3020 wxPanel::ScrollWindow( dx
, dy
, rect
);
3021 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3022 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3026 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3028 m_owner
->ProcessGridCellMouseEvent( event
);
3032 // This seems to be required for wxMotif otherwise the mouse
3033 // cursor must be in the cell edit control to get key events
3035 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3037 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3041 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3046 //////////////////////////////////////////////////////////////////////
3049 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3051 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3052 EVT_PAINT( wxGrid::OnPaint
)
3053 EVT_SIZE( wxGrid::OnSize
)
3054 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3055 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3058 wxGrid::wxGrid( wxWindow
*parent
,
3063 const wxString
& name
)
3064 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3065 m_colMinWidths(GRID_HASH_SIZE
),
3066 m_rowMinHeights(GRID_HASH_SIZE
)
3075 wxSafeDecRef(m_defaultCellAttr
);
3077 #ifdef DEBUG_ATTR_CACHE
3078 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3079 wxPrintf(_T("wxGrid attribute cache statistics: "
3080 "total: %u, hits: %u (%u%%)\n"),
3081 total
, gs_nAttrCacheHits
,
3082 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3088 delete m_typeRegistry
;
3094 // ----- internal init and update functions
3097 void wxGrid::Create()
3099 m_created
= FALSE
; // set to TRUE by CreateGrid
3101 m_table
= (wxGridTableBase
*) NULL
;
3104 m_cellEditCtrlEnabled
= FALSE
;
3106 m_defaultCellAttr
= new wxGridCellAttr
;
3107 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3109 // Set default cell attributes
3110 m_defaultCellAttr
->SetFont(GetFont());
3111 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
3112 m_defaultCellAttr
->SetTextColour(
3113 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3114 m_defaultCellAttr
->SetBackgroundColour(
3115 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3116 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3117 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3122 m_currentCellCoords
= wxGridNoCellCoords
;
3124 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3125 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3127 // create the type registry
3128 m_typeRegistry
= new wxGridTypeRegistry
;
3130 // subwindow components that make up the wxGrid
3131 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3136 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3141 m_colLabelWin
= new wxGridColLabelWindow( this,
3146 m_gridWin
= new wxGridWindow( this,
3153 SetTargetWindow( m_gridWin
);
3157 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3158 wxGrid::wxGridSelectionModes selmode
)
3162 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3167 m_numRows
= numRows
;
3168 m_numCols
= numCols
;
3170 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3171 m_table
->SetView( this );
3174 m_selection
= new wxGridSelection( this, selmode
);
3180 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3184 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3187 m_selection
->SetSelectionMode( selmode
);
3190 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3191 wxGrid::wxGridSelectionModes selmode
)
3195 // RD: Actually, this should probably be allowed. I think it would be
3196 // nice to be able to switch multiple Tables in and out of a single
3197 // View at runtime. Is there anything in the implmentation that would
3200 // At least, you now have to cope with m_selection
3201 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3206 m_numRows
= table
->GetNumberRows();
3207 m_numCols
= table
->GetNumberCols();
3210 m_table
->SetView( this );
3214 m_selection
= new wxGridSelection( this, selmode
);
3224 if ( m_numRows
<= 0 )
3225 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
3227 if ( m_numCols
<= 0 )
3228 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
3230 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3231 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3233 if ( m_rowLabelWin
)
3235 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3239 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3242 m_labelTextColour
= wxColour( _T("BLACK") );
3245 m_attrCache
.row
= -1;
3247 // TODO: something better than this ?
3249 m_labelFont
= this->GetFont();
3250 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3252 m_rowLabelHorizAlign
= wxLEFT
;
3253 m_rowLabelVertAlign
= wxCENTRE
;
3255 m_colLabelHorizAlign
= wxCENTRE
;
3256 m_colLabelVertAlign
= wxTOP
;
3258 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3259 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3261 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3262 m_defaultRowHeight
+= 8;
3264 m_defaultRowHeight
+= 4;
3267 m_gridLineColour
= wxColour( 128, 128, 255 );
3268 m_gridLinesEnabled
= TRUE
;
3270 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3271 m_winCapture
= (wxWindow
*)NULL
;
3272 m_canDragRowSize
= TRUE
;
3273 m_canDragColSize
= TRUE
;
3274 m_canDragGridSize
= TRUE
;
3276 m_dragRowOrCol
= -1;
3277 m_isDragging
= FALSE
;
3278 m_startDragPos
= wxDefaultPosition
;
3280 m_waitForSlowClick
= FALSE
;
3282 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3283 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3285 m_currentCellCoords
= wxGridNoCellCoords
;
3287 m_selectingTopLeft
= wxGridNoCellCoords
;
3288 m_selectingBottomRight
= wxGridNoCellCoords
;
3289 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3290 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3292 m_editable
= TRUE
; // default for whole grid
3294 m_inOnKeyDown
= FALSE
;
3303 // ----------------------------------------------------------------------------
3304 // the idea is to call these functions only when necessary because they create
3305 // quite big arrays which eat memory mostly unnecessary - in particular, if
3306 // default widths/heights are used for all rows/columns, we may not use these
3309 // with some extra code, it should be possible to only store the
3310 // widths/heights different from default ones but this will be done later...
3311 // ----------------------------------------------------------------------------
3313 void wxGrid::InitRowHeights()
3315 m_rowHeights
.Empty();
3316 m_rowBottoms
.Empty();
3318 m_rowHeights
.Alloc( m_numRows
);
3319 m_rowBottoms
.Alloc( m_numRows
);
3322 for ( int i
= 0; i
< m_numRows
; i
++ )
3324 m_rowHeights
.Add( m_defaultRowHeight
);
3325 rowBottom
+= m_defaultRowHeight
;
3326 m_rowBottoms
.Add( rowBottom
);
3330 void wxGrid::InitColWidths()
3332 m_colWidths
.Empty();
3333 m_colRights
.Empty();
3335 m_colWidths
.Alloc( m_numCols
);
3336 m_colRights
.Alloc( m_numCols
);
3338 for ( int i
= 0; i
< m_numCols
; i
++ )
3340 m_colWidths
.Add( m_defaultColWidth
);
3341 colRight
+= m_defaultColWidth
;
3342 m_colRights
.Add( colRight
);
3346 int wxGrid::GetColWidth(int col
) const
3348 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3351 int wxGrid::GetColLeft(int col
) const
3353 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3354 : m_colRights
[col
] - m_colWidths
[col
];
3357 int wxGrid::GetColRight(int col
) const
3359 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3363 int wxGrid::GetRowHeight(int row
) const
3365 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3368 int wxGrid::GetRowTop(int row
) const
3370 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3371 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3374 int wxGrid::GetRowBottom(int row
) const
3376 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3377 : m_rowBottoms
[row
];
3380 void wxGrid::CalcDimensions()
3383 GetClientSize( &cw
, &ch
);
3385 if ( m_numRows
> 0 && m_numCols
> 0 )
3387 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3388 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3390 // TODO: restore the scroll position that we had before sizing
3393 GetViewStart( &x
, &y
);
3394 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3395 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3401 void wxGrid::CalcWindowSizes()
3404 GetClientSize( &cw
, &ch
);
3406 if ( m_cornerLabelWin
->IsShown() )
3407 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3409 if ( m_colLabelWin
->IsShown() )
3410 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3412 if ( m_rowLabelWin
->IsShown() )
3413 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3415 if ( m_gridWin
->IsShown() )
3416 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3420 // this is called when the grid table sends a message to say that it
3421 // has been redimensioned
3423 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3427 // if we were using the default widths/heights so far, we must change them
3429 if ( m_colWidths
.IsEmpty() )
3434 if ( m_rowHeights
.IsEmpty() )
3439 switch ( msg
.GetId() )
3441 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3443 size_t pos
= msg
.GetCommandInt();
3444 int numRows
= msg
.GetCommandInt2();
3445 for ( i
= 0; i
< numRows
; i
++ )
3447 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3448 m_rowBottoms
.Insert( 0, pos
);
3450 m_numRows
+= numRows
;
3453 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3455 for ( i
= pos
; i
< m_numRows
; i
++ )
3457 bottom
+= m_rowHeights
[i
];
3458 m_rowBottoms
[i
] = bottom
;
3464 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3466 int numRows
= msg
.GetCommandInt();
3467 for ( i
= 0; i
< numRows
; i
++ )
3469 m_rowHeights
.Add( m_defaultRowHeight
);
3470 m_rowBottoms
.Add( 0 );
3473 int oldNumRows
= m_numRows
;
3474 m_numRows
+= numRows
;
3477 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3479 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3481 bottom
+= m_rowHeights
[i
];
3482 m_rowBottoms
[i
] = bottom
;
3488 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3490 size_t pos
= msg
.GetCommandInt();
3491 int numRows
= msg
.GetCommandInt2();
3492 for ( i
= 0; i
< numRows
; i
++ )
3494 m_rowHeights
.Remove( pos
);
3495 m_rowBottoms
.Remove( pos
);
3497 m_numRows
-= numRows
;
3502 m_colWidths
.Clear();
3503 m_colRights
.Clear();
3504 m_currentCellCoords
= wxGridNoCellCoords
;
3508 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3509 m_currentCellCoords
.Set( 0, 0 );
3512 for ( i
= 0; i
< m_numRows
; i
++ )
3514 h
+= m_rowHeights
[i
];
3515 m_rowBottoms
[i
] = h
;
3523 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3525 size_t pos
= msg
.GetCommandInt();
3526 int numCols
= msg
.GetCommandInt2();
3527 for ( i
= 0; i
< numCols
; i
++ )
3529 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3530 m_colRights
.Insert( 0, pos
);
3532 m_numCols
+= numCols
;
3535 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3537 for ( i
= pos
; i
< m_numCols
; i
++ )
3539 right
+= m_colWidths
[i
];
3540 m_colRights
[i
] = right
;
3546 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3548 int numCols
= msg
.GetCommandInt();
3549 for ( i
= 0; i
< numCols
; i
++ )
3551 m_colWidths
.Add( m_defaultColWidth
);
3552 m_colRights
.Add( 0 );
3555 int oldNumCols
= m_numCols
;
3556 m_numCols
+= numCols
;
3559 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3561 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3563 right
+= m_colWidths
[i
];
3564 m_colRights
[i
] = right
;
3570 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3572 size_t pos
= msg
.GetCommandInt();
3573 int numCols
= msg
.GetCommandInt2();
3574 for ( i
= 0; i
< numCols
; i
++ )
3576 m_colWidths
.Remove( pos
);
3577 m_colRights
.Remove( pos
);
3579 m_numCols
-= numCols
;
3583 #if 0 // leave the row alone here so that AppendCols will work subsequently
3585 m_rowHeights
.Clear();
3586 m_rowBottoms
.Clear();
3588 m_currentCellCoords
= wxGridNoCellCoords
;
3592 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3593 m_currentCellCoords
.Set( 0, 0 );
3596 for ( i
= 0; i
< m_numCols
; i
++ )
3598 w
+= m_colWidths
[i
];
3611 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3613 wxRegionIterator
iter( reg
);
3616 m_rowLabelsExposed
.Empty();
3623 // TODO: remove this when we can...
3624 // There is a bug in wxMotif that gives garbage update
3625 // rectangles if you jump-scroll a long way by clicking the
3626 // scrollbar with middle button. This is a work-around
3628 #if defined(__WXMOTIF__)
3630 m_gridWin
->GetClientSize( &cw
, &ch
);
3631 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3632 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3635 // logical bounds of update region
3638 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3639 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3641 // find the row labels within these bounds
3644 for ( row
= 0; row
< m_numRows
; row
++ )
3646 if ( GetRowBottom(row
) < top
)
3649 if ( GetRowTop(row
) > bottom
)
3652 m_rowLabelsExposed
.Add( row
);
3660 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3662 wxRegionIterator
iter( reg
);
3665 m_colLabelsExposed
.Empty();
3672 // TODO: remove this when we can...
3673 // There is a bug in wxMotif that gives garbage update
3674 // rectangles if you jump-scroll a long way by clicking the
3675 // scrollbar with middle button. This is a work-around
3677 #if defined(__WXMOTIF__)
3679 m_gridWin
->GetClientSize( &cw
, &ch
);
3680 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3681 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3684 // logical bounds of update region
3687 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3688 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3690 // find the cells within these bounds
3693 for ( col
= 0; col
< m_numCols
; col
++ )
3695 if ( GetColRight(col
) < left
)
3698 if ( GetColLeft(col
) > right
)
3701 m_colLabelsExposed
.Add( col
);
3709 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3711 wxRegionIterator
iter( reg
);
3714 m_cellsExposed
.Empty();
3715 m_rowsExposed
.Empty();
3716 m_colsExposed
.Empty();
3718 int left
, top
, right
, bottom
;
3723 // TODO: remove this when we can...
3724 // There is a bug in wxMotif that gives garbage update
3725 // rectangles if you jump-scroll a long way by clicking the
3726 // scrollbar with middle button. This is a work-around
3728 #if defined(__WXMOTIF__)
3730 m_gridWin
->GetClientSize( &cw
, &ch
);
3731 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3732 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3733 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3734 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3737 // logical bounds of update region
3739 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3740 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3742 // find the cells within these bounds
3745 for ( row
= 0; row
< m_numRows
; row
++ )
3747 if ( GetRowBottom(row
) <= top
)
3750 if ( GetRowTop(row
) > bottom
)
3753 m_rowsExposed
.Add( row
);
3755 for ( col
= 0; col
< m_numCols
; col
++ )
3757 if ( GetColRight(col
) <= left
)
3760 if ( GetColLeft(col
) > right
)
3763 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3764 m_colsExposed
.Add( col
);
3765 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3774 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3777 wxPoint
pos( event
.GetPosition() );
3778 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3780 if ( event
.Dragging() )
3782 m_isDragging
= TRUE
;
3784 if ( event
.LeftIsDown() )
3786 switch( m_cursorMode
)
3788 case WXGRID_CURSOR_RESIZE_ROW
:
3790 int cw
, ch
, left
, dummy
;
3791 m_gridWin
->GetClientSize( &cw
, &ch
);
3792 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3794 wxClientDC
dc( m_gridWin
);
3797 GetRowTop(m_dragRowOrCol
) +
3798 GetRowMinimalHeight(m_dragRowOrCol
) );
3799 dc
.SetLogicalFunction(wxINVERT
);
3800 if ( m_dragLastPos
>= 0 )
3802 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3804 dc
.DrawLine( left
, y
, left
+cw
, y
);
3809 case WXGRID_CURSOR_SELECT_ROW
:
3810 if ( (row
= YToRow( y
)) >= 0 )
3812 m_selection
->SelectRow( row
,
3813 event
.ControlDown(),
3819 // default label to suppress warnings about "enumeration value
3820 // 'xxx' not handled in switch
3828 m_isDragging
= FALSE
;
3831 // ------------ Entering or leaving the window
3833 if ( event
.Entering() || event
.Leaving() )
3835 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3839 // ------------ Left button pressed
3841 else if ( event
.LeftDown() )
3843 // don't send a label click event for a hit on the
3844 // edge of the row label - this is probably the user
3845 // wanting to resize the row
3847 if ( YToEdgeOfRow(y
) < 0 )
3851 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3853 if ( !event
.ShiftDown() && !event
.ControlDown() )
3855 if ( event
.ShiftDown() )
3856 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
3859 GetNumberCols() - 1,
3860 event
.ControlDown(),
3865 m_selection
->SelectRow( row
,
3866 event
.ControlDown(),
3870 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3875 // starting to drag-resize a row
3877 if ( CanDragRowSize() )
3878 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3883 // ------------ Left double click
3885 else if (event
.LeftDClick() )
3887 if ( YToEdgeOfRow(y
) < 0 )
3890 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3895 // ------------ Left button released
3897 else if ( event
.LeftUp() )
3899 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3901 DoEndDragResizeRow();
3903 // Note: we are ending the event *after* doing
3904 // default processing in this case
3906 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3909 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3914 // ------------ Right button down
3916 else if ( event
.RightDown() )
3919 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3921 // no default action at the moment
3926 // ------------ Right double click
3928 else if ( event
.RightDClick() )
3931 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3933 // no default action at the moment
3938 // ------------ No buttons down and mouse moving
3940 else if ( event
.Moving() )
3942 m_dragRowOrCol
= YToEdgeOfRow( y
);
3943 if ( m_dragRowOrCol
>= 0 )
3945 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3947 // don't capture the mouse yet
3948 if ( CanDragRowSize() )
3949 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3952 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3954 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3960 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3963 wxPoint
pos( event
.GetPosition() );
3964 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3966 if ( event
.Dragging() )
3968 m_isDragging
= TRUE
;
3970 if ( event
.LeftIsDown() )
3972 switch( m_cursorMode
)
3974 case WXGRID_CURSOR_RESIZE_COL
:
3976 int cw
, ch
, dummy
, top
;
3977 m_gridWin
->GetClientSize( &cw
, &ch
);
3978 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3980 wxClientDC
dc( m_gridWin
);
3983 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3984 GetColMinimalWidth(m_dragRowOrCol
));
3985 dc
.SetLogicalFunction(wxINVERT
);
3986 if ( m_dragLastPos
>= 0 )
3988 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3990 dc
.DrawLine( x
, top
, x
, top
+ch
);
3995 case WXGRID_CURSOR_SELECT_COL
:
3996 if ( (col
= XToCol( x
)) >= 0 )
3998 m_selection
->SelectCol( col
,
3999 event
.ControlDown(),
4005 // default label to suppress warnings about "enumeration value
4006 // 'xxx' not handled in switch
4014 m_isDragging
= FALSE
;
4017 // ------------ Entering or leaving the window
4019 if ( event
.Entering() || event
.Leaving() )
4021 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4025 // ------------ Left button pressed
4027 else if ( event
.LeftDown() )
4029 // don't send a label click event for a hit on the
4030 // edge of the col label - this is probably the user
4031 // wanting to resize the col
4033 if ( XToEdgeOfCol(x
) < 0 )
4037 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4039 if ( !event
.ShiftDown() && !event
.ControlDown() )
4041 if ( event
.ShiftDown() )
4042 m_selection
->SelectBlock( 0,
4043 m_currentCellCoords
.GetCol(),
4044 GetNumberRows() - 1, col
,
4045 event
.ControlDown(),
4050 m_selection
->SelectCol( col
,
4051 event
.ControlDown(),
4055 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4060 // starting to drag-resize a col
4062 if ( CanDragColSize() )
4063 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4068 // ------------ Left double click
4070 if ( event
.LeftDClick() )
4072 if ( XToEdgeOfCol(x
) < 0 )
4075 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4080 // ------------ Left button released
4082 else if ( event
.LeftUp() )
4084 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4086 DoEndDragResizeCol();
4088 // Note: we are ending the event *after* doing
4089 // default processing in this case
4091 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4094 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4099 // ------------ Right button down
4101 else if ( event
.RightDown() )
4104 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4106 // no default action at the moment
4111 // ------------ Right double click
4113 else if ( event
.RightDClick() )
4116 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4118 // no default action at the moment
4123 // ------------ No buttons down and mouse moving
4125 else if ( event
.Moving() )
4127 m_dragRowOrCol
= XToEdgeOfCol( x
);
4128 if ( m_dragRowOrCol
>= 0 )
4130 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4132 // don't capture the cursor yet
4133 if ( CanDragColSize() )
4134 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4137 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4139 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4145 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4147 if ( event
.LeftDown() )
4149 // indicate corner label by having both row and
4152 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4158 else if ( event
.LeftDClick() )
4160 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4163 else if ( event
.RightDown() )
4165 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4167 // no default action at the moment
4171 else if ( event
.RightDClick() )
4173 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4175 // no default action at the moment
4180 void wxGrid::ChangeCursorMode(CursorMode mode
,
4185 static const wxChar
*cursorModes
[] =
4194 wxLogTrace(_T("grid"),
4195 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4196 win
== m_colLabelWin
? _T("colLabelWin")
4197 : win
? _T("rowLabelWin")
4199 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4200 #endif // __WXDEBUG__
4202 if ( mode
== m_cursorMode
)
4207 // by default use the grid itself
4213 m_winCapture
->ReleaseMouse();
4214 m_winCapture
= (wxWindow
*)NULL
;
4217 m_cursorMode
= mode
;
4219 switch ( m_cursorMode
)
4221 case WXGRID_CURSOR_RESIZE_ROW
:
4222 win
->SetCursor( m_rowResizeCursor
);
4225 case WXGRID_CURSOR_RESIZE_COL
:
4226 win
->SetCursor( m_colResizeCursor
);
4230 win
->SetCursor( *wxSTANDARD_CURSOR
);
4233 // we need to capture mouse when resizing
4234 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4235 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4237 if ( captureMouse
&& resize
)
4239 win
->CaptureMouse();
4244 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4247 wxPoint
pos( event
.GetPosition() );
4248 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4250 wxGridCellCoords coords
;
4251 XYToCell( x
, y
, coords
);
4253 if ( event
.Dragging() )
4255 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4257 // Don't start doing anything until the mouse has been drug at
4258 // least 3 pixels in any direction...
4261 if (m_startDragPos
== wxDefaultPosition
)
4263 m_startDragPos
= pos
;
4266 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4270 m_isDragging
= TRUE
;
4271 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4273 // Hide the edit control, so it
4274 // won't interfer with drag-shrinking.
4275 if ( IsCellEditControlEnabled() )
4277 HideCellEditControl();
4278 SaveEditControlValue();
4281 // Have we captured the mouse yet?
4284 m_winCapture
= m_gridWin
;
4285 m_winCapture
->CaptureMouse();
4288 if ( coords
!= wxGridNoCellCoords
)
4290 if ( event
.ControlDown() )
4292 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4293 m_selectingKeyboard
= coords
;
4294 SelectBlock ( m_selectingKeyboard
, coords
);
4298 if ( !IsSelection() )
4300 SelectBlock( coords
, coords
);
4304 SelectBlock( m_currentCellCoords
, coords
);
4308 if (! IsVisible(coords
))
4310 MakeCellVisible(coords
);
4311 // TODO: need to introduce a delay or something here. The
4312 // scrolling is way to fast, at least on MSW - also on GTK.
4316 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4318 int cw
, ch
, left
, dummy
;
4319 m_gridWin
->GetClientSize( &cw
, &ch
);
4320 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4322 wxClientDC
dc( m_gridWin
);
4324 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4325 GetRowMinimalHeight(m_dragRowOrCol
) );
4326 dc
.SetLogicalFunction(wxINVERT
);
4327 if ( m_dragLastPos
>= 0 )
4329 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4331 dc
.DrawLine( left
, y
, left
+cw
, y
);
4334 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4336 int cw
, ch
, dummy
, top
;
4337 m_gridWin
->GetClientSize( &cw
, &ch
);
4338 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4340 wxClientDC
dc( m_gridWin
);
4342 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4343 GetColMinimalWidth(m_dragRowOrCol
) );
4344 dc
.SetLogicalFunction(wxINVERT
);
4345 if ( m_dragLastPos
>= 0 )
4347 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4349 dc
.DrawLine( x
, top
, x
, top
+ch
);
4356 m_isDragging
= FALSE
;
4357 m_startDragPos
= wxDefaultPosition
;
4359 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4360 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4363 if ( event
.Entering() || event
.Leaving() )
4365 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4366 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4371 // ------------ Left button pressed
4373 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4375 if ( !event
.ShiftDown() && !event
.ControlDown() )
4377 if ( event
.ShiftDown() )
4379 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4380 m_currentCellCoords
.GetCol(),
4383 event
.ControlDown(),
4388 else if ( XToEdgeOfCol(x
) < 0 &&
4389 YToEdgeOfRow(y
) < 0 )
4391 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4396 DisableCellEditControl();
4397 MakeCellVisible( coords
);
4399 // if this is the second click on this cell then start
4401 if ( m_waitForSlowClick
&&
4402 (coords
== m_currentCellCoords
) &&
4403 CanEnableCellControl())
4405 EnableCellEditControl();
4407 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4408 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4411 editor
->StartingClick();
4415 m_waitForSlowClick
= FALSE
;
4419 if ( event
.ControlDown() )
4421 m_selection
->ToggleCellSelection( coords
.GetRow(),
4423 event
.ControlDown(),
4427 m_selectingTopLeft
= wxGridNoCellCoords
;
4428 m_selectingBottomRight
= wxGridNoCellCoords
;
4429 m_selectingKeyboard
= coords
;
4432 SetCurrentCell( coords
);
4433 m_waitForSlowClick
= TRUE
;
4440 // ------------ Left double click
4442 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4444 DisableCellEditControl();
4446 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4448 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4456 // ------------ Left button released
4458 else if ( event
.LeftUp() )
4460 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4462 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4463 m_selectingBottomRight
!= wxGridNoCellCoords
)
4467 m_winCapture
->ReleaseMouse();
4468 m_winCapture
= NULL
;
4470 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4471 m_selectingTopLeft
.GetCol(),
4472 m_selectingBottomRight
.GetRow(),
4473 m_selectingBottomRight
.GetCol(),
4474 event
.ControlDown(),
4478 m_selectingTopLeft
= wxGridNoCellCoords
;
4479 m_selectingBottomRight
= wxGridNoCellCoords
;
4482 // Show the edit control, if it has been hidden for
4484 ShowCellEditControl();
4486 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4488 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4489 DoEndDragResizeRow();
4491 // Note: we are ending the event *after* doing
4492 // default processing in this case
4494 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4496 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4498 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4499 DoEndDragResizeCol();
4501 // Note: we are ending the event *after* doing
4502 // default processing in this case
4504 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4511 // ------------ Right button down
4513 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4515 DisableCellEditControl();
4516 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4521 // no default action at the moment
4526 // ------------ Right double click
4528 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4530 DisableCellEditControl();
4531 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4536 // no default action at the moment
4540 // ------------ Moving and no button action
4542 else if ( event
.Moving() && !event
.IsButton() )
4544 int dragRow
= YToEdgeOfRow( y
);
4545 int dragCol
= XToEdgeOfCol( x
);
4547 // Dragging on the corner of a cell to resize in both
4548 // directions is not implemented yet...
4550 if ( dragRow
>= 0 && dragCol
>= 0 )
4552 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4558 m_dragRowOrCol
= dragRow
;
4560 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4562 if ( CanDragRowSize() && CanDragGridSize() )
4563 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4568 m_dragRowOrCol
= dragCol
;
4576 m_dragRowOrCol
= dragCol
;
4578 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4580 if ( CanDragColSize() && CanDragGridSize() )
4581 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4587 // Neither on a row or col edge
4589 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4591 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4597 void wxGrid::DoEndDragResizeRow()
4599 if ( m_dragLastPos
>= 0 )
4601 // erase the last line and resize the row
4603 int cw
, ch
, left
, dummy
;
4604 m_gridWin
->GetClientSize( &cw
, &ch
);
4605 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4607 wxClientDC
dc( m_gridWin
);
4609 dc
.SetLogicalFunction( wxINVERT
);
4610 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4611 HideCellEditControl();
4612 SaveEditControlValue();
4614 int rowTop
= GetRowTop(m_dragRowOrCol
);
4615 SetRowSize( m_dragRowOrCol
,
4616 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4618 if ( !GetBatchCount() )
4620 // Only needed to get the correct rect.y:
4621 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4623 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4624 rect
.width
= m_rowLabelWidth
;
4625 rect
.height
= ch
- rect
.y
;
4626 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4628 m_gridWin
->Refresh( FALSE
, &rect
);
4631 ShowCellEditControl();
4636 void wxGrid::DoEndDragResizeCol()
4638 if ( m_dragLastPos
>= 0 )
4640 // erase the last line and resize the col
4642 int cw
, ch
, dummy
, top
;
4643 m_gridWin
->GetClientSize( &cw
, &ch
);
4644 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4646 wxClientDC
dc( m_gridWin
);
4648 dc
.SetLogicalFunction( wxINVERT
);
4649 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4650 HideCellEditControl();
4651 SaveEditControlValue();
4653 int colLeft
= GetColLeft(m_dragRowOrCol
);
4654 SetColSize( m_dragRowOrCol
,
4655 wxMax( m_dragLastPos
- colLeft
,
4656 GetColMinimalWidth(m_dragRowOrCol
) ) );
4658 if ( !GetBatchCount() )
4660 // Only needed to get the correct rect.x:
4661 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4663 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4664 rect
.width
= cw
- rect
.x
;
4665 rect
.height
= m_colLabelHeight
;
4666 m_colLabelWin
->Refresh( TRUE
, &rect
);
4668 m_gridWin
->Refresh( FALSE
, &rect
);
4671 ShowCellEditControl();
4678 // ------ interaction with data model
4680 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4682 switch ( msg
.GetId() )
4684 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4685 return GetModelValues();
4687 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4688 return SetModelValues();
4690 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4691 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4692 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4693 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4694 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4695 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4696 return Redimension( msg
);
4705 // The behaviour of this function depends on the grid table class
4706 // Clear() function. For the default wxGridStringTable class the
4707 // behavious is to replace all cell contents with wxEmptyString but
4708 // not to change the number of rows or cols.
4710 void wxGrid::ClearGrid()
4714 if (IsCellEditControlEnabled())
4715 DisableCellEditControl();
4718 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4723 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4725 // TODO: something with updateLabels flag
4729 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4735 if (IsCellEditControlEnabled())
4736 DisableCellEditControl();
4738 bool ok
= m_table
->InsertRows( pos
, numRows
);
4740 // the table will have sent the results of the insert row
4741 // operation to this view object as a grid table message
4745 if ( m_numCols
== 0 )
4747 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4749 // TODO: perhaps instead of appending the default number of cols
4750 // we should remember what the last non-zero number of cols was ?
4754 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4756 // if we have just inserted cols into an empty grid the current
4757 // cell will be undefined...
4759 SetCurrentCell( 0, 0 );
4762 m_selection
->UpdateRows( pos
, numRows
);
4763 if ( !GetBatchCount() ) Refresh();
4775 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4777 // TODO: something with updateLabels flag
4781 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4785 if ( m_table
&& m_table
->AppendRows( numRows
) )
4787 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4789 // if we have just inserted cols into an empty grid the current
4790 // cell will be undefined...
4792 SetCurrentCell( 0, 0 );
4795 // the table will have sent the results of the append row
4796 // operation to this view object as a grid table message
4798 if ( !GetBatchCount() ) Refresh();
4808 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4810 // TODO: something with updateLabels flag
4814 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4820 if (IsCellEditControlEnabled())
4821 DisableCellEditControl();
4823 if (m_table
->DeleteRows( pos
, numRows
))
4826 // the table will have sent the results of the delete row
4827 // operation to this view object as a grid table message
4829 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4830 if ( !GetBatchCount() ) Refresh();
4838 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4840 // TODO: something with updateLabels flag
4844 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4850 if (IsCellEditControlEnabled())
4851 DisableCellEditControl();
4853 bool ok
= m_table
->InsertCols( pos
, numCols
);
4855 // the table will have sent the results of the insert col
4856 // operation to this view object as a grid table message
4860 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4862 // if we have just inserted cols into an empty grid the current
4863 // cell will be undefined...
4865 SetCurrentCell( 0, 0 );
4868 m_selection
->UpdateCols( pos
, numCols
);
4869 if ( !GetBatchCount() ) Refresh();
4881 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4883 // TODO: something with updateLabels flag
4887 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4891 if ( m_table
&& m_table
->AppendCols( numCols
) )
4893 // the table will have sent the results of the append col
4894 // operation to this view object as a grid table message
4896 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4898 // if we have just inserted cols into an empty grid the current
4899 // cell will be undefined...
4901 SetCurrentCell( 0, 0 );
4904 if ( !GetBatchCount() ) Refresh();
4914 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4916 // TODO: something with updateLabels flag
4920 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4926 if (IsCellEditControlEnabled())
4927 DisableCellEditControl();
4929 if ( m_table
->DeleteCols( pos
, numCols
) )
4931 // the table will have sent the results of the delete col
4932 // operation to this view object as a grid table message
4934 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4935 if ( !GetBatchCount() ) Refresh();
4945 // ----- event handlers
4948 // Generate a grid event based on a mouse event and
4949 // return the result of ProcessEvent()
4951 bool wxGrid::SendEvent( const wxEventType type
,
4953 wxMouseEvent
& mouseEv
)
4955 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4957 int rowOrCol
= (row
== -1 ? col
: row
);
4959 wxGridSizeEvent
gridEvt( GetId(),
4963 mouseEv
.GetX(), mouseEv
.GetY(),
4964 mouseEv
.ControlDown(),
4965 mouseEv
.ShiftDown(),
4967 mouseEv
.MetaDown() );
4969 return GetEventHandler()->ProcessEvent(gridEvt
);
4971 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4973 // Right now, it should _never_ end up here!
4974 wxGridRangeSelectEvent
gridEvt( GetId(),
4978 m_selectingBottomRight
,
4980 mouseEv
.ControlDown(),
4981 mouseEv
.ShiftDown(),
4983 mouseEv
.MetaDown() );
4985 return GetEventHandler()->ProcessEvent(gridEvt
);
4989 wxGridEvent
gridEvt( GetId(),
4993 mouseEv
.GetX(), mouseEv
.GetY(),
4995 mouseEv
.ControlDown(),
4996 mouseEv
.ShiftDown(),
4998 mouseEv
.MetaDown() );
5000 return GetEventHandler()->ProcessEvent(gridEvt
);
5005 // Generate a grid event of specified type and return the result
5006 // of ProcessEvent().
5008 bool wxGrid::SendEvent( const wxEventType type
,
5011 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5013 int rowOrCol
= (row
== -1 ? col
: row
);
5015 wxGridSizeEvent
gridEvt( GetId(),
5020 return GetEventHandler()->ProcessEvent(gridEvt
);
5024 wxGridEvent
gridEvt( GetId(),
5029 return GetEventHandler()->ProcessEvent(gridEvt
);
5034 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5036 wxPaintDC
dc( this );
5040 // This is just here to make sure that CalcDimensions gets called when
5041 // the grid view is resized... then the size event is skipped to allow
5042 // the box sizers to handle everything
5044 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5051 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5053 if ( m_inOnKeyDown
)
5055 // shouldn't be here - we are going round in circles...
5057 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5060 m_inOnKeyDown
= TRUE
;
5062 // propagate the event up and see if it gets processed
5064 wxWindow
*parent
= GetParent();
5065 wxKeyEvent
keyEvt( event
);
5066 keyEvt
.SetEventObject( parent
);
5068 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5071 // try local handlers
5073 if ( !event
.ShiftDown() &&
5074 m_selectingKeyboard
!= wxGridNoCellCoords
)
5076 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5077 m_selectingBottomRight
!= wxGridNoCellCoords
)
5078 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5079 m_selectingTopLeft
.GetCol(),
5080 m_selectingBottomRight
.GetRow(),
5081 m_selectingBottomRight
.GetCol(),
5082 event
.ControlDown(),
5086 m_selectingTopLeft
= wxGridNoCellCoords
;
5087 m_selectingBottomRight
= wxGridNoCellCoords
;
5088 m_selectingKeyboard
= wxGridNoCellCoords
;
5091 switch ( event
.KeyCode() )
5094 if ( event
.ControlDown() )
5096 MoveCursorUpBlock( event
.ShiftDown() );
5100 MoveCursorUp( event
.ShiftDown() );
5105 if ( event
.ControlDown() )
5107 MoveCursorDownBlock( event
.ShiftDown() );
5111 MoveCursorDown( event
.ShiftDown() );
5116 if ( event
.ControlDown() )
5118 MoveCursorLeftBlock( event
.ShiftDown() );
5122 MoveCursorLeft( event
.ShiftDown() );
5127 if ( event
.ControlDown() )
5129 MoveCursorRightBlock( event
.ShiftDown() );
5133 MoveCursorRight( event
.ShiftDown() );
5138 if ( event
.ControlDown() )
5140 event
.Skip(); // to let the edit control have the return
5144 MoveCursorDown( event
.ShiftDown() );
5153 if (event
.ShiftDown())
5154 MoveCursorLeft( FALSE
);
5156 MoveCursorRight( FALSE
);
5160 if ( event
.ControlDown() )
5162 MakeCellVisible( 0, 0 );
5163 SetCurrentCell( 0, 0 );
5172 if ( event
.ControlDown() )
5174 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5175 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5192 if ( event
.ControlDown() )
5194 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5195 m_currentCellCoords
.GetCol(),
5196 event
.ControlDown(),
5202 if ( !IsEditable() )
5204 MoveCursorRight( FALSE
);
5207 // Otherwise fall through to default
5210 // alphanumeric keys or F2 (special key just for this) enable
5211 // the cell edit control
5212 // On just Shift/Control I get values for event.KeyCode()
5213 // that are outside the range where isalnum's behaviour is
5214 // well defined, so do an additional sanity check.
5215 if ( !(event
.AltDown() ||
5217 event
.ControlDown()) &&
5218 ((isalnum((int)event
.KeyCode()) &&
5219 (event
.KeyCode() < 256 && event
.KeyCode() >= 0)) ||
5220 event
.KeyCode() == WXK_F2
) &&
5221 !IsCellEditControlEnabled() &&
5222 CanEnableCellControl() )
5224 EnableCellEditControl();
5225 int row
= m_currentCellCoords
.GetRow();
5226 int col
= m_currentCellCoords
.GetCol();
5227 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5228 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5229 editor
->StartingKey(event
);
5235 // let others process char events with modifiers or all
5236 // char events for readonly cells
5243 m_inOnKeyDown
= FALSE
;
5246 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5250 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5252 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5254 HideCellEditControl();
5255 DisableCellEditControl();
5257 // Clear the old current cell highlight
5258 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5260 // Otherwise refresh redraws the highlight!
5261 m_currentCellCoords
= coords
;
5263 m_gridWin
->Refresh( FALSE
, &r
);
5266 m_currentCellCoords
= coords
;
5268 wxClientDC
dc(m_gridWin
);
5271 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5272 DrawCellHighlight(dc
, attr
);
5276 // SN: For my extended selection code, automatic
5277 // deselection is definitely not a good idea.
5278 if ( IsSelection() )
5280 wxRect
r( SelectionToDeviceRect() );
5282 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
5289 // ------ functions to get/send data (see also public functions)
5292 bool wxGrid::GetModelValues()
5296 // all we need to do is repaint the grid
5298 m_gridWin
->Refresh();
5306 bool wxGrid::SetModelValues()
5312 for ( row
= 0; row
< m_numRows
; row
++ )
5314 for ( col
= 0; col
< m_numCols
; col
++ )
5316 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5328 // Note - this function only draws cells that are in the list of
5329 // exposed cells (usually set from the update region by
5330 // CalcExposedCells)
5332 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5334 if ( !m_numRows
|| !m_numCols
) return;
5337 size_t numCells
= m_cellsExposed
.GetCount();
5339 for ( i
= 0; i
< numCells
; i
++ )
5341 DrawCell( dc
, m_cellsExposed
[i
] );
5346 void wxGrid::DrawGridSpace( wxDC
& dc
)
5348 if ( m_numRows
&& m_numCols
)
5351 m_gridWin
->GetClientSize( &cw
, &ch
);
5354 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5356 if ( right
> GetColRight(m_numCols
-1) ||
5357 bottom
> GetRowBottom(m_numRows
-1) )
5360 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5362 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5363 dc
.SetPen( *wxTRANSPARENT_PEN
);
5365 if ( right
> GetColRight(m_numCols
-1) )
5366 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
5367 right
- GetColRight(m_numCols
-1), ch
);
5369 if ( bottom
> GetRowBottom(m_numRows
-1) )
5370 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
5371 cw
, bottom
- GetRowBottom(m_numRows
-1) );
5377 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5379 int row
= coords
.GetRow();
5380 int col
= coords
.GetCol();
5382 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5385 // we draw the cell border ourselves
5386 #if !WXGRID_DRAW_LINES
5387 if ( m_gridLinesEnabled
)
5388 DrawCellBorder( dc
, coords
);
5391 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5393 bool isCurrent
= coords
== m_currentCellCoords
;
5396 rect
.x
= GetColLeft(col
);
5397 rect
.y
= GetRowTop(row
);
5398 rect
.width
= GetColWidth(col
) - 1;
5399 rect
.height
= GetRowHeight(row
) - 1;
5401 // if the editor is shown, we should use it and not the renderer
5402 if ( isCurrent
&& IsCellEditControlEnabled() )
5404 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5405 editor
->PaintBackground(rect
, attr
);
5410 // but all the rest is drawn by the cell renderer and hence may be
5412 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5413 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5420 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5422 int row
= m_currentCellCoords
.GetRow();
5423 int col
= m_currentCellCoords
.GetCol();
5425 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5429 rect
.x
= GetColLeft(col
);
5430 rect
.y
= GetRowTop(row
);
5431 rect
.width
= GetColWidth(col
) - 1;
5432 rect
.height
= GetRowHeight(row
) - 1;
5434 // hmmm... what could we do here to show that the cell is disabled?
5435 // for now, I just draw a thinner border than for the other ones, but
5436 // it doesn't look really good
5437 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5438 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5440 dc
.DrawRectangle(rect
);
5443 // VZ: my experiments with 3d borders...
5445 // how to properly set colours for arbitrary bg?
5446 wxCoord x1
= rect
.x
,
5448 x2
= rect
.x
+ rect
.width
-1,
5449 y2
= rect
.y
+ rect
.height
-1;
5451 dc
.SetPen(*wxWHITE_PEN
);
5452 dc
.DrawLine(x1
, y1
, x2
, y1
);
5453 dc
.DrawLine(x1
, y1
, x1
, y2
);
5455 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5456 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5458 dc
.SetPen(*wxBLACK_PEN
);
5459 dc
.DrawLine(x1
, y2
, x2
, y2
);
5460 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5465 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5467 int row
= coords
.GetRow();
5468 int col
= coords
.GetCol();
5469 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5472 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5474 // right hand border
5476 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5477 GetColRight(col
), GetRowBottom(row
) );
5481 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5482 GetColRight(col
), GetRowBottom(row
) );
5485 void wxGrid::DrawHighlight(wxDC
& dc
)
5487 // This if block was previously in wxGrid::OnPaint but that doesn't
5488 // seem to get called under wxGTK - MB
5490 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5491 m_numRows
&& m_numCols
)
5493 m_currentCellCoords
.Set(0, 0);
5496 if ( IsCellEditControlEnabled() )
5498 // don't show highlight when the edit control is shown
5502 // if the active cell was repainted, repaint its highlight too because it
5503 // might have been damaged by the grid lines
5504 size_t count
= m_cellsExposed
.GetCount();
5505 for ( size_t n
= 0; n
< count
; n
++ )
5507 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5509 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5510 DrawCellHighlight(dc
, attr
);
5518 // TODO: remove this ???
5519 // This is used to redraw all grid lines e.g. when the grid line colour
5522 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5524 if ( !m_gridLinesEnabled
||
5526 !m_numCols
) return;
5528 int top
, bottom
, left
, right
;
5534 m_gridWin
->GetClientSize(&cw
, &ch
);
5536 // virtual coords of visible area
5538 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5539 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5544 reg
.GetBox(x
, y
, w
, h
);
5545 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5546 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5550 m_gridWin
->GetClientSize(&cw
, &ch
);
5551 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5552 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5555 // avoid drawing grid lines past the last row and col
5557 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5558 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5560 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5562 // horizontal grid lines
5565 for ( i
= 0; i
< m_numRows
; i
++ )
5567 int bot
= GetRowBottom(i
) - 1;
5576 dc
.DrawLine( left
, bot
, right
, bot
);
5581 // vertical grid lines
5583 for ( i
= 0; i
< m_numCols
; i
++ )
5585 int colRight
= GetColRight(i
) - 1;
5586 if ( colRight
> right
)
5591 if ( colRight
>= left
)
5593 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5599 void wxGrid::DrawRowLabels( wxDC
& dc
)
5601 if ( !m_numRows
|| !m_numCols
) return;
5604 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5606 for ( i
= 0; i
< numLabels
; i
++ )
5608 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5613 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5615 if ( GetRowHeight(row
) <= 0 )
5618 int rowTop
= GetRowTop(row
),
5619 rowBottom
= GetRowBottom(row
) - 1;
5621 dc
.SetPen( *wxBLACK_PEN
);
5622 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5623 m_rowLabelWidth
-1, rowBottom
);
5625 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5627 dc
.SetPen( *wxWHITE_PEN
);
5628 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5629 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5631 dc
.SetBackgroundMode( wxTRANSPARENT
);
5632 dc
.SetTextForeground( GetLabelTextColour() );
5633 dc
.SetFont( GetLabelFont() );
5636 GetRowLabelAlignment( &hAlign
, &vAlign
);
5640 rect
.SetY( GetRowTop(row
) + 2 );
5641 rect
.SetWidth( m_rowLabelWidth
- 4 );
5642 rect
.SetHeight( GetRowHeight(row
) - 4 );
5643 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5647 void wxGrid::DrawColLabels( wxDC
& dc
)
5649 if ( !m_numRows
|| !m_numCols
) return;
5652 size_t numLabels
= m_colLabelsExposed
.GetCount();
5654 for ( i
= 0; i
< numLabels
; i
++ )
5656 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5661 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5663 if ( GetColWidth(col
) <= 0 )
5666 int colLeft
= GetColLeft(col
),
5667 colRight
= GetColRight(col
) - 1;
5669 dc
.SetPen( *wxBLACK_PEN
);
5670 dc
.DrawLine( colRight
, 0,
5671 colRight
, m_colLabelHeight
-1 );
5673 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5674 colRight
, m_colLabelHeight
-1 );
5676 dc
.SetPen( *wxWHITE_PEN
);
5677 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5678 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5680 dc
.SetBackgroundMode( wxTRANSPARENT
);
5681 dc
.SetTextForeground( GetLabelTextColour() );
5682 dc
.SetFont( GetLabelFont() );
5684 dc
.SetBackgroundMode( wxTRANSPARENT
);
5685 dc
.SetTextForeground( GetLabelTextColour() );
5686 dc
.SetFont( GetLabelFont() );
5689 GetColLabelAlignment( &hAlign
, &vAlign
);
5692 rect
.SetX( colLeft
+ 2 );
5694 rect
.SetWidth( GetColWidth(col
) - 4 );
5695 rect
.SetHeight( m_colLabelHeight
- 4 );
5696 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5700 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5701 const wxString
& value
,
5706 long textWidth
, textHeight
;
5707 long lineWidth
, lineHeight
;
5708 wxArrayString lines
;
5710 dc
.SetClippingRegion( rect
);
5711 StringToLines( value
, lines
);
5712 if ( lines
.GetCount() )
5714 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5715 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5718 switch ( horizAlign
)
5721 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5725 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5734 switch ( vertAlign
)
5737 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5741 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5750 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5752 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
5757 dc
.DestroyClippingRegion();
5761 // Split multi line text up into an array of strings. Any existing
5762 // contents of the string array are preserved.
5764 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5768 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5769 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5771 while ( startPos
< (int)tVal
.Length() )
5773 pos
= tVal
.Mid(startPos
).Find( eol
);
5778 else if ( pos
== 0 )
5780 lines
.Add( wxEmptyString
);
5784 lines
.Add( value
.Mid(startPos
, pos
) );
5788 if ( startPos
< (int)value
.Length() )
5790 lines
.Add( value
.Mid( startPos
) );
5795 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5796 wxArrayString
& lines
,
5797 long *width
, long *height
)
5804 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5806 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5807 w
= wxMax( w
, lineW
);
5817 // ------ Edit control functions
5821 void wxGrid::EnableEditing( bool edit
)
5823 // TODO: improve this ?
5825 if ( edit
!= m_editable
)
5829 // FIXME IMHO this won't disable the edit control if edit == FALSE
5830 // because of the check in the beginning of
5831 // EnableCellEditControl() just below (VZ)
5832 EnableCellEditControl(m_editable
);
5837 void wxGrid::EnableCellEditControl( bool enable
)
5842 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5843 SetCurrentCell( 0, 0 );
5845 if ( enable
!= m_cellEditCtrlEnabled
)
5847 // TODO allow the app to Veto() this event?
5848 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5852 // this should be checked by the caller!
5853 wxASSERT_MSG( CanEnableCellControl(),
5854 _T("can't enable editing for this cell!") );
5856 // do it before ShowCellEditControl()
5857 m_cellEditCtrlEnabled
= enable
;
5859 ShowCellEditControl();
5863 HideCellEditControl();
5864 SaveEditControlValue();
5866 // do it after HideCellEditControl()
5867 m_cellEditCtrlEnabled
= enable
;
5872 bool wxGrid::IsCurrentCellReadOnly() const
5875 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5876 bool readonly
= attr
->IsReadOnly();
5882 bool wxGrid::CanEnableCellControl() const
5884 return m_editable
&& !IsCurrentCellReadOnly();
5887 bool wxGrid::IsCellEditControlEnabled() const
5889 // the cell edit control might be disable for all cells or just for the
5890 // current one if it's read only
5891 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5894 void wxGrid::ShowCellEditControl()
5896 if ( IsCellEditControlEnabled() )
5898 if ( !IsVisible( m_currentCellCoords
) )
5904 wxRect rect
= CellToRect( m_currentCellCoords
);
5905 int row
= m_currentCellCoords
.GetRow();
5906 int col
= m_currentCellCoords
.GetCol();
5908 // convert to scrolled coords
5910 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5912 // done in PaintBackground()
5914 // erase the highlight and the cell contents because the editor
5915 // might not cover the entire cell
5916 wxClientDC
dc( m_gridWin
);
5918 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5919 dc
.SetPen(*wxTRANSPARENT_PEN
);
5920 dc
.DrawRectangle(rect
);
5923 // cell is shifted by one pixel
5927 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5928 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5929 if ( !editor
->IsCreated() )
5931 editor
->Create(m_gridWin
, -1,
5932 new wxGridCellEditorEvtHandler(this, editor
));
5935 editor
->Show( TRUE
, attr
);
5937 editor
->SetSize( rect
);
5939 editor
->BeginEdit(row
, col
, this);
5948 void wxGrid::HideCellEditControl()
5950 if ( IsCellEditControlEnabled() )
5952 int row
= m_currentCellCoords
.GetRow();
5953 int col
= m_currentCellCoords
.GetCol();
5955 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5956 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5957 editor
->Show( FALSE
);
5960 m_gridWin
->SetFocus();
5965 void wxGrid::SaveEditControlValue()
5967 if ( IsCellEditControlEnabled() )
5969 int row
= m_currentCellCoords
.GetRow();
5970 int col
= m_currentCellCoords
.GetCol();
5972 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5973 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5974 bool changed
= editor
->EndEdit(row
, col
, this);
5981 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5982 m_currentCellCoords
.GetRow(),
5983 m_currentCellCoords
.GetCol() );
5990 // ------ Grid location functions
5991 // Note that all of these functions work with the logical coordinates of
5992 // grid cells and labels so you will need to convert from device
5993 // coordinates for mouse events etc.
5996 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5998 int row
= YToRow(y
);
5999 int col
= XToCol(x
);
6001 if ( row
== -1 || col
== -1 )
6003 coords
= wxGridNoCellCoords
;
6007 coords
.Set( row
, col
);
6012 int wxGrid::YToRow( int y
)
6016 for ( i
= 0; i
< m_numRows
; i
++ )
6018 if ( y
< GetRowBottom(i
) )
6026 int wxGrid::XToCol( int x
)
6030 for ( i
= 0; i
< m_numCols
; i
++ )
6032 if ( x
< GetColRight(i
) )
6040 // return the row number that that the y coord is near the edge of, or
6041 // -1 if not near an edge
6043 int wxGrid::YToEdgeOfRow( int y
)
6047 for ( i
= 0; i
< m_numRows
; i
++ )
6049 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6051 d
= abs( y
- GetRowBottom(i
) );
6052 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6061 // return the col number that that the x coord is near the edge of, or
6062 // -1 if not near an edge
6064 int wxGrid::XToEdgeOfCol( int x
)
6068 for ( i
= 0; i
< m_numCols
; i
++ )
6070 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6072 d
= abs( x
- GetColRight(i
) );
6073 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6082 wxRect
wxGrid::CellToRect( int row
, int col
)
6084 wxRect
rect( -1, -1, -1, -1 );
6086 if ( row
>= 0 && row
< m_numRows
&&
6087 col
>= 0 && col
< m_numCols
)
6089 rect
.x
= GetColLeft(col
);
6090 rect
.y
= GetRowTop(row
);
6091 rect
.width
= GetColWidth(col
);
6092 rect
.height
= GetRowHeight(row
);
6099 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6101 // get the cell rectangle in logical coords
6103 wxRect
r( CellToRect( row
, col
) );
6105 // convert to device coords
6107 int left
, top
, right
, bottom
;
6108 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6109 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6111 // check against the client area of the grid window
6114 m_gridWin
->GetClientSize( &cw
, &ch
);
6116 if ( wholeCellVisible
)
6118 // is the cell wholly visible ?
6120 return ( left
>= 0 && right
<= cw
&&
6121 top
>= 0 && bottom
<= ch
);
6125 // is the cell partly visible ?
6127 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6128 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6133 // make the specified cell location visible by doing a minimal amount
6136 void wxGrid::MakeCellVisible( int row
, int col
)
6139 int xpos
= -1, ypos
= -1;
6141 if ( row
>= 0 && row
< m_numRows
&&
6142 col
>= 0 && col
< m_numCols
)
6144 // get the cell rectangle in logical coords
6146 wxRect
r( CellToRect( row
, col
) );
6148 // convert to device coords
6150 int left
, top
, right
, bottom
;
6151 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6152 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6155 m_gridWin
->GetClientSize( &cw
, &ch
);
6161 else if ( bottom
> ch
)
6163 int h
= r
.GetHeight();
6165 for ( i
= row
-1; i
>= 0; i
-- )
6167 int rowHeight
= GetRowHeight(i
);
6168 if ( h
+ rowHeight
> ch
)
6175 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6176 // have rounding errors (this is important, because if we do, we
6177 // might not scroll at all and some cells won't be redrawn)
6178 ypos
+= GRID_SCROLL_LINE
/ 2;
6185 else if ( right
> cw
)
6187 int w
= r
.GetWidth();
6189 for ( i
= col
-1; i
>= 0; i
-- )
6191 int colWidth
= GetColWidth(i
);
6192 if ( w
+ colWidth
> cw
)
6199 // see comment for ypos above
6200 xpos
+= GRID_SCROLL_LINE
/ 2;
6203 if ( xpos
!= -1 || ypos
!= -1 )
6205 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6206 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6207 Scroll( xpos
, ypos
);
6215 // ------ Grid cursor movement functions
6218 bool wxGrid::MoveCursorUp( bool expandSelection
)
6220 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6221 m_currentCellCoords
.GetRow() > 0 )
6223 if ( expandSelection
)
6225 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6226 m_selectingKeyboard
= m_currentCellCoords
;
6227 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6228 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6229 m_selectingKeyboard
.GetCol() );
6230 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6235 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6236 m_currentCellCoords
.GetCol() );
6237 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6238 m_currentCellCoords
.GetCol() );
6247 bool wxGrid::MoveCursorDown( bool expandSelection
)
6249 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6250 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6252 if ( expandSelection
)
6254 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6255 m_selectingKeyboard
= m_currentCellCoords
;
6256 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6257 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6258 m_selectingKeyboard
.GetCol() );
6259 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6264 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6265 m_currentCellCoords
.GetCol() );
6266 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6267 m_currentCellCoords
.GetCol() );
6276 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6278 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6279 m_currentCellCoords
.GetCol() > 0 )
6281 if ( expandSelection
)
6283 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6284 m_selectingKeyboard
= m_currentCellCoords
;
6285 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6286 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6287 m_selectingKeyboard
.GetCol() );
6288 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6293 MakeCellVisible( m_currentCellCoords
.GetRow(),
6294 m_currentCellCoords
.GetCol() - 1 );
6295 SetCurrentCell( m_currentCellCoords
.GetRow(),
6296 m_currentCellCoords
.GetCol() - 1 );
6305 bool wxGrid::MoveCursorRight( bool expandSelection
)
6307 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6308 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6310 if ( expandSelection
)
6312 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6313 m_selectingKeyboard
= m_currentCellCoords
;
6314 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6315 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6316 m_selectingKeyboard
.GetCol() );
6317 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6322 MakeCellVisible( m_currentCellCoords
.GetRow(),
6323 m_currentCellCoords
.GetCol() + 1 );
6324 SetCurrentCell( m_currentCellCoords
.GetRow(),
6325 m_currentCellCoords
.GetCol() + 1 );
6334 bool wxGrid::MovePageUp()
6336 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6338 int row
= m_currentCellCoords
.GetRow();
6342 m_gridWin
->GetClientSize( &cw
, &ch
);
6344 int y
= GetRowTop(row
);
6345 int newRow
= YToRow( y
- ch
+ 1 );
6350 else if ( newRow
== row
)
6355 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6356 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6364 bool wxGrid::MovePageDown()
6366 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6368 int row
= m_currentCellCoords
.GetRow();
6369 if ( row
< m_numRows
)
6372 m_gridWin
->GetClientSize( &cw
, &ch
);
6374 int y
= GetRowTop(row
);
6375 int newRow
= YToRow( y
+ ch
);
6378 newRow
= m_numRows
- 1;
6380 else if ( newRow
== row
)
6385 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6386 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6394 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
6397 m_currentCellCoords
!= wxGridNoCellCoords
&&
6398 m_currentCellCoords
.GetRow() > 0 )
6400 int row
= m_currentCellCoords
.GetRow();
6401 int col
= m_currentCellCoords
.GetCol();
6403 if ( m_table
->IsEmptyCell(row
, col
) )
6405 // starting in an empty cell: find the next block of
6411 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6414 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6416 // starting at the top of a block: find the next block
6422 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6427 // starting within a block: find the top of the block
6432 if ( m_table
->IsEmptyCell(row
, col
) )
6440 MakeCellVisible( row
, col
);
6441 if ( expandSelection
)
6443 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6444 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6449 SetCurrentCell( row
, col
);
6457 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6460 m_currentCellCoords
!= wxGridNoCellCoords
&&
6461 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6463 int row
= m_currentCellCoords
.GetRow();
6464 int col
= m_currentCellCoords
.GetCol();
6466 if ( m_table
->IsEmptyCell(row
, col
) )
6468 // starting in an empty cell: find the next block of
6471 while ( row
< m_numRows
-1 )
6474 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6477 else if ( m_table
->IsEmptyCell(row
+1, col
) )
6479 // starting at the bottom of a block: find the next block
6482 while ( row
< m_numRows
-1 )
6485 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6490 // starting within a block: find the bottom of the block
6492 while ( row
< m_numRows
-1 )
6495 if ( m_table
->IsEmptyCell(row
, col
) )
6503 MakeCellVisible( row
, col
);
6504 if ( expandSelection
)
6506 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6507 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6512 SetCurrentCell( row
, col
);
6521 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6524 m_currentCellCoords
!= wxGridNoCellCoords
&&
6525 m_currentCellCoords
.GetCol() > 0 )
6527 int row
= m_currentCellCoords
.GetRow();
6528 int col
= m_currentCellCoords
.GetCol();
6530 if ( m_table
->IsEmptyCell(row
, col
) )
6532 // starting in an empty cell: find the next block of
6538 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6541 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6543 // starting at the left of a block: find the next block
6549 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6554 // starting within a block: find the left of the block
6559 if ( m_table
->IsEmptyCell(row
, col
) )
6567 MakeCellVisible( row
, col
);
6568 if ( expandSelection
)
6570 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6571 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6576 SetCurrentCell( row
, col
);
6585 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6588 m_currentCellCoords
!= wxGridNoCellCoords
&&
6589 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6591 int row
= m_currentCellCoords
.GetRow();
6592 int col
= m_currentCellCoords
.GetCol();
6594 if ( m_table
->IsEmptyCell(row
, col
) )
6596 // starting in an empty cell: find the next block of
6599 while ( col
< m_numCols
-1 )
6602 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6605 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6607 // starting at the right of a block: find the next block
6610 while ( col
< m_numCols
-1 )
6613 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6618 // starting within a block: find the right of the block
6620 while ( col
< m_numCols
-1 )
6623 if ( m_table
->IsEmptyCell(row
, col
) )
6631 MakeCellVisible( row
, col
);
6632 if ( expandSelection
)
6634 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6635 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6640 SetCurrentCell( row
, col
);
6652 // ------ Label values and formatting
6655 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6657 *horiz
= m_rowLabelHorizAlign
;
6658 *vert
= m_rowLabelVertAlign
;
6661 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6663 *horiz
= m_colLabelHorizAlign
;
6664 *vert
= m_colLabelVertAlign
;
6667 wxString
wxGrid::GetRowLabelValue( int row
)
6671 return m_table
->GetRowLabelValue( row
);
6681 wxString
wxGrid::GetColLabelValue( int col
)
6685 return m_table
->GetColLabelValue( col
);
6696 void wxGrid::SetRowLabelSize( int width
)
6698 width
= wxMax( width
, 0 );
6699 if ( width
!= m_rowLabelWidth
)
6703 m_rowLabelWin
->Show( FALSE
);
6704 m_cornerLabelWin
->Show( FALSE
);
6706 else if ( m_rowLabelWidth
== 0 )
6708 m_rowLabelWin
->Show( TRUE
);
6709 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6712 m_rowLabelWidth
= width
;
6719 void wxGrid::SetColLabelSize( int height
)
6721 height
= wxMax( height
, 0 );
6722 if ( height
!= m_colLabelHeight
)
6726 m_colLabelWin
->Show( FALSE
);
6727 m_cornerLabelWin
->Show( FALSE
);
6729 else if ( m_colLabelHeight
== 0 )
6731 m_colLabelWin
->Show( TRUE
);
6732 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6735 m_colLabelHeight
= height
;
6742 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6744 if ( m_labelBackgroundColour
!= colour
)
6746 m_labelBackgroundColour
= colour
;
6747 m_rowLabelWin
->SetBackgroundColour( colour
);
6748 m_colLabelWin
->SetBackgroundColour( colour
);
6749 m_cornerLabelWin
->SetBackgroundColour( colour
);
6751 if ( !GetBatchCount() )
6753 m_rowLabelWin
->Refresh();
6754 m_colLabelWin
->Refresh();
6755 m_cornerLabelWin
->Refresh();
6760 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6762 if ( m_labelTextColour
!= colour
)
6764 m_labelTextColour
= colour
;
6765 if ( !GetBatchCount() )
6767 m_rowLabelWin
->Refresh();
6768 m_colLabelWin
->Refresh();
6773 void wxGrid::SetLabelFont( const wxFont
& font
)
6776 if ( !GetBatchCount() )
6778 m_rowLabelWin
->Refresh();
6779 m_colLabelWin
->Refresh();
6783 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6785 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6787 m_rowLabelHorizAlign
= horiz
;
6790 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6792 m_rowLabelVertAlign
= vert
;
6795 if ( !GetBatchCount() )
6797 m_rowLabelWin
->Refresh();
6801 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6803 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6805 m_colLabelHorizAlign
= horiz
;
6808 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6810 m_colLabelVertAlign
= vert
;
6813 if ( !GetBatchCount() )
6815 m_colLabelWin
->Refresh();
6819 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6823 m_table
->SetRowLabelValue( row
, s
);
6824 if ( !GetBatchCount() )
6826 wxRect rect
= CellToRect( row
, 0);
6827 if ( rect
.height
> 0 )
6829 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6831 rect
.width
= m_rowLabelWidth
;
6832 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6838 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6842 m_table
->SetColLabelValue( col
, s
);
6843 if ( !GetBatchCount() )
6845 wxRect rect
= CellToRect( 0, col
);
6846 if ( rect
.width
> 0 )
6848 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6850 rect
.height
= m_colLabelHeight
;
6851 m_colLabelWin
->Refresh( TRUE
, &rect
);
6857 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6859 if ( m_gridLineColour
!= colour
)
6861 m_gridLineColour
= colour
;
6863 wxClientDC
dc( m_gridWin
);
6865 DrawAllGridLines( dc
, wxRegion() );
6869 void wxGrid::EnableGridLines( bool enable
)
6871 if ( enable
!= m_gridLinesEnabled
)
6873 m_gridLinesEnabled
= enable
;
6875 if ( !GetBatchCount() )
6879 wxClientDC
dc( m_gridWin
);
6881 DrawAllGridLines( dc
, wxRegion() );
6885 m_gridWin
->Refresh();
6892 int wxGrid::GetDefaultRowSize()
6894 return m_defaultRowHeight
;
6897 int wxGrid::GetRowSize( int row
)
6899 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6901 return GetRowHeight(row
);
6904 int wxGrid::GetDefaultColSize()
6906 return m_defaultColWidth
;
6909 int wxGrid::GetColSize( int col
)
6911 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6913 return GetColWidth(col
);
6916 // ============================================================================
6917 // access to the grid attributes: each of them has a default value in the grid
6918 // itself and may be overidden on a per-cell basis
6919 // ============================================================================
6921 // ----------------------------------------------------------------------------
6922 // setting default attributes
6923 // ----------------------------------------------------------------------------
6925 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6927 m_defaultCellAttr
->SetBackgroundColour(col
);
6929 m_gridWin
->SetBackgroundColour(col
);
6933 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6935 m_defaultCellAttr
->SetTextColour(col
);
6938 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6940 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6943 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6945 m_defaultCellAttr
->SetFont(font
);
6948 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6950 m_defaultCellAttr
->SetRenderer(renderer
);
6953 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6955 m_defaultCellAttr
->SetEditor(editor
);
6958 // ----------------------------------------------------------------------------
6959 // access to the default attrbiutes
6960 // ----------------------------------------------------------------------------
6962 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6964 return m_defaultCellAttr
->GetBackgroundColour();
6967 wxColour
wxGrid::GetDefaultCellTextColour()
6969 return m_defaultCellAttr
->GetTextColour();
6972 wxFont
wxGrid::GetDefaultCellFont()
6974 return m_defaultCellAttr
->GetFont();
6977 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6979 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6982 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6984 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
6987 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6989 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6992 // ----------------------------------------------------------------------------
6993 // access to cell attributes
6994 // ----------------------------------------------------------------------------
6996 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6998 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6999 wxColour colour
= attr
->GetBackgroundColour();
7004 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7006 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7007 wxColour colour
= attr
->GetTextColour();
7012 wxFont
wxGrid::GetCellFont( int row
, int col
)
7014 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7015 wxFont font
= attr
->GetFont();
7020 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7022 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7023 attr
->GetAlignment(horiz
, vert
);
7027 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7029 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7030 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7036 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7038 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7039 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7045 bool wxGrid::IsReadOnly(int row
, int col
) const
7047 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7048 bool isReadOnly
= attr
->IsReadOnly();
7053 // ----------------------------------------------------------------------------
7054 // attribute support: cache, automatic provider creation, ...
7055 // ----------------------------------------------------------------------------
7057 bool wxGrid::CanHaveAttributes()
7064 return m_table
->CanHaveAttributes();
7067 void wxGrid::ClearAttrCache()
7069 if ( m_attrCache
.row
!= -1 )
7071 wxSafeDecRef(m_attrCache
.attr
);
7072 m_attrCache
.row
= -1;
7076 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7078 wxGrid
*self
= (wxGrid
*)this; // const_cast
7080 self
->ClearAttrCache();
7081 self
->m_attrCache
.row
= row
;
7082 self
->m_attrCache
.col
= col
;
7083 self
->m_attrCache
.attr
= attr
;
7087 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7089 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7091 *attr
= m_attrCache
.attr
;
7092 wxSafeIncRef(m_attrCache
.attr
);
7094 #ifdef DEBUG_ATTR_CACHE
7095 gs_nAttrCacheHits
++;
7102 #ifdef DEBUG_ATTR_CACHE
7103 gs_nAttrCacheMisses
++;
7109 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7111 wxGridCellAttr
*attr
;
7112 if ( !LookupAttr(row
, col
, &attr
) )
7114 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7115 CacheAttr(row
, col
, attr
);
7119 attr
->SetDefAttr(m_defaultCellAttr
);
7123 attr
= m_defaultCellAttr
;
7130 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7132 wxGridCellAttr
*attr
;
7133 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7135 wxASSERT_MSG( m_table
,
7136 _T("we may only be called if CanHaveAttributes() "
7137 "returned TRUE and then m_table should be !NULL") );
7139 attr
= m_table
->GetAttr(row
, col
);
7142 attr
= new wxGridCellAttr
;
7144 // artificially inc the ref count to match DecRef() in caller
7147 m_table
->SetAttr(attr
, row
, col
);
7150 CacheAttr(row
, col
, attr
);
7152 attr
->SetDefAttr(m_defaultCellAttr
);
7156 // ----------------------------------------------------------------------------
7157 // setting column attributes (wrappers around SetColAttr)
7158 // ----------------------------------------------------------------------------
7160 void wxGrid::SetColFormatBool(int col
)
7162 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7165 void wxGrid::SetColFormatNumber(int col
)
7167 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7170 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7172 wxString typeName
= wxGRID_VALUE_FLOAT
;
7173 if ( (width
!= -1) || (precision
!= -1) )
7175 typeName
<< _T(':') << width
<< _T(',') << precision
;
7178 SetColFormatCustom(col
, typeName
);
7181 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7183 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7184 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7185 attr
->SetRenderer(renderer
);
7187 SetColAttr(col
, attr
);
7190 // ----------------------------------------------------------------------------
7191 // setting cell attributes: this is forwarded to the table
7192 // ----------------------------------------------------------------------------
7194 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7196 if ( CanHaveAttributes() )
7198 m_table
->SetRowAttr(attr
, row
);
7206 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7208 if ( CanHaveAttributes() )
7210 m_table
->SetColAttr(attr
, col
);
7218 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7220 if ( CanHaveAttributes() )
7222 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7223 attr
->SetBackgroundColour(colour
);
7228 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7230 if ( CanHaveAttributes() )
7232 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7233 attr
->SetTextColour(colour
);
7238 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7240 if ( CanHaveAttributes() )
7242 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7243 attr
->SetFont(font
);
7248 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7250 if ( CanHaveAttributes() )
7252 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7253 attr
->SetAlignment(horiz
, vert
);
7258 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7260 if ( CanHaveAttributes() )
7262 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7263 attr
->SetRenderer(renderer
);
7268 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7270 if ( CanHaveAttributes() )
7272 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7273 attr
->SetEditor(editor
);
7278 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7280 if ( CanHaveAttributes() )
7282 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7283 attr
->SetReadOnly(isReadOnly
);
7288 // ----------------------------------------------------------------------------
7289 // Data type registration
7290 // ----------------------------------------------------------------------------
7292 void wxGrid::RegisterDataType(const wxString
& typeName
,
7293 wxGridCellRenderer
* renderer
,
7294 wxGridCellEditor
* editor
)
7296 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7300 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7302 wxString typeName
= m_table
->GetTypeName(row
, col
);
7303 return GetDefaultEditorForType(typeName
);
7306 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7308 wxString typeName
= m_table
->GetTypeName(row
, col
);
7309 return GetDefaultRendererForType(typeName
);
7313 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7315 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7316 if ( index
== wxNOT_FOUND
)
7318 wxFAIL_MSG(wxT("Unknown data type name"));
7323 return m_typeRegistry
->GetEditor(index
);
7327 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7329 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7330 if ( index
== wxNOT_FOUND
)
7332 wxFAIL_MSG(wxT("Unknown data type name"));
7337 return m_typeRegistry
->GetRenderer(index
);
7341 // ----------------------------------------------------------------------------
7343 // ----------------------------------------------------------------------------
7345 void wxGrid::EnableDragRowSize( bool enable
)
7347 m_canDragRowSize
= enable
;
7351 void wxGrid::EnableDragColSize( bool enable
)
7353 m_canDragColSize
= enable
;
7356 void wxGrid::EnableDragGridSize( bool enable
)
7358 m_canDragGridSize
= enable
;
7362 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7364 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7366 if ( resizeExistingRows
)
7374 void wxGrid::SetRowSize( int row
, int height
)
7376 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7378 if ( m_rowHeights
.IsEmpty() )
7380 // need to really create the array
7384 int h
= wxMax( 0, height
);
7385 int diff
= h
- m_rowHeights
[row
];
7387 m_rowHeights
[row
] = h
;
7389 for ( i
= row
; i
< m_numRows
; i
++ )
7391 m_rowBottoms
[i
] += diff
;
7396 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7398 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
7400 if ( resizeExistingCols
)
7408 void wxGrid::SetColSize( int col
, int width
)
7410 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
7412 // should we check that it's bigger than GetColMinimalWidth(col) here?
7414 if ( m_colWidths
.IsEmpty() )
7416 // need to really create the array
7420 int w
= wxMax( 0, width
);
7421 int diff
= w
- m_colWidths
[col
];
7422 m_colWidths
[col
] = w
;
7425 for ( i
= col
; i
< m_numCols
; i
++ )
7427 m_colRights
[i
] += diff
;
7433 void wxGrid::SetColMinimalWidth( int col
, int width
)
7435 m_colMinWidths
.Put(col
, width
);
7438 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7440 m_rowMinHeights
.Put(row
, width
);
7443 int wxGrid::GetColMinimalWidth(int col
) const
7445 long value
= m_colMinWidths
.Get(col
);
7446 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
7449 int wxGrid::GetRowMinimalHeight(int row
) const
7451 long value
= m_rowMinHeights
.Get(row
);
7452 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
7455 // ----------------------------------------------------------------------------
7457 // ----------------------------------------------------------------------------
7459 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
7461 wxClientDC
dc(m_gridWin
);
7463 // init both of them to avoid compiler warnings, even if weo nly need one
7471 wxCoord extent
, extentMax
= 0;
7472 int max
= column
? m_numRows
: m_numCols
;
7473 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7480 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7481 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7484 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7485 extent
= column
? size
.x
: size
.y
;
7486 if ( extent
> extentMax
)
7497 // now also compare with the column label extent
7499 dc
.SetFont( GetLabelFont() );
7502 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
7504 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
7506 extent
= column
? w
: h
;
7507 if ( extent
> extentMax
)
7514 // empty column - give default extent (notice that if extentMax is less
7515 // than default extent but != 0, it's ok)
7516 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7522 // leave some space around text
7528 SetColSize(col
, extentMax
);
7530 SetRowSize(row
, extentMax
);
7535 SetColMinimalWidth(col
, extentMax
);
7537 SetRowMinimalHeight(row
, extentMax
);
7541 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7543 int width
= m_rowLabelWidth
;
7545 for ( int col
= 0; col
< m_numCols
; col
++ )
7549 AutoSizeColumn(col
, setAsMin
);
7552 width
+= GetColWidth(col
);
7558 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7560 int height
= m_colLabelHeight
;
7562 for ( int row
= 0; row
< m_numRows
; row
++ )
7566 AutoSizeRow(row
, setAsMin
);
7569 height
+= GetRowHeight(row
);
7575 void wxGrid::AutoSize()
7578 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
7581 wxSize
wxGrid::DoGetBestSize() const
7583 // don't set sizes, only calculate them
7584 wxGrid
*self
= (wxGrid
*)this; // const_cast
7586 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
7587 self
->SetOrCalcRowSizes(TRUE
));
7595 // ----------------------------------------------------------------------------
7596 // cell value accessor functions
7597 // ----------------------------------------------------------------------------
7599 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
7603 m_table
->SetValue( row
, col
, s
.c_str() );
7604 if ( !GetBatchCount() )
7606 wxClientDC
dc( m_gridWin
);
7608 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7611 if ( m_currentCellCoords
.GetRow() == row
&&
7612 m_currentCellCoords
.GetCol() == col
&&
7613 IsCellEditControlEnabled())
7615 HideCellEditControl();
7616 ShowCellEditControl(); // will reread data from table
7623 // ------ Block, row and col selection
7626 void wxGrid::SelectRow( int row
, bool addToSelected
)
7628 if ( IsSelection() && !addToSelected
)
7631 m_selection
->SelectRow( row
);
7635 void wxGrid::SelectCol( int col
, bool addToSelected
)
7637 if ( IsSelection() && !addToSelected
)
7640 m_selection
->SelectCol( col
);
7644 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7647 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7649 if ( topRow
> bottomRow
)
7656 if ( leftCol
> rightCol
)
7663 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7664 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7666 if ( m_selectingTopLeft
!= updateTopLeft
||
7667 m_selectingBottomRight
!= updateBottomRight
)
7669 // Compute two optimal update rectangles:
7670 // Either one rectangle is a real subset of the
7671 // other, or they are (almost) disjoint!
7673 bool need_refresh
[4];
7677 need_refresh
[3] = FALSE
;
7680 // Store intermediate values
7681 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7682 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7683 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7684 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7686 // Determine the outer/inner coordinates.
7687 if (oldLeft
> leftCol
)
7693 if (oldTop
> topRow
)
7699 if (oldRight
< rightCol
)
7702 oldRight
= rightCol
;
7705 if (oldBottom
< bottomRow
)
7708 oldBottom
= bottomRow
;
7712 // Now, either the stuff marked old is the outer
7713 // rectangle or we don't have a situation where one
7714 // is contained in the other.
7716 if ( oldLeft
< leftCol
)
7718 need_refresh
[0] = TRUE
;
7719 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7721 wxGridCellCoords ( oldBottom
,
7725 if ( oldTop
< topRow
)
7727 need_refresh
[1] = TRUE
;
7728 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7730 wxGridCellCoords ( topRow
- 1,
7734 if ( oldRight
> rightCol
)
7736 need_refresh
[2] = TRUE
;
7737 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7739 wxGridCellCoords ( oldBottom
,
7743 if ( oldBottom
> bottomRow
)
7745 need_refresh
[3] = TRUE
;
7746 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7748 wxGridCellCoords ( oldBottom
,
7754 m_selectingTopLeft
= updateTopLeft
;
7755 m_selectingBottomRight
= updateBottomRight
;
7757 // various Refresh() calls
7758 for (i
= 0; i
< 4; i
++ )
7759 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7760 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7763 // never generate an event as it will be generated from
7764 // wxGridSelection::SelectBlock!
7767 void wxGrid::SelectAll()
7769 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
7772 bool wxGrid::IsSelection()
7774 return ( m_selection
->IsSelection() ||
7775 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7776 m_selectingBottomRight
!= wxGridNoCellCoords
) );
7779 bool wxGrid::IsInSelection( int row
, int col
)
7781 return ( m_selection
->IsInSelection( row
, col
) ||
7782 ( row
>= m_selectingTopLeft
.GetRow() &&
7783 col
>= m_selectingTopLeft
.GetCol() &&
7784 row
<= m_selectingBottomRight
.GetRow() &&
7785 col
<= m_selectingBottomRight
.GetCol() ) );
7788 void wxGrid::ClearSelection()
7790 m_selectingTopLeft
= wxGridNoCellCoords
;
7791 m_selectingBottomRight
= wxGridNoCellCoords
;
7792 m_selection
->ClearSelection();
7796 // This function returns the rectangle that encloses the given block
7797 // in device coords clipped to the client size of the grid window.
7799 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7800 const wxGridCellCoords
&bottomRight
)
7802 wxRect
rect( wxGridNoCellRect
);
7805 cellRect
= CellToRect( topLeft
);
7806 if ( cellRect
!= wxGridNoCellRect
)
7812 rect
= wxRect( 0, 0, 0, 0 );
7815 cellRect
= CellToRect( bottomRight
);
7816 if ( cellRect
!= wxGridNoCellRect
)
7822 return wxGridNoCellRect
;
7825 // convert to scrolled coords
7827 int left
, top
, right
, bottom
;
7828 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7829 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7832 m_gridWin
->GetClientSize( &cw
, &ch
);
7834 rect
.SetLeft( wxMax(0, left
) );
7835 rect
.SetTop( wxMax(0, top
) );
7836 rect
.SetRight( wxMin(cw
, right
) );
7837 rect
.SetBottom( wxMin(ch
, bottom
) );
7845 // ------ Grid event classes
7848 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7850 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7851 int row
, int col
, int x
, int y
, bool sel
,
7852 bool control
, bool shift
, bool alt
, bool meta
)
7853 : wxNotifyEvent( type
, id
)
7860 m_control
= control
;
7865 SetEventObject(obj
);
7869 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7871 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7872 int rowOrCol
, int x
, int y
,
7873 bool control
, bool shift
, bool alt
, bool meta
)
7874 : wxNotifyEvent( type
, id
)
7876 m_rowOrCol
= rowOrCol
;
7879 m_control
= control
;
7884 SetEventObject(obj
);
7888 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7890 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7891 const wxGridCellCoords
& topLeft
,
7892 const wxGridCellCoords
& bottomRight
,
7893 bool sel
, bool control
,
7894 bool shift
, bool alt
, bool meta
)
7895 : wxNotifyEvent( type
, id
)
7897 m_topLeft
= topLeft
;
7898 m_bottomRight
= bottomRight
;
7900 m_control
= control
;
7905 SetEventObject(obj
);
7909 #endif // ifndef wxUSE_NEW_GRID