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
)
3507 wxSafeDecRef(m_defaultCellAttr
);
3509 #ifdef DEBUG_ATTR_CACHE
3510 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3511 wxPrintf(_T("wxGrid attribute cache statistics: "
3512 "total: %u, hits: %u (%u%%)\n"),
3513 total
, gs_nAttrCacheHits
,
3514 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3520 delete m_typeRegistry
;
3526 // ----- internal init and update functions
3529 void wxGrid::Create()
3531 m_created
= FALSE
; // set to TRUE by CreateGrid
3533 m_table
= (wxGridTableBase
*) NULL
;
3536 m_cellEditCtrlEnabled
= FALSE
;
3538 m_defaultCellAttr
= new wxGridCellAttr
;
3539 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3541 // Set default cell attributes
3542 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3543 m_defaultCellAttr
->SetFont(GetFont());
3544 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3545 m_defaultCellAttr
->SetTextColour(
3546 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3547 m_defaultCellAttr
->SetBackgroundColour(
3548 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3549 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3550 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3555 m_currentCellCoords
= wxGridNoCellCoords
;
3557 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3558 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3560 // create the type registry
3561 m_typeRegistry
= new wxGridTypeRegistry
;
3563 // subwindow components that make up the wxGrid
3564 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3569 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3574 m_colLabelWin
= new wxGridColLabelWindow( this,
3579 m_gridWin
= new wxGridWindow( this,
3586 SetTargetWindow( m_gridWin
);
3590 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3591 wxGrid::wxGridSelectionModes selmode
)
3593 wxCHECK_MSG( !m_created
,
3595 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3597 m_numRows
= numRows
;
3598 m_numCols
= numCols
;
3600 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3601 m_table
->SetView( this );
3603 m_selection
= new wxGridSelection( this, selmode
);
3610 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3614 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3617 m_selection
->SetSelectionMode( selmode
);
3620 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3621 wxGrid::wxGridSelectionModes selmode
)
3625 // RD: Actually, this should probably be allowed. I think it would be
3626 // nice to be able to switch multiple Tables in and out of a single
3627 // View at runtime. Is there anything in the implmentation that would
3630 // At least, you now have to cope with m_selection
3631 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3636 m_numRows
= table
->GetNumberRows();
3637 m_numCols
= table
->GetNumberCols();
3640 m_table
->SetView( this );
3643 m_selection
= new wxGridSelection( this, selmode
);
3654 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3655 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3657 if ( m_rowLabelWin
)
3659 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3663 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3666 m_labelTextColour
= wxColour( _T("BLACK") );
3669 m_attrCache
.row
= -1;
3671 // TODO: something better than this ?
3673 m_labelFont
= this->GetFont();
3674 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3676 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3677 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3679 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3680 m_colLabelVertAlign
= wxALIGN_TOP
;
3682 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3683 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3685 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3686 m_defaultRowHeight
+= 8;
3688 m_defaultRowHeight
+= 4;
3691 m_gridLineColour
= wxColour( 128, 128, 255 );
3692 m_gridLinesEnabled
= TRUE
;
3693 m_cellHighlightColour
= m_gridLineColour
;
3694 m_cellHighlightPenWidth
= 2;
3695 m_cellHighlightROPenWidth
= 1;
3697 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3698 m_winCapture
= (wxWindow
*)NULL
;
3699 m_canDragRowSize
= TRUE
;
3700 m_canDragColSize
= TRUE
;
3701 m_canDragGridSize
= TRUE
;
3703 m_dragRowOrCol
= -1;
3704 m_isDragging
= FALSE
;
3705 m_startDragPos
= wxDefaultPosition
;
3707 m_waitForSlowClick
= FALSE
;
3709 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3710 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3712 m_currentCellCoords
= wxGridNoCellCoords
;
3714 m_selectingTopLeft
= wxGridNoCellCoords
;
3715 m_selectingBottomRight
= wxGridNoCellCoords
;
3716 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3717 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3719 m_editable
= TRUE
; // default for whole grid
3721 m_inOnKeyDown
= FALSE
;
3730 // ----------------------------------------------------------------------------
3731 // the idea is to call these functions only when necessary because they create
3732 // quite big arrays which eat memory mostly unnecessary - in particular, if
3733 // default widths/heights are used for all rows/columns, we may not use these
3736 // with some extra code, it should be possible to only store the
3737 // widths/heights different from default ones but this will be done later...
3738 // ----------------------------------------------------------------------------
3740 void wxGrid::InitRowHeights()
3742 m_rowHeights
.Empty();
3743 m_rowBottoms
.Empty();
3745 m_rowHeights
.Alloc( m_numRows
);
3746 m_rowBottoms
.Alloc( m_numRows
);
3749 for ( int i
= 0; i
< m_numRows
; i
++ )
3751 m_rowHeights
.Add( m_defaultRowHeight
);
3752 rowBottom
+= m_defaultRowHeight
;
3753 m_rowBottoms
.Add( rowBottom
);
3757 void wxGrid::InitColWidths()
3759 m_colWidths
.Empty();
3760 m_colRights
.Empty();
3762 m_colWidths
.Alloc( m_numCols
);
3763 m_colRights
.Alloc( m_numCols
);
3765 for ( int i
= 0; i
< m_numCols
; i
++ )
3767 m_colWidths
.Add( m_defaultColWidth
);
3768 colRight
+= m_defaultColWidth
;
3769 m_colRights
.Add( colRight
);
3773 int wxGrid::GetColWidth(int col
) const
3775 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3778 int wxGrid::GetColLeft(int col
) const
3780 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3781 : m_colRights
[col
] - m_colWidths
[col
];
3784 int wxGrid::GetColRight(int col
) const
3786 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3790 int wxGrid::GetRowHeight(int row
) const
3792 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3795 int wxGrid::GetRowTop(int row
) const
3797 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3798 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3801 int wxGrid::GetRowBottom(int row
) const
3803 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3804 : m_rowBottoms
[row
];
3807 void wxGrid::CalcDimensions()
3810 GetClientSize( &cw
, &ch
);
3812 if ( m_colLabelWin
->IsShown() )
3813 cw
-= m_rowLabelWidth
;
3814 if ( m_rowLabelWin
->IsShown() )
3815 ch
-= m_colLabelHeight
;
3818 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3819 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3821 // preserve (more or less) the previous position
3823 GetViewStart( &x
, &y
);
3824 // maybe we don't need scrollbars at all? and if we do, transform w and h
3825 // from pixels into logical units
3832 w
= (w
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3842 h
= (h
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3847 // do set scrollbar parameters
3848 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3849 w
, h
, x
, y
, (GetBatchCount() != 0));
3853 void wxGrid::CalcWindowSizes()
3856 GetClientSize( &cw
, &ch
);
3858 if ( m_cornerLabelWin
->IsShown() )
3859 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3861 if ( m_colLabelWin
->IsShown() )
3862 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3864 if ( m_rowLabelWin
->IsShown() )
3865 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3867 if ( m_gridWin
->IsShown() )
3868 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3872 // this is called when the grid table sends a message to say that it
3873 // has been redimensioned
3875 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3878 bool result
= FALSE
;
3881 // if we were using the default widths/heights so far, we must change them
3883 if ( m_colWidths
.IsEmpty() )
3888 if ( m_rowHeights
.IsEmpty() )
3894 switch ( msg
.GetId() )
3896 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3898 size_t pos
= msg
.GetCommandInt();
3899 int numRows
= msg
.GetCommandInt2();
3901 m_numRows
+= numRows
;
3903 if ( !m_rowHeights
.IsEmpty() )
3905 for ( i
= 0; i
< numRows
; i
++ )
3907 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3908 m_rowBottoms
.Insert( 0, pos
);
3912 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3914 for ( i
= pos
; i
< m_numRows
; i
++ )
3916 bottom
+= m_rowHeights
[i
];
3917 m_rowBottoms
[i
] = bottom
;
3920 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3922 // if we have just inserted cols into an empty grid the current
3923 // cell will be undefined...
3925 SetCurrentCell( 0, 0 );
3927 m_selection
->UpdateRows( pos
, numRows
);
3928 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3930 attrProvider
->UpdateAttrRows( pos
, numRows
);
3932 if ( !GetBatchCount() )
3935 m_rowLabelWin
->Refresh();
3941 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3943 int numRows
= msg
.GetCommandInt();
3944 int oldNumRows
= m_numRows
;
3945 m_numRows
+= numRows
;
3947 if ( !m_rowHeights
.IsEmpty() )
3949 for ( i
= 0; i
< numRows
; i
++ )
3951 m_rowHeights
.Add( m_defaultRowHeight
);
3952 m_rowBottoms
.Add( 0 );
3956 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3958 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3960 bottom
+= m_rowHeights
[i
];
3961 m_rowBottoms
[i
] = bottom
;
3964 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3966 // if we have just inserted cols into an empty grid the current
3967 // cell will be undefined...
3969 SetCurrentCell( 0, 0 );
3971 if ( !GetBatchCount() )
3974 m_rowLabelWin
->Refresh();
3980 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3982 size_t pos
= msg
.GetCommandInt();
3983 int numRows
= msg
.GetCommandInt2();
3984 m_numRows
-= numRows
;
3986 if ( !m_rowHeights
.IsEmpty() )
3988 for ( i
= 0; i
< numRows
; i
++ )
3990 m_rowHeights
.Remove( pos
);
3991 m_rowBottoms
.Remove( pos
);
3995 for ( i
= 0; i
< m_numRows
; i
++ )
3997 h
+= m_rowHeights
[i
];
3998 m_rowBottoms
[i
] = h
;
4003 m_currentCellCoords
= wxGridNoCellCoords
;
4007 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4008 m_currentCellCoords
.Set( 0, 0 );
4010 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4011 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4013 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4014 // ifdef'd out following patch from Paul Gammans
4016 // No need to touch column attributes, unless we
4017 // removed _all_ rows, in this case, we remove
4018 // all column attributes.
4019 // I hate to do this here, but the
4020 // needed data is not available inside UpdateAttrRows.
4021 if ( !GetNumberRows() )
4022 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4025 if ( !GetBatchCount() )
4028 m_rowLabelWin
->Refresh();
4034 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4036 size_t pos
= msg
.GetCommandInt();
4037 int numCols
= msg
.GetCommandInt2();
4038 m_numCols
+= numCols
;
4040 if ( !m_colWidths
.IsEmpty() )
4042 for ( i
= 0; i
< numCols
; i
++ )
4044 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4045 m_colRights
.Insert( 0, pos
);
4049 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4051 for ( i
= pos
; i
< m_numCols
; i
++ )
4053 right
+= m_colWidths
[i
];
4054 m_colRights
[i
] = right
;
4057 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4059 // if we have just inserted cols into an empty grid the current
4060 // cell will be undefined...
4062 SetCurrentCell( 0, 0 );
4064 m_selection
->UpdateCols( pos
, numCols
);
4065 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4067 attrProvider
->UpdateAttrCols( pos
, numCols
);
4068 if ( !GetBatchCount() )
4071 m_colLabelWin
->Refresh();
4078 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4080 int numCols
= msg
.GetCommandInt();
4081 int oldNumCols
= m_numCols
;
4082 m_numCols
+= numCols
;
4083 if ( !m_colWidths
.IsEmpty() )
4085 for ( i
= 0; i
< numCols
; i
++ )
4087 m_colWidths
.Add( m_defaultColWidth
);
4088 m_colRights
.Add( 0 );
4092 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4094 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4096 right
+= m_colWidths
[i
];
4097 m_colRights
[i
] = right
;
4100 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4102 // if we have just inserted cols into an empty grid the current
4103 // cell will be undefined...
4105 SetCurrentCell( 0, 0 );
4107 if ( !GetBatchCount() )
4110 m_colLabelWin
->Refresh();
4116 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4118 size_t pos
= msg
.GetCommandInt();
4119 int numCols
= msg
.GetCommandInt2();
4120 m_numCols
-= numCols
;
4122 if ( !m_colWidths
.IsEmpty() )
4124 for ( i
= 0; i
< numCols
; i
++ )
4126 m_colWidths
.Remove( pos
);
4127 m_colRights
.Remove( pos
);
4131 for ( i
= 0; i
< m_numCols
; i
++ )
4133 w
+= m_colWidths
[i
];
4139 m_currentCellCoords
= wxGridNoCellCoords
;
4143 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4144 m_currentCellCoords
.Set( 0, 0 );
4146 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4147 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4149 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4150 // ifdef'd out following patch from Paul Gammans
4152 // No need to touch row attributes, unless we
4153 // removed _all_ columns, in this case, we remove
4154 // all row attributes.
4155 // I hate to do this here, but the
4156 // needed data is not available inside UpdateAttrCols.
4157 if ( !GetNumberCols() )
4158 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4161 if ( !GetBatchCount() )
4164 m_colLabelWin
->Refresh();
4171 if (result
&& !GetBatchCount() )
4172 m_gridWin
->Refresh();
4177 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4179 wxRegionIterator
iter( reg
);
4182 wxArrayInt rowlabels
;
4189 // TODO: remove this when we can...
4190 // There is a bug in wxMotif that gives garbage update
4191 // rectangles if you jump-scroll a long way by clicking the
4192 // scrollbar with middle button. This is a work-around
4194 #if defined(__WXMOTIF__)
4196 m_gridWin
->GetClientSize( &cw
, &ch
);
4197 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4198 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4201 // logical bounds of update region
4204 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4205 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4207 // find the row labels within these bounds
4210 for ( row
= 0; row
< m_numRows
; row
++ )
4212 if ( GetRowBottom(row
) < top
)
4215 if ( GetRowTop(row
) > bottom
)
4218 rowlabels
.Add( row
);
4228 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4230 wxRegionIterator
iter( reg
);
4233 wxArrayInt colLabels
;
4240 // TODO: remove this when we can...
4241 // There is a bug in wxMotif that gives garbage update
4242 // rectangles if you jump-scroll a long way by clicking the
4243 // scrollbar with middle button. This is a work-around
4245 #if defined(__WXMOTIF__)
4247 m_gridWin
->GetClientSize( &cw
, &ch
);
4248 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4249 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4252 // logical bounds of update region
4255 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4256 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4258 // find the cells within these bounds
4261 for ( col
= 0; col
< m_numCols
; col
++ )
4263 if ( GetColRight(col
) < left
)
4266 if ( GetColLeft(col
) > right
)
4269 colLabels
.Add( col
);
4278 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4280 wxRegionIterator
iter( reg
);
4283 wxGridCellCoordsArray cellsExposed
;
4285 int left
, top
, right
, bottom
;
4290 // TODO: remove this when we can...
4291 // There is a bug in wxMotif that gives garbage update
4292 // rectangles if you jump-scroll a long way by clicking the
4293 // scrollbar with middle button. This is a work-around
4295 #if defined(__WXMOTIF__)
4297 m_gridWin
->GetClientSize( &cw
, &ch
);
4298 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4299 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4300 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4301 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4304 // logical bounds of update region
4306 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4307 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4309 // find the cells within these bounds
4312 for ( row
= 0; row
< m_numRows
; row
++ )
4314 if ( GetRowBottom(row
) <= top
)
4317 if ( GetRowTop(row
) > bottom
)
4321 for ( col
= 0; col
< m_numCols
; col
++ )
4323 if ( GetColRight(col
) <= left
)
4326 if ( GetColLeft(col
) > right
)
4329 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4336 return cellsExposed
;
4340 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4343 wxPoint
pos( event
.GetPosition() );
4344 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4346 if ( event
.Dragging() )
4348 m_isDragging
= TRUE
;
4350 if ( event
.LeftIsDown() )
4352 switch( m_cursorMode
)
4354 case WXGRID_CURSOR_RESIZE_ROW
:
4356 int cw
, ch
, left
, dummy
;
4357 m_gridWin
->GetClientSize( &cw
, &ch
);
4358 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4360 wxClientDC
dc( m_gridWin
);
4363 GetRowTop(m_dragRowOrCol
) +
4364 GetRowMinimalHeight(m_dragRowOrCol
) );
4365 dc
.SetLogicalFunction(wxINVERT
);
4366 if ( m_dragLastPos
>= 0 )
4368 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4370 dc
.DrawLine( left
, y
, left
+cw
, y
);
4375 case WXGRID_CURSOR_SELECT_ROW
:
4376 if ( (row
= YToRow( y
)) >= 0 )
4378 m_selection
->SelectRow( row
,
4379 event
.ControlDown(),
4385 // default label to suppress warnings about "enumeration value
4386 // 'xxx' not handled in switch
4394 m_isDragging
= FALSE
;
4397 // ------------ Entering or leaving the window
4399 if ( event
.Entering() || event
.Leaving() )
4401 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4405 // ------------ Left button pressed
4407 else if ( event
.LeftDown() )
4409 // don't send a label click event for a hit on the
4410 // edge of the row label - this is probably the user
4411 // wanting to resize the row
4413 if ( YToEdgeOfRow(y
) < 0 )
4417 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4419 if ( !event
.ShiftDown() && !event
.ControlDown() )
4421 if ( event
.ShiftDown() )
4422 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4425 GetNumberCols() - 1,
4426 event
.ControlDown(),
4431 m_selection
->SelectRow( row
,
4432 event
.ControlDown(),
4436 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4441 // starting to drag-resize a row
4443 if ( CanDragRowSize() )
4444 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4449 // ------------ Left double click
4451 else if (event
.LeftDClick() )
4453 if ( YToEdgeOfRow(y
) < 0 )
4456 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4461 // ------------ Left button released
4463 else if ( event
.LeftUp() )
4465 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4467 DoEndDragResizeRow();
4469 // Note: we are ending the event *after* doing
4470 // default processing in this case
4472 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4475 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4480 // ------------ Right button down
4482 else if ( event
.RightDown() )
4485 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4487 // no default action at the moment
4492 // ------------ Right double click
4494 else if ( event
.RightDClick() )
4497 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4499 // no default action at the moment
4504 // ------------ No buttons down and mouse moving
4506 else if ( event
.Moving() )
4508 m_dragRowOrCol
= YToEdgeOfRow( y
);
4509 if ( m_dragRowOrCol
>= 0 )
4511 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4513 // don't capture the mouse yet
4514 if ( CanDragRowSize() )
4515 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4518 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4520 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4526 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4529 wxPoint
pos( event
.GetPosition() );
4530 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4532 if ( event
.Dragging() )
4534 m_isDragging
= TRUE
;
4536 if ( event
.LeftIsDown() )
4538 switch( m_cursorMode
)
4540 case WXGRID_CURSOR_RESIZE_COL
:
4542 int cw
, ch
, dummy
, top
;
4543 m_gridWin
->GetClientSize( &cw
, &ch
);
4544 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4546 wxClientDC
dc( m_gridWin
);
4549 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4550 GetColMinimalWidth(m_dragRowOrCol
));
4551 dc
.SetLogicalFunction(wxINVERT
);
4552 if ( m_dragLastPos
>= 0 )
4554 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4556 dc
.DrawLine( x
, top
, x
, top
+ch
);
4561 case WXGRID_CURSOR_SELECT_COL
:
4562 if ( (col
= XToCol( x
)) >= 0 )
4564 m_selection
->SelectCol( col
,
4565 event
.ControlDown(),
4571 // default label to suppress warnings about "enumeration value
4572 // 'xxx' not handled in switch
4580 m_isDragging
= FALSE
;
4583 // ------------ Entering or leaving the window
4585 if ( event
.Entering() || event
.Leaving() )
4587 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4591 // ------------ Left button pressed
4593 else if ( event
.LeftDown() )
4595 // don't send a label click event for a hit on the
4596 // edge of the col label - this is probably the user
4597 // wanting to resize the col
4599 if ( XToEdgeOfCol(x
) < 0 )
4603 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4605 if ( !event
.ShiftDown() && !event
.ControlDown() )
4607 if ( event
.ShiftDown() )
4608 m_selection
->SelectBlock( 0,
4609 m_currentCellCoords
.GetCol(),
4610 GetNumberRows() - 1, col
,
4611 event
.ControlDown(),
4616 m_selection
->SelectCol( col
,
4617 event
.ControlDown(),
4621 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4626 // starting to drag-resize a col
4628 if ( CanDragColSize() )
4629 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4634 // ------------ Left double click
4636 if ( event
.LeftDClick() )
4638 if ( XToEdgeOfCol(x
) < 0 )
4641 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4646 // ------------ Left button released
4648 else if ( event
.LeftUp() )
4650 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4652 DoEndDragResizeCol();
4654 // Note: we are ending the event *after* doing
4655 // default processing in this case
4657 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4660 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4665 // ------------ Right button down
4667 else if ( event
.RightDown() )
4670 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4672 // no default action at the moment
4677 // ------------ Right double click
4679 else if ( event
.RightDClick() )
4682 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4684 // no default action at the moment
4689 // ------------ No buttons down and mouse moving
4691 else if ( event
.Moving() )
4693 m_dragRowOrCol
= XToEdgeOfCol( x
);
4694 if ( m_dragRowOrCol
>= 0 )
4696 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4698 // don't capture the cursor yet
4699 if ( CanDragColSize() )
4700 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4703 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4705 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4711 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4713 if ( event
.LeftDown() )
4715 // indicate corner label by having both row and
4718 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4724 else if ( event
.LeftDClick() )
4726 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4729 else if ( event
.RightDown() )
4731 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4733 // no default action at the moment
4737 else if ( event
.RightDClick() )
4739 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4741 // no default action at the moment
4746 void wxGrid::ChangeCursorMode(CursorMode mode
,
4751 static const wxChar
*cursorModes
[] =
4760 wxLogTrace(_T("grid"),
4761 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4762 win
== m_colLabelWin
? _T("colLabelWin")
4763 : win
? _T("rowLabelWin")
4765 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4766 #endif // __WXDEBUG__
4768 if ( mode
== m_cursorMode
&&
4769 win
== m_winCapture
&&
4770 captureMouse
== (m_winCapture
!= NULL
))
4775 // by default use the grid itself
4781 m_winCapture
->ReleaseMouse();
4782 m_winCapture
= (wxWindow
*)NULL
;
4785 m_cursorMode
= mode
;
4787 switch ( m_cursorMode
)
4789 case WXGRID_CURSOR_RESIZE_ROW
:
4790 win
->SetCursor( m_rowResizeCursor
);
4793 case WXGRID_CURSOR_RESIZE_COL
:
4794 win
->SetCursor( m_colResizeCursor
);
4798 win
->SetCursor( *wxSTANDARD_CURSOR
);
4801 // we need to capture mouse when resizing
4802 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4803 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4805 if ( captureMouse
&& resize
)
4807 win
->CaptureMouse();
4812 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4815 wxPoint
pos( event
.GetPosition() );
4816 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4818 wxGridCellCoords coords
;
4819 XYToCell( x
, y
, coords
);
4821 if ( event
.Dragging() )
4823 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4825 // Don't start doing anything until the mouse has been drug at
4826 // least 3 pixels in any direction...
4829 if (m_startDragPos
== wxDefaultPosition
)
4831 m_startDragPos
= pos
;
4834 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4838 m_isDragging
= TRUE
;
4839 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4841 // Hide the edit control, so it
4842 // won't interfer with drag-shrinking.
4843 if ( IsCellEditControlShown() )
4845 HideCellEditControl();
4846 SaveEditControlValue();
4849 // Have we captured the mouse yet?
4852 m_winCapture
= m_gridWin
;
4853 m_winCapture
->CaptureMouse();
4856 if ( coords
!= wxGridNoCellCoords
)
4858 if ( event
.ControlDown() )
4860 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4861 m_selectingKeyboard
= coords
;
4862 HighlightBlock ( m_selectingKeyboard
, coords
);
4866 if ( !IsSelection() )
4868 HighlightBlock( coords
, coords
);
4872 HighlightBlock( m_currentCellCoords
, coords
);
4876 if (! IsVisible(coords
))
4878 MakeCellVisible(coords
);
4879 // TODO: need to introduce a delay or something here. The
4880 // scrolling is way to fast, at least on MSW - also on GTK.
4884 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4886 int cw
, ch
, left
, dummy
;
4887 m_gridWin
->GetClientSize( &cw
, &ch
);
4888 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4890 wxClientDC
dc( m_gridWin
);
4892 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4893 GetRowMinimalHeight(m_dragRowOrCol
) );
4894 dc
.SetLogicalFunction(wxINVERT
);
4895 if ( m_dragLastPos
>= 0 )
4897 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4899 dc
.DrawLine( left
, y
, left
+cw
, y
);
4902 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4904 int cw
, ch
, dummy
, top
;
4905 m_gridWin
->GetClientSize( &cw
, &ch
);
4906 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4908 wxClientDC
dc( m_gridWin
);
4910 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4911 GetColMinimalWidth(m_dragRowOrCol
) );
4912 dc
.SetLogicalFunction(wxINVERT
);
4913 if ( m_dragLastPos
>= 0 )
4915 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4917 dc
.DrawLine( x
, top
, x
, top
+ch
);
4924 m_isDragging
= FALSE
;
4925 m_startDragPos
= wxDefaultPosition
;
4927 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4928 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4931 if ( event
.Entering() || event
.Leaving() )
4933 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4934 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4939 // ------------ Left button pressed
4941 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4943 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4948 if ( !event
.ControlDown() )
4950 if ( event
.ShiftDown() )
4952 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4953 m_currentCellCoords
.GetCol(),
4956 event
.ControlDown(),
4961 else if ( XToEdgeOfCol(x
) < 0 &&
4962 YToEdgeOfRow(y
) < 0 )
4964 DisableCellEditControl();
4965 MakeCellVisible( coords
);
4967 // if this is the second click on this cell then start
4969 if ( m_waitForSlowClick
&&
4970 (coords
== m_currentCellCoords
) &&
4971 CanEnableCellControl())
4973 EnableCellEditControl();
4975 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4976 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4979 editor
->StartingClick();
4983 m_waitForSlowClick
= FALSE
;
4987 if ( event
.ControlDown() )
4989 m_selection
->ToggleCellSelection( coords
.GetRow(),
4991 event
.ControlDown(),
4995 m_selectingTopLeft
= wxGridNoCellCoords
;
4996 m_selectingBottomRight
= wxGridNoCellCoords
;
4997 m_selectingKeyboard
= coords
;
5001 SetCurrentCell( coords
);
5002 if ( m_selection
->GetSelectionMode()
5003 != wxGrid::wxGridSelectCells
)
5004 HighlightBlock( coords
, coords
);
5006 m_waitForSlowClick
= TRUE
;
5013 // ------------ Left double click
5015 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5017 DisableCellEditControl();
5019 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5021 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5029 // ------------ Left button released
5031 else if ( event
.LeftUp() )
5033 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5035 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5036 m_selectingBottomRight
!= wxGridNoCellCoords
)
5040 m_winCapture
->ReleaseMouse();
5041 m_winCapture
= NULL
;
5043 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5044 m_selectingTopLeft
.GetCol(),
5045 m_selectingBottomRight
.GetRow(),
5046 m_selectingBottomRight
.GetCol(),
5047 event
.ControlDown(),
5051 m_selectingTopLeft
= wxGridNoCellCoords
;
5052 m_selectingBottomRight
= wxGridNoCellCoords
;
5055 // Show the edit control, if it has been hidden for
5057 ShowCellEditControl();
5059 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5061 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5062 DoEndDragResizeRow();
5064 // Note: we are ending the event *after* doing
5065 // default processing in this case
5067 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5069 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5071 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5072 DoEndDragResizeCol();
5074 // Note: we are ending the event *after* doing
5075 // default processing in this case
5077 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5084 // ------------ Right button down
5086 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5088 DisableCellEditControl();
5089 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5094 // no default action at the moment
5099 // ------------ Right double click
5101 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5103 DisableCellEditControl();
5104 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5109 // no default action at the moment
5113 // ------------ Moving and no button action
5115 else if ( event
.Moving() && !event
.IsButton() )
5117 int dragRow
= YToEdgeOfRow( y
);
5118 int dragCol
= XToEdgeOfCol( x
);
5120 // Dragging on the corner of a cell to resize in both
5121 // directions is not implemented yet...
5123 if ( dragRow
>= 0 && dragCol
>= 0 )
5125 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5131 m_dragRowOrCol
= dragRow
;
5133 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5135 if ( CanDragRowSize() && CanDragGridSize() )
5136 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5141 m_dragRowOrCol
= dragCol
;
5149 m_dragRowOrCol
= dragCol
;
5151 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5153 if ( CanDragColSize() && CanDragGridSize() )
5154 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5160 // Neither on a row or col edge
5162 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5164 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5170 void wxGrid::DoEndDragResizeRow()
5172 if ( m_dragLastPos
>= 0 )
5174 // erase the last line and resize the row
5176 int cw
, ch
, left
, dummy
;
5177 m_gridWin
->GetClientSize( &cw
, &ch
);
5178 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5180 wxClientDC
dc( m_gridWin
);
5182 dc
.SetLogicalFunction( wxINVERT
);
5183 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5184 HideCellEditControl();
5185 SaveEditControlValue();
5187 int rowTop
= GetRowTop(m_dragRowOrCol
);
5188 SetRowSize( m_dragRowOrCol
,
5189 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5191 if ( !GetBatchCount() )
5193 // Only needed to get the correct rect.y:
5194 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5196 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5197 rect
.width
= m_rowLabelWidth
;
5198 rect
.height
= ch
- rect
.y
;
5199 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5201 m_gridWin
->Refresh( FALSE
, &rect
);
5204 ShowCellEditControl();
5209 void wxGrid::DoEndDragResizeCol()
5211 if ( m_dragLastPos
>= 0 )
5213 // erase the last line and resize the col
5215 int cw
, ch
, dummy
, top
;
5216 m_gridWin
->GetClientSize( &cw
, &ch
);
5217 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5219 wxClientDC
dc( m_gridWin
);
5221 dc
.SetLogicalFunction( wxINVERT
);
5222 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5223 HideCellEditControl();
5224 SaveEditControlValue();
5226 int colLeft
= GetColLeft(m_dragRowOrCol
);
5227 SetColSize( m_dragRowOrCol
,
5228 wxMax( m_dragLastPos
- colLeft
,
5229 GetColMinimalWidth(m_dragRowOrCol
) ) );
5231 if ( !GetBatchCount() )
5233 // Only needed to get the correct rect.x:
5234 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5236 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5237 rect
.width
= cw
- rect
.x
;
5238 rect
.height
= m_colLabelHeight
;
5239 m_colLabelWin
->Refresh( TRUE
, &rect
);
5241 m_gridWin
->Refresh( FALSE
, &rect
);
5244 ShowCellEditControl();
5251 // ------ interaction with data model
5253 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5255 switch ( msg
.GetId() )
5257 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5258 return GetModelValues();
5260 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5261 return SetModelValues();
5263 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5264 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5265 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5266 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5267 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5268 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5269 return Redimension( msg
);
5278 // The behaviour of this function depends on the grid table class
5279 // Clear() function. For the default wxGridStringTable class the
5280 // behavious is to replace all cell contents with wxEmptyString but
5281 // not to change the number of rows or cols.
5283 void wxGrid::ClearGrid()
5287 if (IsCellEditControlEnabled())
5288 DisableCellEditControl();
5291 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5296 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5298 // TODO: something with updateLabels flag
5302 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5308 if (IsCellEditControlEnabled())
5309 DisableCellEditControl();
5311 return m_table
->InsertRows( pos
, numRows
);
5313 // the table will have sent the results of the insert row
5314 // operation to this view object as a grid table message
5320 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5322 // TODO: something with updateLabels flag
5326 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5330 return ( m_table
&& m_table
->AppendRows( numRows
) );
5331 // the table will have sent the results of the append row
5332 // operation to this view object as a grid table message
5336 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5338 // TODO: something with updateLabels flag
5342 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5348 if (IsCellEditControlEnabled())
5349 DisableCellEditControl();
5351 return (m_table
->DeleteRows( pos
, numRows
));
5352 // the table will have sent the results of the delete row
5353 // operation to this view object as a grid table message
5359 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5361 // TODO: something with updateLabels flag
5365 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5371 if (IsCellEditControlEnabled())
5372 DisableCellEditControl();
5374 return m_table
->InsertCols( pos
, numCols
);
5375 // the table will have sent the results of the insert col
5376 // operation to this view object as a grid table message
5382 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5384 // TODO: something with updateLabels flag
5388 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5392 return ( m_table
&& m_table
->AppendCols( numCols
) );
5393 // the table will have sent the results of the append col
5394 // operation to this view object as a grid table message
5398 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5400 // TODO: something with updateLabels flag
5404 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5410 if (IsCellEditControlEnabled())
5411 DisableCellEditControl();
5413 return ( m_table
->DeleteCols( pos
, numCols
) );
5414 // the table will have sent the results of the delete col
5415 // operation to this view object as a grid table message
5423 // ----- event handlers
5426 // Generate a grid event based on a mouse event and
5427 // return the result of ProcessEvent()
5429 bool wxGrid::SendEvent( const wxEventType type
,
5431 wxMouseEvent
& mouseEv
)
5433 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5435 int rowOrCol
= (row
== -1 ? col
: row
);
5437 wxGridSizeEvent
gridEvt( GetId(),
5441 mouseEv
.GetX() + GetRowLabelSize(),
5442 mouseEv
.GetY() + GetColLabelSize(),
5443 mouseEv
.ControlDown(),
5444 mouseEv
.ShiftDown(),
5446 mouseEv
.MetaDown() );
5447 return GetEventHandler()->ProcessEvent(gridEvt
);
5449 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5451 // Right now, it should _never_ end up here!
5452 wxGridRangeSelectEvent
gridEvt( GetId(),
5456 m_selectingBottomRight
,
5458 mouseEv
.ControlDown(),
5459 mouseEv
.ShiftDown(),
5461 mouseEv
.MetaDown() );
5463 return GetEventHandler()->ProcessEvent(gridEvt
);
5467 wxGridEvent
gridEvt( GetId(),
5471 mouseEv
.GetX() + GetRowLabelSize(),
5472 mouseEv
.GetY() + GetColLabelSize(),
5474 mouseEv
.ControlDown(),
5475 mouseEv
.ShiftDown(),
5477 mouseEv
.MetaDown() );
5478 return GetEventHandler()->ProcessEvent(gridEvt
);
5483 // Generate a grid event of specified type and return the result
5484 // of ProcessEvent().
5486 bool wxGrid::SendEvent( const wxEventType type
,
5489 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5491 int rowOrCol
= (row
== -1 ? col
: row
);
5493 wxGridSizeEvent
gridEvt( GetId(),
5498 return GetEventHandler()->ProcessEvent(gridEvt
);
5502 wxGridEvent
gridEvt( GetId(),
5507 return GetEventHandler()->ProcessEvent(gridEvt
);
5512 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5514 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5518 // This is just here to make sure that CalcDimensions gets called when
5519 // the grid view is resized... then the size event is skipped to allow
5520 // the box sizers to handle everything
5522 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5529 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5531 if ( m_inOnKeyDown
)
5533 // shouldn't be here - we are going round in circles...
5535 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5538 m_inOnKeyDown
= TRUE
;
5540 // propagate the event up and see if it gets processed
5542 wxWindow
*parent
= GetParent();
5543 wxKeyEvent
keyEvt( event
);
5544 keyEvt
.SetEventObject( parent
);
5546 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5549 // try local handlers
5551 switch ( event
.KeyCode() )
5554 if ( event
.ControlDown() )
5556 MoveCursorUpBlock( event
.ShiftDown() );
5560 MoveCursorUp( event
.ShiftDown() );
5565 if ( event
.ControlDown() )
5567 MoveCursorDownBlock( event
.ShiftDown() );
5571 MoveCursorDown( event
.ShiftDown() );
5576 if ( event
.ControlDown() )
5578 MoveCursorLeftBlock( event
.ShiftDown() );
5582 MoveCursorLeft( event
.ShiftDown() );
5587 if ( event
.ControlDown() )
5589 MoveCursorRightBlock( event
.ShiftDown() );
5593 MoveCursorRight( event
.ShiftDown() );
5598 case WXK_NUMPAD_ENTER
:
5599 if ( event
.ControlDown() )
5601 event
.Skip(); // to let the edit control have the return
5605 if ( GetGridCursorRow() < GetNumberRows()-1 )
5607 MoveCursorDown( event
.ShiftDown() );
5611 // at the bottom of a column
5612 HideCellEditControl();
5613 SaveEditControlValue();
5623 if (event
.ShiftDown())
5625 if ( GetGridCursorCol() > 0 )
5627 MoveCursorLeft( FALSE
);
5632 HideCellEditControl();
5633 SaveEditControlValue();
5638 if ( GetGridCursorCol() < GetNumberCols()-1 )
5640 MoveCursorRight( FALSE
);
5645 HideCellEditControl();
5646 SaveEditControlValue();
5652 if ( event
.ControlDown() )
5654 MakeCellVisible( 0, 0 );
5655 SetCurrentCell( 0, 0 );
5664 if ( event
.ControlDown() )
5666 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5667 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5684 if ( event
.ControlDown() )
5686 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5687 m_currentCellCoords
.GetCol(),
5688 event
.ControlDown(),
5694 if ( !IsEditable() )
5696 MoveCursorRight( FALSE
);
5699 // Otherwise fall through to default
5702 // is it possible to edit the current cell at all?
5703 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5705 // yes, now check whether the cells editor accepts the key
5706 int row
= m_currentCellCoords
.GetRow();
5707 int col
= m_currentCellCoords
.GetCol();
5708 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5709 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5711 // <F2> is special and will always start editing, for
5712 // other keys - ask the editor itself
5713 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5714 || editor
->IsAcceptedKey(event
) )
5716 EnableCellEditControl();
5717 editor
->StartingKey(event
);
5729 // let others process char events with modifiers or all
5730 // char events for readonly cells
5737 m_inOnKeyDown
= FALSE
;
5740 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5742 // try local handlers
5744 if ( event
.KeyCode() == WXK_SHIFT
)
5746 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5747 m_selectingBottomRight
!= wxGridNoCellCoords
)
5748 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5749 m_selectingTopLeft
.GetCol(),
5750 m_selectingBottomRight
.GetRow(),
5751 m_selectingBottomRight
.GetCol(),
5752 event
.ControlDown(),
5756 m_selectingTopLeft
= wxGridNoCellCoords
;
5757 m_selectingBottomRight
= wxGridNoCellCoords
;
5758 m_selectingKeyboard
= wxGridNoCellCoords
;
5762 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5766 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5768 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5770 // the event has been intercepted - do nothing
5774 wxClientDC
dc(m_gridWin
);
5777 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5779 HideCellEditControl();
5780 DisableCellEditControl();
5782 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5785 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5786 if ( !m_gridLinesEnabled
)
5794 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5796 // Otherwise refresh redraws the highlight!
5797 m_currentCellCoords
= coords
;
5799 DrawGridCellArea(dc
,cells
);
5800 DrawAllGridLines( dc
, r
);
5804 m_currentCellCoords
= coords
;
5806 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5807 DrawCellHighlight(dc
, attr
);
5812 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5815 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5817 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5820 rightCol
= GetNumberCols() - 1;
5822 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5825 bottomRow
= GetNumberRows() - 1;
5827 if ( topRow
> bottomRow
)
5834 if ( leftCol
> rightCol
)
5841 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5842 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5844 if ( m_selectingTopLeft
!= updateTopLeft
||
5845 m_selectingBottomRight
!= updateBottomRight
)
5847 // Compute two optimal update rectangles:
5848 // Either one rectangle is a real subset of the
5849 // other, or they are (almost) disjoint!
5851 bool need_refresh
[4];
5855 need_refresh
[3] = FALSE
;
5858 // Store intermediate values
5859 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5860 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5861 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5862 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5864 // Determine the outer/inner coordinates.
5865 if (oldLeft
> leftCol
)
5871 if (oldTop
> topRow
)
5877 if (oldRight
< rightCol
)
5880 oldRight
= rightCol
;
5883 if (oldBottom
< bottomRow
)
5886 oldBottom
= bottomRow
;
5890 // Now, either the stuff marked old is the outer
5891 // rectangle or we don't have a situation where one
5892 // is contained in the other.
5894 if ( oldLeft
< leftCol
)
5896 need_refresh
[0] = TRUE
;
5897 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5899 wxGridCellCoords ( oldBottom
,
5903 if ( oldTop
< topRow
)
5905 need_refresh
[1] = TRUE
;
5906 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5908 wxGridCellCoords ( topRow
- 1,
5912 if ( oldRight
> rightCol
)
5914 need_refresh
[2] = TRUE
;
5915 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5917 wxGridCellCoords ( oldBottom
,
5921 if ( oldBottom
> bottomRow
)
5923 need_refresh
[3] = TRUE
;
5924 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5926 wxGridCellCoords ( oldBottom
,
5932 m_selectingTopLeft
= updateTopLeft
;
5933 m_selectingBottomRight
= updateBottomRight
;
5935 // various Refresh() calls
5936 for (i
= 0; i
< 4; i
++ )
5937 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5938 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5941 // never generate an event as it will be generated from
5942 // wxGridSelection::SelectBlock!
5943 // (old comment from when this was the body of SelectBlock)
5947 // ------ functions to get/send data (see also public functions)
5950 bool wxGrid::GetModelValues()
5954 // all we need to do is repaint the grid
5956 m_gridWin
->Refresh();
5964 bool wxGrid::SetModelValues()
5970 for ( row
= 0; row
< m_numRows
; row
++ )
5972 for ( col
= 0; col
< m_numCols
; col
++ )
5974 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5986 // Note - this function only draws cells that are in the list of
5987 // exposed cells (usually set from the update region by
5988 // CalcExposedCells)
5990 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5992 if ( !m_numRows
|| !m_numCols
) return;
5995 size_t numCells
= cells
.GetCount();
5997 for ( i
= 0; i
< numCells
; i
++ )
5999 DrawCell( dc
, cells
[i
] );
6004 void wxGrid::DrawGridSpace( wxDC
& dc
)
6007 m_gridWin
->GetClientSize( &cw
, &ch
);
6010 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6012 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6013 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6015 if ( right
> rightCol
|| bottom
> bottomRow
)
6018 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6020 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6021 dc
.SetPen( *wxTRANSPARENT_PEN
);
6023 if ( right
> rightCol
)
6025 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6028 if ( bottom
> bottomRow
)
6030 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6036 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6038 int row
= coords
.GetRow();
6039 int col
= coords
.GetCol();
6041 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6044 // we draw the cell border ourselves
6045 #if !WXGRID_DRAW_LINES
6046 if ( m_gridLinesEnabled
)
6047 DrawCellBorder( dc
, coords
);
6050 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6052 bool isCurrent
= coords
== m_currentCellCoords
;
6054 wxRect rect
= CellToRect( row
, col
);
6056 // if the editor is shown, we should use it and not the renderer
6057 // Note: However, only if it is really _shown_, i.e. not hidden!
6058 if ( isCurrent
&& IsCellEditControlShown() )
6060 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6061 editor
->PaintBackground(rect
, attr
);
6066 // but all the rest is drawn by the cell renderer and hence may be
6068 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6069 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6076 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6078 int row
= m_currentCellCoords
.GetRow();
6079 int col
= m_currentCellCoords
.GetCol();
6081 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6084 wxRect rect
= CellToRect(row
, col
);
6086 // hmmm... what could we do here to show that the cell is disabled?
6087 // for now, I just draw a thinner border than for the other ones, but
6088 // it doesn't look really good
6090 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6094 // The center of th drawn line is where the position/width/height of
6095 // the rectangle is actually at, (on wxMSW atr least,) so we will
6096 // reduce the size of the rectangle to compensate for the thickness of
6097 // the line. If this is too strange on non wxMSW platforms then
6098 // please #ifdef this appropriately.
6099 rect
.x
+= penWidth
/2;
6100 rect
.y
+= penWidth
/2;
6101 rect
.width
-= penWidth
-1;
6102 rect
.height
-= penWidth
-1;
6105 // Now draw the rectangle
6106 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6107 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6108 dc
.DrawRectangle(rect
);
6112 // VZ: my experiments with 3d borders...
6114 // how to properly set colours for arbitrary bg?
6115 wxCoord x1
= rect
.x
,
6117 x2
= rect
.x
+ rect
.width
-1,
6118 y2
= rect
.y
+ rect
.height
-1;
6120 dc
.SetPen(*wxWHITE_PEN
);
6121 dc
.DrawLine(x1
, y1
, x2
, y1
);
6122 dc
.DrawLine(x1
, y1
, x1
, y2
);
6124 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6125 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6127 dc
.SetPen(*wxBLACK_PEN
);
6128 dc
.DrawLine(x1
, y2
, x2
, y2
);
6129 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6134 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6136 int row
= coords
.GetRow();
6137 int col
= coords
.GetCol();
6138 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6141 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6143 // right hand border
6145 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6146 GetColRight(col
), GetRowBottom(row
) );
6150 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6151 GetColRight(col
), GetRowBottom(row
) );
6154 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6156 // This if block was previously in wxGrid::OnPaint but that doesn't
6157 // seem to get called under wxGTK - MB
6159 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6160 m_numRows
&& m_numCols
)
6162 m_currentCellCoords
.Set(0, 0);
6165 if ( IsCellEditControlShown() )
6167 // don't show highlight when the edit control is shown
6171 // if the active cell was repainted, repaint its highlight too because it
6172 // might have been damaged by the grid lines
6173 size_t count
= cells
.GetCount();
6174 for ( size_t n
= 0; n
< count
; n
++ )
6176 if ( cells
[n
] == m_currentCellCoords
)
6178 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6179 DrawCellHighlight(dc
, attr
);
6187 // TODO: remove this ???
6188 // This is used to redraw all grid lines e.g. when the grid line colour
6191 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6193 if ( !m_gridLinesEnabled
||
6195 !m_numCols
) return;
6197 int top
, bottom
, left
, right
;
6199 #if 0 //#ifndef __WXGTK__
6203 m_gridWin
->GetClientSize(&cw
, &ch
);
6205 // virtual coords of visible area
6207 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6208 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6213 reg
.GetBox(x
, y
, w
, h
);
6214 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6215 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6219 m_gridWin
->GetClientSize(&cw
, &ch
);
6220 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6221 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6224 // avoid drawing grid lines past the last row and col
6226 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6227 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6229 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6231 // horizontal grid lines
6234 for ( i
= 0; i
< m_numRows
; i
++ )
6236 int bot
= GetRowBottom(i
) - 1;
6245 dc
.DrawLine( left
, bot
, right
, bot
);
6250 // vertical grid lines
6252 for ( i
= 0; i
< m_numCols
; i
++ )
6254 int colRight
= GetColRight(i
) - 1;
6255 if ( colRight
> right
)
6260 if ( colRight
>= left
)
6262 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6268 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6270 if ( !m_numRows
) return;
6273 size_t numLabels
= rows
.GetCount();
6275 for ( i
= 0; i
< numLabels
; i
++ )
6277 DrawRowLabel( dc
, rows
[i
] );
6282 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6284 if ( GetRowHeight(row
) <= 0 )
6287 int rowTop
= GetRowTop(row
),
6288 rowBottom
= GetRowBottom(row
) - 1;
6290 dc
.SetPen( *wxBLACK_PEN
);
6291 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6292 m_rowLabelWidth
-1, rowBottom
);
6294 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6296 dc
.SetPen( *wxWHITE_PEN
);
6297 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6298 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6300 dc
.SetBackgroundMode( wxTRANSPARENT
);
6301 dc
.SetTextForeground( GetLabelTextColour() );
6302 dc
.SetFont( GetLabelFont() );
6305 GetRowLabelAlignment( &hAlign
, &vAlign
);
6309 rect
.SetY( GetRowTop(row
) + 2 );
6310 rect
.SetWidth( m_rowLabelWidth
- 4 );
6311 rect
.SetHeight( GetRowHeight(row
) - 4 );
6312 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6316 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6318 if ( !m_numCols
) return;
6321 size_t numLabels
= cols
.GetCount();
6323 for ( i
= 0; i
< numLabels
; i
++ )
6325 DrawColLabel( dc
, cols
[i
] );
6330 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6332 if ( GetColWidth(col
) <= 0 )
6335 int colLeft
= GetColLeft(col
),
6336 colRight
= GetColRight(col
) - 1;
6338 dc
.SetPen( *wxBLACK_PEN
);
6339 dc
.DrawLine( colRight
, 0,
6340 colRight
, m_colLabelHeight
-1 );
6342 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6343 colRight
, m_colLabelHeight
-1 );
6345 dc
.SetPen( *wxWHITE_PEN
);
6346 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6347 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6349 dc
.SetBackgroundMode( wxTRANSPARENT
);
6350 dc
.SetTextForeground( GetLabelTextColour() );
6351 dc
.SetFont( GetLabelFont() );
6353 dc
.SetBackgroundMode( wxTRANSPARENT
);
6354 dc
.SetTextForeground( GetLabelTextColour() );
6355 dc
.SetFont( GetLabelFont() );
6358 GetColLabelAlignment( &hAlign
, &vAlign
);
6361 rect
.SetX( colLeft
+ 2 );
6363 rect
.SetWidth( GetColWidth(col
) - 4 );
6364 rect
.SetHeight( m_colLabelHeight
- 4 );
6365 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6368 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6369 const wxString
& value
,
6374 wxArrayString lines
;
6376 dc
.SetClippingRegion( rect
);
6377 StringToLines( value
, lines
);
6380 //Forward to new API.
6381 DrawTextRectangle( dc
,
6389 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6390 const wxArrayString
& lines
,
6395 long textWidth
, textHeight
;
6396 long lineWidth
, lineHeight
;
6398 if ( lines
.GetCount() )
6400 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6401 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6404 switch ( horizAlign
)
6407 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6410 case wxALIGN_CENTRE
:
6411 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6420 switch ( vertAlign
)
6422 case wxALIGN_BOTTOM
:
6423 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6426 case wxALIGN_CENTRE
:
6427 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6436 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6438 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6443 dc
.DestroyClippingRegion();
6447 // Split multi line text up into an array of strings. Any existing
6448 // contents of the string array are preserved.
6450 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6454 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6455 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6457 while ( startPos
< (int)tVal
.Length() )
6459 pos
= tVal
.Mid(startPos
).Find( eol
);
6464 else if ( pos
== 0 )
6466 lines
.Add( wxEmptyString
);
6470 lines
.Add( value
.Mid(startPos
, pos
) );
6474 if ( startPos
< (int)value
.Length() )
6476 lines
.Add( value
.Mid( startPos
) );
6481 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6482 const wxArrayString
& lines
,
6483 long *width
, long *height
)
6490 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6492 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6493 w
= wxMax( w
, lineW
);
6502 // ------ Batch processing.
6504 void wxGrid::EndBatch()
6506 if ( m_batchCount
> 0 )
6509 if ( !m_batchCount
)
6512 m_rowLabelWin
->Refresh();
6513 m_colLabelWin
->Refresh();
6514 m_cornerLabelWin
->Refresh();
6515 m_gridWin
->Refresh();
6520 // Use this, rather than wxWindow::Refresh(), to force an immediate
6521 // repainting of the grid. Has no effect if you are already inside a
6522 // BeginBatch / EndBatch block.
6524 void wxGrid::ForceRefresh()
6532 // ------ Edit control functions
6536 void wxGrid::EnableEditing( bool edit
)
6538 // TODO: improve this ?
6540 if ( edit
!= m_editable
)
6542 if(!edit
) EnableCellEditControl(edit
);
6548 void wxGrid::EnableCellEditControl( bool enable
)
6553 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6554 SetCurrentCell( 0, 0 );
6556 if ( enable
!= m_cellEditCtrlEnabled
)
6558 // TODO allow the app to Veto() this event?
6559 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6563 // this should be checked by the caller!
6564 wxASSERT_MSG( CanEnableCellControl(),
6565 _T("can't enable editing for this cell!") );
6567 // do it before ShowCellEditControl()
6568 m_cellEditCtrlEnabled
= enable
;
6570 ShowCellEditControl();
6574 HideCellEditControl();
6575 SaveEditControlValue();
6577 // do it after HideCellEditControl()
6578 m_cellEditCtrlEnabled
= enable
;
6583 bool wxGrid::IsCurrentCellReadOnly() const
6586 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6587 bool readonly
= attr
->IsReadOnly();
6593 bool wxGrid::CanEnableCellControl() const
6595 return m_editable
&& !IsCurrentCellReadOnly();
6598 bool wxGrid::IsCellEditControlEnabled() const
6600 // the cell edit control might be disable for all cells or just for the
6601 // current one if it's read only
6602 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6605 bool wxGrid::IsCellEditControlShown() const
6607 bool isShown
= FALSE
;
6609 if ( m_cellEditCtrlEnabled
)
6611 int row
= m_currentCellCoords
.GetRow();
6612 int col
= m_currentCellCoords
.GetCol();
6613 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6614 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6619 if ( editor
->IsCreated() )
6621 isShown
= editor
->GetControl()->IsShown();
6631 void wxGrid::ShowCellEditControl()
6633 if ( IsCellEditControlEnabled() )
6635 if ( !IsVisible( m_currentCellCoords
) )
6637 m_cellEditCtrlEnabled
= FALSE
;
6642 wxRect rect
= CellToRect( m_currentCellCoords
);
6643 int row
= m_currentCellCoords
.GetRow();
6644 int col
= m_currentCellCoords
.GetCol();
6646 // convert to scrolled coords
6648 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6650 // done in PaintBackground()
6652 // erase the highlight and the cell contents because the editor
6653 // might not cover the entire cell
6654 wxClientDC
dc( m_gridWin
);
6656 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6657 dc
.SetPen(*wxTRANSPARENT_PEN
);
6658 dc
.DrawRectangle(rect
);
6661 // cell is shifted by one pixel
6665 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6666 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6667 if ( !editor
->IsCreated() )
6669 editor
->Create(m_gridWin
, -1,
6670 new wxGridCellEditorEvtHandler(this, editor
));
6672 wxGridEditorCreatedEvent
evt(GetId(),
6673 wxEVT_GRID_EDITOR_CREATED
,
6677 editor
->GetControl());
6678 GetEventHandler()->ProcessEvent(evt
);
6681 editor
->Show( TRUE
, attr
);
6683 editor
->SetSize( rect
);
6685 editor
->BeginEdit(row
, col
, this);
6694 void wxGrid::HideCellEditControl()
6696 if ( IsCellEditControlEnabled() )
6698 int row
= m_currentCellCoords
.GetRow();
6699 int col
= m_currentCellCoords
.GetCol();
6701 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6702 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6703 editor
->Show( FALSE
);
6706 m_gridWin
->SetFocus();
6707 wxRect
rect( CellToRect( row
, col
) );
6708 m_gridWin
->Refresh( FALSE
, &rect
);
6713 void wxGrid::SaveEditControlValue()
6715 if ( IsCellEditControlEnabled() )
6717 int row
= m_currentCellCoords
.GetRow();
6718 int col
= m_currentCellCoords
.GetCol();
6720 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6721 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6722 bool changed
= editor
->EndEdit(row
, col
, this);
6729 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6730 m_currentCellCoords
.GetRow(),
6731 m_currentCellCoords
.GetCol() );
6738 // ------ Grid location functions
6739 // Note that all of these functions work with the logical coordinates of
6740 // grid cells and labels so you will need to convert from device
6741 // coordinates for mouse events etc.
6744 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6746 int row
= YToRow(y
);
6747 int col
= XToCol(x
);
6749 if ( row
== -1 || col
== -1 )
6751 coords
= wxGridNoCellCoords
;
6755 coords
.Set( row
, col
);
6760 int wxGrid::YToRow( int y
)
6764 for ( i
= 0; i
< m_numRows
; i
++ )
6766 if ( y
< GetRowBottom(i
) )
6774 int wxGrid::XToCol( int x
)
6778 for ( i
= 0; i
< m_numCols
; i
++ )
6780 if ( x
< GetColRight(i
) )
6788 // return the row number that that the y coord is near the edge of, or
6789 // -1 if not near an edge
6791 int wxGrid::YToEdgeOfRow( int y
)
6795 for ( i
= 0; i
< m_numRows
; i
++ )
6797 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6799 d
= abs( y
- GetRowBottom(i
) );
6800 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6809 // return the col number that that the x coord is near the edge of, or
6810 // -1 if not near an edge
6812 int wxGrid::XToEdgeOfCol( int x
)
6816 for ( i
= 0; i
< m_numCols
; i
++ )
6818 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6820 d
= abs( x
- GetColRight(i
) );
6821 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6830 wxRect
wxGrid::CellToRect( int row
, int col
)
6832 wxRect
rect( -1, -1, -1, -1 );
6834 if ( row
>= 0 && row
< m_numRows
&&
6835 col
>= 0 && col
< m_numCols
)
6837 rect
.x
= GetColLeft(col
);
6838 rect
.y
= GetRowTop(row
);
6839 rect
.width
= GetColWidth(col
);
6840 rect
.height
= GetRowHeight(row
);
6843 // if grid lines are enabled, then the area of the cell is a bit smaller
6844 if (m_gridLinesEnabled
) {
6852 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6854 // get the cell rectangle in logical coords
6856 wxRect
r( CellToRect( row
, col
) );
6858 // convert to device coords
6860 int left
, top
, right
, bottom
;
6861 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6862 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6864 // check against the client area of the grid window
6867 m_gridWin
->GetClientSize( &cw
, &ch
);
6869 if ( wholeCellVisible
)
6871 // is the cell wholly visible ?
6873 return ( left
>= 0 && right
<= cw
&&
6874 top
>= 0 && bottom
<= ch
);
6878 // is the cell partly visible ?
6880 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6881 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6886 // make the specified cell location visible by doing a minimal amount
6889 void wxGrid::MakeCellVisible( int row
, int col
)
6892 int xpos
= -1, ypos
= -1;
6894 if ( row
>= 0 && row
< m_numRows
&&
6895 col
>= 0 && col
< m_numCols
)
6897 // get the cell rectangle in logical coords
6899 wxRect
r( CellToRect( row
, col
) );
6901 // convert to device coords
6903 int left
, top
, right
, bottom
;
6904 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6905 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6908 m_gridWin
->GetClientSize( &cw
, &ch
);
6914 else if ( bottom
> ch
)
6916 int h
= r
.GetHeight();
6918 for ( i
= row
-1; i
>= 0; i
-- )
6920 int rowHeight
= GetRowHeight(i
);
6921 if ( h
+ rowHeight
> ch
)
6928 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6929 // have rounding errors (this is important, because if we do, we
6930 // might not scroll at all and some cells won't be redrawn)
6931 ypos
+= GRID_SCROLL_LINE
/ 2;
6938 else if ( right
> cw
)
6940 int w
= r
.GetWidth();
6942 for ( i
= col
-1; i
>= 0; i
-- )
6944 int colWidth
= GetColWidth(i
);
6945 if ( w
+ colWidth
> cw
)
6952 // see comment for ypos above
6953 xpos
+= GRID_SCROLL_LINE
/ 2;
6956 if ( xpos
!= -1 || ypos
!= -1 )
6958 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6959 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6960 Scroll( xpos
, ypos
);
6968 // ------ Grid cursor movement functions
6971 bool wxGrid::MoveCursorUp( bool expandSelection
)
6973 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6974 m_currentCellCoords
.GetRow() >= 0 )
6976 if ( expandSelection
)
6978 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6979 m_selectingKeyboard
= m_currentCellCoords
;
6980 if ( m_selectingKeyboard
.GetRow() > 0 )
6982 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6983 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6984 m_selectingKeyboard
.GetCol() );
6985 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6988 else if ( m_currentCellCoords
.GetRow() > 0 )
6991 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6992 m_currentCellCoords
.GetCol() );
6993 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6994 m_currentCellCoords
.GetCol() );
7005 bool wxGrid::MoveCursorDown( bool expandSelection
)
7007 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7008 m_currentCellCoords
.GetRow() < m_numRows
)
7010 if ( expandSelection
)
7012 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7013 m_selectingKeyboard
= m_currentCellCoords
;
7014 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7016 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7017 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7018 m_selectingKeyboard
.GetCol() );
7019 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7022 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7025 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7026 m_currentCellCoords
.GetCol() );
7027 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7028 m_currentCellCoords
.GetCol() );
7039 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7041 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7042 m_currentCellCoords
.GetCol() >= 0 )
7044 if ( expandSelection
)
7046 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7047 m_selectingKeyboard
= m_currentCellCoords
;
7048 if ( m_selectingKeyboard
.GetCol() > 0 )
7050 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7051 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7052 m_selectingKeyboard
.GetCol() );
7053 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7056 else if ( m_currentCellCoords
.GetCol() > 0 )
7059 MakeCellVisible( m_currentCellCoords
.GetRow(),
7060 m_currentCellCoords
.GetCol() - 1 );
7061 SetCurrentCell( m_currentCellCoords
.GetRow(),
7062 m_currentCellCoords
.GetCol() - 1 );
7073 bool wxGrid::MoveCursorRight( bool expandSelection
)
7075 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7076 m_currentCellCoords
.GetCol() < m_numCols
)
7078 if ( expandSelection
)
7080 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7081 m_selectingKeyboard
= m_currentCellCoords
;
7082 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7084 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7085 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7086 m_selectingKeyboard
.GetCol() );
7087 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7090 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7093 MakeCellVisible( m_currentCellCoords
.GetRow(),
7094 m_currentCellCoords
.GetCol() + 1 );
7095 SetCurrentCell( m_currentCellCoords
.GetRow(),
7096 m_currentCellCoords
.GetCol() + 1 );
7107 bool wxGrid::MovePageUp()
7109 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7111 int row
= m_currentCellCoords
.GetRow();
7115 m_gridWin
->GetClientSize( &cw
, &ch
);
7117 int y
= GetRowTop(row
);
7118 int newRow
= YToRow( y
- ch
+ 1 );
7123 else if ( newRow
== row
)
7128 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7129 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7137 bool wxGrid::MovePageDown()
7139 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7141 int row
= m_currentCellCoords
.GetRow();
7142 if ( row
< m_numRows
)
7145 m_gridWin
->GetClientSize( &cw
, &ch
);
7147 int y
= GetRowTop(row
);
7148 int newRow
= YToRow( y
+ ch
);
7151 newRow
= m_numRows
- 1;
7153 else if ( newRow
== row
)
7158 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7159 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7167 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7170 m_currentCellCoords
!= wxGridNoCellCoords
&&
7171 m_currentCellCoords
.GetRow() > 0 )
7173 int row
= m_currentCellCoords
.GetRow();
7174 int col
= m_currentCellCoords
.GetCol();
7176 if ( m_table
->IsEmptyCell(row
, col
) )
7178 // starting in an empty cell: find the next block of
7184 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7187 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7189 // starting at the top of a block: find the next block
7195 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7200 // starting within a block: find the top of the block
7205 if ( m_table
->IsEmptyCell(row
, col
) )
7213 MakeCellVisible( row
, col
);
7214 if ( expandSelection
)
7216 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7217 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7222 SetCurrentCell( row
, col
);
7230 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7233 m_currentCellCoords
!= wxGridNoCellCoords
&&
7234 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7236 int row
= m_currentCellCoords
.GetRow();
7237 int col
= m_currentCellCoords
.GetCol();
7239 if ( m_table
->IsEmptyCell(row
, col
) )
7241 // starting in an empty cell: find the next block of
7244 while ( row
< m_numRows
-1 )
7247 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7250 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7252 // starting at the bottom of a block: find the next block
7255 while ( row
< m_numRows
-1 )
7258 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7263 // starting within a block: find the bottom of the block
7265 while ( row
< m_numRows
-1 )
7268 if ( m_table
->IsEmptyCell(row
, col
) )
7276 MakeCellVisible( row
, col
);
7277 if ( expandSelection
)
7279 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7280 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7285 SetCurrentCell( row
, col
);
7294 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7297 m_currentCellCoords
!= wxGridNoCellCoords
&&
7298 m_currentCellCoords
.GetCol() > 0 )
7300 int row
= m_currentCellCoords
.GetRow();
7301 int col
= m_currentCellCoords
.GetCol();
7303 if ( m_table
->IsEmptyCell(row
, col
) )
7305 // starting in an empty cell: find the next block of
7311 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7314 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7316 // starting at the left of a block: find the next block
7322 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7327 // starting within a block: find the left of the block
7332 if ( m_table
->IsEmptyCell(row
, col
) )
7340 MakeCellVisible( row
, col
);
7341 if ( expandSelection
)
7343 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7344 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7349 SetCurrentCell( row
, col
);
7358 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7361 m_currentCellCoords
!= wxGridNoCellCoords
&&
7362 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7364 int row
= m_currentCellCoords
.GetRow();
7365 int col
= m_currentCellCoords
.GetCol();
7367 if ( m_table
->IsEmptyCell(row
, col
) )
7369 // starting in an empty cell: find the next block of
7372 while ( col
< m_numCols
-1 )
7375 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7378 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7380 // starting at the right of a block: find the next block
7383 while ( col
< m_numCols
-1 )
7386 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7391 // starting within a block: find the right of the block
7393 while ( col
< m_numCols
-1 )
7396 if ( m_table
->IsEmptyCell(row
, col
) )
7404 MakeCellVisible( row
, col
);
7405 if ( expandSelection
)
7407 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7408 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7413 SetCurrentCell( row
, col
);
7425 // ------ Label values and formatting
7428 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7430 *horiz
= m_rowLabelHorizAlign
;
7431 *vert
= m_rowLabelVertAlign
;
7434 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7436 *horiz
= m_colLabelHorizAlign
;
7437 *vert
= m_colLabelVertAlign
;
7440 wxString
wxGrid::GetRowLabelValue( int row
)
7444 return m_table
->GetRowLabelValue( row
);
7454 wxString
wxGrid::GetColLabelValue( int col
)
7458 return m_table
->GetColLabelValue( col
);
7469 void wxGrid::SetRowLabelSize( int width
)
7471 width
= wxMax( width
, 0 );
7472 if ( width
!= m_rowLabelWidth
)
7476 m_rowLabelWin
->Show( FALSE
);
7477 m_cornerLabelWin
->Show( FALSE
);
7479 else if ( m_rowLabelWidth
== 0 )
7481 m_rowLabelWin
->Show( TRUE
);
7482 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7485 m_rowLabelWidth
= width
;
7492 void wxGrid::SetColLabelSize( int height
)
7494 height
= wxMax( height
, 0 );
7495 if ( height
!= m_colLabelHeight
)
7499 m_colLabelWin
->Show( FALSE
);
7500 m_cornerLabelWin
->Show( FALSE
);
7502 else if ( m_colLabelHeight
== 0 )
7504 m_colLabelWin
->Show( TRUE
);
7505 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7508 m_colLabelHeight
= height
;
7515 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7517 if ( m_labelBackgroundColour
!= colour
)
7519 m_labelBackgroundColour
= colour
;
7520 m_rowLabelWin
->SetBackgroundColour( colour
);
7521 m_colLabelWin
->SetBackgroundColour( colour
);
7522 m_cornerLabelWin
->SetBackgroundColour( colour
);
7524 if ( !GetBatchCount() )
7526 m_rowLabelWin
->Refresh();
7527 m_colLabelWin
->Refresh();
7528 m_cornerLabelWin
->Refresh();
7533 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7535 if ( m_labelTextColour
!= colour
)
7537 m_labelTextColour
= colour
;
7538 if ( !GetBatchCount() )
7540 m_rowLabelWin
->Refresh();
7541 m_colLabelWin
->Refresh();
7546 void wxGrid::SetLabelFont( const wxFont
& font
)
7549 if ( !GetBatchCount() )
7551 m_rowLabelWin
->Refresh();
7552 m_colLabelWin
->Refresh();
7556 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7558 // allow old (incorrect) defs to be used
7561 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7562 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7563 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7568 case wxTOP
: vert
= wxALIGN_TOP
; break;
7569 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7570 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7573 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7575 m_rowLabelHorizAlign
= horiz
;
7578 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7580 m_rowLabelVertAlign
= vert
;
7583 if ( !GetBatchCount() )
7585 m_rowLabelWin
->Refresh();
7589 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7591 // allow old (incorrect) defs to be used
7594 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7595 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7596 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7601 case wxTOP
: vert
= wxALIGN_TOP
; break;
7602 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7603 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7606 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7608 m_colLabelHorizAlign
= horiz
;
7611 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7613 m_colLabelVertAlign
= vert
;
7616 if ( !GetBatchCount() )
7618 m_colLabelWin
->Refresh();
7622 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7626 m_table
->SetRowLabelValue( row
, s
);
7627 if ( !GetBatchCount() )
7629 wxRect rect
= CellToRect( row
, 0);
7630 if ( rect
.height
> 0 )
7632 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7634 rect
.width
= m_rowLabelWidth
;
7635 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7641 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7645 m_table
->SetColLabelValue( col
, s
);
7646 if ( !GetBatchCount() )
7648 wxRect rect
= CellToRect( 0, col
);
7649 if ( rect
.width
> 0 )
7651 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7653 rect
.height
= m_colLabelHeight
;
7654 m_colLabelWin
->Refresh( TRUE
, &rect
);
7660 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7662 if ( m_gridLineColour
!= colour
)
7664 m_gridLineColour
= colour
;
7666 wxClientDC
dc( m_gridWin
);
7668 DrawAllGridLines( dc
, wxRegion() );
7673 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7675 if ( m_cellHighlightColour
!= colour
)
7677 m_cellHighlightColour
= colour
;
7679 wxClientDC
dc( m_gridWin
);
7681 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7682 DrawCellHighlight(dc
, attr
);
7687 void wxGrid::SetCellHighlightPenWidth(int width
)
7689 if (m_cellHighlightPenWidth
!= width
) {
7690 m_cellHighlightPenWidth
= width
;
7692 // Just redrawing the cell highlight is not enough since that won't
7693 // make any visible change if the the thickness is getting smaller.
7694 int row
= m_currentCellCoords
.GetRow();
7695 int col
= m_currentCellCoords
.GetCol();
7696 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7698 wxRect rect
= CellToRect(row
, col
);
7699 m_gridWin
->Refresh(TRUE
, &rect
);
7703 void wxGrid::SetCellHighlightROPenWidth(int width
)
7705 if (m_cellHighlightROPenWidth
!= width
) {
7706 m_cellHighlightROPenWidth
= width
;
7708 // Just redrawing the cell highlight is not enough since that won't
7709 // make any visible change if the the thickness is getting smaller.
7710 int row
= m_currentCellCoords
.GetRow();
7711 int col
= m_currentCellCoords
.GetCol();
7712 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7714 wxRect rect
= CellToRect(row
, col
);
7715 m_gridWin
->Refresh(TRUE
, &rect
);
7719 void wxGrid::EnableGridLines( bool enable
)
7721 if ( enable
!= m_gridLinesEnabled
)
7723 m_gridLinesEnabled
= enable
;
7725 if ( !GetBatchCount() )
7729 wxClientDC
dc( m_gridWin
);
7731 DrawAllGridLines( dc
, wxRegion() );
7735 m_gridWin
->Refresh();
7742 int wxGrid::GetDefaultRowSize()
7744 return m_defaultRowHeight
;
7747 int wxGrid::GetRowSize( int row
)
7749 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7751 return GetRowHeight(row
);
7754 int wxGrid::GetDefaultColSize()
7756 return m_defaultColWidth
;
7759 int wxGrid::GetColSize( int col
)
7761 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7763 return GetColWidth(col
);
7766 // ============================================================================
7767 // access to the grid attributes: each of them has a default value in the grid
7768 // itself and may be overidden on a per-cell basis
7769 // ============================================================================
7771 // ----------------------------------------------------------------------------
7772 // setting default attributes
7773 // ----------------------------------------------------------------------------
7775 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7777 m_defaultCellAttr
->SetBackgroundColour(col
);
7779 m_gridWin
->SetBackgroundColour(col
);
7783 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7785 m_defaultCellAttr
->SetTextColour(col
);
7788 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7790 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7793 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7795 m_defaultCellAttr
->SetFont(font
);
7798 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7800 m_defaultCellAttr
->SetRenderer(renderer
);
7803 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7805 m_defaultCellAttr
->SetEditor(editor
);
7808 // ----------------------------------------------------------------------------
7809 // access to the default attrbiutes
7810 // ----------------------------------------------------------------------------
7812 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7814 return m_defaultCellAttr
->GetBackgroundColour();
7817 wxColour
wxGrid::GetDefaultCellTextColour()
7819 return m_defaultCellAttr
->GetTextColour();
7822 wxFont
wxGrid::GetDefaultCellFont()
7824 return m_defaultCellAttr
->GetFont();
7827 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7829 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7832 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7834 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7837 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7839 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7842 // ----------------------------------------------------------------------------
7843 // access to cell attributes
7844 // ----------------------------------------------------------------------------
7846 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7848 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7849 wxColour colour
= attr
->GetBackgroundColour();
7854 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7856 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7857 wxColour colour
= attr
->GetTextColour();
7862 wxFont
wxGrid::GetCellFont( int row
, int col
)
7864 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7865 wxFont font
= attr
->GetFont();
7870 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7872 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7873 attr
->GetAlignment(horiz
, vert
);
7877 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7879 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7880 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7886 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7888 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7889 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7895 bool wxGrid::IsReadOnly(int row
, int col
) const
7897 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7898 bool isReadOnly
= attr
->IsReadOnly();
7903 // ----------------------------------------------------------------------------
7904 // attribute support: cache, automatic provider creation, ...
7905 // ----------------------------------------------------------------------------
7907 bool wxGrid::CanHaveAttributes()
7914 return m_table
->CanHaveAttributes();
7917 void wxGrid::ClearAttrCache()
7919 if ( m_attrCache
.row
!= -1 )
7921 wxSafeDecRef(m_attrCache
.attr
);
7922 m_attrCache
.attr
= NULL
;
7923 m_attrCache
.row
= -1;
7927 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7929 wxGrid
*self
= (wxGrid
*)this; // const_cast
7931 self
->ClearAttrCache();
7932 self
->m_attrCache
.row
= row
;
7933 self
->m_attrCache
.col
= col
;
7934 self
->m_attrCache
.attr
= attr
;
7938 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7940 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7942 *attr
= m_attrCache
.attr
;
7943 wxSafeIncRef(m_attrCache
.attr
);
7945 #ifdef DEBUG_ATTR_CACHE
7946 gs_nAttrCacheHits
++;
7953 #ifdef DEBUG_ATTR_CACHE
7954 gs_nAttrCacheMisses
++;
7960 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7962 wxGridCellAttr
*attr
;
7963 if ( !LookupAttr(row
, col
, &attr
) )
7965 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
7966 CacheAttr(row
, col
, attr
);
7970 attr
->SetDefAttr(m_defaultCellAttr
);
7974 attr
= m_defaultCellAttr
;
7981 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7983 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
7984 wxASSERT_MSG( m_table
,
7985 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7987 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7990 attr
= new wxGridCellAttr
;
7992 // artificially inc the ref count to match DecRef() in caller
7994 m_table
->SetAttr(attr
, row
, col
);
7996 attr
->SetDefAttr(m_defaultCellAttr
);
8000 // ----------------------------------------------------------------------------
8001 // setting column attributes (wrappers around SetColAttr)
8002 // ----------------------------------------------------------------------------
8004 void wxGrid::SetColFormatBool(int col
)
8006 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8009 void wxGrid::SetColFormatNumber(int col
)
8011 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8014 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8016 wxString typeName
= wxGRID_VALUE_FLOAT
;
8017 if ( (width
!= -1) || (precision
!= -1) )
8019 typeName
<< _T(':') << width
<< _T(',') << precision
;
8022 SetColFormatCustom(col
, typeName
);
8025 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8027 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8029 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8031 attr
= new wxGridCellAttr
;
8032 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8033 attr
->SetRenderer(renderer
);
8035 SetColAttr(col
, attr
);
8039 // ----------------------------------------------------------------------------
8040 // setting cell attributes: this is forwarded to the table
8041 // ----------------------------------------------------------------------------
8043 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8045 if ( CanHaveAttributes() )
8047 m_table
->SetRowAttr(attr
, row
);
8056 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8058 if ( CanHaveAttributes() )
8060 m_table
->SetColAttr(attr
, col
);
8069 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8071 if ( CanHaveAttributes() )
8073 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8074 attr
->SetBackgroundColour(colour
);
8079 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8081 if ( CanHaveAttributes() )
8083 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8084 attr
->SetTextColour(colour
);
8089 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8091 if ( CanHaveAttributes() )
8093 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8094 attr
->SetFont(font
);
8099 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8101 if ( CanHaveAttributes() )
8103 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8104 attr
->SetAlignment(horiz
, vert
);
8109 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8111 if ( CanHaveAttributes() )
8113 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8114 attr
->SetRenderer(renderer
);
8119 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8121 if ( CanHaveAttributes() )
8123 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8124 attr
->SetEditor(editor
);
8129 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8131 if ( CanHaveAttributes() )
8133 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8134 attr
->SetReadOnly(isReadOnly
);
8139 // ----------------------------------------------------------------------------
8140 // Data type registration
8141 // ----------------------------------------------------------------------------
8143 void wxGrid::RegisterDataType(const wxString
& typeName
,
8144 wxGridCellRenderer
* renderer
,
8145 wxGridCellEditor
* editor
)
8147 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8151 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8153 wxString typeName
= m_table
->GetTypeName(row
, col
);
8154 return GetDefaultEditorForType(typeName
);
8157 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8159 wxString typeName
= m_table
->GetTypeName(row
, col
);
8160 return GetDefaultRendererForType(typeName
);
8164 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8166 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8167 if ( index
== wxNOT_FOUND
)
8169 wxFAIL_MSG(wxT("Unknown data type name"));
8174 return m_typeRegistry
->GetEditor(index
);
8178 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8180 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8181 if ( index
== wxNOT_FOUND
)
8183 wxFAIL_MSG(wxT("Unknown data type name"));
8188 return m_typeRegistry
->GetRenderer(index
);
8192 // ----------------------------------------------------------------------------
8194 // ----------------------------------------------------------------------------
8196 void wxGrid::EnableDragRowSize( bool enable
)
8198 m_canDragRowSize
= enable
;
8202 void wxGrid::EnableDragColSize( bool enable
)
8204 m_canDragColSize
= enable
;
8207 void wxGrid::EnableDragGridSize( bool enable
)
8209 m_canDragGridSize
= enable
;
8213 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8215 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8217 if ( resizeExistingRows
)
8220 if ( !GetBatchCount() )
8225 void wxGrid::SetRowSize( int row
, int height
)
8227 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8229 if ( m_rowHeights
.IsEmpty() )
8231 // need to really create the array
8235 int h
= wxMax( 0, height
);
8236 int diff
= h
- m_rowHeights
[row
];
8238 m_rowHeights
[row
] = h
;
8240 for ( i
= row
; i
< m_numRows
; i
++ )
8242 m_rowBottoms
[i
] += diff
;
8244 if ( !GetBatchCount() )
8248 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8250 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8252 if ( resizeExistingCols
)
8255 if ( !GetBatchCount() )
8260 void wxGrid::SetColSize( int col
, int width
)
8262 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8264 // should we check that it's bigger than GetColMinimalWidth(col) here?
8266 if ( m_colWidths
.IsEmpty() )
8268 // need to really create the array
8272 int w
= wxMax( 0, width
);
8273 int diff
= w
- m_colWidths
[col
];
8274 m_colWidths
[col
] = w
;
8277 for ( i
= col
; i
< m_numCols
; i
++ )
8279 m_colRights
[i
] += diff
;
8281 if ( !GetBatchCount() )
8286 void wxGrid::SetColMinimalWidth( int col
, int width
)
8288 m_colMinWidths
.Put(col
, width
);
8291 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8293 m_rowMinHeights
.Put(row
, width
);
8296 int wxGrid::GetColMinimalWidth(int col
) const
8298 long value
= m_colMinWidths
.Get(col
);
8299 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8302 int wxGrid::GetRowMinimalHeight(int row
) const
8304 long value
= m_rowMinHeights
.Get(row
);
8305 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8308 // ----------------------------------------------------------------------------
8310 // ----------------------------------------------------------------------------
8312 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8314 wxClientDC
dc(m_gridWin
);
8316 // init both of them to avoid compiler warnings, even if weo nly need one
8324 wxCoord extent
, extentMax
= 0;
8325 int max
= column
? m_numRows
: m_numCols
;
8326 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8333 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8334 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8337 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8338 extent
= column
? size
.x
: size
.y
;
8339 if ( extent
> extentMax
)
8350 // now also compare with the column label extent
8352 dc
.SetFont( GetLabelFont() );
8355 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8357 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8359 extent
= column
? w
: h
;
8360 if ( extent
> extentMax
)
8367 // empty column - give default extent (notice that if extentMax is less
8368 // than default extent but != 0, it's ok)
8369 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8375 // leave some space around text
8385 SetColSize(col
, extentMax
);
8386 if ( !GetBatchCount() )
8389 m_gridWin
->GetClientSize( &cw
, &ch
);
8390 wxRect
rect ( CellToRect( 0, col
) );
8392 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8393 rect
.width
= cw
- rect
.x
;
8394 rect
.height
= m_colLabelHeight
;
8395 m_colLabelWin
->Refresh( TRUE
, &rect
);
8399 SetRowSize(row
, extentMax
);
8400 if ( !GetBatchCount() )
8403 m_gridWin
->GetClientSize( &cw
, &ch
);
8404 wxRect
rect ( CellToRect( row
, 0 ) );
8406 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8407 rect
.width
= m_rowLabelWidth
;
8408 rect
.height
= ch
- rect
.y
;
8409 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8415 SetColMinimalWidth(col
, extentMax
);
8417 SetRowMinimalHeight(row
, extentMax
);
8421 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8423 int width
= m_rowLabelWidth
;
8427 for ( int col
= 0; col
< m_numCols
; col
++ )
8431 AutoSizeColumn(col
, setAsMin
);
8434 width
+= GetColWidth(col
);
8441 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8443 int height
= m_colLabelHeight
;
8447 for ( int row
= 0; row
< m_numRows
; row
++ )
8451 AutoSizeRow(row
, setAsMin
);
8454 height
+= GetRowHeight(row
);
8461 void wxGrid::AutoSize()
8464 SetClientSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8467 wxSize
wxGrid::DoGetBestSize() const
8469 // don't set sizes, only calculate them
8470 wxGrid
*self
= (wxGrid
*)this; // const_cast
8472 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8473 self
->SetOrCalcRowSizes(TRUE
));
8482 wxPen
& wxGrid::GetDividerPen() const
8487 // ----------------------------------------------------------------------------
8488 // cell value accessor functions
8489 // ----------------------------------------------------------------------------
8491 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8495 m_table
->SetValue( row
, col
, s
);
8496 if ( !GetBatchCount() )
8498 wxClientDC
dc( m_gridWin
);
8500 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8503 if ( m_currentCellCoords
.GetRow() == row
&&
8504 m_currentCellCoords
.GetCol() == col
&&
8505 IsCellEditControlShown())
8506 // Note: If we are using IsCellEditControlEnabled,
8507 // this interacts badly with calling SetCellValue from
8508 // an EVT_GRID_CELL_CHANGE handler.
8510 HideCellEditControl();
8511 ShowCellEditControl(); // will reread data from table
8518 // ------ Block, row and col selection
8521 void wxGrid::SelectRow( int row
, bool addToSelected
)
8523 if ( IsSelection() && !addToSelected
)
8526 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8530 void wxGrid::SelectCol( int col
, bool addToSelected
)
8532 if ( IsSelection() && !addToSelected
)
8535 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8539 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8540 bool addToSelected
)
8542 if ( IsSelection() && !addToSelected
)
8545 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8546 FALSE
, addToSelected
);
8550 void wxGrid::SelectAll()
8552 if ( m_numRows
> 0 && m_numCols
> 0 )
8553 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8557 // ------ Cell, row and col deselection
8560 void wxGrid::DeselectRow( int row
)
8562 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8564 if ( m_selection
->IsInSelection(row
, 0 ) )
8565 m_selection
->ToggleCellSelection( row
, 0);
8569 int nCols
= GetNumberCols();
8570 for ( int i
= 0; i
< nCols
; i
++ )
8572 if ( m_selection
->IsInSelection(row
, i
) )
8573 m_selection
->ToggleCellSelection( row
, i
);
8578 void wxGrid::DeselectCol( int col
)
8580 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8582 if ( m_selection
->IsInSelection(0, col
) )
8583 m_selection
->ToggleCellSelection( 0, col
);
8587 int nRows
= GetNumberRows();
8588 for ( int i
= 0; i
< nRows
; i
++ )
8590 if ( m_selection
->IsInSelection(i
, col
) )
8591 m_selection
->ToggleCellSelection(i
, col
);
8596 void wxGrid::DeselectCell( int row
, int col
)
8598 if ( m_selection
->IsInSelection(row
, col
) )
8599 m_selection
->ToggleCellSelection(row
, col
);
8602 bool wxGrid::IsSelection()
8604 return ( m_selection
->IsSelection() ||
8605 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8606 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8609 bool wxGrid::IsInSelection( int row
, int col
)
8611 return ( m_selection
->IsInSelection( row
, col
) ||
8612 ( row
>= m_selectingTopLeft
.GetRow() &&
8613 col
>= m_selectingTopLeft
.GetCol() &&
8614 row
<= m_selectingBottomRight
.GetRow() &&
8615 col
<= m_selectingBottomRight
.GetCol() ) );
8618 void wxGrid::ClearSelection()
8620 m_selectingTopLeft
= wxGridNoCellCoords
;
8621 m_selectingBottomRight
= wxGridNoCellCoords
;
8622 m_selection
->ClearSelection();
8626 // This function returns the rectangle that encloses the given block
8627 // in device coords clipped to the client size of the grid window.
8629 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8630 const wxGridCellCoords
&bottomRight
)
8632 wxRect
rect( wxGridNoCellRect
);
8635 cellRect
= CellToRect( topLeft
);
8636 if ( cellRect
!= wxGridNoCellRect
)
8642 rect
= wxRect( 0, 0, 0, 0 );
8645 cellRect
= CellToRect( bottomRight
);
8646 if ( cellRect
!= wxGridNoCellRect
)
8652 return wxGridNoCellRect
;
8655 // convert to scrolled coords
8657 int left
, top
, right
, bottom
;
8658 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8659 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8662 m_gridWin
->GetClientSize( &cw
, &ch
);
8664 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8665 return wxRect( 0, 0, 0, 0);
8667 rect
.SetLeft( wxMax(0, left
) );
8668 rect
.SetTop( wxMax(0, top
) );
8669 rect
.SetRight( wxMin(cw
, right
) );
8670 rect
.SetBottom( wxMin(ch
, bottom
) );
8678 // ------ Grid event classes
8681 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8683 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8684 int row
, int col
, int x
, int y
, bool sel
,
8685 bool control
, bool shift
, bool alt
, bool meta
)
8686 : wxNotifyEvent( type
, id
)
8693 m_control
= control
;
8698 SetEventObject(obj
);
8702 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8704 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8705 int rowOrCol
, int x
, int y
,
8706 bool control
, bool shift
, bool alt
, bool meta
)
8707 : wxNotifyEvent( type
, id
)
8709 m_rowOrCol
= rowOrCol
;
8712 m_control
= control
;
8717 SetEventObject(obj
);
8721 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8723 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8724 const wxGridCellCoords
& topLeft
,
8725 const wxGridCellCoords
& bottomRight
,
8726 bool sel
, bool control
,
8727 bool shift
, bool alt
, bool meta
)
8728 : wxNotifyEvent( type
, id
)
8730 m_topLeft
= topLeft
;
8731 m_bottomRight
= bottomRight
;
8733 m_control
= control
;
8738 SetEventObject(obj
);
8742 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8744 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8745 wxObject
* obj
, int row
,
8746 int col
, wxControl
* ctrl
)
8747 : wxCommandEvent(type
, id
)
8749 SetEventObject(obj
);
8756 #endif // ifndef wxUSE_NEW_GRID