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 wxPanel
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 wxGridCellAttr
*wxGridCellAttr::Clone() const
1903 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1904 if ( HasTextColour() )
1905 attr
->SetTextColour(GetTextColour());
1906 if ( HasBackgroundColour() )
1907 attr
->SetBackgroundColour(GetBackgroundColour());
1909 attr
->SetFont(GetFont());
1910 if ( HasAlignment() )
1911 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1915 attr
->SetRenderer(m_renderer
);
1916 m_renderer
->IncRef();
1920 attr
->SetEditor(m_editor
);
1925 attr
->SetReadOnly();
1927 attr
->SetKind( m_attrkind
);
1929 attr
->SetDefAttr(m_defGridAttr
);
1934 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1936 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1937 SetTextColour(mergefrom
->GetTextColour());
1938 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1939 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1940 if ( !HasFont() && mergefrom
->HasFont() )
1941 SetFont(mergefrom
->GetFont());
1942 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1944 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1945 SetAlignment(hAlign
, vAlign
);
1948 // Directly access member functions as GetRender/Editor don't just return
1949 // m_renderer/m_editor
1951 // Maybe add support for merge of Render and Editor?
1952 if (!HasRenderer() && mergefrom
->HasRenderer() )
1954 m_renderer
= mergefrom
->m_renderer
;
1955 m_renderer
->IncRef();
1957 if ( !HasEditor() && mergefrom
->HasEditor() )
1959 m_editor
= mergefrom
->m_editor
;
1962 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1963 SetReadOnly(mergefrom
->IsReadOnly());
1965 SetDefAttr(mergefrom
->m_defGridAttr
);
1968 const wxColour
& wxGridCellAttr::GetTextColour() const
1970 if (HasTextColour())
1974 else if (m_defGridAttr
!= this)
1976 return m_defGridAttr
->GetTextColour();
1980 wxFAIL_MSG(wxT("Missing default cell attribute"));
1981 return wxNullColour
;
1986 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1988 if (HasBackgroundColour())
1990 else if (m_defGridAttr
!= this)
1991 return m_defGridAttr
->GetBackgroundColour();
1994 wxFAIL_MSG(wxT("Missing default cell attribute"));
1995 return wxNullColour
;
2000 const wxFont
& wxGridCellAttr::GetFont() const
2004 else if (m_defGridAttr
!= this)
2005 return m_defGridAttr
->GetFont();
2008 wxFAIL_MSG(wxT("Missing default cell attribute"));
2014 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2018 if ( hAlign
) *hAlign
= m_hAlign
;
2019 if ( vAlign
) *vAlign
= m_vAlign
;
2021 else if (m_defGridAttr
!= this)
2022 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2025 wxFAIL_MSG(wxT("Missing default cell attribute"));
2030 // GetRenderer and GetEditor use a slightly different decision path about
2031 // which attribute to use. If a non-default attr object has one then it is
2032 // used, otherwise the default editor or renderer is fetched from the grid and
2033 // used. It should be the default for the data type of the cell. If it is
2034 // NULL (because the table has a type that the grid does not have in its
2035 // registry,) then the grid's default editor or renderer is used.
2037 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2039 wxGridCellRenderer
*renderer
;
2041 if ( m_renderer
&& this != m_defGridAttr
)
2043 // use the cells renderer if it has one
2044 renderer
= m_renderer
;
2047 else // no non default cell renderer
2049 // get default renderer for the data type
2052 // GetDefaultRendererForCell() will do IncRef() for us
2053 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2062 if ( this != m_defGridAttr
)
2064 // if we still don't have one then use the grid default
2065 // (no need for IncRef() here neither)
2066 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2068 else // default grid attr
2070 // use m_renderer which we had decided not to use initially
2071 renderer
= m_renderer
;
2078 // we're supposed to always find something
2079 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2084 // same as above, except for s/renderer/editor/g
2085 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2087 wxGridCellEditor
*editor
;
2089 if ( m_editor
&& this != m_defGridAttr
)
2091 // use the cells editor if it has one
2095 else // no non default cell editor
2097 // get default editor for the data type
2100 // GetDefaultEditorForCell() will do IncRef() for us
2101 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2110 if ( this != m_defGridAttr
)
2112 // if we still don't have one then use the grid default
2113 // (no need for IncRef() here neither)
2114 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2116 else // default grid attr
2118 // use m_editor which we had decided not to use initially
2126 // we're supposed to always find something
2127 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2132 // ----------------------------------------------------------------------------
2133 // wxGridCellAttrData
2134 // ----------------------------------------------------------------------------
2136 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2138 int n
= FindIndex(row
, col
);
2139 if ( n
== wxNOT_FOUND
)
2141 // add the attribute
2142 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2146 // free the old attribute
2147 m_attrs
[(size_t)n
].attr
->DecRef();
2151 // change the attribute
2152 m_attrs
[(size_t)n
].attr
= attr
;
2156 // remove this attribute
2157 m_attrs
.RemoveAt((size_t)n
);
2162 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2164 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2166 int n
= FindIndex(row
, col
);
2167 if ( n
!= wxNOT_FOUND
)
2169 attr
= m_attrs
[(size_t)n
].attr
;
2176 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2178 size_t count
= m_attrs
.GetCount();
2179 for ( size_t n
= 0; n
< count
; n
++ )
2181 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2182 wxCoord row
= coords
.GetRow();
2183 if ((size_t)row
>= pos
)
2187 // If rows inserted, include row counter where necessary
2188 coords
.SetRow(row
+ numRows
);
2190 else if (numRows
< 0)
2192 // If rows deleted ...
2193 if ((size_t)row
>= pos
- numRows
)
2195 // ...either decrement row counter (if row still exists)...
2196 coords
.SetRow(row
+ numRows
);
2200 // ...or remove the attribute
2201 m_attrs
.RemoveAt((size_t)n
);
2209 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2211 size_t count
= m_attrs
.GetCount();
2212 for ( size_t n
= 0; n
< count
; n
++ )
2214 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2215 wxCoord col
= coords
.GetCol();
2216 if ( (size_t)col
>= pos
)
2220 // If rows inserted, include row counter where necessary
2221 coords
.SetCol(col
+ numCols
);
2223 else if (numCols
< 0)
2225 // If rows deleted ...
2226 if ((size_t)col
>= pos
- numCols
)
2228 // ...either decrement row counter (if row still exists)...
2229 coords
.SetCol(col
+ numCols
);
2233 // ...or remove the attribute
2234 m_attrs
.RemoveAt((size_t)n
);
2242 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2244 size_t count
= m_attrs
.GetCount();
2245 for ( size_t n
= 0; n
< count
; n
++ )
2247 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2248 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2257 // ----------------------------------------------------------------------------
2258 // wxGridRowOrColAttrData
2259 // ----------------------------------------------------------------------------
2261 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2263 size_t count
= m_attrs
.Count();
2264 for ( size_t n
= 0; n
< count
; n
++ )
2266 m_attrs
[n
]->DecRef();
2270 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2272 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2274 int n
= m_rowsOrCols
.Index(rowOrCol
);
2275 if ( n
!= wxNOT_FOUND
)
2277 attr
= m_attrs
[(size_t)n
];
2284 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2286 int i
= m_rowsOrCols
.Index(rowOrCol
);
2287 if ( i
== wxNOT_FOUND
)
2289 // add the attribute
2290 m_rowsOrCols
.Add(rowOrCol
);
2295 size_t n
= (size_t)i
;
2298 // change the attribute
2299 m_attrs
[n
]->DecRef();
2304 // remove this attribute
2305 m_attrs
[n
]->DecRef();
2306 m_rowsOrCols
.RemoveAt(n
);
2307 m_attrs
.RemoveAt(n
);
2312 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2314 size_t count
= m_attrs
.GetCount();
2315 for ( size_t n
= 0; n
< count
; n
++ )
2317 int & rowOrCol
= m_rowsOrCols
[n
];
2318 if ( (size_t)rowOrCol
>= pos
)
2320 if ( numRowsOrCols
> 0 )
2322 // If rows inserted, include row counter where necessary
2323 rowOrCol
+= numRowsOrCols
;
2325 else if ( numRowsOrCols
< 0)
2327 // If rows deleted, either decrement row counter (if row still exists)
2328 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2329 rowOrCol
+= numRowsOrCols
;
2332 m_rowsOrCols
.RemoveAt((size_t)n
);
2333 m_attrs
.RemoveAt((size_t)n
);
2341 // ----------------------------------------------------------------------------
2342 // wxGridCellAttrProvider
2343 // ----------------------------------------------------------------------------
2345 wxGridCellAttrProvider::wxGridCellAttrProvider()
2347 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2350 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2355 void wxGridCellAttrProvider::InitData()
2357 m_data
= new wxGridCellAttrProviderData
;
2360 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2361 wxGridCellAttr::wxAttrKind kind
) const
2363 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2368 case (wxGridCellAttr::Any
):
2369 //Get cached merge attributes.
2370 // Currenlty not used as no cache implemented as not mutiable
2371 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2374 //Basicaly implement old version.
2375 //Also check merge cache, so we don't have to re-merge every time..
2376 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2377 *attrrow
= (wxGridCellAttr
*)NULL
,
2378 *attrcol
= (wxGridCellAttr
*)NULL
;
2380 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2381 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2382 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2384 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2385 // Two or move are non NULL
2386 attr
= new wxGridCellAttr
;
2387 attr
->SetKind(wxGridCellAttr::Merged
);
2391 attr
->MergeWith(attrcell
);
2395 attr
->MergeWith(attrcol
);
2399 attr
->MergeWith(attrrow
);
2402 //store merge attr if cache implemented
2404 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2408 // one or none is non null return it or null.
2409 if(attrrow
) attr
= attrrow
;
2410 if(attrcol
) attr
= attrcol
;
2411 if(attrcell
) attr
= attrcell
;
2415 case (wxGridCellAttr::Cell
):
2416 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2418 case (wxGridCellAttr::Col
):
2419 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2421 case (wxGridCellAttr::Row
):
2422 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2426 // (wxGridCellAttr::Default):
2427 // (wxGridCellAttr::Merged):
2434 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2440 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2443 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2448 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2451 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2456 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2459 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2463 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2465 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2469 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2473 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2475 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2479 // ----------------------------------------------------------------------------
2480 // wxGridTypeRegistry
2481 // ----------------------------------------------------------------------------
2483 wxGridTypeRegistry::~wxGridTypeRegistry()
2485 size_t count
= m_typeinfo
.Count();
2486 for ( size_t i
= 0; i
< count
; i
++ )
2487 delete m_typeinfo
[i
];
2491 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2492 wxGridCellRenderer
* renderer
,
2493 wxGridCellEditor
* editor
)
2495 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2497 // is it already registered?
2498 int loc
= FindRegisteredDataType(typeName
);
2499 if ( loc
!= wxNOT_FOUND
)
2501 delete m_typeinfo
[loc
];
2502 m_typeinfo
[loc
] = info
;
2506 m_typeinfo
.Add(info
);
2510 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2512 size_t count
= m_typeinfo
.GetCount();
2513 for ( size_t i
= 0; i
< count
; i
++ )
2515 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2524 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2526 int index
= FindRegisteredDataType(typeName
);
2527 if ( index
== wxNOT_FOUND
)
2529 // check whether this is one of the standard ones, in which case
2530 // register it "on the fly"
2532 if ( typeName
== wxGRID_VALUE_STRING
)
2534 RegisterDataType(wxGRID_VALUE_STRING
,
2535 new wxGridCellStringRenderer
,
2536 new wxGridCellTextEditor
);
2538 #endif // wxUSE_TEXTCTRL
2540 if ( typeName
== wxGRID_VALUE_BOOL
)
2542 RegisterDataType(wxGRID_VALUE_BOOL
,
2543 new wxGridCellBoolRenderer
,
2544 new wxGridCellBoolEditor
);
2546 #endif // wxUSE_CHECKBOX
2548 if ( typeName
== wxGRID_VALUE_NUMBER
)
2550 RegisterDataType(wxGRID_VALUE_NUMBER
,
2551 new wxGridCellNumberRenderer
,
2552 new wxGridCellNumberEditor
);
2554 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2556 RegisterDataType(wxGRID_VALUE_FLOAT
,
2557 new wxGridCellFloatRenderer
,
2558 new wxGridCellFloatEditor
);
2560 #endif // wxUSE_TEXTCTRL
2562 if ( typeName
== wxGRID_VALUE_CHOICE
)
2564 RegisterDataType(wxGRID_VALUE_CHOICE
,
2565 new wxGridCellStringRenderer
,
2566 new wxGridCellChoiceEditor
);
2568 #endif // wxUSE_COMBOBOX
2573 // we get here only if just added the entry for this type, so return
2575 index
= m_typeinfo
.GetCount() - 1;
2581 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2583 int index
= FindDataType(typeName
);
2584 if ( index
== wxNOT_FOUND
)
2586 // the first part of the typename is the "real" type, anything after ':'
2587 // are the parameters for the renderer
2588 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2589 if ( index
== wxNOT_FOUND
)
2594 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2595 wxGridCellRenderer
*rendererOld
= renderer
;
2596 renderer
= renderer
->Clone();
2597 rendererOld
->DecRef();
2599 wxGridCellEditor
*editor
= GetEditor(index
);
2600 wxGridCellEditor
*editorOld
= editor
;
2601 editor
= editor
->Clone();
2602 editorOld
->DecRef();
2604 // do it even if there are no parameters to reset them to defaults
2605 wxString params
= typeName
.AfterFirst(_T(':'));
2606 renderer
->SetParameters(params
);
2607 editor
->SetParameters(params
);
2609 // register the new typename
2610 RegisterDataType(typeName
, renderer
, editor
);
2612 // we just registered it, it's the last one
2613 index
= m_typeinfo
.GetCount() - 1;
2619 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2621 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2627 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2629 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2635 // ----------------------------------------------------------------------------
2637 // ----------------------------------------------------------------------------
2639 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2642 wxGridTableBase::wxGridTableBase()
2644 m_view
= (wxGrid
*) NULL
;
2645 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2648 wxGridTableBase::~wxGridTableBase()
2650 delete m_attrProvider
;
2653 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2655 delete m_attrProvider
;
2656 m_attrProvider
= attrProvider
;
2659 bool wxGridTableBase::CanHaveAttributes()
2661 if ( ! GetAttrProvider() )
2663 // use the default attr provider by default
2664 SetAttrProvider(new wxGridCellAttrProvider
);
2669 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2671 if ( m_attrProvider
)
2672 return m_attrProvider
->GetAttr(row
, col
, kind
);
2674 return (wxGridCellAttr
*)NULL
;
2677 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2679 if ( m_attrProvider
)
2681 attr
->SetKind(wxGridCellAttr::Cell
);
2682 m_attrProvider
->SetAttr(attr
, row
, col
);
2686 // as we take ownership of the pointer and don't store it, we must
2692 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2694 if ( m_attrProvider
)
2696 attr
->SetKind(wxGridCellAttr::Row
);
2697 m_attrProvider
->SetRowAttr(attr
, row
);
2701 // as we take ownership of the pointer and don't store it, we must
2707 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2709 if ( m_attrProvider
)
2711 attr
->SetKind(wxGridCellAttr::Col
);
2712 m_attrProvider
->SetColAttr(attr
, col
);
2716 // as we take ownership of the pointer and don't store it, we must
2722 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2723 size_t WXUNUSED(numRows
) )
2725 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2730 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2732 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2737 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2738 size_t WXUNUSED(numRows
) )
2740 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2745 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2746 size_t WXUNUSED(numCols
) )
2748 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2753 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2755 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2760 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2761 size_t WXUNUSED(numCols
) )
2763 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2769 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2772 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2773 // how much it makes sense to us geeks.
2777 wxString
wxGridTableBase::GetColLabelValue( int col
)
2779 // default col labels are:
2780 // cols 0 to 25 : A-Z
2781 // cols 26 to 675 : AA-ZZ
2786 for ( n
= 1; ; n
++ )
2788 s
+= (_T('A') + (wxChar
)( col%26
));
2790 if ( col
< 0 ) break;
2793 // reverse the string...
2795 for ( i
= 0; i
< n
; i
++ )
2804 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2806 return wxGRID_VALUE_STRING
;
2809 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2810 const wxString
& typeName
)
2812 return typeName
== wxGRID_VALUE_STRING
;
2815 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2817 return CanGetValueAs(row
, col
, typeName
);
2820 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2825 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2830 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2835 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2836 long WXUNUSED(value
) )
2840 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2841 double WXUNUSED(value
) )
2845 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2846 bool WXUNUSED(value
) )
2851 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2852 const wxString
& WXUNUSED(typeName
) )
2857 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2858 const wxString
& WXUNUSED(typeName
),
2859 void* WXUNUSED(value
) )
2863 //////////////////////////////////////////////////////////////////////
2865 // Message class for the grid table to send requests and notifications
2869 wxGridTableMessage::wxGridTableMessage()
2871 m_table
= (wxGridTableBase
*) NULL
;
2877 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2878 int commandInt1
, int commandInt2
)
2882 m_comInt1
= commandInt1
;
2883 m_comInt2
= commandInt2
;
2888 //////////////////////////////////////////////////////////////////////
2890 // A basic grid table for string data. An object of this class will
2891 // created by wxGrid if you don't specify an alternative table class.
2894 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2896 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2898 wxGridStringTable::wxGridStringTable()
2903 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2908 m_data
.Alloc( numRows
);
2911 sa
.Alloc( numCols
);
2912 for ( col
= 0; col
< numCols
; col
++ )
2914 sa
.Add( wxEmptyString
);
2917 for ( row
= 0; row
< numRows
; row
++ )
2923 wxGridStringTable::~wxGridStringTable()
2927 int wxGridStringTable::GetNumberRows()
2929 return m_data
.GetCount();
2932 int wxGridStringTable::GetNumberCols()
2934 if ( m_data
.GetCount() > 0 )
2935 return m_data
[0].GetCount();
2940 wxString
wxGridStringTable::GetValue( int row
, int col
)
2942 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2943 _T("invalid row or column index in wxGridStringTable") );
2945 return m_data
[row
][col
];
2948 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2950 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2951 _T("invalid row or column index in wxGridStringTable") );
2953 m_data
[row
][col
] = value
;
2956 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2958 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2959 _T("invalid row or column index in wxGridStringTable") );
2961 return (m_data
[row
][col
] == wxEmptyString
);
2964 void wxGridStringTable::Clear()
2967 int numRows
, numCols
;
2969 numRows
= m_data
.GetCount();
2972 numCols
= m_data
[0].GetCount();
2974 for ( row
= 0; row
< numRows
; row
++ )
2976 for ( col
= 0; col
< numCols
; col
++ )
2978 m_data
[row
][col
] = wxEmptyString
;
2985 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2989 size_t curNumRows
= m_data
.GetCount();
2990 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2991 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2993 if ( pos
>= curNumRows
)
2995 return AppendRows( numRows
);
2999 sa
.Alloc( curNumCols
);
3000 for ( col
= 0; col
< curNumCols
; col
++ )
3002 sa
.Add( wxEmptyString
);
3005 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
3007 m_data
.Insert( sa
, row
);
3011 wxGridTableMessage
msg( this,
3012 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3016 GetView()->ProcessTableMessage( msg
);
3022 bool wxGridStringTable::AppendRows( size_t numRows
)
3026 size_t curNumRows
= m_data
.GetCount();
3027 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3028 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3031 if ( curNumCols
> 0 )
3033 sa
.Alloc( curNumCols
);
3034 for ( col
= 0; col
< curNumCols
; col
++ )
3036 sa
.Add( wxEmptyString
);
3040 for ( row
= 0; row
< numRows
; row
++ )
3047 wxGridTableMessage
msg( this,
3048 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3051 GetView()->ProcessTableMessage( msg
);
3057 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3061 size_t curNumRows
= m_data
.GetCount();
3063 if ( pos
>= curNumRows
)
3066 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3067 pos
, numRows
, curNumRows
);
3068 wxFAIL_MSG( errmsg
);
3072 if ( numRows
> curNumRows
- pos
)
3074 numRows
= curNumRows
- pos
;
3077 if ( numRows
>= curNumRows
)
3079 m_data
.Empty(); // don't release memory just yet
3083 for ( n
= 0; n
< numRows
; n
++ )
3085 m_data
.RemoveAt( pos
);
3090 wxGridTableMessage
msg( this,
3091 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3095 GetView()->ProcessTableMessage( msg
);
3101 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3105 size_t curNumRows
= m_data
.GetCount();
3106 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3107 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3109 if ( pos
>= curNumCols
)
3111 return AppendCols( numCols
);
3114 for ( row
= 0; row
< curNumRows
; row
++ )
3116 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3118 m_data
[row
].Insert( wxEmptyString
, col
);
3123 wxGridTableMessage
msg( this,
3124 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3128 GetView()->ProcessTableMessage( msg
);
3134 bool wxGridStringTable::AppendCols( size_t numCols
)
3138 size_t curNumRows
= m_data
.GetCount();
3142 // TODO: something better than this ?
3144 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3149 for ( row
= 0; row
< curNumRows
; row
++ )
3151 for ( n
= 0; n
< numCols
; n
++ )
3153 m_data
[row
].Add( wxEmptyString
);
3159 wxGridTableMessage
msg( this,
3160 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3163 GetView()->ProcessTableMessage( msg
);
3169 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3173 size_t curNumRows
= m_data
.GetCount();
3174 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3175 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3177 if ( pos
>= curNumCols
)
3180 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3181 pos
, numCols
, curNumCols
);
3182 wxFAIL_MSG( errmsg
);
3186 if ( numCols
> curNumCols
- pos
)
3188 numCols
= curNumCols
- pos
;
3191 for ( row
= 0; row
< curNumRows
; row
++ )
3193 if ( numCols
>= curNumCols
)
3195 m_data
[row
].Clear();
3199 for ( n
= 0; n
< numCols
; n
++ )
3201 m_data
[row
].RemoveAt( pos
);
3207 wxGridTableMessage
msg( this,
3208 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3212 GetView()->ProcessTableMessage( msg
);
3218 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3220 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3222 // using default label
3224 return wxGridTableBase::GetRowLabelValue( row
);
3228 return m_rowLabels
[ row
];
3232 wxString
wxGridStringTable::GetColLabelValue( int col
)
3234 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3236 // using default label
3238 return wxGridTableBase::GetColLabelValue( col
);
3242 return m_colLabels
[ col
];
3246 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3248 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3250 int n
= m_rowLabels
.GetCount();
3252 for ( i
= n
; i
<= row
; i
++ )
3254 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3258 m_rowLabels
[row
] = value
;
3261 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3263 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3265 int n
= m_colLabels
.GetCount();
3267 for ( i
= n
; i
<= col
; i
++ )
3269 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3273 m_colLabels
[col
] = value
;
3278 //////////////////////////////////////////////////////////////////////
3279 //////////////////////////////////////////////////////////////////////
3281 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3283 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3284 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3285 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3286 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3287 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3288 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3291 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3293 const wxPoint
&pos
, const wxSize
&size
)
3294 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3299 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3303 // NO - don't do this because it will set both the x and y origin
3304 // coords to match the parent scrolled window and we just want to
3305 // set the y coord - MB
3307 // m_owner->PrepareDC( dc );
3310 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3311 dc
.SetDeviceOrigin( 0, -y
);
3313 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3314 m_owner
->DrawRowLabels( dc
, rows
);
3318 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3320 m_owner
->ProcessRowLabelMouseEvent( event
);
3324 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3326 m_owner
->GetEventHandler()->ProcessEvent(event
);
3330 // This seems to be required for wxMotif otherwise the mouse
3331 // cursor must be in the cell edit control to get key events
3333 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3335 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3338 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3340 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3345 //////////////////////////////////////////////////////////////////////
3347 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3349 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3350 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3351 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3352 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3353 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3354 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3357 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3359 const wxPoint
&pos
, const wxSize
&size
)
3360 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3365 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3369 // NO - don't do this because it will set both the x and y origin
3370 // coords to match the parent scrolled window and we just want to
3371 // set the x coord - MB
3373 // m_owner->PrepareDC( dc );
3376 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3377 dc
.SetDeviceOrigin( -x
, 0 );
3379 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3380 m_owner
->DrawColLabels( dc
, cols
);
3384 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3386 m_owner
->ProcessColLabelMouseEvent( event
);
3389 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3391 m_owner
->GetEventHandler()->ProcessEvent(event
);
3395 // This seems to be required for wxMotif otherwise the mouse
3396 // cursor must be in the cell edit control to get key events
3398 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3400 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3403 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3405 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3410 //////////////////////////////////////////////////////////////////////
3412 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3414 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3415 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3416 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3417 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3418 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3419 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3422 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3424 const wxPoint
&pos
, const wxSize
&size
)
3425 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3430 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3434 int client_height
= 0;
3435 int client_width
= 0;
3436 GetClientSize( &client_width
, &client_height
);
3438 dc
.SetPen( *wxBLACK_PEN
);
3439 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3440 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3442 dc
.SetPen( *wxWHITE_PEN
);
3443 dc
.DrawLine( 0, 0, client_width
, 0 );
3444 dc
.DrawLine( 0, 0, 0, client_height
);
3448 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3450 m_owner
->ProcessCornerLabelMouseEvent( event
);
3454 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3456 m_owner
->GetEventHandler()->ProcessEvent(event
);
3459 // This seems to be required for wxMotif otherwise the mouse
3460 // cursor must be in the cell edit control to get key events
3462 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3464 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3467 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3469 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3474 //////////////////////////////////////////////////////////////////////
3476 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3478 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3479 EVT_PAINT( wxGridWindow::OnPaint
)
3480 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3481 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3482 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3483 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3484 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3487 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3488 wxGridRowLabelWindow
*rowLblWin
,
3489 wxGridColLabelWindow
*colLblWin
,
3490 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3491 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3494 m_rowLabelWin
= rowLblWin
;
3495 m_colLabelWin
= colLblWin
;
3496 SetBackgroundColour(_T("WHITE"));
3500 wxGridWindow::~wxGridWindow()
3505 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3507 wxPaintDC
dc( this );
3508 m_owner
->PrepareDC( dc
);
3509 wxRegion reg
= GetUpdateRegion();
3510 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3511 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3512 #if WXGRID_DRAW_LINES
3513 m_owner
->DrawAllGridLines( dc
, reg
);
3515 m_owner
->DrawGridSpace( dc
);
3516 m_owner
->DrawHighlight( dc
, DirtyCells
);
3520 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3522 wxPanel::ScrollWindow( dx
, dy
, rect
);
3523 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3524 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3528 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3530 m_owner
->ProcessGridCellMouseEvent( event
);
3533 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3535 m_owner
->GetEventHandler()->ProcessEvent(event
);
3538 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3539 // cursor must be in the cell edit control to get key events
3541 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3543 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3546 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3548 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3551 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3556 //////////////////////////////////////////////////////////////////////
3559 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3561 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3562 EVT_PAINT( wxGrid::OnPaint
)
3563 EVT_SIZE( wxGrid::OnSize
)
3564 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3565 EVT_KEY_UP( wxGrid::OnKeyUp
)
3566 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3569 wxGrid::wxGrid( wxWindow
*parent
,
3574 const wxString
& name
)
3575 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3576 m_colMinWidths(GRID_HASH_SIZE
),
3577 m_rowMinHeights(GRID_HASH_SIZE
)
3585 // Must do this or ~wxScrollHelper will pop the wrong event handler
3586 SetTargetWindow(this);
3588 wxSafeDecRef(m_defaultCellAttr
);
3590 #ifdef DEBUG_ATTR_CACHE
3591 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3592 wxPrintf(_T("wxGrid attribute cache statistics: "
3593 "total: %u, hits: %u (%u%%)\n"),
3594 total
, gs_nAttrCacheHits
,
3595 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3601 delete m_typeRegistry
;
3607 // ----- internal init and update functions
3610 void wxGrid::Create()
3612 m_created
= FALSE
; // set to TRUE by CreateGrid
3614 m_table
= (wxGridTableBase
*) NULL
;
3617 m_cellEditCtrlEnabled
= FALSE
;
3619 m_defaultCellAttr
= new wxGridCellAttr
;
3620 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3622 // Set default cell attributes
3623 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3624 m_defaultCellAttr
->SetFont(GetFont());
3625 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3626 m_defaultCellAttr
->SetTextColour(
3627 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3628 m_defaultCellAttr
->SetBackgroundColour(
3629 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3630 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3631 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3636 m_currentCellCoords
= wxGridNoCellCoords
;
3638 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3639 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3641 // create the type registry
3642 m_typeRegistry
= new wxGridTypeRegistry
;
3644 // subwindow components that make up the wxGrid
3645 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3650 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3655 m_colLabelWin
= new wxGridColLabelWindow( this,
3660 m_gridWin
= new wxGridWindow( this,
3667 SetTargetWindow( m_gridWin
);
3673 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3674 wxGrid::wxGridSelectionModes selmode
)
3676 wxCHECK_MSG( !m_created
,
3678 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3680 m_numRows
= numRows
;
3681 m_numCols
= numCols
;
3683 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3684 m_table
->SetView( this );
3686 m_selection
= new wxGridSelection( this, selmode
);
3695 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3697 wxCHECK_RET( m_created
,
3698 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3700 m_selection
->SetSelectionMode( selmode
);
3703 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3704 wxGrid::wxGridSelectionModes selmode
)
3708 // RD: Actually, this should probably be allowed. I think it would be
3709 // nice to be able to switch multiple Tables in and out of a single
3710 // View at runtime. Is there anything in the implmentation that would
3713 // At least, you now have to cope with m_selection
3714 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3719 m_numRows
= table
->GetNumberRows();
3720 m_numCols
= table
->GetNumberCols();
3723 m_table
->SetView( this );
3726 m_selection
= new wxGridSelection( this, selmode
);
3739 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3740 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3742 if ( m_rowLabelWin
)
3744 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3748 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3751 m_labelTextColour
= wxColour( _T("BLACK") );
3754 m_attrCache
.row
= -1;
3756 // TODO: something better than this ?
3758 m_labelFont
= this->GetFont();
3759 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3761 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3762 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3764 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3765 m_colLabelVertAlign
= wxALIGN_TOP
;
3767 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3768 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3770 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3771 m_defaultRowHeight
+= 8;
3773 m_defaultRowHeight
+= 4;
3776 m_gridLineColour
= wxColour( 128, 128, 255 );
3777 m_gridLinesEnabled
= TRUE
;
3778 m_cellHighlightColour
= m_gridLineColour
;
3779 m_cellHighlightPenWidth
= 2;
3780 m_cellHighlightROPenWidth
= 1;
3782 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3783 m_winCapture
= (wxWindow
*)NULL
;
3784 m_canDragRowSize
= TRUE
;
3785 m_canDragColSize
= TRUE
;
3786 m_canDragGridSize
= TRUE
;
3788 m_dragRowOrCol
= -1;
3789 m_isDragging
= FALSE
;
3790 m_startDragPos
= wxDefaultPosition
;
3792 m_waitForSlowClick
= FALSE
;
3794 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3795 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3797 m_currentCellCoords
= wxGridNoCellCoords
;
3799 m_selectingTopLeft
= wxGridNoCellCoords
;
3800 m_selectingBottomRight
= wxGridNoCellCoords
;
3801 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3802 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3804 m_editable
= TRUE
; // default for whole grid
3806 m_inOnKeyDown
= FALSE
;
3813 // ----------------------------------------------------------------------------
3814 // the idea is to call these functions only when necessary because they create
3815 // quite big arrays which eat memory mostly unnecessary - in particular, if
3816 // default widths/heights are used for all rows/columns, we may not use these
3819 // with some extra code, it should be possible to only store the
3820 // widths/heights different from default ones but this will be done later...
3821 // ----------------------------------------------------------------------------
3823 void wxGrid::InitRowHeights()
3825 m_rowHeights
.Empty();
3826 m_rowBottoms
.Empty();
3828 m_rowHeights
.Alloc( m_numRows
);
3829 m_rowBottoms
.Alloc( m_numRows
);
3832 for ( int i
= 0; i
< m_numRows
; i
++ )
3834 m_rowHeights
.Add( m_defaultRowHeight
);
3835 rowBottom
+= m_defaultRowHeight
;
3836 m_rowBottoms
.Add( rowBottom
);
3840 void wxGrid::InitColWidths()
3842 m_colWidths
.Empty();
3843 m_colRights
.Empty();
3845 m_colWidths
.Alloc( m_numCols
);
3846 m_colRights
.Alloc( m_numCols
);
3848 for ( int i
= 0; i
< m_numCols
; i
++ )
3850 m_colWidths
.Add( m_defaultColWidth
);
3851 colRight
+= m_defaultColWidth
;
3852 m_colRights
.Add( colRight
);
3856 int wxGrid::GetColWidth(int col
) const
3858 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3861 int wxGrid::GetColLeft(int col
) const
3863 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3864 : m_colRights
[col
] - m_colWidths
[col
];
3867 int wxGrid::GetColRight(int col
) const
3869 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3873 int wxGrid::GetRowHeight(int row
) const
3875 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3878 int wxGrid::GetRowTop(int row
) const
3880 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3881 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3884 int wxGrid::GetRowBottom(int row
) const
3886 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3887 : m_rowBottoms
[row
];
3890 void wxGrid::CalcDimensions()
3893 GetClientSize( &cw
, &ch
);
3895 if ( m_colLabelWin
->IsShown() )
3896 cw
-= m_rowLabelWidth
;
3897 if ( m_rowLabelWin
->IsShown() )
3898 ch
-= m_colLabelHeight
;
3901 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3902 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3904 // preserve (more or less) the previous position
3906 GetViewStart( &x
, &y
);
3908 // maybe we don't need scrollbars at all?
3910 // also adjust the position to be valid for the new scroll rangs
3931 // do set scrollbar parameters
3932 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
3933 GetScrollX(w
), GetScrollY(h
), x
, y
,
3934 GetBatchCount() != 0);
3938 void wxGrid::CalcWindowSizes()
3941 GetClientSize( &cw
, &ch
);
3943 if ( m_cornerLabelWin
->IsShown() )
3944 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3946 if ( m_colLabelWin
->IsShown() )
3947 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3949 if ( m_rowLabelWin
->IsShown() )
3950 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3952 if ( m_gridWin
->IsShown() )
3953 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3957 // this is called when the grid table sends a message to say that it
3958 // has been redimensioned
3960 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3963 bool result
= FALSE
;
3966 // if we were using the default widths/heights so far, we must change them
3968 if ( m_colWidths
.IsEmpty() )
3973 if ( m_rowHeights
.IsEmpty() )
3979 switch ( msg
.GetId() )
3981 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3983 size_t pos
= msg
.GetCommandInt();
3984 int numRows
= msg
.GetCommandInt2();
3986 m_numRows
+= numRows
;
3988 if ( !m_rowHeights
.IsEmpty() )
3990 for ( i
= 0; i
< numRows
; i
++ )
3992 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3993 m_rowBottoms
.Insert( 0, pos
);
3997 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3999 for ( i
= pos
; i
< m_numRows
; i
++ )
4001 bottom
+= m_rowHeights
[i
];
4002 m_rowBottoms
[i
] = bottom
;
4005 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4007 // if we have just inserted cols into an empty grid the current
4008 // cell will be undefined...
4010 SetCurrentCell( 0, 0 );
4012 m_selection
->UpdateRows( pos
, numRows
);
4013 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4015 attrProvider
->UpdateAttrRows( pos
, numRows
);
4017 if ( !GetBatchCount() )
4020 m_rowLabelWin
->Refresh();
4026 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4028 int numRows
= msg
.GetCommandInt();
4029 int oldNumRows
= m_numRows
;
4030 m_numRows
+= numRows
;
4032 if ( !m_rowHeights
.IsEmpty() )
4034 for ( i
= 0; i
< numRows
; i
++ )
4036 m_rowHeights
.Add( m_defaultRowHeight
);
4037 m_rowBottoms
.Add( 0 );
4041 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4043 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4045 bottom
+= m_rowHeights
[i
];
4046 m_rowBottoms
[i
] = bottom
;
4049 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4051 // if we have just inserted cols into an empty grid the current
4052 // cell will be undefined...
4054 SetCurrentCell( 0, 0 );
4056 if ( !GetBatchCount() )
4059 m_rowLabelWin
->Refresh();
4065 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4067 size_t pos
= msg
.GetCommandInt();
4068 int numRows
= msg
.GetCommandInt2();
4069 m_numRows
-= numRows
;
4071 if ( !m_rowHeights
.IsEmpty() )
4073 for ( i
= 0; i
< numRows
; i
++ )
4075 m_rowHeights
.RemoveAt( pos
);
4076 m_rowBottoms
.RemoveAt( pos
);
4080 for ( i
= 0; i
< m_numRows
; i
++ )
4082 h
+= m_rowHeights
[i
];
4083 m_rowBottoms
[i
] = h
;
4088 m_currentCellCoords
= wxGridNoCellCoords
;
4092 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4093 m_currentCellCoords
.Set( 0, 0 );
4095 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4096 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4098 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4099 // ifdef'd out following patch from Paul Gammans
4101 // No need to touch column attributes, unless we
4102 // removed _all_ rows, in this case, we remove
4103 // all column attributes.
4104 // I hate to do this here, but the
4105 // needed data is not available inside UpdateAttrRows.
4106 if ( !GetNumberRows() )
4107 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4110 if ( !GetBatchCount() )
4113 m_rowLabelWin
->Refresh();
4119 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4121 size_t pos
= msg
.GetCommandInt();
4122 int numCols
= msg
.GetCommandInt2();
4123 m_numCols
+= numCols
;
4125 if ( !m_colWidths
.IsEmpty() )
4127 for ( i
= 0; i
< numCols
; i
++ )
4129 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4130 m_colRights
.Insert( 0, pos
);
4134 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4136 for ( i
= pos
; i
< m_numCols
; i
++ )
4138 right
+= m_colWidths
[i
];
4139 m_colRights
[i
] = right
;
4142 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4144 // if we have just inserted cols into an empty grid the current
4145 // cell will be undefined...
4147 SetCurrentCell( 0, 0 );
4149 m_selection
->UpdateCols( pos
, numCols
);
4150 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4152 attrProvider
->UpdateAttrCols( pos
, numCols
);
4153 if ( !GetBatchCount() )
4156 m_colLabelWin
->Refresh();
4163 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4165 int numCols
= msg
.GetCommandInt();
4166 int oldNumCols
= m_numCols
;
4167 m_numCols
+= numCols
;
4168 if ( !m_colWidths
.IsEmpty() )
4170 for ( i
= 0; i
< numCols
; i
++ )
4172 m_colWidths
.Add( m_defaultColWidth
);
4173 m_colRights
.Add( 0 );
4177 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4179 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4181 right
+= m_colWidths
[i
];
4182 m_colRights
[i
] = right
;
4185 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4187 // if we have just inserted cols into an empty grid the current
4188 // cell will be undefined...
4190 SetCurrentCell( 0, 0 );
4192 if ( !GetBatchCount() )
4195 m_colLabelWin
->Refresh();
4201 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4203 size_t pos
= msg
.GetCommandInt();
4204 int numCols
= msg
.GetCommandInt2();
4205 m_numCols
-= numCols
;
4207 if ( !m_colWidths
.IsEmpty() )
4209 for ( i
= 0; i
< numCols
; i
++ )
4211 m_colWidths
.RemoveAt( pos
);
4212 m_colRights
.RemoveAt( pos
);
4216 for ( i
= 0; i
< m_numCols
; i
++ )
4218 w
+= m_colWidths
[i
];
4224 m_currentCellCoords
= wxGridNoCellCoords
;
4228 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4229 m_currentCellCoords
.Set( 0, 0 );
4231 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4232 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4234 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4235 // ifdef'd out following patch from Paul Gammans
4237 // No need to touch row attributes, unless we
4238 // removed _all_ columns, in this case, we remove
4239 // all row attributes.
4240 // I hate to do this here, but the
4241 // needed data is not available inside UpdateAttrCols.
4242 if ( !GetNumberCols() )
4243 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4246 if ( !GetBatchCount() )
4249 m_colLabelWin
->Refresh();
4256 if (result
&& !GetBatchCount() )
4257 m_gridWin
->Refresh();
4262 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4264 wxRegionIterator
iter( reg
);
4267 wxArrayInt rowlabels
;
4274 // TODO: remove this when we can...
4275 // There is a bug in wxMotif that gives garbage update
4276 // rectangles if you jump-scroll a long way by clicking the
4277 // scrollbar with middle button. This is a work-around
4279 #if defined(__WXMOTIF__)
4281 m_gridWin
->GetClientSize( &cw
, &ch
);
4282 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4283 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4286 // logical bounds of update region
4289 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4290 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4292 // find the row labels within these bounds
4295 for ( row
= 0; row
< m_numRows
; row
++ )
4297 if ( GetRowBottom(row
) < top
)
4300 if ( GetRowTop(row
) > bottom
)
4303 rowlabels
.Add( row
);
4313 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4315 wxRegionIterator
iter( reg
);
4318 wxArrayInt colLabels
;
4325 // TODO: remove this when we can...
4326 // There is a bug in wxMotif that gives garbage update
4327 // rectangles if you jump-scroll a long way by clicking the
4328 // scrollbar with middle button. This is a work-around
4330 #if defined(__WXMOTIF__)
4332 m_gridWin
->GetClientSize( &cw
, &ch
);
4333 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4334 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4337 // logical bounds of update region
4340 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4341 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4343 // find the cells within these bounds
4346 for ( col
= 0; col
< m_numCols
; col
++ )
4348 if ( GetColRight(col
) < left
)
4351 if ( GetColLeft(col
) > right
)
4354 colLabels
.Add( col
);
4363 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4365 wxRegionIterator
iter( reg
);
4368 wxGridCellCoordsArray cellsExposed
;
4370 int left
, top
, right
, bottom
;
4375 // TODO: remove this when we can...
4376 // There is a bug in wxMotif that gives garbage update
4377 // rectangles if you jump-scroll a long way by clicking the
4378 // scrollbar with middle button. This is a work-around
4380 #if defined(__WXMOTIF__)
4382 m_gridWin
->GetClientSize( &cw
, &ch
);
4383 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4384 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4385 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4386 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4389 // logical bounds of update region
4391 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4392 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4394 // find the cells within these bounds
4397 for ( row
= 0; row
< m_numRows
; row
++ )
4399 if ( GetRowBottom(row
) <= top
)
4402 if ( GetRowTop(row
) > bottom
)
4406 for ( col
= 0; col
< m_numCols
; col
++ )
4408 if ( GetColRight(col
) <= left
)
4411 if ( GetColLeft(col
) > right
)
4414 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4421 return cellsExposed
;
4425 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4428 wxPoint
pos( event
.GetPosition() );
4429 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4431 if ( event
.Dragging() )
4433 m_isDragging
= TRUE
;
4435 if ( event
.LeftIsDown() )
4437 switch( m_cursorMode
)
4439 case WXGRID_CURSOR_RESIZE_ROW
:
4441 int cw
, ch
, left
, dummy
;
4442 m_gridWin
->GetClientSize( &cw
, &ch
);
4443 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4445 wxClientDC
dc( m_gridWin
);
4448 GetRowTop(m_dragRowOrCol
) +
4449 GetRowMinimalHeight(m_dragRowOrCol
) );
4450 dc
.SetLogicalFunction(wxINVERT
);
4451 if ( m_dragLastPos
>= 0 )
4453 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4455 dc
.DrawLine( left
, y
, left
+cw
, y
);
4460 case WXGRID_CURSOR_SELECT_ROW
:
4461 if ( (row
= YToRow( y
)) >= 0 )
4463 m_selection
->SelectRow( row
,
4464 event
.ControlDown(),
4470 // default label to suppress warnings about "enumeration value
4471 // 'xxx' not handled in switch
4479 m_isDragging
= FALSE
;
4482 // ------------ Entering or leaving the window
4484 if ( event
.Entering() || event
.Leaving() )
4486 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4490 // ------------ Left button pressed
4492 else if ( event
.LeftDown() )
4494 // don't send a label click event for a hit on the
4495 // edge of the row label - this is probably the user
4496 // wanting to resize the row
4498 if ( YToEdgeOfRow(y
) < 0 )
4502 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4504 if ( !event
.ShiftDown() && !event
.ControlDown() )
4506 if ( event
.ShiftDown() )
4507 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4510 GetNumberCols() - 1,
4511 event
.ControlDown(),
4516 m_selection
->SelectRow( row
,
4517 event
.ControlDown(),
4521 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4526 // starting to drag-resize a row
4528 if ( CanDragRowSize() )
4529 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4534 // ------------ Left double click
4536 else if (event
.LeftDClick() )
4538 if ( YToEdgeOfRow(y
) < 0 )
4541 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4546 // ------------ Left button released
4548 else if ( event
.LeftUp() )
4550 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4552 DoEndDragResizeRow();
4554 // Note: we are ending the event *after* doing
4555 // default processing in this case
4557 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4560 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4565 // ------------ Right button down
4567 else if ( event
.RightDown() )
4570 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4572 // no default action at the moment
4577 // ------------ Right double click
4579 else if ( event
.RightDClick() )
4582 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4584 // no default action at the moment
4589 // ------------ No buttons down and mouse moving
4591 else if ( event
.Moving() )
4593 m_dragRowOrCol
= YToEdgeOfRow( y
);
4594 if ( m_dragRowOrCol
>= 0 )
4596 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4598 // don't capture the mouse yet
4599 if ( CanDragRowSize() )
4600 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4603 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4605 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4611 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4614 wxPoint
pos( event
.GetPosition() );
4615 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4617 if ( event
.Dragging() )
4619 m_isDragging
= TRUE
;
4621 if ( event
.LeftIsDown() )
4623 switch( m_cursorMode
)
4625 case WXGRID_CURSOR_RESIZE_COL
:
4627 int cw
, ch
, dummy
, top
;
4628 m_gridWin
->GetClientSize( &cw
, &ch
);
4629 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4631 wxClientDC
dc( m_gridWin
);
4634 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4635 GetColMinimalWidth(m_dragRowOrCol
));
4636 dc
.SetLogicalFunction(wxINVERT
);
4637 if ( m_dragLastPos
>= 0 )
4639 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4641 dc
.DrawLine( x
, top
, x
, top
+ch
);
4646 case WXGRID_CURSOR_SELECT_COL
:
4647 if ( (col
= XToCol( x
)) >= 0 )
4649 m_selection
->SelectCol( col
,
4650 event
.ControlDown(),
4656 // default label to suppress warnings about "enumeration value
4657 // 'xxx' not handled in switch
4665 m_isDragging
= FALSE
;
4668 // ------------ Entering or leaving the window
4670 if ( event
.Entering() || event
.Leaving() )
4672 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4676 // ------------ Left button pressed
4678 else if ( event
.LeftDown() )
4680 // don't send a label click event for a hit on the
4681 // edge of the col label - this is probably the user
4682 // wanting to resize the col
4684 if ( XToEdgeOfCol(x
) < 0 )
4688 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4690 if ( !event
.ShiftDown() && !event
.ControlDown() )
4692 if ( event
.ShiftDown() )
4693 m_selection
->SelectBlock( 0,
4694 m_currentCellCoords
.GetCol(),
4695 GetNumberRows() - 1, col
,
4696 event
.ControlDown(),
4701 m_selection
->SelectCol( col
,
4702 event
.ControlDown(),
4706 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4711 // starting to drag-resize a col
4713 if ( CanDragColSize() )
4714 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4719 // ------------ Left double click
4721 if ( event
.LeftDClick() )
4723 if ( XToEdgeOfCol(x
) < 0 )
4726 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4731 // ------------ Left button released
4733 else if ( event
.LeftUp() )
4735 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4737 DoEndDragResizeCol();
4739 // Note: we are ending the event *after* doing
4740 // default processing in this case
4742 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4745 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4750 // ------------ Right button down
4752 else if ( event
.RightDown() )
4755 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4757 // no default action at the moment
4762 // ------------ Right double click
4764 else if ( event
.RightDClick() )
4767 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4769 // no default action at the moment
4774 // ------------ No buttons down and mouse moving
4776 else if ( event
.Moving() )
4778 m_dragRowOrCol
= XToEdgeOfCol( x
);
4779 if ( m_dragRowOrCol
>= 0 )
4781 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4783 // don't capture the cursor yet
4784 if ( CanDragColSize() )
4785 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4788 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4790 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4796 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4798 if ( event
.LeftDown() )
4800 // indicate corner label by having both row and
4803 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4809 else if ( event
.LeftDClick() )
4811 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4814 else if ( event
.RightDown() )
4816 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4818 // no default action at the moment
4822 else if ( event
.RightDClick() )
4824 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4826 // no default action at the moment
4831 void wxGrid::ChangeCursorMode(CursorMode mode
,
4836 static const wxChar
*cursorModes
[] =
4845 wxLogTrace(_T("grid"),
4846 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4847 win
== m_colLabelWin
? _T("colLabelWin")
4848 : win
? _T("rowLabelWin")
4850 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4851 #endif // __WXDEBUG__
4853 if ( mode
== m_cursorMode
&&
4854 win
== m_winCapture
&&
4855 captureMouse
== (m_winCapture
!= NULL
))
4860 // by default use the grid itself
4866 m_winCapture
->ReleaseMouse();
4867 m_winCapture
= (wxWindow
*)NULL
;
4870 m_cursorMode
= mode
;
4872 switch ( m_cursorMode
)
4874 case WXGRID_CURSOR_RESIZE_ROW
:
4875 win
->SetCursor( m_rowResizeCursor
);
4878 case WXGRID_CURSOR_RESIZE_COL
:
4879 win
->SetCursor( m_colResizeCursor
);
4883 win
->SetCursor( *wxSTANDARD_CURSOR
);
4886 // we need to capture mouse when resizing
4887 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4888 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4890 if ( captureMouse
&& resize
)
4892 win
->CaptureMouse();
4897 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4900 wxPoint
pos( event
.GetPosition() );
4901 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4903 wxGridCellCoords coords
;
4904 XYToCell( x
, y
, coords
);
4906 if ( event
.Dragging() )
4908 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4910 // Don't start doing anything until the mouse has been drug at
4911 // least 3 pixels in any direction...
4914 if (m_startDragPos
== wxDefaultPosition
)
4916 m_startDragPos
= pos
;
4919 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4923 m_isDragging
= TRUE
;
4924 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4926 // Hide the edit control, so it
4927 // won't interfer with drag-shrinking.
4928 if ( IsCellEditControlShown() )
4930 HideCellEditControl();
4931 SaveEditControlValue();
4934 // Have we captured the mouse yet?
4937 m_winCapture
= m_gridWin
;
4938 m_winCapture
->CaptureMouse();
4941 if ( coords
!= wxGridNoCellCoords
)
4943 if ( event
.ControlDown() )
4945 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4946 m_selectingKeyboard
= coords
;
4947 HighlightBlock ( m_selectingKeyboard
, coords
);
4951 if ( !IsSelection() )
4953 HighlightBlock( coords
, coords
);
4957 HighlightBlock( m_currentCellCoords
, coords
);
4961 if (! IsVisible(coords
))
4963 MakeCellVisible(coords
);
4964 // TODO: need to introduce a delay or something here. The
4965 // scrolling is way to fast, at least on MSW - also on GTK.
4969 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4971 int cw
, ch
, left
, dummy
;
4972 m_gridWin
->GetClientSize( &cw
, &ch
);
4973 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4975 wxClientDC
dc( m_gridWin
);
4977 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4978 GetRowMinimalHeight(m_dragRowOrCol
) );
4979 dc
.SetLogicalFunction(wxINVERT
);
4980 if ( m_dragLastPos
>= 0 )
4982 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4984 dc
.DrawLine( left
, y
, left
+cw
, y
);
4987 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4989 int cw
, ch
, dummy
, top
;
4990 m_gridWin
->GetClientSize( &cw
, &ch
);
4991 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4993 wxClientDC
dc( m_gridWin
);
4995 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4996 GetColMinimalWidth(m_dragRowOrCol
) );
4997 dc
.SetLogicalFunction(wxINVERT
);
4998 if ( m_dragLastPos
>= 0 )
5000 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5002 dc
.DrawLine( x
, top
, x
, top
+ch
);
5009 m_isDragging
= FALSE
;
5010 m_startDragPos
= wxDefaultPosition
;
5012 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5013 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5016 if ( event
.Entering() || event
.Leaving() )
5018 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5019 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5024 // ------------ Left button pressed
5026 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5028 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5033 if ( !event
.ControlDown() )
5035 if ( event
.ShiftDown() )
5037 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5038 m_currentCellCoords
.GetCol(),
5041 event
.ControlDown(),
5046 else if ( XToEdgeOfCol(x
) < 0 &&
5047 YToEdgeOfRow(y
) < 0 )
5049 DisableCellEditControl();
5050 MakeCellVisible( coords
);
5052 // if this is the second click on this cell then start
5054 if ( m_waitForSlowClick
&&
5055 (coords
== m_currentCellCoords
) &&
5056 CanEnableCellControl())
5058 EnableCellEditControl();
5060 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5061 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5064 editor
->StartingClick();
5068 m_waitForSlowClick
= FALSE
;
5072 if ( event
.ControlDown() )
5074 m_selection
->ToggleCellSelection( coords
.GetRow(),
5076 event
.ControlDown(),
5080 m_selectingTopLeft
= wxGridNoCellCoords
;
5081 m_selectingBottomRight
= wxGridNoCellCoords
;
5082 m_selectingKeyboard
= coords
;
5086 SetCurrentCell( coords
);
5087 if ( m_selection
->GetSelectionMode()
5088 != wxGrid::wxGridSelectCells
)
5089 HighlightBlock( coords
, coords
);
5091 m_waitForSlowClick
= TRUE
;
5098 // ------------ Left double click
5100 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5102 DisableCellEditControl();
5104 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5106 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5114 // ------------ Left button released
5116 else if ( event
.LeftUp() )
5118 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5120 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5121 m_selectingBottomRight
!= wxGridNoCellCoords
)
5125 m_winCapture
->ReleaseMouse();
5126 m_winCapture
= NULL
;
5128 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5129 m_selectingTopLeft
.GetCol(),
5130 m_selectingBottomRight
.GetRow(),
5131 m_selectingBottomRight
.GetCol(),
5132 event
.ControlDown(),
5136 m_selectingTopLeft
= wxGridNoCellCoords
;
5137 m_selectingBottomRight
= wxGridNoCellCoords
;
5140 // Show the edit control, if it has been hidden for
5142 ShowCellEditControl();
5144 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5146 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5147 DoEndDragResizeRow();
5149 // Note: we are ending the event *after* doing
5150 // default processing in this case
5152 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5154 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5156 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5157 DoEndDragResizeCol();
5159 // Note: we are ending the event *after* doing
5160 // default processing in this case
5162 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5169 // ------------ Right button down
5171 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5173 DisableCellEditControl();
5174 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5179 // no default action at the moment
5184 // ------------ Right double click
5186 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5188 DisableCellEditControl();
5189 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5194 // no default action at the moment
5198 // ------------ Moving and no button action
5200 else if ( event
.Moving() && !event
.IsButton() )
5202 int dragRow
= YToEdgeOfRow( y
);
5203 int dragCol
= XToEdgeOfCol( x
);
5205 // Dragging on the corner of a cell to resize in both
5206 // directions is not implemented yet...
5208 if ( dragRow
>= 0 && dragCol
>= 0 )
5210 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5216 m_dragRowOrCol
= dragRow
;
5218 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5220 if ( CanDragRowSize() && CanDragGridSize() )
5221 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5226 m_dragRowOrCol
= dragCol
;
5234 m_dragRowOrCol
= dragCol
;
5236 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5238 if ( CanDragColSize() && CanDragGridSize() )
5239 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5245 // Neither on a row or col edge
5247 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5249 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5255 void wxGrid::DoEndDragResizeRow()
5257 if ( m_dragLastPos
>= 0 )
5259 // erase the last line and resize the row
5261 int cw
, ch
, left
, dummy
;
5262 m_gridWin
->GetClientSize( &cw
, &ch
);
5263 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5265 wxClientDC
dc( m_gridWin
);
5267 dc
.SetLogicalFunction( wxINVERT
);
5268 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5269 HideCellEditControl();
5270 SaveEditControlValue();
5272 int rowTop
= GetRowTop(m_dragRowOrCol
);
5273 SetRowSize( m_dragRowOrCol
,
5274 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5276 if ( !GetBatchCount() )
5278 // Only needed to get the correct rect.y:
5279 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5281 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5282 rect
.width
= m_rowLabelWidth
;
5283 rect
.height
= ch
- rect
.y
;
5284 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5286 m_gridWin
->Refresh( FALSE
, &rect
);
5289 ShowCellEditControl();
5294 void wxGrid::DoEndDragResizeCol()
5296 if ( m_dragLastPos
>= 0 )
5298 // erase the last line and resize the col
5300 int cw
, ch
, dummy
, top
;
5301 m_gridWin
->GetClientSize( &cw
, &ch
);
5302 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5304 wxClientDC
dc( m_gridWin
);
5306 dc
.SetLogicalFunction( wxINVERT
);
5307 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5308 HideCellEditControl();
5309 SaveEditControlValue();
5311 int colLeft
= GetColLeft(m_dragRowOrCol
);
5312 SetColSize( m_dragRowOrCol
,
5313 wxMax( m_dragLastPos
- colLeft
,
5314 GetColMinimalWidth(m_dragRowOrCol
) ) );
5316 if ( !GetBatchCount() )
5318 // Only needed to get the correct rect.x:
5319 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5321 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5322 rect
.width
= cw
- rect
.x
;
5323 rect
.height
= m_colLabelHeight
;
5324 m_colLabelWin
->Refresh( TRUE
, &rect
);
5326 m_gridWin
->Refresh( FALSE
, &rect
);
5329 ShowCellEditControl();
5336 // ------ interaction with data model
5338 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5340 switch ( msg
.GetId() )
5342 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5343 return GetModelValues();
5345 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5346 return SetModelValues();
5348 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5349 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5350 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5351 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5352 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5353 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5354 return Redimension( msg
);
5363 // The behaviour of this function depends on the grid table class
5364 // Clear() function. For the default wxGridStringTable class the
5365 // behavious is to replace all cell contents with wxEmptyString but
5366 // not to change the number of rows or cols.
5368 void wxGrid::ClearGrid()
5372 if (IsCellEditControlEnabled())
5373 DisableCellEditControl();
5376 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5381 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5383 // TODO: something with updateLabels flag
5387 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5393 if (IsCellEditControlEnabled())
5394 DisableCellEditControl();
5396 return m_table
->InsertRows( pos
, numRows
);
5398 // the table will have sent the results of the insert row
5399 // operation to this view object as a grid table message
5405 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5407 // TODO: something with updateLabels flag
5411 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5415 return ( m_table
&& m_table
->AppendRows( numRows
) );
5416 // the table will have sent the results of the append row
5417 // operation to this view object as a grid table message
5421 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5423 // TODO: something with updateLabels flag
5427 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5433 if (IsCellEditControlEnabled())
5434 DisableCellEditControl();
5436 return (m_table
->DeleteRows( pos
, numRows
));
5437 // the table will have sent the results of the delete row
5438 // operation to this view object as a grid table message
5444 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5446 // TODO: something with updateLabels flag
5450 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5456 if (IsCellEditControlEnabled())
5457 DisableCellEditControl();
5459 return m_table
->InsertCols( pos
, numCols
);
5460 // the table will have sent the results of the insert col
5461 // operation to this view object as a grid table message
5467 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5469 // TODO: something with updateLabels flag
5473 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5477 return ( m_table
&& m_table
->AppendCols( numCols
) );
5478 // the table will have sent the results of the append col
5479 // operation to this view object as a grid table message
5483 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5485 // TODO: something with updateLabels flag
5489 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5495 if (IsCellEditControlEnabled())
5496 DisableCellEditControl();
5498 return ( m_table
->DeleteCols( pos
, numCols
) );
5499 // the table will have sent the results of the delete col
5500 // operation to this view object as a grid table message
5508 // ----- event handlers
5511 // Generate a grid event based on a mouse event and
5512 // return the result of ProcessEvent()
5514 int wxGrid::SendEvent( const wxEventType type
,
5516 wxMouseEvent
& mouseEv
)
5521 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5523 int rowOrCol
= (row
== -1 ? col
: row
);
5525 wxGridSizeEvent
gridEvt( GetId(),
5529 mouseEv
.GetX() + GetRowLabelSize(),
5530 mouseEv
.GetY() + GetColLabelSize(),
5531 mouseEv
.ControlDown(),
5532 mouseEv
.ShiftDown(),
5534 mouseEv
.MetaDown() );
5536 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5537 vetoed
= !gridEvt
.IsAllowed();
5539 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5541 // Right now, it should _never_ end up here!
5542 wxGridRangeSelectEvent
gridEvt( GetId(),
5546 m_selectingBottomRight
,
5548 mouseEv
.ControlDown(),
5549 mouseEv
.ShiftDown(),
5551 mouseEv
.MetaDown() );
5553 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5554 vetoed
= !gridEvt
.IsAllowed();
5558 wxGridEvent
gridEvt( GetId(),
5562 mouseEv
.GetX() + GetRowLabelSize(),
5563 mouseEv
.GetY() + GetColLabelSize(),
5565 mouseEv
.ControlDown(),
5566 mouseEv
.ShiftDown(),
5568 mouseEv
.MetaDown() );
5569 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5570 vetoed
= !gridEvt
.IsAllowed();
5573 // A Veto'd event may not be `claimed' so test this first
5574 if (vetoed
) return -1;
5575 return claimed
? 1 : 0;
5579 // Generate a grid event of specified type and return the result
5580 // of ProcessEvent().
5582 int wxGrid::SendEvent( const wxEventType type
,
5588 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5590 int rowOrCol
= (row
== -1 ? col
: row
);
5592 wxGridSizeEvent
gridEvt( GetId(),
5597 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5598 vetoed
= !gridEvt
.IsAllowed();
5602 wxGridEvent
gridEvt( GetId(),
5607 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5608 vetoed
= !gridEvt
.IsAllowed();
5611 // A Veto'd event may not be `claimed' so test this first
5612 if (vetoed
) return -1;
5613 return claimed
? 1 : 0;
5617 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5619 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5623 void wxGrid::OnSize( wxSizeEvent
& event
)
5625 // position the child windows
5628 // don't call CalcDimensions() from here, the base class handles the size
5634 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5636 if ( m_inOnKeyDown
)
5638 // shouldn't be here - we are going round in circles...
5640 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5643 m_inOnKeyDown
= TRUE
;
5645 // propagate the event up and see if it gets processed
5647 wxWindow
*parent
= GetParent();
5648 wxKeyEvent
keyEvt( event
);
5649 keyEvt
.SetEventObject( parent
);
5651 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5654 // try local handlers
5656 switch ( event
.KeyCode() )
5659 if ( event
.ControlDown() )
5661 MoveCursorUpBlock( event
.ShiftDown() );
5665 MoveCursorUp( event
.ShiftDown() );
5670 if ( event
.ControlDown() )
5672 MoveCursorDownBlock( event
.ShiftDown() );
5676 MoveCursorDown( event
.ShiftDown() );
5681 if ( event
.ControlDown() )
5683 MoveCursorLeftBlock( event
.ShiftDown() );
5687 MoveCursorLeft( event
.ShiftDown() );
5692 if ( event
.ControlDown() )
5694 MoveCursorRightBlock( event
.ShiftDown() );
5698 MoveCursorRight( event
.ShiftDown() );
5703 case WXK_NUMPAD_ENTER
:
5704 if ( event
.ControlDown() )
5706 event
.Skip(); // to let the edit control have the return
5710 if ( GetGridCursorRow() < GetNumberRows()-1 )
5712 MoveCursorDown( event
.ShiftDown() );
5716 // at the bottom of a column
5717 HideCellEditControl();
5718 SaveEditControlValue();
5728 if (event
.ShiftDown())
5730 if ( GetGridCursorCol() > 0 )
5732 MoveCursorLeft( FALSE
);
5737 HideCellEditControl();
5738 SaveEditControlValue();
5743 if ( GetGridCursorCol() < GetNumberCols()-1 )
5745 MoveCursorRight( FALSE
);
5750 HideCellEditControl();
5751 SaveEditControlValue();
5757 if ( event
.ControlDown() )
5759 MakeCellVisible( 0, 0 );
5760 SetCurrentCell( 0, 0 );
5769 if ( event
.ControlDown() )
5771 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5772 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5789 if ( event
.ControlDown() )
5791 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5792 m_currentCellCoords
.GetCol(),
5793 event
.ControlDown(),
5799 if ( !IsEditable() )
5801 MoveCursorRight( FALSE
);
5804 // Otherwise fall through to default
5807 // is it possible to edit the current cell at all?
5808 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5810 // yes, now check whether the cells editor accepts the key
5811 int row
= m_currentCellCoords
.GetRow();
5812 int col
= m_currentCellCoords
.GetCol();
5813 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5814 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5816 // <F2> is special and will always start editing, for
5817 // other keys - ask the editor itself
5818 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5819 || editor
->IsAcceptedKey(event
) )
5821 EnableCellEditControl();
5822 editor
->StartingKey(event
);
5834 // let others process char events with modifiers or all
5835 // char events for readonly cells
5842 m_inOnKeyDown
= FALSE
;
5845 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5847 // try local handlers
5849 if ( event
.KeyCode() == WXK_SHIFT
)
5851 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5852 m_selectingBottomRight
!= wxGridNoCellCoords
)
5853 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5854 m_selectingTopLeft
.GetCol(),
5855 m_selectingBottomRight
.GetRow(),
5856 m_selectingBottomRight
.GetCol(),
5857 event
.ControlDown(),
5861 m_selectingTopLeft
= wxGridNoCellCoords
;
5862 m_selectingBottomRight
= wxGridNoCellCoords
;
5863 m_selectingKeyboard
= wxGridNoCellCoords
;
5867 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5871 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5873 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5875 // the event has been intercepted - do nothing
5879 wxClientDC
dc(m_gridWin
);
5882 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5884 HideCellEditControl();
5885 DisableCellEditControl();
5887 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5890 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5891 if ( !m_gridLinesEnabled
)
5899 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5901 // Otherwise refresh redraws the highlight!
5902 m_currentCellCoords
= coords
;
5904 DrawGridCellArea(dc
,cells
);
5905 DrawAllGridLines( dc
, r
);
5909 m_currentCellCoords
= coords
;
5911 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5912 DrawCellHighlight(dc
, attr
);
5917 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5920 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5922 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5925 rightCol
= GetNumberCols() - 1;
5927 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5930 bottomRow
= GetNumberRows() - 1;
5932 if ( topRow
> bottomRow
)
5939 if ( leftCol
> rightCol
)
5946 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5947 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5949 if ( m_selectingTopLeft
!= updateTopLeft
||
5950 m_selectingBottomRight
!= updateBottomRight
)
5952 // Compute two optimal update rectangles:
5953 // Either one rectangle is a real subset of the
5954 // other, or they are (almost) disjoint!
5956 bool need_refresh
[4];
5960 need_refresh
[3] = FALSE
;
5963 // Store intermediate values
5964 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5965 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5966 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5967 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5969 // Determine the outer/inner coordinates.
5970 if (oldLeft
> leftCol
)
5976 if (oldTop
> topRow
)
5982 if (oldRight
< rightCol
)
5985 oldRight
= rightCol
;
5988 if (oldBottom
< bottomRow
)
5991 oldBottom
= bottomRow
;
5995 // Now, either the stuff marked old is the outer
5996 // rectangle or we don't have a situation where one
5997 // is contained in the other.
5999 if ( oldLeft
< leftCol
)
6001 need_refresh
[0] = TRUE
;
6002 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6004 wxGridCellCoords ( oldBottom
,
6008 if ( oldTop
< topRow
)
6010 need_refresh
[1] = TRUE
;
6011 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6013 wxGridCellCoords ( topRow
- 1,
6017 if ( oldRight
> rightCol
)
6019 need_refresh
[2] = TRUE
;
6020 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6022 wxGridCellCoords ( oldBottom
,
6026 if ( oldBottom
> bottomRow
)
6028 need_refresh
[3] = TRUE
;
6029 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6031 wxGridCellCoords ( oldBottom
,
6037 m_selectingTopLeft
= updateTopLeft
;
6038 m_selectingBottomRight
= updateBottomRight
;
6040 // various Refresh() calls
6041 for (i
= 0; i
< 4; i
++ )
6042 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6043 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6046 // never generate an event as it will be generated from
6047 // wxGridSelection::SelectBlock!
6048 // (old comment from when this was the body of SelectBlock)
6052 // ------ functions to get/send data (see also public functions)
6055 bool wxGrid::GetModelValues()
6059 // all we need to do is repaint the grid
6061 m_gridWin
->Refresh();
6069 bool wxGrid::SetModelValues()
6075 for ( row
= 0; row
< m_numRows
; row
++ )
6077 for ( col
= 0; col
< m_numCols
; col
++ )
6079 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6091 // Note - this function only draws cells that are in the list of
6092 // exposed cells (usually set from the update region by
6093 // CalcExposedCells)
6095 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6097 if ( !m_numRows
|| !m_numCols
) return;
6100 size_t numCells
= cells
.GetCount();
6102 for ( i
= 0; i
< numCells
; i
++ )
6104 DrawCell( dc
, cells
[i
] );
6109 void wxGrid::DrawGridSpace( wxDC
& dc
)
6112 m_gridWin
->GetClientSize( &cw
, &ch
);
6115 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6117 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6118 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6120 if ( right
> rightCol
|| bottom
> bottomRow
)
6123 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6125 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6126 dc
.SetPen( *wxTRANSPARENT_PEN
);
6128 if ( right
> rightCol
)
6130 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6133 if ( bottom
> bottomRow
)
6135 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6141 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6143 int row
= coords
.GetRow();
6144 int col
= coords
.GetCol();
6146 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6149 // we draw the cell border ourselves
6150 #if !WXGRID_DRAW_LINES
6151 if ( m_gridLinesEnabled
)
6152 DrawCellBorder( dc
, coords
);
6155 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6157 bool isCurrent
= coords
== m_currentCellCoords
;
6159 wxRect rect
= CellToRect( row
, col
);
6161 // if the editor is shown, we should use it and not the renderer
6162 // Note: However, only if it is really _shown_, i.e. not hidden!
6163 if ( isCurrent
&& IsCellEditControlShown() )
6165 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6166 editor
->PaintBackground(rect
, attr
);
6171 // but all the rest is drawn by the cell renderer and hence may be
6173 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6174 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6181 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6183 int row
= m_currentCellCoords
.GetRow();
6184 int col
= m_currentCellCoords
.GetCol();
6186 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6189 wxRect rect
= CellToRect(row
, col
);
6191 // hmmm... what could we do here to show that the cell is disabled?
6192 // for now, I just draw a thinner border than for the other ones, but
6193 // it doesn't look really good
6195 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6199 // The center of th drawn line is where the position/width/height of
6200 // the rectangle is actually at, (on wxMSW atr least,) so we will
6201 // reduce the size of the rectangle to compensate for the thickness of
6202 // the line. If this is too strange on non wxMSW platforms then
6203 // please #ifdef this appropriately.
6204 rect
.x
+= penWidth
/2;
6205 rect
.y
+= penWidth
/2;
6206 rect
.width
-= penWidth
-1;
6207 rect
.height
-= penWidth
-1;
6210 // Now draw the rectangle
6211 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6212 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6213 dc
.DrawRectangle(rect
);
6217 // VZ: my experiments with 3d borders...
6219 // how to properly set colours for arbitrary bg?
6220 wxCoord x1
= rect
.x
,
6222 x2
= rect
.x
+ rect
.width
-1,
6223 y2
= rect
.y
+ rect
.height
-1;
6225 dc
.SetPen(*wxWHITE_PEN
);
6226 dc
.DrawLine(x1
, y1
, x2
, y1
);
6227 dc
.DrawLine(x1
, y1
, x1
, y2
);
6229 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6230 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6232 dc
.SetPen(*wxBLACK_PEN
);
6233 dc
.DrawLine(x1
, y2
, x2
, y2
);
6234 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6239 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6241 int row
= coords
.GetRow();
6242 int col
= coords
.GetCol();
6243 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6246 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6248 // right hand border
6250 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6251 GetColRight(col
), GetRowBottom(row
) );
6255 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6256 GetColRight(col
), GetRowBottom(row
) );
6259 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6261 // This if block was previously in wxGrid::OnPaint but that doesn't
6262 // seem to get called under wxGTK - MB
6264 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6265 m_numRows
&& m_numCols
)
6267 m_currentCellCoords
.Set(0, 0);
6270 if ( IsCellEditControlShown() )
6272 // don't show highlight when the edit control is shown
6276 // if the active cell was repainted, repaint its highlight too because it
6277 // might have been damaged by the grid lines
6278 size_t count
= cells
.GetCount();
6279 for ( size_t n
= 0; n
< count
; n
++ )
6281 if ( cells
[n
] == m_currentCellCoords
)
6283 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6284 DrawCellHighlight(dc
, attr
);
6292 // TODO: remove this ???
6293 // This is used to redraw all grid lines e.g. when the grid line colour
6296 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6298 if ( !m_gridLinesEnabled
||
6300 !m_numCols
) return;
6302 int top
, bottom
, left
, right
;
6304 #if 0 //#ifndef __WXGTK__
6308 m_gridWin
->GetClientSize(&cw
, &ch
);
6310 // virtual coords of visible area
6312 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6313 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6318 reg
.GetBox(x
, y
, w
, h
);
6319 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6320 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6324 m_gridWin
->GetClientSize(&cw
, &ch
);
6325 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6326 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6329 // avoid drawing grid lines past the last row and col
6331 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6332 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6334 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6336 // horizontal grid lines
6339 for ( i
= 0; i
< m_numRows
; i
++ )
6341 int bot
= GetRowBottom(i
) - 1;
6350 dc
.DrawLine( left
, bot
, right
, bot
);
6355 // vertical grid lines
6357 for ( i
= 0; i
< m_numCols
; i
++ )
6359 int colRight
= GetColRight(i
) - 1;
6360 if ( colRight
> right
)
6365 if ( colRight
>= left
)
6367 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6373 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6375 if ( !m_numRows
) return;
6378 size_t numLabels
= rows
.GetCount();
6380 for ( i
= 0; i
< numLabels
; i
++ )
6382 DrawRowLabel( dc
, rows
[i
] );
6387 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6389 if ( GetRowHeight(row
) <= 0 )
6392 int rowTop
= GetRowTop(row
),
6393 rowBottom
= GetRowBottom(row
) - 1;
6395 dc
.SetPen( *wxBLACK_PEN
);
6396 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6397 m_rowLabelWidth
-1, rowBottom
);
6399 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6401 dc
.SetPen( *wxWHITE_PEN
);
6402 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6403 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6405 dc
.SetBackgroundMode( wxTRANSPARENT
);
6406 dc
.SetTextForeground( GetLabelTextColour() );
6407 dc
.SetFont( GetLabelFont() );
6410 GetRowLabelAlignment( &hAlign
, &vAlign
);
6414 rect
.SetY( GetRowTop(row
) + 2 );
6415 rect
.SetWidth( m_rowLabelWidth
- 4 );
6416 rect
.SetHeight( GetRowHeight(row
) - 4 );
6417 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6421 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6423 if ( !m_numCols
) return;
6426 size_t numLabels
= cols
.GetCount();
6428 for ( i
= 0; i
< numLabels
; i
++ )
6430 DrawColLabel( dc
, cols
[i
] );
6435 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6437 if ( GetColWidth(col
) <= 0 )
6440 int colLeft
= GetColLeft(col
),
6441 colRight
= GetColRight(col
) - 1;
6443 dc
.SetPen( *wxBLACK_PEN
);
6444 dc
.DrawLine( colRight
, 0,
6445 colRight
, m_colLabelHeight
-1 );
6447 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6448 colRight
, m_colLabelHeight
-1 );
6450 dc
.SetPen( *wxWHITE_PEN
);
6451 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6452 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6454 dc
.SetBackgroundMode( wxTRANSPARENT
);
6455 dc
.SetTextForeground( GetLabelTextColour() );
6456 dc
.SetFont( GetLabelFont() );
6458 dc
.SetBackgroundMode( wxTRANSPARENT
);
6459 dc
.SetTextForeground( GetLabelTextColour() );
6460 dc
.SetFont( GetLabelFont() );
6463 GetColLabelAlignment( &hAlign
, &vAlign
);
6466 rect
.SetX( colLeft
+ 2 );
6468 rect
.SetWidth( GetColWidth(col
) - 4 );
6469 rect
.SetHeight( m_colLabelHeight
- 4 );
6470 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6473 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6474 const wxString
& value
,
6479 wxArrayString lines
;
6481 dc
.SetClippingRegion( rect
);
6482 StringToLines( value
, lines
);
6485 //Forward to new API.
6486 DrawTextRectangle( dc
,
6494 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6495 const wxArrayString
& lines
,
6500 long textWidth
, textHeight
;
6501 long lineWidth
, lineHeight
;
6503 if ( lines
.GetCount() )
6505 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6506 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6509 switch ( horizAlign
)
6512 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6515 case wxALIGN_CENTRE
:
6516 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6525 switch ( vertAlign
)
6527 case wxALIGN_BOTTOM
:
6528 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6531 case wxALIGN_CENTRE
:
6532 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6541 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6543 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6548 dc
.DestroyClippingRegion();
6552 // Split multi line text up into an array of strings. Any existing
6553 // contents of the string array are preserved.
6555 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6559 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6560 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6562 while ( startPos
< (int)tVal
.Length() )
6564 pos
= tVal
.Mid(startPos
).Find( eol
);
6569 else if ( pos
== 0 )
6571 lines
.Add( wxEmptyString
);
6575 lines
.Add( value
.Mid(startPos
, pos
) );
6579 if ( startPos
< (int)value
.Length() )
6581 lines
.Add( value
.Mid( startPos
) );
6586 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6587 const wxArrayString
& lines
,
6588 long *width
, long *height
)
6595 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6597 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6598 w
= wxMax( w
, lineW
);
6607 // ------ Batch processing.
6609 void wxGrid::EndBatch()
6611 if ( m_batchCount
> 0 )
6614 if ( !m_batchCount
)
6617 m_rowLabelWin
->Refresh();
6618 m_colLabelWin
->Refresh();
6619 m_cornerLabelWin
->Refresh();
6620 m_gridWin
->Refresh();
6625 // Use this, rather than wxWindow::Refresh(), to force an immediate
6626 // repainting of the grid. Has no effect if you are already inside a
6627 // BeginBatch / EndBatch block.
6629 void wxGrid::ForceRefresh()
6637 // ------ Edit control functions
6641 void wxGrid::EnableEditing( bool edit
)
6643 // TODO: improve this ?
6645 if ( edit
!= m_editable
)
6647 if(!edit
) EnableCellEditControl(edit
);
6653 void wxGrid::EnableCellEditControl( bool enable
)
6658 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6659 SetCurrentCell( 0, 0 );
6661 if ( enable
!= m_cellEditCtrlEnabled
)
6665 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6668 // this should be checked by the caller!
6669 wxASSERT_MSG( CanEnableCellControl(),
6670 _T("can't enable editing for this cell!") );
6672 // do it before ShowCellEditControl()
6673 m_cellEditCtrlEnabled
= enable
;
6675 ShowCellEditControl();
6679 //FIXME:add veto support
6680 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
6682 HideCellEditControl();
6683 SaveEditControlValue();
6685 // do it after HideCellEditControl()
6686 m_cellEditCtrlEnabled
= enable
;
6691 bool wxGrid::IsCurrentCellReadOnly() const
6694 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6695 bool readonly
= attr
->IsReadOnly();
6701 bool wxGrid::CanEnableCellControl() const
6703 return m_editable
&& !IsCurrentCellReadOnly();
6706 bool wxGrid::IsCellEditControlEnabled() const
6708 // the cell edit control might be disable for all cells or just for the
6709 // current one if it's read only
6710 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6713 bool wxGrid::IsCellEditControlShown() const
6715 bool isShown
= FALSE
;
6717 if ( m_cellEditCtrlEnabled
)
6719 int row
= m_currentCellCoords
.GetRow();
6720 int col
= m_currentCellCoords
.GetCol();
6721 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6722 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6727 if ( editor
->IsCreated() )
6729 isShown
= editor
->GetControl()->IsShown();
6739 void wxGrid::ShowCellEditControl()
6741 if ( IsCellEditControlEnabled() )
6743 if ( !IsVisible( m_currentCellCoords
) )
6745 m_cellEditCtrlEnabled
= FALSE
;
6750 wxRect rect
= CellToRect( m_currentCellCoords
);
6751 int row
= m_currentCellCoords
.GetRow();
6752 int col
= m_currentCellCoords
.GetCol();
6754 // convert to scrolled coords
6756 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6758 // done in PaintBackground()
6760 // erase the highlight and the cell contents because the editor
6761 // might not cover the entire cell
6762 wxClientDC
dc( m_gridWin
);
6764 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6765 dc
.SetPen(*wxTRANSPARENT_PEN
);
6766 dc
.DrawRectangle(rect
);
6769 // cell is shifted by one pixel
6773 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6774 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6775 if ( !editor
->IsCreated() )
6777 editor
->Create(m_gridWin
, -1,
6778 new wxGridCellEditorEvtHandler(this, editor
));
6780 wxGridEditorCreatedEvent
evt(GetId(),
6781 wxEVT_GRID_EDITOR_CREATED
,
6785 editor
->GetControl());
6786 GetEventHandler()->ProcessEvent(evt
);
6789 editor
->Show( TRUE
, attr
);
6791 editor
->SetSize( rect
);
6793 editor
->BeginEdit(row
, col
, this);
6802 void wxGrid::HideCellEditControl()
6804 if ( IsCellEditControlEnabled() )
6806 int row
= m_currentCellCoords
.GetRow();
6807 int col
= m_currentCellCoords
.GetCol();
6809 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6810 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6811 editor
->Show( FALSE
);
6814 m_gridWin
->SetFocus();
6815 wxRect
rect( CellToRect( row
, col
) );
6816 m_gridWin
->Refresh( FALSE
, &rect
);
6821 void wxGrid::SaveEditControlValue()
6823 if ( IsCellEditControlEnabled() )
6825 int row
= m_currentCellCoords
.GetRow();
6826 int col
= m_currentCellCoords
.GetCol();
6828 wxString oldval
= GetCellValue(row
,col
);
6830 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6831 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6832 bool changed
= editor
->EndEdit(row
, col
, this);
6839 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
6840 m_currentCellCoords
.GetRow(),
6841 m_currentCellCoords
.GetCol() ) < 0 ) {
6843 // Event has been vetoed, set the data back.
6844 SetCellValue(row
,col
,oldval
);
6852 // ------ Grid location functions
6853 // Note that all of these functions work with the logical coordinates of
6854 // grid cells and labels so you will need to convert from device
6855 // coordinates for mouse events etc.
6858 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6860 int row
= YToRow(y
);
6861 int col
= XToCol(x
);
6863 if ( row
== -1 || col
== -1 )
6865 coords
= wxGridNoCellCoords
;
6869 coords
.Set( row
, col
);
6874 int wxGrid::YToRow( int y
)
6878 for ( i
= 0; i
< m_numRows
; i
++ )
6880 if ( y
< GetRowBottom(i
) )
6888 int wxGrid::XToCol( int x
)
6892 for ( i
= 0; i
< m_numCols
; i
++ )
6894 if ( x
< GetColRight(i
) )
6902 // return the row number that that the y coord is near the edge of, or
6903 // -1 if not near an edge
6905 int wxGrid::YToEdgeOfRow( int y
)
6909 for ( i
= 0; i
< m_numRows
; i
++ )
6911 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6913 d
= abs( y
- GetRowBottom(i
) );
6914 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6923 // return the col number that that the x coord is near the edge of, or
6924 // -1 if not near an edge
6926 int wxGrid::XToEdgeOfCol( int x
)
6930 for ( i
= 0; i
< m_numCols
; i
++ )
6932 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6934 d
= abs( x
- GetColRight(i
) );
6935 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6944 wxRect
wxGrid::CellToRect( int row
, int col
)
6946 wxRect
rect( -1, -1, -1, -1 );
6948 if ( row
>= 0 && row
< m_numRows
&&
6949 col
>= 0 && col
< m_numCols
)
6951 rect
.x
= GetColLeft(col
);
6952 rect
.y
= GetRowTop(row
);
6953 rect
.width
= GetColWidth(col
);
6954 rect
.height
= GetRowHeight(row
);
6957 // if grid lines are enabled, then the area of the cell is a bit smaller
6958 if (m_gridLinesEnabled
) {
6966 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6968 // get the cell rectangle in logical coords
6970 wxRect
r( CellToRect( row
, col
) );
6972 // convert to device coords
6974 int left
, top
, right
, bottom
;
6975 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6976 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6978 // check against the client area of the grid window
6981 m_gridWin
->GetClientSize( &cw
, &ch
);
6983 if ( wholeCellVisible
)
6985 // is the cell wholly visible ?
6987 return ( left
>= 0 && right
<= cw
&&
6988 top
>= 0 && bottom
<= ch
);
6992 // is the cell partly visible ?
6994 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6995 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7000 // make the specified cell location visible by doing a minimal amount
7003 void wxGrid::MakeCellVisible( int row
, int col
)
7007 int xpos
= -1, ypos
= -1;
7009 if ( row
>= 0 && row
< m_numRows
&&
7010 col
>= 0 && col
< m_numCols
)
7012 // get the cell rectangle in logical coords
7014 wxRect
r( CellToRect( row
, col
) );
7016 // convert to device coords
7018 int left
, top
, right
, bottom
;
7019 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7020 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7023 m_gridWin
->GetClientSize( &cw
, &ch
);
7029 else if ( bottom
> ch
)
7031 int h
= r
.GetHeight();
7033 for ( i
= row
-1; i
>= 0; i
-- )
7035 int rowHeight
= GetRowHeight(i
);
7036 if ( h
+ rowHeight
> ch
)
7043 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7044 // have rounding errors (this is important, because if we do, we
7045 // might not scroll at all and some cells won't be redrawn)
7047 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7049 ypos
+= GRID_SCROLL_LINE_Y
;
7056 else if ( right
> cw
)
7058 int w
= r
.GetWidth();
7060 for ( i
= col
-1; i
>= 0; i
-- )
7062 int colWidth
= GetColWidth(i
);
7063 if ( w
+ colWidth
> cw
)
7070 // see comment for ypos above
7071 xpos
+= GRID_SCROLL_LINE_X
;
7074 if ( xpos
!= -1 || ypos
!= -1 )
7077 xpos
/= GRID_SCROLL_LINE_X
;
7079 ypos
/= GRID_SCROLL_LINE_Y
;
7080 Scroll( xpos
, ypos
);
7088 // ------ Grid cursor movement functions
7091 bool wxGrid::MoveCursorUp( bool expandSelection
)
7093 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7094 m_currentCellCoords
.GetRow() >= 0 )
7096 if ( expandSelection
)
7098 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7099 m_selectingKeyboard
= m_currentCellCoords
;
7100 if ( m_selectingKeyboard
.GetRow() > 0 )
7102 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7103 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7104 m_selectingKeyboard
.GetCol() );
7105 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7108 else if ( m_currentCellCoords
.GetRow() > 0 )
7111 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7112 m_currentCellCoords
.GetCol() );
7113 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7114 m_currentCellCoords
.GetCol() );
7125 bool wxGrid::MoveCursorDown( bool expandSelection
)
7127 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7128 m_currentCellCoords
.GetRow() < m_numRows
)
7130 if ( expandSelection
)
7132 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7133 m_selectingKeyboard
= m_currentCellCoords
;
7134 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7136 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7137 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7138 m_selectingKeyboard
.GetCol() );
7139 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7142 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7145 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7146 m_currentCellCoords
.GetCol() );
7147 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7148 m_currentCellCoords
.GetCol() );
7159 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7161 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7162 m_currentCellCoords
.GetCol() >= 0 )
7164 if ( expandSelection
)
7166 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7167 m_selectingKeyboard
= m_currentCellCoords
;
7168 if ( m_selectingKeyboard
.GetCol() > 0 )
7170 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7171 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7172 m_selectingKeyboard
.GetCol() );
7173 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7176 else if ( m_currentCellCoords
.GetCol() > 0 )
7179 MakeCellVisible( m_currentCellCoords
.GetRow(),
7180 m_currentCellCoords
.GetCol() - 1 );
7181 SetCurrentCell( m_currentCellCoords
.GetRow(),
7182 m_currentCellCoords
.GetCol() - 1 );
7193 bool wxGrid::MoveCursorRight( bool expandSelection
)
7195 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7196 m_currentCellCoords
.GetCol() < m_numCols
)
7198 if ( expandSelection
)
7200 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7201 m_selectingKeyboard
= m_currentCellCoords
;
7202 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7204 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7205 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7206 m_selectingKeyboard
.GetCol() );
7207 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7210 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7213 MakeCellVisible( m_currentCellCoords
.GetRow(),
7214 m_currentCellCoords
.GetCol() + 1 );
7215 SetCurrentCell( m_currentCellCoords
.GetRow(),
7216 m_currentCellCoords
.GetCol() + 1 );
7227 bool wxGrid::MovePageUp()
7229 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7231 int row
= m_currentCellCoords
.GetRow();
7235 m_gridWin
->GetClientSize( &cw
, &ch
);
7237 int y
= GetRowTop(row
);
7238 int newRow
= YToRow( y
- ch
+ 1 );
7243 else if ( newRow
== row
)
7248 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7249 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7257 bool wxGrid::MovePageDown()
7259 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7261 int row
= m_currentCellCoords
.GetRow();
7262 if ( row
< m_numRows
)
7265 m_gridWin
->GetClientSize( &cw
, &ch
);
7267 int y
= GetRowTop(row
);
7268 int newRow
= YToRow( y
+ ch
);
7271 newRow
= m_numRows
- 1;
7273 else if ( newRow
== row
)
7278 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7279 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7287 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7290 m_currentCellCoords
!= wxGridNoCellCoords
&&
7291 m_currentCellCoords
.GetRow() > 0 )
7293 int row
= m_currentCellCoords
.GetRow();
7294 int col
= m_currentCellCoords
.GetCol();
7296 if ( m_table
->IsEmptyCell(row
, col
) )
7298 // starting in an empty cell: find the next block of
7304 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7307 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7309 // starting at the top of a block: find the next block
7315 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7320 // starting within a block: find the top of the block
7325 if ( m_table
->IsEmptyCell(row
, col
) )
7333 MakeCellVisible( row
, col
);
7334 if ( expandSelection
)
7336 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7337 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7342 SetCurrentCell( row
, col
);
7350 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7353 m_currentCellCoords
!= wxGridNoCellCoords
&&
7354 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7356 int row
= m_currentCellCoords
.GetRow();
7357 int col
= m_currentCellCoords
.GetCol();
7359 if ( m_table
->IsEmptyCell(row
, col
) )
7361 // starting in an empty cell: find the next block of
7364 while ( row
< m_numRows
-1 )
7367 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7370 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7372 // starting at the bottom of a block: find the next block
7375 while ( row
< m_numRows
-1 )
7378 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7383 // starting within a block: find the bottom of the block
7385 while ( row
< m_numRows
-1 )
7388 if ( m_table
->IsEmptyCell(row
, col
) )
7396 MakeCellVisible( row
, col
);
7397 if ( expandSelection
)
7399 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7400 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7405 SetCurrentCell( row
, col
);
7414 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7417 m_currentCellCoords
!= wxGridNoCellCoords
&&
7418 m_currentCellCoords
.GetCol() > 0 )
7420 int row
= m_currentCellCoords
.GetRow();
7421 int col
= m_currentCellCoords
.GetCol();
7423 if ( m_table
->IsEmptyCell(row
, col
) )
7425 // starting in an empty cell: find the next block of
7431 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7434 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7436 // starting at the left of a block: find the next block
7442 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7447 // starting within a block: find the left of the block
7452 if ( m_table
->IsEmptyCell(row
, col
) )
7460 MakeCellVisible( row
, col
);
7461 if ( expandSelection
)
7463 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7464 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7469 SetCurrentCell( row
, col
);
7478 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7481 m_currentCellCoords
!= wxGridNoCellCoords
&&
7482 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7484 int row
= m_currentCellCoords
.GetRow();
7485 int col
= m_currentCellCoords
.GetCol();
7487 if ( m_table
->IsEmptyCell(row
, col
) )
7489 // starting in an empty cell: find the next block of
7492 while ( col
< m_numCols
-1 )
7495 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7498 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7500 // starting at the right of a block: find the next block
7503 while ( col
< m_numCols
-1 )
7506 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7511 // starting within a block: find the right of the block
7513 while ( col
< m_numCols
-1 )
7516 if ( m_table
->IsEmptyCell(row
, col
) )
7524 MakeCellVisible( row
, col
);
7525 if ( expandSelection
)
7527 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7528 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7533 SetCurrentCell( row
, col
);
7545 // ------ Label values and formatting
7548 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7550 *horiz
= m_rowLabelHorizAlign
;
7551 *vert
= m_rowLabelVertAlign
;
7554 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7556 *horiz
= m_colLabelHorizAlign
;
7557 *vert
= m_colLabelVertAlign
;
7560 wxString
wxGrid::GetRowLabelValue( int row
)
7564 return m_table
->GetRowLabelValue( row
);
7574 wxString
wxGrid::GetColLabelValue( int col
)
7578 return m_table
->GetColLabelValue( col
);
7589 void wxGrid::SetRowLabelSize( int width
)
7591 width
= wxMax( width
, 0 );
7592 if ( width
!= m_rowLabelWidth
)
7596 m_rowLabelWin
->Show( FALSE
);
7597 m_cornerLabelWin
->Show( FALSE
);
7599 else if ( m_rowLabelWidth
== 0 )
7601 m_rowLabelWin
->Show( TRUE
);
7602 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7605 m_rowLabelWidth
= width
;
7612 void wxGrid::SetColLabelSize( int height
)
7614 height
= wxMax( height
, 0 );
7615 if ( height
!= m_colLabelHeight
)
7619 m_colLabelWin
->Show( FALSE
);
7620 m_cornerLabelWin
->Show( FALSE
);
7622 else if ( m_colLabelHeight
== 0 )
7624 m_colLabelWin
->Show( TRUE
);
7625 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7628 m_colLabelHeight
= height
;
7635 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7637 if ( m_labelBackgroundColour
!= colour
)
7639 m_labelBackgroundColour
= colour
;
7640 m_rowLabelWin
->SetBackgroundColour( colour
);
7641 m_colLabelWin
->SetBackgroundColour( colour
);
7642 m_cornerLabelWin
->SetBackgroundColour( colour
);
7644 if ( !GetBatchCount() )
7646 m_rowLabelWin
->Refresh();
7647 m_colLabelWin
->Refresh();
7648 m_cornerLabelWin
->Refresh();
7653 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7655 if ( m_labelTextColour
!= colour
)
7657 m_labelTextColour
= colour
;
7658 if ( !GetBatchCount() )
7660 m_rowLabelWin
->Refresh();
7661 m_colLabelWin
->Refresh();
7666 void wxGrid::SetLabelFont( const wxFont
& font
)
7669 if ( !GetBatchCount() )
7671 m_rowLabelWin
->Refresh();
7672 m_colLabelWin
->Refresh();
7676 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7678 // allow old (incorrect) defs to be used
7681 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7682 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7683 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7688 case wxTOP
: vert
= wxALIGN_TOP
; break;
7689 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7690 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7693 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7695 m_rowLabelHorizAlign
= horiz
;
7698 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7700 m_rowLabelVertAlign
= vert
;
7703 if ( !GetBatchCount() )
7705 m_rowLabelWin
->Refresh();
7709 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7711 // allow old (incorrect) defs to be used
7714 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7715 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7716 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7721 case wxTOP
: vert
= wxALIGN_TOP
; break;
7722 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7723 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7726 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7728 m_colLabelHorizAlign
= horiz
;
7731 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7733 m_colLabelVertAlign
= vert
;
7736 if ( !GetBatchCount() )
7738 m_colLabelWin
->Refresh();
7742 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7746 m_table
->SetRowLabelValue( row
, s
);
7747 if ( !GetBatchCount() )
7749 wxRect rect
= CellToRect( row
, 0);
7750 if ( rect
.height
> 0 )
7752 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7754 rect
.width
= m_rowLabelWidth
;
7755 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7761 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7765 m_table
->SetColLabelValue( col
, s
);
7766 if ( !GetBatchCount() )
7768 wxRect rect
= CellToRect( 0, col
);
7769 if ( rect
.width
> 0 )
7771 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7773 rect
.height
= m_colLabelHeight
;
7774 m_colLabelWin
->Refresh( TRUE
, &rect
);
7780 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7782 if ( m_gridLineColour
!= colour
)
7784 m_gridLineColour
= colour
;
7786 wxClientDC
dc( m_gridWin
);
7788 DrawAllGridLines( dc
, wxRegion() );
7793 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7795 if ( m_cellHighlightColour
!= colour
)
7797 m_cellHighlightColour
= colour
;
7799 wxClientDC
dc( m_gridWin
);
7801 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7802 DrawCellHighlight(dc
, attr
);
7807 void wxGrid::SetCellHighlightPenWidth(int width
)
7809 if (m_cellHighlightPenWidth
!= width
) {
7810 m_cellHighlightPenWidth
= width
;
7812 // Just redrawing the cell highlight is not enough since that won't
7813 // make any visible change if the the thickness is getting smaller.
7814 int row
= m_currentCellCoords
.GetRow();
7815 int col
= m_currentCellCoords
.GetCol();
7816 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7818 wxRect rect
= CellToRect(row
, col
);
7819 m_gridWin
->Refresh(TRUE
, &rect
);
7823 void wxGrid::SetCellHighlightROPenWidth(int width
)
7825 if (m_cellHighlightROPenWidth
!= width
) {
7826 m_cellHighlightROPenWidth
= width
;
7828 // Just redrawing the cell highlight is not enough since that won't
7829 // make any visible change if the the thickness is getting smaller.
7830 int row
= m_currentCellCoords
.GetRow();
7831 int col
= m_currentCellCoords
.GetCol();
7832 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7834 wxRect rect
= CellToRect(row
, col
);
7835 m_gridWin
->Refresh(TRUE
, &rect
);
7839 void wxGrid::EnableGridLines( bool enable
)
7841 if ( enable
!= m_gridLinesEnabled
)
7843 m_gridLinesEnabled
= enable
;
7845 if ( !GetBatchCount() )
7849 wxClientDC
dc( m_gridWin
);
7851 DrawAllGridLines( dc
, wxRegion() );
7855 m_gridWin
->Refresh();
7862 int wxGrid::GetDefaultRowSize()
7864 return m_defaultRowHeight
;
7867 int wxGrid::GetRowSize( int row
)
7869 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7871 return GetRowHeight(row
);
7874 int wxGrid::GetDefaultColSize()
7876 return m_defaultColWidth
;
7879 int wxGrid::GetColSize( int col
)
7881 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7883 return GetColWidth(col
);
7886 // ============================================================================
7887 // access to the grid attributes: each of them has a default value in the grid
7888 // itself and may be overidden on a per-cell basis
7889 // ============================================================================
7891 // ----------------------------------------------------------------------------
7892 // setting default attributes
7893 // ----------------------------------------------------------------------------
7895 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7897 m_defaultCellAttr
->SetBackgroundColour(col
);
7899 m_gridWin
->SetBackgroundColour(col
);
7903 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7905 m_defaultCellAttr
->SetTextColour(col
);
7908 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7910 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7913 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7915 m_defaultCellAttr
->SetFont(font
);
7918 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7920 m_defaultCellAttr
->SetRenderer(renderer
);
7923 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7925 m_defaultCellAttr
->SetEditor(editor
);
7928 // ----------------------------------------------------------------------------
7929 // access to the default attrbiutes
7930 // ----------------------------------------------------------------------------
7932 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7934 return m_defaultCellAttr
->GetBackgroundColour();
7937 wxColour
wxGrid::GetDefaultCellTextColour()
7939 return m_defaultCellAttr
->GetTextColour();
7942 wxFont
wxGrid::GetDefaultCellFont()
7944 return m_defaultCellAttr
->GetFont();
7947 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7949 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7952 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7954 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7957 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7959 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7962 // ----------------------------------------------------------------------------
7963 // access to cell attributes
7964 // ----------------------------------------------------------------------------
7966 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7968 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7969 wxColour colour
= attr
->GetBackgroundColour();
7974 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7976 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7977 wxColour colour
= attr
->GetTextColour();
7982 wxFont
wxGrid::GetCellFont( int row
, int col
)
7984 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7985 wxFont font
= attr
->GetFont();
7990 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7992 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7993 attr
->GetAlignment(horiz
, vert
);
7997 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7999 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8000 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8006 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8008 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8009 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8015 bool wxGrid::IsReadOnly(int row
, int col
) const
8017 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8018 bool isReadOnly
= attr
->IsReadOnly();
8023 // ----------------------------------------------------------------------------
8024 // attribute support: cache, automatic provider creation, ...
8025 // ----------------------------------------------------------------------------
8027 bool wxGrid::CanHaveAttributes()
8034 return m_table
->CanHaveAttributes();
8037 void wxGrid::ClearAttrCache()
8039 if ( m_attrCache
.row
!= -1 )
8041 wxSafeDecRef(m_attrCache
.attr
);
8042 m_attrCache
.attr
= NULL
;
8043 m_attrCache
.row
= -1;
8047 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8049 wxGrid
*self
= (wxGrid
*)this; // const_cast
8051 self
->ClearAttrCache();
8052 self
->m_attrCache
.row
= row
;
8053 self
->m_attrCache
.col
= col
;
8054 self
->m_attrCache
.attr
= attr
;
8058 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8060 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8062 *attr
= m_attrCache
.attr
;
8063 wxSafeIncRef(m_attrCache
.attr
);
8065 #ifdef DEBUG_ATTR_CACHE
8066 gs_nAttrCacheHits
++;
8073 #ifdef DEBUG_ATTR_CACHE
8074 gs_nAttrCacheMisses
++;
8080 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8082 wxGridCellAttr
*attr
;
8083 if ( !LookupAttr(row
, col
, &attr
) )
8085 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
8086 CacheAttr(row
, col
, attr
);
8090 attr
->SetDefAttr(m_defaultCellAttr
);
8094 attr
= m_defaultCellAttr
;
8101 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8103 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8104 wxASSERT_MSG( m_table
,
8105 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8107 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8110 attr
= new wxGridCellAttr
;
8112 // artificially inc the ref count to match DecRef() in caller
8114 m_table
->SetAttr(attr
, row
, col
);
8116 attr
->SetDefAttr(m_defaultCellAttr
);
8120 // ----------------------------------------------------------------------------
8121 // setting column attributes (wrappers around SetColAttr)
8122 // ----------------------------------------------------------------------------
8124 void wxGrid::SetColFormatBool(int col
)
8126 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8129 void wxGrid::SetColFormatNumber(int col
)
8131 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8134 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8136 wxString typeName
= wxGRID_VALUE_FLOAT
;
8137 if ( (width
!= -1) || (precision
!= -1) )
8139 typeName
<< _T(':') << width
<< _T(',') << precision
;
8142 SetColFormatCustom(col
, typeName
);
8145 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8147 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8149 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8151 attr
= new wxGridCellAttr
;
8152 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8153 attr
->SetRenderer(renderer
);
8155 SetColAttr(col
, attr
);
8159 // ----------------------------------------------------------------------------
8160 // setting cell attributes: this is forwarded to the table
8161 // ----------------------------------------------------------------------------
8163 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8165 if ( CanHaveAttributes() )
8167 m_table
->SetRowAttr(attr
, row
);
8176 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8178 if ( CanHaveAttributes() )
8180 m_table
->SetColAttr(attr
, col
);
8189 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8191 if ( CanHaveAttributes() )
8193 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8194 attr
->SetBackgroundColour(colour
);
8199 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8201 if ( CanHaveAttributes() )
8203 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8204 attr
->SetTextColour(colour
);
8209 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8211 if ( CanHaveAttributes() )
8213 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8214 attr
->SetFont(font
);
8219 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8221 if ( CanHaveAttributes() )
8223 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8224 attr
->SetAlignment(horiz
, vert
);
8229 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8231 if ( CanHaveAttributes() )
8233 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8234 attr
->SetRenderer(renderer
);
8239 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8241 if ( CanHaveAttributes() )
8243 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8244 attr
->SetEditor(editor
);
8249 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8251 if ( CanHaveAttributes() )
8253 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8254 attr
->SetReadOnly(isReadOnly
);
8259 // ----------------------------------------------------------------------------
8260 // Data type registration
8261 // ----------------------------------------------------------------------------
8263 void wxGrid::RegisterDataType(const wxString
& typeName
,
8264 wxGridCellRenderer
* renderer
,
8265 wxGridCellEditor
* editor
)
8267 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8271 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8273 wxString typeName
= m_table
->GetTypeName(row
, col
);
8274 return GetDefaultEditorForType(typeName
);
8277 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8279 wxString typeName
= m_table
->GetTypeName(row
, col
);
8280 return GetDefaultRendererForType(typeName
);
8284 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8286 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8287 if ( index
== wxNOT_FOUND
)
8289 wxFAIL_MSG(wxT("Unknown data type name"));
8294 return m_typeRegistry
->GetEditor(index
);
8298 wxGrid::GetDefaultRendererForType(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
->GetRenderer(index
);
8312 // ----------------------------------------------------------------------------
8314 // ----------------------------------------------------------------------------
8316 void wxGrid::EnableDragRowSize( bool enable
)
8318 m_canDragRowSize
= enable
;
8322 void wxGrid::EnableDragColSize( bool enable
)
8324 m_canDragColSize
= enable
;
8327 void wxGrid::EnableDragGridSize( bool enable
)
8329 m_canDragGridSize
= enable
;
8333 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8335 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8337 if ( resizeExistingRows
)
8340 if ( !GetBatchCount() )
8345 void wxGrid::SetRowSize( int row
, int height
)
8347 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8349 if ( m_rowHeights
.IsEmpty() )
8351 // need to really create the array
8355 int h
= wxMax( 0, height
);
8356 int diff
= h
- m_rowHeights
[row
];
8358 m_rowHeights
[row
] = h
;
8360 for ( i
= row
; i
< m_numRows
; i
++ )
8362 m_rowBottoms
[i
] += diff
;
8364 if ( !GetBatchCount() )
8368 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8370 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8372 if ( resizeExistingCols
)
8375 if ( !GetBatchCount() )
8380 void wxGrid::SetColSize( int col
, int width
)
8382 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8384 // should we check that it's bigger than GetColMinimalWidth(col) here?
8386 if ( m_colWidths
.IsEmpty() )
8388 // need to really create the array
8392 int w
= wxMax( 0, width
);
8393 int diff
= w
- m_colWidths
[col
];
8394 m_colWidths
[col
] = w
;
8397 for ( i
= col
; i
< m_numCols
; i
++ )
8399 m_colRights
[i
] += diff
;
8401 if ( !GetBatchCount() )
8406 void wxGrid::SetColMinimalWidth( int col
, int width
)
8408 m_colMinWidths
.Put(col
, width
);
8411 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8413 m_rowMinHeights
.Put(row
, width
);
8416 int wxGrid::GetColMinimalWidth(int col
) const
8418 long value
= m_colMinWidths
.Get(col
);
8419 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8422 int wxGrid::GetRowMinimalHeight(int row
) const
8424 long value
= m_rowMinHeights
.Get(row
);
8425 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8428 // ----------------------------------------------------------------------------
8430 // ----------------------------------------------------------------------------
8432 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8434 wxClientDC
dc(m_gridWin
);
8436 // init both of them to avoid compiler warnings, even if weo nly need one
8444 wxCoord extent
, extentMax
= 0;
8445 int max
= column
? m_numRows
: m_numCols
;
8446 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8453 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8454 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8457 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8458 extent
= column
? size
.x
: size
.y
;
8459 if ( extent
> extentMax
)
8470 // now also compare with the column label extent
8472 dc
.SetFont( GetLabelFont() );
8475 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8477 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8479 extent
= column
? w
: h
;
8480 if ( extent
> extentMax
)
8487 // empty column - give default extent (notice that if extentMax is less
8488 // than default extent but != 0, it's ok)
8489 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8495 // leave some space around text
8506 SetColSize(col
, extentMax
);
8507 if ( !GetBatchCount() )
8510 m_gridWin
->GetClientSize( &cw
, &ch
);
8511 wxRect
rect ( CellToRect( 0, col
) );
8513 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8514 rect
.width
= cw
- rect
.x
;
8515 rect
.height
= m_colLabelHeight
;
8516 m_colLabelWin
->Refresh( TRUE
, &rect
);
8521 SetRowSize(row
, extentMax
);
8522 if ( !GetBatchCount() )
8525 m_gridWin
->GetClientSize( &cw
, &ch
);
8526 wxRect
rect ( CellToRect( row
, 0 ) );
8528 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8529 rect
.width
= m_rowLabelWidth
;
8530 rect
.height
= ch
- rect
.y
;
8531 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8537 SetColMinimalWidth(col
, extentMax
);
8539 SetRowMinimalHeight(row
, extentMax
);
8543 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8545 int width
= m_rowLabelWidth
;
8550 for ( int col
= 0; col
< m_numCols
; col
++ )
8554 AutoSizeColumn(col
, setAsMin
);
8557 width
+= GetColWidth(col
);
8566 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8568 int height
= m_colLabelHeight
;
8573 for ( int row
= 0; row
< m_numRows
; row
++ )
8577 AutoSizeRow(row
, setAsMin
);
8580 height
+= GetRowHeight(row
);
8589 void wxGrid::AutoSize()
8593 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8595 // round up the size to a multiple of scroll step - this ensures that we
8596 // won't get the scrollbars if we're sized exactly to this width
8597 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
8598 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
8600 // distribute the extra space between teh columns/rows to avoid having
8601 // extra white space
8602 wxCoord diff
= sizeFit
.x
- size
.x
;
8605 // try to resize the columns uniformly
8606 wxCoord diffPerCol
= diff
/ m_numCols
;
8609 for ( int col
= 0; col
< m_numCols
; col
++ )
8611 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
8615 // add remaining amount to the last columns
8616 diff
-= diffPerCol
* m_numCols
;
8619 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
8621 SetColSize(col
, GetColWidth(col
) + 1);
8627 diff
= sizeFit
.y
- size
.y
;
8630 // try to resize the columns uniformly
8631 wxCoord diffPerRow
= diff
/ m_numRows
;
8634 for ( int row
= 0; row
< m_numRows
; row
++ )
8636 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
8640 // add remaining amount to the last rows
8641 diff
-= diffPerRow
* m_numRows
;
8644 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
8646 SetRowSize(row
, GetRowHeight(row
) + 1);
8653 SetClientSize(sizeFit
);
8656 wxSize
wxGrid::DoGetBestSize() const
8658 // don't set sizes, only calculate them
8659 wxGrid
*self
= (wxGrid
*)this; // const_cast
8661 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8662 self
->SetOrCalcRowSizes(TRUE
));
8671 wxPen
& wxGrid::GetDividerPen() const
8676 // ----------------------------------------------------------------------------
8677 // cell value accessor functions
8678 // ----------------------------------------------------------------------------
8680 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8684 m_table
->SetValue( row
, col
, s
);
8685 if ( !GetBatchCount() )
8687 wxClientDC
dc( m_gridWin
);
8689 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8692 if ( m_currentCellCoords
.GetRow() == row
&&
8693 m_currentCellCoords
.GetCol() == col
&&
8694 IsCellEditControlShown())
8695 // Note: If we are using IsCellEditControlEnabled,
8696 // this interacts badly with calling SetCellValue from
8697 // an EVT_GRID_CELL_CHANGE handler.
8699 HideCellEditControl();
8700 ShowCellEditControl(); // will reread data from table
8707 // ------ Block, row and col selection
8710 void wxGrid::SelectRow( int row
, bool addToSelected
)
8712 if ( IsSelection() && !addToSelected
)
8715 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8719 void wxGrid::SelectCol( int col
, bool addToSelected
)
8721 if ( IsSelection() && !addToSelected
)
8724 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8728 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8729 bool addToSelected
)
8731 if ( IsSelection() && !addToSelected
)
8734 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8735 FALSE
, addToSelected
);
8739 void wxGrid::SelectAll()
8741 if ( m_numRows
> 0 && m_numCols
> 0 )
8742 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8746 // ------ Cell, row and col deselection
8749 void wxGrid::DeselectRow( int row
)
8751 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8753 if ( m_selection
->IsInSelection(row
, 0 ) )
8754 m_selection
->ToggleCellSelection( row
, 0);
8758 int nCols
= GetNumberCols();
8759 for ( int i
= 0; i
< nCols
; i
++ )
8761 if ( m_selection
->IsInSelection(row
, i
) )
8762 m_selection
->ToggleCellSelection( row
, i
);
8767 void wxGrid::DeselectCol( int col
)
8769 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8771 if ( m_selection
->IsInSelection(0, col
) )
8772 m_selection
->ToggleCellSelection( 0, col
);
8776 int nRows
= GetNumberRows();
8777 for ( int i
= 0; i
< nRows
; i
++ )
8779 if ( m_selection
->IsInSelection(i
, col
) )
8780 m_selection
->ToggleCellSelection(i
, col
);
8785 void wxGrid::DeselectCell( int row
, int col
)
8787 if ( m_selection
->IsInSelection(row
, col
) )
8788 m_selection
->ToggleCellSelection(row
, col
);
8791 bool wxGrid::IsSelection()
8793 return ( m_selection
->IsSelection() ||
8794 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8795 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8798 bool wxGrid::IsInSelection( int row
, int col
)
8800 return ( m_selection
->IsInSelection( row
, col
) ||
8801 ( row
>= m_selectingTopLeft
.GetRow() &&
8802 col
>= m_selectingTopLeft
.GetCol() &&
8803 row
<= m_selectingBottomRight
.GetRow() &&
8804 col
<= m_selectingBottomRight
.GetCol() ) );
8807 void wxGrid::ClearSelection()
8809 m_selectingTopLeft
= wxGridNoCellCoords
;
8810 m_selectingBottomRight
= wxGridNoCellCoords
;
8811 m_selection
->ClearSelection();
8815 // This function returns the rectangle that encloses the given block
8816 // in device coords clipped to the client size of the grid window.
8818 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8819 const wxGridCellCoords
&bottomRight
)
8821 wxRect
rect( wxGridNoCellRect
);
8824 cellRect
= CellToRect( topLeft
);
8825 if ( cellRect
!= wxGridNoCellRect
)
8831 rect
= wxRect( 0, 0, 0, 0 );
8834 cellRect
= CellToRect( bottomRight
);
8835 if ( cellRect
!= wxGridNoCellRect
)
8841 return wxGridNoCellRect
;
8844 // convert to scrolled coords
8846 int left
, top
, right
, bottom
;
8847 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8848 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8851 m_gridWin
->GetClientSize( &cw
, &ch
);
8853 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8854 return wxRect( 0, 0, 0, 0);
8856 rect
.SetLeft( wxMax(0, left
) );
8857 rect
.SetTop( wxMax(0, top
) );
8858 rect
.SetRight( wxMin(cw
, right
) );
8859 rect
.SetBottom( wxMin(ch
, bottom
) );
8867 // ------ Grid event classes
8870 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8872 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8873 int row
, int col
, int x
, int y
, bool sel
,
8874 bool control
, bool shift
, bool alt
, bool meta
)
8875 : wxNotifyEvent( type
, id
)
8882 m_control
= control
;
8887 SetEventObject(obj
);
8891 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8893 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8894 int rowOrCol
, int x
, int y
,
8895 bool control
, bool shift
, bool alt
, bool meta
)
8896 : wxNotifyEvent( type
, id
)
8898 m_rowOrCol
= rowOrCol
;
8901 m_control
= control
;
8906 SetEventObject(obj
);
8910 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8912 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8913 const wxGridCellCoords
& topLeft
,
8914 const wxGridCellCoords
& bottomRight
,
8915 bool sel
, bool control
,
8916 bool shift
, bool alt
, bool meta
)
8917 : wxNotifyEvent( type
, id
)
8919 m_topLeft
= topLeft
;
8920 m_bottomRight
= bottomRight
;
8922 m_control
= control
;
8927 SetEventObject(obj
);
8931 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8933 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8934 wxObject
* obj
, int row
,
8935 int col
, wxControl
* ctrl
)
8936 : wxCommandEvent(type
, id
)
8938 SetEventObject(obj
);
8945 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
8947 #endif // wxUSE_GRID