1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
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 precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
35 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
37 #else // wxUSE_NEW_GRID
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
46 #include "wx/combobox.h"
47 #include "wx/valtext.h"
50 #include "wx/textfile.h"
51 #include "wx/spinctrl.h"
52 #include "wx/tokenzr.h"
55 #include "wx/generic/gridsel.h"
57 #if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_MOTIF(identifier) identifier
63 #if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
66 #define WXUNUSED_GTK(identifier) identifier
69 // Required for wxIs... functions
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
90 wxGridCellCoords coords
;
94 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
129 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
130 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
131 const wxPoint
&pos
, const wxSize
&size
);
136 void OnPaint( wxPaintEvent
& event
);
137 void OnMouseEvent( wxMouseEvent
& event
);
138 void OnMouseWheel( wxMouseEvent
& event
);
139 void OnKeyDown( wxKeyEvent
& event
);
140 void OnKeyUp( wxKeyEvent
& );
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
143 DECLARE_EVENT_TABLE()
147 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
150 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
151 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
152 const wxPoint
&pos
, const wxSize
&size
);
157 void OnPaint( wxPaintEvent
&event
);
158 void OnMouseEvent( wxMouseEvent
& event
);
159 void OnMouseWheel( wxMouseEvent
& event
);
160 void OnKeyDown( wxKeyEvent
& event
);
161 void OnKeyUp( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
168 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
171 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
172 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
178 void OnMouseEvent( wxMouseEvent
& event
);
179 void OnMouseWheel( wxMouseEvent
& event
);
180 void OnKeyDown( wxKeyEvent
& event
);
181 void OnKeyUp( wxKeyEvent
& );
182 void OnPaint( wxPaintEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
185 DECLARE_EVENT_TABLE()
188 class WXDLLEXPORT wxGridWindow
: public wxWindow
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnEraseBackground( wxEraseEvent
& );
219 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
220 DECLARE_EVENT_TABLE()
225 class wxGridCellEditorEvtHandler
: public wxEvtHandler
228 wxGridCellEditorEvtHandler()
229 : m_grid(0), m_editor(0)
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
232 : m_grid(grid
), m_editor(editor
)
235 void OnKeyDown(wxKeyEvent
& event
);
236 void OnChar(wxKeyEvent
& event
);
240 wxGridCellEditor
* m_editor
;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
242 DECLARE_EVENT_TABLE()
246 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
247 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
254 // ----------------------------------------------------------------------------
255 // the internal data representation used by wxGridCellAttrProvider
256 // ----------------------------------------------------------------------------
258 // this class stores attributes set for cells
259 class WXDLLEXPORT wxGridCellAttrData
262 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
263 wxGridCellAttr
*GetAttr(int row
, int col
) const;
264 void UpdateAttrRows( size_t pos
, int numRows
);
265 void UpdateAttrCols( size_t pos
, int numCols
);
268 // searches for the attr for given cell, returns wxNOT_FOUND if not found
269 int FindIndex(int row
, int col
) const;
271 wxGridCellWithAttrArray m_attrs
;
274 // this class stores attributes set for rows or columns
275 class WXDLLEXPORT wxGridRowOrColAttrData
278 // empty ctor to suppress warnings
279 wxGridRowOrColAttrData() { }
280 ~wxGridRowOrColAttrData();
282 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
283 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
284 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
287 wxArrayInt m_rowsOrCols
;
288 wxArrayAttrs m_attrs
;
291 // NB: this is just a wrapper around 3 objects: one which stores cell
292 // attributes, and 2 others for row/col ones
293 class WXDLLEXPORT wxGridCellAttrProviderData
296 wxGridCellAttrData m_cellAttrs
;
297 wxGridRowOrColAttrData m_rowAttrs
,
302 // ----------------------------------------------------------------------------
303 // data structures used for the data type registry
304 // ----------------------------------------------------------------------------
306 struct wxGridDataTypeInfo
308 wxGridDataTypeInfo(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
)
311 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
314 ~wxGridDataTypeInfo()
316 wxSafeDecRef(m_renderer
);
317 wxSafeDecRef(m_editor
);
321 wxGridCellRenderer
* m_renderer
;
322 wxGridCellEditor
* m_editor
;
326 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
329 class WXDLLEXPORT wxGridTypeRegistry
332 wxGridTypeRegistry() {}
333 ~wxGridTypeRegistry();
335 void RegisterDataType(const wxString
& typeName
,
336 wxGridCellRenderer
* renderer
,
337 wxGridCellEditor
* editor
);
339 // find one of already registered data types
340 int FindRegisteredDataType(const wxString
& typeName
);
342 // try to FindRegisteredDataType(), if this fails and typeName is one of
343 // standard typenames, register it and return its index
344 int FindDataType(const wxString
& typeName
);
346 // try to FindDataType(), if it fails see if it is not one of already
347 // registered data types with some params in which case clone the
348 // registered data type and set params for it
349 int FindOrCloneDataType(const wxString
& typeName
);
351 wxGridCellRenderer
* GetRenderer(int index
);
352 wxGridCellEditor
* GetEditor(int index
);
355 wxGridDataTypeInfoArray m_typeinfo
;
358 // ----------------------------------------------------------------------------
359 // conditional compilation
360 // ----------------------------------------------------------------------------
362 #ifndef WXGRID_DRAW_LINES
363 #define WXGRID_DRAW_LINES 1
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 //#define DEBUG_ATTR_CACHE
371 #ifdef DEBUG_ATTR_CACHE
372 static size_t gs_nAttrCacheHits
= 0;
373 static size_t gs_nAttrCacheMisses
= 0;
374 #endif // DEBUG_ATTR_CACHE
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
381 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
384 // TODO: this doesn't work at all, grid cells have different sizes and approx
385 // calculations don't work as because of the size mismatch scrollbars
386 // sometimes fail to be shown when they should be or vice versa
388 // The scroll bars may be a little flakey once in a while, but that is
389 // surely much less horrible than having scroll lines of only 1!!!
392 // Well, it's still seriously broken so it might be better but needs
395 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
396 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
398 // the size of hash tables used a bit everywhere (the max number of elements
399 // in these hash tables is the number of rows/columns)
400 static const int GRID_HASH_SIZE
= 100;
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 static inline int GetScrollX(int x
)
408 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
411 static inline int GetScrollY(int y
)
413 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
416 // ============================================================================
418 // ============================================================================
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxGridCellEditor::wxGridCellEditor()
430 wxGridCellEditor::~wxGridCellEditor()
435 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
436 wxWindowID
WXUNUSED(id
),
437 wxEvtHandler
* evtHandler
)
440 m_control
->PushEventHandler(evtHandler
);
443 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
444 wxGridCellAttr
*attr
)
446 // erase the background because we might not fill the cell
447 wxClientDC
dc(m_control
->GetParent());
448 dc
.SetPen(*wxTRANSPARENT_PEN
);
449 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
450 dc
.DrawRectangle(rectCell
);
452 // redraw the control we just painted over
453 m_control
->Refresh();
456 void wxGridCellEditor::Destroy()
460 m_control
->PopEventHandler(TRUE
/* delete it*/);
462 m_control
->Destroy();
467 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
469 wxASSERT_MSG(m_control
,
470 wxT("The wxGridCellEditor must be Created first!"));
471 m_control
->Show(show
);
475 // set the colours/fonts if we have any
478 m_colFgOld
= m_control
->GetForegroundColour();
479 m_control
->SetForegroundColour(attr
->GetTextColour());
481 m_colBgOld
= m_control
->GetBackgroundColour();
482 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
484 m_fontOld
= m_control
->GetFont();
485 m_control
->SetFont(attr
->GetFont());
487 // can't do anything more in the base class version, the other
488 // attributes may only be used by the derived classes
493 // restore the standard colours fonts
494 if ( m_colFgOld
.Ok() )
496 m_control
->SetForegroundColour(m_colFgOld
);
497 m_colFgOld
= wxNullColour
;
500 if ( m_colBgOld
.Ok() )
502 m_control
->SetBackgroundColour(m_colBgOld
);
503 m_colBgOld
= wxNullColour
;
506 if ( m_fontOld
.Ok() )
508 m_control
->SetFont(m_fontOld
);
509 m_fontOld
= wxNullFont
;
514 void wxGridCellEditor::SetSize(const wxRect
& rect
)
516 wxASSERT_MSG(m_control
,
517 wxT("The wxGridCellEditor must be Created first!"));
518 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
521 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
526 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
528 // accept the simple key presses, not anything with Ctrl/Alt/Meta
529 return !(event
.ControlDown() || event
.AltDown());
532 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
537 void wxGridCellEditor::StartingClick()
543 // ----------------------------------------------------------------------------
544 // wxGridCellTextEditor
545 // ----------------------------------------------------------------------------
547 wxGridCellTextEditor::wxGridCellTextEditor()
552 void wxGridCellTextEditor::Create(wxWindow
* parent
,
554 wxEvtHandler
* evtHandler
)
556 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
557 wxDefaultPosition
, wxDefaultSize
558 #if defined(__WXMSW__)
559 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
560 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
564 // TODO: use m_maxChars
566 wxGridCellEditor::Create(parent
, id
, evtHandler
);
569 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
570 wxGridCellAttr
* WXUNUSED(attr
))
572 // as we fill the entire client area, don't do anything here to minimize
576 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
578 wxRect
rect(rectOrig
);
580 // Make the edit control large enough to allow for internal
583 // TODO: remove this if the text ctrl sizing is improved esp. for
586 #if defined(__WXGTK__)
595 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
597 // MB: treat MSW separately here otherwise the caret doesn't show
598 // when the editor is in the first row.
599 #if defined(__WXMSW__)
602 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
605 #if defined(__WXMOTIF__)
609 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
610 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
611 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
612 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
615 wxGridCellEditor::SetSize(rect
);
618 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
620 wxASSERT_MSG(m_control
,
621 wxT("The wxGridCellEditor must be Created first!"));
623 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
625 DoBeginEdit(m_startValue
);
628 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
630 Text()->SetValue(startValue
);
631 Text()->SetInsertionPointEnd();
635 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
638 wxASSERT_MSG(m_control
,
639 wxT("The wxGridCellEditor must be Created first!"));
641 bool changed
= FALSE
;
642 wxString value
= Text()->GetValue();
643 if (value
!= m_startValue
)
647 grid
->GetTable()->SetValue(row
, col
, value
);
649 m_startValue
= wxEmptyString
;
650 Text()->SetValue(m_startValue
);
656 void wxGridCellTextEditor::Reset()
658 wxASSERT_MSG(m_control
,
659 wxT("The wxGridCellEditor must be Created first!"));
661 DoReset(m_startValue
);
664 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
666 Text()->SetValue(startValue
);
667 Text()->SetInsertionPointEnd();
670 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
672 if ( wxGridCellEditor::IsAcceptedKey(event
) )
674 int keycode
= event
.GetKeyCode();
688 case WXK_NUMPAD_MULTIPLY
:
692 case WXK_NUMPAD_SUBTRACT
:
694 case WXK_NUMPAD_DECIMAL
:
696 case WXK_NUMPAD_DIVIDE
:
700 // accept 8 bit chars too if isprint() agrees
701 if ( (keycode
< 255) && (isprint(keycode
)) )
709 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
711 // we don't check for !HasModifiers() because IsAcceptedKey() did it
713 // insert the key in the control
715 int keycode
= event
.GetKeyCode();
728 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
732 case WXK_NUMPAD_MULTIPLY
:
742 case WXK_NUMPAD_SUBTRACT
:
747 case WXK_NUMPAD_DECIMAL
:
752 case WXK_NUMPAD_DIVIDE
:
757 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
759 // FIXME this is not going to work for non letters...
760 if ( !event
.ShiftDown() )
762 keycode
= tolower(keycode
);
765 ch
= (wxChar
)keycode
;
775 Text()->AppendText(ch
);
783 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
784 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
786 #if defined(__WXMOTIF__) || defined(__WXGTK__)
787 // wxMotif needs a little extra help...
788 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
789 wxString
s( Text()->GetValue() );
790 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
792 Text()->SetInsertionPoint( pos
);
794 // the other ports can handle a Return key press
800 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
810 if ( !params
.ToLong(&tmp
) )
812 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
816 m_maxChars
= (size_t)tmp
;
821 // ----------------------------------------------------------------------------
822 // wxGridCellNumberEditor
823 // ----------------------------------------------------------------------------
825 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
831 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
833 wxEvtHandler
* evtHandler
)
837 // create a spin ctrl
838 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
839 wxDefaultPosition
, wxDefaultSize
,
843 wxGridCellEditor::Create(parent
, id
, evtHandler
);
847 // just a text control
848 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
851 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
852 #endif // wxUSE_VALIDATORS
856 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
858 // first get the value
859 wxGridTableBase
*table
= grid
->GetTable();
860 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
862 m_valueOld
= table
->GetValueAsLong(row
, col
);
866 wxString sValue
= table
->GetValue(row
, col
);
867 if (! sValue
.ToLong(&m_valueOld
))
869 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
876 Spin()->SetValue((int)m_valueOld
);
881 DoBeginEdit(GetString());
885 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
893 value
= Spin()->GetValue();
894 changed
= value
!= m_valueOld
;
898 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
903 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
904 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
906 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
912 void wxGridCellNumberEditor::Reset()
916 Spin()->SetValue((int)m_valueOld
);
920 DoReset(GetString());
924 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
926 if ( wxGridCellEditor::IsAcceptedKey(event
) )
928 int keycode
= event
.GetKeyCode();
944 case WXK_NUMPAD_SUBTRACT
:
950 if ( (keycode
< 128) && isdigit(keycode
) )
958 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
962 int keycode
= (int) event
.KeyCode();
963 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
965 wxGridCellTextEditor::StartingKey(event
);
975 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
986 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
990 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
994 // skip the error message below
999 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1003 // ----------------------------------------------------------------------------
1004 // wxGridCellFloatEditor
1005 // ----------------------------------------------------------------------------
1007 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1010 m_precision
= precision
;
1013 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1015 wxEvtHandler
* evtHandler
)
1017 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1019 #if wxUSE_VALIDATORS
1020 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1021 #endif // wxUSE_VALIDATORS
1024 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1026 // first get the value
1027 wxGridTableBase
*table
= grid
->GetTable();
1028 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1030 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1034 wxString sValue
= table
->GetValue(row
, col
);
1035 if (! sValue
.ToDouble(&m_valueOld
))
1037 wxFAIL_MSG( _T("this cell doesn't have float value") );
1042 DoBeginEdit(GetString());
1045 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1049 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
1051 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1052 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1054 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1064 void wxGridCellFloatEditor::Reset()
1066 DoReset(GetString());
1069 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1071 int keycode
= (int)event
.KeyCode();
1072 if ( isdigit(keycode
) ||
1073 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1075 wxGridCellTextEditor::StartingKey(event
);
1077 // skip Skip() below
1084 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1095 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1099 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1101 m_precision
= (int)tmp
;
1103 // skip the error message below
1108 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1112 wxString
wxGridCellFloatEditor::GetString() const
1115 if ( m_width
== -1 )
1117 // default width/precision
1120 else if ( m_precision
== -1 )
1122 // default precision
1123 fmt
.Printf(_T("%%%d.g"), m_width
);
1127 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1130 return wxString::Format(fmt
, m_valueOld
);
1133 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1135 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1137 int keycode
= event
.GetKeyCode();
1151 case WXK_NUMPAD_ADD
:
1153 case WXK_NUMPAD_SUBTRACT
:
1155 case WXK_NUMPAD_DECIMAL
:
1159 // additionally accept 'e' as in '1e+6'
1160 if ( (keycode
< 128) &&
1161 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1169 #endif // wxUSE_TEXTCTRL
1173 // ----------------------------------------------------------------------------
1174 // wxGridCellBoolEditor
1175 // ----------------------------------------------------------------------------
1177 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1179 wxEvtHandler
* evtHandler
)
1181 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1182 wxDefaultPosition
, wxDefaultSize
,
1185 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1188 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1190 bool resize
= FALSE
;
1191 wxSize size
= m_control
->GetSize();
1192 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1194 // check if the checkbox is not too big/small for this cell
1195 wxSize sizeBest
= m_control
->GetBestSize();
1196 if ( !(size
== sizeBest
) )
1198 // reset to default size if it had been made smaller
1204 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1206 // leave 1 pixel margin
1207 size
.x
= size
.y
= minSize
- 2;
1214 m_control
->SetSize(size
);
1217 // position it in the centre of the rectangle (TODO: support alignment?)
1219 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1220 // the checkbox without label still has some space to the right in wxGTK,
1221 // so shift it to the right
1223 #elif defined(__WXMSW__)
1224 // here too, but in other way
1229 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1232 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1234 m_control
->Show(show
);
1238 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1239 CBox()->SetBackgroundColour(colBg
);
1243 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1245 wxASSERT_MSG(m_control
,
1246 wxT("The wxGridCellEditor must be Created first!"));
1248 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1249 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1252 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1253 m_startValue
= !( !cellval
|| (cellval
== "0") );
1255 CBox()->SetValue(m_startValue
);
1259 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1262 wxASSERT_MSG(m_control
,
1263 wxT("The wxGridCellEditor must be Created first!"));
1265 bool changed
= FALSE
;
1266 bool value
= CBox()->GetValue();
1267 if ( value
!= m_startValue
)
1272 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1273 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1275 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1281 void wxGridCellBoolEditor::Reset()
1283 wxASSERT_MSG(m_control
,
1284 wxT("The wxGridCellEditor must be Created first!"));
1286 CBox()->SetValue(m_startValue
);
1289 void wxGridCellBoolEditor::StartingClick()
1291 CBox()->SetValue(!CBox()->GetValue());
1294 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1296 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1298 int keycode
= event
.GetKeyCode();
1302 case WXK_NUMPAD_MULTIPLY
:
1304 case WXK_NUMPAD_ADD
:
1306 case WXK_NUMPAD_SUBTRACT
:
1317 #endif // wxUSE_CHECKBOX
1321 // ----------------------------------------------------------------------------
1322 // wxGridCellChoiceEditor
1323 // ----------------------------------------------------------------------------
1325 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1326 const wxString choices
[],
1328 : m_allowOthers(allowOthers
)
1332 m_choices
.Alloc(count
);
1333 for ( size_t n
= 0; n
< count
; n
++ )
1335 m_choices
.Add(choices
[n
]);
1340 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1342 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1343 editor
->m_allowOthers
= m_allowOthers
;
1344 editor
->m_choices
= m_choices
;
1349 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1351 wxEvtHandler
* evtHandler
)
1353 size_t count
= m_choices
.GetCount();
1354 wxString
*choices
= new wxString
[count
];
1355 for ( size_t n
= 0; n
< count
; n
++ )
1357 choices
[n
] = m_choices
[n
];
1360 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1361 wxDefaultPosition
, wxDefaultSize
,
1363 m_allowOthers
? 0 : wxCB_READONLY
);
1367 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1370 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1371 wxGridCellAttr
* attr
)
1373 // as we fill the entire client area, don't do anything here to minimize
1376 // TODO: It doesn't actually fill the client area since the height of a
1377 // combo always defaults to the standard... Until someone has time to
1378 // figure out the right rectangle to paint, just do it the normal way...
1379 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1382 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1384 wxASSERT_MSG(m_control
,
1385 wxT("The wxGridCellEditor must be Created first!"));
1387 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1389 Combo()->SetValue(m_startValue
);
1390 size_t count
= m_choices
.GetCount();
1391 for (size_t i
=0; i
<count
; i
++)
1393 if (m_startValue
== m_choices
[i
])
1395 Combo()->SetSelection(i
);
1399 Combo()->SetInsertionPointEnd();
1400 Combo()->SetFocus();
1403 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1406 wxString value
= Combo()->GetValue();
1407 bool changed
= value
!= m_startValue
;
1410 grid
->GetTable()->SetValue(row
, col
, value
);
1412 m_startValue
= wxEmptyString
;
1413 Combo()->SetValue(m_startValue
);
1418 void wxGridCellChoiceEditor::Reset()
1420 Combo()->SetValue(m_startValue
);
1421 Combo()->SetInsertionPointEnd();
1424 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1434 wxStringTokenizer
tk(params
, _T(','));
1435 while ( tk
.HasMoreTokens() )
1437 m_choices
.Add(tk
.GetNextToken());
1441 #endif // wxUSE_COMBOBOX
1443 // ----------------------------------------------------------------------------
1444 // wxGridCellEditorEvtHandler
1445 // ----------------------------------------------------------------------------
1447 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1449 switch ( event
.KeyCode() )
1453 m_grid
->DisableCellEditControl();
1457 m_grid
->GetEventHandler()->ProcessEvent( event
);
1461 case WXK_NUMPAD_ENTER
:
1462 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1463 m_editor
->HandleReturn(event
);
1472 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1474 switch ( event
.KeyCode() )
1479 case WXK_NUMPAD_ENTER
:
1487 // ----------------------------------------------------------------------------
1488 // wxGridCellWorker is an (almost) empty common base class for
1489 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1490 // ----------------------------------------------------------------------------
1492 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1497 wxGridCellWorker::~wxGridCellWorker()
1501 // ============================================================================
1503 // ============================================================================
1505 // ----------------------------------------------------------------------------
1506 // wxGridCellRenderer
1507 // ----------------------------------------------------------------------------
1509 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1510 wxGridCellAttr
& attr
,
1513 int WXUNUSED(row
), int WXUNUSED(col
),
1516 dc
.SetBackgroundMode( wxSOLID
);
1520 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1524 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1527 dc
.SetPen( *wxTRANSPARENT_PEN
);
1528 dc
.DrawRectangle(rect
);
1531 // ----------------------------------------------------------------------------
1532 // wxGridCellStringRenderer
1533 // ----------------------------------------------------------------------------
1535 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1536 wxGridCellAttr
& attr
,
1540 dc
.SetBackgroundMode( wxTRANSPARENT
);
1542 // TODO some special colours for attr.IsReadOnly() case?
1546 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1547 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1551 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1552 dc
.SetTextForeground( attr
.GetTextColour() );
1555 dc
.SetFont( attr
.GetFont() );
1558 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1560 const wxString
& text
)
1562 wxCoord x
= 0, y
= 0, max_x
= 0;
1563 dc
.SetFont(attr
.GetFont());
1564 wxStringTokenizer
tk(text
, _T('\n'));
1565 while ( tk
.HasMoreTokens() )
1567 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1568 max_x
= wxMax(max_x
, x
);
1571 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1573 return wxSize(max_x
, y
);
1576 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1577 wxGridCellAttr
& attr
,
1581 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1584 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1585 wxGridCellAttr
& attr
,
1587 const wxRect
& rectCell
,
1591 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1593 // now we only have to draw the text
1594 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1597 attr
.GetAlignment(&hAlign
, &vAlign
);
1599 wxRect rect
= rectCell
;
1602 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1603 rect
, hAlign
, vAlign
);
1606 // ----------------------------------------------------------------------------
1607 // wxGridCellNumberRenderer
1608 // ----------------------------------------------------------------------------
1610 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1612 wxGridTableBase
*table
= grid
.GetTable();
1614 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1616 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1620 text
= table
->GetValue(row
, col
);
1626 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1627 wxGridCellAttr
& attr
,
1629 const wxRect
& rectCell
,
1633 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1635 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1637 // draw the text right aligned by default
1639 attr
.GetAlignment(&hAlign
, &vAlign
);
1640 hAlign
= wxALIGN_RIGHT
;
1642 wxRect rect
= rectCell
;
1645 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1648 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1649 wxGridCellAttr
& attr
,
1653 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1656 // ----------------------------------------------------------------------------
1657 // wxGridCellFloatRenderer
1658 // ----------------------------------------------------------------------------
1660 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1663 SetPrecision(precision
);
1666 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1668 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1669 renderer
->m_width
= m_width
;
1670 renderer
->m_precision
= m_precision
;
1671 renderer
->m_format
= m_format
;
1676 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1678 wxGridTableBase
*table
= grid
.GetTable();
1683 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1685 val
= table
->GetValueAsDouble(row
, col
);
1690 text
= table
->GetValue(row
, col
);
1691 hasDouble
= text
.ToDouble(&val
);
1698 if ( m_width
== -1 )
1700 if ( m_precision
== -1 )
1702 // default width/precision
1703 m_format
= _T("%f");
1707 m_format
.Printf(_T("%%.%df"), m_precision
);
1710 else if ( m_precision
== -1 )
1712 // default precision
1713 m_format
.Printf(_T("%%%d.f"), m_width
);
1717 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1721 text
.Printf(m_format
, val
);
1724 //else: text already contains the string
1729 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1730 wxGridCellAttr
& attr
,
1732 const wxRect
& rectCell
,
1736 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1738 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1740 // draw the text right aligned by default
1742 attr
.GetAlignment(&hAlign
, &vAlign
);
1743 hAlign
= wxALIGN_RIGHT
;
1745 wxRect rect
= rectCell
;
1748 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1751 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1752 wxGridCellAttr
& attr
,
1756 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1759 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1763 // reset to defaults
1769 wxString tmp
= params
.BeforeFirst(_T(','));
1773 if ( tmp
.ToLong(&width
) )
1775 SetWidth((int)width
);
1779 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1783 tmp
= params
.AfterFirst(_T(','));
1787 if ( tmp
.ToLong(&precision
) )
1789 SetPrecision((int)precision
);
1793 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1801 // ----------------------------------------------------------------------------
1802 // wxGridCellBoolRenderer
1803 // ----------------------------------------------------------------------------
1805 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1807 // FIXME these checkbox size calculations are really ugly...
1809 // between checkmark and box
1810 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1812 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1813 wxGridCellAttr
& WXUNUSED(attr
),
1818 // compute it only once (no locks for MT safeness in GUI thread...)
1819 if ( !ms_sizeCheckMark
.x
)
1821 // get checkbox size
1822 wxCoord checkSize
= 0;
1823 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1824 wxSize size
= checkbox
->GetBestSize();
1825 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1827 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1828 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1829 checkSize
-= size
.y
/ 2;
1834 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1837 return ms_sizeCheckMark
;
1840 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1841 wxGridCellAttr
& attr
,
1847 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1849 // draw a check mark in the centre (ignoring alignment - TODO)
1850 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1852 // don't draw outside the cell
1853 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1854 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1856 // and even leave (at least) 1 pixel margin
1857 size
.x
= size
.y
= minSize
- 2;
1860 // draw a border around checkmark
1862 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1863 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1864 rectBorder
.width
= size
.x
;
1865 rectBorder
.height
= size
.y
;
1868 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1869 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1872 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1873 value
= !( !cellval
|| (cellval
== "0") );
1878 wxRect rectMark
= rectBorder
;
1880 // MSW DrawCheckMark() is weird (and should probably be changed...)
1881 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1885 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1888 dc
.SetTextForeground(attr
.GetTextColour());
1889 dc
.DrawCheckMark(rectMark
);
1892 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1893 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1894 dc
.DrawRectangle(rectBorder
);
1897 // ----------------------------------------------------------------------------
1899 // ----------------------------------------------------------------------------
1901 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1905 m_isReadOnly
= Unset
;
1910 m_attrkind
= wxGridCellAttr::Cell
;
1912 SetDefAttr(attrDefault
);
1915 wxGridCellAttr
*wxGridCellAttr::Clone() const
1917 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
1919 if ( HasTextColour() )
1920 attr
->SetTextColour(GetTextColour());
1921 if ( HasBackgroundColour() )
1922 attr
->SetBackgroundColour(GetBackgroundColour());
1924 attr
->SetFont(GetFont());
1925 if ( HasAlignment() )
1926 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1930 attr
->SetRenderer(m_renderer
);
1931 m_renderer
->IncRef();
1935 attr
->SetEditor(m_editor
);
1940 attr
->SetReadOnly();
1942 attr
->SetKind( m_attrkind
);
1947 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1949 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1950 SetTextColour(mergefrom
->GetTextColour());
1951 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1952 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1953 if ( !HasFont() && mergefrom
->HasFont() )
1954 SetFont(mergefrom
->GetFont());
1955 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1957 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1958 SetAlignment(hAlign
, vAlign
);
1961 // Directly access member functions as GetRender/Editor don't just return
1962 // m_renderer/m_editor
1964 // Maybe add support for merge of Render and Editor?
1965 if (!HasRenderer() && mergefrom
->HasRenderer() )
1967 m_renderer
= mergefrom
->m_renderer
;
1968 m_renderer
->IncRef();
1970 if ( !HasEditor() && mergefrom
->HasEditor() )
1972 m_editor
= mergefrom
->m_editor
;
1975 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1976 SetReadOnly(mergefrom
->IsReadOnly());
1978 SetDefAttr(mergefrom
->m_defGridAttr
);
1981 const wxColour
& wxGridCellAttr::GetTextColour() const
1983 if (HasTextColour())
1987 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1989 return m_defGridAttr
->GetTextColour();
1993 wxFAIL_MSG(wxT("Missing default cell attribute"));
1994 return wxNullColour
;
1999 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2001 if (HasBackgroundColour())
2003 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2004 return m_defGridAttr
->GetBackgroundColour();
2007 wxFAIL_MSG(wxT("Missing default cell attribute"));
2008 return wxNullColour
;
2013 const wxFont
& wxGridCellAttr::GetFont() const
2017 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2018 return m_defGridAttr
->GetFont();
2021 wxFAIL_MSG(wxT("Missing default cell attribute"));
2027 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2031 if ( hAlign
) *hAlign
= m_hAlign
;
2032 if ( vAlign
) *vAlign
= m_vAlign
;
2034 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2035 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2038 wxFAIL_MSG(wxT("Missing default cell attribute"));
2043 // GetRenderer and GetEditor use a slightly different decision path about
2044 // which attribute to use. If a non-default attr object has one then it is
2045 // used, otherwise the default editor or renderer is fetched from the grid and
2046 // used. It should be the default for the data type of the cell. If it is
2047 // NULL (because the table has a type that the grid does not have in its
2048 // registry,) then the grid's default editor or renderer is used.
2050 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2052 wxGridCellRenderer
*renderer
;
2054 if ( m_renderer
&& this != m_defGridAttr
)
2056 // use the cells renderer if it has one
2057 renderer
= m_renderer
;
2060 else // no non default cell renderer
2062 // get default renderer for the data type
2065 // GetDefaultRendererForCell() will do IncRef() for us
2066 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2075 if (m_defGridAttr
&& this != m_defGridAttr
)
2077 // if we still don't have one then use the grid default
2078 // (no need for IncRef() here neither)
2079 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2081 else // default grid attr
2083 // use m_renderer which we had decided not to use initially
2084 renderer
= m_renderer
;
2091 // we're supposed to always find something
2092 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2097 // same as above, except for s/renderer/editor/g
2098 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2100 wxGridCellEditor
*editor
;
2102 if ( m_editor
&& this != m_defGridAttr
)
2104 // use the cells editor if it has one
2108 else // no non default cell editor
2110 // get default editor for the data type
2113 // GetDefaultEditorForCell() will do IncRef() for us
2114 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2123 if ( m_defGridAttr
&& this != m_defGridAttr
)
2125 // if we still don't have one then use the grid default
2126 // (no need for IncRef() here neither)
2127 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2129 else // default grid attr
2131 // use m_editor which we had decided not to use initially
2139 // we're supposed to always find something
2140 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2145 // ----------------------------------------------------------------------------
2146 // wxGridCellAttrData
2147 // ----------------------------------------------------------------------------
2149 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2151 int n
= FindIndex(row
, col
);
2152 if ( n
== wxNOT_FOUND
)
2154 // add the attribute
2155 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2159 // free the old attribute
2160 m_attrs
[(size_t)n
].attr
->DecRef();
2164 // change the attribute
2165 m_attrs
[(size_t)n
].attr
= attr
;
2169 // remove this attribute
2170 m_attrs
.RemoveAt((size_t)n
);
2175 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2177 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2179 int n
= FindIndex(row
, col
);
2180 if ( n
!= wxNOT_FOUND
)
2182 attr
= m_attrs
[(size_t)n
].attr
;
2189 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2191 size_t count
= m_attrs
.GetCount();
2192 for ( size_t n
= 0; n
< count
; n
++ )
2194 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2195 wxCoord row
= coords
.GetRow();
2196 if ((size_t)row
>= pos
)
2200 // If rows inserted, include row counter where necessary
2201 coords
.SetRow(row
+ numRows
);
2203 else if (numRows
< 0)
2205 // If rows deleted ...
2206 if ((size_t)row
>= pos
- numRows
)
2208 // ...either decrement row counter (if row still exists)...
2209 coords
.SetRow(row
+ numRows
);
2213 // ...or remove the attribute
2214 m_attrs
.RemoveAt((size_t)n
);
2222 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2224 size_t count
= m_attrs
.GetCount();
2225 for ( size_t n
= 0; n
< count
; n
++ )
2227 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2228 wxCoord col
= coords
.GetCol();
2229 if ( (size_t)col
>= pos
)
2233 // If rows inserted, include row counter where necessary
2234 coords
.SetCol(col
+ numCols
);
2236 else if (numCols
< 0)
2238 // If rows deleted ...
2239 if ((size_t)col
>= pos
- numCols
)
2241 // ...either decrement row counter (if row still exists)...
2242 coords
.SetCol(col
+ numCols
);
2246 // ...or remove the attribute
2247 m_attrs
.RemoveAt((size_t)n
);
2255 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2257 size_t count
= m_attrs
.GetCount();
2258 for ( size_t n
= 0; n
< count
; n
++ )
2260 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2261 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2270 // ----------------------------------------------------------------------------
2271 // wxGridRowOrColAttrData
2272 // ----------------------------------------------------------------------------
2274 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2276 size_t count
= m_attrs
.Count();
2277 for ( size_t n
= 0; n
< count
; n
++ )
2279 m_attrs
[n
]->DecRef();
2283 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2285 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2287 int n
= m_rowsOrCols
.Index(rowOrCol
);
2288 if ( n
!= wxNOT_FOUND
)
2290 attr
= m_attrs
[(size_t)n
];
2297 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2299 int i
= m_rowsOrCols
.Index(rowOrCol
);
2300 if ( i
== wxNOT_FOUND
)
2302 // add the attribute
2303 m_rowsOrCols
.Add(rowOrCol
);
2308 size_t n
= (size_t)i
;
2311 // change the attribute
2312 m_attrs
[n
]->DecRef();
2317 // remove this attribute
2318 m_attrs
[n
]->DecRef();
2319 m_rowsOrCols
.RemoveAt(n
);
2320 m_attrs
.RemoveAt(n
);
2325 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2327 size_t count
= m_attrs
.GetCount();
2328 for ( size_t n
= 0; n
< count
; n
++ )
2330 int & rowOrCol
= m_rowsOrCols
[n
];
2331 if ( (size_t)rowOrCol
>= pos
)
2333 if ( numRowsOrCols
> 0 )
2335 // If rows inserted, include row counter where necessary
2336 rowOrCol
+= numRowsOrCols
;
2338 else if ( numRowsOrCols
< 0)
2340 // If rows deleted, either decrement row counter (if row still exists)
2341 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2342 rowOrCol
+= numRowsOrCols
;
2345 m_rowsOrCols
.RemoveAt((size_t)n
);
2346 m_attrs
.RemoveAt((size_t)n
);
2354 // ----------------------------------------------------------------------------
2355 // wxGridCellAttrProvider
2356 // ----------------------------------------------------------------------------
2358 wxGridCellAttrProvider::wxGridCellAttrProvider()
2360 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2363 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2368 void wxGridCellAttrProvider::InitData()
2370 m_data
= new wxGridCellAttrProviderData
;
2373 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2374 wxGridCellAttr::wxAttrKind kind
) const
2376 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2381 case (wxGridCellAttr::Any
):
2382 //Get cached merge attributes.
2383 // Currenlty not used as no cache implemented as not mutiable
2384 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2387 //Basicaly implement old version.
2388 //Also check merge cache, so we don't have to re-merge every time..
2389 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2390 *attrrow
= (wxGridCellAttr
*)NULL
,
2391 *attrcol
= (wxGridCellAttr
*)NULL
;
2393 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2394 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2395 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2397 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2398 // Two or move are non NULL
2399 attr
= new wxGridCellAttr
;
2400 attr
->SetKind(wxGridCellAttr::Merged
);
2404 attr
->MergeWith(attrcell
);
2408 attr
->MergeWith(attrcol
);
2412 attr
->MergeWith(attrrow
);
2415 //store merge attr if cache implemented
2417 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2421 // one or none is non null return it or null.
2422 if(attrrow
) attr
= attrrow
;
2423 if(attrcol
) attr
= attrcol
;
2424 if(attrcell
) attr
= attrcell
;
2428 case (wxGridCellAttr::Cell
):
2429 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2431 case (wxGridCellAttr::Col
):
2432 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2434 case (wxGridCellAttr::Row
):
2435 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2439 // (wxGridCellAttr::Default):
2440 // (wxGridCellAttr::Merged):
2447 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2453 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2456 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2461 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2464 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2469 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2472 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2476 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2478 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2482 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2486 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2488 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2492 // ----------------------------------------------------------------------------
2493 // wxGridTypeRegistry
2494 // ----------------------------------------------------------------------------
2496 wxGridTypeRegistry::~wxGridTypeRegistry()
2498 size_t count
= m_typeinfo
.Count();
2499 for ( size_t i
= 0; i
< count
; i
++ )
2500 delete m_typeinfo
[i
];
2504 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2505 wxGridCellRenderer
* renderer
,
2506 wxGridCellEditor
* editor
)
2508 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2510 // is it already registered?
2511 int loc
= FindRegisteredDataType(typeName
);
2512 if ( loc
!= wxNOT_FOUND
)
2514 delete m_typeinfo
[loc
];
2515 m_typeinfo
[loc
] = info
;
2519 m_typeinfo
.Add(info
);
2523 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2525 size_t count
= m_typeinfo
.GetCount();
2526 for ( size_t i
= 0; i
< count
; i
++ )
2528 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2537 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2539 int index
= FindRegisteredDataType(typeName
);
2540 if ( index
== wxNOT_FOUND
)
2542 // check whether this is one of the standard ones, in which case
2543 // register it "on the fly"
2545 if ( typeName
== wxGRID_VALUE_STRING
)
2547 RegisterDataType(wxGRID_VALUE_STRING
,
2548 new wxGridCellStringRenderer
,
2549 new wxGridCellTextEditor
);
2551 #endif // wxUSE_TEXTCTRL
2553 if ( typeName
== wxGRID_VALUE_BOOL
)
2555 RegisterDataType(wxGRID_VALUE_BOOL
,
2556 new wxGridCellBoolRenderer
,
2557 new wxGridCellBoolEditor
);
2559 #endif // wxUSE_CHECKBOX
2561 if ( typeName
== wxGRID_VALUE_NUMBER
)
2563 RegisterDataType(wxGRID_VALUE_NUMBER
,
2564 new wxGridCellNumberRenderer
,
2565 new wxGridCellNumberEditor
);
2567 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2569 RegisterDataType(wxGRID_VALUE_FLOAT
,
2570 new wxGridCellFloatRenderer
,
2571 new wxGridCellFloatEditor
);
2573 #endif // wxUSE_TEXTCTRL
2575 if ( typeName
== wxGRID_VALUE_CHOICE
)
2577 RegisterDataType(wxGRID_VALUE_CHOICE
,
2578 new wxGridCellStringRenderer
,
2579 new wxGridCellChoiceEditor
);
2581 #endif // wxUSE_COMBOBOX
2586 // we get here only if just added the entry for this type, so return
2588 index
= m_typeinfo
.GetCount() - 1;
2594 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2596 int index
= FindDataType(typeName
);
2597 if ( index
== wxNOT_FOUND
)
2599 // the first part of the typename is the "real" type, anything after ':'
2600 // are the parameters for the renderer
2601 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2602 if ( index
== wxNOT_FOUND
)
2607 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2608 wxGridCellRenderer
*rendererOld
= renderer
;
2609 renderer
= renderer
->Clone();
2610 rendererOld
->DecRef();
2612 wxGridCellEditor
*editor
= GetEditor(index
);
2613 wxGridCellEditor
*editorOld
= editor
;
2614 editor
= editor
->Clone();
2615 editorOld
->DecRef();
2617 // do it even if there are no parameters to reset them to defaults
2618 wxString params
= typeName
.AfterFirst(_T(':'));
2619 renderer
->SetParameters(params
);
2620 editor
->SetParameters(params
);
2622 // register the new typename
2623 RegisterDataType(typeName
, renderer
, editor
);
2625 // we just registered it, it's the last one
2626 index
= m_typeinfo
.GetCount() - 1;
2632 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2634 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2640 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2642 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2648 // ----------------------------------------------------------------------------
2650 // ----------------------------------------------------------------------------
2652 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2655 wxGridTableBase::wxGridTableBase()
2657 m_view
= (wxGrid
*) NULL
;
2658 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2661 wxGridTableBase::~wxGridTableBase()
2663 delete m_attrProvider
;
2666 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2668 delete m_attrProvider
;
2669 m_attrProvider
= attrProvider
;
2672 bool wxGridTableBase::CanHaveAttributes()
2674 if ( ! GetAttrProvider() )
2676 // use the default attr provider by default
2677 SetAttrProvider(new wxGridCellAttrProvider
);
2682 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2684 if ( m_attrProvider
)
2685 return m_attrProvider
->GetAttr(row
, col
, kind
);
2687 return (wxGridCellAttr
*)NULL
;
2690 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2692 if ( m_attrProvider
)
2694 attr
->SetKind(wxGridCellAttr::Cell
);
2695 m_attrProvider
->SetAttr(attr
, row
, col
);
2699 // as we take ownership of the pointer and don't store it, we must
2705 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2707 if ( m_attrProvider
)
2709 attr
->SetKind(wxGridCellAttr::Row
);
2710 m_attrProvider
->SetRowAttr(attr
, row
);
2714 // as we take ownership of the pointer and don't store it, we must
2720 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2722 if ( m_attrProvider
)
2724 attr
->SetKind(wxGridCellAttr::Col
);
2725 m_attrProvider
->SetColAttr(attr
, col
);
2729 // as we take ownership of the pointer and don't store it, we must
2735 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2736 size_t WXUNUSED(numRows
) )
2738 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2743 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2745 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2750 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2751 size_t WXUNUSED(numRows
) )
2753 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2758 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2759 size_t WXUNUSED(numCols
) )
2761 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2766 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2768 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2773 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2774 size_t WXUNUSED(numCols
) )
2776 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2782 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2785 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2786 // how much it makes sense to us geeks.
2790 wxString
wxGridTableBase::GetColLabelValue( int col
)
2792 // default col labels are:
2793 // cols 0 to 25 : A-Z
2794 // cols 26 to 675 : AA-ZZ
2799 for ( n
= 1; ; n
++ )
2801 s
+= (_T('A') + (wxChar
)( col%26
));
2803 if ( col
< 0 ) break;
2806 // reverse the string...
2808 for ( i
= 0; i
< n
; i
++ )
2817 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2819 return wxGRID_VALUE_STRING
;
2822 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2823 const wxString
& typeName
)
2825 return typeName
== wxGRID_VALUE_STRING
;
2828 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2830 return CanGetValueAs(row
, col
, typeName
);
2833 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2838 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2843 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2848 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2849 long WXUNUSED(value
) )
2853 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2854 double WXUNUSED(value
) )
2858 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2859 bool WXUNUSED(value
) )
2864 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2865 const wxString
& WXUNUSED(typeName
) )
2870 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2871 const wxString
& WXUNUSED(typeName
),
2872 void* WXUNUSED(value
) )
2876 //////////////////////////////////////////////////////////////////////
2878 // Message class for the grid table to send requests and notifications
2882 wxGridTableMessage::wxGridTableMessage()
2884 m_table
= (wxGridTableBase
*) NULL
;
2890 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2891 int commandInt1
, int commandInt2
)
2895 m_comInt1
= commandInt1
;
2896 m_comInt2
= commandInt2
;
2901 //////////////////////////////////////////////////////////////////////
2903 // A basic grid table for string data. An object of this class will
2904 // created by wxGrid if you don't specify an alternative table class.
2907 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2909 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2911 wxGridStringTable::wxGridStringTable()
2916 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2921 m_data
.Alloc( numRows
);
2924 sa
.Alloc( numCols
);
2925 for ( col
= 0; col
< numCols
; col
++ )
2927 sa
.Add( wxEmptyString
);
2930 for ( row
= 0; row
< numRows
; row
++ )
2936 wxGridStringTable::~wxGridStringTable()
2940 int wxGridStringTable::GetNumberRows()
2942 return m_data
.GetCount();
2945 int wxGridStringTable::GetNumberCols()
2947 if ( m_data
.GetCount() > 0 )
2948 return m_data
[0].GetCount();
2953 wxString
wxGridStringTable::GetValue( int row
, int col
)
2955 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2956 _T("invalid row or column index in wxGridStringTable") );
2958 return m_data
[row
][col
];
2961 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2963 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2964 _T("invalid row or column index in wxGridStringTable") );
2966 m_data
[row
][col
] = value
;
2969 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2971 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2972 _T("invalid row or column index in wxGridStringTable") );
2974 return (m_data
[row
][col
] == wxEmptyString
);
2977 void wxGridStringTable::Clear()
2980 int numRows
, numCols
;
2982 numRows
= m_data
.GetCount();
2985 numCols
= m_data
[0].GetCount();
2987 for ( row
= 0; row
< numRows
; row
++ )
2989 for ( col
= 0; col
< numCols
; col
++ )
2991 m_data
[row
][col
] = wxEmptyString
;
2998 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3002 size_t curNumRows
= m_data
.GetCount();
3003 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3004 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3006 if ( pos
>= curNumRows
)
3008 return AppendRows( numRows
);
3012 sa
.Alloc( curNumCols
);
3013 for ( col
= 0; col
< curNumCols
; col
++ )
3015 sa
.Add( wxEmptyString
);
3018 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
3020 m_data
.Insert( sa
, row
);
3024 wxGridTableMessage
msg( this,
3025 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3029 GetView()->ProcessTableMessage( msg
);
3035 bool wxGridStringTable::AppendRows( size_t numRows
)
3039 size_t curNumRows
= m_data
.GetCount();
3040 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3041 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3044 if ( curNumCols
> 0 )
3046 sa
.Alloc( curNumCols
);
3047 for ( col
= 0; col
< curNumCols
; col
++ )
3049 sa
.Add( wxEmptyString
);
3053 for ( row
= 0; row
< numRows
; row
++ )
3060 wxGridTableMessage
msg( this,
3061 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3064 GetView()->ProcessTableMessage( msg
);
3070 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3074 size_t curNumRows
= m_data
.GetCount();
3076 if ( pos
>= curNumRows
)
3079 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3080 pos
, numRows
, curNumRows
);
3081 wxFAIL_MSG( errmsg
);
3085 if ( numRows
> curNumRows
- pos
)
3087 numRows
= curNumRows
- pos
;
3090 if ( numRows
>= curNumRows
)
3092 m_data
.Empty(); // don't release memory just yet
3096 for ( n
= 0; n
< numRows
; n
++ )
3098 m_data
.RemoveAt( pos
);
3103 wxGridTableMessage
msg( this,
3104 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3108 GetView()->ProcessTableMessage( msg
);
3114 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3118 size_t curNumRows
= m_data
.GetCount();
3119 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3120 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3122 if ( pos
>= curNumCols
)
3124 return AppendCols( numCols
);
3127 for ( row
= 0; row
< curNumRows
; row
++ )
3129 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3131 m_data
[row
].Insert( wxEmptyString
, col
);
3136 wxGridTableMessage
msg( this,
3137 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3141 GetView()->ProcessTableMessage( msg
);
3147 bool wxGridStringTable::AppendCols( size_t numCols
)
3151 size_t curNumRows
= m_data
.GetCount();
3155 // TODO: something better than this ?
3157 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3162 for ( row
= 0; row
< curNumRows
; row
++ )
3164 for ( n
= 0; n
< numCols
; n
++ )
3166 m_data
[row
].Add( wxEmptyString
);
3172 wxGridTableMessage
msg( this,
3173 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3176 GetView()->ProcessTableMessage( msg
);
3182 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3186 size_t curNumRows
= m_data
.GetCount();
3187 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3188 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3190 if ( pos
>= curNumCols
)
3193 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3194 pos
, numCols
, curNumCols
);
3195 wxFAIL_MSG( errmsg
);
3199 if ( numCols
> curNumCols
- pos
)
3201 numCols
= curNumCols
- pos
;
3204 for ( row
= 0; row
< curNumRows
; row
++ )
3206 if ( numCols
>= curNumCols
)
3208 m_data
[row
].Clear();
3212 for ( n
= 0; n
< numCols
; n
++ )
3214 m_data
[row
].RemoveAt( pos
);
3220 wxGridTableMessage
msg( this,
3221 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3225 GetView()->ProcessTableMessage( msg
);
3231 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3233 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3235 // using default label
3237 return wxGridTableBase::GetRowLabelValue( row
);
3241 return m_rowLabels
[ row
];
3245 wxString
wxGridStringTable::GetColLabelValue( int col
)
3247 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3249 // using default label
3251 return wxGridTableBase::GetColLabelValue( col
);
3255 return m_colLabels
[ col
];
3259 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3261 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3263 int n
= m_rowLabels
.GetCount();
3265 for ( i
= n
; i
<= row
; i
++ )
3267 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3271 m_rowLabels
[row
] = value
;
3274 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3276 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3278 int n
= m_colLabels
.GetCount();
3280 for ( i
= n
; i
<= col
; i
++ )
3282 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3286 m_colLabels
[col
] = value
;
3291 //////////////////////////////////////////////////////////////////////
3292 //////////////////////////////////////////////////////////////////////
3294 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3296 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3297 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3298 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3299 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3300 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3301 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3304 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3306 const wxPoint
&pos
, const wxSize
&size
)
3307 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3312 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3316 // NO - don't do this because it will set both the x and y origin
3317 // coords to match the parent scrolled window and we just want to
3318 // set the y coord - MB
3320 // m_owner->PrepareDC( dc );
3323 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3324 dc
.SetDeviceOrigin( 0, -y
);
3326 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3327 m_owner
->DrawRowLabels( dc
, rows
);
3331 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3333 m_owner
->ProcessRowLabelMouseEvent( event
);
3337 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3339 m_owner
->GetEventHandler()->ProcessEvent(event
);
3343 // This seems to be required for wxMotif otherwise the mouse
3344 // cursor must be in the cell edit control to get key events
3346 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3348 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3351 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3353 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3358 //////////////////////////////////////////////////////////////////////
3360 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3362 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3363 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3364 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3365 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3366 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3367 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3370 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3372 const wxPoint
&pos
, const wxSize
&size
)
3373 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3378 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3382 // NO - don't do this because it will set both the x and y origin
3383 // coords to match the parent scrolled window and we just want to
3384 // set the x coord - MB
3386 // m_owner->PrepareDC( dc );
3389 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3390 dc
.SetDeviceOrigin( -x
, 0 );
3392 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3393 m_owner
->DrawColLabels( dc
, cols
);
3397 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3399 m_owner
->ProcessColLabelMouseEvent( event
);
3402 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3404 m_owner
->GetEventHandler()->ProcessEvent(event
);
3408 // This seems to be required for wxMotif otherwise the mouse
3409 // cursor must be in the cell edit control to get key events
3411 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3413 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3416 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3418 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3423 //////////////////////////////////////////////////////////////////////
3425 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3427 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3428 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3429 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3430 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3431 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3432 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3435 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3437 const wxPoint
&pos
, const wxSize
&size
)
3438 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3443 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3447 int client_height
= 0;
3448 int client_width
= 0;
3449 GetClientSize( &client_width
, &client_height
);
3451 dc
.SetPen( *wxBLACK_PEN
);
3452 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3453 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3455 dc
.SetPen( *wxWHITE_PEN
);
3456 dc
.DrawLine( 0, 0, client_width
, 0 );
3457 dc
.DrawLine( 0, 0, 0, client_height
);
3461 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3463 m_owner
->ProcessCornerLabelMouseEvent( event
);
3467 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3469 m_owner
->GetEventHandler()->ProcessEvent(event
);
3472 // This seems to be required for wxMotif otherwise the mouse
3473 // cursor must be in the cell edit control to get key events
3475 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3477 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3480 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3482 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3487 //////////////////////////////////////////////////////////////////////
3489 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3491 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3492 EVT_PAINT( wxGridWindow::OnPaint
)
3493 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3494 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3495 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3496 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3497 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3500 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3501 wxGridRowLabelWindow
*rowLblWin
,
3502 wxGridColLabelWindow
*colLblWin
,
3503 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3504 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3507 m_rowLabelWin
= rowLblWin
;
3508 m_colLabelWin
= colLblWin
;
3509 SetBackgroundColour(_T("WHITE"));
3513 wxGridWindow::~wxGridWindow()
3518 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3520 wxPaintDC
dc( this );
3521 m_owner
->PrepareDC( dc
);
3522 wxRegion reg
= GetUpdateRegion();
3523 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3524 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3525 #if WXGRID_DRAW_LINES
3526 m_owner
->DrawAllGridLines( dc
, reg
);
3528 m_owner
->DrawGridSpace( dc
);
3529 m_owner
->DrawHighlight( dc
, DirtyCells
);
3533 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3535 wxWindow::ScrollWindow( dx
, dy
, rect
);
3536 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3537 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3541 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3543 m_owner
->ProcessGridCellMouseEvent( event
);
3546 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3548 m_owner
->GetEventHandler()->ProcessEvent(event
);
3551 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3552 // cursor must be in the cell edit control to get key events
3554 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3556 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3559 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3561 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3564 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3569 //////////////////////////////////////////////////////////////////////
3572 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3574 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3575 EVT_PAINT( wxGrid::OnPaint
)
3576 EVT_SIZE( wxGrid::OnSize
)
3577 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3578 EVT_KEY_UP( wxGrid::OnKeyUp
)
3579 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3582 wxGrid::wxGrid( wxWindow
*parent
,
3587 const wxString
& name
)
3588 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3589 m_colMinWidths(GRID_HASH_SIZE
),
3590 m_rowMinHeights(GRID_HASH_SIZE
)
3598 // Must do this or ~wxScrollHelper will pop the wrong event handler
3599 SetTargetWindow(this);
3601 wxSafeDecRef(m_defaultCellAttr
);
3603 #ifdef DEBUG_ATTR_CACHE
3604 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3605 wxPrintf(_T("wxGrid attribute cache statistics: "
3606 "total: %u, hits: %u (%u%%)\n"),
3607 total
, gs_nAttrCacheHits
,
3608 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3614 delete m_typeRegistry
;
3620 // ----- internal init and update functions
3623 void wxGrid::Create()
3625 m_created
= FALSE
; // set to TRUE by CreateGrid
3627 m_table
= (wxGridTableBase
*) NULL
;
3630 m_cellEditCtrlEnabled
= FALSE
;
3632 m_defaultCellAttr
= new wxGridCellAttr();
3634 // Set default cell attributes
3635 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3636 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3637 m_defaultCellAttr
->SetFont(GetFont());
3638 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3639 m_defaultCellAttr
->SetTextColour(
3640 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3641 m_defaultCellAttr
->SetBackgroundColour(
3642 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3643 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3644 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3649 m_currentCellCoords
= wxGridNoCellCoords
;
3651 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3652 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3654 // create the type registry
3655 m_typeRegistry
= new wxGridTypeRegistry
;
3657 // subwindow components that make up the wxGrid
3658 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3663 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3668 m_colLabelWin
= new wxGridColLabelWindow( this,
3673 m_gridWin
= new wxGridWindow( this,
3680 SetTargetWindow( m_gridWin
);
3686 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3687 wxGrid::wxGridSelectionModes selmode
)
3689 wxCHECK_MSG( !m_created
,
3691 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3693 m_numRows
= numRows
;
3694 m_numCols
= numCols
;
3696 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3697 m_table
->SetView( this );
3699 m_selection
= new wxGridSelection( this, selmode
);
3708 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3710 wxCHECK_RET( m_created
,
3711 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3713 m_selection
->SetSelectionMode( selmode
);
3716 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3717 wxGrid::wxGridSelectionModes selmode
)
3721 // RD: Actually, this should probably be allowed. I think it would be
3722 // nice to be able to switch multiple Tables in and out of a single
3723 // View at runtime. Is there anything in the implmentation that would
3726 // At least, you now have to cope with m_selection
3727 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3732 m_numRows
= table
->GetNumberRows();
3733 m_numCols
= table
->GetNumberCols();
3736 m_table
->SetView( this );
3739 m_selection
= new wxGridSelection( this, selmode
);
3752 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3753 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3755 if ( m_rowLabelWin
)
3757 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3761 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3764 m_labelTextColour
= wxColour( _T("BLACK") );
3767 m_attrCache
.row
= -1;
3769 // TODO: something better than this ?
3771 m_labelFont
= this->GetFont();
3772 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3774 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3775 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3777 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3778 m_colLabelVertAlign
= wxALIGN_TOP
;
3780 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3781 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3783 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3784 m_defaultRowHeight
+= 8;
3786 m_defaultRowHeight
+= 4;
3789 m_gridLineColour
= wxColour( 128, 128, 255 );
3790 m_gridLinesEnabled
= TRUE
;
3791 m_cellHighlightColour
= m_gridLineColour
;
3792 m_cellHighlightPenWidth
= 2;
3793 m_cellHighlightROPenWidth
= 1;
3795 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3796 m_winCapture
= (wxWindow
*)NULL
;
3797 m_canDragRowSize
= TRUE
;
3798 m_canDragColSize
= TRUE
;
3799 m_canDragGridSize
= TRUE
;
3801 m_dragRowOrCol
= -1;
3802 m_isDragging
= FALSE
;
3803 m_startDragPos
= wxDefaultPosition
;
3805 m_waitForSlowClick
= FALSE
;
3807 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3808 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3810 m_currentCellCoords
= wxGridNoCellCoords
;
3812 m_selectingTopLeft
= wxGridNoCellCoords
;
3813 m_selectingBottomRight
= wxGridNoCellCoords
;
3814 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3815 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3817 m_editable
= TRUE
; // default for whole grid
3819 m_inOnKeyDown
= FALSE
;
3826 // ----------------------------------------------------------------------------
3827 // the idea is to call these functions only when necessary because they create
3828 // quite big arrays which eat memory mostly unnecessary - in particular, if
3829 // default widths/heights are used for all rows/columns, we may not use these
3832 // with some extra code, it should be possible to only store the
3833 // widths/heights different from default ones but this will be done later...
3834 // ----------------------------------------------------------------------------
3836 void wxGrid::InitRowHeights()
3838 m_rowHeights
.Empty();
3839 m_rowBottoms
.Empty();
3841 m_rowHeights
.Alloc( m_numRows
);
3842 m_rowBottoms
.Alloc( m_numRows
);
3845 for ( int i
= 0; i
< m_numRows
; i
++ )
3847 m_rowHeights
.Add( m_defaultRowHeight
);
3848 rowBottom
+= m_defaultRowHeight
;
3849 m_rowBottoms
.Add( rowBottom
);
3853 void wxGrid::InitColWidths()
3855 m_colWidths
.Empty();
3856 m_colRights
.Empty();
3858 m_colWidths
.Alloc( m_numCols
);
3859 m_colRights
.Alloc( m_numCols
);
3861 for ( int i
= 0; i
< m_numCols
; i
++ )
3863 m_colWidths
.Add( m_defaultColWidth
);
3864 colRight
+= m_defaultColWidth
;
3865 m_colRights
.Add( colRight
);
3869 int wxGrid::GetColWidth(int col
) const
3871 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3874 int wxGrid::GetColLeft(int col
) const
3876 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3877 : m_colRights
[col
] - m_colWidths
[col
];
3880 int wxGrid::GetColRight(int col
) const
3882 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3886 int wxGrid::GetRowHeight(int row
) const
3888 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3891 int wxGrid::GetRowTop(int row
) const
3893 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3894 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3897 int wxGrid::GetRowBottom(int row
) const
3899 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3900 : m_rowBottoms
[row
];
3903 void wxGrid::CalcDimensions()
3906 GetClientSize( &cw
, &ch
);
3908 if ( m_rowLabelWin
->IsShown() )
3909 cw
-= m_rowLabelWidth
;
3910 if ( m_colLabelWin
->IsShown() )
3911 ch
-= m_colLabelHeight
;
3914 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3915 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3917 // preserve (more or less) the previous position
3919 GetViewStart( &x
, &y
);
3921 // maybe we don't need scrollbars at all?
3923 // also adjust the position to be valid for the new scroll rangs
3944 // do set scrollbar parameters
3945 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
3946 GetScrollX(w
), GetScrollY(h
), x
, y
,
3947 GetBatchCount() != 0);
3951 void wxGrid::CalcWindowSizes()
3954 GetClientSize( &cw
, &ch
);
3956 if ( m_cornerLabelWin
->IsShown() )
3957 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3959 if ( m_colLabelWin
->IsShown() )
3960 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3962 if ( m_rowLabelWin
->IsShown() )
3963 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3965 if ( m_gridWin
->IsShown() )
3966 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3970 // this is called when the grid table sends a message to say that it
3971 // has been redimensioned
3973 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3976 bool result
= FALSE
;
3979 // if we were using the default widths/heights so far, we must change them
3981 if ( m_colWidths
.IsEmpty() )
3986 if ( m_rowHeights
.IsEmpty() )
3992 switch ( msg
.GetId() )
3994 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3996 size_t pos
= msg
.GetCommandInt();
3997 int numRows
= msg
.GetCommandInt2();
3999 m_numRows
+= numRows
;
4001 if ( !m_rowHeights
.IsEmpty() )
4003 for ( i
= 0; i
< numRows
; i
++ )
4005 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
4006 m_rowBottoms
.Insert( 0, pos
);
4010 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4012 for ( i
= pos
; i
< m_numRows
; i
++ )
4014 bottom
+= m_rowHeights
[i
];
4015 m_rowBottoms
[i
] = bottom
;
4018 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4020 // if we have just inserted cols into an empty grid the current
4021 // cell will be undefined...
4023 SetCurrentCell( 0, 0 );
4025 m_selection
->UpdateRows( pos
, numRows
);
4026 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4028 attrProvider
->UpdateAttrRows( pos
, numRows
);
4030 if ( !GetBatchCount() )
4033 m_rowLabelWin
->Refresh();
4039 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4041 int numRows
= msg
.GetCommandInt();
4042 int oldNumRows
= m_numRows
;
4043 m_numRows
+= numRows
;
4045 if ( !m_rowHeights
.IsEmpty() )
4047 for ( i
= 0; i
< numRows
; i
++ )
4049 m_rowHeights
.Add( m_defaultRowHeight
);
4050 m_rowBottoms
.Add( 0 );
4054 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4056 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4058 bottom
+= m_rowHeights
[i
];
4059 m_rowBottoms
[i
] = bottom
;
4062 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4064 // if we have just inserted cols into an empty grid the current
4065 // cell will be undefined...
4067 SetCurrentCell( 0, 0 );
4069 if ( !GetBatchCount() )
4072 m_rowLabelWin
->Refresh();
4078 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4080 size_t pos
= msg
.GetCommandInt();
4081 int numRows
= msg
.GetCommandInt2();
4082 m_numRows
-= numRows
;
4084 if ( !m_rowHeights
.IsEmpty() )
4086 for ( i
= 0; i
< numRows
; i
++ )
4088 m_rowHeights
.RemoveAt( pos
);
4089 m_rowBottoms
.RemoveAt( pos
);
4093 for ( i
= 0; i
< m_numRows
; i
++ )
4095 h
+= m_rowHeights
[i
];
4096 m_rowBottoms
[i
] = h
;
4101 m_currentCellCoords
= wxGridNoCellCoords
;
4105 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4106 m_currentCellCoords
.Set( 0, 0 );
4108 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4109 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4111 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4112 // ifdef'd out following patch from Paul Gammans
4114 // No need to touch column attributes, unless we
4115 // removed _all_ rows, in this case, we remove
4116 // all column attributes.
4117 // I hate to do this here, but the
4118 // needed data is not available inside UpdateAttrRows.
4119 if ( !GetNumberRows() )
4120 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4123 if ( !GetBatchCount() )
4126 m_rowLabelWin
->Refresh();
4132 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4134 size_t pos
= msg
.GetCommandInt();
4135 int numCols
= msg
.GetCommandInt2();
4136 m_numCols
+= numCols
;
4138 if ( !m_colWidths
.IsEmpty() )
4140 for ( i
= 0; i
< numCols
; i
++ )
4142 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4143 m_colRights
.Insert( 0, pos
);
4147 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4149 for ( i
= pos
; i
< m_numCols
; i
++ )
4151 right
+= m_colWidths
[i
];
4152 m_colRights
[i
] = right
;
4155 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4157 // if we have just inserted cols into an empty grid the current
4158 // cell will be undefined...
4160 SetCurrentCell( 0, 0 );
4162 m_selection
->UpdateCols( pos
, numCols
);
4163 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4165 attrProvider
->UpdateAttrCols( pos
, numCols
);
4166 if ( !GetBatchCount() )
4169 m_colLabelWin
->Refresh();
4176 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4178 int numCols
= msg
.GetCommandInt();
4179 int oldNumCols
= m_numCols
;
4180 m_numCols
+= numCols
;
4181 if ( !m_colWidths
.IsEmpty() )
4183 for ( i
= 0; i
< numCols
; i
++ )
4185 m_colWidths
.Add( m_defaultColWidth
);
4186 m_colRights
.Add( 0 );
4190 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4192 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4194 right
+= m_colWidths
[i
];
4195 m_colRights
[i
] = right
;
4198 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4200 // if we have just inserted cols into an empty grid the current
4201 // cell will be undefined...
4203 SetCurrentCell( 0, 0 );
4205 if ( !GetBatchCount() )
4208 m_colLabelWin
->Refresh();
4214 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4216 size_t pos
= msg
.GetCommandInt();
4217 int numCols
= msg
.GetCommandInt2();
4218 m_numCols
-= numCols
;
4220 if ( !m_colWidths
.IsEmpty() )
4222 for ( i
= 0; i
< numCols
; i
++ )
4224 m_colWidths
.RemoveAt( pos
);
4225 m_colRights
.RemoveAt( pos
);
4229 for ( i
= 0; i
< m_numCols
; i
++ )
4231 w
+= m_colWidths
[i
];
4237 m_currentCellCoords
= wxGridNoCellCoords
;
4241 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4242 m_currentCellCoords
.Set( 0, 0 );
4244 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4245 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4247 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4248 // ifdef'd out following patch from Paul Gammans
4250 // No need to touch row attributes, unless we
4251 // removed _all_ columns, in this case, we remove
4252 // all row attributes.
4253 // I hate to do this here, but the
4254 // needed data is not available inside UpdateAttrCols.
4255 if ( !GetNumberCols() )
4256 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4259 if ( !GetBatchCount() )
4262 m_colLabelWin
->Refresh();
4269 if (result
&& !GetBatchCount() )
4270 m_gridWin
->Refresh();
4275 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4277 wxRegionIterator
iter( reg
);
4280 wxArrayInt rowlabels
;
4287 // TODO: remove this when we can...
4288 // There is a bug in wxMotif that gives garbage update
4289 // rectangles if you jump-scroll a long way by clicking the
4290 // scrollbar with middle button. This is a work-around
4292 #if defined(__WXMOTIF__)
4294 m_gridWin
->GetClientSize( &cw
, &ch
);
4295 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4296 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4299 // logical bounds of update region
4302 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4303 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4305 // find the row labels within these bounds
4308 for ( row
= 0; row
< m_numRows
; row
++ )
4310 if ( GetRowBottom(row
) < top
)
4313 if ( GetRowTop(row
) > bottom
)
4316 rowlabels
.Add( row
);
4326 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4328 wxRegionIterator
iter( reg
);
4331 wxArrayInt colLabels
;
4338 // TODO: remove this when we can...
4339 // There is a bug in wxMotif that gives garbage update
4340 // rectangles if you jump-scroll a long way by clicking the
4341 // scrollbar with middle button. This is a work-around
4343 #if defined(__WXMOTIF__)
4345 m_gridWin
->GetClientSize( &cw
, &ch
);
4346 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4347 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4350 // logical bounds of update region
4353 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4354 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4356 // find the cells within these bounds
4359 for ( col
= 0; col
< m_numCols
; col
++ )
4361 if ( GetColRight(col
) < left
)
4364 if ( GetColLeft(col
) > right
)
4367 colLabels
.Add( col
);
4376 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4378 wxRegionIterator
iter( reg
);
4381 wxGridCellCoordsArray cellsExposed
;
4383 int left
, top
, right
, bottom
;
4388 // TODO: remove this when we can...
4389 // There is a bug in wxMotif that gives garbage update
4390 // rectangles if you jump-scroll a long way by clicking the
4391 // scrollbar with middle button. This is a work-around
4393 #if defined(__WXMOTIF__)
4395 m_gridWin
->GetClientSize( &cw
, &ch
);
4396 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4397 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4398 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4399 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4402 // logical bounds of update region
4404 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4405 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4407 // find the cells within these bounds
4410 for ( row
= 0; row
< m_numRows
; row
++ )
4412 if ( GetRowBottom(row
) <= top
)
4415 if ( GetRowTop(row
) > bottom
)
4419 for ( col
= 0; col
< m_numCols
; col
++ )
4421 if ( GetColRight(col
) <= left
)
4424 if ( GetColLeft(col
) > right
)
4427 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4434 return cellsExposed
;
4438 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4441 wxPoint
pos( event
.GetPosition() );
4442 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4444 if ( event
.Dragging() )
4446 m_isDragging
= TRUE
;
4448 if ( event
.LeftIsDown() )
4450 switch( m_cursorMode
)
4452 case WXGRID_CURSOR_RESIZE_ROW
:
4454 int cw
, ch
, left
, dummy
;
4455 m_gridWin
->GetClientSize( &cw
, &ch
);
4456 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4458 wxClientDC
dc( m_gridWin
);
4461 GetRowTop(m_dragRowOrCol
) +
4462 GetRowMinimalHeight(m_dragRowOrCol
) );
4463 dc
.SetLogicalFunction(wxINVERT
);
4464 if ( m_dragLastPos
>= 0 )
4466 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4468 dc
.DrawLine( left
, y
, left
+cw
, y
);
4473 case WXGRID_CURSOR_SELECT_ROW
:
4474 if ( (row
= YToRow( y
)) >= 0 )
4476 m_selection
->SelectRow( row
,
4477 event
.ControlDown(),
4483 // default label to suppress warnings about "enumeration value
4484 // 'xxx' not handled in switch
4492 m_isDragging
= FALSE
;
4495 // ------------ Entering or leaving the window
4497 if ( event
.Entering() || event
.Leaving() )
4499 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4503 // ------------ Left button pressed
4505 else if ( event
.LeftDown() )
4507 // don't send a label click event for a hit on the
4508 // edge of the row label - this is probably the user
4509 // wanting to resize the row
4511 if ( YToEdgeOfRow(y
) < 0 )
4515 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4517 if ( !event
.ShiftDown() && !event
.ControlDown() )
4519 if ( event
.ShiftDown() )
4520 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4523 GetNumberCols() - 1,
4524 event
.ControlDown(),
4529 m_selection
->SelectRow( row
,
4530 event
.ControlDown(),
4534 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4539 // starting to drag-resize a row
4541 if ( CanDragRowSize() )
4542 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4547 // ------------ Left double click
4549 else if (event
.LeftDClick() )
4551 if ( YToEdgeOfRow(y
) < 0 )
4554 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4559 // ------------ Left button released
4561 else if ( event
.LeftUp() )
4563 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4565 DoEndDragResizeRow();
4567 // Note: we are ending the event *after* doing
4568 // default processing in this case
4570 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4573 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4578 // ------------ Right button down
4580 else if ( event
.RightDown() )
4583 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4585 // no default action at the moment
4590 // ------------ Right double click
4592 else if ( event
.RightDClick() )
4595 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4597 // no default action at the moment
4602 // ------------ No buttons down and mouse moving
4604 else if ( event
.Moving() )
4606 m_dragRowOrCol
= YToEdgeOfRow( y
);
4607 if ( m_dragRowOrCol
>= 0 )
4609 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4611 // don't capture the mouse yet
4612 if ( CanDragRowSize() )
4613 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4616 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4618 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4624 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4627 wxPoint
pos( event
.GetPosition() );
4628 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4630 if ( event
.Dragging() )
4632 m_isDragging
= TRUE
;
4634 if ( event
.LeftIsDown() )
4636 switch( m_cursorMode
)
4638 case WXGRID_CURSOR_RESIZE_COL
:
4640 int cw
, ch
, dummy
, top
;
4641 m_gridWin
->GetClientSize( &cw
, &ch
);
4642 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4644 wxClientDC
dc( m_gridWin
);
4647 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4648 GetColMinimalWidth(m_dragRowOrCol
));
4649 dc
.SetLogicalFunction(wxINVERT
);
4650 if ( m_dragLastPos
>= 0 )
4652 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4654 dc
.DrawLine( x
, top
, x
, top
+ch
);
4659 case WXGRID_CURSOR_SELECT_COL
:
4660 if ( (col
= XToCol( x
)) >= 0 )
4662 m_selection
->SelectCol( col
,
4663 event
.ControlDown(),
4669 // default label to suppress warnings about "enumeration value
4670 // 'xxx' not handled in switch
4678 m_isDragging
= FALSE
;
4681 // ------------ Entering or leaving the window
4683 if ( event
.Entering() || event
.Leaving() )
4685 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4689 // ------------ Left button pressed
4691 else if ( event
.LeftDown() )
4693 // don't send a label click event for a hit on the
4694 // edge of the col label - this is probably the user
4695 // wanting to resize the col
4697 if ( XToEdgeOfCol(x
) < 0 )
4701 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4703 if ( !event
.ShiftDown() && !event
.ControlDown() )
4705 if ( event
.ShiftDown() )
4706 m_selection
->SelectBlock( 0,
4707 m_currentCellCoords
.GetCol(),
4708 GetNumberRows() - 1, col
,
4709 event
.ControlDown(),
4714 m_selection
->SelectCol( col
,
4715 event
.ControlDown(),
4719 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4724 // starting to drag-resize a col
4726 if ( CanDragColSize() )
4727 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4732 // ------------ Left double click
4734 if ( event
.LeftDClick() )
4736 if ( XToEdgeOfCol(x
) < 0 )
4739 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4744 // ------------ Left button released
4746 else if ( event
.LeftUp() )
4748 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4750 DoEndDragResizeCol();
4752 // Note: we are ending the event *after* doing
4753 // default processing in this case
4755 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4758 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4763 // ------------ Right button down
4765 else if ( event
.RightDown() )
4768 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4770 // no default action at the moment
4775 // ------------ Right double click
4777 else if ( event
.RightDClick() )
4780 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4782 // no default action at the moment
4787 // ------------ No buttons down and mouse moving
4789 else if ( event
.Moving() )
4791 m_dragRowOrCol
= XToEdgeOfCol( x
);
4792 if ( m_dragRowOrCol
>= 0 )
4794 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4796 // don't capture the cursor yet
4797 if ( CanDragColSize() )
4798 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4801 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4803 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4809 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4811 if ( event
.LeftDown() )
4813 // indicate corner label by having both row and
4816 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4822 else if ( event
.LeftDClick() )
4824 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4827 else if ( event
.RightDown() )
4829 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4831 // no default action at the moment
4835 else if ( event
.RightDClick() )
4837 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4839 // no default action at the moment
4844 void wxGrid::ChangeCursorMode(CursorMode mode
,
4849 static const wxChar
*cursorModes
[] =
4858 wxLogTrace(_T("grid"),
4859 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4860 win
== m_colLabelWin
? _T("colLabelWin")
4861 : win
? _T("rowLabelWin")
4863 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4864 #endif // __WXDEBUG__
4866 if ( mode
== m_cursorMode
&&
4867 win
== m_winCapture
&&
4868 captureMouse
== (m_winCapture
!= NULL
))
4873 // by default use the grid itself
4879 m_winCapture
->ReleaseMouse();
4880 m_winCapture
= (wxWindow
*)NULL
;
4883 m_cursorMode
= mode
;
4885 switch ( m_cursorMode
)
4887 case WXGRID_CURSOR_RESIZE_ROW
:
4888 win
->SetCursor( m_rowResizeCursor
);
4891 case WXGRID_CURSOR_RESIZE_COL
:
4892 win
->SetCursor( m_colResizeCursor
);
4896 win
->SetCursor( *wxSTANDARD_CURSOR
);
4899 // we need to capture mouse when resizing
4900 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4901 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4903 if ( captureMouse
&& resize
)
4905 win
->CaptureMouse();
4910 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4913 wxPoint
pos( event
.GetPosition() );
4914 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4916 wxGridCellCoords coords
;
4917 XYToCell( x
, y
, coords
);
4919 if ( event
.Dragging() )
4921 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4923 // Don't start doing anything until the mouse has been drug at
4924 // least 3 pixels in any direction...
4927 if (m_startDragPos
== wxDefaultPosition
)
4929 m_startDragPos
= pos
;
4932 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4936 m_isDragging
= TRUE
;
4937 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4939 // Hide the edit control, so it
4940 // won't interfer with drag-shrinking.
4941 if ( IsCellEditControlShown() )
4943 HideCellEditControl();
4944 SaveEditControlValue();
4947 // Have we captured the mouse yet?
4950 m_winCapture
= m_gridWin
;
4951 m_winCapture
->CaptureMouse();
4954 if ( coords
!= wxGridNoCellCoords
)
4956 if ( event
.ControlDown() )
4958 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4959 m_selectingKeyboard
= coords
;
4960 HighlightBlock ( m_selectingKeyboard
, coords
);
4964 if ( !IsSelection() )
4966 HighlightBlock( coords
, coords
);
4970 HighlightBlock( m_currentCellCoords
, coords
);
4974 if (! IsVisible(coords
))
4976 MakeCellVisible(coords
);
4977 // TODO: need to introduce a delay or something here. The
4978 // scrolling is way to fast, at least on MSW - also on GTK.
4982 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4984 int cw
, ch
, left
, dummy
;
4985 m_gridWin
->GetClientSize( &cw
, &ch
);
4986 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4988 wxClientDC
dc( m_gridWin
);
4990 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4991 GetRowMinimalHeight(m_dragRowOrCol
) );
4992 dc
.SetLogicalFunction(wxINVERT
);
4993 if ( m_dragLastPos
>= 0 )
4995 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4997 dc
.DrawLine( left
, y
, left
+cw
, y
);
5000 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5002 int cw
, ch
, dummy
, top
;
5003 m_gridWin
->GetClientSize( &cw
, &ch
);
5004 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5006 wxClientDC
dc( m_gridWin
);
5008 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5009 GetColMinimalWidth(m_dragRowOrCol
) );
5010 dc
.SetLogicalFunction(wxINVERT
);
5011 if ( m_dragLastPos
>= 0 )
5013 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5015 dc
.DrawLine( x
, top
, x
, top
+ch
);
5022 m_isDragging
= FALSE
;
5023 m_startDragPos
= wxDefaultPosition
;
5025 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5026 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5029 if ( event
.Entering() || event
.Leaving() )
5031 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5032 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5037 // ------------ Left button pressed
5039 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5041 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5046 if ( !event
.ControlDown() )
5048 if ( event
.ShiftDown() )
5050 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5051 m_currentCellCoords
.GetCol(),
5054 event
.ControlDown(),
5059 else if ( XToEdgeOfCol(x
) < 0 &&
5060 YToEdgeOfRow(y
) < 0 )
5062 DisableCellEditControl();
5063 MakeCellVisible( coords
);
5065 // if this is the second click on this cell then start
5067 if ( m_waitForSlowClick
&&
5068 (coords
== m_currentCellCoords
) &&
5069 CanEnableCellControl())
5071 EnableCellEditControl();
5073 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5074 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5077 editor
->StartingClick();
5081 m_waitForSlowClick
= FALSE
;
5085 if ( event
.ControlDown() )
5087 m_selection
->ToggleCellSelection( coords
.GetRow(),
5089 event
.ControlDown(),
5093 m_selectingTopLeft
= wxGridNoCellCoords
;
5094 m_selectingBottomRight
= wxGridNoCellCoords
;
5095 m_selectingKeyboard
= coords
;
5099 SetCurrentCell( coords
);
5100 if ( m_selection
->GetSelectionMode()
5101 != wxGrid::wxGridSelectCells
)
5102 HighlightBlock( coords
, coords
);
5104 m_waitForSlowClick
= TRUE
;
5111 // ------------ Left double click
5113 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5115 DisableCellEditControl();
5117 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5119 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5127 // ------------ Left button released
5129 else if ( event
.LeftUp() )
5131 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5133 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5134 m_selectingBottomRight
!= wxGridNoCellCoords
)
5138 m_winCapture
->ReleaseMouse();
5139 m_winCapture
= NULL
;
5141 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5142 m_selectingTopLeft
.GetCol(),
5143 m_selectingBottomRight
.GetRow(),
5144 m_selectingBottomRight
.GetCol(),
5145 event
.ControlDown(),
5149 m_selectingTopLeft
= wxGridNoCellCoords
;
5150 m_selectingBottomRight
= wxGridNoCellCoords
;
5153 // Show the edit control, if it has been hidden for
5155 ShowCellEditControl();
5157 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5159 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5160 DoEndDragResizeRow();
5162 // Note: we are ending the event *after* doing
5163 // default processing in this case
5165 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5167 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5169 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5170 DoEndDragResizeCol();
5172 // Note: we are ending the event *after* doing
5173 // default processing in this case
5175 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5182 // ------------ Right button down
5184 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5186 DisableCellEditControl();
5187 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5192 // no default action at the moment
5197 // ------------ Right double click
5199 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5201 DisableCellEditControl();
5202 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5207 // no default action at the moment
5211 // ------------ Moving and no button action
5213 else if ( event
.Moving() && !event
.IsButton() )
5215 int dragRow
= YToEdgeOfRow( y
);
5216 int dragCol
= XToEdgeOfCol( x
);
5218 // Dragging on the corner of a cell to resize in both
5219 // directions is not implemented yet...
5221 if ( dragRow
>= 0 && dragCol
>= 0 )
5223 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5229 m_dragRowOrCol
= dragRow
;
5231 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5233 if ( CanDragRowSize() && CanDragGridSize() )
5234 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5239 m_dragRowOrCol
= dragCol
;
5247 m_dragRowOrCol
= dragCol
;
5249 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5251 if ( CanDragColSize() && CanDragGridSize() )
5252 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5258 // Neither on a row or col edge
5260 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5262 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5268 void wxGrid::DoEndDragResizeRow()
5270 if ( m_dragLastPos
>= 0 )
5272 // erase the last line and resize the row
5274 int cw
, ch
, left
, dummy
;
5275 m_gridWin
->GetClientSize( &cw
, &ch
);
5276 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5278 wxClientDC
dc( m_gridWin
);
5280 dc
.SetLogicalFunction( wxINVERT
);
5281 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5282 HideCellEditControl();
5283 SaveEditControlValue();
5285 int rowTop
= GetRowTop(m_dragRowOrCol
);
5286 SetRowSize( m_dragRowOrCol
,
5287 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5289 if ( !GetBatchCount() )
5291 // Only needed to get the correct rect.y:
5292 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5294 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5295 rect
.width
= m_rowLabelWidth
;
5296 rect
.height
= ch
- rect
.y
;
5297 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5299 m_gridWin
->Refresh( FALSE
, &rect
);
5302 ShowCellEditControl();
5307 void wxGrid::DoEndDragResizeCol()
5309 if ( m_dragLastPos
>= 0 )
5311 // erase the last line and resize the col
5313 int cw
, ch
, dummy
, top
;
5314 m_gridWin
->GetClientSize( &cw
, &ch
);
5315 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5317 wxClientDC
dc( m_gridWin
);
5319 dc
.SetLogicalFunction( wxINVERT
);
5320 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5321 HideCellEditControl();
5322 SaveEditControlValue();
5324 int colLeft
= GetColLeft(m_dragRowOrCol
);
5325 SetColSize( m_dragRowOrCol
,
5326 wxMax( m_dragLastPos
- colLeft
,
5327 GetColMinimalWidth(m_dragRowOrCol
) ) );
5329 if ( !GetBatchCount() )
5331 // Only needed to get the correct rect.x:
5332 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5334 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5335 rect
.width
= cw
- rect
.x
;
5336 rect
.height
= m_colLabelHeight
;
5337 m_colLabelWin
->Refresh( TRUE
, &rect
);
5339 m_gridWin
->Refresh( FALSE
, &rect
);
5342 ShowCellEditControl();
5349 // ------ interaction with data model
5351 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5353 switch ( msg
.GetId() )
5355 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5356 return GetModelValues();
5358 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5359 return SetModelValues();
5361 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5362 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5363 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5364 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5365 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5366 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5367 return Redimension( msg
);
5376 // The behaviour of this function depends on the grid table class
5377 // Clear() function. For the default wxGridStringTable class the
5378 // behavious is to replace all cell contents with wxEmptyString but
5379 // not to change the number of rows or cols.
5381 void wxGrid::ClearGrid()
5385 if (IsCellEditControlEnabled())
5386 DisableCellEditControl();
5389 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5394 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5396 // TODO: something with updateLabels flag
5400 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5406 if (IsCellEditControlEnabled())
5407 DisableCellEditControl();
5409 return m_table
->InsertRows( pos
, numRows
);
5411 // the table will have sent the results of the insert row
5412 // operation to this view object as a grid table message
5418 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5420 // TODO: something with updateLabels flag
5424 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5428 return ( m_table
&& m_table
->AppendRows( numRows
) );
5429 // the table will have sent the results of the append row
5430 // operation to this view object as a grid table message
5434 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5436 // TODO: something with updateLabels flag
5440 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5446 if (IsCellEditControlEnabled())
5447 DisableCellEditControl();
5449 return (m_table
->DeleteRows( pos
, numRows
));
5450 // the table will have sent the results of the delete row
5451 // operation to this view object as a grid table message
5457 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5459 // TODO: something with updateLabels flag
5463 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5469 if (IsCellEditControlEnabled())
5470 DisableCellEditControl();
5472 return m_table
->InsertCols( pos
, numCols
);
5473 // the table will have sent the results of the insert col
5474 // operation to this view object as a grid table message
5480 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5482 // TODO: something with updateLabels flag
5486 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5490 return ( m_table
&& m_table
->AppendCols( numCols
) );
5491 // the table will have sent the results of the append col
5492 // operation to this view object as a grid table message
5496 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5498 // TODO: something with updateLabels flag
5502 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5508 if (IsCellEditControlEnabled())
5509 DisableCellEditControl();
5511 return ( m_table
->DeleteCols( pos
, numCols
) );
5512 // the table will have sent the results of the delete col
5513 // operation to this view object as a grid table message
5521 // ----- event handlers
5524 // Generate a grid event based on a mouse event and
5525 // return the result of ProcessEvent()
5527 int wxGrid::SendEvent( const wxEventType type
,
5529 wxMouseEvent
& mouseEv
)
5534 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5536 int rowOrCol
= (row
== -1 ? col
: row
);
5538 wxGridSizeEvent
gridEvt( GetId(),
5542 mouseEv
.GetX() + GetRowLabelSize(),
5543 mouseEv
.GetY() + GetColLabelSize(),
5544 mouseEv
.ControlDown(),
5545 mouseEv
.ShiftDown(),
5547 mouseEv
.MetaDown() );
5549 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5550 vetoed
= !gridEvt
.IsAllowed();
5552 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5554 // Right now, it should _never_ end up here!
5555 wxGridRangeSelectEvent
gridEvt( GetId(),
5559 m_selectingBottomRight
,
5561 mouseEv
.ControlDown(),
5562 mouseEv
.ShiftDown(),
5564 mouseEv
.MetaDown() );
5566 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5567 vetoed
= !gridEvt
.IsAllowed();
5571 wxGridEvent
gridEvt( GetId(),
5575 mouseEv
.GetX() + GetRowLabelSize(),
5576 mouseEv
.GetY() + GetColLabelSize(),
5578 mouseEv
.ControlDown(),
5579 mouseEv
.ShiftDown(),
5581 mouseEv
.MetaDown() );
5582 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5583 vetoed
= !gridEvt
.IsAllowed();
5586 // A Veto'd event may not be `claimed' so test this first
5587 if (vetoed
) return -1;
5588 return claimed
? 1 : 0;
5592 // Generate a grid event of specified type and return the result
5593 // of ProcessEvent().
5595 int wxGrid::SendEvent( const wxEventType type
,
5601 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5603 int rowOrCol
= (row
== -1 ? col
: row
);
5605 wxGridSizeEvent
gridEvt( GetId(),
5610 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5611 vetoed
= !gridEvt
.IsAllowed();
5615 wxGridEvent
gridEvt( GetId(),
5620 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5621 vetoed
= !gridEvt
.IsAllowed();
5624 // A Veto'd event may not be `claimed' so test this first
5625 if (vetoed
) return -1;
5626 return claimed
? 1 : 0;
5630 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5632 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5636 void wxGrid::OnSize( wxSizeEvent
& event
)
5638 // position the child windows
5641 // don't call CalcDimensions() from here, the base class handles the size
5647 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5649 if ( m_inOnKeyDown
)
5651 // shouldn't be here - we are going round in circles...
5653 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5656 m_inOnKeyDown
= TRUE
;
5658 // propagate the event up and see if it gets processed
5660 wxWindow
*parent
= GetParent();
5661 wxKeyEvent
keyEvt( event
);
5662 keyEvt
.SetEventObject( parent
);
5664 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5667 // try local handlers
5669 switch ( event
.KeyCode() )
5672 if ( event
.ControlDown() )
5674 MoveCursorUpBlock( event
.ShiftDown() );
5678 MoveCursorUp( event
.ShiftDown() );
5683 if ( event
.ControlDown() )
5685 MoveCursorDownBlock( event
.ShiftDown() );
5689 MoveCursorDown( event
.ShiftDown() );
5694 if ( event
.ControlDown() )
5696 MoveCursorLeftBlock( event
.ShiftDown() );
5700 MoveCursorLeft( event
.ShiftDown() );
5705 if ( event
.ControlDown() )
5707 MoveCursorRightBlock( event
.ShiftDown() );
5711 MoveCursorRight( event
.ShiftDown() );
5716 case WXK_NUMPAD_ENTER
:
5717 if ( event
.ControlDown() )
5719 event
.Skip(); // to let the edit control have the return
5723 if ( GetGridCursorRow() < GetNumberRows()-1 )
5725 MoveCursorDown( event
.ShiftDown() );
5729 // at the bottom of a column
5730 HideCellEditControl();
5731 SaveEditControlValue();
5741 if (event
.ShiftDown())
5743 if ( GetGridCursorCol() > 0 )
5745 MoveCursorLeft( FALSE
);
5750 HideCellEditControl();
5751 SaveEditControlValue();
5756 if ( GetGridCursorCol() < GetNumberCols()-1 )
5758 MoveCursorRight( FALSE
);
5763 HideCellEditControl();
5764 SaveEditControlValue();
5770 if ( event
.ControlDown() )
5772 MakeCellVisible( 0, 0 );
5773 SetCurrentCell( 0, 0 );
5782 if ( event
.ControlDown() )
5784 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5785 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5802 if ( event
.ControlDown() )
5804 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5805 m_currentCellCoords
.GetCol(),
5806 event
.ControlDown(),
5812 if ( !IsEditable() )
5814 MoveCursorRight( FALSE
);
5817 // Otherwise fall through to default
5820 // is it possible to edit the current cell at all?
5821 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5823 // yes, now check whether the cells editor accepts the key
5824 int row
= m_currentCellCoords
.GetRow();
5825 int col
= m_currentCellCoords
.GetCol();
5826 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5827 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5829 // <F2> is special and will always start editing, for
5830 // other keys - ask the editor itself
5831 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5832 || editor
->IsAcceptedKey(event
) )
5834 EnableCellEditControl();
5835 editor
->StartingKey(event
);
5847 // let others process char events with modifiers or all
5848 // char events for readonly cells
5855 m_inOnKeyDown
= FALSE
;
5858 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5860 // try local handlers
5862 if ( event
.KeyCode() == WXK_SHIFT
)
5864 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5865 m_selectingBottomRight
!= wxGridNoCellCoords
)
5866 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5867 m_selectingTopLeft
.GetCol(),
5868 m_selectingBottomRight
.GetRow(),
5869 m_selectingBottomRight
.GetCol(),
5870 event
.ControlDown(),
5874 m_selectingTopLeft
= wxGridNoCellCoords
;
5875 m_selectingBottomRight
= wxGridNoCellCoords
;
5876 m_selectingKeyboard
= wxGridNoCellCoords
;
5880 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5884 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5886 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5888 // the event has been intercepted - do nothing
5892 wxClientDC
dc(m_gridWin
);
5895 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5897 HideCellEditControl();
5898 DisableCellEditControl();
5900 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5903 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5904 if ( !m_gridLinesEnabled
)
5912 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5914 // Otherwise refresh redraws the highlight!
5915 m_currentCellCoords
= coords
;
5917 DrawGridCellArea(dc
,cells
);
5918 DrawAllGridLines( dc
, r
);
5922 m_currentCellCoords
= coords
;
5924 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5925 DrawCellHighlight(dc
, attr
);
5930 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5933 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5935 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5938 rightCol
= GetNumberCols() - 1;
5940 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5943 bottomRow
= GetNumberRows() - 1;
5945 if ( topRow
> bottomRow
)
5952 if ( leftCol
> rightCol
)
5959 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5960 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5962 if ( m_selectingTopLeft
!= updateTopLeft
||
5963 m_selectingBottomRight
!= updateBottomRight
)
5965 // Compute two optimal update rectangles:
5966 // Either one rectangle is a real subset of the
5967 // other, or they are (almost) disjoint!
5969 bool need_refresh
[4];
5973 need_refresh
[3] = FALSE
;
5976 // Store intermediate values
5977 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5978 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5979 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5980 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5982 // Determine the outer/inner coordinates.
5983 if (oldLeft
> leftCol
)
5989 if (oldTop
> topRow
)
5995 if (oldRight
< rightCol
)
5998 oldRight
= rightCol
;
6001 if (oldBottom
< bottomRow
)
6004 oldBottom
= bottomRow
;
6008 // Now, either the stuff marked old is the outer
6009 // rectangle or we don't have a situation where one
6010 // is contained in the other.
6012 if ( oldLeft
< leftCol
)
6014 need_refresh
[0] = TRUE
;
6015 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6017 wxGridCellCoords ( oldBottom
,
6021 if ( oldTop
< topRow
)
6023 need_refresh
[1] = TRUE
;
6024 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6026 wxGridCellCoords ( topRow
- 1,
6030 if ( oldRight
> rightCol
)
6032 need_refresh
[2] = TRUE
;
6033 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6035 wxGridCellCoords ( oldBottom
,
6039 if ( oldBottom
> bottomRow
)
6041 need_refresh
[3] = TRUE
;
6042 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6044 wxGridCellCoords ( oldBottom
,
6050 m_selectingTopLeft
= updateTopLeft
;
6051 m_selectingBottomRight
= updateBottomRight
;
6053 // various Refresh() calls
6054 for (i
= 0; i
< 4; i
++ )
6055 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6056 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6059 // never generate an event as it will be generated from
6060 // wxGridSelection::SelectBlock!
6061 // (old comment from when this was the body of SelectBlock)
6065 // ------ functions to get/send data (see also public functions)
6068 bool wxGrid::GetModelValues()
6072 // all we need to do is repaint the grid
6074 m_gridWin
->Refresh();
6082 bool wxGrid::SetModelValues()
6088 for ( row
= 0; row
< m_numRows
; row
++ )
6090 for ( col
= 0; col
< m_numCols
; col
++ )
6092 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6104 // Note - this function only draws cells that are in the list of
6105 // exposed cells (usually set from the update region by
6106 // CalcExposedCells)
6108 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6110 if ( !m_numRows
|| !m_numCols
) return;
6113 size_t numCells
= cells
.GetCount();
6115 for ( i
= 0; i
< numCells
; i
++ )
6117 DrawCell( dc
, cells
[i
] );
6122 void wxGrid::DrawGridSpace( wxDC
& dc
)
6125 m_gridWin
->GetClientSize( &cw
, &ch
);
6128 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6130 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6131 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6133 if ( right
> rightCol
|| bottom
> bottomRow
)
6136 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6138 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6139 dc
.SetPen( *wxTRANSPARENT_PEN
);
6141 if ( right
> rightCol
)
6143 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6146 if ( bottom
> bottomRow
)
6148 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6154 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6156 int row
= coords
.GetRow();
6157 int col
= coords
.GetCol();
6159 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6162 // we draw the cell border ourselves
6163 #if !WXGRID_DRAW_LINES
6164 if ( m_gridLinesEnabled
)
6165 DrawCellBorder( dc
, coords
);
6168 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6170 bool isCurrent
= coords
== m_currentCellCoords
;
6172 wxRect rect
= CellToRect( row
, col
);
6174 // if the editor is shown, we should use it and not the renderer
6175 // Note: However, only if it is really _shown_, i.e. not hidden!
6176 if ( isCurrent
&& IsCellEditControlShown() )
6178 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6179 editor
->PaintBackground(rect
, attr
);
6184 // but all the rest is drawn by the cell renderer and hence may be
6186 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6187 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6194 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6196 int row
= m_currentCellCoords
.GetRow();
6197 int col
= m_currentCellCoords
.GetCol();
6199 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6202 wxRect rect
= CellToRect(row
, col
);
6204 // hmmm... what could we do here to show that the cell is disabled?
6205 // for now, I just draw a thinner border than for the other ones, but
6206 // it doesn't look really good
6208 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6212 // The center of th drawn line is where the position/width/height of
6213 // the rectangle is actually at, (on wxMSW atr least,) so we will
6214 // reduce the size of the rectangle to compensate for the thickness of
6215 // the line. If this is too strange on non wxMSW platforms then
6216 // please #ifdef this appropriately.
6217 rect
.x
+= penWidth
/2;
6218 rect
.y
+= penWidth
/2;
6219 rect
.width
-= penWidth
-1;
6220 rect
.height
-= penWidth
-1;
6223 // Now draw the rectangle
6224 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6225 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6226 dc
.DrawRectangle(rect
);
6230 // VZ: my experiments with 3d borders...
6232 // how to properly set colours for arbitrary bg?
6233 wxCoord x1
= rect
.x
,
6235 x2
= rect
.x
+ rect
.width
-1,
6236 y2
= rect
.y
+ rect
.height
-1;
6238 dc
.SetPen(*wxWHITE_PEN
);
6239 dc
.DrawLine(x1
, y1
, x2
, y1
);
6240 dc
.DrawLine(x1
, y1
, x1
, y2
);
6242 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6243 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6245 dc
.SetPen(*wxBLACK_PEN
);
6246 dc
.DrawLine(x1
, y2
, x2
, y2
);
6247 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6252 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6254 int row
= coords
.GetRow();
6255 int col
= coords
.GetCol();
6256 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6259 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6261 // right hand border
6263 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6264 GetColRight(col
), GetRowBottom(row
) );
6268 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6269 GetColRight(col
), GetRowBottom(row
) );
6272 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6274 // This if block was previously in wxGrid::OnPaint but that doesn't
6275 // seem to get called under wxGTK - MB
6277 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6278 m_numRows
&& m_numCols
)
6280 m_currentCellCoords
.Set(0, 0);
6283 if ( IsCellEditControlShown() )
6285 // don't show highlight when the edit control is shown
6289 // if the active cell was repainted, repaint its highlight too because it
6290 // might have been damaged by the grid lines
6291 size_t count
= cells
.GetCount();
6292 for ( size_t n
= 0; n
< count
; n
++ )
6294 if ( cells
[n
] == m_currentCellCoords
)
6296 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6297 DrawCellHighlight(dc
, attr
);
6305 // TODO: remove this ???
6306 // This is used to redraw all grid lines e.g. when the grid line colour
6309 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6311 if ( !m_gridLinesEnabled
||
6313 !m_numCols
) return;
6315 int top
, bottom
, left
, right
;
6317 #if 0 //#ifndef __WXGTK__
6321 m_gridWin
->GetClientSize(&cw
, &ch
);
6323 // virtual coords of visible area
6325 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6326 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6331 reg
.GetBox(x
, y
, w
, h
);
6332 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6333 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6337 m_gridWin
->GetClientSize(&cw
, &ch
);
6338 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6339 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6342 // avoid drawing grid lines past the last row and col
6344 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6345 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6347 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6349 // horizontal grid lines
6352 for ( i
= 0; i
< m_numRows
; i
++ )
6354 int bot
= GetRowBottom(i
) - 1;
6363 dc
.DrawLine( left
, bot
, right
, bot
);
6368 // vertical grid lines
6370 for ( i
= 0; i
< m_numCols
; i
++ )
6372 int colRight
= GetColRight(i
) - 1;
6373 if ( colRight
> right
)
6378 if ( colRight
>= left
)
6380 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6386 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6388 if ( !m_numRows
) return;
6391 size_t numLabels
= rows
.GetCount();
6393 for ( i
= 0; i
< numLabels
; i
++ )
6395 DrawRowLabel( dc
, rows
[i
] );
6400 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6402 if ( GetRowHeight(row
) <= 0 )
6405 int rowTop
= GetRowTop(row
),
6406 rowBottom
= GetRowBottom(row
) - 1;
6408 dc
.SetPen( *wxBLACK_PEN
);
6409 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6410 m_rowLabelWidth
-1, rowBottom
);
6412 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6414 dc
.SetPen( *wxWHITE_PEN
);
6415 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6416 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6418 dc
.SetBackgroundMode( wxTRANSPARENT
);
6419 dc
.SetTextForeground( GetLabelTextColour() );
6420 dc
.SetFont( GetLabelFont() );
6423 GetRowLabelAlignment( &hAlign
, &vAlign
);
6427 rect
.SetY( GetRowTop(row
) + 2 );
6428 rect
.SetWidth( m_rowLabelWidth
- 4 );
6429 rect
.SetHeight( GetRowHeight(row
) - 4 );
6430 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6434 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6436 if ( !m_numCols
) return;
6439 size_t numLabels
= cols
.GetCount();
6441 for ( i
= 0; i
< numLabels
; i
++ )
6443 DrawColLabel( dc
, cols
[i
] );
6448 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6450 if ( GetColWidth(col
) <= 0 )
6453 int colLeft
= GetColLeft(col
),
6454 colRight
= GetColRight(col
) - 1;
6456 dc
.SetPen( *wxBLACK_PEN
);
6457 dc
.DrawLine( colRight
, 0,
6458 colRight
, m_colLabelHeight
-1 );
6460 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6461 colRight
, m_colLabelHeight
-1 );
6463 dc
.SetPen( *wxWHITE_PEN
);
6464 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6465 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6467 dc
.SetBackgroundMode( wxTRANSPARENT
);
6468 dc
.SetTextForeground( GetLabelTextColour() );
6469 dc
.SetFont( GetLabelFont() );
6471 dc
.SetBackgroundMode( wxTRANSPARENT
);
6472 dc
.SetTextForeground( GetLabelTextColour() );
6473 dc
.SetFont( GetLabelFont() );
6476 GetColLabelAlignment( &hAlign
, &vAlign
);
6479 rect
.SetX( colLeft
+ 2 );
6481 rect
.SetWidth( GetColWidth(col
) - 4 );
6482 rect
.SetHeight( m_colLabelHeight
- 4 );
6483 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6486 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6487 const wxString
& value
,
6492 wxArrayString lines
;
6494 dc
.SetClippingRegion( rect
);
6495 StringToLines( value
, lines
);
6498 //Forward to new API.
6499 DrawTextRectangle( dc
,
6507 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6508 const wxArrayString
& lines
,
6513 long textWidth
, textHeight
;
6514 long lineWidth
, lineHeight
;
6516 if ( lines
.GetCount() )
6518 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6519 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6522 switch ( horizAlign
)
6525 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6528 case wxALIGN_CENTRE
:
6529 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6538 switch ( vertAlign
)
6540 case wxALIGN_BOTTOM
:
6541 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6544 case wxALIGN_CENTRE
:
6545 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6554 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6556 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6561 dc
.DestroyClippingRegion();
6565 // Split multi line text up into an array of strings. Any existing
6566 // contents of the string array are preserved.
6568 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6572 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6573 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6575 while ( startPos
< (int)tVal
.Length() )
6577 pos
= tVal
.Mid(startPos
).Find( eol
);
6582 else if ( pos
== 0 )
6584 lines
.Add( wxEmptyString
);
6588 lines
.Add( value
.Mid(startPos
, pos
) );
6592 if ( startPos
< (int)value
.Length() )
6594 lines
.Add( value
.Mid( startPos
) );
6599 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6600 const wxArrayString
& lines
,
6601 long *width
, long *height
)
6608 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6610 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6611 w
= wxMax( w
, lineW
);
6620 // ------ Batch processing.
6622 void wxGrid::EndBatch()
6624 if ( m_batchCount
> 0 )
6627 if ( !m_batchCount
)
6630 m_rowLabelWin
->Refresh();
6631 m_colLabelWin
->Refresh();
6632 m_cornerLabelWin
->Refresh();
6633 m_gridWin
->Refresh();
6638 // Use this, rather than wxWindow::Refresh(), to force an immediate
6639 // repainting of the grid. Has no effect if you are already inside a
6640 // BeginBatch / EndBatch block.
6642 void wxGrid::ForceRefresh()
6650 // ------ Edit control functions
6654 void wxGrid::EnableEditing( bool edit
)
6656 // TODO: improve this ?
6658 if ( edit
!= m_editable
)
6660 if(!edit
) EnableCellEditControl(edit
);
6666 void wxGrid::EnableCellEditControl( bool enable
)
6671 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6672 SetCurrentCell( 0, 0 );
6674 if ( enable
!= m_cellEditCtrlEnabled
)
6678 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6681 // this should be checked by the caller!
6682 wxASSERT_MSG( CanEnableCellControl(),
6683 _T("can't enable editing for this cell!") );
6685 // do it before ShowCellEditControl()
6686 m_cellEditCtrlEnabled
= enable
;
6688 ShowCellEditControl();
6692 //FIXME:add veto support
6693 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
6695 HideCellEditControl();
6696 SaveEditControlValue();
6698 // do it after HideCellEditControl()
6699 m_cellEditCtrlEnabled
= enable
;
6704 bool wxGrid::IsCurrentCellReadOnly() const
6707 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6708 bool readonly
= attr
->IsReadOnly();
6714 bool wxGrid::CanEnableCellControl() const
6716 return m_editable
&& !IsCurrentCellReadOnly();
6719 bool wxGrid::IsCellEditControlEnabled() const
6721 // the cell edit control might be disable for all cells or just for the
6722 // current one if it's read only
6723 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6726 bool wxGrid::IsCellEditControlShown() const
6728 bool isShown
= FALSE
;
6730 if ( m_cellEditCtrlEnabled
)
6732 int row
= m_currentCellCoords
.GetRow();
6733 int col
= m_currentCellCoords
.GetCol();
6734 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6735 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6740 if ( editor
->IsCreated() )
6742 isShown
= editor
->GetControl()->IsShown();
6752 void wxGrid::ShowCellEditControl()
6754 if ( IsCellEditControlEnabled() )
6756 if ( !IsVisible( m_currentCellCoords
) )
6758 m_cellEditCtrlEnabled
= FALSE
;
6763 wxRect rect
= CellToRect( m_currentCellCoords
);
6764 int row
= m_currentCellCoords
.GetRow();
6765 int col
= m_currentCellCoords
.GetCol();
6767 // convert to scrolled coords
6769 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6771 // done in PaintBackground()
6773 // erase the highlight and the cell contents because the editor
6774 // might not cover the entire cell
6775 wxClientDC
dc( m_gridWin
);
6777 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6778 dc
.SetPen(*wxTRANSPARENT_PEN
);
6779 dc
.DrawRectangle(rect
);
6782 // cell is shifted by one pixel
6786 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6787 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6788 if ( !editor
->IsCreated() )
6790 editor
->Create(m_gridWin
, -1,
6791 new wxGridCellEditorEvtHandler(this, editor
));
6793 wxGridEditorCreatedEvent
evt(GetId(),
6794 wxEVT_GRID_EDITOR_CREATED
,
6798 editor
->GetControl());
6799 GetEventHandler()->ProcessEvent(evt
);
6802 editor
->Show( TRUE
, attr
);
6804 editor
->SetSize( rect
);
6806 editor
->BeginEdit(row
, col
, this);
6815 void wxGrid::HideCellEditControl()
6817 if ( IsCellEditControlEnabled() )
6819 int row
= m_currentCellCoords
.GetRow();
6820 int col
= m_currentCellCoords
.GetCol();
6822 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6823 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6824 editor
->Show( FALSE
);
6827 m_gridWin
->SetFocus();
6828 wxRect
rect( CellToRect( row
, col
) );
6829 m_gridWin
->Refresh( FALSE
, &rect
);
6834 void wxGrid::SaveEditControlValue()
6836 if ( IsCellEditControlEnabled() )
6838 int row
= m_currentCellCoords
.GetRow();
6839 int col
= m_currentCellCoords
.GetCol();
6841 wxString oldval
= GetCellValue(row
,col
);
6843 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6844 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6845 bool changed
= editor
->EndEdit(row
, col
, this);
6852 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
6853 m_currentCellCoords
.GetRow(),
6854 m_currentCellCoords
.GetCol() ) < 0 ) {
6856 // Event has been vetoed, set the data back.
6857 SetCellValue(row
,col
,oldval
);
6865 // ------ Grid location functions
6866 // Note that all of these functions work with the logical coordinates of
6867 // grid cells and labels so you will need to convert from device
6868 // coordinates for mouse events etc.
6871 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6873 int row
= YToRow(y
);
6874 int col
= XToCol(x
);
6876 if ( row
== -1 || col
== -1 )
6878 coords
= wxGridNoCellCoords
;
6882 coords
.Set( row
, col
);
6887 int wxGrid::YToRow( int y
)
6891 for ( i
= 0; i
< m_numRows
; i
++ )
6893 if ( y
< GetRowBottom(i
) )
6901 int wxGrid::XToCol( int x
)
6905 for ( i
= 0; i
< m_numCols
; i
++ )
6907 if ( x
< GetColRight(i
) )
6915 // return the row number that that the y coord is near the edge of, or
6916 // -1 if not near an edge
6918 int wxGrid::YToEdgeOfRow( int y
)
6922 for ( i
= 0; i
< m_numRows
; i
++ )
6924 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6926 d
= abs( y
- GetRowBottom(i
) );
6927 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6936 // return the col number that that the x coord is near the edge of, or
6937 // -1 if not near an edge
6939 int wxGrid::XToEdgeOfCol( int x
)
6943 for ( i
= 0; i
< m_numCols
; i
++ )
6945 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6947 d
= abs( x
- GetColRight(i
) );
6948 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6957 wxRect
wxGrid::CellToRect( int row
, int col
)
6959 wxRect
rect( -1, -1, -1, -1 );
6961 if ( row
>= 0 && row
< m_numRows
&&
6962 col
>= 0 && col
< m_numCols
)
6964 rect
.x
= GetColLeft(col
);
6965 rect
.y
= GetRowTop(row
);
6966 rect
.width
= GetColWidth(col
);
6967 rect
.height
= GetRowHeight(row
);
6970 // if grid lines are enabled, then the area of the cell is a bit smaller
6971 if (m_gridLinesEnabled
) {
6979 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6981 // get the cell rectangle in logical coords
6983 wxRect
r( CellToRect( row
, col
) );
6985 // convert to device coords
6987 int left
, top
, right
, bottom
;
6988 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6989 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6991 // check against the client area of the grid window
6994 m_gridWin
->GetClientSize( &cw
, &ch
);
6996 if ( wholeCellVisible
)
6998 // is the cell wholly visible ?
7000 return ( left
>= 0 && right
<= cw
&&
7001 top
>= 0 && bottom
<= ch
);
7005 // is the cell partly visible ?
7007 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7008 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7013 // make the specified cell location visible by doing a minimal amount
7016 void wxGrid::MakeCellVisible( int row
, int col
)
7020 int xpos
= -1, ypos
= -1;
7022 if ( row
>= 0 && row
< m_numRows
&&
7023 col
>= 0 && col
< m_numCols
)
7025 // get the cell rectangle in logical coords
7027 wxRect
r( CellToRect( row
, col
) );
7029 // convert to device coords
7031 int left
, top
, right
, bottom
;
7032 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7033 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7036 m_gridWin
->GetClientSize( &cw
, &ch
);
7042 else if ( bottom
> ch
)
7044 int h
= r
.GetHeight();
7046 for ( i
= row
-1; i
>= 0; i
-- )
7048 int rowHeight
= GetRowHeight(i
);
7049 if ( h
+ rowHeight
> ch
)
7056 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7057 // have rounding errors (this is important, because if we do, we
7058 // might not scroll at all and some cells won't be redrawn)
7060 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7062 ypos
+= GRID_SCROLL_LINE_Y
;
7069 else if ( right
> cw
)
7071 int w
= r
.GetWidth();
7073 for ( i
= col
-1; i
>= 0; i
-- )
7075 int colWidth
= GetColWidth(i
);
7076 if ( w
+ colWidth
> cw
)
7083 // see comment for ypos above
7084 xpos
+= GRID_SCROLL_LINE_X
;
7087 if ( xpos
!= -1 || ypos
!= -1 )
7090 xpos
/= GRID_SCROLL_LINE_X
;
7092 ypos
/= GRID_SCROLL_LINE_Y
;
7093 Scroll( xpos
, ypos
);
7101 // ------ Grid cursor movement functions
7104 bool wxGrid::MoveCursorUp( bool expandSelection
)
7106 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7107 m_currentCellCoords
.GetRow() >= 0 )
7109 if ( expandSelection
)
7111 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7112 m_selectingKeyboard
= m_currentCellCoords
;
7113 if ( m_selectingKeyboard
.GetRow() > 0 )
7115 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7116 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7117 m_selectingKeyboard
.GetCol() );
7118 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7121 else if ( m_currentCellCoords
.GetRow() > 0 )
7124 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7125 m_currentCellCoords
.GetCol() );
7126 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7127 m_currentCellCoords
.GetCol() );
7138 bool wxGrid::MoveCursorDown( bool expandSelection
)
7140 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7141 m_currentCellCoords
.GetRow() < m_numRows
)
7143 if ( expandSelection
)
7145 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7146 m_selectingKeyboard
= m_currentCellCoords
;
7147 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7149 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7150 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7151 m_selectingKeyboard
.GetCol() );
7152 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7155 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7158 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7159 m_currentCellCoords
.GetCol() );
7160 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7161 m_currentCellCoords
.GetCol() );
7172 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7174 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7175 m_currentCellCoords
.GetCol() >= 0 )
7177 if ( expandSelection
)
7179 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7180 m_selectingKeyboard
= m_currentCellCoords
;
7181 if ( m_selectingKeyboard
.GetCol() > 0 )
7183 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7184 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7185 m_selectingKeyboard
.GetCol() );
7186 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7189 else if ( m_currentCellCoords
.GetCol() > 0 )
7192 MakeCellVisible( m_currentCellCoords
.GetRow(),
7193 m_currentCellCoords
.GetCol() - 1 );
7194 SetCurrentCell( m_currentCellCoords
.GetRow(),
7195 m_currentCellCoords
.GetCol() - 1 );
7206 bool wxGrid::MoveCursorRight( bool expandSelection
)
7208 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7209 m_currentCellCoords
.GetCol() < m_numCols
)
7211 if ( expandSelection
)
7213 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7214 m_selectingKeyboard
= m_currentCellCoords
;
7215 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7217 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7218 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7219 m_selectingKeyboard
.GetCol() );
7220 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7223 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7226 MakeCellVisible( m_currentCellCoords
.GetRow(),
7227 m_currentCellCoords
.GetCol() + 1 );
7228 SetCurrentCell( m_currentCellCoords
.GetRow(),
7229 m_currentCellCoords
.GetCol() + 1 );
7240 bool wxGrid::MovePageUp()
7242 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7244 int row
= m_currentCellCoords
.GetRow();
7248 m_gridWin
->GetClientSize( &cw
, &ch
);
7250 int y
= GetRowTop(row
);
7251 int newRow
= YToRow( y
- ch
+ 1 );
7256 else if ( newRow
== row
)
7261 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7262 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7270 bool wxGrid::MovePageDown()
7272 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7274 int row
= m_currentCellCoords
.GetRow();
7275 if ( row
< m_numRows
)
7278 m_gridWin
->GetClientSize( &cw
, &ch
);
7280 int y
= GetRowTop(row
);
7281 int newRow
= YToRow( y
+ ch
);
7284 newRow
= m_numRows
- 1;
7286 else if ( newRow
== row
)
7291 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7292 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7300 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7303 m_currentCellCoords
!= wxGridNoCellCoords
&&
7304 m_currentCellCoords
.GetRow() > 0 )
7306 int row
= m_currentCellCoords
.GetRow();
7307 int col
= m_currentCellCoords
.GetCol();
7309 if ( m_table
->IsEmptyCell(row
, col
) )
7311 // starting in an empty cell: find the next block of
7317 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7320 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7322 // starting at the top of a block: find the next block
7328 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7333 // starting within a block: find the top of the block
7338 if ( m_table
->IsEmptyCell(row
, col
) )
7346 MakeCellVisible( row
, col
);
7347 if ( expandSelection
)
7349 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7350 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7355 SetCurrentCell( row
, col
);
7363 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7366 m_currentCellCoords
!= wxGridNoCellCoords
&&
7367 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7369 int row
= m_currentCellCoords
.GetRow();
7370 int col
= m_currentCellCoords
.GetCol();
7372 if ( m_table
->IsEmptyCell(row
, col
) )
7374 // starting in an empty cell: find the next block of
7377 while ( row
< m_numRows
-1 )
7380 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7383 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7385 // starting at the bottom of a block: find the next block
7388 while ( row
< m_numRows
-1 )
7391 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7396 // starting within a block: find the bottom of the block
7398 while ( row
< m_numRows
-1 )
7401 if ( m_table
->IsEmptyCell(row
, col
) )
7409 MakeCellVisible( row
, col
);
7410 if ( expandSelection
)
7412 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7413 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7418 SetCurrentCell( row
, col
);
7427 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7430 m_currentCellCoords
!= wxGridNoCellCoords
&&
7431 m_currentCellCoords
.GetCol() > 0 )
7433 int row
= m_currentCellCoords
.GetRow();
7434 int col
= m_currentCellCoords
.GetCol();
7436 if ( m_table
->IsEmptyCell(row
, col
) )
7438 // starting in an empty cell: find the next block of
7444 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7447 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7449 // starting at the left of a block: find the next block
7455 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7460 // starting within a block: find the left of the block
7465 if ( m_table
->IsEmptyCell(row
, col
) )
7473 MakeCellVisible( row
, col
);
7474 if ( expandSelection
)
7476 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7477 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7482 SetCurrentCell( row
, col
);
7491 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7494 m_currentCellCoords
!= wxGridNoCellCoords
&&
7495 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7497 int row
= m_currentCellCoords
.GetRow();
7498 int col
= m_currentCellCoords
.GetCol();
7500 if ( m_table
->IsEmptyCell(row
, col
) )
7502 // starting in an empty cell: find the next block of
7505 while ( col
< m_numCols
-1 )
7508 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7511 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7513 // starting at the right of a block: find the next block
7516 while ( col
< m_numCols
-1 )
7519 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7524 // starting within a block: find the right of the block
7526 while ( col
< m_numCols
-1 )
7529 if ( m_table
->IsEmptyCell(row
, col
) )
7537 MakeCellVisible( row
, col
);
7538 if ( expandSelection
)
7540 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7541 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7546 SetCurrentCell( row
, col
);
7558 // ------ Label values and formatting
7561 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7563 *horiz
= m_rowLabelHorizAlign
;
7564 *vert
= m_rowLabelVertAlign
;
7567 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7569 *horiz
= m_colLabelHorizAlign
;
7570 *vert
= m_colLabelVertAlign
;
7573 wxString
wxGrid::GetRowLabelValue( int row
)
7577 return m_table
->GetRowLabelValue( row
);
7587 wxString
wxGrid::GetColLabelValue( int col
)
7591 return m_table
->GetColLabelValue( col
);
7602 void wxGrid::SetRowLabelSize( int width
)
7604 width
= wxMax( width
, 0 );
7605 if ( width
!= m_rowLabelWidth
)
7609 m_rowLabelWin
->Show( FALSE
);
7610 m_cornerLabelWin
->Show( FALSE
);
7612 else if ( m_rowLabelWidth
== 0 )
7614 m_rowLabelWin
->Show( TRUE
);
7615 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7618 m_rowLabelWidth
= width
;
7625 void wxGrid::SetColLabelSize( int height
)
7627 height
= wxMax( height
, 0 );
7628 if ( height
!= m_colLabelHeight
)
7632 m_colLabelWin
->Show( FALSE
);
7633 m_cornerLabelWin
->Show( FALSE
);
7635 else if ( m_colLabelHeight
== 0 )
7637 m_colLabelWin
->Show( TRUE
);
7638 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7641 m_colLabelHeight
= height
;
7648 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7650 if ( m_labelBackgroundColour
!= colour
)
7652 m_labelBackgroundColour
= colour
;
7653 m_rowLabelWin
->SetBackgroundColour( colour
);
7654 m_colLabelWin
->SetBackgroundColour( colour
);
7655 m_cornerLabelWin
->SetBackgroundColour( colour
);
7657 if ( !GetBatchCount() )
7659 m_rowLabelWin
->Refresh();
7660 m_colLabelWin
->Refresh();
7661 m_cornerLabelWin
->Refresh();
7666 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7668 if ( m_labelTextColour
!= colour
)
7670 m_labelTextColour
= colour
;
7671 if ( !GetBatchCount() )
7673 m_rowLabelWin
->Refresh();
7674 m_colLabelWin
->Refresh();
7679 void wxGrid::SetLabelFont( const wxFont
& font
)
7682 if ( !GetBatchCount() )
7684 m_rowLabelWin
->Refresh();
7685 m_colLabelWin
->Refresh();
7689 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7691 // allow old (incorrect) defs to be used
7694 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7695 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7696 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7701 case wxTOP
: vert
= wxALIGN_TOP
; break;
7702 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7703 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7706 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7708 m_rowLabelHorizAlign
= horiz
;
7711 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7713 m_rowLabelVertAlign
= vert
;
7716 if ( !GetBatchCount() )
7718 m_rowLabelWin
->Refresh();
7722 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7724 // allow old (incorrect) defs to be used
7727 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7728 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7729 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7734 case wxTOP
: vert
= wxALIGN_TOP
; break;
7735 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7736 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7739 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7741 m_colLabelHorizAlign
= horiz
;
7744 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7746 m_colLabelVertAlign
= vert
;
7749 if ( !GetBatchCount() )
7751 m_colLabelWin
->Refresh();
7755 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7759 m_table
->SetRowLabelValue( row
, s
);
7760 if ( !GetBatchCount() )
7762 wxRect rect
= CellToRect( row
, 0);
7763 if ( rect
.height
> 0 )
7765 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7767 rect
.width
= m_rowLabelWidth
;
7768 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7774 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7778 m_table
->SetColLabelValue( col
, s
);
7779 if ( !GetBatchCount() )
7781 wxRect rect
= CellToRect( 0, col
);
7782 if ( rect
.width
> 0 )
7784 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7786 rect
.height
= m_colLabelHeight
;
7787 m_colLabelWin
->Refresh( TRUE
, &rect
);
7793 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7795 if ( m_gridLineColour
!= colour
)
7797 m_gridLineColour
= colour
;
7799 wxClientDC
dc( m_gridWin
);
7801 DrawAllGridLines( dc
, wxRegion() );
7806 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7808 if ( m_cellHighlightColour
!= colour
)
7810 m_cellHighlightColour
= colour
;
7812 wxClientDC
dc( m_gridWin
);
7814 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7815 DrawCellHighlight(dc
, attr
);
7820 void wxGrid::SetCellHighlightPenWidth(int width
)
7822 if (m_cellHighlightPenWidth
!= width
) {
7823 m_cellHighlightPenWidth
= width
;
7825 // Just redrawing the cell highlight is not enough since that won't
7826 // make any visible change if the the thickness is getting smaller.
7827 int row
= m_currentCellCoords
.GetRow();
7828 int col
= m_currentCellCoords
.GetCol();
7829 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7831 wxRect rect
= CellToRect(row
, col
);
7832 m_gridWin
->Refresh(TRUE
, &rect
);
7836 void wxGrid::SetCellHighlightROPenWidth(int width
)
7838 if (m_cellHighlightROPenWidth
!= width
) {
7839 m_cellHighlightROPenWidth
= width
;
7841 // Just redrawing the cell highlight is not enough since that won't
7842 // make any visible change if the the thickness is getting smaller.
7843 int row
= m_currentCellCoords
.GetRow();
7844 int col
= m_currentCellCoords
.GetCol();
7845 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7847 wxRect rect
= CellToRect(row
, col
);
7848 m_gridWin
->Refresh(TRUE
, &rect
);
7852 void wxGrid::EnableGridLines( bool enable
)
7854 if ( enable
!= m_gridLinesEnabled
)
7856 m_gridLinesEnabled
= enable
;
7858 if ( !GetBatchCount() )
7862 wxClientDC
dc( m_gridWin
);
7864 DrawAllGridLines( dc
, wxRegion() );
7868 m_gridWin
->Refresh();
7875 int wxGrid::GetDefaultRowSize()
7877 return m_defaultRowHeight
;
7880 int wxGrid::GetRowSize( int row
)
7882 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7884 return GetRowHeight(row
);
7887 int wxGrid::GetDefaultColSize()
7889 return m_defaultColWidth
;
7892 int wxGrid::GetColSize( int col
)
7894 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7896 return GetColWidth(col
);
7899 // ============================================================================
7900 // access to the grid attributes: each of them has a default value in the grid
7901 // itself and may be overidden on a per-cell basis
7902 // ============================================================================
7904 // ----------------------------------------------------------------------------
7905 // setting default attributes
7906 // ----------------------------------------------------------------------------
7908 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7910 m_defaultCellAttr
->SetBackgroundColour(col
);
7912 m_gridWin
->SetBackgroundColour(col
);
7916 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7918 m_defaultCellAttr
->SetTextColour(col
);
7921 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7923 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7926 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7928 m_defaultCellAttr
->SetFont(font
);
7931 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7933 m_defaultCellAttr
->SetRenderer(renderer
);
7936 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7938 m_defaultCellAttr
->SetEditor(editor
);
7941 // ----------------------------------------------------------------------------
7942 // access to the default attrbiutes
7943 // ----------------------------------------------------------------------------
7945 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7947 return m_defaultCellAttr
->GetBackgroundColour();
7950 wxColour
wxGrid::GetDefaultCellTextColour()
7952 return m_defaultCellAttr
->GetTextColour();
7955 wxFont
wxGrid::GetDefaultCellFont()
7957 return m_defaultCellAttr
->GetFont();
7960 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7962 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7965 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7967 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7970 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7972 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7975 // ----------------------------------------------------------------------------
7976 // access to cell attributes
7977 // ----------------------------------------------------------------------------
7979 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7981 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7982 wxColour colour
= attr
->GetBackgroundColour();
7987 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7989 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7990 wxColour colour
= attr
->GetTextColour();
7995 wxFont
wxGrid::GetCellFont( int row
, int col
)
7997 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7998 wxFont font
= attr
->GetFont();
8003 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8005 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8006 attr
->GetAlignment(horiz
, vert
);
8010 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8012 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8013 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8019 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8021 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8022 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8028 bool wxGrid::IsReadOnly(int row
, int col
) const
8030 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8031 bool isReadOnly
= attr
->IsReadOnly();
8036 // ----------------------------------------------------------------------------
8037 // attribute support: cache, automatic provider creation, ...
8038 // ----------------------------------------------------------------------------
8040 bool wxGrid::CanHaveAttributes()
8047 return m_table
->CanHaveAttributes();
8050 void wxGrid::ClearAttrCache()
8052 if ( m_attrCache
.row
!= -1 )
8054 wxSafeDecRef(m_attrCache
.attr
);
8055 m_attrCache
.attr
= NULL
;
8056 m_attrCache
.row
= -1;
8060 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8062 wxGrid
*self
= (wxGrid
*)this; // const_cast
8064 self
->ClearAttrCache();
8065 self
->m_attrCache
.row
= row
;
8066 self
->m_attrCache
.col
= col
;
8067 self
->m_attrCache
.attr
= attr
;
8071 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8073 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8075 *attr
= m_attrCache
.attr
;
8076 wxSafeIncRef(m_attrCache
.attr
);
8078 #ifdef DEBUG_ATTR_CACHE
8079 gs_nAttrCacheHits
++;
8086 #ifdef DEBUG_ATTR_CACHE
8087 gs_nAttrCacheMisses
++;
8093 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8095 wxGridCellAttr
*attr
;
8096 if ( !LookupAttr(row
, col
, &attr
) )
8098 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
8099 CacheAttr(row
, col
, attr
);
8103 attr
->SetDefAttr(m_defaultCellAttr
);
8107 attr
= m_defaultCellAttr
;
8114 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8116 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8118 wxCHECK_MSG( m_table
, attr
,
8119 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8121 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8124 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8126 // artificially inc the ref count to match DecRef() in caller
8128 m_table
->SetAttr(attr
, row
, col
);
8134 // ----------------------------------------------------------------------------
8135 // setting column attributes (wrappers around SetColAttr)
8136 // ----------------------------------------------------------------------------
8138 void wxGrid::SetColFormatBool(int col
)
8140 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8143 void wxGrid::SetColFormatNumber(int col
)
8145 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8148 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8150 wxString typeName
= wxGRID_VALUE_FLOAT
;
8151 if ( (width
!= -1) || (precision
!= -1) )
8153 typeName
<< _T(':') << width
<< _T(',') << precision
;
8156 SetColFormatCustom(col
, typeName
);
8159 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8161 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8163 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8165 attr
= new wxGridCellAttr
;
8166 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8167 attr
->SetRenderer(renderer
);
8169 SetColAttr(col
, attr
);
8173 // ----------------------------------------------------------------------------
8174 // setting cell attributes: this is forwarded to the table
8175 // ----------------------------------------------------------------------------
8177 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8179 if ( CanHaveAttributes() )
8181 m_table
->SetRowAttr(attr
, row
);
8190 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8192 if ( CanHaveAttributes() )
8194 m_table
->SetColAttr(attr
, col
);
8203 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8205 if ( CanHaveAttributes() )
8207 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8208 attr
->SetBackgroundColour(colour
);
8213 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8215 if ( CanHaveAttributes() )
8217 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8218 attr
->SetTextColour(colour
);
8223 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8225 if ( CanHaveAttributes() )
8227 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8228 attr
->SetFont(font
);
8233 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8235 if ( CanHaveAttributes() )
8237 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8238 attr
->SetAlignment(horiz
, vert
);
8243 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8245 if ( CanHaveAttributes() )
8247 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8248 attr
->SetRenderer(renderer
);
8253 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8255 if ( CanHaveAttributes() )
8257 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8258 attr
->SetEditor(editor
);
8263 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8265 if ( CanHaveAttributes() )
8267 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8268 attr
->SetReadOnly(isReadOnly
);
8273 // ----------------------------------------------------------------------------
8274 // Data type registration
8275 // ----------------------------------------------------------------------------
8277 void wxGrid::RegisterDataType(const wxString
& typeName
,
8278 wxGridCellRenderer
* renderer
,
8279 wxGridCellEditor
* editor
)
8281 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8285 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8287 wxString typeName
= m_table
->GetTypeName(row
, col
);
8288 return GetDefaultEditorForType(typeName
);
8291 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8293 wxString typeName
= m_table
->GetTypeName(row
, col
);
8294 return GetDefaultRendererForType(typeName
);
8298 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8300 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8301 if ( index
== wxNOT_FOUND
)
8303 wxFAIL_MSG(wxT("Unknown data type name"));
8308 return m_typeRegistry
->GetEditor(index
);
8312 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8314 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8315 if ( index
== wxNOT_FOUND
)
8317 wxFAIL_MSG(wxT("Unknown data type name"));
8322 return m_typeRegistry
->GetRenderer(index
);
8326 // ----------------------------------------------------------------------------
8328 // ----------------------------------------------------------------------------
8330 void wxGrid::EnableDragRowSize( bool enable
)
8332 m_canDragRowSize
= enable
;
8336 void wxGrid::EnableDragColSize( bool enable
)
8338 m_canDragColSize
= enable
;
8341 void wxGrid::EnableDragGridSize( bool enable
)
8343 m_canDragGridSize
= enable
;
8347 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8349 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8351 if ( resizeExistingRows
)
8354 if ( !GetBatchCount() )
8359 void wxGrid::SetRowSize( int row
, int height
)
8361 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8363 if ( m_rowHeights
.IsEmpty() )
8365 // need to really create the array
8369 int h
= wxMax( 0, height
);
8370 int diff
= h
- m_rowHeights
[row
];
8372 m_rowHeights
[row
] = h
;
8374 for ( i
= row
; i
< m_numRows
; i
++ )
8376 m_rowBottoms
[i
] += diff
;
8378 if ( !GetBatchCount() )
8382 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8384 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8386 if ( resizeExistingCols
)
8389 if ( !GetBatchCount() )
8394 void wxGrid::SetColSize( int col
, int width
)
8396 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8398 // should we check that it's bigger than GetColMinimalWidth(col) here?
8400 if ( m_colWidths
.IsEmpty() )
8402 // need to really create the array
8406 int w
= wxMax( 0, width
);
8407 int diff
= w
- m_colWidths
[col
];
8408 m_colWidths
[col
] = w
;
8411 for ( i
= col
; i
< m_numCols
; i
++ )
8413 m_colRights
[i
] += diff
;
8415 if ( !GetBatchCount() )
8420 void wxGrid::SetColMinimalWidth( int col
, int width
)
8422 m_colMinWidths
.Put(col
, width
);
8425 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8427 m_rowMinHeights
.Put(row
, width
);
8430 int wxGrid::GetColMinimalWidth(int col
) const
8432 long value
= m_colMinWidths
.Get(col
);
8433 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8436 int wxGrid::GetRowMinimalHeight(int row
) const
8438 long value
= m_rowMinHeights
.Get(row
);
8439 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8442 // ----------------------------------------------------------------------------
8444 // ----------------------------------------------------------------------------
8446 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8448 wxClientDC
dc(m_gridWin
);
8450 // init both of them to avoid compiler warnings, even if weo nly need one
8458 wxCoord extent
, extentMax
= 0;
8459 int max
= column
? m_numRows
: m_numCols
;
8460 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8467 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8468 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8471 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8472 extent
= column
? size
.x
: size
.y
;
8473 if ( extent
> extentMax
)
8484 // now also compare with the column label extent
8486 dc
.SetFont( GetLabelFont() );
8489 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8491 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8493 extent
= column
? w
: h
;
8494 if ( extent
> extentMax
)
8501 // empty column - give default extent (notice that if extentMax is less
8502 // than default extent but != 0, it's ok)
8503 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8509 // leave some space around text
8520 SetColSize(col
, extentMax
);
8521 if ( !GetBatchCount() )
8524 m_gridWin
->GetClientSize( &cw
, &ch
);
8525 wxRect
rect ( CellToRect( 0, col
) );
8527 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8528 rect
.width
= cw
- rect
.x
;
8529 rect
.height
= m_colLabelHeight
;
8530 m_colLabelWin
->Refresh( TRUE
, &rect
);
8535 SetRowSize(row
, extentMax
);
8536 if ( !GetBatchCount() )
8539 m_gridWin
->GetClientSize( &cw
, &ch
);
8540 wxRect
rect ( CellToRect( row
, 0 ) );
8542 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8543 rect
.width
= m_rowLabelWidth
;
8544 rect
.height
= ch
- rect
.y
;
8545 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8551 SetColMinimalWidth(col
, extentMax
);
8553 SetRowMinimalHeight(row
, extentMax
);
8557 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8559 int width
= m_rowLabelWidth
;
8564 for ( int col
= 0; col
< m_numCols
; col
++ )
8568 AutoSizeColumn(col
, setAsMin
);
8571 width
+= GetColWidth(col
);
8580 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8582 int height
= m_colLabelHeight
;
8587 for ( int row
= 0; row
< m_numRows
; row
++ )
8591 AutoSizeRow(row
, setAsMin
);
8594 height
+= GetRowHeight(row
);
8603 void wxGrid::AutoSize()
8607 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8609 // round up the size to a multiple of scroll step - this ensures that we
8610 // won't get the scrollbars if we're sized exactly to this width
8611 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
8612 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
8614 // distribute the extra space between teh columns/rows to avoid having
8615 // extra white space
8616 wxCoord diff
= sizeFit
.x
- size
.x
;
8619 // try to resize the columns uniformly
8620 wxCoord diffPerCol
= diff
/ m_numCols
;
8623 for ( int col
= 0; col
< m_numCols
; col
++ )
8625 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
8629 // add remaining amount to the last columns
8630 diff
-= diffPerCol
* m_numCols
;
8633 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
8635 SetColSize(col
, GetColWidth(col
) + 1);
8641 diff
= sizeFit
.y
- size
.y
;
8644 // try to resize the columns uniformly
8645 wxCoord diffPerRow
= diff
/ m_numRows
;
8648 for ( int row
= 0; row
< m_numRows
; row
++ )
8650 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
8654 // add remaining amount to the last rows
8655 diff
-= diffPerRow
* m_numRows
;
8658 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
8660 SetRowSize(row
, GetRowHeight(row
) + 1);
8667 SetClientSize(sizeFit
);
8670 wxSize
wxGrid::DoGetBestSize() const
8672 // don't set sizes, only calculate them
8673 wxGrid
*self
= (wxGrid
*)this; // const_cast
8675 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8676 self
->SetOrCalcRowSizes(TRUE
));
8685 wxPen
& wxGrid::GetDividerPen() const
8690 // ----------------------------------------------------------------------------
8691 // cell value accessor functions
8692 // ----------------------------------------------------------------------------
8694 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8698 m_table
->SetValue( row
, col
, s
);
8699 if ( !GetBatchCount() )
8701 wxClientDC
dc( m_gridWin
);
8703 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8706 if ( m_currentCellCoords
.GetRow() == row
&&
8707 m_currentCellCoords
.GetCol() == col
&&
8708 IsCellEditControlShown())
8709 // Note: If we are using IsCellEditControlEnabled,
8710 // this interacts badly with calling SetCellValue from
8711 // an EVT_GRID_CELL_CHANGE handler.
8713 HideCellEditControl();
8714 ShowCellEditControl(); // will reread data from table
8721 // ------ Block, row and col selection
8724 void wxGrid::SelectRow( int row
, bool addToSelected
)
8726 if ( IsSelection() && !addToSelected
)
8729 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8733 void wxGrid::SelectCol( int col
, bool addToSelected
)
8735 if ( IsSelection() && !addToSelected
)
8738 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8742 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8743 bool addToSelected
)
8745 if ( IsSelection() && !addToSelected
)
8748 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8749 FALSE
, addToSelected
);
8753 void wxGrid::SelectAll()
8755 if ( m_numRows
> 0 && m_numCols
> 0 )
8756 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8760 // ------ Cell, row and col deselection
8763 void wxGrid::DeselectRow( int row
)
8765 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8767 if ( m_selection
->IsInSelection(row
, 0 ) )
8768 m_selection
->ToggleCellSelection( row
, 0);
8772 int nCols
= GetNumberCols();
8773 for ( int i
= 0; i
< nCols
; i
++ )
8775 if ( m_selection
->IsInSelection(row
, i
) )
8776 m_selection
->ToggleCellSelection( row
, i
);
8781 void wxGrid::DeselectCol( int col
)
8783 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8785 if ( m_selection
->IsInSelection(0, col
) )
8786 m_selection
->ToggleCellSelection( 0, col
);
8790 int nRows
= GetNumberRows();
8791 for ( int i
= 0; i
< nRows
; i
++ )
8793 if ( m_selection
->IsInSelection(i
, col
) )
8794 m_selection
->ToggleCellSelection(i
, col
);
8799 void wxGrid::DeselectCell( int row
, int col
)
8801 if ( m_selection
->IsInSelection(row
, col
) )
8802 m_selection
->ToggleCellSelection(row
, col
);
8805 bool wxGrid::IsSelection()
8807 return ( m_selection
->IsSelection() ||
8808 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8809 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8812 bool wxGrid::IsInSelection( int row
, int col
)
8814 return ( m_selection
->IsInSelection( row
, col
) ||
8815 ( row
>= m_selectingTopLeft
.GetRow() &&
8816 col
>= m_selectingTopLeft
.GetCol() &&
8817 row
<= m_selectingBottomRight
.GetRow() &&
8818 col
<= m_selectingBottomRight
.GetCol() ) );
8821 void wxGrid::ClearSelection()
8823 m_selectingTopLeft
= wxGridNoCellCoords
;
8824 m_selectingBottomRight
= wxGridNoCellCoords
;
8825 m_selection
->ClearSelection();
8829 // This function returns the rectangle that encloses the given block
8830 // in device coords clipped to the client size of the grid window.
8832 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8833 const wxGridCellCoords
&bottomRight
)
8835 wxRect
rect( wxGridNoCellRect
);
8838 cellRect
= CellToRect( topLeft
);
8839 if ( cellRect
!= wxGridNoCellRect
)
8845 rect
= wxRect( 0, 0, 0, 0 );
8848 cellRect
= CellToRect( bottomRight
);
8849 if ( cellRect
!= wxGridNoCellRect
)
8855 return wxGridNoCellRect
;
8858 // convert to scrolled coords
8860 int left
, top
, right
, bottom
;
8861 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8862 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8865 m_gridWin
->GetClientSize( &cw
, &ch
);
8867 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8868 return wxRect( 0, 0, 0, 0);
8870 rect
.SetLeft( wxMax(0, left
) );
8871 rect
.SetTop( wxMax(0, top
) );
8872 rect
.SetRight( wxMin(cw
, right
) );
8873 rect
.SetBottom( wxMin(ch
, bottom
) );
8881 // ------ Grid event classes
8884 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8886 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8887 int row
, int col
, int x
, int y
, bool sel
,
8888 bool control
, bool shift
, bool alt
, bool meta
)
8889 : wxNotifyEvent( type
, id
)
8896 m_control
= control
;
8901 SetEventObject(obj
);
8905 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8907 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8908 int rowOrCol
, int x
, int y
,
8909 bool control
, bool shift
, bool alt
, bool meta
)
8910 : wxNotifyEvent( type
, id
)
8912 m_rowOrCol
= rowOrCol
;
8915 m_control
= control
;
8920 SetEventObject(obj
);
8924 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8926 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8927 const wxGridCellCoords
& topLeft
,
8928 const wxGridCellCoords
& bottomRight
,
8929 bool sel
, bool control
,
8930 bool shift
, bool alt
, bool meta
)
8931 : wxNotifyEvent( type
, id
)
8933 m_topLeft
= topLeft
;
8934 m_bottomRight
= bottomRight
;
8936 m_control
= control
;
8941 SetEventObject(obj
);
8945 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8947 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8948 wxObject
* obj
, int row
,
8949 int col
, wxControl
* ctrl
)
8950 : wxCommandEvent(type
, id
)
8952 SetEventObject(obj
);
8959 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
8961 #endif // wxUSE_GRID