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 long keycode
= 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 int 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(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(m_valueOld
);
745 DoReset(GetString());
749 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
753 long keycode
= 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 long keycode
= 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 pos
, size_t numRows
)
2254 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
2255 "but your derived table class does not override this function") );
2260 bool wxGridTableBase::AppendRows( size_t numRows
)
2262 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
2263 "but your derived table class does not override this function"));
2268 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
2270 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
2271 "but your derived table class does not override this function"));
2276 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
2278 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
2279 "but your derived table class does not override this function"));
2284 bool wxGridTableBase::AppendCols( size_t numCols
)
2286 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
2287 "but your derived table class does not override this function"));
2292 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
2294 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
2295 "but your derived table class does not override this function"));
2301 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2304 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2305 // how much it makes sense to us geeks.
2309 wxString
wxGridTableBase::GetColLabelValue( int col
)
2311 // default col labels are:
2312 // cols 0 to 25 : A-Z
2313 // cols 26 to 675 : AA-ZZ
2318 for ( n
= 1; ; n
++ )
2320 s
+= (_T('A') + (wxChar
)( col%26
));
2322 if ( col
< 0 ) break;
2325 // reverse the string...
2327 for ( i
= 0; i
< n
; i
++ )
2336 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2338 return wxGRID_VALUE_STRING
;
2341 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2342 const wxString
& typeName
)
2344 return typeName
== wxGRID_VALUE_STRING
;
2347 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2349 return CanGetValueAs(row
, col
, typeName
);
2352 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2357 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2362 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2367 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2368 long WXUNUSED(value
) )
2372 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2373 double WXUNUSED(value
) )
2377 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2378 bool WXUNUSED(value
) )
2383 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2384 const wxString
& WXUNUSED(typeName
) )
2389 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2390 const wxString
& WXUNUSED(typeName
),
2391 void* WXUNUSED(value
) )
2395 //////////////////////////////////////////////////////////////////////
2397 // Message class for the grid table to send requests and notifications
2401 wxGridTableMessage::wxGridTableMessage()
2403 m_table
= (wxGridTableBase
*) NULL
;
2409 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2410 int commandInt1
, int commandInt2
)
2414 m_comInt1
= commandInt1
;
2415 m_comInt2
= commandInt2
;
2420 //////////////////////////////////////////////////////////////////////
2422 // A basic grid table for string data. An object of this class will
2423 // created by wxGrid if you don't specify an alternative table class.
2426 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2428 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2430 wxGridStringTable::wxGridStringTable()
2435 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2440 m_data
.Alloc( numRows
);
2443 sa
.Alloc( numCols
);
2444 for ( col
= 0; col
< numCols
; col
++ )
2446 sa
.Add( wxEmptyString
);
2449 for ( row
= 0; row
< numRows
; row
++ )
2455 wxGridStringTable::~wxGridStringTable()
2459 int wxGridStringTable::GetNumberRows()
2461 return m_data
.GetCount();
2464 int wxGridStringTable::GetNumberCols()
2466 if ( m_data
.GetCount() > 0 )
2467 return m_data
[0].GetCount();
2472 wxString
wxGridStringTable::GetValue( int row
, int col
)
2474 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2475 _T("invalid row or column index in wxGridStringTable") );
2477 return m_data
[row
][col
];
2480 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2482 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2483 _T("invalid row or column index in wxGridStringTable") );
2485 m_data
[row
][col
] = value
;
2488 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2490 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2491 _T("invalid row or column index in wxGridStringTable") );
2493 return (m_data
[row
][col
] == wxEmptyString
);
2496 void wxGridStringTable::Clear()
2499 int numRows
, numCols
;
2501 numRows
= m_data
.GetCount();
2504 numCols
= m_data
[0].GetCount();
2506 for ( row
= 0; row
< numRows
; row
++ )
2508 for ( col
= 0; col
< numCols
; col
++ )
2510 m_data
[row
][col
] = wxEmptyString
;
2517 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2521 size_t curNumRows
= m_data
.GetCount();
2522 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2524 if ( pos
>= curNumRows
)
2526 return AppendRows( numRows
);
2530 sa
.Alloc( curNumCols
);
2531 for ( col
= 0; col
< curNumCols
; col
++ )
2533 sa
.Add( wxEmptyString
);
2536 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2538 m_data
.Insert( sa
, row
);
2540 UpdateAttrRows( pos
, numRows
);
2543 wxGridTableMessage
msg( this,
2544 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2548 GetView()->ProcessTableMessage( msg
);
2554 bool wxGridStringTable::AppendRows( size_t numRows
)
2558 size_t curNumRows
= m_data
.GetCount();
2559 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2562 if ( curNumCols
> 0 )
2564 sa
.Alloc( curNumCols
);
2565 for ( col
= 0; col
< curNumCols
; col
++ )
2567 sa
.Add( wxEmptyString
);
2571 for ( row
= 0; row
< numRows
; row
++ )
2578 wxGridTableMessage
msg( this,
2579 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2582 GetView()->ProcessTableMessage( msg
);
2588 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2592 size_t curNumRows
= m_data
.GetCount();
2594 if ( pos
>= curNumRows
)
2597 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2598 "Pos value is invalid for present table with %d rows",
2599 pos
, numRows
, curNumRows
);
2600 wxFAIL_MSG( wxT(errmsg
) );
2604 if ( numRows
> curNumRows
- pos
)
2606 numRows
= curNumRows
- pos
;
2609 if ( numRows
>= curNumRows
)
2611 m_data
.Empty(); // don't release memory just yet
2615 for ( n
= 0; n
< numRows
; n
++ )
2617 m_data
.Remove( pos
);
2620 UpdateAttrRows( pos
, -((int)numRows
) );
2623 wxGridTableMessage
msg( this,
2624 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2628 GetView()->ProcessTableMessage( msg
);
2634 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2638 size_t curNumRows
= m_data
.GetCount();
2639 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2641 if ( pos
>= curNumCols
)
2643 return AppendCols( numCols
);
2646 for ( row
= 0; row
< curNumRows
; row
++ )
2648 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2650 m_data
[row
].Insert( wxEmptyString
, col
);
2653 UpdateAttrCols( pos
, numCols
);
2656 wxGridTableMessage
msg( this,
2657 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2661 GetView()->ProcessTableMessage( msg
);
2667 bool wxGridStringTable::AppendCols( size_t numCols
)
2671 size_t curNumRows
= m_data
.GetCount();
2674 // TODO: something better than this ?
2676 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2677 "Call AppendRows() first") );
2681 for ( row
= 0; row
< curNumRows
; row
++ )
2683 for ( n
= 0; n
< numCols
; n
++ )
2685 m_data
[row
].Add( wxEmptyString
);
2691 wxGridTableMessage
msg( this,
2692 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2695 GetView()->ProcessTableMessage( msg
);
2701 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2705 size_t curNumRows
= m_data
.GetCount();
2706 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2708 if ( pos
>= curNumCols
)
2711 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2712 "Pos value is invalid for present table with %d cols",
2713 pos
, numCols
, curNumCols
);
2714 wxFAIL_MSG( wxT( errmsg
) );
2718 if ( numCols
> curNumCols
- pos
)
2720 numCols
= curNumCols
- pos
;
2723 for ( row
= 0; row
< curNumRows
; row
++ )
2725 if ( numCols
>= curNumCols
)
2727 m_data
[row
].Clear();
2731 for ( n
= 0; n
< numCols
; n
++ )
2733 m_data
[row
].Remove( pos
);
2737 UpdateAttrCols( pos
, -((int)numCols
) );
2740 wxGridTableMessage
msg( this,
2741 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2745 GetView()->ProcessTableMessage( msg
);
2751 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2753 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2755 // using default label
2757 return wxGridTableBase::GetRowLabelValue( row
);
2761 return m_rowLabels
[ row
];
2765 wxString
wxGridStringTable::GetColLabelValue( int col
)
2767 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2769 // using default label
2771 return wxGridTableBase::GetColLabelValue( col
);
2775 return m_colLabels
[ col
];
2779 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2781 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2783 int n
= m_rowLabels
.GetCount();
2785 for ( i
= n
; i
<= row
; i
++ )
2787 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2791 m_rowLabels
[row
] = value
;
2794 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2796 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2798 int n
= m_colLabels
.GetCount();
2800 for ( i
= n
; i
<= col
; i
++ )
2802 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2806 m_colLabels
[col
] = value
;
2811 //////////////////////////////////////////////////////////////////////
2812 //////////////////////////////////////////////////////////////////////
2814 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2816 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2817 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2818 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2819 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2822 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2824 const wxPoint
&pos
, const wxSize
&size
)
2825 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2830 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2834 // NO - don't do this because it will set both the x and y origin
2835 // coords to match the parent scrolled window and we just want to
2836 // set the y coord - MB
2838 // m_owner->PrepareDC( dc );
2841 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2842 dc
.SetDeviceOrigin( 0, -y
);
2844 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2845 m_owner
->DrawRowLabels( dc
);
2849 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2851 m_owner
->ProcessRowLabelMouseEvent( event
);
2855 // This seems to be required for wxMotif otherwise the mouse
2856 // cursor must be in the cell edit control to get key events
2858 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2860 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2865 //////////////////////////////////////////////////////////////////////
2867 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2869 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2870 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2871 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2872 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2875 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2877 const wxPoint
&pos
, const wxSize
&size
)
2878 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2883 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2887 // NO - don't do this because it will set both the x and y origin
2888 // coords to match the parent scrolled window and we just want to
2889 // set the x coord - MB
2891 // m_owner->PrepareDC( dc );
2894 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2895 dc
.SetDeviceOrigin( -x
, 0 );
2897 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2898 m_owner
->DrawColLabels( dc
);
2902 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2904 m_owner
->ProcessColLabelMouseEvent( event
);
2908 // This seems to be required for wxMotif otherwise the mouse
2909 // cursor must be in the cell edit control to get key events
2911 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2913 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2918 //////////////////////////////////////////////////////////////////////
2920 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2922 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2923 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2924 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2925 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2928 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2930 const wxPoint
&pos
, const wxSize
&size
)
2931 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2936 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2940 int client_height
= 0;
2941 int client_width
= 0;
2942 GetClientSize( &client_width
, &client_height
);
2944 dc
.SetPen( *wxBLACK_PEN
);
2945 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2946 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2948 dc
.SetPen( *wxWHITE_PEN
);
2949 dc
.DrawLine( 0, 0, client_width
, 0 );
2950 dc
.DrawLine( 0, 0, 0, client_height
);
2954 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2956 m_owner
->ProcessCornerLabelMouseEvent( event
);
2960 // This seems to be required for wxMotif otherwise the mouse
2961 // cursor must be in the cell edit control to get key events
2963 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2965 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2970 //////////////////////////////////////////////////////////////////////
2972 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2974 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2975 EVT_PAINT( wxGridWindow::OnPaint
)
2976 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2977 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2978 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2981 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2982 wxGridRowLabelWindow
*rowLblWin
,
2983 wxGridColLabelWindow
*colLblWin
,
2984 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2985 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
2988 m_rowLabelWin
= rowLblWin
;
2989 m_colLabelWin
= colLblWin
;
2990 SetBackgroundColour( "WHITE" );
2994 wxGridWindow::~wxGridWindow()
2999 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3001 wxPaintDC
dc( this );
3002 m_owner
->PrepareDC( dc
);
3003 wxRegion reg
= GetUpdateRegion();
3004 m_owner
->CalcCellsExposed( reg
);
3005 m_owner
->DrawGridCellArea( dc
);
3006 #if WXGRID_DRAW_LINES
3007 m_owner
->DrawAllGridLines( dc
, reg
);
3009 m_owner
->DrawGridSpace( dc
);
3010 m_owner
->DrawHighlight( dc
);
3014 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3016 wxPanel::ScrollWindow( dx
, dy
, rect
);
3017 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3018 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3022 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3024 m_owner
->ProcessGridCellMouseEvent( event
);
3028 // This seems to be required for wxMotif otherwise the mouse
3029 // cursor must be in the cell edit control to get key events
3031 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3033 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3037 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
3042 //////////////////////////////////////////////////////////////////////
3045 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3047 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3048 EVT_PAINT( wxGrid::OnPaint
)
3049 EVT_SIZE( wxGrid::OnSize
)
3050 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3051 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3054 wxGrid::wxGrid( wxWindow
*parent
,
3059 const wxString
& name
)
3060 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3061 m_colMinWidths(GRID_HASH_SIZE
),
3062 m_rowMinHeights(GRID_HASH_SIZE
)
3071 wxSafeDecRef(m_defaultCellAttr
);
3073 #ifdef DEBUG_ATTR_CACHE
3074 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3075 wxPrintf(_T("wxGrid attribute cache statistics: "
3076 "total: %u, hits: %u (%u%%)\n"),
3077 total
, gs_nAttrCacheHits
,
3078 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3084 delete m_typeRegistry
;
3090 // ----- internal init and update functions
3093 void wxGrid::Create()
3095 m_created
= FALSE
; // set to TRUE by CreateGrid
3097 m_table
= (wxGridTableBase
*) NULL
;
3100 m_cellEditCtrlEnabled
= FALSE
;
3102 m_defaultCellAttr
= new wxGridCellAttr
;
3103 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3105 // Set default cell attributes
3106 m_defaultCellAttr
->SetFont(GetFont());
3107 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
3108 m_defaultCellAttr
->SetTextColour(
3109 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3110 m_defaultCellAttr
->SetBackgroundColour(
3111 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3112 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3113 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3118 m_currentCellCoords
= wxGridNoCellCoords
;
3120 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3121 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3123 // create the type registry
3124 m_typeRegistry
= new wxGridTypeRegistry
;
3126 // subwindow components that make up the wxGrid
3127 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3132 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3137 m_colLabelWin
= new wxGridColLabelWindow( this,
3142 m_gridWin
= new wxGridWindow( this,
3149 SetTargetWindow( m_gridWin
);
3153 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3154 wxGrid::wxGridSelectionModes selmode
)
3158 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3163 m_numRows
= numRows
;
3164 m_numCols
= numCols
;
3166 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3167 m_table
->SetView( this );
3170 m_selection
= new wxGridSelection( this, selmode
);
3176 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3180 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3183 m_selection
->SetSelectionMode( selmode
);
3186 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3187 wxGrid::wxGridSelectionModes selmode
)
3191 // RD: Actually, this should probably be allowed. I think it would be
3192 // nice to be able to switch multiple Tables in and out of a single
3193 // View at runtime. Is there anything in the implmentation that would
3196 // At least, you now have to cope with m_selection
3197 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3202 m_numRows
= table
->GetNumberRows();
3203 m_numCols
= table
->GetNumberCols();
3206 m_table
->SetView( this );
3210 m_selection
= new wxGridSelection( this, selmode
);
3220 if ( m_numRows
<= 0 )
3221 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
3223 if ( m_numCols
<= 0 )
3224 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
3226 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3227 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3229 if ( m_rowLabelWin
)
3231 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3235 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3238 m_labelTextColour
= wxColour( _T("BLACK") );
3241 m_attrCache
.row
= -1;
3243 // TODO: something better than this ?
3245 m_labelFont
= this->GetFont();
3246 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3248 m_rowLabelHorizAlign
= wxLEFT
;
3249 m_rowLabelVertAlign
= wxCENTRE
;
3251 m_colLabelHorizAlign
= wxCENTRE
;
3252 m_colLabelVertAlign
= wxTOP
;
3254 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3255 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3257 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3258 m_defaultRowHeight
+= 8;
3260 m_defaultRowHeight
+= 4;
3263 m_gridLineColour
= wxColour( 128, 128, 255 );
3264 m_gridLinesEnabled
= TRUE
;
3266 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3267 m_winCapture
= (wxWindow
*)NULL
;
3268 m_canDragRowSize
= TRUE
;
3269 m_canDragColSize
= TRUE
;
3270 m_canDragGridSize
= TRUE
;
3272 m_dragRowOrCol
= -1;
3273 m_isDragging
= FALSE
;
3274 m_startDragPos
= wxDefaultPosition
;
3276 m_waitForSlowClick
= FALSE
;
3278 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3279 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3281 m_currentCellCoords
= wxGridNoCellCoords
;
3283 m_selectingTopLeft
= wxGridNoCellCoords
;
3284 m_selectingBottomRight
= wxGridNoCellCoords
;
3285 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3286 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3288 m_editable
= TRUE
; // default for whole grid
3290 m_inOnKeyDown
= FALSE
;
3299 // ----------------------------------------------------------------------------
3300 // the idea is to call these functions only when necessary because they create
3301 // quite big arrays which eat memory mostly unnecessary - in particular, if
3302 // default widths/heights are used for all rows/columns, we may not use these
3305 // with some extra code, it should be possible to only store the
3306 // widths/heights different from default ones but this will be done later...
3307 // ----------------------------------------------------------------------------
3309 void wxGrid::InitRowHeights()
3311 m_rowHeights
.Empty();
3312 m_rowBottoms
.Empty();
3314 m_rowHeights
.Alloc( m_numRows
);
3315 m_rowBottoms
.Alloc( m_numRows
);
3318 for ( int i
= 0; i
< m_numRows
; i
++ )
3320 m_rowHeights
.Add( m_defaultRowHeight
);
3321 rowBottom
+= m_defaultRowHeight
;
3322 m_rowBottoms
.Add( rowBottom
);
3326 void wxGrid::InitColWidths()
3328 m_colWidths
.Empty();
3329 m_colRights
.Empty();
3331 m_colWidths
.Alloc( m_numCols
);
3332 m_colRights
.Alloc( m_numCols
);
3334 for ( int i
= 0; i
< m_numCols
; i
++ )
3336 m_colWidths
.Add( m_defaultColWidth
);
3337 colRight
+= m_defaultColWidth
;
3338 m_colRights
.Add( colRight
);
3342 int wxGrid::GetColWidth(int col
) const
3344 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3347 int wxGrid::GetColLeft(int col
) const
3349 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3350 : m_colRights
[col
] - m_colWidths
[col
];
3353 int wxGrid::GetColRight(int col
) const
3355 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3359 int wxGrid::GetRowHeight(int row
) const
3361 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3364 int wxGrid::GetRowTop(int row
) const
3366 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3367 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3370 int wxGrid::GetRowBottom(int row
) const
3372 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3373 : m_rowBottoms
[row
];
3376 void wxGrid::CalcDimensions()
3379 GetClientSize( &cw
, &ch
);
3381 if ( m_numRows
> 0 && m_numCols
> 0 )
3383 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3384 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3386 // TODO: restore the scroll position that we had before sizing
3389 GetViewStart( &x
, &y
);
3390 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3391 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3397 void wxGrid::CalcWindowSizes()
3400 GetClientSize( &cw
, &ch
);
3402 if ( m_cornerLabelWin
->IsShown() )
3403 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3405 if ( m_colLabelWin
->IsShown() )
3406 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3408 if ( m_rowLabelWin
->IsShown() )
3409 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3411 if ( m_gridWin
->IsShown() )
3412 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3416 // this is called when the grid table sends a message to say that it
3417 // has been redimensioned
3419 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3423 // if we were using the default widths/heights so far, we must change them
3425 if ( m_colWidths
.IsEmpty() )
3430 if ( m_rowHeights
.IsEmpty() )
3435 switch ( msg
.GetId() )
3437 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3439 size_t pos
= msg
.GetCommandInt();
3440 int numRows
= msg
.GetCommandInt2();
3441 for ( i
= 0; i
< numRows
; i
++ )
3443 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3444 m_rowBottoms
.Insert( 0, pos
);
3446 m_numRows
+= numRows
;
3449 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3451 for ( i
= pos
; i
< m_numRows
; i
++ )
3453 bottom
+= m_rowHeights
[i
];
3454 m_rowBottoms
[i
] = bottom
;
3460 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3462 int numRows
= msg
.GetCommandInt();
3463 for ( i
= 0; i
< numRows
; i
++ )
3465 m_rowHeights
.Add( m_defaultRowHeight
);
3466 m_rowBottoms
.Add( 0 );
3469 int oldNumRows
= m_numRows
;
3470 m_numRows
+= numRows
;
3473 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3475 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3477 bottom
+= m_rowHeights
[i
];
3478 m_rowBottoms
[i
] = bottom
;
3484 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3486 size_t pos
= msg
.GetCommandInt();
3487 int numRows
= msg
.GetCommandInt2();
3488 for ( i
= 0; i
< numRows
; i
++ )
3490 m_rowHeights
.Remove( pos
);
3491 m_rowBottoms
.Remove( pos
);
3493 m_numRows
-= numRows
;
3498 m_colWidths
.Clear();
3499 m_colRights
.Clear();
3500 m_currentCellCoords
= wxGridNoCellCoords
;
3504 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3505 m_currentCellCoords
.Set( 0, 0 );
3508 for ( i
= 0; i
< m_numRows
; i
++ )
3510 h
+= m_rowHeights
[i
];
3511 m_rowBottoms
[i
] = h
;
3519 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3521 size_t pos
= msg
.GetCommandInt();
3522 int numCols
= msg
.GetCommandInt2();
3523 for ( i
= 0; i
< numCols
; i
++ )
3525 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3526 m_colRights
.Insert( 0, pos
);
3528 m_numCols
+= numCols
;
3531 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3533 for ( i
= pos
; i
< m_numCols
; i
++ )
3535 right
+= m_colWidths
[i
];
3536 m_colRights
[i
] = right
;
3542 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3544 int numCols
= msg
.GetCommandInt();
3545 for ( i
= 0; i
< numCols
; i
++ )
3547 m_colWidths
.Add( m_defaultColWidth
);
3548 m_colRights
.Add( 0 );
3551 int oldNumCols
= m_numCols
;
3552 m_numCols
+= numCols
;
3555 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3557 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3559 right
+= m_colWidths
[i
];
3560 m_colRights
[i
] = right
;
3566 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3568 size_t pos
= msg
.GetCommandInt();
3569 int numCols
= msg
.GetCommandInt2();
3570 for ( i
= 0; i
< numCols
; i
++ )
3572 m_colWidths
.Remove( pos
);
3573 m_colRights
.Remove( pos
);
3575 m_numCols
-= numCols
;
3579 #if 0 // leave the row alone here so that AppendCols will work subsequently
3581 m_rowHeights
.Clear();
3582 m_rowBottoms
.Clear();
3584 m_currentCellCoords
= wxGridNoCellCoords
;
3588 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3589 m_currentCellCoords
.Set( 0, 0 );
3592 for ( i
= 0; i
< m_numCols
; i
++ )
3594 w
+= m_colWidths
[i
];
3607 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3609 wxRegionIterator
iter( reg
);
3612 m_rowLabelsExposed
.Empty();
3619 // TODO: remove this when we can...
3620 // There is a bug in wxMotif that gives garbage update
3621 // rectangles if you jump-scroll a long way by clicking the
3622 // scrollbar with middle button. This is a work-around
3624 #if defined(__WXMOTIF__)
3626 m_gridWin
->GetClientSize( &cw
, &ch
);
3627 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3628 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3631 // logical bounds of update region
3634 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3635 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3637 // find the row labels within these bounds
3640 for ( row
= 0; row
< m_numRows
; row
++ )
3642 if ( GetRowBottom(row
) < top
)
3645 if ( GetRowTop(row
) > bottom
)
3648 m_rowLabelsExposed
.Add( row
);
3656 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3658 wxRegionIterator
iter( reg
);
3661 m_colLabelsExposed
.Empty();
3668 // TODO: remove this when we can...
3669 // There is a bug in wxMotif that gives garbage update
3670 // rectangles if you jump-scroll a long way by clicking the
3671 // scrollbar with middle button. This is a work-around
3673 #if defined(__WXMOTIF__)
3675 m_gridWin
->GetClientSize( &cw
, &ch
);
3676 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3677 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3680 // logical bounds of update region
3683 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3684 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3686 // find the cells within these bounds
3689 for ( col
= 0; col
< m_numCols
; col
++ )
3691 if ( GetColRight(col
) < left
)
3694 if ( GetColLeft(col
) > right
)
3697 m_colLabelsExposed
.Add( col
);
3705 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3707 wxRegionIterator
iter( reg
);
3710 m_cellsExposed
.Empty();
3711 m_rowsExposed
.Empty();
3712 m_colsExposed
.Empty();
3714 int left
, top
, right
, bottom
;
3719 // TODO: remove this when we can...
3720 // There is a bug in wxMotif that gives garbage update
3721 // rectangles if you jump-scroll a long way by clicking the
3722 // scrollbar with middle button. This is a work-around
3724 #if defined(__WXMOTIF__)
3726 m_gridWin
->GetClientSize( &cw
, &ch
);
3727 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3728 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3729 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3730 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3733 // logical bounds of update region
3735 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3736 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3738 // find the cells within these bounds
3741 for ( row
= 0; row
< m_numRows
; row
++ )
3743 if ( GetRowBottom(row
) <= top
)
3746 if ( GetRowTop(row
) > bottom
)
3749 m_rowsExposed
.Add( row
);
3751 for ( col
= 0; col
< m_numCols
; col
++ )
3753 if ( GetColRight(col
) <= left
)
3756 if ( GetColLeft(col
) > right
)
3759 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3760 m_colsExposed
.Add( col
);
3761 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3770 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3773 wxPoint
pos( event
.GetPosition() );
3774 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3776 if ( event
.Dragging() )
3778 m_isDragging
= TRUE
;
3780 if ( event
.LeftIsDown() )
3782 switch( m_cursorMode
)
3784 case WXGRID_CURSOR_RESIZE_ROW
:
3786 int cw
, ch
, left
, dummy
;
3787 m_gridWin
->GetClientSize( &cw
, &ch
);
3788 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3790 wxClientDC
dc( m_gridWin
);
3793 GetRowTop(m_dragRowOrCol
) +
3794 GetRowMinimalHeight(m_dragRowOrCol
) );
3795 dc
.SetLogicalFunction(wxINVERT
);
3796 if ( m_dragLastPos
>= 0 )
3798 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3800 dc
.DrawLine( left
, y
, left
+cw
, y
);
3805 case WXGRID_CURSOR_SELECT_ROW
:
3806 if ( (row
= YToRow( y
)) >= 0 )
3808 m_selection
->SelectRow( row
,
3809 event
.ControlDown(),
3815 // default label to suppress warnings about "enumeration value
3816 // 'xxx' not handled in switch
3824 m_isDragging
= FALSE
;
3827 // ------------ Entering or leaving the window
3829 if ( event
.Entering() || event
.Leaving() )
3831 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3835 // ------------ Left button pressed
3837 else if ( event
.LeftDown() )
3839 // don't send a label click event for a hit on the
3840 // edge of the row label - this is probably the user
3841 // wanting to resize the row
3843 if ( YToEdgeOfRow(y
) < 0 )
3847 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3849 if ( !event
.ShiftDown() && !event
.ControlDown() )
3851 if ( event
.ShiftDown() )
3852 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
3855 GetNumberCols() - 1,
3856 event
.ControlDown(),
3861 m_selection
->SelectRow( row
,
3862 event
.ControlDown(),
3866 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3871 // starting to drag-resize a row
3873 if ( CanDragRowSize() )
3874 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3879 // ------------ Left double click
3881 else if (event
.LeftDClick() )
3883 if ( YToEdgeOfRow(y
) < 0 )
3886 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3891 // ------------ Left button released
3893 else if ( event
.LeftUp() )
3895 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3897 DoEndDragResizeRow();
3899 // Note: we are ending the event *after* doing
3900 // default processing in this case
3902 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3905 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3910 // ------------ Right button down
3912 else if ( event
.RightDown() )
3915 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3917 // no default action at the moment
3922 // ------------ Right double click
3924 else if ( event
.RightDClick() )
3927 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3929 // no default action at the moment
3934 // ------------ No buttons down and mouse moving
3936 else if ( event
.Moving() )
3938 m_dragRowOrCol
= YToEdgeOfRow( y
);
3939 if ( m_dragRowOrCol
>= 0 )
3941 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3943 // don't capture the mouse yet
3944 if ( CanDragRowSize() )
3945 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3948 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3950 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3956 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3959 wxPoint
pos( event
.GetPosition() );
3960 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3962 if ( event
.Dragging() )
3964 m_isDragging
= TRUE
;
3966 if ( event
.LeftIsDown() )
3968 switch( m_cursorMode
)
3970 case WXGRID_CURSOR_RESIZE_COL
:
3972 int cw
, ch
, dummy
, top
;
3973 m_gridWin
->GetClientSize( &cw
, &ch
);
3974 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3976 wxClientDC
dc( m_gridWin
);
3979 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3980 GetColMinimalWidth(m_dragRowOrCol
));
3981 dc
.SetLogicalFunction(wxINVERT
);
3982 if ( m_dragLastPos
>= 0 )
3984 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3986 dc
.DrawLine( x
, top
, x
, top
+ch
);
3991 case WXGRID_CURSOR_SELECT_COL
:
3992 if ( (col
= XToCol( x
)) >= 0 )
3994 m_selection
->SelectCol( col
,
3995 event
.ControlDown(),
4001 // default label to suppress warnings about "enumeration value
4002 // 'xxx' not handled in switch
4010 m_isDragging
= FALSE
;
4013 // ------------ Entering or leaving the window
4015 if ( event
.Entering() || event
.Leaving() )
4017 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4021 // ------------ Left button pressed
4023 else if ( event
.LeftDown() )
4025 // don't send a label click event for a hit on the
4026 // edge of the col label - this is probably the user
4027 // wanting to resize the col
4029 if ( XToEdgeOfCol(x
) < 0 )
4033 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4035 if ( !event
.ShiftDown() && !event
.ControlDown() )
4037 if ( event
.ShiftDown() )
4038 m_selection
->SelectBlock( 0,
4039 m_currentCellCoords
.GetCol(),
4040 GetNumberRows() - 1, col
,
4041 event
.ControlDown(),
4046 m_selection
->SelectCol( col
,
4047 event
.ControlDown(),
4051 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4056 // starting to drag-resize a col
4058 if ( CanDragColSize() )
4059 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4064 // ------------ Left double click
4066 if ( event
.LeftDClick() )
4068 if ( XToEdgeOfCol(x
) < 0 )
4071 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4076 // ------------ Left button released
4078 else if ( event
.LeftUp() )
4080 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4082 DoEndDragResizeCol();
4084 // Note: we are ending the event *after* doing
4085 // default processing in this case
4087 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4090 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4095 // ------------ Right button down
4097 else if ( event
.RightDown() )
4100 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4102 // no default action at the moment
4107 // ------------ Right double click
4109 else if ( event
.RightDClick() )
4112 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4114 // no default action at the moment
4119 // ------------ No buttons down and mouse moving
4121 else if ( event
.Moving() )
4123 m_dragRowOrCol
= XToEdgeOfCol( x
);
4124 if ( m_dragRowOrCol
>= 0 )
4126 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4128 // don't capture the cursor yet
4129 if ( CanDragColSize() )
4130 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4133 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4135 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4141 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4143 if ( event
.LeftDown() )
4145 // indicate corner label by having both row and
4148 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4154 else if ( event
.LeftDClick() )
4156 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4159 else if ( event
.RightDown() )
4161 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4163 // no default action at the moment
4167 else if ( event
.RightDClick() )
4169 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4171 // no default action at the moment
4176 void wxGrid::ChangeCursorMode(CursorMode mode
,
4181 static const wxChar
*cursorModes
[] =
4190 wxLogTrace(_T("grid"),
4191 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4192 win
== m_colLabelWin
? _T("colLabelWin")
4193 : win
? _T("rowLabelWin")
4195 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4196 #endif // __WXDEBUG__
4198 if ( mode
== m_cursorMode
)
4203 // by default use the grid itself
4209 m_winCapture
->ReleaseMouse();
4210 m_winCapture
= (wxWindow
*)NULL
;
4213 m_cursorMode
= mode
;
4215 switch ( m_cursorMode
)
4217 case WXGRID_CURSOR_RESIZE_ROW
:
4218 win
->SetCursor( m_rowResizeCursor
);
4221 case WXGRID_CURSOR_RESIZE_COL
:
4222 win
->SetCursor( m_colResizeCursor
);
4226 win
->SetCursor( *wxSTANDARD_CURSOR
);
4229 // we need to capture mouse when resizing
4230 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4231 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4233 if ( captureMouse
&& resize
)
4235 win
->CaptureMouse();
4240 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4243 wxPoint
pos( event
.GetPosition() );
4244 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4246 wxGridCellCoords coords
;
4247 XYToCell( x
, y
, coords
);
4249 if ( event
.Dragging() )
4251 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4253 // Don't start doing anything until the mouse has been drug at
4254 // least 3 pixels in any direction...
4257 if (m_startDragPos
== wxDefaultPosition
)
4259 m_startDragPos
= pos
;
4262 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4266 m_isDragging
= TRUE
;
4267 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4269 // Hide the edit control, so it
4270 // won't interfer with drag-shrinking.
4271 if ( IsCellEditControlEnabled() )
4273 HideCellEditControl();
4274 SaveEditControlValue();
4277 // Have we captured the mouse yet?
4280 m_winCapture
= m_gridWin
;
4281 m_winCapture
->CaptureMouse();
4284 if ( coords
!= wxGridNoCellCoords
)
4286 if ( event
.ControlDown() )
4288 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4289 m_selectingKeyboard
= coords
;
4290 SelectBlock ( m_selectingKeyboard
, coords
);
4294 if ( !IsSelection() )
4296 SelectBlock( coords
, coords
);
4300 SelectBlock( m_currentCellCoords
, coords
);
4304 if (! IsVisible(coords
))
4306 MakeCellVisible(coords
);
4307 // TODO: need to introduce a delay or something here. The
4308 // scrolling is way to fast, at least on MSW - also on GTK.
4312 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4314 int cw
, ch
, left
, dummy
;
4315 m_gridWin
->GetClientSize( &cw
, &ch
);
4316 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4318 wxClientDC
dc( m_gridWin
);
4320 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4321 GetRowMinimalHeight(m_dragRowOrCol
) );
4322 dc
.SetLogicalFunction(wxINVERT
);
4323 if ( m_dragLastPos
>= 0 )
4325 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4327 dc
.DrawLine( left
, y
, left
+cw
, y
);
4330 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4332 int cw
, ch
, dummy
, top
;
4333 m_gridWin
->GetClientSize( &cw
, &ch
);
4334 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4336 wxClientDC
dc( m_gridWin
);
4338 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4339 GetColMinimalWidth(m_dragRowOrCol
) );
4340 dc
.SetLogicalFunction(wxINVERT
);
4341 if ( m_dragLastPos
>= 0 )
4343 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4345 dc
.DrawLine( x
, top
, x
, top
+ch
);
4352 m_isDragging
= FALSE
;
4353 m_startDragPos
= wxDefaultPosition
;
4355 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4356 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4359 if ( event
.Entering() || event
.Leaving() )
4361 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4362 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4367 // ------------ Left button pressed
4369 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4371 if ( !event
.ShiftDown() && !event
.ControlDown() )
4373 if ( event
.ShiftDown() )
4375 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4376 m_currentCellCoords
.GetCol(),
4379 event
.ControlDown(),
4384 else if ( XToEdgeOfCol(x
) < 0 &&
4385 YToEdgeOfRow(y
) < 0 )
4387 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4392 DisableCellEditControl();
4393 MakeCellVisible( coords
);
4395 // if this is the second click on this cell then start
4397 if ( m_waitForSlowClick
&&
4398 (coords
== m_currentCellCoords
) &&
4399 CanEnableCellControl())
4401 EnableCellEditControl();
4403 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4404 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4407 editor
->StartingClick();
4411 m_waitForSlowClick
= FALSE
;
4415 if ( event
.ControlDown() )
4417 m_selection
->ToggleCellSelection( coords
.GetRow(),
4419 event
.ControlDown(),
4423 m_selectingTopLeft
= wxGridNoCellCoords
;
4424 m_selectingBottomRight
= wxGridNoCellCoords
;
4425 m_selectingKeyboard
= coords
;
4428 SetCurrentCell( coords
);
4429 m_waitForSlowClick
= TRUE
;
4436 // ------------ Left double click
4438 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4440 DisableCellEditControl();
4442 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4444 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4452 // ------------ Left button released
4454 else if ( event
.LeftUp() )
4456 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4458 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4459 m_selectingBottomRight
!= wxGridNoCellCoords
)
4463 m_winCapture
->ReleaseMouse();
4464 m_winCapture
= NULL
;
4466 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4467 m_selectingTopLeft
.GetCol(),
4468 m_selectingBottomRight
.GetRow(),
4469 m_selectingBottomRight
.GetCol(),
4470 event
.ControlDown(),
4474 m_selectingTopLeft
= wxGridNoCellCoords
;
4475 m_selectingBottomRight
= wxGridNoCellCoords
;
4478 // Show the edit control, if it has been hidden for
4480 ShowCellEditControl();
4482 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4484 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4485 DoEndDragResizeRow();
4487 // Note: we are ending the event *after* doing
4488 // default processing in this case
4490 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4492 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4494 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4495 DoEndDragResizeCol();
4497 // Note: we are ending the event *after* doing
4498 // default processing in this case
4500 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4507 // ------------ Right button down
4509 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4511 DisableCellEditControl();
4512 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4517 // no default action at the moment
4522 // ------------ Right double click
4524 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4526 DisableCellEditControl();
4527 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4532 // no default action at the moment
4536 // ------------ Moving and no button action
4538 else if ( event
.Moving() && !event
.IsButton() )
4540 int dragRow
= YToEdgeOfRow( y
);
4541 int dragCol
= XToEdgeOfCol( x
);
4543 // Dragging on the corner of a cell to resize in both
4544 // directions is not implemented yet...
4546 if ( dragRow
>= 0 && dragCol
>= 0 )
4548 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4554 m_dragRowOrCol
= dragRow
;
4556 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4558 if ( CanDragRowSize() && CanDragGridSize() )
4559 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4564 m_dragRowOrCol
= dragCol
;
4572 m_dragRowOrCol
= dragCol
;
4574 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4576 if ( CanDragColSize() && CanDragGridSize() )
4577 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4583 // Neither on a row or col edge
4585 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4587 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4593 void wxGrid::DoEndDragResizeRow()
4595 if ( m_dragLastPos
>= 0 )
4597 // erase the last line and resize the row
4599 int cw
, ch
, left
, dummy
;
4600 m_gridWin
->GetClientSize( &cw
, &ch
);
4601 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4603 wxClientDC
dc( m_gridWin
);
4605 dc
.SetLogicalFunction( wxINVERT
);
4606 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4607 HideCellEditControl();
4608 SaveEditControlValue();
4610 int rowTop
= GetRowTop(m_dragRowOrCol
);
4611 SetRowSize( m_dragRowOrCol
,
4612 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4614 if ( !GetBatchCount() )
4616 // Only needed to get the correct rect.y:
4617 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4619 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4620 rect
.width
= m_rowLabelWidth
;
4621 rect
.height
= ch
- rect
.y
;
4622 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4624 m_gridWin
->Refresh( FALSE
, &rect
);
4627 ShowCellEditControl();
4632 void wxGrid::DoEndDragResizeCol()
4634 if ( m_dragLastPos
>= 0 )
4636 // erase the last line and resize the col
4638 int cw
, ch
, dummy
, top
;
4639 m_gridWin
->GetClientSize( &cw
, &ch
);
4640 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4642 wxClientDC
dc( m_gridWin
);
4644 dc
.SetLogicalFunction( wxINVERT
);
4645 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4646 HideCellEditControl();
4647 SaveEditControlValue();
4649 int colLeft
= GetColLeft(m_dragRowOrCol
);
4650 SetColSize( m_dragRowOrCol
,
4651 wxMax( m_dragLastPos
- colLeft
,
4652 GetColMinimalWidth(m_dragRowOrCol
) ) );
4654 if ( !GetBatchCount() )
4656 // Only needed to get the correct rect.x:
4657 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4659 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4660 rect
.width
= cw
- rect
.x
;
4661 rect
.height
= m_colLabelHeight
;
4662 m_colLabelWin
->Refresh( TRUE
, &rect
);
4664 m_gridWin
->Refresh( FALSE
, &rect
);
4667 ShowCellEditControl();
4674 // ------ interaction with data model
4676 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4678 switch ( msg
.GetId() )
4680 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4681 return GetModelValues();
4683 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4684 return SetModelValues();
4686 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4687 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4688 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4689 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4690 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4691 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4692 return Redimension( msg
);
4701 // The behaviour of this function depends on the grid table class
4702 // Clear() function. For the default wxGridStringTable class the
4703 // behavious is to replace all cell contents with wxEmptyString but
4704 // not to change the number of rows or cols.
4706 void wxGrid::ClearGrid()
4710 if (IsCellEditControlEnabled())
4711 DisableCellEditControl();
4714 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4719 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4721 // TODO: something with updateLabels flag
4725 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4731 if (IsCellEditControlEnabled())
4732 DisableCellEditControl();
4734 bool ok
= m_table
->InsertRows( pos
, numRows
);
4736 // the table will have sent the results of the insert row
4737 // operation to this view object as a grid table message
4741 if ( m_numCols
== 0 )
4743 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4745 // TODO: perhaps instead of appending the default number of cols
4746 // we should remember what the last non-zero number of cols was ?
4750 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4752 // if we have just inserted cols into an empty grid the current
4753 // cell will be undefined...
4755 SetCurrentCell( 0, 0 );
4758 m_selection
->UpdateRows( pos
, numRows
);
4759 if ( !GetBatchCount() ) Refresh();
4771 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4773 // TODO: something with updateLabels flag
4777 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4781 if ( m_table
&& m_table
->AppendRows( numRows
) )
4783 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4785 // if we have just inserted cols into an empty grid the current
4786 // cell will be undefined...
4788 SetCurrentCell( 0, 0 );
4791 // the table will have sent the results of the append row
4792 // operation to this view object as a grid table message
4794 if ( !GetBatchCount() ) Refresh();
4804 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4806 // TODO: something with updateLabels flag
4810 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4816 if (IsCellEditControlEnabled())
4817 DisableCellEditControl();
4819 if (m_table
->DeleteRows( pos
, numRows
))
4822 // the table will have sent the results of the delete row
4823 // operation to this view object as a grid table message
4825 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4826 if ( !GetBatchCount() ) Refresh();
4834 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4836 // TODO: something with updateLabels flag
4840 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4846 if (IsCellEditControlEnabled())
4847 DisableCellEditControl();
4849 bool ok
= m_table
->InsertCols( pos
, numCols
);
4851 // the table will have sent the results of the insert col
4852 // operation to this view object as a grid table message
4856 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4858 // if we have just inserted cols into an empty grid the current
4859 // cell will be undefined...
4861 SetCurrentCell( 0, 0 );
4864 m_selection
->UpdateCols( pos
, numCols
);
4865 if ( !GetBatchCount() ) Refresh();
4877 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4879 // TODO: something with updateLabels flag
4883 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4887 if ( m_table
&& m_table
->AppendCols( numCols
) )
4889 // the table will have sent the results of the append col
4890 // operation to this view object as a grid table message
4892 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4894 // if we have just inserted cols into an empty grid the current
4895 // cell will be undefined...
4897 SetCurrentCell( 0, 0 );
4900 if ( !GetBatchCount() ) Refresh();
4910 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4912 // TODO: something with updateLabels flag
4916 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4922 if (IsCellEditControlEnabled())
4923 DisableCellEditControl();
4925 if ( m_table
->DeleteCols( pos
, numCols
) )
4927 // the table will have sent the results of the delete col
4928 // operation to this view object as a grid table message
4930 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4931 if ( !GetBatchCount() ) Refresh();
4941 // ----- event handlers
4944 // Generate a grid event based on a mouse event and
4945 // return the result of ProcessEvent()
4947 bool wxGrid::SendEvent( const wxEventType type
,
4949 wxMouseEvent
& mouseEv
)
4951 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4953 int rowOrCol
= (row
== -1 ? col
: row
);
4955 wxGridSizeEvent
gridEvt( GetId(),
4959 mouseEv
.GetX(), mouseEv
.GetY(),
4960 mouseEv
.ControlDown(),
4961 mouseEv
.ShiftDown(),
4963 mouseEv
.MetaDown() );
4965 return GetEventHandler()->ProcessEvent(gridEvt
);
4967 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4969 // Right now, it should _never_ end up here!
4970 wxGridRangeSelectEvent
gridEvt( GetId(),
4974 m_selectingBottomRight
,
4976 mouseEv
.ControlDown(),
4977 mouseEv
.ShiftDown(),
4979 mouseEv
.MetaDown() );
4981 return GetEventHandler()->ProcessEvent(gridEvt
);
4985 wxGridEvent
gridEvt( GetId(),
4989 mouseEv
.GetX(), mouseEv
.GetY(),
4991 mouseEv
.ControlDown(),
4992 mouseEv
.ShiftDown(),
4994 mouseEv
.MetaDown() );
4996 return GetEventHandler()->ProcessEvent(gridEvt
);
5001 // Generate a grid event of specified type and return the result
5002 // of ProcessEvent().
5004 bool wxGrid::SendEvent( const wxEventType type
,
5007 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5009 int rowOrCol
= (row
== -1 ? col
: row
);
5011 wxGridSizeEvent
gridEvt( GetId(),
5016 return GetEventHandler()->ProcessEvent(gridEvt
);
5020 wxGridEvent
gridEvt( GetId(),
5025 return GetEventHandler()->ProcessEvent(gridEvt
);
5030 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5032 wxPaintDC
dc( this );
5036 // This is just here to make sure that CalcDimensions gets called when
5037 // the grid view is resized... then the size event is skipped to allow
5038 // the box sizers to handle everything
5040 void wxGrid::OnSize( wxSizeEvent
& event
)
5047 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5049 if ( m_inOnKeyDown
)
5051 // shouldn't be here - we are going round in circles...
5053 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5056 m_inOnKeyDown
= TRUE
;
5058 // propagate the event up and see if it gets processed
5060 wxWindow
*parent
= GetParent();
5061 wxKeyEvent
keyEvt( event
);
5062 keyEvt
.SetEventObject( parent
);
5064 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5067 // try local handlers
5069 if ( !event
.ShiftDown() &&
5070 m_selectingKeyboard
!= wxGridNoCellCoords
)
5072 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5073 m_selectingBottomRight
!= wxGridNoCellCoords
)
5074 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5075 m_selectingTopLeft
.GetCol(),
5076 m_selectingBottomRight
.GetRow(),
5077 m_selectingBottomRight
.GetCol(),
5078 event
.ControlDown(),
5082 m_selectingTopLeft
= wxGridNoCellCoords
;
5083 m_selectingBottomRight
= wxGridNoCellCoords
;
5084 m_selectingKeyboard
= wxGridNoCellCoords
;
5087 switch ( event
.KeyCode() )
5090 if ( event
.ControlDown() )
5092 MoveCursorUpBlock( event
.ShiftDown() );
5096 MoveCursorUp( event
.ShiftDown() );
5101 if ( event
.ControlDown() )
5103 MoveCursorDownBlock( event
.ShiftDown() );
5107 MoveCursorDown( event
.ShiftDown() );
5112 if ( event
.ControlDown() )
5114 MoveCursorLeftBlock( event
.ShiftDown() );
5118 MoveCursorLeft( event
.ShiftDown() );
5123 if ( event
.ControlDown() )
5125 MoveCursorRightBlock( event
.ShiftDown() );
5129 MoveCursorRight( event
.ShiftDown() );
5134 if ( event
.ControlDown() )
5136 event
.Skip(); // to let the edit control have the return
5140 MoveCursorDown( event
.ShiftDown() );
5149 if (event
.ShiftDown())
5150 MoveCursorLeft( FALSE
);
5152 MoveCursorRight( FALSE
);
5156 if ( event
.ControlDown() )
5158 MakeCellVisible( 0, 0 );
5159 SetCurrentCell( 0, 0 );
5168 if ( event
.ControlDown() )
5170 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5171 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5188 if ( event
.ControlDown() )
5190 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5191 m_currentCellCoords
.GetCol(),
5192 event
.ControlDown(),
5198 if ( !IsEditable() )
5200 MoveCursorRight( FALSE
);
5203 // Otherwise fall through to default
5206 // alphanumeric keys or F2 (special key just for this) enable
5207 // the cell edit control
5208 // On just Shift/Control I get values for event.KeyCode()
5209 // that are outside the range where isalnum's behaviour is
5210 // well defined, so do an additional sanity check.
5211 if ( !(event
.AltDown() ||
5213 event
.ControlDown()) &&
5214 ((isalnum(event
.KeyCode()) &&
5215 (event
.KeyCode() < 256 && event
.KeyCode() >= 0)) ||
5216 event
.KeyCode() == WXK_F2
) &&
5217 !IsCellEditControlEnabled() &&
5218 CanEnableCellControl() )
5220 EnableCellEditControl();
5221 int row
= m_currentCellCoords
.GetRow();
5222 int col
= m_currentCellCoords
.GetCol();
5223 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5224 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5225 editor
->StartingKey(event
);
5231 // let others process char events with modifiers or all
5232 // char events for readonly cells
5239 m_inOnKeyDown
= FALSE
;
5242 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5246 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5248 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5250 HideCellEditControl();
5251 DisableCellEditControl();
5253 // Clear the old current cell highlight
5254 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5256 // Otherwise refresh redraws the highlight!
5257 m_currentCellCoords
= coords
;
5259 m_gridWin
->Refresh( FALSE
, &r
);
5262 m_currentCellCoords
= coords
;
5264 wxClientDC
dc(m_gridWin
);
5267 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5268 DrawCellHighlight(dc
, attr
);
5272 // SN: For my extended selection code, automatic
5273 // deselection is definitely not a good idea.
5274 if ( IsSelection() )
5276 wxRect
r( SelectionToDeviceRect() );
5278 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
5285 // ------ functions to get/send data (see also public functions)
5288 bool wxGrid::GetModelValues()
5292 // all we need to do is repaint the grid
5294 m_gridWin
->Refresh();
5302 bool wxGrid::SetModelValues()
5308 for ( row
= 0; row
< m_numRows
; row
++ )
5310 for ( col
= 0; col
< m_numCols
; col
++ )
5312 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5324 // Note - this function only draws cells that are in the list of
5325 // exposed cells (usually set from the update region by
5326 // CalcExposedCells)
5328 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5330 if ( !m_numRows
|| !m_numCols
) return;
5333 size_t numCells
= m_cellsExposed
.GetCount();
5335 for ( i
= 0; i
< numCells
; i
++ )
5337 DrawCell( dc
, m_cellsExposed
[i
] );
5342 void wxGrid::DrawGridSpace( wxDC
& dc
)
5344 if ( m_numRows
&& m_numCols
)
5347 m_gridWin
->GetClientSize( &cw
, &ch
);
5350 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5352 if ( right
> GetColRight(m_numCols
-1) ||
5353 bottom
> GetRowBottom(m_numRows
-1) )
5356 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5358 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5359 dc
.SetPen( *wxTRANSPARENT_PEN
);
5361 if ( right
> GetColRight(m_numCols
-1) )
5362 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
5363 right
- GetColRight(m_numCols
-1), ch
);
5365 if ( bottom
> GetRowBottom(m_numRows
-1) )
5366 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
5367 cw
, bottom
- GetRowBottom(m_numRows
-1) );
5373 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5375 int row
= coords
.GetRow();
5376 int col
= coords
.GetCol();
5378 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5381 // we draw the cell border ourselves
5382 #if !WXGRID_DRAW_LINES
5383 if ( m_gridLinesEnabled
)
5384 DrawCellBorder( dc
, coords
);
5387 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5389 bool isCurrent
= coords
== m_currentCellCoords
;
5392 rect
.x
= GetColLeft(col
);
5393 rect
.y
= GetRowTop(row
);
5394 rect
.width
= GetColWidth(col
) - 1;
5395 rect
.height
= GetRowHeight(row
) - 1;
5397 // if the editor is shown, we should use it and not the renderer
5398 if ( isCurrent
&& IsCellEditControlEnabled() )
5400 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5401 editor
->PaintBackground(rect
, attr
);
5406 // but all the rest is drawn by the cell renderer and hence may be
5408 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5409 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5416 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5418 int row
= m_currentCellCoords
.GetRow();
5419 int col
= m_currentCellCoords
.GetCol();
5421 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5425 rect
.x
= GetColLeft(col
);
5426 rect
.y
= GetRowTop(row
);
5427 rect
.width
= GetColWidth(col
) - 1;
5428 rect
.height
= GetRowHeight(row
) - 1;
5430 // hmmm... what could we do here to show that the cell is disabled?
5431 // for now, I just draw a thinner border than for the other ones, but
5432 // it doesn't look really good
5433 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5434 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5436 dc
.DrawRectangle(rect
);
5439 // VZ: my experiments with 3d borders...
5441 // how to properly set colours for arbitrary bg?
5442 wxCoord x1
= rect
.x
,
5444 x2
= rect
.x
+ rect
.width
-1,
5445 y2
= rect
.y
+ rect
.height
-1;
5447 dc
.SetPen(*wxWHITE_PEN
);
5448 dc
.DrawLine(x1
, y1
, x2
, y1
);
5449 dc
.DrawLine(x1
, y1
, x1
, y2
);
5451 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5452 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5454 dc
.SetPen(*wxBLACK_PEN
);
5455 dc
.DrawLine(x1
, y2
, x2
, y2
);
5456 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5461 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5463 int row
= coords
.GetRow();
5464 int col
= coords
.GetCol();
5465 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5468 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5470 // right hand border
5472 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5473 GetColRight(col
), GetRowBottom(row
) );
5477 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5478 GetColRight(col
), GetRowBottom(row
) );
5481 void wxGrid::DrawHighlight(wxDC
& dc
)
5483 // This if block was previously in wxGrid::OnPaint but that doesn't
5484 // seem to get called under wxGTK - MB
5486 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5487 m_numRows
&& m_numCols
)
5489 m_currentCellCoords
.Set(0, 0);
5492 if ( IsCellEditControlEnabled() )
5494 // don't show highlight when the edit control is shown
5498 // if the active cell was repainted, repaint its highlight too because it
5499 // might have been damaged by the grid lines
5500 size_t count
= m_cellsExposed
.GetCount();
5501 for ( size_t n
= 0; n
< count
; n
++ )
5503 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5505 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5506 DrawCellHighlight(dc
, attr
);
5514 // TODO: remove this ???
5515 // This is used to redraw all grid lines e.g. when the grid line colour
5518 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5520 if ( !m_gridLinesEnabled
||
5522 !m_numCols
) return;
5524 int top
, bottom
, left
, right
;
5530 m_gridWin
->GetClientSize(&cw
, &ch
);
5532 // virtual coords of visible area
5534 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5535 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5540 reg
.GetBox(x
, y
, w
, h
);
5541 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5542 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5546 m_gridWin
->GetClientSize(&cw
, &ch
);
5547 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5548 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5551 // avoid drawing grid lines past the last row and col
5553 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5554 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5556 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5558 // horizontal grid lines
5561 for ( i
= 0; i
< m_numRows
; i
++ )
5563 int bot
= GetRowBottom(i
) - 1;
5572 dc
.DrawLine( left
, bot
, right
, bot
);
5577 // vertical grid lines
5579 for ( i
= 0; i
< m_numCols
; i
++ )
5581 int colRight
= GetColRight(i
) - 1;
5582 if ( colRight
> right
)
5587 if ( colRight
>= left
)
5589 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5595 void wxGrid::DrawRowLabels( wxDC
& dc
)
5597 if ( !m_numRows
|| !m_numCols
) return;
5600 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5602 for ( i
= 0; i
< numLabels
; i
++ )
5604 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5609 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5611 if ( GetRowHeight(row
) <= 0 )
5614 int rowTop
= GetRowTop(row
),
5615 rowBottom
= GetRowBottom(row
) - 1;
5617 dc
.SetPen( *wxBLACK_PEN
);
5618 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5619 m_rowLabelWidth
-1, rowBottom
);
5621 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5623 dc
.SetPen( *wxWHITE_PEN
);
5624 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5625 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5627 dc
.SetBackgroundMode( wxTRANSPARENT
);
5628 dc
.SetTextForeground( GetLabelTextColour() );
5629 dc
.SetFont( GetLabelFont() );
5632 GetRowLabelAlignment( &hAlign
, &vAlign
);
5636 rect
.SetY( GetRowTop(row
) + 2 );
5637 rect
.SetWidth( m_rowLabelWidth
- 4 );
5638 rect
.SetHeight( GetRowHeight(row
) - 4 );
5639 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5643 void wxGrid::DrawColLabels( wxDC
& dc
)
5645 if ( !m_numRows
|| !m_numCols
) return;
5648 size_t numLabels
= m_colLabelsExposed
.GetCount();
5650 for ( i
= 0; i
< numLabels
; i
++ )
5652 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5657 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5659 if ( GetColWidth(col
) <= 0 )
5662 int colLeft
= GetColLeft(col
),
5663 colRight
= GetColRight(col
) - 1;
5665 dc
.SetPen( *wxBLACK_PEN
);
5666 dc
.DrawLine( colRight
, 0,
5667 colRight
, m_colLabelHeight
-1 );
5669 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5670 colRight
, m_colLabelHeight
-1 );
5672 dc
.SetPen( *wxWHITE_PEN
);
5673 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5674 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5676 dc
.SetBackgroundMode( wxTRANSPARENT
);
5677 dc
.SetTextForeground( GetLabelTextColour() );
5678 dc
.SetFont( GetLabelFont() );
5680 dc
.SetBackgroundMode( wxTRANSPARENT
);
5681 dc
.SetTextForeground( GetLabelTextColour() );
5682 dc
.SetFont( GetLabelFont() );
5685 GetColLabelAlignment( &hAlign
, &vAlign
);
5688 rect
.SetX( colLeft
+ 2 );
5690 rect
.SetWidth( GetColWidth(col
) - 4 );
5691 rect
.SetHeight( m_colLabelHeight
- 4 );
5692 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5696 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5697 const wxString
& value
,
5702 long textWidth
, textHeight
;
5703 long lineWidth
, lineHeight
;
5704 wxArrayString lines
;
5706 dc
.SetClippingRegion( rect
);
5707 StringToLines( value
, lines
);
5708 if ( lines
.GetCount() )
5710 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5711 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5714 switch ( horizAlign
)
5717 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5721 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5730 switch ( vertAlign
)
5733 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5737 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5746 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5748 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5753 dc
.DestroyClippingRegion();
5757 // Split multi line text up into an array of strings. Any existing
5758 // contents of the string array are preserved.
5760 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5764 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5765 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5767 while ( startPos
< (int)tVal
.Length() )
5769 pos
= tVal
.Mid(startPos
).Find( eol
);
5774 else if ( pos
== 0 )
5776 lines
.Add( wxEmptyString
);
5780 lines
.Add( value
.Mid(startPos
, pos
) );
5784 if ( startPos
< (int)value
.Length() )
5786 lines
.Add( value
.Mid( startPos
) );
5791 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5792 wxArrayString
& lines
,
5793 long *width
, long *height
)
5800 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5802 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5803 w
= wxMax( w
, lineW
);
5813 // ------ Edit control functions
5817 void wxGrid::EnableEditing( bool edit
)
5819 // TODO: improve this ?
5821 if ( edit
!= m_editable
)
5825 // FIXME IMHO this won't disable the edit control if edit == FALSE
5826 // because of the check in the beginning of
5827 // EnableCellEditControl() just below (VZ)
5828 EnableCellEditControl(m_editable
);
5833 void wxGrid::EnableCellEditControl( bool enable
)
5838 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5839 SetCurrentCell( 0, 0 );
5841 if ( enable
!= m_cellEditCtrlEnabled
)
5843 // TODO allow the app to Veto() this event?
5844 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5848 // this should be checked by the caller!
5849 wxASSERT_MSG( CanEnableCellControl(),
5850 _T("can't enable editing for this cell!") );
5852 // do it before ShowCellEditControl()
5853 m_cellEditCtrlEnabled
= enable
;
5855 ShowCellEditControl();
5859 HideCellEditControl();
5860 SaveEditControlValue();
5862 // do it after HideCellEditControl()
5863 m_cellEditCtrlEnabled
= enable
;
5868 bool wxGrid::IsCurrentCellReadOnly() const
5871 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5872 bool readonly
= attr
->IsReadOnly();
5878 bool wxGrid::CanEnableCellControl() const
5880 return m_editable
&& !IsCurrentCellReadOnly();
5883 bool wxGrid::IsCellEditControlEnabled() const
5885 // the cell edit control might be disable for all cells or just for the
5886 // current one if it's read only
5887 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5890 void wxGrid::ShowCellEditControl()
5892 if ( IsCellEditControlEnabled() )
5894 if ( !IsVisible( m_currentCellCoords
) )
5900 wxRect rect
= CellToRect( m_currentCellCoords
);
5901 int row
= m_currentCellCoords
.GetRow();
5902 int col
= m_currentCellCoords
.GetCol();
5904 // convert to scrolled coords
5906 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5908 // done in PaintBackground()
5910 // erase the highlight and the cell contents because the editor
5911 // might not cover the entire cell
5912 wxClientDC
dc( m_gridWin
);
5914 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5915 dc
.SetPen(*wxTRANSPARENT_PEN
);
5916 dc
.DrawRectangle(rect
);
5919 // cell is shifted by one pixel
5923 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5924 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5925 if ( !editor
->IsCreated() )
5927 editor
->Create(m_gridWin
, -1,
5928 new wxGridCellEditorEvtHandler(this, editor
));
5931 editor
->Show( TRUE
, attr
);
5933 editor
->SetSize( rect
);
5935 editor
->BeginEdit(row
, col
, this);
5944 void wxGrid::HideCellEditControl()
5946 if ( IsCellEditControlEnabled() )
5948 int row
= m_currentCellCoords
.GetRow();
5949 int col
= m_currentCellCoords
.GetCol();
5951 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5952 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5953 editor
->Show( FALSE
);
5956 m_gridWin
->SetFocus();
5961 void wxGrid::SaveEditControlValue()
5963 if ( IsCellEditControlEnabled() )
5965 int row
= m_currentCellCoords
.GetRow();
5966 int col
= m_currentCellCoords
.GetCol();
5968 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5969 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5970 bool changed
= editor
->EndEdit(row
, col
, this);
5977 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5978 m_currentCellCoords
.GetRow(),
5979 m_currentCellCoords
.GetCol() );
5986 // ------ Grid location functions
5987 // Note that all of these functions work with the logical coordinates of
5988 // grid cells and labels so you will need to convert from device
5989 // coordinates for mouse events etc.
5992 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5994 int row
= YToRow(y
);
5995 int col
= XToCol(x
);
5997 if ( row
== -1 || col
== -1 )
5999 coords
= wxGridNoCellCoords
;
6003 coords
.Set( row
, col
);
6008 int wxGrid::YToRow( int y
)
6012 for ( i
= 0; i
< m_numRows
; i
++ )
6014 if ( y
< GetRowBottom(i
) )
6022 int wxGrid::XToCol( int x
)
6026 for ( i
= 0; i
< m_numCols
; i
++ )
6028 if ( x
< GetColRight(i
) )
6036 // return the row number that that the y coord is near the edge of, or
6037 // -1 if not near an edge
6039 int wxGrid::YToEdgeOfRow( int y
)
6043 for ( i
= 0; i
< m_numRows
; i
++ )
6045 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6047 d
= abs( y
- GetRowBottom(i
) );
6048 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6057 // return the col number that that the x coord is near the edge of, or
6058 // -1 if not near an edge
6060 int wxGrid::XToEdgeOfCol( int x
)
6064 for ( i
= 0; i
< m_numCols
; i
++ )
6066 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6068 d
= abs( x
- GetColRight(i
) );
6069 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6078 wxRect
wxGrid::CellToRect( int row
, int col
)
6080 wxRect
rect( -1, -1, -1, -1 );
6082 if ( row
>= 0 && row
< m_numRows
&&
6083 col
>= 0 && col
< m_numCols
)
6085 rect
.x
= GetColLeft(col
);
6086 rect
.y
= GetRowTop(row
);
6087 rect
.width
= GetColWidth(col
);
6088 rect
.height
= GetRowHeight(row
);
6095 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6097 // get the cell rectangle in logical coords
6099 wxRect
r( CellToRect( row
, col
) );
6101 // convert to device coords
6103 int left
, top
, right
, bottom
;
6104 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6105 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6107 // check against the client area of the grid window
6110 m_gridWin
->GetClientSize( &cw
, &ch
);
6112 if ( wholeCellVisible
)
6114 // is the cell wholly visible ?
6116 return ( left
>= 0 && right
<= cw
&&
6117 top
>= 0 && bottom
<= ch
);
6121 // is the cell partly visible ?
6123 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6124 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6129 // make the specified cell location visible by doing a minimal amount
6132 void wxGrid::MakeCellVisible( int row
, int col
)
6135 int xpos
= -1, ypos
= -1;
6137 if ( row
>= 0 && row
< m_numRows
&&
6138 col
>= 0 && col
< m_numCols
)
6140 // get the cell rectangle in logical coords
6142 wxRect
r( CellToRect( row
, col
) );
6144 // convert to device coords
6146 int left
, top
, right
, bottom
;
6147 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6148 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6151 m_gridWin
->GetClientSize( &cw
, &ch
);
6157 else if ( bottom
> ch
)
6159 int h
= r
.GetHeight();
6161 for ( i
= row
-1; i
>= 0; i
-- )
6163 int rowHeight
= GetRowHeight(i
);
6164 if ( h
+ rowHeight
> ch
)
6171 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6172 // have rounding errors (this is important, because if we do, we
6173 // might not scroll at all and some cells won't be redrawn)
6174 ypos
+= GRID_SCROLL_LINE
/ 2;
6181 else if ( right
> cw
)
6183 int w
= r
.GetWidth();
6185 for ( i
= col
-1; i
>= 0; i
-- )
6187 int colWidth
= GetColWidth(i
);
6188 if ( w
+ colWidth
> cw
)
6195 // see comment for ypos above
6196 xpos
+= GRID_SCROLL_LINE
/ 2;
6199 if ( xpos
!= -1 || ypos
!= -1 )
6201 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6202 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6203 Scroll( xpos
, ypos
);
6211 // ------ Grid cursor movement functions
6214 bool wxGrid::MoveCursorUp( bool expandSelection
)
6216 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6217 m_currentCellCoords
.GetRow() > 0 )
6219 if ( expandSelection
)
6221 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6222 m_selectingKeyboard
= m_currentCellCoords
;
6223 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6224 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6225 m_selectingKeyboard
.GetCol() );
6226 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6231 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6232 m_currentCellCoords
.GetCol() );
6233 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6234 m_currentCellCoords
.GetCol() );
6243 bool wxGrid::MoveCursorDown( bool expandSelection
)
6245 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6246 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6248 if ( expandSelection
)
6250 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6251 m_selectingKeyboard
= m_currentCellCoords
;
6252 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6253 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6254 m_selectingKeyboard
.GetCol() );
6255 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6260 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6261 m_currentCellCoords
.GetCol() );
6262 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6263 m_currentCellCoords
.GetCol() );
6272 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6274 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6275 m_currentCellCoords
.GetCol() > 0 )
6277 if ( expandSelection
)
6279 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6280 m_selectingKeyboard
= m_currentCellCoords
;
6281 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6282 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6283 m_selectingKeyboard
.GetCol() );
6284 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6289 MakeCellVisible( m_currentCellCoords
.GetRow(),
6290 m_currentCellCoords
.GetCol() - 1 );
6291 SetCurrentCell( m_currentCellCoords
.GetRow(),
6292 m_currentCellCoords
.GetCol() - 1 );
6301 bool wxGrid::MoveCursorRight( bool expandSelection
)
6303 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6304 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6306 if ( expandSelection
)
6308 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6309 m_selectingKeyboard
= m_currentCellCoords
;
6310 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6311 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6312 m_selectingKeyboard
.GetCol() );
6313 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6318 MakeCellVisible( m_currentCellCoords
.GetRow(),
6319 m_currentCellCoords
.GetCol() + 1 );
6320 SetCurrentCell( m_currentCellCoords
.GetRow(),
6321 m_currentCellCoords
.GetCol() + 1 );
6330 bool wxGrid::MovePageUp()
6332 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6334 int row
= m_currentCellCoords
.GetRow();
6338 m_gridWin
->GetClientSize( &cw
, &ch
);
6340 int y
= GetRowTop(row
);
6341 int newRow
= YToRow( y
- ch
+ 1 );
6346 else if ( newRow
== row
)
6351 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6352 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6360 bool wxGrid::MovePageDown()
6362 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6364 int row
= m_currentCellCoords
.GetRow();
6365 if ( row
< m_numRows
)
6368 m_gridWin
->GetClientSize( &cw
, &ch
);
6370 int y
= GetRowTop(row
);
6371 int newRow
= YToRow( y
+ ch
);
6374 newRow
= m_numRows
- 1;
6376 else if ( newRow
== row
)
6381 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6382 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6390 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
6393 m_currentCellCoords
!= wxGridNoCellCoords
&&
6394 m_currentCellCoords
.GetRow() > 0 )
6396 int row
= m_currentCellCoords
.GetRow();
6397 int col
= m_currentCellCoords
.GetCol();
6399 if ( m_table
->IsEmptyCell(row
, col
) )
6401 // starting in an empty cell: find the next block of
6407 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6410 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6412 // starting at the top of a block: find the next block
6418 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6423 // starting within a block: find the top of the block
6428 if ( m_table
->IsEmptyCell(row
, col
) )
6436 MakeCellVisible( row
, col
);
6437 if ( expandSelection
)
6439 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6440 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6445 SetCurrentCell( row
, col
);
6453 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6456 m_currentCellCoords
!= wxGridNoCellCoords
&&
6457 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6459 int row
= m_currentCellCoords
.GetRow();
6460 int col
= m_currentCellCoords
.GetCol();
6462 if ( m_table
->IsEmptyCell(row
, col
) )
6464 // starting in an empty cell: find the next block of
6467 while ( row
< m_numRows
-1 )
6470 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6473 else if ( m_table
->IsEmptyCell(row
+1, col
) )
6475 // starting at the bottom of a block: find the next block
6478 while ( row
< m_numRows
-1 )
6481 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6486 // starting within a block: find the bottom of the block
6488 while ( row
< m_numRows
-1 )
6491 if ( m_table
->IsEmptyCell(row
, col
) )
6499 MakeCellVisible( row
, col
);
6500 if ( expandSelection
)
6502 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6503 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6508 SetCurrentCell( row
, col
);
6517 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6520 m_currentCellCoords
!= wxGridNoCellCoords
&&
6521 m_currentCellCoords
.GetCol() > 0 )
6523 int row
= m_currentCellCoords
.GetRow();
6524 int col
= m_currentCellCoords
.GetCol();
6526 if ( m_table
->IsEmptyCell(row
, col
) )
6528 // starting in an empty cell: find the next block of
6534 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6537 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6539 // starting at the left of a block: find the next block
6545 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6550 // starting within a block: find the left of the block
6555 if ( m_table
->IsEmptyCell(row
, col
) )
6563 MakeCellVisible( row
, col
);
6564 if ( expandSelection
)
6566 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6567 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6572 SetCurrentCell( row
, col
);
6581 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6584 m_currentCellCoords
!= wxGridNoCellCoords
&&
6585 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6587 int row
= m_currentCellCoords
.GetRow();
6588 int col
= m_currentCellCoords
.GetCol();
6590 if ( m_table
->IsEmptyCell(row
, col
) )
6592 // starting in an empty cell: find the next block of
6595 while ( col
< m_numCols
-1 )
6598 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6601 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6603 // starting at the right of a block: find the next block
6606 while ( col
< m_numCols
-1 )
6609 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6614 // starting within a block: find the right of the block
6616 while ( col
< m_numCols
-1 )
6619 if ( m_table
->IsEmptyCell(row
, col
) )
6627 MakeCellVisible( row
, col
);
6628 if ( expandSelection
)
6630 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6631 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6636 SetCurrentCell( row
, col
);
6648 // ------ Label values and formatting
6651 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6653 *horiz
= m_rowLabelHorizAlign
;
6654 *vert
= m_rowLabelVertAlign
;
6657 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6659 *horiz
= m_colLabelHorizAlign
;
6660 *vert
= m_colLabelVertAlign
;
6663 wxString
wxGrid::GetRowLabelValue( int row
)
6667 return m_table
->GetRowLabelValue( row
);
6677 wxString
wxGrid::GetColLabelValue( int col
)
6681 return m_table
->GetColLabelValue( col
);
6692 void wxGrid::SetRowLabelSize( int width
)
6694 width
= wxMax( width
, 0 );
6695 if ( width
!= m_rowLabelWidth
)
6699 m_rowLabelWin
->Show( FALSE
);
6700 m_cornerLabelWin
->Show( FALSE
);
6702 else if ( m_rowLabelWidth
== 0 )
6704 m_rowLabelWin
->Show( TRUE
);
6705 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6708 m_rowLabelWidth
= width
;
6715 void wxGrid::SetColLabelSize( int height
)
6717 height
= wxMax( height
, 0 );
6718 if ( height
!= m_colLabelHeight
)
6722 m_colLabelWin
->Show( FALSE
);
6723 m_cornerLabelWin
->Show( FALSE
);
6725 else if ( m_colLabelHeight
== 0 )
6727 m_colLabelWin
->Show( TRUE
);
6728 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6731 m_colLabelHeight
= height
;
6738 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6740 if ( m_labelBackgroundColour
!= colour
)
6742 m_labelBackgroundColour
= colour
;
6743 m_rowLabelWin
->SetBackgroundColour( colour
);
6744 m_colLabelWin
->SetBackgroundColour( colour
);
6745 m_cornerLabelWin
->SetBackgroundColour( colour
);
6747 if ( !GetBatchCount() )
6749 m_rowLabelWin
->Refresh();
6750 m_colLabelWin
->Refresh();
6751 m_cornerLabelWin
->Refresh();
6756 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6758 if ( m_labelTextColour
!= colour
)
6760 m_labelTextColour
= colour
;
6761 if ( !GetBatchCount() )
6763 m_rowLabelWin
->Refresh();
6764 m_colLabelWin
->Refresh();
6769 void wxGrid::SetLabelFont( const wxFont
& font
)
6772 if ( !GetBatchCount() )
6774 m_rowLabelWin
->Refresh();
6775 m_colLabelWin
->Refresh();
6779 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6781 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6783 m_rowLabelHorizAlign
= horiz
;
6786 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6788 m_rowLabelVertAlign
= vert
;
6791 if ( !GetBatchCount() )
6793 m_rowLabelWin
->Refresh();
6797 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6799 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6801 m_colLabelHorizAlign
= horiz
;
6804 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6806 m_colLabelVertAlign
= vert
;
6809 if ( !GetBatchCount() )
6811 m_colLabelWin
->Refresh();
6815 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6819 m_table
->SetRowLabelValue( row
, s
);
6820 if ( !GetBatchCount() )
6822 wxRect rect
= CellToRect( row
, 0);
6823 if ( rect
.height
> 0 )
6825 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6827 rect
.width
= m_rowLabelWidth
;
6828 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6834 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6838 m_table
->SetColLabelValue( col
, s
);
6839 if ( !GetBatchCount() )
6841 wxRect rect
= CellToRect( 0, col
);
6842 if ( rect
.width
> 0 )
6844 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6846 rect
.height
= m_colLabelHeight
;
6847 m_colLabelWin
->Refresh( TRUE
, &rect
);
6853 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6855 if ( m_gridLineColour
!= colour
)
6857 m_gridLineColour
= colour
;
6859 wxClientDC
dc( m_gridWin
);
6861 DrawAllGridLines( dc
, wxRegion() );
6865 void wxGrid::EnableGridLines( bool enable
)
6867 if ( enable
!= m_gridLinesEnabled
)
6869 m_gridLinesEnabled
= enable
;
6871 if ( !GetBatchCount() )
6875 wxClientDC
dc( m_gridWin
);
6877 DrawAllGridLines( dc
, wxRegion() );
6881 m_gridWin
->Refresh();
6888 int wxGrid::GetDefaultRowSize()
6890 return m_defaultRowHeight
;
6893 int wxGrid::GetRowSize( int row
)
6895 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6897 return GetRowHeight(row
);
6900 int wxGrid::GetDefaultColSize()
6902 return m_defaultColWidth
;
6905 int wxGrid::GetColSize( int col
)
6907 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6909 return GetColWidth(col
);
6912 // ============================================================================
6913 // access to the grid attributes: each of them has a default value in the grid
6914 // itself and may be overidden on a per-cell basis
6915 // ============================================================================
6917 // ----------------------------------------------------------------------------
6918 // setting default attributes
6919 // ----------------------------------------------------------------------------
6921 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6923 m_defaultCellAttr
->SetBackgroundColour(col
);
6925 m_gridWin
->SetBackgroundColour(col
);
6929 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6931 m_defaultCellAttr
->SetTextColour(col
);
6934 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6936 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6939 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6941 m_defaultCellAttr
->SetFont(font
);
6944 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6946 m_defaultCellAttr
->SetRenderer(renderer
);
6949 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6951 m_defaultCellAttr
->SetEditor(editor
);
6954 // ----------------------------------------------------------------------------
6955 // access to the default attrbiutes
6956 // ----------------------------------------------------------------------------
6958 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6960 return m_defaultCellAttr
->GetBackgroundColour();
6963 wxColour
wxGrid::GetDefaultCellTextColour()
6965 return m_defaultCellAttr
->GetTextColour();
6968 wxFont
wxGrid::GetDefaultCellFont()
6970 return m_defaultCellAttr
->GetFont();
6973 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6975 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6978 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6980 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
6983 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6985 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6988 // ----------------------------------------------------------------------------
6989 // access to cell attributes
6990 // ----------------------------------------------------------------------------
6992 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6994 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6995 wxColour colour
= attr
->GetBackgroundColour();
7000 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7002 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7003 wxColour colour
= attr
->GetTextColour();
7008 wxFont
wxGrid::GetCellFont( int row
, int col
)
7010 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7011 wxFont font
= attr
->GetFont();
7016 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7018 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7019 attr
->GetAlignment(horiz
, vert
);
7023 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7025 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7026 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7032 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7034 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7035 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7041 bool wxGrid::IsReadOnly(int row
, int col
) const
7043 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7044 bool isReadOnly
= attr
->IsReadOnly();
7049 // ----------------------------------------------------------------------------
7050 // attribute support: cache, automatic provider creation, ...
7051 // ----------------------------------------------------------------------------
7053 bool wxGrid::CanHaveAttributes()
7060 return m_table
->CanHaveAttributes();
7063 void wxGrid::ClearAttrCache()
7065 if ( m_attrCache
.row
!= -1 )
7067 wxSafeDecRef(m_attrCache
.attr
);
7068 m_attrCache
.row
= -1;
7072 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7074 wxGrid
*self
= (wxGrid
*)this; // const_cast
7076 self
->ClearAttrCache();
7077 self
->m_attrCache
.row
= row
;
7078 self
->m_attrCache
.col
= col
;
7079 self
->m_attrCache
.attr
= attr
;
7083 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7085 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7087 *attr
= m_attrCache
.attr
;
7088 wxSafeIncRef(m_attrCache
.attr
);
7090 #ifdef DEBUG_ATTR_CACHE
7091 gs_nAttrCacheHits
++;
7098 #ifdef DEBUG_ATTR_CACHE
7099 gs_nAttrCacheMisses
++;
7105 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7107 wxGridCellAttr
*attr
;
7108 if ( !LookupAttr(row
, col
, &attr
) )
7110 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7111 CacheAttr(row
, col
, attr
);
7115 attr
->SetDefAttr(m_defaultCellAttr
);
7119 attr
= m_defaultCellAttr
;
7126 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7128 wxGridCellAttr
*attr
;
7129 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7131 wxASSERT_MSG( m_table
,
7132 _T("we may only be called if CanHaveAttributes() "
7133 "returned TRUE and then m_table should be !NULL") );
7135 attr
= m_table
->GetAttr(row
, col
);
7138 attr
= new wxGridCellAttr
;
7140 // artificially inc the ref count to match DecRef() in caller
7143 m_table
->SetAttr(attr
, row
, col
);
7146 CacheAttr(row
, col
, attr
);
7148 attr
->SetDefAttr(m_defaultCellAttr
);
7152 // ----------------------------------------------------------------------------
7153 // setting column attributes (wrappers around SetColAttr)
7154 // ----------------------------------------------------------------------------
7156 void wxGrid::SetColFormatBool(int col
)
7158 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7161 void wxGrid::SetColFormatNumber(int col
)
7163 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7166 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7168 wxString typeName
= wxGRID_VALUE_FLOAT
;
7169 if ( (width
!= -1) || (precision
!= -1) )
7171 typeName
<< _T(':') << width
<< _T(',') << precision
;
7174 SetColFormatCustom(col
, typeName
);
7177 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7179 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7180 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7181 attr
->SetRenderer(renderer
);
7183 SetColAttr(col
, attr
);
7186 // ----------------------------------------------------------------------------
7187 // setting cell attributes: this is forwarded to the table
7188 // ----------------------------------------------------------------------------
7190 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7192 if ( CanHaveAttributes() )
7194 m_table
->SetRowAttr(attr
, row
);
7202 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7204 if ( CanHaveAttributes() )
7206 m_table
->SetColAttr(attr
, col
);
7214 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7216 if ( CanHaveAttributes() )
7218 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7219 attr
->SetBackgroundColour(colour
);
7224 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7226 if ( CanHaveAttributes() )
7228 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7229 attr
->SetTextColour(colour
);
7234 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7236 if ( CanHaveAttributes() )
7238 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7239 attr
->SetFont(font
);
7244 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7246 if ( CanHaveAttributes() )
7248 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7249 attr
->SetAlignment(horiz
, vert
);
7254 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7256 if ( CanHaveAttributes() )
7258 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7259 attr
->SetRenderer(renderer
);
7264 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7266 if ( CanHaveAttributes() )
7268 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7269 attr
->SetEditor(editor
);
7274 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7276 if ( CanHaveAttributes() )
7278 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7279 attr
->SetReadOnly(isReadOnly
);
7284 // ----------------------------------------------------------------------------
7285 // Data type registration
7286 // ----------------------------------------------------------------------------
7288 void wxGrid::RegisterDataType(const wxString
& typeName
,
7289 wxGridCellRenderer
* renderer
,
7290 wxGridCellEditor
* editor
)
7292 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7296 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7298 wxString typeName
= m_table
->GetTypeName(row
, col
);
7299 return GetDefaultEditorForType(typeName
);
7302 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7304 wxString typeName
= m_table
->GetTypeName(row
, col
);
7305 return GetDefaultRendererForType(typeName
);
7309 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7311 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7312 if ( index
== wxNOT_FOUND
)
7314 wxFAIL_MSG(wxT("Unknown data type name"));
7319 return m_typeRegistry
->GetEditor(index
);
7323 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7325 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7326 if ( index
== wxNOT_FOUND
)
7328 wxFAIL_MSG(wxT("Unknown data type name"));
7333 return m_typeRegistry
->GetRenderer(index
);
7337 // ----------------------------------------------------------------------------
7339 // ----------------------------------------------------------------------------
7341 void wxGrid::EnableDragRowSize( bool enable
)
7343 m_canDragRowSize
= enable
;
7347 void wxGrid::EnableDragColSize( bool enable
)
7349 m_canDragColSize
= enable
;
7352 void wxGrid::EnableDragGridSize( bool enable
)
7354 m_canDragGridSize
= enable
;
7358 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7360 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7362 if ( resizeExistingRows
)
7370 void wxGrid::SetRowSize( int row
, int height
)
7372 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7374 if ( m_rowHeights
.IsEmpty() )
7376 // need to really create the array
7380 int h
= wxMax( 0, height
);
7381 int diff
= h
- m_rowHeights
[row
];
7383 m_rowHeights
[row
] = h
;
7385 for ( i
= row
; i
< m_numRows
; i
++ )
7387 m_rowBottoms
[i
] += diff
;
7392 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7394 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
7396 if ( resizeExistingCols
)
7404 void wxGrid::SetColSize( int col
, int width
)
7406 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
7408 // should we check that it's bigger than GetColMinimalWidth(col) here?
7410 if ( m_colWidths
.IsEmpty() )
7412 // need to really create the array
7416 int w
= wxMax( 0, width
);
7417 int diff
= w
- m_colWidths
[col
];
7418 m_colWidths
[col
] = w
;
7421 for ( i
= col
; i
< m_numCols
; i
++ )
7423 m_colRights
[i
] += diff
;
7429 void wxGrid::SetColMinimalWidth( int col
, int width
)
7431 m_colMinWidths
.Put(col
, width
);
7434 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7436 m_rowMinHeights
.Put(row
, width
);
7439 int wxGrid::GetColMinimalWidth(int col
) const
7441 long value
= m_colMinWidths
.Get(col
);
7442 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
7445 int wxGrid::GetRowMinimalHeight(int row
) const
7447 long value
= m_rowMinHeights
.Get(row
);
7448 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
7451 // ----------------------------------------------------------------------------
7453 // ----------------------------------------------------------------------------
7455 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
7457 wxClientDC
dc(m_gridWin
);
7459 // init both of them to avoid compiler warnings, even if weo nly need one
7467 wxCoord extent
, extentMax
= 0;
7468 int max
= column
? m_numRows
: m_numCols
;
7469 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7476 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7477 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7480 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7481 extent
= column
? size
.x
: size
.y
;
7482 if ( extent
> extentMax
)
7493 // now also compare with the column label extent
7495 dc
.SetFont( GetLabelFont() );
7498 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
7500 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
7502 extent
= column
? w
: h
;
7503 if ( extent
> extentMax
)
7510 // empty column - give default extent (notice that if extentMax is less
7511 // than default extent but != 0, it's ok)
7512 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7518 // leave some space around text
7524 SetColSize(col
, extentMax
);
7526 SetRowSize(row
, extentMax
);
7531 SetColMinimalWidth(col
, extentMax
);
7533 SetRowMinimalHeight(row
, extentMax
);
7537 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7539 int width
= m_rowLabelWidth
;
7541 for ( int col
= 0; col
< m_numCols
; col
++ )
7545 AutoSizeColumn(col
, setAsMin
);
7548 width
+= GetColWidth(col
);
7554 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7556 int height
= m_colLabelHeight
;
7558 for ( int row
= 0; row
< m_numRows
; row
++ )
7562 AutoSizeRow(row
, setAsMin
);
7565 height
+= GetRowHeight(row
);
7571 void wxGrid::AutoSize()
7574 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
7577 wxSize
wxGrid::DoGetBestSize() const
7579 // don't set sizes, only calculate them
7580 wxGrid
*self
= (wxGrid
*)this; // const_cast
7582 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
7583 self
->SetOrCalcRowSizes(TRUE
));
7591 // ----------------------------------------------------------------------------
7592 // cell value accessor functions
7593 // ----------------------------------------------------------------------------
7595 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
7599 m_table
->SetValue( row
, col
, s
.c_str() );
7600 if ( !GetBatchCount() )
7602 wxClientDC
dc( m_gridWin
);
7604 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7607 if ( m_currentCellCoords
.GetRow() == row
&&
7608 m_currentCellCoords
.GetCol() == col
&&
7609 IsCellEditControlEnabled())
7611 HideCellEditControl();
7612 ShowCellEditControl(); // will reread data from table
7619 // ------ Block, row and col selection
7622 void wxGrid::SelectRow( int row
, bool addToSelected
)
7624 if ( IsSelection() && !addToSelected
)
7627 m_selection
->SelectRow( row
);
7631 void wxGrid::SelectCol( int col
, bool addToSelected
)
7633 if ( IsSelection() && !addToSelected
)
7636 m_selection
->SelectCol( col
);
7640 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7643 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7645 if ( topRow
> bottomRow
)
7652 if ( leftCol
> rightCol
)
7659 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7660 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7662 if ( m_selectingTopLeft
!= updateTopLeft
||
7663 m_selectingBottomRight
!= updateBottomRight
)
7665 // Compute two optimal update rectangles:
7666 // Either one rectangle is a real subset of the
7667 // other, or they are (almost) disjoint!
7669 bool need_refresh
[4];
7673 need_refresh
[3] = FALSE
;
7676 // Store intermediate values
7677 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7678 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7679 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7680 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7682 // Determine the outer/inner coordinates.
7683 if (oldLeft
> leftCol
)
7689 if (oldTop
> topRow
)
7695 if (oldRight
< rightCol
)
7698 oldRight
= rightCol
;
7701 if (oldBottom
< bottomRow
)
7704 oldBottom
= bottomRow
;
7708 // Now, either the stuff marked old is the outer
7709 // rectangle or we don't have a situation where one
7710 // is contained in the other.
7712 if ( oldLeft
< leftCol
)
7714 need_refresh
[0] = TRUE
;
7715 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7717 wxGridCellCoords ( oldBottom
,
7721 if ( oldTop
< topRow
)
7723 need_refresh
[1] = TRUE
;
7724 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7726 wxGridCellCoords ( topRow
- 1,
7730 if ( oldRight
> rightCol
)
7732 need_refresh
[2] = TRUE
;
7733 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7735 wxGridCellCoords ( oldBottom
,
7739 if ( oldBottom
> bottomRow
)
7741 need_refresh
[3] = TRUE
;
7742 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7744 wxGridCellCoords ( oldBottom
,
7750 m_selectingTopLeft
= updateTopLeft
;
7751 m_selectingBottomRight
= updateBottomRight
;
7753 // various Refresh() calls
7754 for (i
= 0; i
< 4; i
++ )
7755 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7756 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7759 // never generate an event as it will be generated from
7760 // wxGridSelection::SelectBlock!
7763 void wxGrid::SelectAll()
7765 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
7768 bool wxGrid::IsSelection()
7770 return ( m_selection
->IsSelection() ||
7771 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7772 m_selectingBottomRight
!= wxGridNoCellCoords
) );
7775 bool wxGrid::IsInSelection( int row
, int col
)
7777 return ( m_selection
->IsInSelection( row
, col
) ||
7778 ( row
>= m_selectingTopLeft
.GetRow() &&
7779 col
>= m_selectingTopLeft
.GetCol() &&
7780 row
<= m_selectingBottomRight
.GetRow() &&
7781 col
<= m_selectingBottomRight
.GetCol() ) );
7784 void wxGrid::ClearSelection()
7786 m_selectingTopLeft
= wxGridNoCellCoords
;
7787 m_selectingBottomRight
= wxGridNoCellCoords
;
7788 m_selection
->ClearSelection();
7792 // This function returns the rectangle that encloses the given block
7793 // in device coords clipped to the client size of the grid window.
7795 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7796 const wxGridCellCoords
&bottomRight
)
7798 wxRect
rect( wxGridNoCellRect
);
7801 cellRect
= CellToRect( topLeft
);
7802 if ( cellRect
!= wxGridNoCellRect
)
7808 rect
= wxRect( 0, 0, 0, 0 );
7811 cellRect
= CellToRect( bottomRight
);
7812 if ( cellRect
!= wxGridNoCellRect
)
7818 return wxGridNoCellRect
;
7821 // convert to scrolled coords
7823 int left
, top
, right
, bottom
;
7824 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7825 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7828 m_gridWin
->GetClientSize( &cw
, &ch
);
7830 rect
.SetLeft( wxMax(0, left
) );
7831 rect
.SetTop( wxMax(0, top
) );
7832 rect
.SetRight( wxMin(cw
, right
) );
7833 rect
.SetBottom( wxMin(ch
, bottom
) );
7841 // ------ Grid event classes
7844 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7846 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7847 int row
, int col
, int x
, int y
, bool sel
,
7848 bool control
, bool shift
, bool alt
, bool meta
)
7849 : wxNotifyEvent( type
, id
)
7856 m_control
= control
;
7861 SetEventObject(obj
);
7865 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7867 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7868 int rowOrCol
, int x
, int y
,
7869 bool control
, bool shift
, bool alt
, bool meta
)
7870 : wxNotifyEvent( type
, id
)
7872 m_rowOrCol
= rowOrCol
;
7875 m_control
= control
;
7880 SetEventObject(obj
);
7884 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7886 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7887 const wxGridCellCoords
& topLeft
,
7888 const wxGridCellCoords
& bottomRight
,
7889 bool sel
, bool control
,
7890 bool shift
, bool alt
, bool meta
)
7891 : wxNotifyEvent( type
, id
)
7893 m_topLeft
= topLeft
;
7894 m_bottomRight
= bottomRight
;
7896 m_control
= control
;
7901 SetEventObject(obj
);
7905 #endif // ifndef wxUSE_NEW_GRID