1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
50 #include "wx/tokenzr.h"
53 #include "wx/generic/gridsel.h"
55 #if defined(__WXMOTIF__)
56 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
58 #define WXUNUSED_MOTIF(identifier) identifier
61 #if defined(__WXGTK__)
62 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
64 #define WXUNUSED_GTK(identifier) identifier
67 // Required for wxIs... functions
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
76 struct wxGridCellWithAttr
78 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
79 : coords(row
, col
), attr(attr_
)
88 wxGridCellCoords coords
;
92 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
94 #include "wx/arrimpl.cpp"
96 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
97 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
127 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
128 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
129 const wxPoint
&pos
, const wxSize
&size
);
134 void OnPaint( wxPaintEvent
& event
);
135 void OnMouseEvent( wxMouseEvent
& event
);
136 void OnMouseWheel( wxMouseEvent
& event
);
137 void OnKeyDown( wxKeyEvent
& event
);
138 void OnKeyUp( wxKeyEvent
& );
140 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
141 DECLARE_EVENT_TABLE()
145 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
148 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
149 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
150 const wxPoint
&pos
, const wxSize
&size
);
155 void OnPaint( wxPaintEvent
&event
);
156 void OnMouseEvent( wxMouseEvent
& event
);
157 void OnMouseWheel( wxMouseEvent
& event
);
158 void OnKeyDown( wxKeyEvent
& event
);
159 void OnKeyUp( wxKeyEvent
& );
161 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
162 DECLARE_EVENT_TABLE()
166 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
169 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
170 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
171 const wxPoint
&pos
, const wxSize
&size
);
176 void OnMouseEvent( wxMouseEvent
& event
);
177 void OnMouseWheel( wxMouseEvent
& event
);
178 void OnKeyDown( wxKeyEvent
& event
);
179 void OnKeyUp( wxKeyEvent
& );
180 void OnPaint( wxPaintEvent
& event
);
182 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
183 DECLARE_EVENT_TABLE()
186 class WXDLLEXPORT wxGridWindow
: public wxPanel
191 m_owner
= (wxGrid
*)NULL
;
192 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
193 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
196 wxGridWindow( wxGrid
*parent
,
197 wxGridRowLabelWindow
*rowLblWin
,
198 wxGridColLabelWindow
*colLblWin
,
199 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
202 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
206 wxGridRowLabelWindow
*m_rowLabelWin
;
207 wxGridColLabelWindow
*m_colLabelWin
;
209 void OnPaint( wxPaintEvent
&event
);
210 void OnMouseWheel( wxMouseEvent
& event
);
211 void OnMouseEvent( wxMouseEvent
& event
);
212 void OnKeyDown( wxKeyEvent
& );
213 void OnKeyUp( wxKeyEvent
& );
214 void OnEraseBackground( wxEraseEvent
& );
217 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
218 DECLARE_EVENT_TABLE()
223 class wxGridCellEditorEvtHandler
: public wxEvtHandler
226 wxGridCellEditorEvtHandler()
227 : m_grid(0), m_editor(0)
229 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
230 : m_grid(grid
), m_editor(editor
)
233 void OnKeyDown(wxKeyEvent
& event
);
234 void OnChar(wxKeyEvent
& event
);
238 wxGridCellEditor
* m_editor
;
239 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
240 DECLARE_EVENT_TABLE()
244 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
245 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
246 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
247 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
252 // ----------------------------------------------------------------------------
253 // the internal data representation used by wxGridCellAttrProvider
254 // ----------------------------------------------------------------------------
256 // this class stores attributes set for cells
257 class WXDLLEXPORT wxGridCellAttrData
260 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
261 wxGridCellAttr
*GetAttr(int row
, int col
) const;
262 void UpdateAttrRows( size_t pos
, int numRows
);
263 void UpdateAttrCols( size_t pos
, int numCols
);
266 // searches for the attr for given cell, returns wxNOT_FOUND if not found
267 int FindIndex(int row
, int col
) const;
269 wxGridCellWithAttrArray m_attrs
;
272 // this class stores attributes set for rows or columns
273 class WXDLLEXPORT wxGridRowOrColAttrData
276 // empty ctor to suppress warnings
277 wxGridRowOrColAttrData() { }
278 ~wxGridRowOrColAttrData();
280 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
281 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
282 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
285 wxArrayInt m_rowsOrCols
;
286 wxArrayAttrs m_attrs
;
289 // NB: this is just a wrapper around 3 objects: one which stores cell
290 // attributes, and 2 others for row/col ones
291 class WXDLLEXPORT wxGridCellAttrProviderData
294 wxGridCellAttrData m_cellAttrs
;
295 wxGridRowOrColAttrData m_rowAttrs
,
300 // ----------------------------------------------------------------------------
301 // data structures used for the data type registry
302 // ----------------------------------------------------------------------------
304 struct wxGridDataTypeInfo
306 wxGridDataTypeInfo(const wxString
& typeName
,
307 wxGridCellRenderer
* renderer
,
308 wxGridCellEditor
* editor
)
309 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
312 ~wxGridDataTypeInfo()
314 wxSafeDecRef(m_renderer
);
315 wxSafeDecRef(m_editor
);
319 wxGridCellRenderer
* m_renderer
;
320 wxGridCellEditor
* m_editor
;
324 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
327 class WXDLLEXPORT wxGridTypeRegistry
330 wxGridTypeRegistry() {}
331 ~wxGridTypeRegistry();
333 void RegisterDataType(const wxString
& typeName
,
334 wxGridCellRenderer
* renderer
,
335 wxGridCellEditor
* editor
);
337 // find one of already registered data types
338 int FindRegisteredDataType(const wxString
& typeName
);
340 // try to FindRegisteredDataType(), if this fails and typeName is one of
341 // standard typenames, register it and return its index
342 int FindDataType(const wxString
& typeName
);
344 // try to FindDataType(), if it fails see if it is not one of already
345 // registered data types with some params in which case clone the
346 // registered data type and set params for it
347 int FindOrCloneDataType(const wxString
& typeName
);
349 wxGridCellRenderer
* GetRenderer(int index
);
350 wxGridCellEditor
* GetEditor(int index
);
353 wxGridDataTypeInfoArray m_typeinfo
;
356 // ----------------------------------------------------------------------------
357 // conditional compilation
358 // ----------------------------------------------------------------------------
360 #ifndef WXGRID_DRAW_LINES
361 #define WXGRID_DRAW_LINES 1
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 //#define DEBUG_ATTR_CACHE
369 #ifdef DEBUG_ATTR_CACHE
370 static size_t gs_nAttrCacheHits
= 0;
371 static size_t gs_nAttrCacheMisses
= 0;
372 #endif // DEBUG_ATTR_CACHE
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
379 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
382 // TODO: this doesn't work at all, grid cells have different sizes and approx
383 // calculations don't work as because of the size mismatch scrollbars
384 // sometimes fail to be shown when they should be or vice versa
386 // The scroll bars may be a little flakey once in a while, but that is
387 // surely much less horrible than having scroll lines of only 1!!!
389 static const size_t GRID_SCROLL_LINE
= 15; // 1;
392 // the size of hash tables used a bit everywhere (the max number of elements
393 // in these hash tables is the number of rows/columns)
394 static const int GRID_HASH_SIZE
= 100;
396 // ============================================================================
398 // ============================================================================
400 // ----------------------------------------------------------------------------
402 // ----------------------------------------------------------------------------
404 wxGridCellEditor::wxGridCellEditor()
410 wxGridCellEditor::~wxGridCellEditor()
415 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
416 wxWindowID
WXUNUSED(id
),
417 wxEvtHandler
* evtHandler
)
420 m_control
->PushEventHandler(evtHandler
);
423 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
424 wxGridCellAttr
*attr
)
426 // erase the background because we might not fill the cell
427 wxClientDC
dc(m_control
->GetParent());
428 dc
.SetPen(*wxTRANSPARENT_PEN
);
429 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
430 dc
.DrawRectangle(rectCell
);
432 // redraw the control we just painted over
433 m_control
->Refresh();
436 void wxGridCellEditor::Destroy()
440 m_control
->PopEventHandler(TRUE
/* delete it*/);
442 m_control
->Destroy();
447 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
449 wxASSERT_MSG(m_control
,
450 wxT("The wxGridCellEditor must be Created first!"));
451 m_control
->Show(show
);
455 // set the colours/fonts if we have any
458 m_colFgOld
= m_control
->GetForegroundColour();
459 m_control
->SetForegroundColour(attr
->GetTextColour());
461 m_colBgOld
= m_control
->GetBackgroundColour();
462 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
464 m_fontOld
= m_control
->GetFont();
465 m_control
->SetFont(attr
->GetFont());
467 // can't do anything more in the base class version, the other
468 // attributes may only be used by the derived classes
473 // restore the standard colours fonts
474 if ( m_colFgOld
.Ok() )
476 m_control
->SetForegroundColour(m_colFgOld
);
477 m_colFgOld
= wxNullColour
;
480 if ( m_colBgOld
.Ok() )
482 m_control
->SetBackgroundColour(m_colBgOld
);
483 m_colBgOld
= wxNullColour
;
486 if ( m_fontOld
.Ok() )
488 m_control
->SetFont(m_fontOld
);
489 m_fontOld
= wxNullFont
;
494 void wxGridCellEditor::SetSize(const wxRect
& rect
)
496 wxASSERT_MSG(m_control
,
497 wxT("The wxGridCellEditor must be Created first!"));
498 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
501 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
506 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
508 // accept the simple key presses, not anything with Ctrl/Alt/Meta
509 return !(event
.ControlDown() || event
.AltDown());
512 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
517 void wxGridCellEditor::StartingClick()
521 // ----------------------------------------------------------------------------
522 // wxGridCellTextEditor
523 // ----------------------------------------------------------------------------
525 wxGridCellTextEditor::wxGridCellTextEditor()
530 void wxGridCellTextEditor::Create(wxWindow
* parent
,
532 wxEvtHandler
* evtHandler
)
534 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
535 wxDefaultPosition
, wxDefaultSize
536 #if defined(__WXMSW__)
537 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
538 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
542 // TODO: use m_maxChars
544 wxGridCellEditor::Create(parent
, id
, evtHandler
);
547 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
548 wxGridCellAttr
* WXUNUSED(attr
))
550 // as we fill the entire client area, don't do anything here to minimize
554 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
556 wxRect
rect(rectOrig
);
558 // Make the edit control large enough to allow for internal
561 // TODO: remove this if the text ctrl sizing is improved esp. for
564 #if defined(__WXGTK__)
573 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
575 // MB: treat MSW separately here otherwise the caret doesn't show
576 // when the editor is in the first row.
577 #if defined(__WXMSW__)
580 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
583 #if defined(__WXMOTIF__)
587 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
588 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
589 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
590 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
593 wxGridCellEditor::SetSize(rect
);
596 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
598 wxASSERT_MSG(m_control
,
599 wxT("The wxGridCellEditor must be Created first!"));
601 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
603 DoBeginEdit(m_startValue
);
606 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
608 Text()->SetValue(startValue
);
609 Text()->SetInsertionPointEnd();
613 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
616 wxASSERT_MSG(m_control
,
617 wxT("The wxGridCellEditor must be Created first!"));
619 bool changed
= FALSE
;
620 wxString value
= Text()->GetValue();
621 if (value
!= m_startValue
)
625 grid
->GetTable()->SetValue(row
, col
, value
);
627 m_startValue
= wxEmptyString
;
628 Text()->SetValue(m_startValue
);
634 void wxGridCellTextEditor::Reset()
636 wxASSERT_MSG(m_control
,
637 wxT("The wxGridCellEditor must be Created first!"));
639 DoReset(m_startValue
);
642 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
644 Text()->SetValue(startValue
);
645 Text()->SetInsertionPointEnd();
648 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
650 if ( wxGridCellEditor::IsAcceptedKey(event
) )
652 int keycode
= event
.GetKeyCode();
666 case WXK_NUMPAD_MULTIPLY
:
670 case WXK_NUMPAD_SUBTRACT
:
672 case WXK_NUMPAD_DECIMAL
:
674 case WXK_NUMPAD_DIVIDE
:
678 // accept 8 bit chars too if isprint() agrees
679 if ( (keycode
< 255) && (isprint(keycode
)) )
687 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
689 // we don't check for !HasModifiers() because IsAcceptedKey() did it
691 // insert the key in the control
693 int keycode
= event
.GetKeyCode();
706 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
710 case WXK_NUMPAD_MULTIPLY
:
720 case WXK_NUMPAD_SUBTRACT
:
725 case WXK_NUMPAD_DECIMAL
:
730 case WXK_NUMPAD_DIVIDE
:
735 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
737 // FIXME this is not going to work for non letters...
738 if ( !event
.ShiftDown() )
740 keycode
= tolower(keycode
);
743 ch
= (wxChar
)keycode
;
753 Text()->AppendText(ch
);
761 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
762 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
764 #if defined(__WXMOTIF__) || defined(__WXGTK__)
765 // wxMotif needs a little extra help...
766 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
767 wxString
s( Text()->GetValue() );
768 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
770 Text()->SetInsertionPoint( pos
);
772 // the other ports can handle a Return key press
778 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
788 if ( !params
.ToLong(&tmp
) )
790 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
794 m_maxChars
= (size_t)tmp
;
799 // ----------------------------------------------------------------------------
800 // wxGridCellNumberEditor
801 // ----------------------------------------------------------------------------
803 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
809 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
811 wxEvtHandler
* evtHandler
)
815 // create a spin ctrl
816 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
817 wxDefaultPosition
, wxDefaultSize
,
821 wxGridCellEditor::Create(parent
, id
, evtHandler
);
825 // just a text control
826 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
829 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
830 #endif // wxUSE_VALIDATORS
834 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
836 // first get the value
837 wxGridTableBase
*table
= grid
->GetTable();
838 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
840 m_valueOld
= table
->GetValueAsLong(row
, col
);
844 wxString sValue
= table
->GetValue(row
, col
);
845 if (! sValue
.ToLong(&m_valueOld
))
847 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
854 Spin()->SetValue((int)m_valueOld
);
859 DoBeginEdit(GetString());
863 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
871 value
= Spin()->GetValue();
872 changed
= value
!= m_valueOld
;
876 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
881 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
882 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
884 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
890 void wxGridCellNumberEditor::Reset()
894 Spin()->SetValue((int)m_valueOld
);
898 DoReset(GetString());
902 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
904 if ( wxGridCellEditor::IsAcceptedKey(event
) )
906 int keycode
= event
.GetKeyCode();
922 case WXK_NUMPAD_SUBTRACT
:
928 if ( (keycode
< 128) && isdigit(keycode
) )
936 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
940 int keycode
= (int) event
.KeyCode();
941 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
943 wxGridCellTextEditor::StartingKey(event
);
953 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
964 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
968 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
972 // skip the error message below
977 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
981 // ----------------------------------------------------------------------------
982 // wxGridCellFloatEditor
983 // ----------------------------------------------------------------------------
985 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
988 m_precision
= precision
;
991 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
993 wxEvtHandler
* evtHandler
)
995 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
998 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
999 #endif // wxUSE_VALIDATORS
1002 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1004 // first get the value
1005 wxGridTableBase
*table
= grid
->GetTable();
1006 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1008 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1012 wxString sValue
= table
->GetValue(row
, col
);
1013 if (! sValue
.ToDouble(&m_valueOld
))
1015 wxFAIL_MSG( _T("this cell doesn't have float value") );
1020 DoBeginEdit(GetString());
1023 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1027 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
1029 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1030 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1032 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1042 void wxGridCellFloatEditor::Reset()
1044 DoReset(GetString());
1047 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1049 int keycode
= (int)event
.KeyCode();
1050 if ( isdigit(keycode
) ||
1051 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1053 wxGridCellTextEditor::StartingKey(event
);
1055 // skip Skip() below
1062 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1073 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1077 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1079 m_precision
= (int)tmp
;
1081 // skip the error message below
1086 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1090 wxString
wxGridCellFloatEditor::GetString() const
1093 if ( m_width
== -1 )
1095 // default width/precision
1098 else if ( m_precision
== -1 )
1100 // default precision
1101 fmt
.Printf(_T("%%%d.g"), m_width
);
1105 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1108 return wxString::Format(fmt
, m_valueOld
);
1111 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1113 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1115 int keycode
= event
.GetKeyCode();
1129 case WXK_NUMPAD_ADD
:
1131 case WXK_NUMPAD_SUBTRACT
:
1133 case WXK_NUMPAD_DECIMAL
:
1137 // additionally accept 'e' as in '1e+6'
1138 if ( (keycode
< 128) &&
1139 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1147 // ----------------------------------------------------------------------------
1148 // wxGridCellBoolEditor
1149 // ----------------------------------------------------------------------------
1151 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1153 wxEvtHandler
* evtHandler
)
1155 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1156 wxDefaultPosition
, wxDefaultSize
,
1159 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1162 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1164 bool resize
= FALSE
;
1165 wxSize size
= m_control
->GetSize();
1166 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1168 // check if the checkbox is not too big/small for this cell
1169 wxSize sizeBest
= m_control
->GetBestSize();
1170 if ( !(size
== sizeBest
) )
1172 // reset to default size if it had been made smaller
1178 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1180 // leave 1 pixel margin
1181 size
.x
= size
.y
= minSize
- 2;
1188 m_control
->SetSize(size
);
1191 // position it in the centre of the rectangle (TODO: support alignment?)
1193 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1194 // the checkbox without label still has some space to the right in wxGTK,
1195 // so shift it to the right
1197 #elif defined(__WXMSW__)
1198 // here too, but in other way
1203 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1206 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1208 m_control
->Show(show
);
1212 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1213 CBox()->SetBackgroundColour(colBg
);
1217 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1219 wxASSERT_MSG(m_control
,
1220 wxT("The wxGridCellEditor must be Created first!"));
1222 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1223 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1226 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1227 m_startValue
= !( !cellval
|| (cellval
== "0") );
1229 CBox()->SetValue(m_startValue
);
1233 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1236 wxASSERT_MSG(m_control
,
1237 wxT("The wxGridCellEditor must be Created first!"));
1239 bool changed
= FALSE
;
1240 bool value
= CBox()->GetValue();
1241 if ( value
!= m_startValue
)
1246 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1247 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1249 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1255 void wxGridCellBoolEditor::Reset()
1257 wxASSERT_MSG(m_control
,
1258 wxT("The wxGridCellEditor must be Created first!"));
1260 CBox()->SetValue(m_startValue
);
1263 void wxGridCellBoolEditor::StartingClick()
1265 CBox()->SetValue(!CBox()->GetValue());
1268 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1270 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1272 int keycode
= event
.GetKeyCode();
1276 case WXK_NUMPAD_MULTIPLY
:
1278 case WXK_NUMPAD_ADD
:
1280 case WXK_NUMPAD_SUBTRACT
:
1291 // ----------------------------------------------------------------------------
1292 // wxGridCellChoiceEditor
1293 // ----------------------------------------------------------------------------
1295 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1296 const wxString choices
[],
1298 : m_allowOthers(allowOthers
)
1302 m_choices
.Alloc(count
);
1303 for ( size_t n
= 0; n
< count
; n
++ )
1305 m_choices
.Add(choices
[n
]);
1310 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1312 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1313 editor
->m_allowOthers
= m_allowOthers
;
1314 editor
->m_choices
= m_choices
;
1319 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1321 wxEvtHandler
* evtHandler
)
1323 size_t count
= m_choices
.GetCount();
1324 wxString
*choices
= new wxString
[count
];
1325 for ( size_t n
= 0; n
< count
; n
++ )
1327 choices
[n
] = m_choices
[n
];
1330 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1331 wxDefaultPosition
, wxDefaultSize
,
1333 m_allowOthers
? 0 : wxCB_READONLY
);
1337 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1340 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1341 wxGridCellAttr
* attr
)
1343 // as we fill the entire client area, don't do anything here to minimize
1346 // TODO: It doesn't actually fill the client area since the height of a
1347 // combo always defaults to the standard... Until someone has time to
1348 // figure out the right rectangle to paint, just do it the normal way...
1349 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1352 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1354 wxASSERT_MSG(m_control
,
1355 wxT("The wxGridCellEditor must be Created first!"));
1357 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1359 Combo()->SetValue(m_startValue
);
1360 size_t count
= m_choices
.GetCount();
1361 for (size_t i
=0; i
<count
; i
++)
1363 if (m_startValue
== m_choices
[i
])
1365 Combo()->SetSelection(i
);
1369 Combo()->SetInsertionPointEnd();
1370 Combo()->SetFocus();
1373 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1376 wxString value
= Combo()->GetValue();
1377 bool changed
= value
!= m_startValue
;
1380 grid
->GetTable()->SetValue(row
, col
, value
);
1382 m_startValue
= wxEmptyString
;
1383 Combo()->SetValue(m_startValue
);
1388 void wxGridCellChoiceEditor::Reset()
1390 Combo()->SetValue(m_startValue
);
1391 Combo()->SetInsertionPointEnd();
1394 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1404 wxStringTokenizer
tk(params
, _T(','));
1405 while ( tk
.HasMoreTokens() )
1407 m_choices
.Add(tk
.GetNextToken());
1411 // ----------------------------------------------------------------------------
1412 // wxGridCellEditorEvtHandler
1413 // ----------------------------------------------------------------------------
1415 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1417 switch ( event
.KeyCode() )
1421 m_grid
->DisableCellEditControl();
1425 m_grid
->GetEventHandler()->ProcessEvent( event
);
1429 case WXK_NUMPAD_ENTER
:
1430 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1431 m_editor
->HandleReturn(event
);
1440 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1442 switch ( event
.KeyCode() )
1447 case WXK_NUMPAD_ENTER
:
1455 // ----------------------------------------------------------------------------
1456 // wxGridCellWorker is an (almost) empty common base class for
1457 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1458 // ----------------------------------------------------------------------------
1460 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1465 wxGridCellWorker::~wxGridCellWorker()
1469 // ============================================================================
1471 // ============================================================================
1473 // ----------------------------------------------------------------------------
1474 // wxGridCellRenderer
1475 // ----------------------------------------------------------------------------
1477 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1478 wxGridCellAttr
& attr
,
1481 int WXUNUSED(row
), int WXUNUSED(col
),
1484 dc
.SetBackgroundMode( wxSOLID
);
1488 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1492 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1495 dc
.SetPen( *wxTRANSPARENT_PEN
);
1496 dc
.DrawRectangle(rect
);
1499 // ----------------------------------------------------------------------------
1500 // wxGridCellStringRenderer
1501 // ----------------------------------------------------------------------------
1503 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1504 wxGridCellAttr
& attr
,
1508 dc
.SetBackgroundMode( wxTRANSPARENT
);
1510 // TODO some special colours for attr.IsReadOnly() case?
1514 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1515 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1519 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1520 dc
.SetTextForeground( attr
.GetTextColour() );
1523 dc
.SetFont( attr
.GetFont() );
1526 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1528 const wxString
& text
)
1530 wxCoord x
= 0, y
= 0, max_x
= 0;
1531 dc
.SetFont(attr
.GetFont());
1532 wxStringTokenizer
tk(text
, _T('\n'));
1533 while ( tk
.HasMoreTokens() )
1535 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1536 max_x
= wxMax(max_x
, x
);
1539 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1541 return wxSize(max_x
, y
);
1544 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1545 wxGridCellAttr
& attr
,
1549 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1552 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1553 wxGridCellAttr
& attr
,
1555 const wxRect
& rectCell
,
1559 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1561 // now we only have to draw the text
1562 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1565 attr
.GetAlignment(&hAlign
, &vAlign
);
1567 wxRect rect
= rectCell
;
1570 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1571 rect
, hAlign
, vAlign
);
1574 // ----------------------------------------------------------------------------
1575 // wxGridCellNumberRenderer
1576 // ----------------------------------------------------------------------------
1578 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1580 wxGridTableBase
*table
= grid
.GetTable();
1582 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1584 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1588 text
= table
->GetValue(row
, col
);
1594 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1595 wxGridCellAttr
& attr
,
1597 const wxRect
& rectCell
,
1601 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1603 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1605 // draw the text right aligned by default
1607 attr
.GetAlignment(&hAlign
, &vAlign
);
1608 hAlign
= wxALIGN_RIGHT
;
1610 wxRect rect
= rectCell
;
1613 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1616 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1617 wxGridCellAttr
& attr
,
1621 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1624 // ----------------------------------------------------------------------------
1625 // wxGridCellFloatRenderer
1626 // ----------------------------------------------------------------------------
1628 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1631 SetPrecision(precision
);
1634 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1636 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1637 renderer
->m_width
= m_width
;
1638 renderer
->m_precision
= m_precision
;
1639 renderer
->m_format
= m_format
;
1644 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1646 wxGridTableBase
*table
= grid
.GetTable();
1651 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1653 val
= table
->GetValueAsDouble(row
, col
);
1658 text
= table
->GetValue(row
, col
);
1659 hasDouble
= text
.ToDouble(&val
);
1666 if ( m_width
== -1 )
1668 if ( m_precision
== -1 )
1670 // default width/precision
1671 m_format
= _T("%f");
1675 m_format
.Printf(_T("%%.%df"), m_precision
);
1678 else if ( m_precision
== -1 )
1680 // default precision
1681 m_format
.Printf(_T("%%%d.f"), m_width
);
1685 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1689 text
.Printf(m_format
, val
);
1692 //else: text already contains the string
1697 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1698 wxGridCellAttr
& attr
,
1700 const wxRect
& rectCell
,
1704 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1706 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1708 // draw the text right aligned by default
1710 attr
.GetAlignment(&hAlign
, &vAlign
);
1711 hAlign
= wxALIGN_RIGHT
;
1713 wxRect rect
= rectCell
;
1716 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1719 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1720 wxGridCellAttr
& attr
,
1724 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1727 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1731 // reset to defaults
1737 wxString tmp
= params
.BeforeFirst(_T(','));
1741 if ( tmp
.ToLong(&width
) )
1743 SetWidth((int)width
);
1747 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1751 tmp
= params
.AfterFirst(_T(','));
1755 if ( tmp
.ToLong(&precision
) )
1757 SetPrecision((int)precision
);
1761 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1769 // ----------------------------------------------------------------------------
1770 // wxGridCellBoolRenderer
1771 // ----------------------------------------------------------------------------
1773 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1775 // FIXME these checkbox size calculations are really ugly...
1777 // between checkmark and box
1778 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1780 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1781 wxGridCellAttr
& WXUNUSED(attr
),
1786 // compute it only once (no locks for MT safeness in GUI thread...)
1787 if ( !ms_sizeCheckMark
.x
)
1789 // get checkbox size
1790 wxCoord checkSize
= 0;
1791 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1792 wxSize size
= checkbox
->GetBestSize();
1793 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1795 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1796 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1797 checkSize
-= size
.y
/ 2;
1802 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1805 return ms_sizeCheckMark
;
1808 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1809 wxGridCellAttr
& attr
,
1815 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1817 // draw a check mark in the centre (ignoring alignment - TODO)
1818 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1820 // don't draw outside the cell
1821 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1822 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1824 // and even leave (at least) 1 pixel margin
1825 size
.x
= size
.y
= minSize
- 2;
1828 // draw a border around checkmark
1830 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1831 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1832 rectBorder
.width
= size
.x
;
1833 rectBorder
.height
= size
.y
;
1836 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1837 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1840 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1841 value
= !( !cellval
|| (cellval
== "0") );
1846 wxRect rectMark
= rectBorder
;
1848 // MSW DrawCheckMark() is weird (and should probably be changed...)
1849 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1853 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1856 dc
.SetTextForeground(attr
.GetTextColour());
1857 dc
.DrawCheckMark(rectMark
);
1860 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1861 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1862 dc
.DrawRectangle(rectBorder
);
1865 // ----------------------------------------------------------------------------
1867 // ----------------------------------------------------------------------------
1869 wxGridCellAttr
*wxGridCellAttr::Clone() const
1871 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1872 if ( HasTextColour() )
1873 attr
->SetTextColour(GetTextColour());
1874 if ( HasBackgroundColour() )
1875 attr
->SetBackgroundColour(GetBackgroundColour());
1877 attr
->SetFont(GetFont());
1878 if ( HasAlignment() )
1879 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1883 attr
->SetRenderer(m_renderer
);
1884 m_renderer
->IncRef();
1888 attr
->SetEditor(m_editor
);
1893 attr
->SetReadOnly();
1895 attr
->SetKind( m_attrkind
);
1897 attr
->SetDefAttr(m_defGridAttr
);
1902 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1904 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1905 SetTextColour(mergefrom
->GetTextColour());
1906 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1907 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1908 if ( !HasFont() && mergefrom
->HasFont() )
1909 SetFont(mergefrom
->GetFont());
1910 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1912 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1913 SetAlignment(hAlign
, vAlign
);
1916 // Directly access member functions as GetRender/Editor don't just return
1917 // m_renderer/m_editor
1919 // Maybe add support for merge of Render and Editor?
1920 if (!HasRenderer() && mergefrom
->HasRenderer() )
1922 m_renderer
= mergefrom
->m_renderer
;
1923 m_renderer
->IncRef();
1925 if ( !HasEditor() && mergefrom
->HasEditor() )
1927 m_editor
= mergefrom
->m_editor
;
1930 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1931 SetReadOnly(mergefrom
->IsReadOnly());
1933 SetDefAttr(mergefrom
->m_defGridAttr
);
1936 const wxColour
& wxGridCellAttr::GetTextColour() const
1938 if (HasTextColour())
1942 else if (m_defGridAttr
!= this)
1944 return m_defGridAttr
->GetTextColour();
1948 wxFAIL_MSG(wxT("Missing default cell attribute"));
1949 return wxNullColour
;
1954 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1956 if (HasBackgroundColour())
1958 else if (m_defGridAttr
!= this)
1959 return m_defGridAttr
->GetBackgroundColour();
1962 wxFAIL_MSG(wxT("Missing default cell attribute"));
1963 return wxNullColour
;
1968 const wxFont
& wxGridCellAttr::GetFont() const
1972 else if (m_defGridAttr
!= this)
1973 return m_defGridAttr
->GetFont();
1976 wxFAIL_MSG(wxT("Missing default cell attribute"));
1982 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1986 if ( hAlign
) *hAlign
= m_hAlign
;
1987 if ( vAlign
) *vAlign
= m_vAlign
;
1989 else if (m_defGridAttr
!= this)
1990 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1993 wxFAIL_MSG(wxT("Missing default cell attribute"));
1998 // GetRenderer and GetEditor use a slightly different decision path about
1999 // which attribute to use. If a non-default attr object has one then it is
2000 // used, otherwise the default editor or renderer is fetched from the grid and
2001 // used. It should be the default for the data type of the cell. If it is
2002 // NULL (because the table has a type that the grid does not have in its
2003 // registry,) then the grid's default editor or renderer is used.
2005 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2007 wxGridCellRenderer
* renderer
= NULL
;
2009 if ( m_defGridAttr
!= this || grid
== NULL
)
2011 renderer
= m_renderer
; // use local attribute
2016 if ( !renderer
&& grid
) // get renderer for the data type
2018 // GetDefaultRendererForCell() will do IncRef() for us
2019 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2024 // if we still don't have one then use the grid default
2025 // (no need for IncRef() here neither)
2026 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
2031 wxFAIL_MSG(wxT("Missing default cell attribute"));
2037 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2039 wxGridCellEditor
* editor
= NULL
;
2041 if ( m_defGridAttr
!= this || grid
== NULL
)
2043 editor
= m_editor
; // use local attribute
2048 if ( !editor
&& grid
) // get renderer for the data type
2049 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2052 // if we still don't have one then use the grid default
2053 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
2057 wxFAIL_MSG(wxT("Missing default cell attribute"));
2063 // ----------------------------------------------------------------------------
2064 // wxGridCellAttrData
2065 // ----------------------------------------------------------------------------
2067 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2069 int n
= FindIndex(row
, col
);
2070 if ( n
== wxNOT_FOUND
)
2072 // add the attribute
2073 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2079 // change the attribute
2080 m_attrs
[(size_t)n
].attr
= attr
;
2084 // remove this attribute
2085 m_attrs
.RemoveAt((size_t)n
);
2090 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2092 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2094 int n
= FindIndex(row
, col
);
2095 if ( n
!= wxNOT_FOUND
)
2097 attr
= m_attrs
[(size_t)n
].attr
;
2104 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2106 size_t count
= m_attrs
.GetCount();
2107 for ( size_t n
= 0; n
< count
; n
++ )
2109 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2110 wxCoord row
= coords
.GetRow();
2111 if ((size_t)row
>= pos
)
2115 // If rows inserted, include row counter where necessary
2116 coords
.SetRow(row
+ numRows
);
2118 else if (numRows
< 0)
2120 // If rows deleted ...
2121 if ((size_t)row
>= pos
- numRows
)
2123 // ...either decrement row counter (if row still exists)...
2124 coords
.SetRow(row
+ numRows
);
2128 // ...or remove the attribute
2129 m_attrs
.RemoveAt((size_t)n
);
2137 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2139 size_t count
= m_attrs
.GetCount();
2140 for ( size_t n
= 0; n
< count
; n
++ )
2142 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2143 wxCoord col
= coords
.GetCol();
2144 if ( (size_t)col
>= pos
)
2148 // If rows inserted, include row counter where necessary
2149 coords
.SetCol(col
+ numCols
);
2151 else if (numCols
< 0)
2153 // If rows deleted ...
2154 if ((size_t)col
>= pos
- numCols
)
2156 // ...either decrement row counter (if row still exists)...
2157 coords
.SetCol(col
+ numCols
);
2161 // ...or remove the attribute
2162 m_attrs
.RemoveAt((size_t)n
);
2170 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2172 size_t count
= m_attrs
.GetCount();
2173 for ( size_t n
= 0; n
< count
; n
++ )
2175 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2176 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2185 // ----------------------------------------------------------------------------
2186 // wxGridRowOrColAttrData
2187 // ----------------------------------------------------------------------------
2189 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2191 size_t count
= m_attrs
.Count();
2192 for ( size_t n
= 0; n
< count
; n
++ )
2194 m_attrs
[n
]->DecRef();
2198 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2200 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2202 int n
= m_rowsOrCols
.Index(rowOrCol
);
2203 if ( n
!= wxNOT_FOUND
)
2205 attr
= m_attrs
[(size_t)n
];
2212 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2214 int i
= m_rowsOrCols
.Index(rowOrCol
);
2215 if ( i
== wxNOT_FOUND
)
2217 // add the attribute
2218 m_rowsOrCols
.Add(rowOrCol
);
2223 size_t n
= (size_t)i
;
2226 // change the attribute
2227 m_attrs
[n
]->DecRef();
2232 // remove this attribute
2233 m_attrs
[n
]->DecRef();
2234 m_rowsOrCols
.RemoveAt(n
);
2235 m_attrs
.RemoveAt(n
);
2240 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2242 size_t count
= m_attrs
.GetCount();
2243 for ( size_t n
= 0; n
< count
; n
++ )
2245 int & rowOrCol
= m_rowsOrCols
[n
];
2246 if ( (size_t)rowOrCol
>= pos
)
2248 if ( numRowsOrCols
> 0 )
2250 // If rows inserted, include row counter where necessary
2251 rowOrCol
+= numRowsOrCols
;
2253 else if ( numRowsOrCols
< 0)
2255 // If rows deleted, either decrement row counter (if row still exists)
2256 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2257 rowOrCol
+= numRowsOrCols
;
2260 m_rowsOrCols
.RemoveAt((size_t)n
);
2261 m_attrs
.RemoveAt((size_t)n
);
2269 // ----------------------------------------------------------------------------
2270 // wxGridCellAttrProvider
2271 // ----------------------------------------------------------------------------
2273 wxGridCellAttrProvider::wxGridCellAttrProvider()
2275 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2278 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2283 void wxGridCellAttrProvider::InitData()
2285 m_data
= new wxGridCellAttrProviderData
;
2288 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2289 wxGridCellAttr::wxAttrKind kind
) const
2291 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2296 case (wxGridCellAttr::Any
):
2297 //Get cached merge attributes.
2298 // Currenlty not used as no cache implemented as not mutiable
2299 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2302 //Basicaly implement old version.
2303 //Also check merge cache, so we don't have to re-merge every time..
2304 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2305 *attrrow
= (wxGridCellAttr
*)NULL
,
2306 *attrcol
= (wxGridCellAttr
*)NULL
;
2308 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2309 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2310 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2312 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2313 // Two or move are non NULL
2314 attr
= new wxGridCellAttr
;
2315 attr
->SetKind(wxGridCellAttr::Merged
);
2319 attr
->MergeWith(attrcell
);
2323 attr
->MergeWith(attrcol
);
2327 attr
->MergeWith(attrrow
);
2330 //store merge attr if cache implemented
2332 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2336 // one or none is non null return it or null.
2337 if(attrrow
) attr
= attrrow
;
2338 if(attrcol
) attr
= attrcol
;
2339 if(attrcell
) attr
= attrcell
;
2343 case (wxGridCellAttr::Cell
):
2344 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2346 case (wxGridCellAttr::Col
):
2347 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2349 case (wxGridCellAttr::Row
):
2350 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2354 // (wxGridCellAttr::Default):
2355 // (wxGridCellAttr::Merged):
2362 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2368 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2371 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2376 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2379 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2384 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2387 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2391 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2393 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2397 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2401 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2403 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2407 // ----------------------------------------------------------------------------
2408 // wxGridTypeRegistry
2409 // ----------------------------------------------------------------------------
2411 wxGridTypeRegistry::~wxGridTypeRegistry()
2413 size_t count
= m_typeinfo
.Count();
2414 for ( size_t i
= 0; i
< count
; i
++ )
2415 delete m_typeinfo
[i
];
2419 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2420 wxGridCellRenderer
* renderer
,
2421 wxGridCellEditor
* editor
)
2423 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2425 // is it already registered?
2426 int loc
= FindRegisteredDataType(typeName
);
2427 if ( loc
!= wxNOT_FOUND
)
2429 delete m_typeinfo
[loc
];
2430 m_typeinfo
[loc
] = info
;
2434 m_typeinfo
.Add(info
);
2438 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2440 size_t count
= m_typeinfo
.GetCount();
2441 for ( size_t i
= 0; i
< count
; i
++ )
2443 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2452 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2454 int index
= FindRegisteredDataType(typeName
);
2455 if ( index
== wxNOT_FOUND
)
2457 // check whether this is one of the standard ones, in which case
2458 // register it "on the fly"
2459 if ( typeName
== wxGRID_VALUE_STRING
)
2461 RegisterDataType(wxGRID_VALUE_STRING
,
2462 new wxGridCellStringRenderer
,
2463 new wxGridCellTextEditor
);
2465 else if ( typeName
== wxGRID_VALUE_BOOL
)
2467 RegisterDataType(wxGRID_VALUE_BOOL
,
2468 new wxGridCellBoolRenderer
,
2469 new wxGridCellBoolEditor
);
2471 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2473 RegisterDataType(wxGRID_VALUE_NUMBER
,
2474 new wxGridCellNumberRenderer
,
2475 new wxGridCellNumberEditor
);
2477 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2479 RegisterDataType(wxGRID_VALUE_FLOAT
,
2480 new wxGridCellFloatRenderer
,
2481 new wxGridCellFloatEditor
);
2483 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2485 RegisterDataType(wxGRID_VALUE_CHOICE
,
2486 new wxGridCellStringRenderer
,
2487 new wxGridCellChoiceEditor
);
2494 // we get here only if just added the entry for this type, so return
2496 index
= m_typeinfo
.GetCount() - 1;
2502 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2504 int index
= FindDataType(typeName
);
2505 if ( index
== wxNOT_FOUND
)
2507 // the first part of the typename is the "real" type, anything after ':'
2508 // are the parameters for the renderer
2509 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2510 if ( index
== wxNOT_FOUND
)
2515 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2516 wxGridCellRenderer
*rendererOld
= renderer
;
2517 renderer
= renderer
->Clone();
2518 rendererOld
->DecRef();
2520 wxGridCellEditor
*editor
= GetEditor(index
);
2521 wxGridCellEditor
*editorOld
= editor
;
2522 editor
= editor
->Clone();
2523 editorOld
->DecRef();
2525 // do it even if there are no parameters to reset them to defaults
2526 wxString params
= typeName
.AfterFirst(_T(':'));
2527 renderer
->SetParameters(params
);
2528 editor
->SetParameters(params
);
2530 // register the new typename
2531 RegisterDataType(typeName
, renderer
, editor
);
2533 // we just registered it, it's the last one
2534 index
= m_typeinfo
.GetCount() - 1;
2540 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2542 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2548 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2550 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2556 // ----------------------------------------------------------------------------
2558 // ----------------------------------------------------------------------------
2560 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2563 wxGridTableBase::wxGridTableBase()
2565 m_view
= (wxGrid
*) NULL
;
2566 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2569 wxGridTableBase::~wxGridTableBase()
2571 delete m_attrProvider
;
2574 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2576 delete m_attrProvider
;
2577 m_attrProvider
= attrProvider
;
2580 bool wxGridTableBase::CanHaveAttributes()
2582 if ( ! GetAttrProvider() )
2584 // use the default attr provider by default
2585 SetAttrProvider(new wxGridCellAttrProvider
);
2590 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2592 if ( m_attrProvider
)
2593 return m_attrProvider
->GetAttr(row
, col
, kind
);
2595 return (wxGridCellAttr
*)NULL
;
2598 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2600 if ( m_attrProvider
)
2602 attr
->SetKind(wxGridCellAttr::Cell
);
2603 m_attrProvider
->SetAttr(attr
, row
, col
);
2607 // as we take ownership of the pointer and don't store it, we must
2613 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2615 if ( m_attrProvider
)
2617 attr
->SetKind(wxGridCellAttr::Row
);
2618 m_attrProvider
->SetRowAttr(attr
, row
);
2622 // as we take ownership of the pointer and don't store it, we must
2628 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2630 if ( m_attrProvider
)
2632 attr
->SetKind(wxGridCellAttr::Col
);
2633 m_attrProvider
->SetColAttr(attr
, col
);
2637 // as we take ownership of the pointer and don't store it, we must
2643 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2644 size_t WXUNUSED(numRows
) )
2646 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2651 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2653 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2658 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2659 size_t WXUNUSED(numRows
) )
2661 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2666 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2667 size_t WXUNUSED(numCols
) )
2669 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2674 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2676 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2681 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2682 size_t WXUNUSED(numCols
) )
2684 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2690 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2693 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2694 // how much it makes sense to us geeks.
2698 wxString
wxGridTableBase::GetColLabelValue( int col
)
2700 // default col labels are:
2701 // cols 0 to 25 : A-Z
2702 // cols 26 to 675 : AA-ZZ
2707 for ( n
= 1; ; n
++ )
2709 s
+= (_T('A') + (wxChar
)( col%26
));
2711 if ( col
< 0 ) break;
2714 // reverse the string...
2716 for ( i
= 0; i
< n
; i
++ )
2725 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2727 return wxGRID_VALUE_STRING
;
2730 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2731 const wxString
& typeName
)
2733 return typeName
== wxGRID_VALUE_STRING
;
2736 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2738 return CanGetValueAs(row
, col
, typeName
);
2741 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2746 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2751 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2756 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2757 long WXUNUSED(value
) )
2761 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2762 double WXUNUSED(value
) )
2766 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2767 bool WXUNUSED(value
) )
2772 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2773 const wxString
& WXUNUSED(typeName
) )
2778 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2779 const wxString
& WXUNUSED(typeName
),
2780 void* WXUNUSED(value
) )
2784 //////////////////////////////////////////////////////////////////////
2786 // Message class for the grid table to send requests and notifications
2790 wxGridTableMessage::wxGridTableMessage()
2792 m_table
= (wxGridTableBase
*) NULL
;
2798 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2799 int commandInt1
, int commandInt2
)
2803 m_comInt1
= commandInt1
;
2804 m_comInt2
= commandInt2
;
2809 //////////////////////////////////////////////////////////////////////
2811 // A basic grid table for string data. An object of this class will
2812 // created by wxGrid if you don't specify an alternative table class.
2815 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2817 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2819 wxGridStringTable::wxGridStringTable()
2824 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2829 m_data
.Alloc( numRows
);
2832 sa
.Alloc( numCols
);
2833 for ( col
= 0; col
< numCols
; col
++ )
2835 sa
.Add( wxEmptyString
);
2838 for ( row
= 0; row
< numRows
; row
++ )
2844 wxGridStringTable::~wxGridStringTable()
2848 int wxGridStringTable::GetNumberRows()
2850 return m_data
.GetCount();
2853 int wxGridStringTable::GetNumberCols()
2855 if ( m_data
.GetCount() > 0 )
2856 return m_data
[0].GetCount();
2861 wxString
wxGridStringTable::GetValue( int row
, int col
)
2863 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2864 _T("invalid row or column index in wxGridStringTable") );
2866 return m_data
[row
][col
];
2869 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2871 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2872 _T("invalid row or column index in wxGridStringTable") );
2874 m_data
[row
][col
] = value
;
2877 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2879 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2880 _T("invalid row or column index in wxGridStringTable") );
2882 return (m_data
[row
][col
] == wxEmptyString
);
2885 void wxGridStringTable::Clear()
2888 int numRows
, numCols
;
2890 numRows
= m_data
.GetCount();
2893 numCols
= m_data
[0].GetCount();
2895 for ( row
= 0; row
< numRows
; row
++ )
2897 for ( col
= 0; col
< numCols
; col
++ )
2899 m_data
[row
][col
] = wxEmptyString
;
2906 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2910 size_t curNumRows
= m_data
.GetCount();
2911 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2912 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2914 if ( pos
>= curNumRows
)
2916 return AppendRows( numRows
);
2920 sa
.Alloc( curNumCols
);
2921 for ( col
= 0; col
< curNumCols
; col
++ )
2923 sa
.Add( wxEmptyString
);
2926 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2928 m_data
.Insert( sa
, row
);
2932 wxGridTableMessage
msg( this,
2933 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2937 GetView()->ProcessTableMessage( msg
);
2943 bool wxGridStringTable::AppendRows( size_t numRows
)
2947 size_t curNumRows
= m_data
.GetCount();
2948 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2949 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2952 if ( curNumCols
> 0 )
2954 sa
.Alloc( curNumCols
);
2955 for ( col
= 0; col
< curNumCols
; col
++ )
2957 sa
.Add( wxEmptyString
);
2961 for ( row
= 0; row
< numRows
; row
++ )
2968 wxGridTableMessage
msg( this,
2969 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2972 GetView()->ProcessTableMessage( msg
);
2978 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2982 size_t curNumRows
= m_data
.GetCount();
2984 if ( pos
>= curNumRows
)
2987 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2988 pos
, numRows
, curNumRows
);
2989 wxFAIL_MSG( errmsg
);
2993 if ( numRows
> curNumRows
- pos
)
2995 numRows
= curNumRows
- pos
;
2998 if ( numRows
>= curNumRows
)
3000 m_data
.Empty(); // don't release memory just yet
3004 for ( n
= 0; n
< numRows
; n
++ )
3006 m_data
.Remove( pos
);
3011 wxGridTableMessage
msg( this,
3012 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3016 GetView()->ProcessTableMessage( msg
);
3022 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3026 size_t curNumRows
= m_data
.GetCount();
3027 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3028 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3030 if ( pos
>= curNumCols
)
3032 return AppendCols( numCols
);
3035 for ( row
= 0; row
< curNumRows
; row
++ )
3037 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3039 m_data
[row
].Insert( wxEmptyString
, col
);
3044 wxGridTableMessage
msg( this,
3045 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3049 GetView()->ProcessTableMessage( msg
);
3055 bool wxGridStringTable::AppendCols( size_t numCols
)
3059 size_t curNumRows
= m_data
.GetCount();
3063 // TODO: something better than this ?
3065 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3070 for ( row
= 0; row
< curNumRows
; row
++ )
3072 for ( n
= 0; n
< numCols
; n
++ )
3074 m_data
[row
].Add( wxEmptyString
);
3080 wxGridTableMessage
msg( this,
3081 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3084 GetView()->ProcessTableMessage( msg
);
3090 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3094 size_t curNumRows
= m_data
.GetCount();
3095 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3096 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3098 if ( pos
>= curNumCols
)
3101 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3102 pos
, numCols
, curNumCols
);
3103 wxFAIL_MSG( errmsg
);
3107 if ( numCols
> curNumCols
- pos
)
3109 numCols
= curNumCols
- pos
;
3112 for ( row
= 0; row
< curNumRows
; row
++ )
3114 if ( numCols
>= curNumCols
)
3116 m_data
[row
].Clear();
3120 for ( n
= 0; n
< numCols
; n
++ )
3122 m_data
[row
].Remove( pos
);
3128 wxGridTableMessage
msg( this,
3129 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3133 GetView()->ProcessTableMessage( msg
);
3139 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3141 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3143 // using default label
3145 return wxGridTableBase::GetRowLabelValue( row
);
3149 return m_rowLabels
[ row
];
3153 wxString
wxGridStringTable::GetColLabelValue( int col
)
3155 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3157 // using default label
3159 return wxGridTableBase::GetColLabelValue( col
);
3163 return m_colLabels
[ col
];
3167 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3169 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3171 int n
= m_rowLabels
.GetCount();
3173 for ( i
= n
; i
<= row
; i
++ )
3175 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3179 m_rowLabels
[row
] = value
;
3182 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3184 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3186 int n
= m_colLabels
.GetCount();
3188 for ( i
= n
; i
<= col
; i
++ )
3190 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3194 m_colLabels
[col
] = value
;
3199 //////////////////////////////////////////////////////////////////////
3200 //////////////////////////////////////////////////////////////////////
3202 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3204 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3205 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3206 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3207 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3208 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3209 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3212 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3214 const wxPoint
&pos
, const wxSize
&size
)
3215 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3220 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3224 // NO - don't do this because it will set both the x and y origin
3225 // coords to match the parent scrolled window and we just want to
3226 // set the y coord - MB
3228 // m_owner->PrepareDC( dc );
3231 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3232 dc
.SetDeviceOrigin( 0, -y
);
3234 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3235 m_owner
->DrawRowLabels( dc
, rows
);
3239 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3241 m_owner
->ProcessRowLabelMouseEvent( event
);
3245 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3247 m_owner
->GetEventHandler()->ProcessEvent(event
);
3251 // This seems to be required for wxMotif otherwise the mouse
3252 // cursor must be in the cell edit control to get key events
3254 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3256 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3259 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3261 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3266 //////////////////////////////////////////////////////////////////////
3268 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3270 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3271 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3272 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3273 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3274 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3275 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3278 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3280 const wxPoint
&pos
, const wxSize
&size
)
3281 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3286 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3290 // NO - don't do this because it will set both the x and y origin
3291 // coords to match the parent scrolled window and we just want to
3292 // set the x coord - MB
3294 // m_owner->PrepareDC( dc );
3297 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3298 dc
.SetDeviceOrigin( -x
, 0 );
3300 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3301 m_owner
->DrawColLabels( dc
, cols
);
3305 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3307 m_owner
->ProcessColLabelMouseEvent( event
);
3310 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3312 m_owner
->GetEventHandler()->ProcessEvent(event
);
3316 // This seems to be required for wxMotif otherwise the mouse
3317 // cursor must be in the cell edit control to get key events
3319 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3321 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3324 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3326 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3331 //////////////////////////////////////////////////////////////////////
3333 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3335 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3336 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3337 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3338 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3339 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3340 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3343 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3345 const wxPoint
&pos
, const wxSize
&size
)
3346 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3351 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3355 int client_height
= 0;
3356 int client_width
= 0;
3357 GetClientSize( &client_width
, &client_height
);
3359 dc
.SetPen( *wxBLACK_PEN
);
3360 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3361 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3363 dc
.SetPen( *wxWHITE_PEN
);
3364 dc
.DrawLine( 0, 0, client_width
, 0 );
3365 dc
.DrawLine( 0, 0, 0, client_height
);
3369 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3371 m_owner
->ProcessCornerLabelMouseEvent( event
);
3375 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3377 m_owner
->GetEventHandler()->ProcessEvent(event
);
3380 // This seems to be required for wxMotif otherwise the mouse
3381 // cursor must be in the cell edit control to get key events
3383 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3385 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3388 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3390 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3395 //////////////////////////////////////////////////////////////////////
3397 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3399 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3400 EVT_PAINT( wxGridWindow::OnPaint
)
3401 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3402 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3403 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3404 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3405 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3408 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3409 wxGridRowLabelWindow
*rowLblWin
,
3410 wxGridColLabelWindow
*colLblWin
,
3411 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3412 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3415 m_rowLabelWin
= rowLblWin
;
3416 m_colLabelWin
= colLblWin
;
3417 SetBackgroundColour( "WHITE" );
3421 wxGridWindow::~wxGridWindow()
3426 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3428 wxPaintDC
dc( this );
3429 m_owner
->PrepareDC( dc
);
3430 wxRegion reg
= GetUpdateRegion();
3431 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3432 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3433 #if WXGRID_DRAW_LINES
3434 m_owner
->DrawAllGridLines( dc
, reg
);
3436 m_owner
->DrawGridSpace( dc
);
3437 m_owner
->DrawHighlight( dc
, DirtyCells
);
3441 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3443 wxPanel::ScrollWindow( dx
, dy
, rect
);
3444 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3445 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3449 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3451 m_owner
->ProcessGridCellMouseEvent( event
);
3454 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3456 m_owner
->GetEventHandler()->ProcessEvent(event
);
3459 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3460 // cursor must be in the cell edit control to get key events
3462 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3464 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3467 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3469 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3472 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3477 //////////////////////////////////////////////////////////////////////
3480 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3482 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3483 EVT_PAINT( wxGrid::OnPaint
)
3484 EVT_SIZE( wxGrid::OnSize
)
3485 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3486 EVT_KEY_UP( wxGrid::OnKeyUp
)
3487 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3490 wxGrid::wxGrid( wxWindow
*parent
,
3495 const wxString
& name
)
3496 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3497 m_colMinWidths(GRID_HASH_SIZE
),
3498 m_rowMinHeights(GRID_HASH_SIZE
)
3506 // Must do this or ~wxScrollHelper will pop the wrong event handler
3507 SetTargetWindow(this);
3509 wxSafeDecRef(m_defaultCellAttr
);
3511 #ifdef DEBUG_ATTR_CACHE
3512 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3513 wxPrintf(_T("wxGrid attribute cache statistics: "
3514 "total: %u, hits: %u (%u%%)\n"),
3515 total
, gs_nAttrCacheHits
,
3516 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3522 delete m_typeRegistry
;
3528 // ----- internal init and update functions
3531 void wxGrid::Create()
3533 m_created
= FALSE
; // set to TRUE by CreateGrid
3535 m_table
= (wxGridTableBase
*) NULL
;
3538 m_cellEditCtrlEnabled
= FALSE
;
3540 m_defaultCellAttr
= new wxGridCellAttr
;
3541 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3543 // Set default cell attributes
3544 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3545 m_defaultCellAttr
->SetFont(GetFont());
3546 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3547 m_defaultCellAttr
->SetTextColour(
3548 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3549 m_defaultCellAttr
->SetBackgroundColour(
3550 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3551 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3552 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3557 m_currentCellCoords
= wxGridNoCellCoords
;
3559 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3560 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3562 // create the type registry
3563 m_typeRegistry
= new wxGridTypeRegistry
;
3565 // subwindow components that make up the wxGrid
3566 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3571 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3576 m_colLabelWin
= new wxGridColLabelWindow( this,
3581 m_gridWin
= new wxGridWindow( this,
3588 SetTargetWindow( m_gridWin
);
3592 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3593 wxGrid::wxGridSelectionModes selmode
)
3595 wxCHECK_MSG( !m_created
,
3597 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3599 m_numRows
= numRows
;
3600 m_numCols
= numCols
;
3602 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3603 m_table
->SetView( this );
3605 m_selection
= new wxGridSelection( this, selmode
);
3612 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3616 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3619 m_selection
->SetSelectionMode( selmode
);
3622 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3623 wxGrid::wxGridSelectionModes selmode
)
3627 // RD: Actually, this should probably be allowed. I think it would be
3628 // nice to be able to switch multiple Tables in and out of a single
3629 // View at runtime. Is there anything in the implmentation that would
3632 // At least, you now have to cope with m_selection
3633 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3638 m_numRows
= table
->GetNumberRows();
3639 m_numCols
= table
->GetNumberCols();
3642 m_table
->SetView( this );
3645 m_selection
= new wxGridSelection( this, selmode
);
3656 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3657 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3659 if ( m_rowLabelWin
)
3661 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3665 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3668 m_labelTextColour
= wxColour( _T("BLACK") );
3671 m_attrCache
.row
= -1;
3673 // TODO: something better than this ?
3675 m_labelFont
= this->GetFont();
3676 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3678 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3679 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3681 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3682 m_colLabelVertAlign
= wxALIGN_TOP
;
3684 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3685 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3687 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3688 m_defaultRowHeight
+= 8;
3690 m_defaultRowHeight
+= 4;
3693 m_gridLineColour
= wxColour( 128, 128, 255 );
3694 m_gridLinesEnabled
= TRUE
;
3695 m_cellHighlightColour
= m_gridLineColour
;
3696 m_cellHighlightPenWidth
= 2;
3697 m_cellHighlightROPenWidth
= 1;
3699 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3700 m_winCapture
= (wxWindow
*)NULL
;
3701 m_canDragRowSize
= TRUE
;
3702 m_canDragColSize
= TRUE
;
3703 m_canDragGridSize
= TRUE
;
3705 m_dragRowOrCol
= -1;
3706 m_isDragging
= FALSE
;
3707 m_startDragPos
= wxDefaultPosition
;
3709 m_waitForSlowClick
= FALSE
;
3711 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3712 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3714 m_currentCellCoords
= wxGridNoCellCoords
;
3716 m_selectingTopLeft
= wxGridNoCellCoords
;
3717 m_selectingBottomRight
= wxGridNoCellCoords
;
3718 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3719 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3721 m_editable
= TRUE
; // default for whole grid
3723 m_inOnKeyDown
= FALSE
;
3732 // ----------------------------------------------------------------------------
3733 // the idea is to call these functions only when necessary because they create
3734 // quite big arrays which eat memory mostly unnecessary - in particular, if
3735 // default widths/heights are used for all rows/columns, we may not use these
3738 // with some extra code, it should be possible to only store the
3739 // widths/heights different from default ones but this will be done later...
3740 // ----------------------------------------------------------------------------
3742 void wxGrid::InitRowHeights()
3744 m_rowHeights
.Empty();
3745 m_rowBottoms
.Empty();
3747 m_rowHeights
.Alloc( m_numRows
);
3748 m_rowBottoms
.Alloc( m_numRows
);
3751 for ( int i
= 0; i
< m_numRows
; i
++ )
3753 m_rowHeights
.Add( m_defaultRowHeight
);
3754 rowBottom
+= m_defaultRowHeight
;
3755 m_rowBottoms
.Add( rowBottom
);
3759 void wxGrid::InitColWidths()
3761 m_colWidths
.Empty();
3762 m_colRights
.Empty();
3764 m_colWidths
.Alloc( m_numCols
);
3765 m_colRights
.Alloc( m_numCols
);
3767 for ( int i
= 0; i
< m_numCols
; i
++ )
3769 m_colWidths
.Add( m_defaultColWidth
);
3770 colRight
+= m_defaultColWidth
;
3771 m_colRights
.Add( colRight
);
3775 int wxGrid::GetColWidth(int col
) const
3777 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3780 int wxGrid::GetColLeft(int col
) const
3782 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3783 : m_colRights
[col
] - m_colWidths
[col
];
3786 int wxGrid::GetColRight(int col
) const
3788 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3792 int wxGrid::GetRowHeight(int row
) const
3794 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3797 int wxGrid::GetRowTop(int row
) const
3799 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3800 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3803 int wxGrid::GetRowBottom(int row
) const
3805 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3806 : m_rowBottoms
[row
];
3809 void wxGrid::CalcDimensions()
3812 GetClientSize( &cw
, &ch
);
3814 if ( m_colLabelWin
->IsShown() )
3815 cw
-= m_rowLabelWidth
;
3816 if ( m_rowLabelWin
->IsShown() )
3817 ch
-= m_colLabelHeight
;
3820 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3821 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3823 // preserve (more or less) the previous position
3825 GetViewStart( &x
, &y
);
3826 // maybe we don't need scrollbars at all? and if we do, transform w and h
3827 // from pixels into logical units
3834 w
= (w
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3844 h
= (h
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3849 // do set scrollbar parameters
3850 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3851 w
, h
, x
, y
, (GetBatchCount() != 0));
3855 void wxGrid::CalcWindowSizes()
3858 GetClientSize( &cw
, &ch
);
3860 if ( m_cornerLabelWin
->IsShown() )
3861 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3863 if ( m_colLabelWin
->IsShown() )
3864 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3866 if ( m_rowLabelWin
->IsShown() )
3867 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3869 if ( m_gridWin
->IsShown() )
3870 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3874 // this is called when the grid table sends a message to say that it
3875 // has been redimensioned
3877 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3880 bool result
= FALSE
;
3883 // if we were using the default widths/heights so far, we must change them
3885 if ( m_colWidths
.IsEmpty() )
3890 if ( m_rowHeights
.IsEmpty() )
3896 switch ( msg
.GetId() )
3898 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3900 size_t pos
= msg
.GetCommandInt();
3901 int numRows
= msg
.GetCommandInt2();
3903 m_numRows
+= numRows
;
3905 if ( !m_rowHeights
.IsEmpty() )
3907 for ( i
= 0; i
< numRows
; i
++ )
3909 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3910 m_rowBottoms
.Insert( 0, pos
);
3914 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3916 for ( i
= pos
; i
< m_numRows
; i
++ )
3918 bottom
+= m_rowHeights
[i
];
3919 m_rowBottoms
[i
] = bottom
;
3922 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3924 // if we have just inserted cols into an empty grid the current
3925 // cell will be undefined...
3927 SetCurrentCell( 0, 0 );
3929 m_selection
->UpdateRows( pos
, numRows
);
3930 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3932 attrProvider
->UpdateAttrRows( pos
, numRows
);
3934 if ( !GetBatchCount() )
3937 m_rowLabelWin
->Refresh();
3943 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3945 int numRows
= msg
.GetCommandInt();
3946 int oldNumRows
= m_numRows
;
3947 m_numRows
+= numRows
;
3949 if ( !m_rowHeights
.IsEmpty() )
3951 for ( i
= 0; i
< numRows
; i
++ )
3953 m_rowHeights
.Add( m_defaultRowHeight
);
3954 m_rowBottoms
.Add( 0 );
3958 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3960 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3962 bottom
+= m_rowHeights
[i
];
3963 m_rowBottoms
[i
] = bottom
;
3966 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3968 // if we have just inserted cols into an empty grid the current
3969 // cell will be undefined...
3971 SetCurrentCell( 0, 0 );
3973 if ( !GetBatchCount() )
3976 m_rowLabelWin
->Refresh();
3982 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3984 size_t pos
= msg
.GetCommandInt();
3985 int numRows
= msg
.GetCommandInt2();
3986 m_numRows
-= numRows
;
3988 if ( !m_rowHeights
.IsEmpty() )
3990 for ( i
= 0; i
< numRows
; i
++ )
3992 m_rowHeights
.Remove( pos
);
3993 m_rowBottoms
.Remove( pos
);
3997 for ( i
= 0; i
< m_numRows
; i
++ )
3999 h
+= m_rowHeights
[i
];
4000 m_rowBottoms
[i
] = h
;
4005 m_currentCellCoords
= wxGridNoCellCoords
;
4009 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4010 m_currentCellCoords
.Set( 0, 0 );
4012 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4013 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4015 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4016 // ifdef'd out following patch from Paul Gammans
4018 // No need to touch column attributes, unless we
4019 // removed _all_ rows, in this case, we remove
4020 // all column attributes.
4021 // I hate to do this here, but the
4022 // needed data is not available inside UpdateAttrRows.
4023 if ( !GetNumberRows() )
4024 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4027 if ( !GetBatchCount() )
4030 m_rowLabelWin
->Refresh();
4036 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4038 size_t pos
= msg
.GetCommandInt();
4039 int numCols
= msg
.GetCommandInt2();
4040 m_numCols
+= numCols
;
4042 if ( !m_colWidths
.IsEmpty() )
4044 for ( i
= 0; i
< numCols
; i
++ )
4046 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4047 m_colRights
.Insert( 0, pos
);
4051 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4053 for ( i
= pos
; i
< m_numCols
; i
++ )
4055 right
+= m_colWidths
[i
];
4056 m_colRights
[i
] = right
;
4059 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4061 // if we have just inserted cols into an empty grid the current
4062 // cell will be undefined...
4064 SetCurrentCell( 0, 0 );
4066 m_selection
->UpdateCols( pos
, numCols
);
4067 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4069 attrProvider
->UpdateAttrCols( pos
, numCols
);
4070 if ( !GetBatchCount() )
4073 m_colLabelWin
->Refresh();
4080 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4082 int numCols
= msg
.GetCommandInt();
4083 int oldNumCols
= m_numCols
;
4084 m_numCols
+= numCols
;
4085 if ( !m_colWidths
.IsEmpty() )
4087 for ( i
= 0; i
< numCols
; i
++ )
4089 m_colWidths
.Add( m_defaultColWidth
);
4090 m_colRights
.Add( 0 );
4094 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4096 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4098 right
+= m_colWidths
[i
];
4099 m_colRights
[i
] = right
;
4102 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4104 // if we have just inserted cols into an empty grid the current
4105 // cell will be undefined...
4107 SetCurrentCell( 0, 0 );
4109 if ( !GetBatchCount() )
4112 m_colLabelWin
->Refresh();
4118 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4120 size_t pos
= msg
.GetCommandInt();
4121 int numCols
= msg
.GetCommandInt2();
4122 m_numCols
-= numCols
;
4124 if ( !m_colWidths
.IsEmpty() )
4126 for ( i
= 0; i
< numCols
; i
++ )
4128 m_colWidths
.Remove( pos
);
4129 m_colRights
.Remove( pos
);
4133 for ( i
= 0; i
< m_numCols
; i
++ )
4135 w
+= m_colWidths
[i
];
4141 m_currentCellCoords
= wxGridNoCellCoords
;
4145 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4146 m_currentCellCoords
.Set( 0, 0 );
4148 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4149 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4151 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4152 // ifdef'd out following patch from Paul Gammans
4154 // No need to touch row attributes, unless we
4155 // removed _all_ columns, in this case, we remove
4156 // all row attributes.
4157 // I hate to do this here, but the
4158 // needed data is not available inside UpdateAttrCols.
4159 if ( !GetNumberCols() )
4160 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4163 if ( !GetBatchCount() )
4166 m_colLabelWin
->Refresh();
4173 if (result
&& !GetBatchCount() )
4174 m_gridWin
->Refresh();
4179 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4181 wxRegionIterator
iter( reg
);
4184 wxArrayInt rowlabels
;
4191 // TODO: remove this when we can...
4192 // There is a bug in wxMotif that gives garbage update
4193 // rectangles if you jump-scroll a long way by clicking the
4194 // scrollbar with middle button. This is a work-around
4196 #if defined(__WXMOTIF__)
4198 m_gridWin
->GetClientSize( &cw
, &ch
);
4199 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4200 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4203 // logical bounds of update region
4206 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4207 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4209 // find the row labels within these bounds
4212 for ( row
= 0; row
< m_numRows
; row
++ )
4214 if ( GetRowBottom(row
) < top
)
4217 if ( GetRowTop(row
) > bottom
)
4220 rowlabels
.Add( row
);
4230 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4232 wxRegionIterator
iter( reg
);
4235 wxArrayInt colLabels
;
4242 // TODO: remove this when we can...
4243 // There is a bug in wxMotif that gives garbage update
4244 // rectangles if you jump-scroll a long way by clicking the
4245 // scrollbar with middle button. This is a work-around
4247 #if defined(__WXMOTIF__)
4249 m_gridWin
->GetClientSize( &cw
, &ch
);
4250 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4251 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4254 // logical bounds of update region
4257 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4258 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4260 // find the cells within these bounds
4263 for ( col
= 0; col
< m_numCols
; col
++ )
4265 if ( GetColRight(col
) < left
)
4268 if ( GetColLeft(col
) > right
)
4271 colLabels
.Add( col
);
4280 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4282 wxRegionIterator
iter( reg
);
4285 wxGridCellCoordsArray cellsExposed
;
4287 int left
, top
, right
, bottom
;
4292 // TODO: remove this when we can...
4293 // There is a bug in wxMotif that gives garbage update
4294 // rectangles if you jump-scroll a long way by clicking the
4295 // scrollbar with middle button. This is a work-around
4297 #if defined(__WXMOTIF__)
4299 m_gridWin
->GetClientSize( &cw
, &ch
);
4300 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4301 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4302 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4303 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4306 // logical bounds of update region
4308 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4309 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4311 // find the cells within these bounds
4314 for ( row
= 0; row
< m_numRows
; row
++ )
4316 if ( GetRowBottom(row
) <= top
)
4319 if ( GetRowTop(row
) > bottom
)
4323 for ( col
= 0; col
< m_numCols
; col
++ )
4325 if ( GetColRight(col
) <= left
)
4328 if ( GetColLeft(col
) > right
)
4331 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4338 return cellsExposed
;
4342 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4345 wxPoint
pos( event
.GetPosition() );
4346 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4348 if ( event
.Dragging() )
4350 m_isDragging
= TRUE
;
4352 if ( event
.LeftIsDown() )
4354 switch( m_cursorMode
)
4356 case WXGRID_CURSOR_RESIZE_ROW
:
4358 int cw
, ch
, left
, dummy
;
4359 m_gridWin
->GetClientSize( &cw
, &ch
);
4360 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4362 wxClientDC
dc( m_gridWin
);
4365 GetRowTop(m_dragRowOrCol
) +
4366 GetRowMinimalHeight(m_dragRowOrCol
) );
4367 dc
.SetLogicalFunction(wxINVERT
);
4368 if ( m_dragLastPos
>= 0 )
4370 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4372 dc
.DrawLine( left
, y
, left
+cw
, y
);
4377 case WXGRID_CURSOR_SELECT_ROW
:
4378 if ( (row
= YToRow( y
)) >= 0 )
4380 m_selection
->SelectRow( row
,
4381 event
.ControlDown(),
4387 // default label to suppress warnings about "enumeration value
4388 // 'xxx' not handled in switch
4396 m_isDragging
= FALSE
;
4399 // ------------ Entering or leaving the window
4401 if ( event
.Entering() || event
.Leaving() )
4403 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4407 // ------------ Left button pressed
4409 else if ( event
.LeftDown() )
4411 // don't send a label click event for a hit on the
4412 // edge of the row label - this is probably the user
4413 // wanting to resize the row
4415 if ( YToEdgeOfRow(y
) < 0 )
4419 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4421 if ( !event
.ShiftDown() && !event
.ControlDown() )
4423 if ( event
.ShiftDown() )
4424 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4427 GetNumberCols() - 1,
4428 event
.ControlDown(),
4433 m_selection
->SelectRow( row
,
4434 event
.ControlDown(),
4438 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4443 // starting to drag-resize a row
4445 if ( CanDragRowSize() )
4446 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4451 // ------------ Left double click
4453 else if (event
.LeftDClick() )
4455 if ( YToEdgeOfRow(y
) < 0 )
4458 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4463 // ------------ Left button released
4465 else if ( event
.LeftUp() )
4467 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4469 DoEndDragResizeRow();
4471 // Note: we are ending the event *after* doing
4472 // default processing in this case
4474 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4477 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4482 // ------------ Right button down
4484 else if ( event
.RightDown() )
4487 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4489 // no default action at the moment
4494 // ------------ Right double click
4496 else if ( event
.RightDClick() )
4499 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4501 // no default action at the moment
4506 // ------------ No buttons down and mouse moving
4508 else if ( event
.Moving() )
4510 m_dragRowOrCol
= YToEdgeOfRow( y
);
4511 if ( m_dragRowOrCol
>= 0 )
4513 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4515 // don't capture the mouse yet
4516 if ( CanDragRowSize() )
4517 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4520 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4522 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4528 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4531 wxPoint
pos( event
.GetPosition() );
4532 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4534 if ( event
.Dragging() )
4536 m_isDragging
= TRUE
;
4538 if ( event
.LeftIsDown() )
4540 switch( m_cursorMode
)
4542 case WXGRID_CURSOR_RESIZE_COL
:
4544 int cw
, ch
, dummy
, top
;
4545 m_gridWin
->GetClientSize( &cw
, &ch
);
4546 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4548 wxClientDC
dc( m_gridWin
);
4551 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4552 GetColMinimalWidth(m_dragRowOrCol
));
4553 dc
.SetLogicalFunction(wxINVERT
);
4554 if ( m_dragLastPos
>= 0 )
4556 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4558 dc
.DrawLine( x
, top
, x
, top
+ch
);
4563 case WXGRID_CURSOR_SELECT_COL
:
4564 if ( (col
= XToCol( x
)) >= 0 )
4566 m_selection
->SelectCol( col
,
4567 event
.ControlDown(),
4573 // default label to suppress warnings about "enumeration value
4574 // 'xxx' not handled in switch
4582 m_isDragging
= FALSE
;
4585 // ------------ Entering or leaving the window
4587 if ( event
.Entering() || event
.Leaving() )
4589 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4593 // ------------ Left button pressed
4595 else if ( event
.LeftDown() )
4597 // don't send a label click event for a hit on the
4598 // edge of the col label - this is probably the user
4599 // wanting to resize the col
4601 if ( XToEdgeOfCol(x
) < 0 )
4605 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4607 if ( !event
.ShiftDown() && !event
.ControlDown() )
4609 if ( event
.ShiftDown() )
4610 m_selection
->SelectBlock( 0,
4611 m_currentCellCoords
.GetCol(),
4612 GetNumberRows() - 1, col
,
4613 event
.ControlDown(),
4618 m_selection
->SelectCol( col
,
4619 event
.ControlDown(),
4623 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4628 // starting to drag-resize a col
4630 if ( CanDragColSize() )
4631 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4636 // ------------ Left double click
4638 if ( event
.LeftDClick() )
4640 if ( XToEdgeOfCol(x
) < 0 )
4643 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4648 // ------------ Left button released
4650 else if ( event
.LeftUp() )
4652 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4654 DoEndDragResizeCol();
4656 // Note: we are ending the event *after* doing
4657 // default processing in this case
4659 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4662 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4667 // ------------ Right button down
4669 else if ( event
.RightDown() )
4672 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4674 // no default action at the moment
4679 // ------------ Right double click
4681 else if ( event
.RightDClick() )
4684 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4686 // no default action at the moment
4691 // ------------ No buttons down and mouse moving
4693 else if ( event
.Moving() )
4695 m_dragRowOrCol
= XToEdgeOfCol( x
);
4696 if ( m_dragRowOrCol
>= 0 )
4698 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4700 // don't capture the cursor yet
4701 if ( CanDragColSize() )
4702 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4705 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4707 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4713 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4715 if ( event
.LeftDown() )
4717 // indicate corner label by having both row and
4720 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4726 else if ( event
.LeftDClick() )
4728 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4731 else if ( event
.RightDown() )
4733 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4735 // no default action at the moment
4739 else if ( event
.RightDClick() )
4741 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4743 // no default action at the moment
4748 void wxGrid::ChangeCursorMode(CursorMode mode
,
4753 static const wxChar
*cursorModes
[] =
4762 wxLogTrace(_T("grid"),
4763 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4764 win
== m_colLabelWin
? _T("colLabelWin")
4765 : win
? _T("rowLabelWin")
4767 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4768 #endif // __WXDEBUG__
4770 if ( mode
== m_cursorMode
&&
4771 win
== m_winCapture
&&
4772 captureMouse
== (m_winCapture
!= NULL
))
4777 // by default use the grid itself
4783 m_winCapture
->ReleaseMouse();
4784 m_winCapture
= (wxWindow
*)NULL
;
4787 m_cursorMode
= mode
;
4789 switch ( m_cursorMode
)
4791 case WXGRID_CURSOR_RESIZE_ROW
:
4792 win
->SetCursor( m_rowResizeCursor
);
4795 case WXGRID_CURSOR_RESIZE_COL
:
4796 win
->SetCursor( m_colResizeCursor
);
4800 win
->SetCursor( *wxSTANDARD_CURSOR
);
4803 // we need to capture mouse when resizing
4804 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4805 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4807 if ( captureMouse
&& resize
)
4809 win
->CaptureMouse();
4814 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4817 wxPoint
pos( event
.GetPosition() );
4818 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4820 wxGridCellCoords coords
;
4821 XYToCell( x
, y
, coords
);
4823 if ( event
.Dragging() )
4825 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4827 // Don't start doing anything until the mouse has been drug at
4828 // least 3 pixels in any direction...
4831 if (m_startDragPos
== wxDefaultPosition
)
4833 m_startDragPos
= pos
;
4836 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4840 m_isDragging
= TRUE
;
4841 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4843 // Hide the edit control, so it
4844 // won't interfer with drag-shrinking.
4845 if ( IsCellEditControlShown() )
4847 HideCellEditControl();
4848 SaveEditControlValue();
4851 // Have we captured the mouse yet?
4854 m_winCapture
= m_gridWin
;
4855 m_winCapture
->CaptureMouse();
4858 if ( coords
!= wxGridNoCellCoords
)
4860 if ( event
.ControlDown() )
4862 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4863 m_selectingKeyboard
= coords
;
4864 HighlightBlock ( m_selectingKeyboard
, coords
);
4868 if ( !IsSelection() )
4870 HighlightBlock( coords
, coords
);
4874 HighlightBlock( m_currentCellCoords
, coords
);
4878 if (! IsVisible(coords
))
4880 MakeCellVisible(coords
);
4881 // TODO: need to introduce a delay or something here. The
4882 // scrolling is way to fast, at least on MSW - also on GTK.
4886 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4888 int cw
, ch
, left
, dummy
;
4889 m_gridWin
->GetClientSize( &cw
, &ch
);
4890 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4892 wxClientDC
dc( m_gridWin
);
4894 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4895 GetRowMinimalHeight(m_dragRowOrCol
) );
4896 dc
.SetLogicalFunction(wxINVERT
);
4897 if ( m_dragLastPos
>= 0 )
4899 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4901 dc
.DrawLine( left
, y
, left
+cw
, y
);
4904 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4906 int cw
, ch
, dummy
, top
;
4907 m_gridWin
->GetClientSize( &cw
, &ch
);
4908 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4910 wxClientDC
dc( m_gridWin
);
4912 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4913 GetColMinimalWidth(m_dragRowOrCol
) );
4914 dc
.SetLogicalFunction(wxINVERT
);
4915 if ( m_dragLastPos
>= 0 )
4917 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4919 dc
.DrawLine( x
, top
, x
, top
+ch
);
4926 m_isDragging
= FALSE
;
4927 m_startDragPos
= wxDefaultPosition
;
4929 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4930 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4933 if ( event
.Entering() || event
.Leaving() )
4935 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4936 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4941 // ------------ Left button pressed
4943 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4945 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4950 if ( !event
.ControlDown() )
4952 if ( event
.ShiftDown() )
4954 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4955 m_currentCellCoords
.GetCol(),
4958 event
.ControlDown(),
4963 else if ( XToEdgeOfCol(x
) < 0 &&
4964 YToEdgeOfRow(y
) < 0 )
4966 DisableCellEditControl();
4967 MakeCellVisible( coords
);
4969 // if this is the second click on this cell then start
4971 if ( m_waitForSlowClick
&&
4972 (coords
== m_currentCellCoords
) &&
4973 CanEnableCellControl())
4975 EnableCellEditControl();
4977 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4978 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4981 editor
->StartingClick();
4985 m_waitForSlowClick
= FALSE
;
4989 if ( event
.ControlDown() )
4991 m_selection
->ToggleCellSelection( coords
.GetRow(),
4993 event
.ControlDown(),
4997 m_selectingTopLeft
= wxGridNoCellCoords
;
4998 m_selectingBottomRight
= wxGridNoCellCoords
;
4999 m_selectingKeyboard
= coords
;
5003 SetCurrentCell( coords
);
5004 if ( m_selection
->GetSelectionMode()
5005 != wxGrid::wxGridSelectCells
)
5006 HighlightBlock( coords
, coords
);
5008 m_waitForSlowClick
= TRUE
;
5015 // ------------ Left double click
5017 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5019 DisableCellEditControl();
5021 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5023 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5031 // ------------ Left button released
5033 else if ( event
.LeftUp() )
5035 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5037 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5038 m_selectingBottomRight
!= wxGridNoCellCoords
)
5042 m_winCapture
->ReleaseMouse();
5043 m_winCapture
= NULL
;
5045 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5046 m_selectingTopLeft
.GetCol(),
5047 m_selectingBottomRight
.GetRow(),
5048 m_selectingBottomRight
.GetCol(),
5049 event
.ControlDown(),
5053 m_selectingTopLeft
= wxGridNoCellCoords
;
5054 m_selectingBottomRight
= wxGridNoCellCoords
;
5057 // Show the edit control, if it has been hidden for
5059 ShowCellEditControl();
5061 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5063 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5064 DoEndDragResizeRow();
5066 // Note: we are ending the event *after* doing
5067 // default processing in this case
5069 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5071 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5073 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5074 DoEndDragResizeCol();
5076 // Note: we are ending the event *after* doing
5077 // default processing in this case
5079 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5086 // ------------ Right button down
5088 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5090 DisableCellEditControl();
5091 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5096 // no default action at the moment
5101 // ------------ Right double click
5103 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5105 DisableCellEditControl();
5106 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5111 // no default action at the moment
5115 // ------------ Moving and no button action
5117 else if ( event
.Moving() && !event
.IsButton() )
5119 int dragRow
= YToEdgeOfRow( y
);
5120 int dragCol
= XToEdgeOfCol( x
);
5122 // Dragging on the corner of a cell to resize in both
5123 // directions is not implemented yet...
5125 if ( dragRow
>= 0 && dragCol
>= 0 )
5127 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5133 m_dragRowOrCol
= dragRow
;
5135 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5137 if ( CanDragRowSize() && CanDragGridSize() )
5138 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5143 m_dragRowOrCol
= dragCol
;
5151 m_dragRowOrCol
= dragCol
;
5153 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5155 if ( CanDragColSize() && CanDragGridSize() )
5156 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5162 // Neither on a row or col edge
5164 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5166 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5172 void wxGrid::DoEndDragResizeRow()
5174 if ( m_dragLastPos
>= 0 )
5176 // erase the last line and resize the row
5178 int cw
, ch
, left
, dummy
;
5179 m_gridWin
->GetClientSize( &cw
, &ch
);
5180 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5182 wxClientDC
dc( m_gridWin
);
5184 dc
.SetLogicalFunction( wxINVERT
);
5185 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5186 HideCellEditControl();
5187 SaveEditControlValue();
5189 int rowTop
= GetRowTop(m_dragRowOrCol
);
5190 SetRowSize( m_dragRowOrCol
,
5191 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5193 if ( !GetBatchCount() )
5195 // Only needed to get the correct rect.y:
5196 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5198 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5199 rect
.width
= m_rowLabelWidth
;
5200 rect
.height
= ch
- rect
.y
;
5201 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5203 m_gridWin
->Refresh( FALSE
, &rect
);
5206 ShowCellEditControl();
5211 void wxGrid::DoEndDragResizeCol()
5213 if ( m_dragLastPos
>= 0 )
5215 // erase the last line and resize the col
5217 int cw
, ch
, dummy
, top
;
5218 m_gridWin
->GetClientSize( &cw
, &ch
);
5219 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5221 wxClientDC
dc( m_gridWin
);
5223 dc
.SetLogicalFunction( wxINVERT
);
5224 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5225 HideCellEditControl();
5226 SaveEditControlValue();
5228 int colLeft
= GetColLeft(m_dragRowOrCol
);
5229 SetColSize( m_dragRowOrCol
,
5230 wxMax( m_dragLastPos
- colLeft
,
5231 GetColMinimalWidth(m_dragRowOrCol
) ) );
5233 if ( !GetBatchCount() )
5235 // Only needed to get the correct rect.x:
5236 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5238 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5239 rect
.width
= cw
- rect
.x
;
5240 rect
.height
= m_colLabelHeight
;
5241 m_colLabelWin
->Refresh( TRUE
, &rect
);
5243 m_gridWin
->Refresh( FALSE
, &rect
);
5246 ShowCellEditControl();
5253 // ------ interaction with data model
5255 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5257 switch ( msg
.GetId() )
5259 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5260 return GetModelValues();
5262 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5263 return SetModelValues();
5265 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5266 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5267 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5268 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5269 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5270 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5271 return Redimension( msg
);
5280 // The behaviour of this function depends on the grid table class
5281 // Clear() function. For the default wxGridStringTable class the
5282 // behavious is to replace all cell contents with wxEmptyString but
5283 // not to change the number of rows or cols.
5285 void wxGrid::ClearGrid()
5289 if (IsCellEditControlEnabled())
5290 DisableCellEditControl();
5293 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5298 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5300 // TODO: something with updateLabels flag
5304 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5310 if (IsCellEditControlEnabled())
5311 DisableCellEditControl();
5313 return m_table
->InsertRows( pos
, numRows
);
5315 // the table will have sent the results of the insert row
5316 // operation to this view object as a grid table message
5322 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5324 // TODO: something with updateLabels flag
5328 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5332 return ( m_table
&& m_table
->AppendRows( numRows
) );
5333 // the table will have sent the results of the append row
5334 // operation to this view object as a grid table message
5338 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5340 // TODO: something with updateLabels flag
5344 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5350 if (IsCellEditControlEnabled())
5351 DisableCellEditControl();
5353 return (m_table
->DeleteRows( pos
, numRows
));
5354 // the table will have sent the results of the delete row
5355 // operation to this view object as a grid table message
5361 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5363 // TODO: something with updateLabels flag
5367 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5373 if (IsCellEditControlEnabled())
5374 DisableCellEditControl();
5376 return m_table
->InsertCols( pos
, numCols
);
5377 // the table will have sent the results of the insert col
5378 // operation to this view object as a grid table message
5384 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5386 // TODO: something with updateLabels flag
5390 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5394 return ( m_table
&& m_table
->AppendCols( numCols
) );
5395 // the table will have sent the results of the append col
5396 // operation to this view object as a grid table message
5400 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5402 // TODO: something with updateLabels flag
5406 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5412 if (IsCellEditControlEnabled())
5413 DisableCellEditControl();
5415 return ( m_table
->DeleteCols( pos
, numCols
) );
5416 // the table will have sent the results of the delete col
5417 // operation to this view object as a grid table message
5425 // ----- event handlers
5428 // Generate a grid event based on a mouse event and
5429 // return the result of ProcessEvent()
5431 bool wxGrid::SendEvent( const wxEventType type
,
5433 wxMouseEvent
& mouseEv
)
5435 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5437 int rowOrCol
= (row
== -1 ? col
: row
);
5439 wxGridSizeEvent
gridEvt( GetId(),
5443 mouseEv
.GetX() + GetRowLabelSize(),
5444 mouseEv
.GetY() + GetColLabelSize(),
5445 mouseEv
.ControlDown(),
5446 mouseEv
.ShiftDown(),
5448 mouseEv
.MetaDown() );
5449 return GetEventHandler()->ProcessEvent(gridEvt
);
5451 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5453 // Right now, it should _never_ end up here!
5454 wxGridRangeSelectEvent
gridEvt( GetId(),
5458 m_selectingBottomRight
,
5460 mouseEv
.ControlDown(),
5461 mouseEv
.ShiftDown(),
5463 mouseEv
.MetaDown() );
5465 return GetEventHandler()->ProcessEvent(gridEvt
);
5469 wxGridEvent
gridEvt( GetId(),
5473 mouseEv
.GetX() + GetRowLabelSize(),
5474 mouseEv
.GetY() + GetColLabelSize(),
5476 mouseEv
.ControlDown(),
5477 mouseEv
.ShiftDown(),
5479 mouseEv
.MetaDown() );
5480 return GetEventHandler()->ProcessEvent(gridEvt
);
5485 // Generate a grid event of specified type and return the result
5486 // of ProcessEvent().
5488 bool wxGrid::SendEvent( const wxEventType type
,
5491 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5493 int rowOrCol
= (row
== -1 ? col
: row
);
5495 wxGridSizeEvent
gridEvt( GetId(),
5500 return GetEventHandler()->ProcessEvent(gridEvt
);
5504 wxGridEvent
gridEvt( GetId(),
5509 return GetEventHandler()->ProcessEvent(gridEvt
);
5514 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5516 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5520 // This is just here to make sure that CalcDimensions gets called when
5521 // the grid view is resized... then the size event is skipped to allow
5522 // the box sizers to handle everything
5524 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5531 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5533 if ( m_inOnKeyDown
)
5535 // shouldn't be here - we are going round in circles...
5537 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5540 m_inOnKeyDown
= TRUE
;
5542 // propagate the event up and see if it gets processed
5544 wxWindow
*parent
= GetParent();
5545 wxKeyEvent
keyEvt( event
);
5546 keyEvt
.SetEventObject( parent
);
5548 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5551 // try local handlers
5553 switch ( event
.KeyCode() )
5556 if ( event
.ControlDown() )
5558 MoveCursorUpBlock( event
.ShiftDown() );
5562 MoveCursorUp( event
.ShiftDown() );
5567 if ( event
.ControlDown() )
5569 MoveCursorDownBlock( event
.ShiftDown() );
5573 MoveCursorDown( event
.ShiftDown() );
5578 if ( event
.ControlDown() )
5580 MoveCursorLeftBlock( event
.ShiftDown() );
5584 MoveCursorLeft( event
.ShiftDown() );
5589 if ( event
.ControlDown() )
5591 MoveCursorRightBlock( event
.ShiftDown() );
5595 MoveCursorRight( event
.ShiftDown() );
5600 case WXK_NUMPAD_ENTER
:
5601 if ( event
.ControlDown() )
5603 event
.Skip(); // to let the edit control have the return
5607 if ( GetGridCursorRow() < GetNumberRows()-1 )
5609 MoveCursorDown( event
.ShiftDown() );
5613 // at the bottom of a column
5614 HideCellEditControl();
5615 SaveEditControlValue();
5625 if (event
.ShiftDown())
5627 if ( GetGridCursorCol() > 0 )
5629 MoveCursorLeft( FALSE
);
5634 HideCellEditControl();
5635 SaveEditControlValue();
5640 if ( GetGridCursorCol() < GetNumberCols()-1 )
5642 MoveCursorRight( FALSE
);
5647 HideCellEditControl();
5648 SaveEditControlValue();
5654 if ( event
.ControlDown() )
5656 MakeCellVisible( 0, 0 );
5657 SetCurrentCell( 0, 0 );
5666 if ( event
.ControlDown() )
5668 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5669 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5686 if ( event
.ControlDown() )
5688 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5689 m_currentCellCoords
.GetCol(),
5690 event
.ControlDown(),
5696 if ( !IsEditable() )
5698 MoveCursorRight( FALSE
);
5701 // Otherwise fall through to default
5704 // is it possible to edit the current cell at all?
5705 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5707 // yes, now check whether the cells editor accepts the key
5708 int row
= m_currentCellCoords
.GetRow();
5709 int col
= m_currentCellCoords
.GetCol();
5710 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5711 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5713 // <F2> is special and will always start editing, for
5714 // other keys - ask the editor itself
5715 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5716 || editor
->IsAcceptedKey(event
) )
5718 EnableCellEditControl();
5719 editor
->StartingKey(event
);
5731 // let others process char events with modifiers or all
5732 // char events for readonly cells
5739 m_inOnKeyDown
= FALSE
;
5742 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5744 // try local handlers
5746 if ( event
.KeyCode() == WXK_SHIFT
)
5748 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5749 m_selectingBottomRight
!= wxGridNoCellCoords
)
5750 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5751 m_selectingTopLeft
.GetCol(),
5752 m_selectingBottomRight
.GetRow(),
5753 m_selectingBottomRight
.GetCol(),
5754 event
.ControlDown(),
5758 m_selectingTopLeft
= wxGridNoCellCoords
;
5759 m_selectingBottomRight
= wxGridNoCellCoords
;
5760 m_selectingKeyboard
= wxGridNoCellCoords
;
5764 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5768 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5770 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5772 // the event has been intercepted - do nothing
5776 wxClientDC
dc(m_gridWin
);
5779 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5781 HideCellEditControl();
5782 DisableCellEditControl();
5784 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5787 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5788 if ( !m_gridLinesEnabled
)
5796 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5798 // Otherwise refresh redraws the highlight!
5799 m_currentCellCoords
= coords
;
5801 DrawGridCellArea(dc
,cells
);
5802 DrawAllGridLines( dc
, r
);
5806 m_currentCellCoords
= coords
;
5808 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5809 DrawCellHighlight(dc
, attr
);
5814 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5817 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5819 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5822 rightCol
= GetNumberCols() - 1;
5824 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5827 bottomRow
= GetNumberRows() - 1;
5829 if ( topRow
> bottomRow
)
5836 if ( leftCol
> rightCol
)
5843 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5844 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5846 if ( m_selectingTopLeft
!= updateTopLeft
||
5847 m_selectingBottomRight
!= updateBottomRight
)
5849 // Compute two optimal update rectangles:
5850 // Either one rectangle is a real subset of the
5851 // other, or they are (almost) disjoint!
5853 bool need_refresh
[4];
5857 need_refresh
[3] = FALSE
;
5860 // Store intermediate values
5861 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5862 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5863 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5864 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5866 // Determine the outer/inner coordinates.
5867 if (oldLeft
> leftCol
)
5873 if (oldTop
> topRow
)
5879 if (oldRight
< rightCol
)
5882 oldRight
= rightCol
;
5885 if (oldBottom
< bottomRow
)
5888 oldBottom
= bottomRow
;
5892 // Now, either the stuff marked old is the outer
5893 // rectangle or we don't have a situation where one
5894 // is contained in the other.
5896 if ( oldLeft
< leftCol
)
5898 need_refresh
[0] = TRUE
;
5899 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5901 wxGridCellCoords ( oldBottom
,
5905 if ( oldTop
< topRow
)
5907 need_refresh
[1] = TRUE
;
5908 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5910 wxGridCellCoords ( topRow
- 1,
5914 if ( oldRight
> rightCol
)
5916 need_refresh
[2] = TRUE
;
5917 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5919 wxGridCellCoords ( oldBottom
,
5923 if ( oldBottom
> bottomRow
)
5925 need_refresh
[3] = TRUE
;
5926 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5928 wxGridCellCoords ( oldBottom
,
5934 m_selectingTopLeft
= updateTopLeft
;
5935 m_selectingBottomRight
= updateBottomRight
;
5937 // various Refresh() calls
5938 for (i
= 0; i
< 4; i
++ )
5939 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5940 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5943 // never generate an event as it will be generated from
5944 // wxGridSelection::SelectBlock!
5945 // (old comment from when this was the body of SelectBlock)
5949 // ------ functions to get/send data (see also public functions)
5952 bool wxGrid::GetModelValues()
5956 // all we need to do is repaint the grid
5958 m_gridWin
->Refresh();
5966 bool wxGrid::SetModelValues()
5972 for ( row
= 0; row
< m_numRows
; row
++ )
5974 for ( col
= 0; col
< m_numCols
; col
++ )
5976 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5988 // Note - this function only draws cells that are in the list of
5989 // exposed cells (usually set from the update region by
5990 // CalcExposedCells)
5992 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5994 if ( !m_numRows
|| !m_numCols
) return;
5997 size_t numCells
= cells
.GetCount();
5999 for ( i
= 0; i
< numCells
; i
++ )
6001 DrawCell( dc
, cells
[i
] );
6006 void wxGrid::DrawGridSpace( wxDC
& dc
)
6009 m_gridWin
->GetClientSize( &cw
, &ch
);
6012 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6014 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6015 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6017 if ( right
> rightCol
|| bottom
> bottomRow
)
6020 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6022 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6023 dc
.SetPen( *wxTRANSPARENT_PEN
);
6025 if ( right
> rightCol
)
6027 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6030 if ( bottom
> bottomRow
)
6032 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6038 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6040 int row
= coords
.GetRow();
6041 int col
= coords
.GetCol();
6043 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6046 // we draw the cell border ourselves
6047 #if !WXGRID_DRAW_LINES
6048 if ( m_gridLinesEnabled
)
6049 DrawCellBorder( dc
, coords
);
6052 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6054 bool isCurrent
= coords
== m_currentCellCoords
;
6056 wxRect rect
= CellToRect( row
, col
);
6058 // if the editor is shown, we should use it and not the renderer
6059 // Note: However, only if it is really _shown_, i.e. not hidden!
6060 if ( isCurrent
&& IsCellEditControlShown() )
6062 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6063 editor
->PaintBackground(rect
, attr
);
6068 // but all the rest is drawn by the cell renderer and hence may be
6070 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6071 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6078 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6080 int row
= m_currentCellCoords
.GetRow();
6081 int col
= m_currentCellCoords
.GetCol();
6083 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6086 wxRect rect
= CellToRect(row
, col
);
6088 // hmmm... what could we do here to show that the cell is disabled?
6089 // for now, I just draw a thinner border than for the other ones, but
6090 // it doesn't look really good
6092 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6096 // The center of th drawn line is where the position/width/height of
6097 // the rectangle is actually at, (on wxMSW atr least,) so we will
6098 // reduce the size of the rectangle to compensate for the thickness of
6099 // the line. If this is too strange on non wxMSW platforms then
6100 // please #ifdef this appropriately.
6101 rect
.x
+= penWidth
/2;
6102 rect
.y
+= penWidth
/2;
6103 rect
.width
-= penWidth
-1;
6104 rect
.height
-= penWidth
-1;
6107 // Now draw the rectangle
6108 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6109 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6110 dc
.DrawRectangle(rect
);
6114 // VZ: my experiments with 3d borders...
6116 // how to properly set colours for arbitrary bg?
6117 wxCoord x1
= rect
.x
,
6119 x2
= rect
.x
+ rect
.width
-1,
6120 y2
= rect
.y
+ rect
.height
-1;
6122 dc
.SetPen(*wxWHITE_PEN
);
6123 dc
.DrawLine(x1
, y1
, x2
, y1
);
6124 dc
.DrawLine(x1
, y1
, x1
, y2
);
6126 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6127 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6129 dc
.SetPen(*wxBLACK_PEN
);
6130 dc
.DrawLine(x1
, y2
, x2
, y2
);
6131 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6136 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6138 int row
= coords
.GetRow();
6139 int col
= coords
.GetCol();
6140 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6143 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6145 // right hand border
6147 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6148 GetColRight(col
), GetRowBottom(row
) );
6152 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6153 GetColRight(col
), GetRowBottom(row
) );
6156 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6158 // This if block was previously in wxGrid::OnPaint but that doesn't
6159 // seem to get called under wxGTK - MB
6161 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6162 m_numRows
&& m_numCols
)
6164 m_currentCellCoords
.Set(0, 0);
6167 if ( IsCellEditControlShown() )
6169 // don't show highlight when the edit control is shown
6173 // if the active cell was repainted, repaint its highlight too because it
6174 // might have been damaged by the grid lines
6175 size_t count
= cells
.GetCount();
6176 for ( size_t n
= 0; n
< count
; n
++ )
6178 if ( cells
[n
] == m_currentCellCoords
)
6180 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6181 DrawCellHighlight(dc
, attr
);
6189 // TODO: remove this ???
6190 // This is used to redraw all grid lines e.g. when the grid line colour
6193 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6195 if ( !m_gridLinesEnabled
||
6197 !m_numCols
) return;
6199 int top
, bottom
, left
, right
;
6201 #if 0 //#ifndef __WXGTK__
6205 m_gridWin
->GetClientSize(&cw
, &ch
);
6207 // virtual coords of visible area
6209 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6210 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6215 reg
.GetBox(x
, y
, w
, h
);
6216 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6217 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6221 m_gridWin
->GetClientSize(&cw
, &ch
);
6222 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6223 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6226 // avoid drawing grid lines past the last row and col
6228 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6229 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6231 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6233 // horizontal grid lines
6236 for ( i
= 0; i
< m_numRows
; i
++ )
6238 int bot
= GetRowBottom(i
) - 1;
6247 dc
.DrawLine( left
, bot
, right
, bot
);
6252 // vertical grid lines
6254 for ( i
= 0; i
< m_numCols
; i
++ )
6256 int colRight
= GetColRight(i
) - 1;
6257 if ( colRight
> right
)
6262 if ( colRight
>= left
)
6264 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6270 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6272 if ( !m_numRows
) return;
6275 size_t numLabels
= rows
.GetCount();
6277 for ( i
= 0; i
< numLabels
; i
++ )
6279 DrawRowLabel( dc
, rows
[i
] );
6284 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6286 if ( GetRowHeight(row
) <= 0 )
6289 int rowTop
= GetRowTop(row
),
6290 rowBottom
= GetRowBottom(row
) - 1;
6292 dc
.SetPen( *wxBLACK_PEN
);
6293 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6294 m_rowLabelWidth
-1, rowBottom
);
6296 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6298 dc
.SetPen( *wxWHITE_PEN
);
6299 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6300 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6302 dc
.SetBackgroundMode( wxTRANSPARENT
);
6303 dc
.SetTextForeground( GetLabelTextColour() );
6304 dc
.SetFont( GetLabelFont() );
6307 GetRowLabelAlignment( &hAlign
, &vAlign
);
6311 rect
.SetY( GetRowTop(row
) + 2 );
6312 rect
.SetWidth( m_rowLabelWidth
- 4 );
6313 rect
.SetHeight( GetRowHeight(row
) - 4 );
6314 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6318 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6320 if ( !m_numCols
) return;
6323 size_t numLabels
= cols
.GetCount();
6325 for ( i
= 0; i
< numLabels
; i
++ )
6327 DrawColLabel( dc
, cols
[i
] );
6332 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6334 if ( GetColWidth(col
) <= 0 )
6337 int colLeft
= GetColLeft(col
),
6338 colRight
= GetColRight(col
) - 1;
6340 dc
.SetPen( *wxBLACK_PEN
);
6341 dc
.DrawLine( colRight
, 0,
6342 colRight
, m_colLabelHeight
-1 );
6344 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6345 colRight
, m_colLabelHeight
-1 );
6347 dc
.SetPen( *wxWHITE_PEN
);
6348 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6349 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6351 dc
.SetBackgroundMode( wxTRANSPARENT
);
6352 dc
.SetTextForeground( GetLabelTextColour() );
6353 dc
.SetFont( GetLabelFont() );
6355 dc
.SetBackgroundMode( wxTRANSPARENT
);
6356 dc
.SetTextForeground( GetLabelTextColour() );
6357 dc
.SetFont( GetLabelFont() );
6360 GetColLabelAlignment( &hAlign
, &vAlign
);
6363 rect
.SetX( colLeft
+ 2 );
6365 rect
.SetWidth( GetColWidth(col
) - 4 );
6366 rect
.SetHeight( m_colLabelHeight
- 4 );
6367 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6370 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6371 const wxString
& value
,
6376 wxArrayString lines
;
6378 dc
.SetClippingRegion( rect
);
6379 StringToLines( value
, lines
);
6382 //Forward to new API.
6383 DrawTextRectangle( dc
,
6391 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6392 const wxArrayString
& lines
,
6397 long textWidth
, textHeight
;
6398 long lineWidth
, lineHeight
;
6400 if ( lines
.GetCount() )
6402 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6403 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6406 switch ( horizAlign
)
6409 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6412 case wxALIGN_CENTRE
:
6413 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6422 switch ( vertAlign
)
6424 case wxALIGN_BOTTOM
:
6425 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6428 case wxALIGN_CENTRE
:
6429 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6438 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6440 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6445 dc
.DestroyClippingRegion();
6449 // Split multi line text up into an array of strings. Any existing
6450 // contents of the string array are preserved.
6452 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6456 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6457 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6459 while ( startPos
< (int)tVal
.Length() )
6461 pos
= tVal
.Mid(startPos
).Find( eol
);
6466 else if ( pos
== 0 )
6468 lines
.Add( wxEmptyString
);
6472 lines
.Add( value
.Mid(startPos
, pos
) );
6476 if ( startPos
< (int)value
.Length() )
6478 lines
.Add( value
.Mid( startPos
) );
6483 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6484 const wxArrayString
& lines
,
6485 long *width
, long *height
)
6492 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6494 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6495 w
= wxMax( w
, lineW
);
6504 // ------ Batch processing.
6506 void wxGrid::EndBatch()
6508 if ( m_batchCount
> 0 )
6511 if ( !m_batchCount
)
6514 m_rowLabelWin
->Refresh();
6515 m_colLabelWin
->Refresh();
6516 m_cornerLabelWin
->Refresh();
6517 m_gridWin
->Refresh();
6522 // Use this, rather than wxWindow::Refresh(), to force an immediate
6523 // repainting of the grid. Has no effect if you are already inside a
6524 // BeginBatch / EndBatch block.
6526 void wxGrid::ForceRefresh()
6534 // ------ Edit control functions
6538 void wxGrid::EnableEditing( bool edit
)
6540 // TODO: improve this ?
6542 if ( edit
!= m_editable
)
6544 if(!edit
) EnableCellEditControl(edit
);
6550 void wxGrid::EnableCellEditControl( bool enable
)
6555 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6556 SetCurrentCell( 0, 0 );
6558 if ( enable
!= m_cellEditCtrlEnabled
)
6560 // TODO allow the app to Veto() this event?
6561 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6565 // this should be checked by the caller!
6566 wxASSERT_MSG( CanEnableCellControl(),
6567 _T("can't enable editing for this cell!") );
6569 // do it before ShowCellEditControl()
6570 m_cellEditCtrlEnabled
= enable
;
6572 ShowCellEditControl();
6576 HideCellEditControl();
6577 SaveEditControlValue();
6579 // do it after HideCellEditControl()
6580 m_cellEditCtrlEnabled
= enable
;
6585 bool wxGrid::IsCurrentCellReadOnly() const
6588 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6589 bool readonly
= attr
->IsReadOnly();
6595 bool wxGrid::CanEnableCellControl() const
6597 return m_editable
&& !IsCurrentCellReadOnly();
6600 bool wxGrid::IsCellEditControlEnabled() const
6602 // the cell edit control might be disable for all cells or just for the
6603 // current one if it's read only
6604 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6607 bool wxGrid::IsCellEditControlShown() const
6609 bool isShown
= FALSE
;
6611 if ( m_cellEditCtrlEnabled
)
6613 int row
= m_currentCellCoords
.GetRow();
6614 int col
= m_currentCellCoords
.GetCol();
6615 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6616 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6621 if ( editor
->IsCreated() )
6623 isShown
= editor
->GetControl()->IsShown();
6633 void wxGrid::ShowCellEditControl()
6635 if ( IsCellEditControlEnabled() )
6637 if ( !IsVisible( m_currentCellCoords
) )
6639 m_cellEditCtrlEnabled
= FALSE
;
6644 wxRect rect
= CellToRect( m_currentCellCoords
);
6645 int row
= m_currentCellCoords
.GetRow();
6646 int col
= m_currentCellCoords
.GetCol();
6648 // convert to scrolled coords
6650 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6652 // done in PaintBackground()
6654 // erase the highlight and the cell contents because the editor
6655 // might not cover the entire cell
6656 wxClientDC
dc( m_gridWin
);
6658 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6659 dc
.SetPen(*wxTRANSPARENT_PEN
);
6660 dc
.DrawRectangle(rect
);
6663 // cell is shifted by one pixel
6667 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6668 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6669 if ( !editor
->IsCreated() )
6671 editor
->Create(m_gridWin
, -1,
6672 new wxGridCellEditorEvtHandler(this, editor
));
6674 wxGridEditorCreatedEvent
evt(GetId(),
6675 wxEVT_GRID_EDITOR_CREATED
,
6679 editor
->GetControl());
6680 GetEventHandler()->ProcessEvent(evt
);
6683 editor
->Show( TRUE
, attr
);
6685 editor
->SetSize( rect
);
6687 editor
->BeginEdit(row
, col
, this);
6696 void wxGrid::HideCellEditControl()
6698 if ( IsCellEditControlEnabled() )
6700 int row
= m_currentCellCoords
.GetRow();
6701 int col
= m_currentCellCoords
.GetCol();
6703 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6704 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6705 editor
->Show( FALSE
);
6708 m_gridWin
->SetFocus();
6709 wxRect
rect( CellToRect( row
, col
) );
6710 m_gridWin
->Refresh( FALSE
, &rect
);
6715 void wxGrid::SaveEditControlValue()
6717 if ( IsCellEditControlEnabled() )
6719 int row
= m_currentCellCoords
.GetRow();
6720 int col
= m_currentCellCoords
.GetCol();
6722 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6723 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6724 bool changed
= editor
->EndEdit(row
, col
, this);
6731 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6732 m_currentCellCoords
.GetRow(),
6733 m_currentCellCoords
.GetCol() );
6740 // ------ Grid location functions
6741 // Note that all of these functions work with the logical coordinates of
6742 // grid cells and labels so you will need to convert from device
6743 // coordinates for mouse events etc.
6746 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6748 int row
= YToRow(y
);
6749 int col
= XToCol(x
);
6751 if ( row
== -1 || col
== -1 )
6753 coords
= wxGridNoCellCoords
;
6757 coords
.Set( row
, col
);
6762 int wxGrid::YToRow( int y
)
6766 for ( i
= 0; i
< m_numRows
; i
++ )
6768 if ( y
< GetRowBottom(i
) )
6776 int wxGrid::XToCol( int x
)
6780 for ( i
= 0; i
< m_numCols
; i
++ )
6782 if ( x
< GetColRight(i
) )
6790 // return the row number that that the y coord is near the edge of, or
6791 // -1 if not near an edge
6793 int wxGrid::YToEdgeOfRow( int y
)
6797 for ( i
= 0; i
< m_numRows
; i
++ )
6799 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6801 d
= abs( y
- GetRowBottom(i
) );
6802 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6811 // return the col number that that the x coord is near the edge of, or
6812 // -1 if not near an edge
6814 int wxGrid::XToEdgeOfCol( int x
)
6818 for ( i
= 0; i
< m_numCols
; i
++ )
6820 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6822 d
= abs( x
- GetColRight(i
) );
6823 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6832 wxRect
wxGrid::CellToRect( int row
, int col
)
6834 wxRect
rect( -1, -1, -1, -1 );
6836 if ( row
>= 0 && row
< m_numRows
&&
6837 col
>= 0 && col
< m_numCols
)
6839 rect
.x
= GetColLeft(col
);
6840 rect
.y
= GetRowTop(row
);
6841 rect
.width
= GetColWidth(col
);
6842 rect
.height
= GetRowHeight(row
);
6845 // if grid lines are enabled, then the area of the cell is a bit smaller
6846 if (m_gridLinesEnabled
) {
6854 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6856 // get the cell rectangle in logical coords
6858 wxRect
r( CellToRect( row
, col
) );
6860 // convert to device coords
6862 int left
, top
, right
, bottom
;
6863 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6864 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6866 // check against the client area of the grid window
6869 m_gridWin
->GetClientSize( &cw
, &ch
);
6871 if ( wholeCellVisible
)
6873 // is the cell wholly visible ?
6875 return ( left
>= 0 && right
<= cw
&&
6876 top
>= 0 && bottom
<= ch
);
6880 // is the cell partly visible ?
6882 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6883 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6888 // make the specified cell location visible by doing a minimal amount
6891 void wxGrid::MakeCellVisible( int row
, int col
)
6894 int xpos
= -1, ypos
= -1;
6896 if ( row
>= 0 && row
< m_numRows
&&
6897 col
>= 0 && col
< m_numCols
)
6899 // get the cell rectangle in logical coords
6901 wxRect
r( CellToRect( row
, col
) );
6903 // convert to device coords
6905 int left
, top
, right
, bottom
;
6906 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6907 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6910 m_gridWin
->GetClientSize( &cw
, &ch
);
6916 else if ( bottom
> ch
)
6918 int h
= r
.GetHeight();
6920 for ( i
= row
-1; i
>= 0; i
-- )
6922 int rowHeight
= GetRowHeight(i
);
6923 if ( h
+ rowHeight
> ch
)
6930 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6931 // have rounding errors (this is important, because if we do, we
6932 // might not scroll at all and some cells won't be redrawn)
6933 ypos
+= GRID_SCROLL_LINE
/ 2;
6940 else if ( right
> cw
)
6942 int w
= r
.GetWidth();
6944 for ( i
= col
-1; i
>= 0; i
-- )
6946 int colWidth
= GetColWidth(i
);
6947 if ( w
+ colWidth
> cw
)
6954 // see comment for ypos above
6955 xpos
+= GRID_SCROLL_LINE
/ 2;
6958 if ( xpos
!= -1 || ypos
!= -1 )
6960 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6961 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6962 Scroll( xpos
, ypos
);
6970 // ------ Grid cursor movement functions
6973 bool wxGrid::MoveCursorUp( bool expandSelection
)
6975 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6976 m_currentCellCoords
.GetRow() >= 0 )
6978 if ( expandSelection
)
6980 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6981 m_selectingKeyboard
= m_currentCellCoords
;
6982 if ( m_selectingKeyboard
.GetRow() > 0 )
6984 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6985 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6986 m_selectingKeyboard
.GetCol() );
6987 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6990 else if ( m_currentCellCoords
.GetRow() > 0 )
6993 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6994 m_currentCellCoords
.GetCol() );
6995 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6996 m_currentCellCoords
.GetCol() );
7007 bool wxGrid::MoveCursorDown( bool expandSelection
)
7009 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7010 m_currentCellCoords
.GetRow() < m_numRows
)
7012 if ( expandSelection
)
7014 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7015 m_selectingKeyboard
= m_currentCellCoords
;
7016 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7018 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7019 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7020 m_selectingKeyboard
.GetCol() );
7021 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7024 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7027 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7028 m_currentCellCoords
.GetCol() );
7029 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7030 m_currentCellCoords
.GetCol() );
7041 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7043 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7044 m_currentCellCoords
.GetCol() >= 0 )
7046 if ( expandSelection
)
7048 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7049 m_selectingKeyboard
= m_currentCellCoords
;
7050 if ( m_selectingKeyboard
.GetCol() > 0 )
7052 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7053 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7054 m_selectingKeyboard
.GetCol() );
7055 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7058 else if ( m_currentCellCoords
.GetCol() > 0 )
7061 MakeCellVisible( m_currentCellCoords
.GetRow(),
7062 m_currentCellCoords
.GetCol() - 1 );
7063 SetCurrentCell( m_currentCellCoords
.GetRow(),
7064 m_currentCellCoords
.GetCol() - 1 );
7075 bool wxGrid::MoveCursorRight( bool expandSelection
)
7077 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7078 m_currentCellCoords
.GetCol() < m_numCols
)
7080 if ( expandSelection
)
7082 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7083 m_selectingKeyboard
= m_currentCellCoords
;
7084 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7086 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7087 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7088 m_selectingKeyboard
.GetCol() );
7089 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7092 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7095 MakeCellVisible( m_currentCellCoords
.GetRow(),
7096 m_currentCellCoords
.GetCol() + 1 );
7097 SetCurrentCell( m_currentCellCoords
.GetRow(),
7098 m_currentCellCoords
.GetCol() + 1 );
7109 bool wxGrid::MovePageUp()
7111 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7113 int row
= m_currentCellCoords
.GetRow();
7117 m_gridWin
->GetClientSize( &cw
, &ch
);
7119 int y
= GetRowTop(row
);
7120 int newRow
= YToRow( y
- ch
+ 1 );
7125 else if ( newRow
== row
)
7130 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7131 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7139 bool wxGrid::MovePageDown()
7141 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7143 int row
= m_currentCellCoords
.GetRow();
7144 if ( row
< m_numRows
)
7147 m_gridWin
->GetClientSize( &cw
, &ch
);
7149 int y
= GetRowTop(row
);
7150 int newRow
= YToRow( y
+ ch
);
7153 newRow
= m_numRows
- 1;
7155 else if ( newRow
== row
)
7160 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7161 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7169 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7172 m_currentCellCoords
!= wxGridNoCellCoords
&&
7173 m_currentCellCoords
.GetRow() > 0 )
7175 int row
= m_currentCellCoords
.GetRow();
7176 int col
= m_currentCellCoords
.GetCol();
7178 if ( m_table
->IsEmptyCell(row
, col
) )
7180 // starting in an empty cell: find the next block of
7186 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7189 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7191 // starting at the top of a block: find the next block
7197 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7202 // starting within a block: find the top of the block
7207 if ( m_table
->IsEmptyCell(row
, col
) )
7215 MakeCellVisible( row
, col
);
7216 if ( expandSelection
)
7218 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7219 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7224 SetCurrentCell( row
, col
);
7232 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7235 m_currentCellCoords
!= wxGridNoCellCoords
&&
7236 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7238 int row
= m_currentCellCoords
.GetRow();
7239 int col
= m_currentCellCoords
.GetCol();
7241 if ( m_table
->IsEmptyCell(row
, col
) )
7243 // starting in an empty cell: find the next block of
7246 while ( row
< m_numRows
-1 )
7249 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7252 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7254 // starting at the bottom of a block: find the next block
7257 while ( row
< m_numRows
-1 )
7260 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7265 // starting within a block: find the bottom of the block
7267 while ( row
< m_numRows
-1 )
7270 if ( m_table
->IsEmptyCell(row
, col
) )
7278 MakeCellVisible( row
, col
);
7279 if ( expandSelection
)
7281 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7282 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7287 SetCurrentCell( row
, col
);
7296 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7299 m_currentCellCoords
!= wxGridNoCellCoords
&&
7300 m_currentCellCoords
.GetCol() > 0 )
7302 int row
= m_currentCellCoords
.GetRow();
7303 int col
= m_currentCellCoords
.GetCol();
7305 if ( m_table
->IsEmptyCell(row
, col
) )
7307 // starting in an empty cell: find the next block of
7313 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7316 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7318 // starting at the left of a block: find the next block
7324 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7329 // starting within a block: find the left of the block
7334 if ( m_table
->IsEmptyCell(row
, col
) )
7342 MakeCellVisible( row
, col
);
7343 if ( expandSelection
)
7345 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7346 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7351 SetCurrentCell( row
, col
);
7360 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7363 m_currentCellCoords
!= wxGridNoCellCoords
&&
7364 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7366 int row
= m_currentCellCoords
.GetRow();
7367 int col
= m_currentCellCoords
.GetCol();
7369 if ( m_table
->IsEmptyCell(row
, col
) )
7371 // starting in an empty cell: find the next block of
7374 while ( col
< m_numCols
-1 )
7377 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7380 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7382 // starting at the right of a block: find the next block
7385 while ( col
< m_numCols
-1 )
7388 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7393 // starting within a block: find the right of the block
7395 while ( col
< m_numCols
-1 )
7398 if ( m_table
->IsEmptyCell(row
, col
) )
7406 MakeCellVisible( row
, col
);
7407 if ( expandSelection
)
7409 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7410 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7415 SetCurrentCell( row
, col
);
7427 // ------ Label values and formatting
7430 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7432 *horiz
= m_rowLabelHorizAlign
;
7433 *vert
= m_rowLabelVertAlign
;
7436 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7438 *horiz
= m_colLabelHorizAlign
;
7439 *vert
= m_colLabelVertAlign
;
7442 wxString
wxGrid::GetRowLabelValue( int row
)
7446 return m_table
->GetRowLabelValue( row
);
7456 wxString
wxGrid::GetColLabelValue( int col
)
7460 return m_table
->GetColLabelValue( col
);
7471 void wxGrid::SetRowLabelSize( int width
)
7473 width
= wxMax( width
, 0 );
7474 if ( width
!= m_rowLabelWidth
)
7478 m_rowLabelWin
->Show( FALSE
);
7479 m_cornerLabelWin
->Show( FALSE
);
7481 else if ( m_rowLabelWidth
== 0 )
7483 m_rowLabelWin
->Show( TRUE
);
7484 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7487 m_rowLabelWidth
= width
;
7494 void wxGrid::SetColLabelSize( int height
)
7496 height
= wxMax( height
, 0 );
7497 if ( height
!= m_colLabelHeight
)
7501 m_colLabelWin
->Show( FALSE
);
7502 m_cornerLabelWin
->Show( FALSE
);
7504 else if ( m_colLabelHeight
== 0 )
7506 m_colLabelWin
->Show( TRUE
);
7507 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7510 m_colLabelHeight
= height
;
7517 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7519 if ( m_labelBackgroundColour
!= colour
)
7521 m_labelBackgroundColour
= colour
;
7522 m_rowLabelWin
->SetBackgroundColour( colour
);
7523 m_colLabelWin
->SetBackgroundColour( colour
);
7524 m_cornerLabelWin
->SetBackgroundColour( colour
);
7526 if ( !GetBatchCount() )
7528 m_rowLabelWin
->Refresh();
7529 m_colLabelWin
->Refresh();
7530 m_cornerLabelWin
->Refresh();
7535 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7537 if ( m_labelTextColour
!= colour
)
7539 m_labelTextColour
= colour
;
7540 if ( !GetBatchCount() )
7542 m_rowLabelWin
->Refresh();
7543 m_colLabelWin
->Refresh();
7548 void wxGrid::SetLabelFont( const wxFont
& font
)
7551 if ( !GetBatchCount() )
7553 m_rowLabelWin
->Refresh();
7554 m_colLabelWin
->Refresh();
7558 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7560 // allow old (incorrect) defs to be used
7563 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7564 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7565 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7570 case wxTOP
: vert
= wxALIGN_TOP
; break;
7571 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7572 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7575 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7577 m_rowLabelHorizAlign
= horiz
;
7580 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7582 m_rowLabelVertAlign
= vert
;
7585 if ( !GetBatchCount() )
7587 m_rowLabelWin
->Refresh();
7591 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7593 // allow old (incorrect) defs to be used
7596 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7597 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7598 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7603 case wxTOP
: vert
= wxALIGN_TOP
; break;
7604 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7605 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7608 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7610 m_colLabelHorizAlign
= horiz
;
7613 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7615 m_colLabelVertAlign
= vert
;
7618 if ( !GetBatchCount() )
7620 m_colLabelWin
->Refresh();
7624 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7628 m_table
->SetRowLabelValue( row
, s
);
7629 if ( !GetBatchCount() )
7631 wxRect rect
= CellToRect( row
, 0);
7632 if ( rect
.height
> 0 )
7634 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7636 rect
.width
= m_rowLabelWidth
;
7637 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7643 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7647 m_table
->SetColLabelValue( col
, s
);
7648 if ( !GetBatchCount() )
7650 wxRect rect
= CellToRect( 0, col
);
7651 if ( rect
.width
> 0 )
7653 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7655 rect
.height
= m_colLabelHeight
;
7656 m_colLabelWin
->Refresh( TRUE
, &rect
);
7662 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7664 if ( m_gridLineColour
!= colour
)
7666 m_gridLineColour
= colour
;
7668 wxClientDC
dc( m_gridWin
);
7670 DrawAllGridLines( dc
, wxRegion() );
7675 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7677 if ( m_cellHighlightColour
!= colour
)
7679 m_cellHighlightColour
= colour
;
7681 wxClientDC
dc( m_gridWin
);
7683 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7684 DrawCellHighlight(dc
, attr
);
7689 void wxGrid::SetCellHighlightPenWidth(int width
)
7691 if (m_cellHighlightPenWidth
!= width
) {
7692 m_cellHighlightPenWidth
= width
;
7694 // Just redrawing the cell highlight is not enough since that won't
7695 // make any visible change if the the thickness is getting smaller.
7696 int row
= m_currentCellCoords
.GetRow();
7697 int col
= m_currentCellCoords
.GetCol();
7698 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7700 wxRect rect
= CellToRect(row
, col
);
7701 m_gridWin
->Refresh(TRUE
, &rect
);
7705 void wxGrid::SetCellHighlightROPenWidth(int width
)
7707 if (m_cellHighlightROPenWidth
!= width
) {
7708 m_cellHighlightROPenWidth
= width
;
7710 // Just redrawing the cell highlight is not enough since that won't
7711 // make any visible change if the the thickness is getting smaller.
7712 int row
= m_currentCellCoords
.GetRow();
7713 int col
= m_currentCellCoords
.GetCol();
7714 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7716 wxRect rect
= CellToRect(row
, col
);
7717 m_gridWin
->Refresh(TRUE
, &rect
);
7721 void wxGrid::EnableGridLines( bool enable
)
7723 if ( enable
!= m_gridLinesEnabled
)
7725 m_gridLinesEnabled
= enable
;
7727 if ( !GetBatchCount() )
7731 wxClientDC
dc( m_gridWin
);
7733 DrawAllGridLines( dc
, wxRegion() );
7737 m_gridWin
->Refresh();
7744 int wxGrid::GetDefaultRowSize()
7746 return m_defaultRowHeight
;
7749 int wxGrid::GetRowSize( int row
)
7751 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7753 return GetRowHeight(row
);
7756 int wxGrid::GetDefaultColSize()
7758 return m_defaultColWidth
;
7761 int wxGrid::GetColSize( int col
)
7763 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7765 return GetColWidth(col
);
7768 // ============================================================================
7769 // access to the grid attributes: each of them has a default value in the grid
7770 // itself and may be overidden on a per-cell basis
7771 // ============================================================================
7773 // ----------------------------------------------------------------------------
7774 // setting default attributes
7775 // ----------------------------------------------------------------------------
7777 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7779 m_defaultCellAttr
->SetBackgroundColour(col
);
7781 m_gridWin
->SetBackgroundColour(col
);
7785 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7787 m_defaultCellAttr
->SetTextColour(col
);
7790 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7792 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7795 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7797 m_defaultCellAttr
->SetFont(font
);
7800 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7802 m_defaultCellAttr
->SetRenderer(renderer
);
7805 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7807 m_defaultCellAttr
->SetEditor(editor
);
7810 // ----------------------------------------------------------------------------
7811 // access to the default attrbiutes
7812 // ----------------------------------------------------------------------------
7814 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7816 return m_defaultCellAttr
->GetBackgroundColour();
7819 wxColour
wxGrid::GetDefaultCellTextColour()
7821 return m_defaultCellAttr
->GetTextColour();
7824 wxFont
wxGrid::GetDefaultCellFont()
7826 return m_defaultCellAttr
->GetFont();
7829 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7831 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7834 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7836 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7839 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7841 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7844 // ----------------------------------------------------------------------------
7845 // access to cell attributes
7846 // ----------------------------------------------------------------------------
7848 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7850 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7851 wxColour colour
= attr
->GetBackgroundColour();
7856 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7858 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7859 wxColour colour
= attr
->GetTextColour();
7864 wxFont
wxGrid::GetCellFont( int row
, int col
)
7866 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7867 wxFont font
= attr
->GetFont();
7872 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7874 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7875 attr
->GetAlignment(horiz
, vert
);
7879 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7881 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7882 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7888 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7890 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7891 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7897 bool wxGrid::IsReadOnly(int row
, int col
) const
7899 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7900 bool isReadOnly
= attr
->IsReadOnly();
7905 // ----------------------------------------------------------------------------
7906 // attribute support: cache, automatic provider creation, ...
7907 // ----------------------------------------------------------------------------
7909 bool wxGrid::CanHaveAttributes()
7916 return m_table
->CanHaveAttributes();
7919 void wxGrid::ClearAttrCache()
7921 if ( m_attrCache
.row
!= -1 )
7923 wxSafeDecRef(m_attrCache
.attr
);
7924 m_attrCache
.attr
= NULL
;
7925 m_attrCache
.row
= -1;
7929 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7931 wxGrid
*self
= (wxGrid
*)this; // const_cast
7933 self
->ClearAttrCache();
7934 self
->m_attrCache
.row
= row
;
7935 self
->m_attrCache
.col
= col
;
7936 self
->m_attrCache
.attr
= attr
;
7940 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7942 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7944 *attr
= m_attrCache
.attr
;
7945 wxSafeIncRef(m_attrCache
.attr
);
7947 #ifdef DEBUG_ATTR_CACHE
7948 gs_nAttrCacheHits
++;
7955 #ifdef DEBUG_ATTR_CACHE
7956 gs_nAttrCacheMisses
++;
7962 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7964 wxGridCellAttr
*attr
;
7965 if ( !LookupAttr(row
, col
, &attr
) )
7967 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
7968 CacheAttr(row
, col
, attr
);
7972 attr
->SetDefAttr(m_defaultCellAttr
);
7976 attr
= m_defaultCellAttr
;
7983 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7985 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
7986 wxASSERT_MSG( m_table
,
7987 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7989 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7992 attr
= new wxGridCellAttr
;
7994 // artificially inc the ref count to match DecRef() in caller
7996 m_table
->SetAttr(attr
, row
, col
);
7998 attr
->SetDefAttr(m_defaultCellAttr
);
8002 // ----------------------------------------------------------------------------
8003 // setting column attributes (wrappers around SetColAttr)
8004 // ----------------------------------------------------------------------------
8006 void wxGrid::SetColFormatBool(int col
)
8008 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8011 void wxGrid::SetColFormatNumber(int col
)
8013 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8016 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8018 wxString typeName
= wxGRID_VALUE_FLOAT
;
8019 if ( (width
!= -1) || (precision
!= -1) )
8021 typeName
<< _T(':') << width
<< _T(',') << precision
;
8024 SetColFormatCustom(col
, typeName
);
8027 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8029 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8031 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8033 attr
= new wxGridCellAttr
;
8034 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8035 attr
->SetRenderer(renderer
);
8037 SetColAttr(col
, attr
);
8041 // ----------------------------------------------------------------------------
8042 // setting cell attributes: this is forwarded to the table
8043 // ----------------------------------------------------------------------------
8045 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8047 if ( CanHaveAttributes() )
8049 m_table
->SetRowAttr(attr
, row
);
8058 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8060 if ( CanHaveAttributes() )
8062 m_table
->SetColAttr(attr
, col
);
8071 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8073 if ( CanHaveAttributes() )
8075 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8076 attr
->SetBackgroundColour(colour
);
8081 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8083 if ( CanHaveAttributes() )
8085 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8086 attr
->SetTextColour(colour
);
8091 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8093 if ( CanHaveAttributes() )
8095 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8096 attr
->SetFont(font
);
8101 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8103 if ( CanHaveAttributes() )
8105 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8106 attr
->SetAlignment(horiz
, vert
);
8111 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8113 if ( CanHaveAttributes() )
8115 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8116 attr
->SetRenderer(renderer
);
8121 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8123 if ( CanHaveAttributes() )
8125 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8126 attr
->SetEditor(editor
);
8131 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8133 if ( CanHaveAttributes() )
8135 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8136 attr
->SetReadOnly(isReadOnly
);
8141 // ----------------------------------------------------------------------------
8142 // Data type registration
8143 // ----------------------------------------------------------------------------
8145 void wxGrid::RegisterDataType(const wxString
& typeName
,
8146 wxGridCellRenderer
* renderer
,
8147 wxGridCellEditor
* editor
)
8149 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8153 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8155 wxString typeName
= m_table
->GetTypeName(row
, col
);
8156 return GetDefaultEditorForType(typeName
);
8159 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8161 wxString typeName
= m_table
->GetTypeName(row
, col
);
8162 return GetDefaultRendererForType(typeName
);
8166 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8168 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8169 if ( index
== wxNOT_FOUND
)
8171 wxFAIL_MSG(wxT("Unknown data type name"));
8176 return m_typeRegistry
->GetEditor(index
);
8180 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8182 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8183 if ( index
== wxNOT_FOUND
)
8185 wxFAIL_MSG(wxT("Unknown data type name"));
8190 return m_typeRegistry
->GetRenderer(index
);
8194 // ----------------------------------------------------------------------------
8196 // ----------------------------------------------------------------------------
8198 void wxGrid::EnableDragRowSize( bool enable
)
8200 m_canDragRowSize
= enable
;
8204 void wxGrid::EnableDragColSize( bool enable
)
8206 m_canDragColSize
= enable
;
8209 void wxGrid::EnableDragGridSize( bool enable
)
8211 m_canDragGridSize
= enable
;
8215 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8217 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8219 if ( resizeExistingRows
)
8222 if ( !GetBatchCount() )
8227 void wxGrid::SetRowSize( int row
, int height
)
8229 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8231 if ( m_rowHeights
.IsEmpty() )
8233 // need to really create the array
8237 int h
= wxMax( 0, height
);
8238 int diff
= h
- m_rowHeights
[row
];
8240 m_rowHeights
[row
] = h
;
8242 for ( i
= row
; i
< m_numRows
; i
++ )
8244 m_rowBottoms
[i
] += diff
;
8246 if ( !GetBatchCount() )
8250 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8252 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8254 if ( resizeExistingCols
)
8257 if ( !GetBatchCount() )
8262 void wxGrid::SetColSize( int col
, int width
)
8264 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8266 // should we check that it's bigger than GetColMinimalWidth(col) here?
8268 if ( m_colWidths
.IsEmpty() )
8270 // need to really create the array
8274 int w
= wxMax( 0, width
);
8275 int diff
= w
- m_colWidths
[col
];
8276 m_colWidths
[col
] = w
;
8279 for ( i
= col
; i
< m_numCols
; i
++ )
8281 m_colRights
[i
] += diff
;
8283 if ( !GetBatchCount() )
8288 void wxGrid::SetColMinimalWidth( int col
, int width
)
8290 m_colMinWidths
.Put(col
, width
);
8293 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8295 m_rowMinHeights
.Put(row
, width
);
8298 int wxGrid::GetColMinimalWidth(int col
) const
8300 long value
= m_colMinWidths
.Get(col
);
8301 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8304 int wxGrid::GetRowMinimalHeight(int row
) const
8306 long value
= m_rowMinHeights
.Get(row
);
8307 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8310 // ----------------------------------------------------------------------------
8312 // ----------------------------------------------------------------------------
8314 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8316 wxClientDC
dc(m_gridWin
);
8318 // init both of them to avoid compiler warnings, even if weo nly need one
8326 wxCoord extent
, extentMax
= 0;
8327 int max
= column
? m_numRows
: m_numCols
;
8328 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8335 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8336 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8339 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8340 extent
= column
? size
.x
: size
.y
;
8341 if ( extent
> extentMax
)
8352 // now also compare with the column label extent
8354 dc
.SetFont( GetLabelFont() );
8357 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8359 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8361 extent
= column
? w
: h
;
8362 if ( extent
> extentMax
)
8369 // empty column - give default extent (notice that if extentMax is less
8370 // than default extent but != 0, it's ok)
8371 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8377 // leave some space around text
8387 SetColSize(col
, extentMax
);
8388 if ( !GetBatchCount() )
8391 m_gridWin
->GetClientSize( &cw
, &ch
);
8392 wxRect
rect ( CellToRect( 0, col
) );
8394 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8395 rect
.width
= cw
- rect
.x
;
8396 rect
.height
= m_colLabelHeight
;
8397 m_colLabelWin
->Refresh( TRUE
, &rect
);
8401 SetRowSize(row
, extentMax
);
8402 if ( !GetBatchCount() )
8405 m_gridWin
->GetClientSize( &cw
, &ch
);
8406 wxRect
rect ( CellToRect( row
, 0 ) );
8408 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8409 rect
.width
= m_rowLabelWidth
;
8410 rect
.height
= ch
- rect
.y
;
8411 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8417 SetColMinimalWidth(col
, extentMax
);
8419 SetRowMinimalHeight(row
, extentMax
);
8423 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8425 int width
= m_rowLabelWidth
;
8429 for ( int col
= 0; col
< m_numCols
; col
++ )
8433 AutoSizeColumn(col
, setAsMin
);
8436 width
+= GetColWidth(col
);
8443 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8445 int height
= m_colLabelHeight
;
8449 for ( int row
= 0; row
< m_numRows
; row
++ )
8453 AutoSizeRow(row
, setAsMin
);
8456 height
+= GetRowHeight(row
);
8463 void wxGrid::AutoSize()
8466 SetClientSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8469 wxSize
wxGrid::DoGetBestSize() const
8471 // don't set sizes, only calculate them
8472 wxGrid
*self
= (wxGrid
*)this; // const_cast
8474 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8475 self
->SetOrCalcRowSizes(TRUE
));
8484 wxPen
& wxGrid::GetDividerPen() const
8489 // ----------------------------------------------------------------------------
8490 // cell value accessor functions
8491 // ----------------------------------------------------------------------------
8493 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8497 m_table
->SetValue( row
, col
, s
);
8498 if ( !GetBatchCount() )
8500 wxClientDC
dc( m_gridWin
);
8502 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8505 if ( m_currentCellCoords
.GetRow() == row
&&
8506 m_currentCellCoords
.GetCol() == col
&&
8507 IsCellEditControlShown())
8508 // Note: If we are using IsCellEditControlEnabled,
8509 // this interacts badly with calling SetCellValue from
8510 // an EVT_GRID_CELL_CHANGE handler.
8512 HideCellEditControl();
8513 ShowCellEditControl(); // will reread data from table
8520 // ------ Block, row and col selection
8523 void wxGrid::SelectRow( int row
, bool addToSelected
)
8525 if ( IsSelection() && !addToSelected
)
8528 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8532 void wxGrid::SelectCol( int col
, bool addToSelected
)
8534 if ( IsSelection() && !addToSelected
)
8537 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8541 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8542 bool addToSelected
)
8544 if ( IsSelection() && !addToSelected
)
8547 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8548 FALSE
, addToSelected
);
8552 void wxGrid::SelectAll()
8554 if ( m_numRows
> 0 && m_numCols
> 0 )
8555 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8559 // ------ Cell, row and col deselection
8562 void wxGrid::DeselectRow( int row
)
8564 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8566 if ( m_selection
->IsInSelection(row
, 0 ) )
8567 m_selection
->ToggleCellSelection( row
, 0);
8571 int nCols
= GetNumberCols();
8572 for ( int i
= 0; i
< nCols
; i
++ )
8574 if ( m_selection
->IsInSelection(row
, i
) )
8575 m_selection
->ToggleCellSelection( row
, i
);
8580 void wxGrid::DeselectCol( int col
)
8582 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8584 if ( m_selection
->IsInSelection(0, col
) )
8585 m_selection
->ToggleCellSelection( 0, col
);
8589 int nRows
= GetNumberRows();
8590 for ( int i
= 0; i
< nRows
; i
++ )
8592 if ( m_selection
->IsInSelection(i
, col
) )
8593 m_selection
->ToggleCellSelection(i
, col
);
8598 void wxGrid::DeselectCell( int row
, int col
)
8600 if ( m_selection
->IsInSelection(row
, col
) )
8601 m_selection
->ToggleCellSelection(row
, col
);
8604 bool wxGrid::IsSelection()
8606 return ( m_selection
->IsSelection() ||
8607 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8608 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8611 bool wxGrid::IsInSelection( int row
, int col
)
8613 return ( m_selection
->IsInSelection( row
, col
) ||
8614 ( row
>= m_selectingTopLeft
.GetRow() &&
8615 col
>= m_selectingTopLeft
.GetCol() &&
8616 row
<= m_selectingBottomRight
.GetRow() &&
8617 col
<= m_selectingBottomRight
.GetCol() ) );
8620 void wxGrid::ClearSelection()
8622 m_selectingTopLeft
= wxGridNoCellCoords
;
8623 m_selectingBottomRight
= wxGridNoCellCoords
;
8624 m_selection
->ClearSelection();
8628 // This function returns the rectangle that encloses the given block
8629 // in device coords clipped to the client size of the grid window.
8631 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8632 const wxGridCellCoords
&bottomRight
)
8634 wxRect
rect( wxGridNoCellRect
);
8637 cellRect
= CellToRect( topLeft
);
8638 if ( cellRect
!= wxGridNoCellRect
)
8644 rect
= wxRect( 0, 0, 0, 0 );
8647 cellRect
= CellToRect( bottomRight
);
8648 if ( cellRect
!= wxGridNoCellRect
)
8654 return wxGridNoCellRect
;
8657 // convert to scrolled coords
8659 int left
, top
, right
, bottom
;
8660 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8661 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8664 m_gridWin
->GetClientSize( &cw
, &ch
);
8666 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8667 return wxRect( 0, 0, 0, 0);
8669 rect
.SetLeft( wxMax(0, left
) );
8670 rect
.SetTop( wxMax(0, top
) );
8671 rect
.SetRight( wxMin(cw
, right
) );
8672 rect
.SetBottom( wxMin(ch
, bottom
) );
8680 // ------ Grid event classes
8683 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8685 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8686 int row
, int col
, int x
, int y
, bool sel
,
8687 bool control
, bool shift
, bool alt
, bool meta
)
8688 : wxNotifyEvent( type
, id
)
8695 m_control
= control
;
8700 SetEventObject(obj
);
8704 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8706 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8707 int rowOrCol
, int x
, int y
,
8708 bool control
, bool shift
, bool alt
, bool meta
)
8709 : wxNotifyEvent( type
, id
)
8711 m_rowOrCol
= rowOrCol
;
8714 m_control
= control
;
8719 SetEventObject(obj
);
8723 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8725 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8726 const wxGridCellCoords
& topLeft
,
8727 const wxGridCellCoords
& bottomRight
,
8728 bool sel
, bool control
,
8729 bool shift
, bool alt
, bool meta
)
8730 : wxNotifyEvent( type
, id
)
8732 m_topLeft
= topLeft
;
8733 m_bottomRight
= bottomRight
;
8735 m_control
= control
;
8740 SetEventObject(obj
);
8744 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8746 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8747 wxObject
* obj
, int row
,
8748 int col
, wxControl
* ctrl
)
8749 : wxCommandEvent(type
, id
)
8751 SetEventObject(obj
);
8758 #endif // ifndef wxUSE_NEW_GRID