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()
523 // ----------------------------------------------------------------------------
524 // wxGridCellTextEditor
525 // ----------------------------------------------------------------------------
527 wxGridCellTextEditor::wxGridCellTextEditor()
532 void wxGridCellTextEditor::Create(wxWindow
* parent
,
534 wxEvtHandler
* evtHandler
)
536 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
537 wxDefaultPosition
, wxDefaultSize
538 #if defined(__WXMSW__)
539 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
540 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
544 // TODO: use m_maxChars
546 wxGridCellEditor::Create(parent
, id
, evtHandler
);
549 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
550 wxGridCellAttr
* WXUNUSED(attr
))
552 // as we fill the entire client area, don't do anything here to minimize
556 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
558 wxRect
rect(rectOrig
);
560 // Make the edit control large enough to allow for internal
563 // TODO: remove this if the text ctrl sizing is improved esp. for
566 #if defined(__WXGTK__)
575 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
577 // MB: treat MSW separately here otherwise the caret doesn't show
578 // when the editor is in the first row.
579 #if defined(__WXMSW__)
582 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
585 #if defined(__WXMOTIF__)
589 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
590 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
591 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
592 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
595 wxGridCellEditor::SetSize(rect
);
598 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
600 wxASSERT_MSG(m_control
,
601 wxT("The wxGridCellEditor must be Created first!"));
603 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
605 DoBeginEdit(m_startValue
);
608 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
610 Text()->SetValue(startValue
);
611 Text()->SetInsertionPointEnd();
615 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
618 wxASSERT_MSG(m_control
,
619 wxT("The wxGridCellEditor must be Created first!"));
621 bool changed
= FALSE
;
622 wxString value
= Text()->GetValue();
623 if (value
!= m_startValue
)
627 grid
->GetTable()->SetValue(row
, col
, value
);
629 m_startValue
= wxEmptyString
;
630 Text()->SetValue(m_startValue
);
636 void wxGridCellTextEditor::Reset()
638 wxASSERT_MSG(m_control
,
639 wxT("The wxGridCellEditor must be Created first!"));
641 DoReset(m_startValue
);
644 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
646 Text()->SetValue(startValue
);
647 Text()->SetInsertionPointEnd();
650 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
652 if ( wxGridCellEditor::IsAcceptedKey(event
) )
654 int keycode
= event
.GetKeyCode();
668 case WXK_NUMPAD_MULTIPLY
:
672 case WXK_NUMPAD_SUBTRACT
:
674 case WXK_NUMPAD_DECIMAL
:
676 case WXK_NUMPAD_DIVIDE
:
680 // accept 8 bit chars too if isprint() agrees
681 if ( (keycode
< 255) && (isprint(keycode
)) )
689 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
691 // we don't check for !HasModifiers() because IsAcceptedKey() did it
693 // insert the key in the control
695 int keycode
= event
.GetKeyCode();
708 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
712 case WXK_NUMPAD_MULTIPLY
:
722 case WXK_NUMPAD_SUBTRACT
:
727 case WXK_NUMPAD_DECIMAL
:
732 case WXK_NUMPAD_DIVIDE
:
737 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
739 // FIXME this is not going to work for non letters...
740 if ( !event
.ShiftDown() )
742 keycode
= tolower(keycode
);
745 ch
= (wxChar
)keycode
;
755 Text()->AppendText(ch
);
763 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
764 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
766 #if defined(__WXMOTIF__) || defined(__WXGTK__)
767 // wxMotif needs a little extra help...
768 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
769 wxString
s( Text()->GetValue() );
770 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
772 Text()->SetInsertionPoint( pos
);
774 // the other ports can handle a Return key press
780 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
790 if ( !params
.ToLong(&tmp
) )
792 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
796 m_maxChars
= (size_t)tmp
;
801 // ----------------------------------------------------------------------------
802 // wxGridCellNumberEditor
803 // ----------------------------------------------------------------------------
805 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
811 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
813 wxEvtHandler
* evtHandler
)
817 // create a spin ctrl
818 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
819 wxDefaultPosition
, wxDefaultSize
,
823 wxGridCellEditor::Create(parent
, id
, evtHandler
);
827 // just a text control
828 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
831 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
832 #endif // wxUSE_VALIDATORS
836 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
838 // first get the value
839 wxGridTableBase
*table
= grid
->GetTable();
840 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
842 m_valueOld
= table
->GetValueAsLong(row
, col
);
846 wxString sValue
= table
->GetValue(row
, col
);
847 if (! sValue
.ToLong(&m_valueOld
))
849 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
856 Spin()->SetValue((int)m_valueOld
);
861 DoBeginEdit(GetString());
865 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
873 value
= Spin()->GetValue();
874 changed
= value
!= m_valueOld
;
878 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
883 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
884 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
886 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
892 void wxGridCellNumberEditor::Reset()
896 Spin()->SetValue((int)m_valueOld
);
900 DoReset(GetString());
904 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
906 if ( wxGridCellEditor::IsAcceptedKey(event
) )
908 int keycode
= event
.GetKeyCode();
924 case WXK_NUMPAD_SUBTRACT
:
930 if ( (keycode
< 128) && isdigit(keycode
) )
938 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
942 int keycode
= (int) event
.KeyCode();
943 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
945 wxGridCellTextEditor::StartingKey(event
);
955 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
966 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
970 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
974 // skip the error message below
979 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
983 // ----------------------------------------------------------------------------
984 // wxGridCellFloatEditor
985 // ----------------------------------------------------------------------------
987 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
990 m_precision
= precision
;
993 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
995 wxEvtHandler
* evtHandler
)
997 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1000 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1001 #endif // wxUSE_VALIDATORS
1004 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1006 // first get the value
1007 wxGridTableBase
*table
= grid
->GetTable();
1008 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1010 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1014 wxString sValue
= table
->GetValue(row
, col
);
1015 if (! sValue
.ToDouble(&m_valueOld
))
1017 wxFAIL_MSG( _T("this cell doesn't have float value") );
1022 DoBeginEdit(GetString());
1025 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1029 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
1031 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1032 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1034 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1044 void wxGridCellFloatEditor::Reset()
1046 DoReset(GetString());
1049 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1051 int keycode
= (int)event
.KeyCode();
1052 if ( isdigit(keycode
) ||
1053 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1055 wxGridCellTextEditor::StartingKey(event
);
1057 // skip Skip() below
1064 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1075 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1079 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1081 m_precision
= (int)tmp
;
1083 // skip the error message below
1088 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1092 wxString
wxGridCellFloatEditor::GetString() const
1095 if ( m_width
== -1 )
1097 // default width/precision
1100 else if ( m_precision
== -1 )
1102 // default precision
1103 fmt
.Printf(_T("%%%d.g"), m_width
);
1107 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1110 return wxString::Format(fmt
, m_valueOld
);
1113 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1115 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1117 int keycode
= event
.GetKeyCode();
1131 case WXK_NUMPAD_ADD
:
1133 case WXK_NUMPAD_SUBTRACT
:
1135 case WXK_NUMPAD_DECIMAL
:
1139 // additionally accept 'e' as in '1e+6'
1140 if ( (keycode
< 128) &&
1141 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1149 #endif // wxUSE_TEXTCTRL
1153 // ----------------------------------------------------------------------------
1154 // wxGridCellBoolEditor
1155 // ----------------------------------------------------------------------------
1157 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1159 wxEvtHandler
* evtHandler
)
1161 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1162 wxDefaultPosition
, wxDefaultSize
,
1165 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1168 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1170 bool resize
= FALSE
;
1171 wxSize size
= m_control
->GetSize();
1172 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1174 // check if the checkbox is not too big/small for this cell
1175 wxSize sizeBest
= m_control
->GetBestSize();
1176 if ( !(size
== sizeBest
) )
1178 // reset to default size if it had been made smaller
1184 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1186 // leave 1 pixel margin
1187 size
.x
= size
.y
= minSize
- 2;
1194 m_control
->SetSize(size
);
1197 // position it in the centre of the rectangle (TODO: support alignment?)
1199 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1200 // the checkbox without label still has some space to the right in wxGTK,
1201 // so shift it to the right
1203 #elif defined(__WXMSW__)
1204 // here too, but in other way
1209 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1212 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1214 m_control
->Show(show
);
1218 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1219 CBox()->SetBackgroundColour(colBg
);
1223 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1225 wxASSERT_MSG(m_control
,
1226 wxT("The wxGridCellEditor must be Created first!"));
1228 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1229 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1232 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1233 m_startValue
= !( !cellval
|| (cellval
== "0") );
1235 CBox()->SetValue(m_startValue
);
1239 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1242 wxASSERT_MSG(m_control
,
1243 wxT("The wxGridCellEditor must be Created first!"));
1245 bool changed
= FALSE
;
1246 bool value
= CBox()->GetValue();
1247 if ( value
!= m_startValue
)
1252 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1253 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1255 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1261 void wxGridCellBoolEditor::Reset()
1263 wxASSERT_MSG(m_control
,
1264 wxT("The wxGridCellEditor must be Created first!"));
1266 CBox()->SetValue(m_startValue
);
1269 void wxGridCellBoolEditor::StartingClick()
1271 CBox()->SetValue(!CBox()->GetValue());
1274 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1276 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1278 int keycode
= event
.GetKeyCode();
1282 case WXK_NUMPAD_MULTIPLY
:
1284 case WXK_NUMPAD_ADD
:
1286 case WXK_NUMPAD_SUBTRACT
:
1297 #endif // wxUSE_CHECKBOX
1301 // ----------------------------------------------------------------------------
1302 // wxGridCellChoiceEditor
1303 // ----------------------------------------------------------------------------
1305 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1306 const wxString choices
[],
1308 : m_allowOthers(allowOthers
)
1312 m_choices
.Alloc(count
);
1313 for ( size_t n
= 0; n
< count
; n
++ )
1315 m_choices
.Add(choices
[n
]);
1320 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1322 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1323 editor
->m_allowOthers
= m_allowOthers
;
1324 editor
->m_choices
= m_choices
;
1329 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1331 wxEvtHandler
* evtHandler
)
1333 size_t count
= m_choices
.GetCount();
1334 wxString
*choices
= new wxString
[count
];
1335 for ( size_t n
= 0; n
< count
; n
++ )
1337 choices
[n
] = m_choices
[n
];
1340 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1341 wxDefaultPosition
, wxDefaultSize
,
1343 m_allowOthers
? 0 : wxCB_READONLY
);
1347 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1350 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1351 wxGridCellAttr
* attr
)
1353 // as we fill the entire client area, don't do anything here to minimize
1356 // TODO: It doesn't actually fill the client area since the height of a
1357 // combo always defaults to the standard... Until someone has time to
1358 // figure out the right rectangle to paint, just do it the normal way...
1359 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1362 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1364 wxASSERT_MSG(m_control
,
1365 wxT("The wxGridCellEditor must be Created first!"));
1367 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1369 Combo()->SetValue(m_startValue
);
1370 size_t count
= m_choices
.GetCount();
1371 for (size_t i
=0; i
<count
; i
++)
1373 if (m_startValue
== m_choices
[i
])
1375 Combo()->SetSelection(i
);
1379 Combo()->SetInsertionPointEnd();
1380 Combo()->SetFocus();
1383 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1386 wxString value
= Combo()->GetValue();
1387 bool changed
= value
!= m_startValue
;
1390 grid
->GetTable()->SetValue(row
, col
, value
);
1392 m_startValue
= wxEmptyString
;
1393 Combo()->SetValue(m_startValue
);
1398 void wxGridCellChoiceEditor::Reset()
1400 Combo()->SetValue(m_startValue
);
1401 Combo()->SetInsertionPointEnd();
1404 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1414 wxStringTokenizer
tk(params
, _T(','));
1415 while ( tk
.HasMoreTokens() )
1417 m_choices
.Add(tk
.GetNextToken());
1421 #endif // wxUSE_COMBOBOX
1423 // ----------------------------------------------------------------------------
1424 // wxGridCellEditorEvtHandler
1425 // ----------------------------------------------------------------------------
1427 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1429 switch ( event
.KeyCode() )
1433 m_grid
->DisableCellEditControl();
1437 m_grid
->GetEventHandler()->ProcessEvent( event
);
1441 case WXK_NUMPAD_ENTER
:
1442 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1443 m_editor
->HandleReturn(event
);
1452 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1454 switch ( event
.KeyCode() )
1459 case WXK_NUMPAD_ENTER
:
1467 // ----------------------------------------------------------------------------
1468 // wxGridCellWorker is an (almost) empty common base class for
1469 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1470 // ----------------------------------------------------------------------------
1472 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1477 wxGridCellWorker::~wxGridCellWorker()
1481 // ============================================================================
1483 // ============================================================================
1485 // ----------------------------------------------------------------------------
1486 // wxGridCellRenderer
1487 // ----------------------------------------------------------------------------
1489 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1490 wxGridCellAttr
& attr
,
1493 int WXUNUSED(row
), int WXUNUSED(col
),
1496 dc
.SetBackgroundMode( wxSOLID
);
1500 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1504 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1507 dc
.SetPen( *wxTRANSPARENT_PEN
);
1508 dc
.DrawRectangle(rect
);
1511 // ----------------------------------------------------------------------------
1512 // wxGridCellStringRenderer
1513 // ----------------------------------------------------------------------------
1515 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1516 wxGridCellAttr
& attr
,
1520 dc
.SetBackgroundMode( wxTRANSPARENT
);
1522 // TODO some special colours for attr.IsReadOnly() case?
1526 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1527 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1531 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1532 dc
.SetTextForeground( attr
.GetTextColour() );
1535 dc
.SetFont( attr
.GetFont() );
1538 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1540 const wxString
& text
)
1542 wxCoord x
= 0, y
= 0, max_x
= 0;
1543 dc
.SetFont(attr
.GetFont());
1544 wxStringTokenizer
tk(text
, _T('\n'));
1545 while ( tk
.HasMoreTokens() )
1547 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1548 max_x
= wxMax(max_x
, x
);
1551 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1553 return wxSize(max_x
, y
);
1556 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1557 wxGridCellAttr
& attr
,
1561 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1564 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1565 wxGridCellAttr
& attr
,
1567 const wxRect
& rectCell
,
1571 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1573 // now we only have to draw the text
1574 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1577 attr
.GetAlignment(&hAlign
, &vAlign
);
1579 wxRect rect
= rectCell
;
1582 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1583 rect
, hAlign
, vAlign
);
1586 // ----------------------------------------------------------------------------
1587 // wxGridCellNumberRenderer
1588 // ----------------------------------------------------------------------------
1590 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1592 wxGridTableBase
*table
= grid
.GetTable();
1594 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1596 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1600 text
= table
->GetValue(row
, col
);
1606 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1607 wxGridCellAttr
& attr
,
1609 const wxRect
& rectCell
,
1613 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1615 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1617 // draw the text right aligned by default
1619 attr
.GetAlignment(&hAlign
, &vAlign
);
1620 hAlign
= wxALIGN_RIGHT
;
1622 wxRect rect
= rectCell
;
1625 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1628 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1629 wxGridCellAttr
& attr
,
1633 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1636 // ----------------------------------------------------------------------------
1637 // wxGridCellFloatRenderer
1638 // ----------------------------------------------------------------------------
1640 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1643 SetPrecision(precision
);
1646 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1648 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1649 renderer
->m_width
= m_width
;
1650 renderer
->m_precision
= m_precision
;
1651 renderer
->m_format
= m_format
;
1656 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1658 wxGridTableBase
*table
= grid
.GetTable();
1663 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1665 val
= table
->GetValueAsDouble(row
, col
);
1670 text
= table
->GetValue(row
, col
);
1671 hasDouble
= text
.ToDouble(&val
);
1678 if ( m_width
== -1 )
1680 if ( m_precision
== -1 )
1682 // default width/precision
1683 m_format
= _T("%f");
1687 m_format
.Printf(_T("%%.%df"), m_precision
);
1690 else if ( m_precision
== -1 )
1692 // default precision
1693 m_format
.Printf(_T("%%%d.f"), m_width
);
1697 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1701 text
.Printf(m_format
, val
);
1704 //else: text already contains the string
1709 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1710 wxGridCellAttr
& attr
,
1712 const wxRect
& rectCell
,
1716 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1718 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1720 // draw the text right aligned by default
1722 attr
.GetAlignment(&hAlign
, &vAlign
);
1723 hAlign
= wxALIGN_RIGHT
;
1725 wxRect rect
= rectCell
;
1728 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1731 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1732 wxGridCellAttr
& attr
,
1736 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1739 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1743 // reset to defaults
1749 wxString tmp
= params
.BeforeFirst(_T(','));
1753 if ( tmp
.ToLong(&width
) )
1755 SetWidth((int)width
);
1759 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1763 tmp
= params
.AfterFirst(_T(','));
1767 if ( tmp
.ToLong(&precision
) )
1769 SetPrecision((int)precision
);
1773 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1781 // ----------------------------------------------------------------------------
1782 // wxGridCellBoolRenderer
1783 // ----------------------------------------------------------------------------
1785 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1787 // FIXME these checkbox size calculations are really ugly...
1789 // between checkmark and box
1790 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1792 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1793 wxGridCellAttr
& WXUNUSED(attr
),
1798 // compute it only once (no locks for MT safeness in GUI thread...)
1799 if ( !ms_sizeCheckMark
.x
)
1801 // get checkbox size
1802 wxCoord checkSize
= 0;
1803 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1804 wxSize size
= checkbox
->GetBestSize();
1805 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1807 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1808 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1809 checkSize
-= size
.y
/ 2;
1814 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1817 return ms_sizeCheckMark
;
1820 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1821 wxGridCellAttr
& attr
,
1827 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1829 // draw a check mark in the centre (ignoring alignment - TODO)
1830 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1832 // don't draw outside the cell
1833 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1834 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1836 // and even leave (at least) 1 pixel margin
1837 size
.x
= size
.y
= minSize
- 2;
1840 // draw a border around checkmark
1842 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1843 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1844 rectBorder
.width
= size
.x
;
1845 rectBorder
.height
= size
.y
;
1848 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1849 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1852 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1853 value
= !( !cellval
|| (cellval
== "0") );
1858 wxRect rectMark
= rectBorder
;
1860 // MSW DrawCheckMark() is weird (and should probably be changed...)
1861 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1865 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1868 dc
.SetTextForeground(attr
.GetTextColour());
1869 dc
.DrawCheckMark(rectMark
);
1872 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1873 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1874 dc
.DrawRectangle(rectBorder
);
1877 // ----------------------------------------------------------------------------
1879 // ----------------------------------------------------------------------------
1881 wxGridCellAttr
*wxGridCellAttr::Clone() const
1883 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1884 if ( HasTextColour() )
1885 attr
->SetTextColour(GetTextColour());
1886 if ( HasBackgroundColour() )
1887 attr
->SetBackgroundColour(GetBackgroundColour());
1889 attr
->SetFont(GetFont());
1890 if ( HasAlignment() )
1891 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1895 attr
->SetRenderer(m_renderer
);
1896 m_renderer
->IncRef();
1900 attr
->SetEditor(m_editor
);
1905 attr
->SetReadOnly();
1907 attr
->SetKind( m_attrkind
);
1909 attr
->SetDefAttr(m_defGridAttr
);
1914 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1916 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1917 SetTextColour(mergefrom
->GetTextColour());
1918 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1919 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1920 if ( !HasFont() && mergefrom
->HasFont() )
1921 SetFont(mergefrom
->GetFont());
1922 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1924 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1925 SetAlignment(hAlign
, vAlign
);
1928 // Directly access member functions as GetRender/Editor don't just return
1929 // m_renderer/m_editor
1931 // Maybe add support for merge of Render and Editor?
1932 if (!HasRenderer() && mergefrom
->HasRenderer() )
1934 m_renderer
= mergefrom
->m_renderer
;
1935 m_renderer
->IncRef();
1937 if ( !HasEditor() && mergefrom
->HasEditor() )
1939 m_editor
= mergefrom
->m_editor
;
1942 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1943 SetReadOnly(mergefrom
->IsReadOnly());
1945 SetDefAttr(mergefrom
->m_defGridAttr
);
1948 const wxColour
& wxGridCellAttr::GetTextColour() const
1950 if (HasTextColour())
1954 else if (m_defGridAttr
!= this)
1956 return m_defGridAttr
->GetTextColour();
1960 wxFAIL_MSG(wxT("Missing default cell attribute"));
1961 return wxNullColour
;
1966 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1968 if (HasBackgroundColour())
1970 else if (m_defGridAttr
!= this)
1971 return m_defGridAttr
->GetBackgroundColour();
1974 wxFAIL_MSG(wxT("Missing default cell attribute"));
1975 return wxNullColour
;
1980 const wxFont
& wxGridCellAttr::GetFont() const
1984 else if (m_defGridAttr
!= this)
1985 return m_defGridAttr
->GetFont();
1988 wxFAIL_MSG(wxT("Missing default cell attribute"));
1994 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1998 if ( hAlign
) *hAlign
= m_hAlign
;
1999 if ( vAlign
) *vAlign
= m_vAlign
;
2001 else if (m_defGridAttr
!= this)
2002 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2005 wxFAIL_MSG(wxT("Missing default cell attribute"));
2010 // GetRenderer and GetEditor use a slightly different decision path about
2011 // which attribute to use. If a non-default attr object has one then it is
2012 // used, otherwise the default editor or renderer is fetched from the grid and
2013 // used. It should be the default for the data type of the cell. If it is
2014 // NULL (because the table has a type that the grid does not have in its
2015 // registry,) then the grid's default editor or renderer is used.
2017 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2019 wxGridCellRenderer
* renderer
= NULL
;
2021 if ( m_defGridAttr
!= this || grid
== NULL
)
2023 renderer
= m_renderer
; // use local attribute
2028 if ( !renderer
&& grid
) // get renderer for the data type
2030 // GetDefaultRendererForCell() will do IncRef() for us
2031 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2036 // if we still don't have one then use the grid default
2037 // (no need for IncRef() here neither)
2038 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
2043 wxFAIL_MSG(wxT("Missing default cell attribute"));
2049 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2051 wxGridCellEditor
* editor
= NULL
;
2053 if ( m_defGridAttr
!= this || grid
== NULL
)
2055 editor
= m_editor
; // use local attribute
2060 if ( !editor
&& grid
) // get renderer for the data type
2061 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2064 // if we still don't have one then use the grid default
2065 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
2069 wxFAIL_MSG(wxT("Missing default cell attribute"));
2075 // ----------------------------------------------------------------------------
2076 // wxGridCellAttrData
2077 // ----------------------------------------------------------------------------
2079 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2081 int n
= FindIndex(row
, col
);
2082 if ( n
== wxNOT_FOUND
)
2084 // add the attribute
2085 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2091 // change the attribute
2092 m_attrs
[(size_t)n
].attr
= attr
;
2096 // remove this attribute
2097 m_attrs
.RemoveAt((size_t)n
);
2102 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2104 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2106 int n
= FindIndex(row
, col
);
2107 if ( n
!= wxNOT_FOUND
)
2109 attr
= m_attrs
[(size_t)n
].attr
;
2116 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2118 size_t count
= m_attrs
.GetCount();
2119 for ( size_t n
= 0; n
< count
; n
++ )
2121 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2122 wxCoord row
= coords
.GetRow();
2123 if ((size_t)row
>= pos
)
2127 // If rows inserted, include row counter where necessary
2128 coords
.SetRow(row
+ numRows
);
2130 else if (numRows
< 0)
2132 // If rows deleted ...
2133 if ((size_t)row
>= pos
- numRows
)
2135 // ...either decrement row counter (if row still exists)...
2136 coords
.SetRow(row
+ numRows
);
2140 // ...or remove the attribute
2141 m_attrs
.RemoveAt((size_t)n
);
2149 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2151 size_t count
= m_attrs
.GetCount();
2152 for ( size_t n
= 0; n
< count
; n
++ )
2154 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2155 wxCoord col
= coords
.GetCol();
2156 if ( (size_t)col
>= pos
)
2160 // If rows inserted, include row counter where necessary
2161 coords
.SetCol(col
+ numCols
);
2163 else if (numCols
< 0)
2165 // If rows deleted ...
2166 if ((size_t)col
>= pos
- numCols
)
2168 // ...either decrement row counter (if row still exists)...
2169 coords
.SetCol(col
+ numCols
);
2173 // ...or remove the attribute
2174 m_attrs
.RemoveAt((size_t)n
);
2182 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2184 size_t count
= m_attrs
.GetCount();
2185 for ( size_t n
= 0; n
< count
; n
++ )
2187 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2188 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2197 // ----------------------------------------------------------------------------
2198 // wxGridRowOrColAttrData
2199 // ----------------------------------------------------------------------------
2201 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2203 size_t count
= m_attrs
.Count();
2204 for ( size_t n
= 0; n
< count
; n
++ )
2206 m_attrs
[n
]->DecRef();
2210 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2212 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2214 int n
= m_rowsOrCols
.Index(rowOrCol
);
2215 if ( n
!= wxNOT_FOUND
)
2217 attr
= m_attrs
[(size_t)n
];
2224 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2226 int i
= m_rowsOrCols
.Index(rowOrCol
);
2227 if ( i
== wxNOT_FOUND
)
2229 // add the attribute
2230 m_rowsOrCols
.Add(rowOrCol
);
2235 size_t n
= (size_t)i
;
2238 // change the attribute
2239 m_attrs
[n
]->DecRef();
2244 // remove this attribute
2245 m_attrs
[n
]->DecRef();
2246 m_rowsOrCols
.RemoveAt(n
);
2247 m_attrs
.RemoveAt(n
);
2252 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2254 size_t count
= m_attrs
.GetCount();
2255 for ( size_t n
= 0; n
< count
; n
++ )
2257 int & rowOrCol
= m_rowsOrCols
[n
];
2258 if ( (size_t)rowOrCol
>= pos
)
2260 if ( numRowsOrCols
> 0 )
2262 // If rows inserted, include row counter where necessary
2263 rowOrCol
+= numRowsOrCols
;
2265 else if ( numRowsOrCols
< 0)
2267 // If rows deleted, either decrement row counter (if row still exists)
2268 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2269 rowOrCol
+= numRowsOrCols
;
2272 m_rowsOrCols
.RemoveAt((size_t)n
);
2273 m_attrs
.RemoveAt((size_t)n
);
2281 // ----------------------------------------------------------------------------
2282 // wxGridCellAttrProvider
2283 // ----------------------------------------------------------------------------
2285 wxGridCellAttrProvider::wxGridCellAttrProvider()
2287 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2290 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2295 void wxGridCellAttrProvider::InitData()
2297 m_data
= new wxGridCellAttrProviderData
;
2300 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2301 wxGridCellAttr::wxAttrKind kind
) const
2303 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2308 case (wxGridCellAttr::Any
):
2309 //Get cached merge attributes.
2310 // Currenlty not used as no cache implemented as not mutiable
2311 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2314 //Basicaly implement old version.
2315 //Also check merge cache, so we don't have to re-merge every time..
2316 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2317 *attrrow
= (wxGridCellAttr
*)NULL
,
2318 *attrcol
= (wxGridCellAttr
*)NULL
;
2320 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2321 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2322 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2324 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2325 // Two or move are non NULL
2326 attr
= new wxGridCellAttr
;
2327 attr
->SetKind(wxGridCellAttr::Merged
);
2331 attr
->MergeWith(attrcell
);
2335 attr
->MergeWith(attrcol
);
2339 attr
->MergeWith(attrrow
);
2342 //store merge attr if cache implemented
2344 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2348 // one or none is non null return it or null.
2349 if(attrrow
) attr
= attrrow
;
2350 if(attrcol
) attr
= attrcol
;
2351 if(attrcell
) attr
= attrcell
;
2355 case (wxGridCellAttr::Cell
):
2356 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2358 case (wxGridCellAttr::Col
):
2359 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2361 case (wxGridCellAttr::Row
):
2362 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2366 // (wxGridCellAttr::Default):
2367 // (wxGridCellAttr::Merged):
2374 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2380 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2383 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2388 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2391 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2396 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2399 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2403 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2405 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2409 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2413 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2415 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2419 // ----------------------------------------------------------------------------
2420 // wxGridTypeRegistry
2421 // ----------------------------------------------------------------------------
2423 wxGridTypeRegistry::~wxGridTypeRegistry()
2425 size_t count
= m_typeinfo
.Count();
2426 for ( size_t i
= 0; i
< count
; i
++ )
2427 delete m_typeinfo
[i
];
2431 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2432 wxGridCellRenderer
* renderer
,
2433 wxGridCellEditor
* editor
)
2435 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2437 // is it already registered?
2438 int loc
= FindRegisteredDataType(typeName
);
2439 if ( loc
!= wxNOT_FOUND
)
2441 delete m_typeinfo
[loc
];
2442 m_typeinfo
[loc
] = info
;
2446 m_typeinfo
.Add(info
);
2450 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2452 size_t count
= m_typeinfo
.GetCount();
2453 for ( size_t i
= 0; i
< count
; i
++ )
2455 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2464 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2466 int index
= FindRegisteredDataType(typeName
);
2467 if ( index
== wxNOT_FOUND
)
2469 // check whether this is one of the standard ones, in which case
2470 // register it "on the fly"
2472 if ( typeName
== wxGRID_VALUE_STRING
)
2474 RegisterDataType(wxGRID_VALUE_STRING
,
2475 new wxGridCellStringRenderer
,
2476 new wxGridCellTextEditor
);
2478 #endif // wxUSE_TEXTCTRL
2480 if ( typeName
== wxGRID_VALUE_BOOL
)
2482 RegisterDataType(wxGRID_VALUE_BOOL
,
2483 new wxGridCellBoolRenderer
,
2484 new wxGridCellBoolEditor
);
2486 #endif // wxUSE_CHECKBOX
2488 if ( typeName
== wxGRID_VALUE_NUMBER
)
2490 RegisterDataType(wxGRID_VALUE_NUMBER
,
2491 new wxGridCellNumberRenderer
,
2492 new wxGridCellNumberEditor
);
2494 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2496 RegisterDataType(wxGRID_VALUE_FLOAT
,
2497 new wxGridCellFloatRenderer
,
2498 new wxGridCellFloatEditor
);
2500 #endif // wxUSE_TEXTCTRL
2502 if ( typeName
== wxGRID_VALUE_CHOICE
)
2504 RegisterDataType(wxGRID_VALUE_CHOICE
,
2505 new wxGridCellStringRenderer
,
2506 new wxGridCellChoiceEditor
);
2508 #endif // wxUSE_COMBOBOX
2513 // we get here only if just added the entry for this type, so return
2515 index
= m_typeinfo
.GetCount() - 1;
2521 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2523 int index
= FindDataType(typeName
);
2524 if ( index
== wxNOT_FOUND
)
2526 // the first part of the typename is the "real" type, anything after ':'
2527 // are the parameters for the renderer
2528 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2529 if ( index
== wxNOT_FOUND
)
2534 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2535 wxGridCellRenderer
*rendererOld
= renderer
;
2536 renderer
= renderer
->Clone();
2537 rendererOld
->DecRef();
2539 wxGridCellEditor
*editor
= GetEditor(index
);
2540 wxGridCellEditor
*editorOld
= editor
;
2541 editor
= editor
->Clone();
2542 editorOld
->DecRef();
2544 // do it even if there are no parameters to reset them to defaults
2545 wxString params
= typeName
.AfterFirst(_T(':'));
2546 renderer
->SetParameters(params
);
2547 editor
->SetParameters(params
);
2549 // register the new typename
2550 RegisterDataType(typeName
, renderer
, editor
);
2552 // we just registered it, it's the last one
2553 index
= m_typeinfo
.GetCount() - 1;
2559 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2561 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2567 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2569 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2575 // ----------------------------------------------------------------------------
2577 // ----------------------------------------------------------------------------
2579 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2582 wxGridTableBase::wxGridTableBase()
2584 m_view
= (wxGrid
*) NULL
;
2585 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2588 wxGridTableBase::~wxGridTableBase()
2590 delete m_attrProvider
;
2593 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2595 delete m_attrProvider
;
2596 m_attrProvider
= attrProvider
;
2599 bool wxGridTableBase::CanHaveAttributes()
2601 if ( ! GetAttrProvider() )
2603 // use the default attr provider by default
2604 SetAttrProvider(new wxGridCellAttrProvider
);
2609 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2611 if ( m_attrProvider
)
2612 return m_attrProvider
->GetAttr(row
, col
, kind
);
2614 return (wxGridCellAttr
*)NULL
;
2617 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2619 if ( m_attrProvider
)
2621 attr
->SetKind(wxGridCellAttr::Cell
);
2622 m_attrProvider
->SetAttr(attr
, row
, col
);
2626 // as we take ownership of the pointer and don't store it, we must
2632 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2634 if ( m_attrProvider
)
2636 attr
->SetKind(wxGridCellAttr::Row
);
2637 m_attrProvider
->SetRowAttr(attr
, row
);
2641 // as we take ownership of the pointer and don't store it, we must
2647 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2649 if ( m_attrProvider
)
2651 attr
->SetKind(wxGridCellAttr::Col
);
2652 m_attrProvider
->SetColAttr(attr
, col
);
2656 // as we take ownership of the pointer and don't store it, we must
2662 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2663 size_t WXUNUSED(numRows
) )
2665 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2670 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2672 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2677 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2678 size_t WXUNUSED(numRows
) )
2680 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2685 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2686 size_t WXUNUSED(numCols
) )
2688 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2693 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2695 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2700 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2701 size_t WXUNUSED(numCols
) )
2703 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2709 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2712 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2713 // how much it makes sense to us geeks.
2717 wxString
wxGridTableBase::GetColLabelValue( int col
)
2719 // default col labels are:
2720 // cols 0 to 25 : A-Z
2721 // cols 26 to 675 : AA-ZZ
2726 for ( n
= 1; ; n
++ )
2728 s
+= (_T('A') + (wxChar
)( col%26
));
2730 if ( col
< 0 ) break;
2733 // reverse the string...
2735 for ( i
= 0; i
< n
; i
++ )
2744 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2746 return wxGRID_VALUE_STRING
;
2749 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2750 const wxString
& typeName
)
2752 return typeName
== wxGRID_VALUE_STRING
;
2755 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2757 return CanGetValueAs(row
, col
, typeName
);
2760 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2765 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2770 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2775 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2776 long WXUNUSED(value
) )
2780 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2781 double WXUNUSED(value
) )
2785 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2786 bool WXUNUSED(value
) )
2791 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2792 const wxString
& WXUNUSED(typeName
) )
2797 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2798 const wxString
& WXUNUSED(typeName
),
2799 void* WXUNUSED(value
) )
2803 //////////////////////////////////////////////////////////////////////
2805 // Message class for the grid table to send requests and notifications
2809 wxGridTableMessage::wxGridTableMessage()
2811 m_table
= (wxGridTableBase
*) NULL
;
2817 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2818 int commandInt1
, int commandInt2
)
2822 m_comInt1
= commandInt1
;
2823 m_comInt2
= commandInt2
;
2828 //////////////////////////////////////////////////////////////////////
2830 // A basic grid table for string data. An object of this class will
2831 // created by wxGrid if you don't specify an alternative table class.
2834 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2836 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2838 wxGridStringTable::wxGridStringTable()
2843 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2848 m_data
.Alloc( numRows
);
2851 sa
.Alloc( numCols
);
2852 for ( col
= 0; col
< numCols
; col
++ )
2854 sa
.Add( wxEmptyString
);
2857 for ( row
= 0; row
< numRows
; row
++ )
2863 wxGridStringTable::~wxGridStringTable()
2867 int wxGridStringTable::GetNumberRows()
2869 return m_data
.GetCount();
2872 int wxGridStringTable::GetNumberCols()
2874 if ( m_data
.GetCount() > 0 )
2875 return m_data
[0].GetCount();
2880 wxString
wxGridStringTable::GetValue( int row
, int col
)
2882 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2883 _T("invalid row or column index in wxGridStringTable") );
2885 return m_data
[row
][col
];
2888 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2890 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2891 _T("invalid row or column index in wxGridStringTable") );
2893 m_data
[row
][col
] = value
;
2896 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2898 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2899 _T("invalid row or column index in wxGridStringTable") );
2901 return (m_data
[row
][col
] == wxEmptyString
);
2904 void wxGridStringTable::Clear()
2907 int numRows
, numCols
;
2909 numRows
= m_data
.GetCount();
2912 numCols
= m_data
[0].GetCount();
2914 for ( row
= 0; row
< numRows
; row
++ )
2916 for ( col
= 0; col
< numCols
; col
++ )
2918 m_data
[row
][col
] = wxEmptyString
;
2925 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2929 size_t curNumRows
= m_data
.GetCount();
2930 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2931 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2933 if ( pos
>= curNumRows
)
2935 return AppendRows( numRows
);
2939 sa
.Alloc( curNumCols
);
2940 for ( col
= 0; col
< curNumCols
; col
++ )
2942 sa
.Add( wxEmptyString
);
2945 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2947 m_data
.Insert( sa
, row
);
2951 wxGridTableMessage
msg( this,
2952 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2956 GetView()->ProcessTableMessage( msg
);
2962 bool wxGridStringTable::AppendRows( size_t numRows
)
2966 size_t curNumRows
= m_data
.GetCount();
2967 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2968 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2971 if ( curNumCols
> 0 )
2973 sa
.Alloc( curNumCols
);
2974 for ( col
= 0; col
< curNumCols
; col
++ )
2976 sa
.Add( wxEmptyString
);
2980 for ( row
= 0; row
< numRows
; row
++ )
2987 wxGridTableMessage
msg( this,
2988 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2991 GetView()->ProcessTableMessage( msg
);
2997 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3001 size_t curNumRows
= m_data
.GetCount();
3003 if ( pos
>= curNumRows
)
3006 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3007 pos
, numRows
, curNumRows
);
3008 wxFAIL_MSG( errmsg
);
3012 if ( numRows
> curNumRows
- pos
)
3014 numRows
= curNumRows
- pos
;
3017 if ( numRows
>= curNumRows
)
3019 m_data
.Empty(); // don't release memory just yet
3023 for ( n
= 0; n
< numRows
; n
++ )
3025 m_data
.Remove( pos
);
3030 wxGridTableMessage
msg( this,
3031 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3035 GetView()->ProcessTableMessage( msg
);
3041 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3045 size_t curNumRows
= m_data
.GetCount();
3046 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3047 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3049 if ( pos
>= curNumCols
)
3051 return AppendCols( numCols
);
3054 for ( row
= 0; row
< curNumRows
; row
++ )
3056 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3058 m_data
[row
].Insert( wxEmptyString
, col
);
3063 wxGridTableMessage
msg( this,
3064 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3068 GetView()->ProcessTableMessage( msg
);
3074 bool wxGridStringTable::AppendCols( size_t numCols
)
3078 size_t curNumRows
= m_data
.GetCount();
3082 // TODO: something better than this ?
3084 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3089 for ( row
= 0; row
< curNumRows
; row
++ )
3091 for ( n
= 0; n
< numCols
; n
++ )
3093 m_data
[row
].Add( wxEmptyString
);
3099 wxGridTableMessage
msg( this,
3100 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3103 GetView()->ProcessTableMessage( msg
);
3109 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3113 size_t curNumRows
= m_data
.GetCount();
3114 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3115 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3117 if ( pos
>= curNumCols
)
3120 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3121 pos
, numCols
, curNumCols
);
3122 wxFAIL_MSG( errmsg
);
3126 if ( numCols
> curNumCols
- pos
)
3128 numCols
= curNumCols
- pos
;
3131 for ( row
= 0; row
< curNumRows
; row
++ )
3133 if ( numCols
>= curNumCols
)
3135 m_data
[row
].Clear();
3139 for ( n
= 0; n
< numCols
; n
++ )
3141 m_data
[row
].Remove( pos
);
3147 wxGridTableMessage
msg( this,
3148 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3152 GetView()->ProcessTableMessage( msg
);
3158 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3160 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3162 // using default label
3164 return wxGridTableBase::GetRowLabelValue( row
);
3168 return m_rowLabels
[ row
];
3172 wxString
wxGridStringTable::GetColLabelValue( int col
)
3174 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3176 // using default label
3178 return wxGridTableBase::GetColLabelValue( col
);
3182 return m_colLabels
[ col
];
3186 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3188 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3190 int n
= m_rowLabels
.GetCount();
3192 for ( i
= n
; i
<= row
; i
++ )
3194 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3198 m_rowLabels
[row
] = value
;
3201 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3203 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3205 int n
= m_colLabels
.GetCount();
3207 for ( i
= n
; i
<= col
; i
++ )
3209 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3213 m_colLabels
[col
] = value
;
3218 //////////////////////////////////////////////////////////////////////
3219 //////////////////////////////////////////////////////////////////////
3221 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3223 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3224 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3225 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3226 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3227 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3228 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3231 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3233 const wxPoint
&pos
, const wxSize
&size
)
3234 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3239 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3243 // NO - don't do this because it will set both the x and y origin
3244 // coords to match the parent scrolled window and we just want to
3245 // set the y coord - MB
3247 // m_owner->PrepareDC( dc );
3250 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3251 dc
.SetDeviceOrigin( 0, -y
);
3253 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3254 m_owner
->DrawRowLabels( dc
, rows
);
3258 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3260 m_owner
->ProcessRowLabelMouseEvent( event
);
3264 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3266 m_owner
->GetEventHandler()->ProcessEvent(event
);
3270 // This seems to be required for wxMotif otherwise the mouse
3271 // cursor must be in the cell edit control to get key events
3273 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3275 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3278 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3280 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3285 //////////////////////////////////////////////////////////////////////
3287 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3289 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3290 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3291 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3292 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3293 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3294 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3297 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3299 const wxPoint
&pos
, const wxSize
&size
)
3300 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3305 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3309 // NO - don't do this because it will set both the x and y origin
3310 // coords to match the parent scrolled window and we just want to
3311 // set the x coord - MB
3313 // m_owner->PrepareDC( dc );
3316 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3317 dc
.SetDeviceOrigin( -x
, 0 );
3319 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3320 m_owner
->DrawColLabels( dc
, cols
);
3324 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3326 m_owner
->ProcessColLabelMouseEvent( event
);
3329 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3331 m_owner
->GetEventHandler()->ProcessEvent(event
);
3335 // This seems to be required for wxMotif otherwise the mouse
3336 // cursor must be in the cell edit control to get key events
3338 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3340 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3343 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3345 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3350 //////////////////////////////////////////////////////////////////////
3352 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3354 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3355 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3356 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3357 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3358 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3359 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3362 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3364 const wxPoint
&pos
, const wxSize
&size
)
3365 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3370 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3374 int client_height
= 0;
3375 int client_width
= 0;
3376 GetClientSize( &client_width
, &client_height
);
3378 dc
.SetPen( *wxBLACK_PEN
);
3379 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3380 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3382 dc
.SetPen( *wxWHITE_PEN
);
3383 dc
.DrawLine( 0, 0, client_width
, 0 );
3384 dc
.DrawLine( 0, 0, 0, client_height
);
3388 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3390 m_owner
->ProcessCornerLabelMouseEvent( event
);
3394 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3396 m_owner
->GetEventHandler()->ProcessEvent(event
);
3399 // This seems to be required for wxMotif otherwise the mouse
3400 // cursor must be in the cell edit control to get key events
3402 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3404 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3407 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3409 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3414 //////////////////////////////////////////////////////////////////////
3416 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3418 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3419 EVT_PAINT( wxGridWindow::OnPaint
)
3420 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3421 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3422 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3423 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3424 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3427 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3428 wxGridRowLabelWindow
*rowLblWin
,
3429 wxGridColLabelWindow
*colLblWin
,
3430 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3431 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3434 m_rowLabelWin
= rowLblWin
;
3435 m_colLabelWin
= colLblWin
;
3436 SetBackgroundColour( "WHITE" );
3440 wxGridWindow::~wxGridWindow()
3445 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3447 wxPaintDC
dc( this );
3448 m_owner
->PrepareDC( dc
);
3449 wxRegion reg
= GetUpdateRegion();
3450 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3451 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3452 #if WXGRID_DRAW_LINES
3453 m_owner
->DrawAllGridLines( dc
, reg
);
3455 m_owner
->DrawGridSpace( dc
);
3456 m_owner
->DrawHighlight( dc
, DirtyCells
);
3460 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3462 wxPanel::ScrollWindow( dx
, dy
, rect
);
3463 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3464 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3468 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3470 m_owner
->ProcessGridCellMouseEvent( event
);
3473 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3475 m_owner
->GetEventHandler()->ProcessEvent(event
);
3478 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3479 // cursor must be in the cell edit control to get key events
3481 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3483 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3486 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3488 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3491 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3496 //////////////////////////////////////////////////////////////////////
3499 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3501 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3502 EVT_PAINT( wxGrid::OnPaint
)
3503 EVT_SIZE( wxGrid::OnSize
)
3504 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3505 EVT_KEY_UP( wxGrid::OnKeyUp
)
3506 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3509 wxGrid::wxGrid( wxWindow
*parent
,
3514 const wxString
& name
)
3515 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3516 m_colMinWidths(GRID_HASH_SIZE
),
3517 m_rowMinHeights(GRID_HASH_SIZE
)
3525 // Must do this or ~wxScrollHelper will pop the wrong event handler
3526 SetTargetWindow(this);
3528 wxSafeDecRef(m_defaultCellAttr
);
3530 #ifdef DEBUG_ATTR_CACHE
3531 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3532 wxPrintf(_T("wxGrid attribute cache statistics: "
3533 "total: %u, hits: %u (%u%%)\n"),
3534 total
, gs_nAttrCacheHits
,
3535 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3541 delete m_typeRegistry
;
3547 // ----- internal init and update functions
3550 void wxGrid::Create()
3552 m_created
= FALSE
; // set to TRUE by CreateGrid
3554 m_table
= (wxGridTableBase
*) NULL
;
3557 m_cellEditCtrlEnabled
= FALSE
;
3559 m_defaultCellAttr
= new wxGridCellAttr
;
3560 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3562 // Set default cell attributes
3563 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3564 m_defaultCellAttr
->SetFont(GetFont());
3565 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3566 m_defaultCellAttr
->SetTextColour(
3567 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3568 m_defaultCellAttr
->SetBackgroundColour(
3569 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3570 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3571 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3576 m_currentCellCoords
= wxGridNoCellCoords
;
3578 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3579 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3581 // create the type registry
3582 m_typeRegistry
= new wxGridTypeRegistry
;
3584 // subwindow components that make up the wxGrid
3585 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3590 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3595 m_colLabelWin
= new wxGridColLabelWindow( this,
3600 m_gridWin
= new wxGridWindow( this,
3607 SetTargetWindow( m_gridWin
);
3611 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3612 wxGrid::wxGridSelectionModes selmode
)
3614 wxCHECK_MSG( !m_created
,
3616 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3618 m_numRows
= numRows
;
3619 m_numCols
= numCols
;
3621 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3622 m_table
->SetView( this );
3624 m_selection
= new wxGridSelection( this, selmode
);
3631 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3635 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3638 m_selection
->SetSelectionMode( selmode
);
3641 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3642 wxGrid::wxGridSelectionModes selmode
)
3646 // RD: Actually, this should probably be allowed. I think it would be
3647 // nice to be able to switch multiple Tables in and out of a single
3648 // View at runtime. Is there anything in the implmentation that would
3651 // At least, you now have to cope with m_selection
3652 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3657 m_numRows
= table
->GetNumberRows();
3658 m_numCols
= table
->GetNumberCols();
3661 m_table
->SetView( this );
3664 m_selection
= new wxGridSelection( this, selmode
);
3675 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3676 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3678 if ( m_rowLabelWin
)
3680 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3684 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3687 m_labelTextColour
= wxColour( _T("BLACK") );
3690 m_attrCache
.row
= -1;
3692 // TODO: something better than this ?
3694 m_labelFont
= this->GetFont();
3695 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3697 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3698 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3700 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3701 m_colLabelVertAlign
= wxALIGN_TOP
;
3703 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3704 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3706 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3707 m_defaultRowHeight
+= 8;
3709 m_defaultRowHeight
+= 4;
3712 m_gridLineColour
= wxColour( 128, 128, 255 );
3713 m_gridLinesEnabled
= TRUE
;
3714 m_cellHighlightColour
= m_gridLineColour
;
3715 m_cellHighlightPenWidth
= 2;
3716 m_cellHighlightROPenWidth
= 1;
3718 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3719 m_winCapture
= (wxWindow
*)NULL
;
3720 m_canDragRowSize
= TRUE
;
3721 m_canDragColSize
= TRUE
;
3722 m_canDragGridSize
= TRUE
;
3724 m_dragRowOrCol
= -1;
3725 m_isDragging
= FALSE
;
3726 m_startDragPos
= wxDefaultPosition
;
3728 m_waitForSlowClick
= FALSE
;
3730 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3731 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3733 m_currentCellCoords
= wxGridNoCellCoords
;
3735 m_selectingTopLeft
= wxGridNoCellCoords
;
3736 m_selectingBottomRight
= wxGridNoCellCoords
;
3737 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3738 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3740 m_editable
= TRUE
; // default for whole grid
3742 m_inOnKeyDown
= FALSE
;
3751 // ----------------------------------------------------------------------------
3752 // the idea is to call these functions only when necessary because they create
3753 // quite big arrays which eat memory mostly unnecessary - in particular, if
3754 // default widths/heights are used for all rows/columns, we may not use these
3757 // with some extra code, it should be possible to only store the
3758 // widths/heights different from default ones but this will be done later...
3759 // ----------------------------------------------------------------------------
3761 void wxGrid::InitRowHeights()
3763 m_rowHeights
.Empty();
3764 m_rowBottoms
.Empty();
3766 m_rowHeights
.Alloc( m_numRows
);
3767 m_rowBottoms
.Alloc( m_numRows
);
3770 for ( int i
= 0; i
< m_numRows
; i
++ )
3772 m_rowHeights
.Add( m_defaultRowHeight
);
3773 rowBottom
+= m_defaultRowHeight
;
3774 m_rowBottoms
.Add( rowBottom
);
3778 void wxGrid::InitColWidths()
3780 m_colWidths
.Empty();
3781 m_colRights
.Empty();
3783 m_colWidths
.Alloc( m_numCols
);
3784 m_colRights
.Alloc( m_numCols
);
3786 for ( int i
= 0; i
< m_numCols
; i
++ )
3788 m_colWidths
.Add( m_defaultColWidth
);
3789 colRight
+= m_defaultColWidth
;
3790 m_colRights
.Add( colRight
);
3794 int wxGrid::GetColWidth(int col
) const
3796 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3799 int wxGrid::GetColLeft(int col
) const
3801 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3802 : m_colRights
[col
] - m_colWidths
[col
];
3805 int wxGrid::GetColRight(int col
) const
3807 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3811 int wxGrid::GetRowHeight(int row
) const
3813 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3816 int wxGrid::GetRowTop(int row
) const
3818 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3819 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3822 int wxGrid::GetRowBottom(int row
) const
3824 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3825 : m_rowBottoms
[row
];
3828 void wxGrid::CalcDimensions()
3831 GetClientSize( &cw
, &ch
);
3833 if ( m_colLabelWin
->IsShown() )
3834 cw
-= m_rowLabelWidth
;
3835 if ( m_rowLabelWin
->IsShown() )
3836 ch
-= m_colLabelHeight
;
3839 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3840 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3842 // preserve (more or less) the previous position
3844 GetViewStart( &x
, &y
);
3845 // maybe we don't need scrollbars at all? and if we do, transform w and h
3846 // from pixels into logical units
3853 w
= (w
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3863 h
= (h
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3868 // do set scrollbar parameters
3869 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3870 w
, h
, x
, y
, (GetBatchCount() != 0));
3874 void wxGrid::CalcWindowSizes()
3877 GetClientSize( &cw
, &ch
);
3879 if ( m_cornerLabelWin
->IsShown() )
3880 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3882 if ( m_colLabelWin
->IsShown() )
3883 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3885 if ( m_rowLabelWin
->IsShown() )
3886 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3888 if ( m_gridWin
->IsShown() )
3889 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3893 // this is called when the grid table sends a message to say that it
3894 // has been redimensioned
3896 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3899 bool result
= FALSE
;
3902 // if we were using the default widths/heights so far, we must change them
3904 if ( m_colWidths
.IsEmpty() )
3909 if ( m_rowHeights
.IsEmpty() )
3915 switch ( msg
.GetId() )
3917 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3919 size_t pos
= msg
.GetCommandInt();
3920 int numRows
= msg
.GetCommandInt2();
3922 m_numRows
+= numRows
;
3924 if ( !m_rowHeights
.IsEmpty() )
3926 for ( i
= 0; i
< numRows
; i
++ )
3928 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3929 m_rowBottoms
.Insert( 0, pos
);
3933 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3935 for ( i
= pos
; i
< m_numRows
; i
++ )
3937 bottom
+= m_rowHeights
[i
];
3938 m_rowBottoms
[i
] = bottom
;
3941 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3943 // if we have just inserted cols into an empty grid the current
3944 // cell will be undefined...
3946 SetCurrentCell( 0, 0 );
3948 m_selection
->UpdateRows( pos
, numRows
);
3949 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3951 attrProvider
->UpdateAttrRows( pos
, numRows
);
3953 if ( !GetBatchCount() )
3956 m_rowLabelWin
->Refresh();
3962 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3964 int numRows
= msg
.GetCommandInt();
3965 int oldNumRows
= m_numRows
;
3966 m_numRows
+= numRows
;
3968 if ( !m_rowHeights
.IsEmpty() )
3970 for ( i
= 0; i
< numRows
; i
++ )
3972 m_rowHeights
.Add( m_defaultRowHeight
);
3973 m_rowBottoms
.Add( 0 );
3977 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3979 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3981 bottom
+= m_rowHeights
[i
];
3982 m_rowBottoms
[i
] = bottom
;
3985 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3987 // if we have just inserted cols into an empty grid the current
3988 // cell will be undefined...
3990 SetCurrentCell( 0, 0 );
3992 if ( !GetBatchCount() )
3995 m_rowLabelWin
->Refresh();
4001 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4003 size_t pos
= msg
.GetCommandInt();
4004 int numRows
= msg
.GetCommandInt2();
4005 m_numRows
-= numRows
;
4007 if ( !m_rowHeights
.IsEmpty() )
4009 for ( i
= 0; i
< numRows
; i
++ )
4011 m_rowHeights
.Remove( pos
);
4012 m_rowBottoms
.Remove( pos
);
4016 for ( i
= 0; i
< m_numRows
; i
++ )
4018 h
+= m_rowHeights
[i
];
4019 m_rowBottoms
[i
] = h
;
4024 m_currentCellCoords
= wxGridNoCellCoords
;
4028 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4029 m_currentCellCoords
.Set( 0, 0 );
4031 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4032 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4034 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4035 // ifdef'd out following patch from Paul Gammans
4037 // No need to touch column attributes, unless we
4038 // removed _all_ rows, in this case, we remove
4039 // all column attributes.
4040 // I hate to do this here, but the
4041 // needed data is not available inside UpdateAttrRows.
4042 if ( !GetNumberRows() )
4043 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4046 if ( !GetBatchCount() )
4049 m_rowLabelWin
->Refresh();
4055 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4057 size_t pos
= msg
.GetCommandInt();
4058 int numCols
= msg
.GetCommandInt2();
4059 m_numCols
+= numCols
;
4061 if ( !m_colWidths
.IsEmpty() )
4063 for ( i
= 0; i
< numCols
; i
++ )
4065 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4066 m_colRights
.Insert( 0, pos
);
4070 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4072 for ( i
= pos
; i
< m_numCols
; i
++ )
4074 right
+= m_colWidths
[i
];
4075 m_colRights
[i
] = right
;
4078 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4080 // if we have just inserted cols into an empty grid the current
4081 // cell will be undefined...
4083 SetCurrentCell( 0, 0 );
4085 m_selection
->UpdateCols( pos
, numCols
);
4086 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4088 attrProvider
->UpdateAttrCols( pos
, numCols
);
4089 if ( !GetBatchCount() )
4092 m_colLabelWin
->Refresh();
4099 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4101 int numCols
= msg
.GetCommandInt();
4102 int oldNumCols
= m_numCols
;
4103 m_numCols
+= numCols
;
4104 if ( !m_colWidths
.IsEmpty() )
4106 for ( i
= 0; i
< numCols
; i
++ )
4108 m_colWidths
.Add( m_defaultColWidth
);
4109 m_colRights
.Add( 0 );
4113 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4115 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4117 right
+= m_colWidths
[i
];
4118 m_colRights
[i
] = right
;
4121 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4123 // if we have just inserted cols into an empty grid the current
4124 // cell will be undefined...
4126 SetCurrentCell( 0, 0 );
4128 if ( !GetBatchCount() )
4131 m_colLabelWin
->Refresh();
4137 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4139 size_t pos
= msg
.GetCommandInt();
4140 int numCols
= msg
.GetCommandInt2();
4141 m_numCols
-= numCols
;
4143 if ( !m_colWidths
.IsEmpty() )
4145 for ( i
= 0; i
< numCols
; i
++ )
4147 m_colWidths
.Remove( pos
);
4148 m_colRights
.Remove( pos
);
4152 for ( i
= 0; i
< m_numCols
; i
++ )
4154 w
+= m_colWidths
[i
];
4160 m_currentCellCoords
= wxGridNoCellCoords
;
4164 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4165 m_currentCellCoords
.Set( 0, 0 );
4167 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4168 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4170 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4171 // ifdef'd out following patch from Paul Gammans
4173 // No need to touch row attributes, unless we
4174 // removed _all_ columns, in this case, we remove
4175 // all row attributes.
4176 // I hate to do this here, but the
4177 // needed data is not available inside UpdateAttrCols.
4178 if ( !GetNumberCols() )
4179 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4182 if ( !GetBatchCount() )
4185 m_colLabelWin
->Refresh();
4192 if (result
&& !GetBatchCount() )
4193 m_gridWin
->Refresh();
4198 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4200 wxRegionIterator
iter( reg
);
4203 wxArrayInt rowlabels
;
4210 // TODO: remove this when we can...
4211 // There is a bug in wxMotif that gives garbage update
4212 // rectangles if you jump-scroll a long way by clicking the
4213 // scrollbar with middle button. This is a work-around
4215 #if defined(__WXMOTIF__)
4217 m_gridWin
->GetClientSize( &cw
, &ch
);
4218 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4219 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4222 // logical bounds of update region
4225 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4226 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4228 // find the row labels within these bounds
4231 for ( row
= 0; row
< m_numRows
; row
++ )
4233 if ( GetRowBottom(row
) < top
)
4236 if ( GetRowTop(row
) > bottom
)
4239 rowlabels
.Add( row
);
4249 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4251 wxRegionIterator
iter( reg
);
4254 wxArrayInt colLabels
;
4261 // TODO: remove this when we can...
4262 // There is a bug in wxMotif that gives garbage update
4263 // rectangles if you jump-scroll a long way by clicking the
4264 // scrollbar with middle button. This is a work-around
4266 #if defined(__WXMOTIF__)
4268 m_gridWin
->GetClientSize( &cw
, &ch
);
4269 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4270 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4273 // logical bounds of update region
4276 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4277 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4279 // find the cells within these bounds
4282 for ( col
= 0; col
< m_numCols
; col
++ )
4284 if ( GetColRight(col
) < left
)
4287 if ( GetColLeft(col
) > right
)
4290 colLabels
.Add( col
);
4299 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4301 wxRegionIterator
iter( reg
);
4304 wxGridCellCoordsArray cellsExposed
;
4306 int left
, top
, right
, bottom
;
4311 // TODO: remove this when we can...
4312 // There is a bug in wxMotif that gives garbage update
4313 // rectangles if you jump-scroll a long way by clicking the
4314 // scrollbar with middle button. This is a work-around
4316 #if defined(__WXMOTIF__)
4318 m_gridWin
->GetClientSize( &cw
, &ch
);
4319 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4320 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4321 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4322 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4325 // logical bounds of update region
4327 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4328 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4330 // find the cells within these bounds
4333 for ( row
= 0; row
< m_numRows
; row
++ )
4335 if ( GetRowBottom(row
) <= top
)
4338 if ( GetRowTop(row
) > bottom
)
4342 for ( col
= 0; col
< m_numCols
; col
++ )
4344 if ( GetColRight(col
) <= left
)
4347 if ( GetColLeft(col
) > right
)
4350 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4357 return cellsExposed
;
4361 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4364 wxPoint
pos( event
.GetPosition() );
4365 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4367 if ( event
.Dragging() )
4369 m_isDragging
= TRUE
;
4371 if ( event
.LeftIsDown() )
4373 switch( m_cursorMode
)
4375 case WXGRID_CURSOR_RESIZE_ROW
:
4377 int cw
, ch
, left
, dummy
;
4378 m_gridWin
->GetClientSize( &cw
, &ch
);
4379 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4381 wxClientDC
dc( m_gridWin
);
4384 GetRowTop(m_dragRowOrCol
) +
4385 GetRowMinimalHeight(m_dragRowOrCol
) );
4386 dc
.SetLogicalFunction(wxINVERT
);
4387 if ( m_dragLastPos
>= 0 )
4389 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4391 dc
.DrawLine( left
, y
, left
+cw
, y
);
4396 case WXGRID_CURSOR_SELECT_ROW
:
4397 if ( (row
= YToRow( y
)) >= 0 )
4399 m_selection
->SelectRow( row
,
4400 event
.ControlDown(),
4406 // default label to suppress warnings about "enumeration value
4407 // 'xxx' not handled in switch
4415 m_isDragging
= FALSE
;
4418 // ------------ Entering or leaving the window
4420 if ( event
.Entering() || event
.Leaving() )
4422 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4426 // ------------ Left button pressed
4428 else if ( event
.LeftDown() )
4430 // don't send a label click event for a hit on the
4431 // edge of the row label - this is probably the user
4432 // wanting to resize the row
4434 if ( YToEdgeOfRow(y
) < 0 )
4438 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4440 if ( !event
.ShiftDown() && !event
.ControlDown() )
4442 if ( event
.ShiftDown() )
4443 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4446 GetNumberCols() - 1,
4447 event
.ControlDown(),
4452 m_selection
->SelectRow( row
,
4453 event
.ControlDown(),
4457 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4462 // starting to drag-resize a row
4464 if ( CanDragRowSize() )
4465 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4470 // ------------ Left double click
4472 else if (event
.LeftDClick() )
4474 if ( YToEdgeOfRow(y
) < 0 )
4477 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4482 // ------------ Left button released
4484 else if ( event
.LeftUp() )
4486 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4488 DoEndDragResizeRow();
4490 // Note: we are ending the event *after* doing
4491 // default processing in this case
4493 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4496 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4501 // ------------ Right button down
4503 else if ( event
.RightDown() )
4506 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4508 // no default action at the moment
4513 // ------------ Right double click
4515 else if ( event
.RightDClick() )
4518 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4520 // no default action at the moment
4525 // ------------ No buttons down and mouse moving
4527 else if ( event
.Moving() )
4529 m_dragRowOrCol
= YToEdgeOfRow( y
);
4530 if ( m_dragRowOrCol
>= 0 )
4532 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4534 // don't capture the mouse yet
4535 if ( CanDragRowSize() )
4536 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4539 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4541 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4547 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4550 wxPoint
pos( event
.GetPosition() );
4551 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4553 if ( event
.Dragging() )
4555 m_isDragging
= TRUE
;
4557 if ( event
.LeftIsDown() )
4559 switch( m_cursorMode
)
4561 case WXGRID_CURSOR_RESIZE_COL
:
4563 int cw
, ch
, dummy
, top
;
4564 m_gridWin
->GetClientSize( &cw
, &ch
);
4565 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4567 wxClientDC
dc( m_gridWin
);
4570 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4571 GetColMinimalWidth(m_dragRowOrCol
));
4572 dc
.SetLogicalFunction(wxINVERT
);
4573 if ( m_dragLastPos
>= 0 )
4575 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4577 dc
.DrawLine( x
, top
, x
, top
+ch
);
4582 case WXGRID_CURSOR_SELECT_COL
:
4583 if ( (col
= XToCol( x
)) >= 0 )
4585 m_selection
->SelectCol( col
,
4586 event
.ControlDown(),
4592 // default label to suppress warnings about "enumeration value
4593 // 'xxx' not handled in switch
4601 m_isDragging
= FALSE
;
4604 // ------------ Entering or leaving the window
4606 if ( event
.Entering() || event
.Leaving() )
4608 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4612 // ------------ Left button pressed
4614 else if ( event
.LeftDown() )
4616 // don't send a label click event for a hit on the
4617 // edge of the col label - this is probably the user
4618 // wanting to resize the col
4620 if ( XToEdgeOfCol(x
) < 0 )
4624 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4626 if ( !event
.ShiftDown() && !event
.ControlDown() )
4628 if ( event
.ShiftDown() )
4629 m_selection
->SelectBlock( 0,
4630 m_currentCellCoords
.GetCol(),
4631 GetNumberRows() - 1, col
,
4632 event
.ControlDown(),
4637 m_selection
->SelectCol( col
,
4638 event
.ControlDown(),
4642 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4647 // starting to drag-resize a col
4649 if ( CanDragColSize() )
4650 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4655 // ------------ Left double click
4657 if ( event
.LeftDClick() )
4659 if ( XToEdgeOfCol(x
) < 0 )
4662 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4667 // ------------ Left button released
4669 else if ( event
.LeftUp() )
4671 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4673 DoEndDragResizeCol();
4675 // Note: we are ending the event *after* doing
4676 // default processing in this case
4678 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4681 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4686 // ------------ Right button down
4688 else if ( event
.RightDown() )
4691 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4693 // no default action at the moment
4698 // ------------ Right double click
4700 else if ( event
.RightDClick() )
4703 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4705 // no default action at the moment
4710 // ------------ No buttons down and mouse moving
4712 else if ( event
.Moving() )
4714 m_dragRowOrCol
= XToEdgeOfCol( x
);
4715 if ( m_dragRowOrCol
>= 0 )
4717 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4719 // don't capture the cursor yet
4720 if ( CanDragColSize() )
4721 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4724 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4726 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4732 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4734 if ( event
.LeftDown() )
4736 // indicate corner label by having both row and
4739 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4745 else if ( event
.LeftDClick() )
4747 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4750 else if ( event
.RightDown() )
4752 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4754 // no default action at the moment
4758 else if ( event
.RightDClick() )
4760 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4762 // no default action at the moment
4767 void wxGrid::ChangeCursorMode(CursorMode mode
,
4772 static const wxChar
*cursorModes
[] =
4781 wxLogTrace(_T("grid"),
4782 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4783 win
== m_colLabelWin
? _T("colLabelWin")
4784 : win
? _T("rowLabelWin")
4786 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4787 #endif // __WXDEBUG__
4789 if ( mode
== m_cursorMode
&&
4790 win
== m_winCapture
&&
4791 captureMouse
== (m_winCapture
!= NULL
))
4796 // by default use the grid itself
4802 m_winCapture
->ReleaseMouse();
4803 m_winCapture
= (wxWindow
*)NULL
;
4806 m_cursorMode
= mode
;
4808 switch ( m_cursorMode
)
4810 case WXGRID_CURSOR_RESIZE_ROW
:
4811 win
->SetCursor( m_rowResizeCursor
);
4814 case WXGRID_CURSOR_RESIZE_COL
:
4815 win
->SetCursor( m_colResizeCursor
);
4819 win
->SetCursor( *wxSTANDARD_CURSOR
);
4822 // we need to capture mouse when resizing
4823 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4824 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4826 if ( captureMouse
&& resize
)
4828 win
->CaptureMouse();
4833 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4836 wxPoint
pos( event
.GetPosition() );
4837 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4839 wxGridCellCoords coords
;
4840 XYToCell( x
, y
, coords
);
4842 if ( event
.Dragging() )
4844 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4846 // Don't start doing anything until the mouse has been drug at
4847 // least 3 pixels in any direction...
4850 if (m_startDragPos
== wxDefaultPosition
)
4852 m_startDragPos
= pos
;
4855 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4859 m_isDragging
= TRUE
;
4860 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4862 // Hide the edit control, so it
4863 // won't interfer with drag-shrinking.
4864 if ( IsCellEditControlShown() )
4866 HideCellEditControl();
4867 SaveEditControlValue();
4870 // Have we captured the mouse yet?
4873 m_winCapture
= m_gridWin
;
4874 m_winCapture
->CaptureMouse();
4877 if ( coords
!= wxGridNoCellCoords
)
4879 if ( event
.ControlDown() )
4881 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4882 m_selectingKeyboard
= coords
;
4883 HighlightBlock ( m_selectingKeyboard
, coords
);
4887 if ( !IsSelection() )
4889 HighlightBlock( coords
, coords
);
4893 HighlightBlock( m_currentCellCoords
, coords
);
4897 if (! IsVisible(coords
))
4899 MakeCellVisible(coords
);
4900 // TODO: need to introduce a delay or something here. The
4901 // scrolling is way to fast, at least on MSW - also on GTK.
4905 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4907 int cw
, ch
, left
, dummy
;
4908 m_gridWin
->GetClientSize( &cw
, &ch
);
4909 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4911 wxClientDC
dc( m_gridWin
);
4913 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4914 GetRowMinimalHeight(m_dragRowOrCol
) );
4915 dc
.SetLogicalFunction(wxINVERT
);
4916 if ( m_dragLastPos
>= 0 )
4918 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4920 dc
.DrawLine( left
, y
, left
+cw
, y
);
4923 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4925 int cw
, ch
, dummy
, top
;
4926 m_gridWin
->GetClientSize( &cw
, &ch
);
4927 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4929 wxClientDC
dc( m_gridWin
);
4931 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4932 GetColMinimalWidth(m_dragRowOrCol
) );
4933 dc
.SetLogicalFunction(wxINVERT
);
4934 if ( m_dragLastPos
>= 0 )
4936 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4938 dc
.DrawLine( x
, top
, x
, top
+ch
);
4945 m_isDragging
= FALSE
;
4946 m_startDragPos
= wxDefaultPosition
;
4948 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4949 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4952 if ( event
.Entering() || event
.Leaving() )
4954 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4955 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4960 // ------------ Left button pressed
4962 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4964 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4969 if ( !event
.ControlDown() )
4971 if ( event
.ShiftDown() )
4973 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4974 m_currentCellCoords
.GetCol(),
4977 event
.ControlDown(),
4982 else if ( XToEdgeOfCol(x
) < 0 &&
4983 YToEdgeOfRow(y
) < 0 )
4985 DisableCellEditControl();
4986 MakeCellVisible( coords
);
4988 // if this is the second click on this cell then start
4990 if ( m_waitForSlowClick
&&
4991 (coords
== m_currentCellCoords
) &&
4992 CanEnableCellControl())
4994 EnableCellEditControl();
4996 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4997 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5000 editor
->StartingClick();
5004 m_waitForSlowClick
= FALSE
;
5008 if ( event
.ControlDown() )
5010 m_selection
->ToggleCellSelection( coords
.GetRow(),
5012 event
.ControlDown(),
5016 m_selectingTopLeft
= wxGridNoCellCoords
;
5017 m_selectingBottomRight
= wxGridNoCellCoords
;
5018 m_selectingKeyboard
= coords
;
5022 SetCurrentCell( coords
);
5023 if ( m_selection
->GetSelectionMode()
5024 != wxGrid::wxGridSelectCells
)
5025 HighlightBlock( coords
, coords
);
5027 m_waitForSlowClick
= TRUE
;
5034 // ------------ Left double click
5036 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5038 DisableCellEditControl();
5040 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5042 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5050 // ------------ Left button released
5052 else if ( event
.LeftUp() )
5054 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5056 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5057 m_selectingBottomRight
!= wxGridNoCellCoords
)
5061 m_winCapture
->ReleaseMouse();
5062 m_winCapture
= NULL
;
5064 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5065 m_selectingTopLeft
.GetCol(),
5066 m_selectingBottomRight
.GetRow(),
5067 m_selectingBottomRight
.GetCol(),
5068 event
.ControlDown(),
5072 m_selectingTopLeft
= wxGridNoCellCoords
;
5073 m_selectingBottomRight
= wxGridNoCellCoords
;
5076 // Show the edit control, if it has been hidden for
5078 ShowCellEditControl();
5080 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5082 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5083 DoEndDragResizeRow();
5085 // Note: we are ending the event *after* doing
5086 // default processing in this case
5088 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5090 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5092 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5093 DoEndDragResizeCol();
5095 // Note: we are ending the event *after* doing
5096 // default processing in this case
5098 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5105 // ------------ Right button down
5107 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5109 DisableCellEditControl();
5110 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5115 // no default action at the moment
5120 // ------------ Right double click
5122 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5124 DisableCellEditControl();
5125 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5130 // no default action at the moment
5134 // ------------ Moving and no button action
5136 else if ( event
.Moving() && !event
.IsButton() )
5138 int dragRow
= YToEdgeOfRow( y
);
5139 int dragCol
= XToEdgeOfCol( x
);
5141 // Dragging on the corner of a cell to resize in both
5142 // directions is not implemented yet...
5144 if ( dragRow
>= 0 && dragCol
>= 0 )
5146 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5152 m_dragRowOrCol
= dragRow
;
5154 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5156 if ( CanDragRowSize() && CanDragGridSize() )
5157 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5162 m_dragRowOrCol
= dragCol
;
5170 m_dragRowOrCol
= dragCol
;
5172 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5174 if ( CanDragColSize() && CanDragGridSize() )
5175 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5181 // Neither on a row or col edge
5183 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5185 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5191 void wxGrid::DoEndDragResizeRow()
5193 if ( m_dragLastPos
>= 0 )
5195 // erase the last line and resize the row
5197 int cw
, ch
, left
, dummy
;
5198 m_gridWin
->GetClientSize( &cw
, &ch
);
5199 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5201 wxClientDC
dc( m_gridWin
);
5203 dc
.SetLogicalFunction( wxINVERT
);
5204 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5205 HideCellEditControl();
5206 SaveEditControlValue();
5208 int rowTop
= GetRowTop(m_dragRowOrCol
);
5209 SetRowSize( m_dragRowOrCol
,
5210 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5212 if ( !GetBatchCount() )
5214 // Only needed to get the correct rect.y:
5215 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5217 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5218 rect
.width
= m_rowLabelWidth
;
5219 rect
.height
= ch
- rect
.y
;
5220 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5222 m_gridWin
->Refresh( FALSE
, &rect
);
5225 ShowCellEditControl();
5230 void wxGrid::DoEndDragResizeCol()
5232 if ( m_dragLastPos
>= 0 )
5234 // erase the last line and resize the col
5236 int cw
, ch
, dummy
, top
;
5237 m_gridWin
->GetClientSize( &cw
, &ch
);
5238 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5240 wxClientDC
dc( m_gridWin
);
5242 dc
.SetLogicalFunction( wxINVERT
);
5243 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5244 HideCellEditControl();
5245 SaveEditControlValue();
5247 int colLeft
= GetColLeft(m_dragRowOrCol
);
5248 SetColSize( m_dragRowOrCol
,
5249 wxMax( m_dragLastPos
- colLeft
,
5250 GetColMinimalWidth(m_dragRowOrCol
) ) );
5252 if ( !GetBatchCount() )
5254 // Only needed to get the correct rect.x:
5255 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5257 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5258 rect
.width
= cw
- rect
.x
;
5259 rect
.height
= m_colLabelHeight
;
5260 m_colLabelWin
->Refresh( TRUE
, &rect
);
5262 m_gridWin
->Refresh( FALSE
, &rect
);
5265 ShowCellEditControl();
5272 // ------ interaction with data model
5274 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5276 switch ( msg
.GetId() )
5278 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5279 return GetModelValues();
5281 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5282 return SetModelValues();
5284 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5285 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5286 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5287 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5288 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5289 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5290 return Redimension( msg
);
5299 // The behaviour of this function depends on the grid table class
5300 // Clear() function. For the default wxGridStringTable class the
5301 // behavious is to replace all cell contents with wxEmptyString but
5302 // not to change the number of rows or cols.
5304 void wxGrid::ClearGrid()
5308 if (IsCellEditControlEnabled())
5309 DisableCellEditControl();
5312 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5317 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5319 // TODO: something with updateLabels flag
5323 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5329 if (IsCellEditControlEnabled())
5330 DisableCellEditControl();
5332 return m_table
->InsertRows( pos
, numRows
);
5334 // the table will have sent the results of the insert row
5335 // operation to this view object as a grid table message
5341 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5343 // TODO: something with updateLabels flag
5347 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5351 return ( m_table
&& m_table
->AppendRows( numRows
) );
5352 // the table will have sent the results of the append row
5353 // operation to this view object as a grid table message
5357 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5359 // TODO: something with updateLabels flag
5363 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5369 if (IsCellEditControlEnabled())
5370 DisableCellEditControl();
5372 return (m_table
->DeleteRows( pos
, numRows
));
5373 // the table will have sent the results of the delete row
5374 // operation to this view object as a grid table message
5380 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5382 // TODO: something with updateLabels flag
5386 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5392 if (IsCellEditControlEnabled())
5393 DisableCellEditControl();
5395 return m_table
->InsertCols( pos
, numCols
);
5396 // the table will have sent the results of the insert col
5397 // operation to this view object as a grid table message
5403 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5405 // TODO: something with updateLabels flag
5409 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5413 return ( m_table
&& m_table
->AppendCols( numCols
) );
5414 // the table will have sent the results of the append col
5415 // operation to this view object as a grid table message
5419 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5421 // TODO: something with updateLabels flag
5425 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5431 if (IsCellEditControlEnabled())
5432 DisableCellEditControl();
5434 return ( m_table
->DeleteCols( pos
, numCols
) );
5435 // the table will have sent the results of the delete col
5436 // operation to this view object as a grid table message
5444 // ----- event handlers
5447 // Generate a grid event based on a mouse event and
5448 // return the result of ProcessEvent()
5450 bool wxGrid::SendEvent( const wxEventType type
,
5452 wxMouseEvent
& mouseEv
)
5454 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5456 int rowOrCol
= (row
== -1 ? col
: row
);
5458 wxGridSizeEvent
gridEvt( GetId(),
5462 mouseEv
.GetX() + GetRowLabelSize(),
5463 mouseEv
.GetY() + GetColLabelSize(),
5464 mouseEv
.ControlDown(),
5465 mouseEv
.ShiftDown(),
5467 mouseEv
.MetaDown() );
5468 return GetEventHandler()->ProcessEvent(gridEvt
);
5470 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5472 // Right now, it should _never_ end up here!
5473 wxGridRangeSelectEvent
gridEvt( GetId(),
5477 m_selectingBottomRight
,
5479 mouseEv
.ControlDown(),
5480 mouseEv
.ShiftDown(),
5482 mouseEv
.MetaDown() );
5484 return GetEventHandler()->ProcessEvent(gridEvt
);
5488 wxGridEvent
gridEvt( GetId(),
5492 mouseEv
.GetX() + GetRowLabelSize(),
5493 mouseEv
.GetY() + GetColLabelSize(),
5495 mouseEv
.ControlDown(),
5496 mouseEv
.ShiftDown(),
5498 mouseEv
.MetaDown() );
5499 return GetEventHandler()->ProcessEvent(gridEvt
);
5504 // Generate a grid event of specified type and return the result
5505 // of ProcessEvent().
5507 bool wxGrid::SendEvent( const wxEventType type
,
5510 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5512 int rowOrCol
= (row
== -1 ? col
: row
);
5514 wxGridSizeEvent
gridEvt( GetId(),
5519 return GetEventHandler()->ProcessEvent(gridEvt
);
5523 wxGridEvent
gridEvt( GetId(),
5528 return GetEventHandler()->ProcessEvent(gridEvt
);
5533 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5535 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5539 // This is just here to make sure that CalcDimensions gets called when
5540 // the grid view is resized... then the size event is skipped to allow
5541 // the box sizers to handle everything
5543 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5550 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5552 if ( m_inOnKeyDown
)
5554 // shouldn't be here - we are going round in circles...
5556 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5559 m_inOnKeyDown
= TRUE
;
5561 // propagate the event up and see if it gets processed
5563 wxWindow
*parent
= GetParent();
5564 wxKeyEvent
keyEvt( event
);
5565 keyEvt
.SetEventObject( parent
);
5567 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5570 // try local handlers
5572 switch ( event
.KeyCode() )
5575 if ( event
.ControlDown() )
5577 MoveCursorUpBlock( event
.ShiftDown() );
5581 MoveCursorUp( event
.ShiftDown() );
5586 if ( event
.ControlDown() )
5588 MoveCursorDownBlock( event
.ShiftDown() );
5592 MoveCursorDown( event
.ShiftDown() );
5597 if ( event
.ControlDown() )
5599 MoveCursorLeftBlock( event
.ShiftDown() );
5603 MoveCursorLeft( event
.ShiftDown() );
5608 if ( event
.ControlDown() )
5610 MoveCursorRightBlock( event
.ShiftDown() );
5614 MoveCursorRight( event
.ShiftDown() );
5619 case WXK_NUMPAD_ENTER
:
5620 if ( event
.ControlDown() )
5622 event
.Skip(); // to let the edit control have the return
5626 if ( GetGridCursorRow() < GetNumberRows()-1 )
5628 MoveCursorDown( event
.ShiftDown() );
5632 // at the bottom of a column
5633 HideCellEditControl();
5634 SaveEditControlValue();
5644 if (event
.ShiftDown())
5646 if ( GetGridCursorCol() > 0 )
5648 MoveCursorLeft( FALSE
);
5653 HideCellEditControl();
5654 SaveEditControlValue();
5659 if ( GetGridCursorCol() < GetNumberCols()-1 )
5661 MoveCursorRight( FALSE
);
5666 HideCellEditControl();
5667 SaveEditControlValue();
5673 if ( event
.ControlDown() )
5675 MakeCellVisible( 0, 0 );
5676 SetCurrentCell( 0, 0 );
5685 if ( event
.ControlDown() )
5687 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5688 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5705 if ( event
.ControlDown() )
5707 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5708 m_currentCellCoords
.GetCol(),
5709 event
.ControlDown(),
5715 if ( !IsEditable() )
5717 MoveCursorRight( FALSE
);
5720 // Otherwise fall through to default
5723 // is it possible to edit the current cell at all?
5724 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5726 // yes, now check whether the cells editor accepts the key
5727 int row
= m_currentCellCoords
.GetRow();
5728 int col
= m_currentCellCoords
.GetCol();
5729 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5730 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5732 // <F2> is special and will always start editing, for
5733 // other keys - ask the editor itself
5734 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5735 || editor
->IsAcceptedKey(event
) )
5737 EnableCellEditControl();
5738 editor
->StartingKey(event
);
5750 // let others process char events with modifiers or all
5751 // char events for readonly cells
5758 m_inOnKeyDown
= FALSE
;
5761 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5763 // try local handlers
5765 if ( event
.KeyCode() == WXK_SHIFT
)
5767 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5768 m_selectingBottomRight
!= wxGridNoCellCoords
)
5769 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5770 m_selectingTopLeft
.GetCol(),
5771 m_selectingBottomRight
.GetRow(),
5772 m_selectingBottomRight
.GetCol(),
5773 event
.ControlDown(),
5777 m_selectingTopLeft
= wxGridNoCellCoords
;
5778 m_selectingBottomRight
= wxGridNoCellCoords
;
5779 m_selectingKeyboard
= wxGridNoCellCoords
;
5783 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5787 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5789 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5791 // the event has been intercepted - do nothing
5795 wxClientDC
dc(m_gridWin
);
5798 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5800 HideCellEditControl();
5801 DisableCellEditControl();
5803 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5806 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5807 if ( !m_gridLinesEnabled
)
5815 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5817 // Otherwise refresh redraws the highlight!
5818 m_currentCellCoords
= coords
;
5820 DrawGridCellArea(dc
,cells
);
5821 DrawAllGridLines( dc
, r
);
5825 m_currentCellCoords
= coords
;
5827 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5828 DrawCellHighlight(dc
, attr
);
5833 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5836 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5838 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5841 rightCol
= GetNumberCols() - 1;
5843 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5846 bottomRow
= GetNumberRows() - 1;
5848 if ( topRow
> bottomRow
)
5855 if ( leftCol
> rightCol
)
5862 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5863 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5865 if ( m_selectingTopLeft
!= updateTopLeft
||
5866 m_selectingBottomRight
!= updateBottomRight
)
5868 // Compute two optimal update rectangles:
5869 // Either one rectangle is a real subset of the
5870 // other, or they are (almost) disjoint!
5872 bool need_refresh
[4];
5876 need_refresh
[3] = FALSE
;
5879 // Store intermediate values
5880 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5881 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5882 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5883 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5885 // Determine the outer/inner coordinates.
5886 if (oldLeft
> leftCol
)
5892 if (oldTop
> topRow
)
5898 if (oldRight
< rightCol
)
5901 oldRight
= rightCol
;
5904 if (oldBottom
< bottomRow
)
5907 oldBottom
= bottomRow
;
5911 // Now, either the stuff marked old is the outer
5912 // rectangle or we don't have a situation where one
5913 // is contained in the other.
5915 if ( oldLeft
< leftCol
)
5917 need_refresh
[0] = TRUE
;
5918 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5920 wxGridCellCoords ( oldBottom
,
5924 if ( oldTop
< topRow
)
5926 need_refresh
[1] = TRUE
;
5927 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5929 wxGridCellCoords ( topRow
- 1,
5933 if ( oldRight
> rightCol
)
5935 need_refresh
[2] = TRUE
;
5936 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5938 wxGridCellCoords ( oldBottom
,
5942 if ( oldBottom
> bottomRow
)
5944 need_refresh
[3] = TRUE
;
5945 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5947 wxGridCellCoords ( oldBottom
,
5953 m_selectingTopLeft
= updateTopLeft
;
5954 m_selectingBottomRight
= updateBottomRight
;
5956 // various Refresh() calls
5957 for (i
= 0; i
< 4; i
++ )
5958 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5959 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5962 // never generate an event as it will be generated from
5963 // wxGridSelection::SelectBlock!
5964 // (old comment from when this was the body of SelectBlock)
5968 // ------ functions to get/send data (see also public functions)
5971 bool wxGrid::GetModelValues()
5975 // all we need to do is repaint the grid
5977 m_gridWin
->Refresh();
5985 bool wxGrid::SetModelValues()
5991 for ( row
= 0; row
< m_numRows
; row
++ )
5993 for ( col
= 0; col
< m_numCols
; col
++ )
5995 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6007 // Note - this function only draws cells that are in the list of
6008 // exposed cells (usually set from the update region by
6009 // CalcExposedCells)
6011 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6013 if ( !m_numRows
|| !m_numCols
) return;
6016 size_t numCells
= cells
.GetCount();
6018 for ( i
= 0; i
< numCells
; i
++ )
6020 DrawCell( dc
, cells
[i
] );
6025 void wxGrid::DrawGridSpace( wxDC
& dc
)
6028 m_gridWin
->GetClientSize( &cw
, &ch
);
6031 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6033 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6034 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6036 if ( right
> rightCol
|| bottom
> bottomRow
)
6039 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6041 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6042 dc
.SetPen( *wxTRANSPARENT_PEN
);
6044 if ( right
> rightCol
)
6046 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6049 if ( bottom
> bottomRow
)
6051 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6057 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6059 int row
= coords
.GetRow();
6060 int col
= coords
.GetCol();
6062 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6065 // we draw the cell border ourselves
6066 #if !WXGRID_DRAW_LINES
6067 if ( m_gridLinesEnabled
)
6068 DrawCellBorder( dc
, coords
);
6071 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6073 bool isCurrent
= coords
== m_currentCellCoords
;
6075 wxRect rect
= CellToRect( row
, col
);
6077 // if the editor is shown, we should use it and not the renderer
6078 // Note: However, only if it is really _shown_, i.e. not hidden!
6079 if ( isCurrent
&& IsCellEditControlShown() )
6081 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6082 editor
->PaintBackground(rect
, attr
);
6087 // but all the rest is drawn by the cell renderer and hence may be
6089 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6090 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6097 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6099 int row
= m_currentCellCoords
.GetRow();
6100 int col
= m_currentCellCoords
.GetCol();
6102 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6105 wxRect rect
= CellToRect(row
, col
);
6107 // hmmm... what could we do here to show that the cell is disabled?
6108 // for now, I just draw a thinner border than for the other ones, but
6109 // it doesn't look really good
6111 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6115 // The center of th drawn line is where the position/width/height of
6116 // the rectangle is actually at, (on wxMSW atr least,) so we will
6117 // reduce the size of the rectangle to compensate for the thickness of
6118 // the line. If this is too strange on non wxMSW platforms then
6119 // please #ifdef this appropriately.
6120 rect
.x
+= penWidth
/2;
6121 rect
.y
+= penWidth
/2;
6122 rect
.width
-= penWidth
-1;
6123 rect
.height
-= penWidth
-1;
6126 // Now draw the rectangle
6127 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6128 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6129 dc
.DrawRectangle(rect
);
6133 // VZ: my experiments with 3d borders...
6135 // how to properly set colours for arbitrary bg?
6136 wxCoord x1
= rect
.x
,
6138 x2
= rect
.x
+ rect
.width
-1,
6139 y2
= rect
.y
+ rect
.height
-1;
6141 dc
.SetPen(*wxWHITE_PEN
);
6142 dc
.DrawLine(x1
, y1
, x2
, y1
);
6143 dc
.DrawLine(x1
, y1
, x1
, y2
);
6145 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6146 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6148 dc
.SetPen(*wxBLACK_PEN
);
6149 dc
.DrawLine(x1
, y2
, x2
, y2
);
6150 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6155 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6157 int row
= coords
.GetRow();
6158 int col
= coords
.GetCol();
6159 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6162 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6164 // right hand border
6166 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6167 GetColRight(col
), GetRowBottom(row
) );
6171 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6172 GetColRight(col
), GetRowBottom(row
) );
6175 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6177 // This if block was previously in wxGrid::OnPaint but that doesn't
6178 // seem to get called under wxGTK - MB
6180 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6181 m_numRows
&& m_numCols
)
6183 m_currentCellCoords
.Set(0, 0);
6186 if ( IsCellEditControlShown() )
6188 // don't show highlight when the edit control is shown
6192 // if the active cell was repainted, repaint its highlight too because it
6193 // might have been damaged by the grid lines
6194 size_t count
= cells
.GetCount();
6195 for ( size_t n
= 0; n
< count
; n
++ )
6197 if ( cells
[n
] == m_currentCellCoords
)
6199 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6200 DrawCellHighlight(dc
, attr
);
6208 // TODO: remove this ???
6209 // This is used to redraw all grid lines e.g. when the grid line colour
6212 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6214 if ( !m_gridLinesEnabled
||
6216 !m_numCols
) return;
6218 int top
, bottom
, left
, right
;
6220 #if 0 //#ifndef __WXGTK__
6224 m_gridWin
->GetClientSize(&cw
, &ch
);
6226 // virtual coords of visible area
6228 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6229 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6234 reg
.GetBox(x
, y
, w
, h
);
6235 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6236 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6240 m_gridWin
->GetClientSize(&cw
, &ch
);
6241 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6242 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6245 // avoid drawing grid lines past the last row and col
6247 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6248 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6250 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6252 // horizontal grid lines
6255 for ( i
= 0; i
< m_numRows
; i
++ )
6257 int bot
= GetRowBottom(i
) - 1;
6266 dc
.DrawLine( left
, bot
, right
, bot
);
6271 // vertical grid lines
6273 for ( i
= 0; i
< m_numCols
; i
++ )
6275 int colRight
= GetColRight(i
) - 1;
6276 if ( colRight
> right
)
6281 if ( colRight
>= left
)
6283 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6289 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6291 if ( !m_numRows
) return;
6294 size_t numLabels
= rows
.GetCount();
6296 for ( i
= 0; i
< numLabels
; i
++ )
6298 DrawRowLabel( dc
, rows
[i
] );
6303 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6305 if ( GetRowHeight(row
) <= 0 )
6308 int rowTop
= GetRowTop(row
),
6309 rowBottom
= GetRowBottom(row
) - 1;
6311 dc
.SetPen( *wxBLACK_PEN
);
6312 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6313 m_rowLabelWidth
-1, rowBottom
);
6315 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6317 dc
.SetPen( *wxWHITE_PEN
);
6318 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6319 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6321 dc
.SetBackgroundMode( wxTRANSPARENT
);
6322 dc
.SetTextForeground( GetLabelTextColour() );
6323 dc
.SetFont( GetLabelFont() );
6326 GetRowLabelAlignment( &hAlign
, &vAlign
);
6330 rect
.SetY( GetRowTop(row
) + 2 );
6331 rect
.SetWidth( m_rowLabelWidth
- 4 );
6332 rect
.SetHeight( GetRowHeight(row
) - 4 );
6333 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6337 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6339 if ( !m_numCols
) return;
6342 size_t numLabels
= cols
.GetCount();
6344 for ( i
= 0; i
< numLabels
; i
++ )
6346 DrawColLabel( dc
, cols
[i
] );
6351 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6353 if ( GetColWidth(col
) <= 0 )
6356 int colLeft
= GetColLeft(col
),
6357 colRight
= GetColRight(col
) - 1;
6359 dc
.SetPen( *wxBLACK_PEN
);
6360 dc
.DrawLine( colRight
, 0,
6361 colRight
, m_colLabelHeight
-1 );
6363 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6364 colRight
, m_colLabelHeight
-1 );
6366 dc
.SetPen( *wxWHITE_PEN
);
6367 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6368 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6370 dc
.SetBackgroundMode( wxTRANSPARENT
);
6371 dc
.SetTextForeground( GetLabelTextColour() );
6372 dc
.SetFont( GetLabelFont() );
6374 dc
.SetBackgroundMode( wxTRANSPARENT
);
6375 dc
.SetTextForeground( GetLabelTextColour() );
6376 dc
.SetFont( GetLabelFont() );
6379 GetColLabelAlignment( &hAlign
, &vAlign
);
6382 rect
.SetX( colLeft
+ 2 );
6384 rect
.SetWidth( GetColWidth(col
) - 4 );
6385 rect
.SetHeight( m_colLabelHeight
- 4 );
6386 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6389 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6390 const wxString
& value
,
6395 wxArrayString lines
;
6397 dc
.SetClippingRegion( rect
);
6398 StringToLines( value
, lines
);
6401 //Forward to new API.
6402 DrawTextRectangle( dc
,
6410 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6411 const wxArrayString
& lines
,
6416 long textWidth
, textHeight
;
6417 long lineWidth
, lineHeight
;
6419 if ( lines
.GetCount() )
6421 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6422 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6425 switch ( horizAlign
)
6428 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6431 case wxALIGN_CENTRE
:
6432 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6441 switch ( vertAlign
)
6443 case wxALIGN_BOTTOM
:
6444 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6447 case wxALIGN_CENTRE
:
6448 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6457 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6459 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6464 dc
.DestroyClippingRegion();
6468 // Split multi line text up into an array of strings. Any existing
6469 // contents of the string array are preserved.
6471 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6475 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6476 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6478 while ( startPos
< (int)tVal
.Length() )
6480 pos
= tVal
.Mid(startPos
).Find( eol
);
6485 else if ( pos
== 0 )
6487 lines
.Add( wxEmptyString
);
6491 lines
.Add( value
.Mid(startPos
, pos
) );
6495 if ( startPos
< (int)value
.Length() )
6497 lines
.Add( value
.Mid( startPos
) );
6502 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6503 const wxArrayString
& lines
,
6504 long *width
, long *height
)
6511 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6513 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6514 w
= wxMax( w
, lineW
);
6523 // ------ Batch processing.
6525 void wxGrid::EndBatch()
6527 if ( m_batchCount
> 0 )
6530 if ( !m_batchCount
)
6533 m_rowLabelWin
->Refresh();
6534 m_colLabelWin
->Refresh();
6535 m_cornerLabelWin
->Refresh();
6536 m_gridWin
->Refresh();
6541 // Use this, rather than wxWindow::Refresh(), to force an immediate
6542 // repainting of the grid. Has no effect if you are already inside a
6543 // BeginBatch / EndBatch block.
6545 void wxGrid::ForceRefresh()
6553 // ------ Edit control functions
6557 void wxGrid::EnableEditing( bool edit
)
6559 // TODO: improve this ?
6561 if ( edit
!= m_editable
)
6563 if(!edit
) EnableCellEditControl(edit
);
6569 void wxGrid::EnableCellEditControl( bool enable
)
6574 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6575 SetCurrentCell( 0, 0 );
6577 if ( enable
!= m_cellEditCtrlEnabled
)
6579 // TODO allow the app to Veto() this event?
6580 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6584 // this should be checked by the caller!
6585 wxASSERT_MSG( CanEnableCellControl(),
6586 _T("can't enable editing for this cell!") );
6588 // do it before ShowCellEditControl()
6589 m_cellEditCtrlEnabled
= enable
;
6591 ShowCellEditControl();
6595 HideCellEditControl();
6596 SaveEditControlValue();
6598 // do it after HideCellEditControl()
6599 m_cellEditCtrlEnabled
= enable
;
6604 bool wxGrid::IsCurrentCellReadOnly() const
6607 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6608 bool readonly
= attr
->IsReadOnly();
6614 bool wxGrid::CanEnableCellControl() const
6616 return m_editable
&& !IsCurrentCellReadOnly();
6619 bool wxGrid::IsCellEditControlEnabled() const
6621 // the cell edit control might be disable for all cells or just for the
6622 // current one if it's read only
6623 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6626 bool wxGrid::IsCellEditControlShown() const
6628 bool isShown
= FALSE
;
6630 if ( m_cellEditCtrlEnabled
)
6632 int row
= m_currentCellCoords
.GetRow();
6633 int col
= m_currentCellCoords
.GetCol();
6634 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6635 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6640 if ( editor
->IsCreated() )
6642 isShown
= editor
->GetControl()->IsShown();
6652 void wxGrid::ShowCellEditControl()
6654 if ( IsCellEditControlEnabled() )
6656 if ( !IsVisible( m_currentCellCoords
) )
6658 m_cellEditCtrlEnabled
= FALSE
;
6663 wxRect rect
= CellToRect( m_currentCellCoords
);
6664 int row
= m_currentCellCoords
.GetRow();
6665 int col
= m_currentCellCoords
.GetCol();
6667 // convert to scrolled coords
6669 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6671 // done in PaintBackground()
6673 // erase the highlight and the cell contents because the editor
6674 // might not cover the entire cell
6675 wxClientDC
dc( m_gridWin
);
6677 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6678 dc
.SetPen(*wxTRANSPARENT_PEN
);
6679 dc
.DrawRectangle(rect
);
6682 // cell is shifted by one pixel
6686 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6687 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6688 if ( !editor
->IsCreated() )
6690 editor
->Create(m_gridWin
, -1,
6691 new wxGridCellEditorEvtHandler(this, editor
));
6693 wxGridEditorCreatedEvent
evt(GetId(),
6694 wxEVT_GRID_EDITOR_CREATED
,
6698 editor
->GetControl());
6699 GetEventHandler()->ProcessEvent(evt
);
6702 editor
->Show( TRUE
, attr
);
6704 editor
->SetSize( rect
);
6706 editor
->BeginEdit(row
, col
, this);
6715 void wxGrid::HideCellEditControl()
6717 if ( IsCellEditControlEnabled() )
6719 int row
= m_currentCellCoords
.GetRow();
6720 int col
= m_currentCellCoords
.GetCol();
6722 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6723 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6724 editor
->Show( FALSE
);
6727 m_gridWin
->SetFocus();
6728 wxRect
rect( CellToRect( row
, col
) );
6729 m_gridWin
->Refresh( FALSE
, &rect
);
6734 void wxGrid::SaveEditControlValue()
6736 if ( IsCellEditControlEnabled() )
6738 int row
= m_currentCellCoords
.GetRow();
6739 int col
= m_currentCellCoords
.GetCol();
6741 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6742 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6743 bool changed
= editor
->EndEdit(row
, col
, this);
6750 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6751 m_currentCellCoords
.GetRow(),
6752 m_currentCellCoords
.GetCol() );
6759 // ------ Grid location functions
6760 // Note that all of these functions work with the logical coordinates of
6761 // grid cells and labels so you will need to convert from device
6762 // coordinates for mouse events etc.
6765 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6767 int row
= YToRow(y
);
6768 int col
= XToCol(x
);
6770 if ( row
== -1 || col
== -1 )
6772 coords
= wxGridNoCellCoords
;
6776 coords
.Set( row
, col
);
6781 int wxGrid::YToRow( int y
)
6785 for ( i
= 0; i
< m_numRows
; i
++ )
6787 if ( y
< GetRowBottom(i
) )
6795 int wxGrid::XToCol( int x
)
6799 for ( i
= 0; i
< m_numCols
; i
++ )
6801 if ( x
< GetColRight(i
) )
6809 // return the row number that that the y coord is near the edge of, or
6810 // -1 if not near an edge
6812 int wxGrid::YToEdgeOfRow( int y
)
6816 for ( i
= 0; i
< m_numRows
; i
++ )
6818 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6820 d
= abs( y
- GetRowBottom(i
) );
6821 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6830 // return the col number that that the x coord is near the edge of, or
6831 // -1 if not near an edge
6833 int wxGrid::XToEdgeOfCol( int x
)
6837 for ( i
= 0; i
< m_numCols
; i
++ )
6839 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6841 d
= abs( x
- GetColRight(i
) );
6842 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6851 wxRect
wxGrid::CellToRect( int row
, int col
)
6853 wxRect
rect( -1, -1, -1, -1 );
6855 if ( row
>= 0 && row
< m_numRows
&&
6856 col
>= 0 && col
< m_numCols
)
6858 rect
.x
= GetColLeft(col
);
6859 rect
.y
= GetRowTop(row
);
6860 rect
.width
= GetColWidth(col
);
6861 rect
.height
= GetRowHeight(row
);
6864 // if grid lines are enabled, then the area of the cell is a bit smaller
6865 if (m_gridLinesEnabled
) {
6873 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6875 // get the cell rectangle in logical coords
6877 wxRect
r( CellToRect( row
, col
) );
6879 // convert to device coords
6881 int left
, top
, right
, bottom
;
6882 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6883 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6885 // check against the client area of the grid window
6888 m_gridWin
->GetClientSize( &cw
, &ch
);
6890 if ( wholeCellVisible
)
6892 // is the cell wholly visible ?
6894 return ( left
>= 0 && right
<= cw
&&
6895 top
>= 0 && bottom
<= ch
);
6899 // is the cell partly visible ?
6901 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6902 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6907 // make the specified cell location visible by doing a minimal amount
6910 void wxGrid::MakeCellVisible( int row
, int col
)
6913 int xpos
= -1, ypos
= -1;
6915 if ( row
>= 0 && row
< m_numRows
&&
6916 col
>= 0 && col
< m_numCols
)
6918 // get the cell rectangle in logical coords
6920 wxRect
r( CellToRect( row
, col
) );
6922 // convert to device coords
6924 int left
, top
, right
, bottom
;
6925 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6926 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6929 m_gridWin
->GetClientSize( &cw
, &ch
);
6935 else if ( bottom
> ch
)
6937 int h
= r
.GetHeight();
6939 for ( i
= row
-1; i
>= 0; i
-- )
6941 int rowHeight
= GetRowHeight(i
);
6942 if ( h
+ rowHeight
> ch
)
6949 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6950 // have rounding errors (this is important, because if we do, we
6951 // might not scroll at all and some cells won't be redrawn)
6952 ypos
+= GRID_SCROLL_LINE
/ 2;
6959 else if ( right
> cw
)
6961 int w
= r
.GetWidth();
6963 for ( i
= col
-1; i
>= 0; i
-- )
6965 int colWidth
= GetColWidth(i
);
6966 if ( w
+ colWidth
> cw
)
6973 // see comment for ypos above
6974 xpos
+= GRID_SCROLL_LINE
/ 2;
6977 if ( xpos
!= -1 || ypos
!= -1 )
6979 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6980 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6981 Scroll( xpos
, ypos
);
6989 // ------ Grid cursor movement functions
6992 bool wxGrid::MoveCursorUp( bool expandSelection
)
6994 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6995 m_currentCellCoords
.GetRow() >= 0 )
6997 if ( expandSelection
)
6999 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7000 m_selectingKeyboard
= m_currentCellCoords
;
7001 if ( m_selectingKeyboard
.GetRow() > 0 )
7003 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7004 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7005 m_selectingKeyboard
.GetCol() );
7006 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7009 else if ( m_currentCellCoords
.GetRow() > 0 )
7012 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7013 m_currentCellCoords
.GetCol() );
7014 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7015 m_currentCellCoords
.GetCol() );
7026 bool wxGrid::MoveCursorDown( bool expandSelection
)
7028 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7029 m_currentCellCoords
.GetRow() < m_numRows
)
7031 if ( expandSelection
)
7033 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7034 m_selectingKeyboard
= m_currentCellCoords
;
7035 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7037 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7038 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7039 m_selectingKeyboard
.GetCol() );
7040 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7043 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7046 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7047 m_currentCellCoords
.GetCol() );
7048 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7049 m_currentCellCoords
.GetCol() );
7060 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7062 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7063 m_currentCellCoords
.GetCol() >= 0 )
7065 if ( expandSelection
)
7067 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7068 m_selectingKeyboard
= m_currentCellCoords
;
7069 if ( m_selectingKeyboard
.GetCol() > 0 )
7071 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7072 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7073 m_selectingKeyboard
.GetCol() );
7074 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7077 else if ( m_currentCellCoords
.GetCol() > 0 )
7080 MakeCellVisible( m_currentCellCoords
.GetRow(),
7081 m_currentCellCoords
.GetCol() - 1 );
7082 SetCurrentCell( m_currentCellCoords
.GetRow(),
7083 m_currentCellCoords
.GetCol() - 1 );
7094 bool wxGrid::MoveCursorRight( bool expandSelection
)
7096 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7097 m_currentCellCoords
.GetCol() < m_numCols
)
7099 if ( expandSelection
)
7101 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7102 m_selectingKeyboard
= m_currentCellCoords
;
7103 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7105 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7106 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7107 m_selectingKeyboard
.GetCol() );
7108 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7111 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7114 MakeCellVisible( m_currentCellCoords
.GetRow(),
7115 m_currentCellCoords
.GetCol() + 1 );
7116 SetCurrentCell( m_currentCellCoords
.GetRow(),
7117 m_currentCellCoords
.GetCol() + 1 );
7128 bool wxGrid::MovePageUp()
7130 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7132 int row
= m_currentCellCoords
.GetRow();
7136 m_gridWin
->GetClientSize( &cw
, &ch
);
7138 int y
= GetRowTop(row
);
7139 int newRow
= YToRow( y
- ch
+ 1 );
7144 else if ( newRow
== row
)
7149 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7150 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7158 bool wxGrid::MovePageDown()
7160 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7162 int row
= m_currentCellCoords
.GetRow();
7163 if ( row
< m_numRows
)
7166 m_gridWin
->GetClientSize( &cw
, &ch
);
7168 int y
= GetRowTop(row
);
7169 int newRow
= YToRow( y
+ ch
);
7172 newRow
= m_numRows
- 1;
7174 else if ( newRow
== row
)
7179 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7180 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7188 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7191 m_currentCellCoords
!= wxGridNoCellCoords
&&
7192 m_currentCellCoords
.GetRow() > 0 )
7194 int row
= m_currentCellCoords
.GetRow();
7195 int col
= m_currentCellCoords
.GetCol();
7197 if ( m_table
->IsEmptyCell(row
, col
) )
7199 // starting in an empty cell: find the next block of
7205 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7208 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7210 // starting at the top of a block: find the next block
7216 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7221 // starting within a block: find the top of the block
7226 if ( m_table
->IsEmptyCell(row
, col
) )
7234 MakeCellVisible( row
, col
);
7235 if ( expandSelection
)
7237 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7238 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7243 SetCurrentCell( row
, col
);
7251 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7254 m_currentCellCoords
!= wxGridNoCellCoords
&&
7255 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7257 int row
= m_currentCellCoords
.GetRow();
7258 int col
= m_currentCellCoords
.GetCol();
7260 if ( m_table
->IsEmptyCell(row
, col
) )
7262 // starting in an empty cell: find the next block of
7265 while ( row
< m_numRows
-1 )
7268 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7271 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7273 // starting at the bottom of a block: find the next block
7276 while ( row
< m_numRows
-1 )
7279 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7284 // starting within a block: find the bottom of the block
7286 while ( row
< m_numRows
-1 )
7289 if ( m_table
->IsEmptyCell(row
, col
) )
7297 MakeCellVisible( row
, col
);
7298 if ( expandSelection
)
7300 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7301 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7306 SetCurrentCell( row
, col
);
7315 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7318 m_currentCellCoords
!= wxGridNoCellCoords
&&
7319 m_currentCellCoords
.GetCol() > 0 )
7321 int row
= m_currentCellCoords
.GetRow();
7322 int col
= m_currentCellCoords
.GetCol();
7324 if ( m_table
->IsEmptyCell(row
, col
) )
7326 // starting in an empty cell: find the next block of
7332 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7335 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7337 // starting at the left of a block: find the next block
7343 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7348 // starting within a block: find the left of the block
7353 if ( m_table
->IsEmptyCell(row
, col
) )
7361 MakeCellVisible( row
, col
);
7362 if ( expandSelection
)
7364 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7365 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7370 SetCurrentCell( row
, col
);
7379 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7382 m_currentCellCoords
!= wxGridNoCellCoords
&&
7383 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7385 int row
= m_currentCellCoords
.GetRow();
7386 int col
= m_currentCellCoords
.GetCol();
7388 if ( m_table
->IsEmptyCell(row
, col
) )
7390 // starting in an empty cell: find the next block of
7393 while ( col
< m_numCols
-1 )
7396 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7399 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7401 // starting at the right of a block: find the next block
7404 while ( col
< m_numCols
-1 )
7407 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7412 // starting within a block: find the right of the block
7414 while ( col
< m_numCols
-1 )
7417 if ( m_table
->IsEmptyCell(row
, col
) )
7425 MakeCellVisible( row
, col
);
7426 if ( expandSelection
)
7428 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7429 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7434 SetCurrentCell( row
, col
);
7446 // ------ Label values and formatting
7449 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7451 *horiz
= m_rowLabelHorizAlign
;
7452 *vert
= m_rowLabelVertAlign
;
7455 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7457 *horiz
= m_colLabelHorizAlign
;
7458 *vert
= m_colLabelVertAlign
;
7461 wxString
wxGrid::GetRowLabelValue( int row
)
7465 return m_table
->GetRowLabelValue( row
);
7475 wxString
wxGrid::GetColLabelValue( int col
)
7479 return m_table
->GetColLabelValue( col
);
7490 void wxGrid::SetRowLabelSize( int width
)
7492 width
= wxMax( width
, 0 );
7493 if ( width
!= m_rowLabelWidth
)
7497 m_rowLabelWin
->Show( FALSE
);
7498 m_cornerLabelWin
->Show( FALSE
);
7500 else if ( m_rowLabelWidth
== 0 )
7502 m_rowLabelWin
->Show( TRUE
);
7503 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7506 m_rowLabelWidth
= width
;
7513 void wxGrid::SetColLabelSize( int height
)
7515 height
= wxMax( height
, 0 );
7516 if ( height
!= m_colLabelHeight
)
7520 m_colLabelWin
->Show( FALSE
);
7521 m_cornerLabelWin
->Show( FALSE
);
7523 else if ( m_colLabelHeight
== 0 )
7525 m_colLabelWin
->Show( TRUE
);
7526 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7529 m_colLabelHeight
= height
;
7536 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7538 if ( m_labelBackgroundColour
!= colour
)
7540 m_labelBackgroundColour
= colour
;
7541 m_rowLabelWin
->SetBackgroundColour( colour
);
7542 m_colLabelWin
->SetBackgroundColour( colour
);
7543 m_cornerLabelWin
->SetBackgroundColour( colour
);
7545 if ( !GetBatchCount() )
7547 m_rowLabelWin
->Refresh();
7548 m_colLabelWin
->Refresh();
7549 m_cornerLabelWin
->Refresh();
7554 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7556 if ( m_labelTextColour
!= colour
)
7558 m_labelTextColour
= colour
;
7559 if ( !GetBatchCount() )
7561 m_rowLabelWin
->Refresh();
7562 m_colLabelWin
->Refresh();
7567 void wxGrid::SetLabelFont( const wxFont
& font
)
7570 if ( !GetBatchCount() )
7572 m_rowLabelWin
->Refresh();
7573 m_colLabelWin
->Refresh();
7577 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7579 // allow old (incorrect) defs to be used
7582 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7583 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7584 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7589 case wxTOP
: vert
= wxALIGN_TOP
; break;
7590 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7591 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7594 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7596 m_rowLabelHorizAlign
= horiz
;
7599 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7601 m_rowLabelVertAlign
= vert
;
7604 if ( !GetBatchCount() )
7606 m_rowLabelWin
->Refresh();
7610 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7612 // allow old (incorrect) defs to be used
7615 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7616 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7617 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7622 case wxTOP
: vert
= wxALIGN_TOP
; break;
7623 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7624 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7627 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7629 m_colLabelHorizAlign
= horiz
;
7632 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7634 m_colLabelVertAlign
= vert
;
7637 if ( !GetBatchCount() )
7639 m_colLabelWin
->Refresh();
7643 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7647 m_table
->SetRowLabelValue( row
, s
);
7648 if ( !GetBatchCount() )
7650 wxRect rect
= CellToRect( row
, 0);
7651 if ( rect
.height
> 0 )
7653 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7655 rect
.width
= m_rowLabelWidth
;
7656 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7662 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7666 m_table
->SetColLabelValue( col
, s
);
7667 if ( !GetBatchCount() )
7669 wxRect rect
= CellToRect( 0, col
);
7670 if ( rect
.width
> 0 )
7672 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7674 rect
.height
= m_colLabelHeight
;
7675 m_colLabelWin
->Refresh( TRUE
, &rect
);
7681 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7683 if ( m_gridLineColour
!= colour
)
7685 m_gridLineColour
= colour
;
7687 wxClientDC
dc( m_gridWin
);
7689 DrawAllGridLines( dc
, wxRegion() );
7694 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7696 if ( m_cellHighlightColour
!= colour
)
7698 m_cellHighlightColour
= colour
;
7700 wxClientDC
dc( m_gridWin
);
7702 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7703 DrawCellHighlight(dc
, attr
);
7708 void wxGrid::SetCellHighlightPenWidth(int width
)
7710 if (m_cellHighlightPenWidth
!= width
) {
7711 m_cellHighlightPenWidth
= width
;
7713 // Just redrawing the cell highlight is not enough since that won't
7714 // make any visible change if the the thickness is getting smaller.
7715 int row
= m_currentCellCoords
.GetRow();
7716 int col
= m_currentCellCoords
.GetCol();
7717 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7719 wxRect rect
= CellToRect(row
, col
);
7720 m_gridWin
->Refresh(TRUE
, &rect
);
7724 void wxGrid::SetCellHighlightROPenWidth(int width
)
7726 if (m_cellHighlightROPenWidth
!= width
) {
7727 m_cellHighlightROPenWidth
= width
;
7729 // Just redrawing the cell highlight is not enough since that won't
7730 // make any visible change if the the thickness is getting smaller.
7731 int row
= m_currentCellCoords
.GetRow();
7732 int col
= m_currentCellCoords
.GetCol();
7733 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7735 wxRect rect
= CellToRect(row
, col
);
7736 m_gridWin
->Refresh(TRUE
, &rect
);
7740 void wxGrid::EnableGridLines( bool enable
)
7742 if ( enable
!= m_gridLinesEnabled
)
7744 m_gridLinesEnabled
= enable
;
7746 if ( !GetBatchCount() )
7750 wxClientDC
dc( m_gridWin
);
7752 DrawAllGridLines( dc
, wxRegion() );
7756 m_gridWin
->Refresh();
7763 int wxGrid::GetDefaultRowSize()
7765 return m_defaultRowHeight
;
7768 int wxGrid::GetRowSize( int row
)
7770 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7772 return GetRowHeight(row
);
7775 int wxGrid::GetDefaultColSize()
7777 return m_defaultColWidth
;
7780 int wxGrid::GetColSize( int col
)
7782 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7784 return GetColWidth(col
);
7787 // ============================================================================
7788 // access to the grid attributes: each of them has a default value in the grid
7789 // itself and may be overidden on a per-cell basis
7790 // ============================================================================
7792 // ----------------------------------------------------------------------------
7793 // setting default attributes
7794 // ----------------------------------------------------------------------------
7796 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7798 m_defaultCellAttr
->SetBackgroundColour(col
);
7800 m_gridWin
->SetBackgroundColour(col
);
7804 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7806 m_defaultCellAttr
->SetTextColour(col
);
7809 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7811 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7814 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7816 m_defaultCellAttr
->SetFont(font
);
7819 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7821 m_defaultCellAttr
->SetRenderer(renderer
);
7824 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7826 m_defaultCellAttr
->SetEditor(editor
);
7829 // ----------------------------------------------------------------------------
7830 // access to the default attrbiutes
7831 // ----------------------------------------------------------------------------
7833 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7835 return m_defaultCellAttr
->GetBackgroundColour();
7838 wxColour
wxGrid::GetDefaultCellTextColour()
7840 return m_defaultCellAttr
->GetTextColour();
7843 wxFont
wxGrid::GetDefaultCellFont()
7845 return m_defaultCellAttr
->GetFont();
7848 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7850 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7853 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7855 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7858 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7860 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7863 // ----------------------------------------------------------------------------
7864 // access to cell attributes
7865 // ----------------------------------------------------------------------------
7867 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7869 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7870 wxColour colour
= attr
->GetBackgroundColour();
7875 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7877 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7878 wxColour colour
= attr
->GetTextColour();
7883 wxFont
wxGrid::GetCellFont( int row
, int col
)
7885 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7886 wxFont font
= attr
->GetFont();
7891 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7893 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7894 attr
->GetAlignment(horiz
, vert
);
7898 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7900 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7901 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7907 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7909 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7910 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7916 bool wxGrid::IsReadOnly(int row
, int col
) const
7918 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7919 bool isReadOnly
= attr
->IsReadOnly();
7924 // ----------------------------------------------------------------------------
7925 // attribute support: cache, automatic provider creation, ...
7926 // ----------------------------------------------------------------------------
7928 bool wxGrid::CanHaveAttributes()
7935 return m_table
->CanHaveAttributes();
7938 void wxGrid::ClearAttrCache()
7940 if ( m_attrCache
.row
!= -1 )
7942 wxSafeDecRef(m_attrCache
.attr
);
7943 m_attrCache
.attr
= NULL
;
7944 m_attrCache
.row
= -1;
7948 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7950 wxGrid
*self
= (wxGrid
*)this; // const_cast
7952 self
->ClearAttrCache();
7953 self
->m_attrCache
.row
= row
;
7954 self
->m_attrCache
.col
= col
;
7955 self
->m_attrCache
.attr
= attr
;
7959 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7961 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7963 *attr
= m_attrCache
.attr
;
7964 wxSafeIncRef(m_attrCache
.attr
);
7966 #ifdef DEBUG_ATTR_CACHE
7967 gs_nAttrCacheHits
++;
7974 #ifdef DEBUG_ATTR_CACHE
7975 gs_nAttrCacheMisses
++;
7981 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7983 wxGridCellAttr
*attr
;
7984 if ( !LookupAttr(row
, col
, &attr
) )
7986 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
7987 CacheAttr(row
, col
, attr
);
7991 attr
->SetDefAttr(m_defaultCellAttr
);
7995 attr
= m_defaultCellAttr
;
8002 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8004 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8005 wxASSERT_MSG( m_table
,
8006 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8008 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8011 attr
= new wxGridCellAttr
;
8013 // artificially inc the ref count to match DecRef() in caller
8015 m_table
->SetAttr(attr
, row
, col
);
8017 attr
->SetDefAttr(m_defaultCellAttr
);
8021 // ----------------------------------------------------------------------------
8022 // setting column attributes (wrappers around SetColAttr)
8023 // ----------------------------------------------------------------------------
8025 void wxGrid::SetColFormatBool(int col
)
8027 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8030 void wxGrid::SetColFormatNumber(int col
)
8032 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8035 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8037 wxString typeName
= wxGRID_VALUE_FLOAT
;
8038 if ( (width
!= -1) || (precision
!= -1) )
8040 typeName
<< _T(':') << width
<< _T(',') << precision
;
8043 SetColFormatCustom(col
, typeName
);
8046 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8048 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8050 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8052 attr
= new wxGridCellAttr
;
8053 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8054 attr
->SetRenderer(renderer
);
8056 SetColAttr(col
, attr
);
8060 // ----------------------------------------------------------------------------
8061 // setting cell attributes: this is forwarded to the table
8062 // ----------------------------------------------------------------------------
8064 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8066 if ( CanHaveAttributes() )
8068 m_table
->SetRowAttr(attr
, row
);
8077 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8079 if ( CanHaveAttributes() )
8081 m_table
->SetColAttr(attr
, col
);
8090 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8092 if ( CanHaveAttributes() )
8094 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8095 attr
->SetBackgroundColour(colour
);
8100 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8102 if ( CanHaveAttributes() )
8104 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8105 attr
->SetTextColour(colour
);
8110 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8112 if ( CanHaveAttributes() )
8114 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8115 attr
->SetFont(font
);
8120 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8122 if ( CanHaveAttributes() )
8124 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8125 attr
->SetAlignment(horiz
, vert
);
8130 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8132 if ( CanHaveAttributes() )
8134 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8135 attr
->SetRenderer(renderer
);
8140 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8142 if ( CanHaveAttributes() )
8144 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8145 attr
->SetEditor(editor
);
8150 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8152 if ( CanHaveAttributes() )
8154 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8155 attr
->SetReadOnly(isReadOnly
);
8160 // ----------------------------------------------------------------------------
8161 // Data type registration
8162 // ----------------------------------------------------------------------------
8164 void wxGrid::RegisterDataType(const wxString
& typeName
,
8165 wxGridCellRenderer
* renderer
,
8166 wxGridCellEditor
* editor
)
8168 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8172 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8174 wxString typeName
= m_table
->GetTypeName(row
, col
);
8175 return GetDefaultEditorForType(typeName
);
8178 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8180 wxString typeName
= m_table
->GetTypeName(row
, col
);
8181 return GetDefaultRendererForType(typeName
);
8185 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8187 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8188 if ( index
== wxNOT_FOUND
)
8190 wxFAIL_MSG(wxT("Unknown data type name"));
8195 return m_typeRegistry
->GetEditor(index
);
8199 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8201 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8202 if ( index
== wxNOT_FOUND
)
8204 wxFAIL_MSG(wxT("Unknown data type name"));
8209 return m_typeRegistry
->GetRenderer(index
);
8213 // ----------------------------------------------------------------------------
8215 // ----------------------------------------------------------------------------
8217 void wxGrid::EnableDragRowSize( bool enable
)
8219 m_canDragRowSize
= enable
;
8223 void wxGrid::EnableDragColSize( bool enable
)
8225 m_canDragColSize
= enable
;
8228 void wxGrid::EnableDragGridSize( bool enable
)
8230 m_canDragGridSize
= enable
;
8234 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8236 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8238 if ( resizeExistingRows
)
8241 if ( !GetBatchCount() )
8246 void wxGrid::SetRowSize( int row
, int height
)
8248 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8250 if ( m_rowHeights
.IsEmpty() )
8252 // need to really create the array
8256 int h
= wxMax( 0, height
);
8257 int diff
= h
- m_rowHeights
[row
];
8259 m_rowHeights
[row
] = h
;
8261 for ( i
= row
; i
< m_numRows
; i
++ )
8263 m_rowBottoms
[i
] += diff
;
8265 if ( !GetBatchCount() )
8269 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8271 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8273 if ( resizeExistingCols
)
8276 if ( !GetBatchCount() )
8281 void wxGrid::SetColSize( int col
, int width
)
8283 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8285 // should we check that it's bigger than GetColMinimalWidth(col) here?
8287 if ( m_colWidths
.IsEmpty() )
8289 // need to really create the array
8293 int w
= wxMax( 0, width
);
8294 int diff
= w
- m_colWidths
[col
];
8295 m_colWidths
[col
] = w
;
8298 for ( i
= col
; i
< m_numCols
; i
++ )
8300 m_colRights
[i
] += diff
;
8302 if ( !GetBatchCount() )
8307 void wxGrid::SetColMinimalWidth( int col
, int width
)
8309 m_colMinWidths
.Put(col
, width
);
8312 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8314 m_rowMinHeights
.Put(row
, width
);
8317 int wxGrid::GetColMinimalWidth(int col
) const
8319 long value
= m_colMinWidths
.Get(col
);
8320 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8323 int wxGrid::GetRowMinimalHeight(int row
) const
8325 long value
= m_rowMinHeights
.Get(row
);
8326 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8329 // ----------------------------------------------------------------------------
8331 // ----------------------------------------------------------------------------
8333 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8335 wxClientDC
dc(m_gridWin
);
8337 // init both of them to avoid compiler warnings, even if weo nly need one
8345 wxCoord extent
, extentMax
= 0;
8346 int max
= column
? m_numRows
: m_numCols
;
8347 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8354 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8355 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8358 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8359 extent
= column
? size
.x
: size
.y
;
8360 if ( extent
> extentMax
)
8371 // now also compare with the column label extent
8373 dc
.SetFont( GetLabelFont() );
8376 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8378 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8380 extent
= column
? w
: h
;
8381 if ( extent
> extentMax
)
8388 // empty column - give default extent (notice that if extentMax is less
8389 // than default extent but != 0, it's ok)
8390 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8396 // leave some space around text
8406 SetColSize(col
, extentMax
);
8407 if ( !GetBatchCount() )
8410 m_gridWin
->GetClientSize( &cw
, &ch
);
8411 wxRect
rect ( CellToRect( 0, col
) );
8413 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8414 rect
.width
= cw
- rect
.x
;
8415 rect
.height
= m_colLabelHeight
;
8416 m_colLabelWin
->Refresh( TRUE
, &rect
);
8420 SetRowSize(row
, extentMax
);
8421 if ( !GetBatchCount() )
8424 m_gridWin
->GetClientSize( &cw
, &ch
);
8425 wxRect
rect ( CellToRect( row
, 0 ) );
8427 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8428 rect
.width
= m_rowLabelWidth
;
8429 rect
.height
= ch
- rect
.y
;
8430 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8436 SetColMinimalWidth(col
, extentMax
);
8438 SetRowMinimalHeight(row
, extentMax
);
8442 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8444 int width
= m_rowLabelWidth
;
8448 for ( int col
= 0; col
< m_numCols
; col
++ )
8452 AutoSizeColumn(col
, setAsMin
);
8455 width
+= GetColWidth(col
);
8462 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8464 int height
= m_colLabelHeight
;
8468 for ( int row
= 0; row
< m_numRows
; row
++ )
8472 AutoSizeRow(row
, setAsMin
);
8475 height
+= GetRowHeight(row
);
8482 void wxGrid::AutoSize()
8485 SetClientSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8488 wxSize
wxGrid::DoGetBestSize() const
8490 // don't set sizes, only calculate them
8491 wxGrid
*self
= (wxGrid
*)this; // const_cast
8493 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8494 self
->SetOrCalcRowSizes(TRUE
));
8503 wxPen
& wxGrid::GetDividerPen() const
8508 // ----------------------------------------------------------------------------
8509 // cell value accessor functions
8510 // ----------------------------------------------------------------------------
8512 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8516 m_table
->SetValue( row
, col
, s
);
8517 if ( !GetBatchCount() )
8519 wxClientDC
dc( m_gridWin
);
8521 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8524 if ( m_currentCellCoords
.GetRow() == row
&&
8525 m_currentCellCoords
.GetCol() == col
&&
8526 IsCellEditControlShown())
8527 // Note: If we are using IsCellEditControlEnabled,
8528 // this interacts badly with calling SetCellValue from
8529 // an EVT_GRID_CELL_CHANGE handler.
8531 HideCellEditControl();
8532 ShowCellEditControl(); // will reread data from table
8539 // ------ Block, row and col selection
8542 void wxGrid::SelectRow( int row
, bool addToSelected
)
8544 if ( IsSelection() && !addToSelected
)
8547 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8551 void wxGrid::SelectCol( int col
, bool addToSelected
)
8553 if ( IsSelection() && !addToSelected
)
8556 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8560 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8561 bool addToSelected
)
8563 if ( IsSelection() && !addToSelected
)
8566 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8567 FALSE
, addToSelected
);
8571 void wxGrid::SelectAll()
8573 if ( m_numRows
> 0 && m_numCols
> 0 )
8574 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8578 // ------ Cell, row and col deselection
8581 void wxGrid::DeselectRow( int row
)
8583 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8585 if ( m_selection
->IsInSelection(row
, 0 ) )
8586 m_selection
->ToggleCellSelection( row
, 0);
8590 int nCols
= GetNumberCols();
8591 for ( int i
= 0; i
< nCols
; i
++ )
8593 if ( m_selection
->IsInSelection(row
, i
) )
8594 m_selection
->ToggleCellSelection( row
, i
);
8599 void wxGrid::DeselectCol( int col
)
8601 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8603 if ( m_selection
->IsInSelection(0, col
) )
8604 m_selection
->ToggleCellSelection( 0, col
);
8608 int nRows
= GetNumberRows();
8609 for ( int i
= 0; i
< nRows
; i
++ )
8611 if ( m_selection
->IsInSelection(i
, col
) )
8612 m_selection
->ToggleCellSelection(i
, col
);
8617 void wxGrid::DeselectCell( int row
, int col
)
8619 if ( m_selection
->IsInSelection(row
, col
) )
8620 m_selection
->ToggleCellSelection(row
, col
);
8623 bool wxGrid::IsSelection()
8625 return ( m_selection
->IsSelection() ||
8626 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8627 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8630 bool wxGrid::IsInSelection( int row
, int col
)
8632 return ( m_selection
->IsInSelection( row
, col
) ||
8633 ( row
>= m_selectingTopLeft
.GetRow() &&
8634 col
>= m_selectingTopLeft
.GetCol() &&
8635 row
<= m_selectingBottomRight
.GetRow() &&
8636 col
<= m_selectingBottomRight
.GetCol() ) );
8639 void wxGrid::ClearSelection()
8641 m_selectingTopLeft
= wxGridNoCellCoords
;
8642 m_selectingBottomRight
= wxGridNoCellCoords
;
8643 m_selection
->ClearSelection();
8647 // This function returns the rectangle that encloses the given block
8648 // in device coords clipped to the client size of the grid window.
8650 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8651 const wxGridCellCoords
&bottomRight
)
8653 wxRect
rect( wxGridNoCellRect
);
8656 cellRect
= CellToRect( topLeft
);
8657 if ( cellRect
!= wxGridNoCellRect
)
8663 rect
= wxRect( 0, 0, 0, 0 );
8666 cellRect
= CellToRect( bottomRight
);
8667 if ( cellRect
!= wxGridNoCellRect
)
8673 return wxGridNoCellRect
;
8676 // convert to scrolled coords
8678 int left
, top
, right
, bottom
;
8679 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8680 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8683 m_gridWin
->GetClientSize( &cw
, &ch
);
8685 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8686 return wxRect( 0, 0, 0, 0);
8688 rect
.SetLeft( wxMax(0, left
) );
8689 rect
.SetTop( wxMax(0, top
) );
8690 rect
.SetRight( wxMin(cw
, right
) );
8691 rect
.SetBottom( wxMin(ch
, bottom
) );
8699 // ------ Grid event classes
8702 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8704 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8705 int row
, int col
, int x
, int y
, bool sel
,
8706 bool control
, bool shift
, bool alt
, bool meta
)
8707 : wxNotifyEvent( type
, id
)
8714 m_control
= control
;
8719 SetEventObject(obj
);
8723 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8725 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8726 int rowOrCol
, int x
, int y
,
8727 bool control
, bool shift
, bool alt
, bool meta
)
8728 : wxNotifyEvent( type
, id
)
8730 m_rowOrCol
= rowOrCol
;
8733 m_control
= control
;
8738 SetEventObject(obj
);
8742 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8744 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8745 const wxGridCellCoords
& topLeft
,
8746 const wxGridCellCoords
& bottomRight
,
8747 bool sel
, bool control
,
8748 bool shift
, bool alt
, bool meta
)
8749 : wxNotifyEvent( type
, id
)
8751 m_topLeft
= topLeft
;
8752 m_bottomRight
= bottomRight
;
8754 m_control
= control
;
8759 SetEventObject(obj
);
8763 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8765 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8766 wxObject
* obj
, int row
,
8767 int col
, wxControl
* ctrl
)
8768 : wxCommandEvent(type
, id
)
8770 SetEventObject(obj
);
8777 #endif // ifndef wxUSE_NEW_GRID