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"
35 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
37 #else // wxUSE_NEW_GRID
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
46 #include "wx/combobox.h"
47 #include "wx/valtext.h"
50 #include "wx/textfile.h"
51 #include "wx/spinctrl.h"
52 #include "wx/tokenzr.h"
55 #include "wx/generic/gridsel.h"
57 #if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_MOTIF(identifier) identifier
63 #if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
66 #define WXUNUSED_GTK(identifier) identifier
69 // Required for wxIs... functions
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
90 wxGridCellCoords coords
;
94 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
129 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
130 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
131 const wxPoint
&pos
, const wxSize
&size
);
136 void OnPaint( wxPaintEvent
& event
);
137 void OnMouseEvent( wxMouseEvent
& event
);
138 void OnMouseWheel( wxMouseEvent
& event
);
139 void OnKeyDown( wxKeyEvent
& event
);
140 void OnKeyUp( wxKeyEvent
& );
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
143 DECLARE_EVENT_TABLE()
147 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
150 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
151 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
152 const wxPoint
&pos
, const wxSize
&size
);
157 void OnPaint( wxPaintEvent
&event
);
158 void OnMouseEvent( wxMouseEvent
& event
);
159 void OnMouseWheel( wxMouseEvent
& event
);
160 void OnKeyDown( wxKeyEvent
& event
);
161 void OnKeyUp( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
168 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
171 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
172 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
178 void OnMouseEvent( wxMouseEvent
& event
);
179 void OnMouseWheel( wxMouseEvent
& event
);
180 void OnKeyDown( wxKeyEvent
& event
);
181 void OnKeyUp( wxKeyEvent
& );
182 void OnPaint( wxPaintEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
185 DECLARE_EVENT_TABLE()
188 class WXDLLEXPORT wxGridWindow
: public wxPanel
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnEraseBackground( wxEraseEvent
& );
219 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
220 DECLARE_EVENT_TABLE()
225 class wxGridCellEditorEvtHandler
: public wxEvtHandler
228 wxGridCellEditorEvtHandler()
229 : m_grid(0), m_editor(0)
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
232 : m_grid(grid
), m_editor(editor
)
235 void OnKeyDown(wxKeyEvent
& event
);
236 void OnChar(wxKeyEvent
& event
);
240 wxGridCellEditor
* m_editor
;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
242 DECLARE_EVENT_TABLE()
246 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
247 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
254 // ----------------------------------------------------------------------------
255 // the internal data representation used by wxGridCellAttrProvider
256 // ----------------------------------------------------------------------------
258 // this class stores attributes set for cells
259 class WXDLLEXPORT wxGridCellAttrData
262 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
263 wxGridCellAttr
*GetAttr(int row
, int col
) const;
264 void UpdateAttrRows( size_t pos
, int numRows
);
265 void UpdateAttrCols( size_t pos
, int numCols
);
268 // searches for the attr for given cell, returns wxNOT_FOUND if not found
269 int FindIndex(int row
, int col
) const;
271 wxGridCellWithAttrArray m_attrs
;
274 // this class stores attributes set for rows or columns
275 class WXDLLEXPORT wxGridRowOrColAttrData
278 // empty ctor to suppress warnings
279 wxGridRowOrColAttrData() { }
280 ~wxGridRowOrColAttrData();
282 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
283 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
284 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
287 wxArrayInt m_rowsOrCols
;
288 wxArrayAttrs m_attrs
;
291 // NB: this is just a wrapper around 3 objects: one which stores cell
292 // attributes, and 2 others for row/col ones
293 class WXDLLEXPORT wxGridCellAttrProviderData
296 wxGridCellAttrData m_cellAttrs
;
297 wxGridRowOrColAttrData m_rowAttrs
,
302 // ----------------------------------------------------------------------------
303 // data structures used for the data type registry
304 // ----------------------------------------------------------------------------
306 struct wxGridDataTypeInfo
308 wxGridDataTypeInfo(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
)
311 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
314 ~wxGridDataTypeInfo()
316 wxSafeDecRef(m_renderer
);
317 wxSafeDecRef(m_editor
);
321 wxGridCellRenderer
* m_renderer
;
322 wxGridCellEditor
* m_editor
;
326 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
329 class WXDLLEXPORT wxGridTypeRegistry
332 wxGridTypeRegistry() {}
333 ~wxGridTypeRegistry();
335 void RegisterDataType(const wxString
& typeName
,
336 wxGridCellRenderer
* renderer
,
337 wxGridCellEditor
* editor
);
339 // find one of already registered data types
340 int FindRegisteredDataType(const wxString
& typeName
);
342 // try to FindRegisteredDataType(), if this fails and typeName is one of
343 // standard typenames, register it and return its index
344 int FindDataType(const wxString
& typeName
);
346 // try to FindDataType(), if it fails see if it is not one of already
347 // registered data types with some params in which case clone the
348 // registered data type and set params for it
349 int FindOrCloneDataType(const wxString
& typeName
);
351 wxGridCellRenderer
* GetRenderer(int index
);
352 wxGridCellEditor
* GetEditor(int index
);
355 wxGridDataTypeInfoArray m_typeinfo
;
358 // ----------------------------------------------------------------------------
359 // conditional compilation
360 // ----------------------------------------------------------------------------
362 #ifndef WXGRID_DRAW_LINES
363 #define WXGRID_DRAW_LINES 1
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 //#define DEBUG_ATTR_CACHE
371 #ifdef DEBUG_ATTR_CACHE
372 static size_t gs_nAttrCacheHits
= 0;
373 static size_t gs_nAttrCacheMisses
= 0;
374 #endif // DEBUG_ATTR_CACHE
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
381 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
384 // TODO: this doesn't work at all, grid cells have different sizes and approx
385 // calculations don't work as because of the size mismatch scrollbars
386 // sometimes fail to be shown when they should be or vice versa
388 // The scroll bars may be a little flakey once in a while, but that is
389 // surely much less horrible than having scroll lines of only 1!!!
391 static const size_t GRID_SCROLL_LINE
= 15; // 1;
394 // the size of hash tables used a bit everywhere (the max number of elements
395 // in these hash tables is the number of rows/columns)
396 static const int GRID_HASH_SIZE
= 100;
398 // ============================================================================
400 // ============================================================================
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 wxGridCellEditor::wxGridCellEditor()
412 wxGridCellEditor::~wxGridCellEditor()
417 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
418 wxWindowID
WXUNUSED(id
),
419 wxEvtHandler
* evtHandler
)
422 m_control
->PushEventHandler(evtHandler
);
425 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
426 wxGridCellAttr
*attr
)
428 // erase the background because we might not fill the cell
429 wxClientDC
dc(m_control
->GetParent());
430 dc
.SetPen(*wxTRANSPARENT_PEN
);
431 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
432 dc
.DrawRectangle(rectCell
);
434 // redraw the control we just painted over
435 m_control
->Refresh();
438 void wxGridCellEditor::Destroy()
442 m_control
->PopEventHandler(TRUE
/* delete it*/);
444 m_control
->Destroy();
449 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
451 wxASSERT_MSG(m_control
,
452 wxT("The wxGridCellEditor must be Created first!"));
453 m_control
->Show(show
);
457 // set the colours/fonts if we have any
460 m_colFgOld
= m_control
->GetForegroundColour();
461 m_control
->SetForegroundColour(attr
->GetTextColour());
463 m_colBgOld
= m_control
->GetBackgroundColour();
464 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
466 m_fontOld
= m_control
->GetFont();
467 m_control
->SetFont(attr
->GetFont());
469 // can't do anything more in the base class version, the other
470 // attributes may only be used by the derived classes
475 // restore the standard colours fonts
476 if ( m_colFgOld
.Ok() )
478 m_control
->SetForegroundColour(m_colFgOld
);
479 m_colFgOld
= wxNullColour
;
482 if ( m_colBgOld
.Ok() )
484 m_control
->SetBackgroundColour(m_colBgOld
);
485 m_colBgOld
= wxNullColour
;
488 if ( m_fontOld
.Ok() )
490 m_control
->SetFont(m_fontOld
);
491 m_fontOld
= wxNullFont
;
496 void wxGridCellEditor::SetSize(const wxRect
& rect
)
498 wxASSERT_MSG(m_control
,
499 wxT("The wxGridCellEditor must be Created first!"));
500 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
503 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
508 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
510 // accept the simple key presses, not anything with Ctrl/Alt/Meta
511 return !(event
.ControlDown() || event
.AltDown());
514 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
519 void wxGridCellEditor::StartingClick()
525 // ----------------------------------------------------------------------------
526 // wxGridCellTextEditor
527 // ----------------------------------------------------------------------------
529 wxGridCellTextEditor::wxGridCellTextEditor()
534 void wxGridCellTextEditor::Create(wxWindow
* parent
,
536 wxEvtHandler
* evtHandler
)
538 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
539 wxDefaultPosition
, wxDefaultSize
540 #if defined(__WXMSW__)
541 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
542 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
546 // TODO: use m_maxChars
548 wxGridCellEditor::Create(parent
, id
, evtHandler
);
551 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
552 wxGridCellAttr
* WXUNUSED(attr
))
554 // as we fill the entire client area, don't do anything here to minimize
558 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
560 wxRect
rect(rectOrig
);
562 // Make the edit control large enough to allow for internal
565 // TODO: remove this if the text ctrl sizing is improved esp. for
568 #if defined(__WXGTK__)
577 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
579 // MB: treat MSW separately here otherwise the caret doesn't show
580 // when the editor is in the first row.
581 #if defined(__WXMSW__)
584 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
587 #if defined(__WXMOTIF__)
591 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
592 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
593 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
594 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
597 wxGridCellEditor::SetSize(rect
);
600 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
602 wxASSERT_MSG(m_control
,
603 wxT("The wxGridCellEditor must be Created first!"));
605 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
607 DoBeginEdit(m_startValue
);
610 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
612 Text()->SetValue(startValue
);
613 Text()->SetInsertionPointEnd();
617 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
620 wxASSERT_MSG(m_control
,
621 wxT("The wxGridCellEditor must be Created first!"));
623 bool changed
= FALSE
;
624 wxString value
= Text()->GetValue();
625 if (value
!= m_startValue
)
629 grid
->GetTable()->SetValue(row
, col
, value
);
631 m_startValue
= wxEmptyString
;
632 Text()->SetValue(m_startValue
);
638 void wxGridCellTextEditor::Reset()
640 wxASSERT_MSG(m_control
,
641 wxT("The wxGridCellEditor must be Created first!"));
643 DoReset(m_startValue
);
646 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
648 Text()->SetValue(startValue
);
649 Text()->SetInsertionPointEnd();
652 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
654 if ( wxGridCellEditor::IsAcceptedKey(event
) )
656 int keycode
= event
.GetKeyCode();
670 case WXK_NUMPAD_MULTIPLY
:
674 case WXK_NUMPAD_SUBTRACT
:
676 case WXK_NUMPAD_DECIMAL
:
678 case WXK_NUMPAD_DIVIDE
:
682 // accept 8 bit chars too if isprint() agrees
683 if ( (keycode
< 255) && (isprint(keycode
)) )
691 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
693 // we don't check for !HasModifiers() because IsAcceptedKey() did it
695 // insert the key in the control
697 int keycode
= event
.GetKeyCode();
710 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
714 case WXK_NUMPAD_MULTIPLY
:
724 case WXK_NUMPAD_SUBTRACT
:
729 case WXK_NUMPAD_DECIMAL
:
734 case WXK_NUMPAD_DIVIDE
:
739 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
741 // FIXME this is not going to work for non letters...
742 if ( !event
.ShiftDown() )
744 keycode
= tolower(keycode
);
747 ch
= (wxChar
)keycode
;
757 Text()->AppendText(ch
);
765 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
766 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
768 #if defined(__WXMOTIF__) || defined(__WXGTK__)
769 // wxMotif needs a little extra help...
770 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
771 wxString
s( Text()->GetValue() );
772 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
774 Text()->SetInsertionPoint( pos
);
776 // the other ports can handle a Return key press
782 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
792 if ( !params
.ToLong(&tmp
) )
794 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
798 m_maxChars
= (size_t)tmp
;
803 // ----------------------------------------------------------------------------
804 // wxGridCellNumberEditor
805 // ----------------------------------------------------------------------------
807 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
813 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
815 wxEvtHandler
* evtHandler
)
819 // create a spin ctrl
820 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
821 wxDefaultPosition
, wxDefaultSize
,
825 wxGridCellEditor::Create(parent
, id
, evtHandler
);
829 // just a text control
830 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
833 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
834 #endif // wxUSE_VALIDATORS
838 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
840 // first get the value
841 wxGridTableBase
*table
= grid
->GetTable();
842 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
844 m_valueOld
= table
->GetValueAsLong(row
, col
);
848 wxString sValue
= table
->GetValue(row
, col
);
849 if (! sValue
.ToLong(&m_valueOld
))
851 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
858 Spin()->SetValue((int)m_valueOld
);
863 DoBeginEdit(GetString());
867 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
875 value
= Spin()->GetValue();
876 changed
= value
!= m_valueOld
;
880 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
885 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
886 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
888 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
894 void wxGridCellNumberEditor::Reset()
898 Spin()->SetValue((int)m_valueOld
);
902 DoReset(GetString());
906 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
908 if ( wxGridCellEditor::IsAcceptedKey(event
) )
910 int keycode
= event
.GetKeyCode();
926 case WXK_NUMPAD_SUBTRACT
:
932 if ( (keycode
< 128) && isdigit(keycode
) )
940 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
944 int keycode
= (int) event
.KeyCode();
945 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
947 wxGridCellTextEditor::StartingKey(event
);
957 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
968 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
972 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
976 // skip the error message below
981 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
985 // ----------------------------------------------------------------------------
986 // wxGridCellFloatEditor
987 // ----------------------------------------------------------------------------
989 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
992 m_precision
= precision
;
995 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
997 wxEvtHandler
* evtHandler
)
999 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1001 #if wxUSE_VALIDATORS
1002 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1003 #endif // wxUSE_VALIDATORS
1006 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1008 // first get the value
1009 wxGridTableBase
*table
= grid
->GetTable();
1010 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1012 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1016 wxString sValue
= table
->GetValue(row
, col
);
1017 if (! sValue
.ToDouble(&m_valueOld
))
1019 wxFAIL_MSG( _T("this cell doesn't have float value") );
1024 DoBeginEdit(GetString());
1027 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1031 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
1033 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1034 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1036 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1046 void wxGridCellFloatEditor::Reset()
1048 DoReset(GetString());
1051 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1053 int keycode
= (int)event
.KeyCode();
1054 if ( isdigit(keycode
) ||
1055 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1057 wxGridCellTextEditor::StartingKey(event
);
1059 // skip Skip() below
1066 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1077 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1081 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1083 m_precision
= (int)tmp
;
1085 // skip the error message below
1090 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1094 wxString
wxGridCellFloatEditor::GetString() const
1097 if ( m_width
== -1 )
1099 // default width/precision
1102 else if ( m_precision
== -1 )
1104 // default precision
1105 fmt
.Printf(_T("%%%d.g"), m_width
);
1109 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1112 return wxString::Format(fmt
, m_valueOld
);
1115 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1117 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1119 int keycode
= event
.GetKeyCode();
1133 case WXK_NUMPAD_ADD
:
1135 case WXK_NUMPAD_SUBTRACT
:
1137 case WXK_NUMPAD_DECIMAL
:
1141 // additionally accept 'e' as in '1e+6'
1142 if ( (keycode
< 128) &&
1143 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1151 #endif // wxUSE_TEXTCTRL
1155 // ----------------------------------------------------------------------------
1156 // wxGridCellBoolEditor
1157 // ----------------------------------------------------------------------------
1159 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1161 wxEvtHandler
* evtHandler
)
1163 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1164 wxDefaultPosition
, wxDefaultSize
,
1167 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1170 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1172 bool resize
= FALSE
;
1173 wxSize size
= m_control
->GetSize();
1174 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1176 // check if the checkbox is not too big/small for this cell
1177 wxSize sizeBest
= m_control
->GetBestSize();
1178 if ( !(size
== sizeBest
) )
1180 // reset to default size if it had been made smaller
1186 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1188 // leave 1 pixel margin
1189 size
.x
= size
.y
= minSize
- 2;
1196 m_control
->SetSize(size
);
1199 // position it in the centre of the rectangle (TODO: support alignment?)
1201 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1202 // the checkbox without label still has some space to the right in wxGTK,
1203 // so shift it to the right
1205 #elif defined(__WXMSW__)
1206 // here too, but in other way
1211 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1214 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1216 m_control
->Show(show
);
1220 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1221 CBox()->SetBackgroundColour(colBg
);
1225 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1227 wxASSERT_MSG(m_control
,
1228 wxT("The wxGridCellEditor must be Created first!"));
1230 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1231 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1234 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1235 m_startValue
= !( !cellval
|| (cellval
== "0") );
1237 CBox()->SetValue(m_startValue
);
1241 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1244 wxASSERT_MSG(m_control
,
1245 wxT("The wxGridCellEditor must be Created first!"));
1247 bool changed
= FALSE
;
1248 bool value
= CBox()->GetValue();
1249 if ( value
!= m_startValue
)
1254 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1255 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1257 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1263 void wxGridCellBoolEditor::Reset()
1265 wxASSERT_MSG(m_control
,
1266 wxT("The wxGridCellEditor must be Created first!"));
1268 CBox()->SetValue(m_startValue
);
1271 void wxGridCellBoolEditor::StartingClick()
1273 CBox()->SetValue(!CBox()->GetValue());
1276 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1278 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1280 int keycode
= event
.GetKeyCode();
1284 case WXK_NUMPAD_MULTIPLY
:
1286 case WXK_NUMPAD_ADD
:
1288 case WXK_NUMPAD_SUBTRACT
:
1299 #endif // wxUSE_CHECKBOX
1303 // ----------------------------------------------------------------------------
1304 // wxGridCellChoiceEditor
1305 // ----------------------------------------------------------------------------
1307 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1308 const wxString choices
[],
1310 : m_allowOthers(allowOthers
)
1314 m_choices
.Alloc(count
);
1315 for ( size_t n
= 0; n
< count
; n
++ )
1317 m_choices
.Add(choices
[n
]);
1322 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1324 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1325 editor
->m_allowOthers
= m_allowOthers
;
1326 editor
->m_choices
= m_choices
;
1331 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1333 wxEvtHandler
* evtHandler
)
1335 size_t count
= m_choices
.GetCount();
1336 wxString
*choices
= new wxString
[count
];
1337 for ( size_t n
= 0; n
< count
; n
++ )
1339 choices
[n
] = m_choices
[n
];
1342 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1343 wxDefaultPosition
, wxDefaultSize
,
1345 m_allowOthers
? 0 : wxCB_READONLY
);
1349 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1352 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1353 wxGridCellAttr
* attr
)
1355 // as we fill the entire client area, don't do anything here to minimize
1358 // TODO: It doesn't actually fill the client area since the height of a
1359 // combo always defaults to the standard... Until someone has time to
1360 // figure out the right rectangle to paint, just do it the normal way...
1361 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1364 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1366 wxASSERT_MSG(m_control
,
1367 wxT("The wxGridCellEditor must be Created first!"));
1369 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1371 Combo()->SetValue(m_startValue
);
1372 size_t count
= m_choices
.GetCount();
1373 for (size_t i
=0; i
<count
; i
++)
1375 if (m_startValue
== m_choices
[i
])
1377 Combo()->SetSelection(i
);
1381 Combo()->SetInsertionPointEnd();
1382 Combo()->SetFocus();
1385 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1388 wxString value
= Combo()->GetValue();
1389 bool changed
= value
!= m_startValue
;
1392 grid
->GetTable()->SetValue(row
, col
, value
);
1394 m_startValue
= wxEmptyString
;
1395 Combo()->SetValue(m_startValue
);
1400 void wxGridCellChoiceEditor::Reset()
1402 Combo()->SetValue(m_startValue
);
1403 Combo()->SetInsertionPointEnd();
1406 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1416 wxStringTokenizer
tk(params
, _T(','));
1417 while ( tk
.HasMoreTokens() )
1419 m_choices
.Add(tk
.GetNextToken());
1423 #endif // wxUSE_COMBOBOX
1425 // ----------------------------------------------------------------------------
1426 // wxGridCellEditorEvtHandler
1427 // ----------------------------------------------------------------------------
1429 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1431 switch ( event
.KeyCode() )
1435 m_grid
->DisableCellEditControl();
1439 m_grid
->GetEventHandler()->ProcessEvent( event
);
1443 case WXK_NUMPAD_ENTER
:
1444 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1445 m_editor
->HandleReturn(event
);
1454 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1456 switch ( event
.KeyCode() )
1461 case WXK_NUMPAD_ENTER
:
1469 // ----------------------------------------------------------------------------
1470 // wxGridCellWorker is an (almost) empty common base class for
1471 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1472 // ----------------------------------------------------------------------------
1474 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1479 wxGridCellWorker::~wxGridCellWorker()
1483 // ============================================================================
1485 // ============================================================================
1487 // ----------------------------------------------------------------------------
1488 // wxGridCellRenderer
1489 // ----------------------------------------------------------------------------
1491 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1492 wxGridCellAttr
& attr
,
1495 int WXUNUSED(row
), int WXUNUSED(col
),
1498 dc
.SetBackgroundMode( wxSOLID
);
1502 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1506 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1509 dc
.SetPen( *wxTRANSPARENT_PEN
);
1510 dc
.DrawRectangle(rect
);
1513 // ----------------------------------------------------------------------------
1514 // wxGridCellStringRenderer
1515 // ----------------------------------------------------------------------------
1517 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1518 wxGridCellAttr
& attr
,
1522 dc
.SetBackgroundMode( wxTRANSPARENT
);
1524 // TODO some special colours for attr.IsReadOnly() case?
1528 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1529 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1533 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1534 dc
.SetTextForeground( attr
.GetTextColour() );
1537 dc
.SetFont( attr
.GetFont() );
1540 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1542 const wxString
& text
)
1544 wxCoord x
= 0, y
= 0, max_x
= 0;
1545 dc
.SetFont(attr
.GetFont());
1546 wxStringTokenizer
tk(text
, _T('\n'));
1547 while ( tk
.HasMoreTokens() )
1549 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1550 max_x
= wxMax(max_x
, x
);
1553 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1555 return wxSize(max_x
, y
);
1558 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1559 wxGridCellAttr
& attr
,
1563 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1566 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1567 wxGridCellAttr
& attr
,
1569 const wxRect
& rectCell
,
1573 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1575 // now we only have to draw the text
1576 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1579 attr
.GetAlignment(&hAlign
, &vAlign
);
1581 wxRect rect
= rectCell
;
1584 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1585 rect
, hAlign
, vAlign
);
1588 // ----------------------------------------------------------------------------
1589 // wxGridCellNumberRenderer
1590 // ----------------------------------------------------------------------------
1592 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1594 wxGridTableBase
*table
= grid
.GetTable();
1596 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1598 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1602 text
= table
->GetValue(row
, col
);
1608 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1609 wxGridCellAttr
& attr
,
1611 const wxRect
& rectCell
,
1615 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1617 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1619 // draw the text right aligned by default
1621 attr
.GetAlignment(&hAlign
, &vAlign
);
1622 hAlign
= wxALIGN_RIGHT
;
1624 wxRect rect
= rectCell
;
1627 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1630 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1631 wxGridCellAttr
& attr
,
1635 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1638 // ----------------------------------------------------------------------------
1639 // wxGridCellFloatRenderer
1640 // ----------------------------------------------------------------------------
1642 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1645 SetPrecision(precision
);
1648 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1650 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1651 renderer
->m_width
= m_width
;
1652 renderer
->m_precision
= m_precision
;
1653 renderer
->m_format
= m_format
;
1658 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1660 wxGridTableBase
*table
= grid
.GetTable();
1665 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1667 val
= table
->GetValueAsDouble(row
, col
);
1672 text
= table
->GetValue(row
, col
);
1673 hasDouble
= text
.ToDouble(&val
);
1680 if ( m_width
== -1 )
1682 if ( m_precision
== -1 )
1684 // default width/precision
1685 m_format
= _T("%f");
1689 m_format
.Printf(_T("%%.%df"), m_precision
);
1692 else if ( m_precision
== -1 )
1694 // default precision
1695 m_format
.Printf(_T("%%%d.f"), m_width
);
1699 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1703 text
.Printf(m_format
, val
);
1706 //else: text already contains the string
1711 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1712 wxGridCellAttr
& attr
,
1714 const wxRect
& rectCell
,
1718 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1720 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1722 // draw the text right aligned by default
1724 attr
.GetAlignment(&hAlign
, &vAlign
);
1725 hAlign
= wxALIGN_RIGHT
;
1727 wxRect rect
= rectCell
;
1730 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1733 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1734 wxGridCellAttr
& attr
,
1738 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1741 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1745 // reset to defaults
1751 wxString tmp
= params
.BeforeFirst(_T(','));
1755 if ( tmp
.ToLong(&width
) )
1757 SetWidth((int)width
);
1761 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1765 tmp
= params
.AfterFirst(_T(','));
1769 if ( tmp
.ToLong(&precision
) )
1771 SetPrecision((int)precision
);
1775 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1783 // ----------------------------------------------------------------------------
1784 // wxGridCellBoolRenderer
1785 // ----------------------------------------------------------------------------
1787 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1789 // FIXME these checkbox size calculations are really ugly...
1791 // between checkmark and box
1792 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1794 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1795 wxGridCellAttr
& WXUNUSED(attr
),
1800 // compute it only once (no locks for MT safeness in GUI thread...)
1801 if ( !ms_sizeCheckMark
.x
)
1803 // get checkbox size
1804 wxCoord checkSize
= 0;
1805 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1806 wxSize size
= checkbox
->GetBestSize();
1807 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1809 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1810 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1811 checkSize
-= size
.y
/ 2;
1816 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1819 return ms_sizeCheckMark
;
1822 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1823 wxGridCellAttr
& attr
,
1829 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1831 // draw a check mark in the centre (ignoring alignment - TODO)
1832 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1834 // don't draw outside the cell
1835 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1836 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1838 // and even leave (at least) 1 pixel margin
1839 size
.x
= size
.y
= minSize
- 2;
1842 // draw a border around checkmark
1844 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1845 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1846 rectBorder
.width
= size
.x
;
1847 rectBorder
.height
= size
.y
;
1850 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1851 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1854 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1855 value
= !( !cellval
|| (cellval
== "0") );
1860 wxRect rectMark
= rectBorder
;
1862 // MSW DrawCheckMark() is weird (and should probably be changed...)
1863 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1867 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1870 dc
.SetTextForeground(attr
.GetTextColour());
1871 dc
.DrawCheckMark(rectMark
);
1874 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1875 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1876 dc
.DrawRectangle(rectBorder
);
1879 // ----------------------------------------------------------------------------
1881 // ----------------------------------------------------------------------------
1883 wxGridCellAttr
*wxGridCellAttr::Clone() const
1885 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1886 if ( HasTextColour() )
1887 attr
->SetTextColour(GetTextColour());
1888 if ( HasBackgroundColour() )
1889 attr
->SetBackgroundColour(GetBackgroundColour());
1891 attr
->SetFont(GetFont());
1892 if ( HasAlignment() )
1893 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1897 attr
->SetRenderer(m_renderer
);
1898 m_renderer
->IncRef();
1902 attr
->SetEditor(m_editor
);
1907 attr
->SetReadOnly();
1909 attr
->SetKind( m_attrkind
);
1911 attr
->SetDefAttr(m_defGridAttr
);
1916 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1918 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1919 SetTextColour(mergefrom
->GetTextColour());
1920 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1921 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1922 if ( !HasFont() && mergefrom
->HasFont() )
1923 SetFont(mergefrom
->GetFont());
1924 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1926 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1927 SetAlignment(hAlign
, vAlign
);
1930 // Directly access member functions as GetRender/Editor don't just return
1931 // m_renderer/m_editor
1933 // Maybe add support for merge of Render and Editor?
1934 if (!HasRenderer() && mergefrom
->HasRenderer() )
1936 m_renderer
= mergefrom
->m_renderer
;
1937 m_renderer
->IncRef();
1939 if ( !HasEditor() && mergefrom
->HasEditor() )
1941 m_editor
= mergefrom
->m_editor
;
1944 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1945 SetReadOnly(mergefrom
->IsReadOnly());
1947 SetDefAttr(mergefrom
->m_defGridAttr
);
1950 const wxColour
& wxGridCellAttr::GetTextColour() const
1952 if (HasTextColour())
1956 else if (m_defGridAttr
!= this)
1958 return m_defGridAttr
->GetTextColour();
1962 wxFAIL_MSG(wxT("Missing default cell attribute"));
1963 return wxNullColour
;
1968 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1970 if (HasBackgroundColour())
1972 else if (m_defGridAttr
!= this)
1973 return m_defGridAttr
->GetBackgroundColour();
1976 wxFAIL_MSG(wxT("Missing default cell attribute"));
1977 return wxNullColour
;
1982 const wxFont
& wxGridCellAttr::GetFont() const
1986 else if (m_defGridAttr
!= this)
1987 return m_defGridAttr
->GetFont();
1990 wxFAIL_MSG(wxT("Missing default cell attribute"));
1996 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2000 if ( hAlign
) *hAlign
= m_hAlign
;
2001 if ( vAlign
) *vAlign
= m_vAlign
;
2003 else if (m_defGridAttr
!= this)
2004 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2007 wxFAIL_MSG(wxT("Missing default cell attribute"));
2012 // GetRenderer and GetEditor use a slightly different decision path about
2013 // which attribute to use. If a non-default attr object has one then it is
2014 // used, otherwise the default editor or renderer is fetched from the grid and
2015 // used. It should be the default for the data type of the cell. If it is
2016 // NULL (because the table has a type that the grid does not have in its
2017 // registry,) then the grid's default editor or renderer is used.
2019 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2021 wxGridCellRenderer
*renderer
;
2023 if ( m_renderer
&& this != m_defGridAttr
)
2025 // use the cells renderer if it has one
2026 renderer
= m_renderer
;
2029 else // no non default cell renderer
2031 // get default renderer for the data type
2034 // GetDefaultRendererForCell() will do IncRef() for us
2035 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2044 if ( this != m_defGridAttr
)
2046 // if we still don't have one then use the grid default
2047 // (no need for IncRef() here neither)
2048 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2050 else // default grid attr
2052 // use m_renderer which we had decided not to use initially
2053 renderer
= m_renderer
;
2060 // we're supposed to always find something
2061 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2066 // same as above, except for s/renderer/editor/g
2067 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2069 wxGridCellEditor
*editor
;
2071 if ( m_editor
&& this != m_defGridAttr
)
2073 // use the cells editor if it has one
2077 else // no non default cell editor
2079 // get default editor for the data type
2082 // GetDefaultEditorForCell() will do IncRef() for us
2083 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2092 if ( this != m_defGridAttr
)
2094 // if we still don't have one then use the grid default
2095 // (no need for IncRef() here neither)
2096 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2098 else // default grid attr
2100 // use m_editor which we had decided not to use initially
2108 // we're supposed to always find something
2109 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2114 // ----------------------------------------------------------------------------
2115 // wxGridCellAttrData
2116 // ----------------------------------------------------------------------------
2118 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2120 int n
= FindIndex(row
, col
);
2121 if ( n
== wxNOT_FOUND
)
2123 // add the attribute
2124 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2128 // free the old attribute
2129 m_attrs
[(size_t)n
].attr
->DecRef();
2133 // change the attribute
2134 m_attrs
[(size_t)n
].attr
= attr
;
2138 // remove this attribute
2139 m_attrs
.RemoveAt((size_t)n
);
2144 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2146 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2148 int n
= FindIndex(row
, col
);
2149 if ( n
!= wxNOT_FOUND
)
2151 attr
= m_attrs
[(size_t)n
].attr
;
2158 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2160 size_t count
= m_attrs
.GetCount();
2161 for ( size_t n
= 0; n
< count
; n
++ )
2163 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2164 wxCoord row
= coords
.GetRow();
2165 if ((size_t)row
>= pos
)
2169 // If rows inserted, include row counter where necessary
2170 coords
.SetRow(row
+ numRows
);
2172 else if (numRows
< 0)
2174 // If rows deleted ...
2175 if ((size_t)row
>= pos
- numRows
)
2177 // ...either decrement row counter (if row still exists)...
2178 coords
.SetRow(row
+ numRows
);
2182 // ...or remove the attribute
2183 m_attrs
.RemoveAt((size_t)n
);
2191 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2193 size_t count
= m_attrs
.GetCount();
2194 for ( size_t n
= 0; n
< count
; n
++ )
2196 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2197 wxCoord col
= coords
.GetCol();
2198 if ( (size_t)col
>= pos
)
2202 // If rows inserted, include row counter where necessary
2203 coords
.SetCol(col
+ numCols
);
2205 else if (numCols
< 0)
2207 // If rows deleted ...
2208 if ((size_t)col
>= pos
- numCols
)
2210 // ...either decrement row counter (if row still exists)...
2211 coords
.SetCol(col
+ numCols
);
2215 // ...or remove the attribute
2216 m_attrs
.RemoveAt((size_t)n
);
2224 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2226 size_t count
= m_attrs
.GetCount();
2227 for ( size_t n
= 0; n
< count
; n
++ )
2229 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2230 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2239 // ----------------------------------------------------------------------------
2240 // wxGridRowOrColAttrData
2241 // ----------------------------------------------------------------------------
2243 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2245 size_t count
= m_attrs
.Count();
2246 for ( size_t n
= 0; n
< count
; n
++ )
2248 m_attrs
[n
]->DecRef();
2252 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2254 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2256 int n
= m_rowsOrCols
.Index(rowOrCol
);
2257 if ( n
!= wxNOT_FOUND
)
2259 attr
= m_attrs
[(size_t)n
];
2266 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2268 int i
= m_rowsOrCols
.Index(rowOrCol
);
2269 if ( i
== wxNOT_FOUND
)
2271 // add the attribute
2272 m_rowsOrCols
.Add(rowOrCol
);
2277 size_t n
= (size_t)i
;
2280 // change the attribute
2281 m_attrs
[n
]->DecRef();
2286 // remove this attribute
2287 m_attrs
[n
]->DecRef();
2288 m_rowsOrCols
.RemoveAt(n
);
2289 m_attrs
.RemoveAt(n
);
2294 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2296 size_t count
= m_attrs
.GetCount();
2297 for ( size_t n
= 0; n
< count
; n
++ )
2299 int & rowOrCol
= m_rowsOrCols
[n
];
2300 if ( (size_t)rowOrCol
>= pos
)
2302 if ( numRowsOrCols
> 0 )
2304 // If rows inserted, include row counter where necessary
2305 rowOrCol
+= numRowsOrCols
;
2307 else if ( numRowsOrCols
< 0)
2309 // If rows deleted, either decrement row counter (if row still exists)
2310 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2311 rowOrCol
+= numRowsOrCols
;
2314 m_rowsOrCols
.RemoveAt((size_t)n
);
2315 m_attrs
.RemoveAt((size_t)n
);
2323 // ----------------------------------------------------------------------------
2324 // wxGridCellAttrProvider
2325 // ----------------------------------------------------------------------------
2327 wxGridCellAttrProvider::wxGridCellAttrProvider()
2329 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2332 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2337 void wxGridCellAttrProvider::InitData()
2339 m_data
= new wxGridCellAttrProviderData
;
2342 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2343 wxGridCellAttr::wxAttrKind kind
) const
2345 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2350 case (wxGridCellAttr::Any
):
2351 //Get cached merge attributes.
2352 // Currenlty not used as no cache implemented as not mutiable
2353 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2356 //Basicaly implement old version.
2357 //Also check merge cache, so we don't have to re-merge every time..
2358 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2359 *attrrow
= (wxGridCellAttr
*)NULL
,
2360 *attrcol
= (wxGridCellAttr
*)NULL
;
2362 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2363 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2364 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2366 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2367 // Two or move are non NULL
2368 attr
= new wxGridCellAttr
;
2369 attr
->SetKind(wxGridCellAttr::Merged
);
2373 attr
->MergeWith(attrcell
);
2377 attr
->MergeWith(attrcol
);
2381 attr
->MergeWith(attrrow
);
2384 //store merge attr if cache implemented
2386 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2390 // one or none is non null return it or null.
2391 if(attrrow
) attr
= attrrow
;
2392 if(attrcol
) attr
= attrcol
;
2393 if(attrcell
) attr
= attrcell
;
2397 case (wxGridCellAttr::Cell
):
2398 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2400 case (wxGridCellAttr::Col
):
2401 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2403 case (wxGridCellAttr::Row
):
2404 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2408 // (wxGridCellAttr::Default):
2409 // (wxGridCellAttr::Merged):
2416 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2422 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2425 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2430 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2433 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2438 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2441 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2445 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2447 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2451 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2455 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2457 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2461 // ----------------------------------------------------------------------------
2462 // wxGridTypeRegistry
2463 // ----------------------------------------------------------------------------
2465 wxGridTypeRegistry::~wxGridTypeRegistry()
2467 size_t count
= m_typeinfo
.Count();
2468 for ( size_t i
= 0; i
< count
; i
++ )
2469 delete m_typeinfo
[i
];
2473 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2474 wxGridCellRenderer
* renderer
,
2475 wxGridCellEditor
* editor
)
2477 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2479 // is it already registered?
2480 int loc
= FindRegisteredDataType(typeName
);
2481 if ( loc
!= wxNOT_FOUND
)
2483 delete m_typeinfo
[loc
];
2484 m_typeinfo
[loc
] = info
;
2488 m_typeinfo
.Add(info
);
2492 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2494 size_t count
= m_typeinfo
.GetCount();
2495 for ( size_t i
= 0; i
< count
; i
++ )
2497 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2506 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2508 int index
= FindRegisteredDataType(typeName
);
2509 if ( index
== wxNOT_FOUND
)
2511 // check whether this is one of the standard ones, in which case
2512 // register it "on the fly"
2514 if ( typeName
== wxGRID_VALUE_STRING
)
2516 RegisterDataType(wxGRID_VALUE_STRING
,
2517 new wxGridCellStringRenderer
,
2518 new wxGridCellTextEditor
);
2520 #endif // wxUSE_TEXTCTRL
2522 if ( typeName
== wxGRID_VALUE_BOOL
)
2524 RegisterDataType(wxGRID_VALUE_BOOL
,
2525 new wxGridCellBoolRenderer
,
2526 new wxGridCellBoolEditor
);
2528 #endif // wxUSE_CHECKBOX
2530 if ( typeName
== wxGRID_VALUE_NUMBER
)
2532 RegisterDataType(wxGRID_VALUE_NUMBER
,
2533 new wxGridCellNumberRenderer
,
2534 new wxGridCellNumberEditor
);
2536 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2538 RegisterDataType(wxGRID_VALUE_FLOAT
,
2539 new wxGridCellFloatRenderer
,
2540 new wxGridCellFloatEditor
);
2542 #endif // wxUSE_TEXTCTRL
2544 if ( typeName
== wxGRID_VALUE_CHOICE
)
2546 RegisterDataType(wxGRID_VALUE_CHOICE
,
2547 new wxGridCellStringRenderer
,
2548 new wxGridCellChoiceEditor
);
2550 #endif // wxUSE_COMBOBOX
2555 // we get here only if just added the entry for this type, so return
2557 index
= m_typeinfo
.GetCount() - 1;
2563 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2565 int index
= FindDataType(typeName
);
2566 if ( index
== wxNOT_FOUND
)
2568 // the first part of the typename is the "real" type, anything after ':'
2569 // are the parameters for the renderer
2570 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2571 if ( index
== wxNOT_FOUND
)
2576 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2577 wxGridCellRenderer
*rendererOld
= renderer
;
2578 renderer
= renderer
->Clone();
2579 rendererOld
->DecRef();
2581 wxGridCellEditor
*editor
= GetEditor(index
);
2582 wxGridCellEditor
*editorOld
= editor
;
2583 editor
= editor
->Clone();
2584 editorOld
->DecRef();
2586 // do it even if there are no parameters to reset them to defaults
2587 wxString params
= typeName
.AfterFirst(_T(':'));
2588 renderer
->SetParameters(params
);
2589 editor
->SetParameters(params
);
2591 // register the new typename
2592 RegisterDataType(typeName
, renderer
, editor
);
2594 // we just registered it, it's the last one
2595 index
= m_typeinfo
.GetCount() - 1;
2601 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2603 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2609 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2611 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2617 // ----------------------------------------------------------------------------
2619 // ----------------------------------------------------------------------------
2621 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2624 wxGridTableBase::wxGridTableBase()
2626 m_view
= (wxGrid
*) NULL
;
2627 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2630 wxGridTableBase::~wxGridTableBase()
2632 delete m_attrProvider
;
2635 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2637 delete m_attrProvider
;
2638 m_attrProvider
= attrProvider
;
2641 bool wxGridTableBase::CanHaveAttributes()
2643 if ( ! GetAttrProvider() )
2645 // use the default attr provider by default
2646 SetAttrProvider(new wxGridCellAttrProvider
);
2651 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2653 if ( m_attrProvider
)
2654 return m_attrProvider
->GetAttr(row
, col
, kind
);
2656 return (wxGridCellAttr
*)NULL
;
2659 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2661 if ( m_attrProvider
)
2663 attr
->SetKind(wxGridCellAttr::Cell
);
2664 m_attrProvider
->SetAttr(attr
, row
, col
);
2668 // as we take ownership of the pointer and don't store it, we must
2674 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2676 if ( m_attrProvider
)
2678 attr
->SetKind(wxGridCellAttr::Row
);
2679 m_attrProvider
->SetRowAttr(attr
, row
);
2683 // as we take ownership of the pointer and don't store it, we must
2689 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2691 if ( m_attrProvider
)
2693 attr
->SetKind(wxGridCellAttr::Col
);
2694 m_attrProvider
->SetColAttr(attr
, col
);
2698 // as we take ownership of the pointer and don't store it, we must
2704 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2705 size_t WXUNUSED(numRows
) )
2707 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2712 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2714 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2719 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2720 size_t WXUNUSED(numRows
) )
2722 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2727 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2728 size_t WXUNUSED(numCols
) )
2730 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2735 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2737 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2742 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2743 size_t WXUNUSED(numCols
) )
2745 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2751 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2754 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2755 // how much it makes sense to us geeks.
2759 wxString
wxGridTableBase::GetColLabelValue( int col
)
2761 // default col labels are:
2762 // cols 0 to 25 : A-Z
2763 // cols 26 to 675 : AA-ZZ
2768 for ( n
= 1; ; n
++ )
2770 s
+= (_T('A') + (wxChar
)( col%26
));
2772 if ( col
< 0 ) break;
2775 // reverse the string...
2777 for ( i
= 0; i
< n
; i
++ )
2786 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2788 return wxGRID_VALUE_STRING
;
2791 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2792 const wxString
& typeName
)
2794 return typeName
== wxGRID_VALUE_STRING
;
2797 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2799 return CanGetValueAs(row
, col
, typeName
);
2802 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2807 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2812 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2817 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2818 long WXUNUSED(value
) )
2822 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2823 double WXUNUSED(value
) )
2827 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2828 bool WXUNUSED(value
) )
2833 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2834 const wxString
& WXUNUSED(typeName
) )
2839 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2840 const wxString
& WXUNUSED(typeName
),
2841 void* WXUNUSED(value
) )
2845 //////////////////////////////////////////////////////////////////////
2847 // Message class for the grid table to send requests and notifications
2851 wxGridTableMessage::wxGridTableMessage()
2853 m_table
= (wxGridTableBase
*) NULL
;
2859 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2860 int commandInt1
, int commandInt2
)
2864 m_comInt1
= commandInt1
;
2865 m_comInt2
= commandInt2
;
2870 //////////////////////////////////////////////////////////////////////
2872 // A basic grid table for string data. An object of this class will
2873 // created by wxGrid if you don't specify an alternative table class.
2876 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2878 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2880 wxGridStringTable::wxGridStringTable()
2885 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2890 m_data
.Alloc( numRows
);
2893 sa
.Alloc( numCols
);
2894 for ( col
= 0; col
< numCols
; col
++ )
2896 sa
.Add( wxEmptyString
);
2899 for ( row
= 0; row
< numRows
; row
++ )
2905 wxGridStringTable::~wxGridStringTable()
2909 int wxGridStringTable::GetNumberRows()
2911 return m_data
.GetCount();
2914 int wxGridStringTable::GetNumberCols()
2916 if ( m_data
.GetCount() > 0 )
2917 return m_data
[0].GetCount();
2922 wxString
wxGridStringTable::GetValue( int row
, int col
)
2924 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2925 _T("invalid row or column index in wxGridStringTable") );
2927 return m_data
[row
][col
];
2930 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2932 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2933 _T("invalid row or column index in wxGridStringTable") );
2935 m_data
[row
][col
] = value
;
2938 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2940 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2941 _T("invalid row or column index in wxGridStringTable") );
2943 return (m_data
[row
][col
] == wxEmptyString
);
2946 void wxGridStringTable::Clear()
2949 int numRows
, numCols
;
2951 numRows
= m_data
.GetCount();
2954 numCols
= m_data
[0].GetCount();
2956 for ( row
= 0; row
< numRows
; row
++ )
2958 for ( col
= 0; col
< numCols
; col
++ )
2960 m_data
[row
][col
] = wxEmptyString
;
2967 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2971 size_t curNumRows
= m_data
.GetCount();
2972 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2973 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2975 if ( pos
>= curNumRows
)
2977 return AppendRows( numRows
);
2981 sa
.Alloc( curNumCols
);
2982 for ( col
= 0; col
< curNumCols
; col
++ )
2984 sa
.Add( wxEmptyString
);
2987 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2989 m_data
.Insert( sa
, row
);
2993 wxGridTableMessage
msg( this,
2994 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2998 GetView()->ProcessTableMessage( msg
);
3004 bool wxGridStringTable::AppendRows( size_t numRows
)
3008 size_t curNumRows
= m_data
.GetCount();
3009 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3010 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3013 if ( curNumCols
> 0 )
3015 sa
.Alloc( curNumCols
);
3016 for ( col
= 0; col
< curNumCols
; col
++ )
3018 sa
.Add( wxEmptyString
);
3022 for ( row
= 0; row
< numRows
; row
++ )
3029 wxGridTableMessage
msg( this,
3030 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3033 GetView()->ProcessTableMessage( msg
);
3039 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3043 size_t curNumRows
= m_data
.GetCount();
3045 if ( pos
>= curNumRows
)
3048 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3049 pos
, numRows
, curNumRows
);
3050 wxFAIL_MSG( errmsg
);
3054 if ( numRows
> curNumRows
- pos
)
3056 numRows
= curNumRows
- pos
;
3059 if ( numRows
>= curNumRows
)
3061 m_data
.Empty(); // don't release memory just yet
3065 for ( n
= 0; n
< numRows
; n
++ )
3067 m_data
.RemoveAt( pos
);
3072 wxGridTableMessage
msg( this,
3073 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3077 GetView()->ProcessTableMessage( msg
);
3083 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3087 size_t curNumRows
= m_data
.GetCount();
3088 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3089 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3091 if ( pos
>= curNumCols
)
3093 return AppendCols( numCols
);
3096 for ( row
= 0; row
< curNumRows
; row
++ )
3098 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3100 m_data
[row
].Insert( wxEmptyString
, col
);
3105 wxGridTableMessage
msg( this,
3106 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3110 GetView()->ProcessTableMessage( msg
);
3116 bool wxGridStringTable::AppendCols( size_t numCols
)
3120 size_t curNumRows
= m_data
.GetCount();
3124 // TODO: something better than this ?
3126 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3131 for ( row
= 0; row
< curNumRows
; row
++ )
3133 for ( n
= 0; n
< numCols
; n
++ )
3135 m_data
[row
].Add( wxEmptyString
);
3141 wxGridTableMessage
msg( this,
3142 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3145 GetView()->ProcessTableMessage( msg
);
3151 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3155 size_t curNumRows
= m_data
.GetCount();
3156 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3157 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3159 if ( pos
>= curNumCols
)
3162 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3163 pos
, numCols
, curNumCols
);
3164 wxFAIL_MSG( errmsg
);
3168 if ( numCols
> curNumCols
- pos
)
3170 numCols
= curNumCols
- pos
;
3173 for ( row
= 0; row
< curNumRows
; row
++ )
3175 if ( numCols
>= curNumCols
)
3177 m_data
[row
].Clear();
3181 for ( n
= 0; n
< numCols
; n
++ )
3183 m_data
[row
].RemoveAt( pos
);
3189 wxGridTableMessage
msg( this,
3190 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3194 GetView()->ProcessTableMessage( msg
);
3200 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3202 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3204 // using default label
3206 return wxGridTableBase::GetRowLabelValue( row
);
3210 return m_rowLabels
[ row
];
3214 wxString
wxGridStringTable::GetColLabelValue( int col
)
3216 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3218 // using default label
3220 return wxGridTableBase::GetColLabelValue( col
);
3224 return m_colLabels
[ col
];
3228 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3230 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3232 int n
= m_rowLabels
.GetCount();
3234 for ( i
= n
; i
<= row
; i
++ )
3236 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3240 m_rowLabels
[row
] = value
;
3243 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3245 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3247 int n
= m_colLabels
.GetCount();
3249 for ( i
= n
; i
<= col
; i
++ )
3251 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3255 m_colLabels
[col
] = value
;
3260 //////////////////////////////////////////////////////////////////////
3261 //////////////////////////////////////////////////////////////////////
3263 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3265 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3266 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3267 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3268 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3269 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3270 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3273 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3275 const wxPoint
&pos
, const wxSize
&size
)
3276 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3281 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3285 // NO - don't do this because it will set both the x and y origin
3286 // coords to match the parent scrolled window and we just want to
3287 // set the y coord - MB
3289 // m_owner->PrepareDC( dc );
3292 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3293 dc
.SetDeviceOrigin( 0, -y
);
3295 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3296 m_owner
->DrawRowLabels( dc
, rows
);
3300 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3302 m_owner
->ProcessRowLabelMouseEvent( event
);
3306 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3308 m_owner
->GetEventHandler()->ProcessEvent(event
);
3312 // This seems to be required for wxMotif otherwise the mouse
3313 // cursor must be in the cell edit control to get key events
3315 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3317 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3320 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3322 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3327 //////////////////////////////////////////////////////////////////////
3329 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3331 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3332 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3333 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3334 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3335 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3336 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3339 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3341 const wxPoint
&pos
, const wxSize
&size
)
3342 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3347 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3351 // NO - don't do this because it will set both the x and y origin
3352 // coords to match the parent scrolled window and we just want to
3353 // set the x coord - MB
3355 // m_owner->PrepareDC( dc );
3358 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3359 dc
.SetDeviceOrigin( -x
, 0 );
3361 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3362 m_owner
->DrawColLabels( dc
, cols
);
3366 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3368 m_owner
->ProcessColLabelMouseEvent( event
);
3371 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3373 m_owner
->GetEventHandler()->ProcessEvent(event
);
3377 // This seems to be required for wxMotif otherwise the mouse
3378 // cursor must be in the cell edit control to get key events
3380 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3382 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3385 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3387 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3392 //////////////////////////////////////////////////////////////////////
3394 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3396 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3397 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3398 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3399 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3400 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3401 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3404 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3406 const wxPoint
&pos
, const wxSize
&size
)
3407 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3412 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3416 int client_height
= 0;
3417 int client_width
= 0;
3418 GetClientSize( &client_width
, &client_height
);
3420 dc
.SetPen( *wxBLACK_PEN
);
3421 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3422 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3424 dc
.SetPen( *wxWHITE_PEN
);
3425 dc
.DrawLine( 0, 0, client_width
, 0 );
3426 dc
.DrawLine( 0, 0, 0, client_height
);
3430 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3432 m_owner
->ProcessCornerLabelMouseEvent( event
);
3436 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3438 m_owner
->GetEventHandler()->ProcessEvent(event
);
3441 // This seems to be required for wxMotif otherwise the mouse
3442 // cursor must be in the cell edit control to get key events
3444 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3446 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3449 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3451 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3456 //////////////////////////////////////////////////////////////////////
3458 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3460 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3461 EVT_PAINT( wxGridWindow::OnPaint
)
3462 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3463 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3464 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3465 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3466 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3469 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3470 wxGridRowLabelWindow
*rowLblWin
,
3471 wxGridColLabelWindow
*colLblWin
,
3472 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3473 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3476 m_rowLabelWin
= rowLblWin
;
3477 m_colLabelWin
= colLblWin
;
3478 SetBackgroundColour(_T("WHITE"));
3482 wxGridWindow::~wxGridWindow()
3487 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3489 wxPaintDC
dc( this );
3490 m_owner
->PrepareDC( dc
);
3491 wxRegion reg
= GetUpdateRegion();
3492 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3493 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3494 #if WXGRID_DRAW_LINES
3495 m_owner
->DrawAllGridLines( dc
, reg
);
3497 m_owner
->DrawGridSpace( dc
);
3498 m_owner
->DrawHighlight( dc
, DirtyCells
);
3502 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3504 wxPanel::ScrollWindow( dx
, dy
, rect
);
3505 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3506 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3510 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3512 m_owner
->ProcessGridCellMouseEvent( event
);
3515 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3517 m_owner
->GetEventHandler()->ProcessEvent(event
);
3520 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3521 // cursor must be in the cell edit control to get key events
3523 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3525 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3528 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3530 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3533 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3538 //////////////////////////////////////////////////////////////////////
3541 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3543 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3544 EVT_PAINT( wxGrid::OnPaint
)
3545 EVT_SIZE( wxGrid::OnSize
)
3546 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3547 EVT_KEY_UP( wxGrid::OnKeyUp
)
3548 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3551 wxGrid::wxGrid( wxWindow
*parent
,
3556 const wxString
& name
)
3557 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3558 m_colMinWidths(GRID_HASH_SIZE
),
3559 m_rowMinHeights(GRID_HASH_SIZE
)
3567 // Must do this or ~wxScrollHelper will pop the wrong event handler
3568 SetTargetWindow(this);
3570 wxSafeDecRef(m_defaultCellAttr
);
3572 #ifdef DEBUG_ATTR_CACHE
3573 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3574 wxPrintf(_T("wxGrid attribute cache statistics: "
3575 "total: %u, hits: %u (%u%%)\n"),
3576 total
, gs_nAttrCacheHits
,
3577 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3583 delete m_typeRegistry
;
3589 // ----- internal init and update functions
3592 void wxGrid::Create()
3594 m_created
= FALSE
; // set to TRUE by CreateGrid
3596 m_table
= (wxGridTableBase
*) NULL
;
3599 m_cellEditCtrlEnabled
= FALSE
;
3601 m_defaultCellAttr
= new wxGridCellAttr
;
3602 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3604 // Set default cell attributes
3605 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3606 m_defaultCellAttr
->SetFont(GetFont());
3607 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3608 m_defaultCellAttr
->SetTextColour(
3609 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3610 m_defaultCellAttr
->SetBackgroundColour(
3611 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3612 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3613 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3618 m_currentCellCoords
= wxGridNoCellCoords
;
3620 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3621 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3623 // create the type registry
3624 m_typeRegistry
= new wxGridTypeRegistry
;
3626 // subwindow components that make up the wxGrid
3627 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3632 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3637 m_colLabelWin
= new wxGridColLabelWindow( this,
3642 m_gridWin
= new wxGridWindow( this,
3649 SetTargetWindow( m_gridWin
);
3655 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3656 wxGrid::wxGridSelectionModes selmode
)
3658 wxCHECK_MSG( !m_created
,
3660 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3662 m_numRows
= numRows
;
3663 m_numCols
= numCols
;
3665 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3666 m_table
->SetView( this );
3668 m_selection
= new wxGridSelection( this, selmode
);
3677 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3679 wxCHECK_RET( m_created
,
3680 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3682 m_selection
->SetSelectionMode( selmode
);
3685 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3686 wxGrid::wxGridSelectionModes selmode
)
3690 // RD: Actually, this should probably be allowed. I think it would be
3691 // nice to be able to switch multiple Tables in and out of a single
3692 // View at runtime. Is there anything in the implmentation that would
3695 // At least, you now have to cope with m_selection
3696 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3701 m_numRows
= table
->GetNumberRows();
3702 m_numCols
= table
->GetNumberCols();
3705 m_table
->SetView( this );
3708 m_selection
= new wxGridSelection( this, selmode
);
3721 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3722 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3724 if ( m_rowLabelWin
)
3726 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3730 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3733 m_labelTextColour
= wxColour( _T("BLACK") );
3736 m_attrCache
.row
= -1;
3738 // TODO: something better than this ?
3740 m_labelFont
= this->GetFont();
3741 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3743 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3744 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3746 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3747 m_colLabelVertAlign
= wxALIGN_TOP
;
3749 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3750 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3752 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3753 m_defaultRowHeight
+= 8;
3755 m_defaultRowHeight
+= 4;
3758 m_gridLineColour
= wxColour( 128, 128, 255 );
3759 m_gridLinesEnabled
= TRUE
;
3760 m_cellHighlightColour
= m_gridLineColour
;
3761 m_cellHighlightPenWidth
= 2;
3762 m_cellHighlightROPenWidth
= 1;
3764 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3765 m_winCapture
= (wxWindow
*)NULL
;
3766 m_canDragRowSize
= TRUE
;
3767 m_canDragColSize
= TRUE
;
3768 m_canDragGridSize
= TRUE
;
3770 m_dragRowOrCol
= -1;
3771 m_isDragging
= FALSE
;
3772 m_startDragPos
= wxDefaultPosition
;
3774 m_waitForSlowClick
= FALSE
;
3776 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3777 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3779 m_currentCellCoords
= wxGridNoCellCoords
;
3781 m_selectingTopLeft
= wxGridNoCellCoords
;
3782 m_selectingBottomRight
= wxGridNoCellCoords
;
3783 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3784 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3786 m_editable
= TRUE
; // default for whole grid
3788 m_inOnKeyDown
= FALSE
;
3795 // ----------------------------------------------------------------------------
3796 // the idea is to call these functions only when necessary because they create
3797 // quite big arrays which eat memory mostly unnecessary - in particular, if
3798 // default widths/heights are used for all rows/columns, we may not use these
3801 // with some extra code, it should be possible to only store the
3802 // widths/heights different from default ones but this will be done later...
3803 // ----------------------------------------------------------------------------
3805 void wxGrid::InitRowHeights()
3807 m_rowHeights
.Empty();
3808 m_rowBottoms
.Empty();
3810 m_rowHeights
.Alloc( m_numRows
);
3811 m_rowBottoms
.Alloc( m_numRows
);
3814 for ( int i
= 0; i
< m_numRows
; i
++ )
3816 m_rowHeights
.Add( m_defaultRowHeight
);
3817 rowBottom
+= m_defaultRowHeight
;
3818 m_rowBottoms
.Add( rowBottom
);
3822 void wxGrid::InitColWidths()
3824 m_colWidths
.Empty();
3825 m_colRights
.Empty();
3827 m_colWidths
.Alloc( m_numCols
);
3828 m_colRights
.Alloc( m_numCols
);
3830 for ( int i
= 0; i
< m_numCols
; i
++ )
3832 m_colWidths
.Add( m_defaultColWidth
);
3833 colRight
+= m_defaultColWidth
;
3834 m_colRights
.Add( colRight
);
3838 int wxGrid::GetColWidth(int col
) const
3840 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3843 int wxGrid::GetColLeft(int col
) const
3845 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3846 : m_colRights
[col
] - m_colWidths
[col
];
3849 int wxGrid::GetColRight(int col
) const
3851 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3855 int wxGrid::GetRowHeight(int row
) const
3857 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3860 int wxGrid::GetRowTop(int row
) const
3862 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3863 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3866 int wxGrid::GetRowBottom(int row
) const
3868 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3869 : m_rowBottoms
[row
];
3872 void wxGrid::CalcDimensions()
3875 GetClientSize( &cw
, &ch
);
3877 if ( m_colLabelWin
->IsShown() )
3878 cw
-= m_rowLabelWidth
;
3879 if ( m_rowLabelWin
->IsShown() )
3880 ch
-= m_colLabelHeight
;
3883 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3884 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3886 // preserve (more or less) the previous position
3888 GetViewStart( &x
, &y
);
3889 // maybe we don't need scrollbars at all? and if we do, transform w and h
3890 // from pixels into logical units
3897 w
= (w
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3907 h
= (h
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3912 // do set scrollbar parameters
3913 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3914 w
, h
, x
, y
, (GetBatchCount() != 0));
3918 void wxGrid::CalcWindowSizes()
3921 GetClientSize( &cw
, &ch
);
3923 if ( m_cornerLabelWin
->IsShown() )
3924 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3926 if ( m_colLabelWin
->IsShown() )
3927 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3929 if ( m_rowLabelWin
->IsShown() )
3930 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3932 if ( m_gridWin
->IsShown() )
3933 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3937 // this is called when the grid table sends a message to say that it
3938 // has been redimensioned
3940 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3943 bool result
= FALSE
;
3946 // if we were using the default widths/heights so far, we must change them
3948 if ( m_colWidths
.IsEmpty() )
3953 if ( m_rowHeights
.IsEmpty() )
3959 switch ( msg
.GetId() )
3961 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3963 size_t pos
= msg
.GetCommandInt();
3964 int numRows
= msg
.GetCommandInt2();
3966 m_numRows
+= numRows
;
3968 if ( !m_rowHeights
.IsEmpty() )
3970 for ( i
= 0; i
< numRows
; i
++ )
3972 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3973 m_rowBottoms
.Insert( 0, pos
);
3977 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3979 for ( i
= pos
; 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 m_selection
->UpdateRows( pos
, numRows
);
3993 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3995 attrProvider
->UpdateAttrRows( pos
, numRows
);
3997 if ( !GetBatchCount() )
4000 m_rowLabelWin
->Refresh();
4006 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4008 int numRows
= msg
.GetCommandInt();
4009 int oldNumRows
= m_numRows
;
4010 m_numRows
+= numRows
;
4012 if ( !m_rowHeights
.IsEmpty() )
4014 for ( i
= 0; i
< numRows
; i
++ )
4016 m_rowHeights
.Add( m_defaultRowHeight
);
4017 m_rowBottoms
.Add( 0 );
4021 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4023 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4025 bottom
+= m_rowHeights
[i
];
4026 m_rowBottoms
[i
] = bottom
;
4029 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4031 // if we have just inserted cols into an empty grid the current
4032 // cell will be undefined...
4034 SetCurrentCell( 0, 0 );
4036 if ( !GetBatchCount() )
4039 m_rowLabelWin
->Refresh();
4045 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4047 size_t pos
= msg
.GetCommandInt();
4048 int numRows
= msg
.GetCommandInt2();
4049 m_numRows
-= numRows
;
4051 if ( !m_rowHeights
.IsEmpty() )
4053 for ( i
= 0; i
< numRows
; i
++ )
4055 m_rowHeights
.RemoveAt( pos
);
4056 m_rowBottoms
.RemoveAt( pos
);
4060 for ( i
= 0; i
< m_numRows
; i
++ )
4062 h
+= m_rowHeights
[i
];
4063 m_rowBottoms
[i
] = h
;
4068 m_currentCellCoords
= wxGridNoCellCoords
;
4072 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4073 m_currentCellCoords
.Set( 0, 0 );
4075 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4076 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4078 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4079 // ifdef'd out following patch from Paul Gammans
4081 // No need to touch column attributes, unless we
4082 // removed _all_ rows, in this case, we remove
4083 // all column attributes.
4084 // I hate to do this here, but the
4085 // needed data is not available inside UpdateAttrRows.
4086 if ( !GetNumberRows() )
4087 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4090 if ( !GetBatchCount() )
4093 m_rowLabelWin
->Refresh();
4099 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4101 size_t pos
= msg
.GetCommandInt();
4102 int numCols
= msg
.GetCommandInt2();
4103 m_numCols
+= numCols
;
4105 if ( !m_colWidths
.IsEmpty() )
4107 for ( i
= 0; i
< numCols
; i
++ )
4109 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4110 m_colRights
.Insert( 0, pos
);
4114 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4116 for ( i
= pos
; i
< m_numCols
; i
++ )
4118 right
+= m_colWidths
[i
];
4119 m_colRights
[i
] = right
;
4122 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4124 // if we have just inserted cols into an empty grid the current
4125 // cell will be undefined...
4127 SetCurrentCell( 0, 0 );
4129 m_selection
->UpdateCols( pos
, numCols
);
4130 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4132 attrProvider
->UpdateAttrCols( pos
, numCols
);
4133 if ( !GetBatchCount() )
4136 m_colLabelWin
->Refresh();
4143 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4145 int numCols
= msg
.GetCommandInt();
4146 int oldNumCols
= m_numCols
;
4147 m_numCols
+= numCols
;
4148 if ( !m_colWidths
.IsEmpty() )
4150 for ( i
= 0; i
< numCols
; i
++ )
4152 m_colWidths
.Add( m_defaultColWidth
);
4153 m_colRights
.Add( 0 );
4157 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4159 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4161 right
+= m_colWidths
[i
];
4162 m_colRights
[i
] = right
;
4165 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4167 // if we have just inserted cols into an empty grid the current
4168 // cell will be undefined...
4170 SetCurrentCell( 0, 0 );
4172 if ( !GetBatchCount() )
4175 m_colLabelWin
->Refresh();
4181 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4183 size_t pos
= msg
.GetCommandInt();
4184 int numCols
= msg
.GetCommandInt2();
4185 m_numCols
-= numCols
;
4187 if ( !m_colWidths
.IsEmpty() )
4189 for ( i
= 0; i
< numCols
; i
++ )
4191 m_colWidths
.RemoveAt( pos
);
4192 m_colRights
.RemoveAt( pos
);
4196 for ( i
= 0; i
< m_numCols
; i
++ )
4198 w
+= m_colWidths
[i
];
4204 m_currentCellCoords
= wxGridNoCellCoords
;
4208 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4209 m_currentCellCoords
.Set( 0, 0 );
4211 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4212 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4214 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4215 // ifdef'd out following patch from Paul Gammans
4217 // No need to touch row attributes, unless we
4218 // removed _all_ columns, in this case, we remove
4219 // all row attributes.
4220 // I hate to do this here, but the
4221 // needed data is not available inside UpdateAttrCols.
4222 if ( !GetNumberCols() )
4223 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4226 if ( !GetBatchCount() )
4229 m_colLabelWin
->Refresh();
4236 if (result
&& !GetBatchCount() )
4237 m_gridWin
->Refresh();
4242 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4244 wxRegionIterator
iter( reg
);
4247 wxArrayInt rowlabels
;
4254 // TODO: remove this when we can...
4255 // There is a bug in wxMotif that gives garbage update
4256 // rectangles if you jump-scroll a long way by clicking the
4257 // scrollbar with middle button. This is a work-around
4259 #if defined(__WXMOTIF__)
4261 m_gridWin
->GetClientSize( &cw
, &ch
);
4262 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4263 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4266 // logical bounds of update region
4269 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4270 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4272 // find the row labels within these bounds
4275 for ( row
= 0; row
< m_numRows
; row
++ )
4277 if ( GetRowBottom(row
) < top
)
4280 if ( GetRowTop(row
) > bottom
)
4283 rowlabels
.Add( row
);
4293 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4295 wxRegionIterator
iter( reg
);
4298 wxArrayInt colLabels
;
4305 // TODO: remove this when we can...
4306 // There is a bug in wxMotif that gives garbage update
4307 // rectangles if you jump-scroll a long way by clicking the
4308 // scrollbar with middle button. This is a work-around
4310 #if defined(__WXMOTIF__)
4312 m_gridWin
->GetClientSize( &cw
, &ch
);
4313 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4314 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4317 // logical bounds of update region
4320 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4321 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4323 // find the cells within these bounds
4326 for ( col
= 0; col
< m_numCols
; col
++ )
4328 if ( GetColRight(col
) < left
)
4331 if ( GetColLeft(col
) > right
)
4334 colLabels
.Add( col
);
4343 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4345 wxRegionIterator
iter( reg
);
4348 wxGridCellCoordsArray cellsExposed
;
4350 int left
, top
, right
, bottom
;
4355 // TODO: remove this when we can...
4356 // There is a bug in wxMotif that gives garbage update
4357 // rectangles if you jump-scroll a long way by clicking the
4358 // scrollbar with middle button. This is a work-around
4360 #if defined(__WXMOTIF__)
4362 m_gridWin
->GetClientSize( &cw
, &ch
);
4363 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4364 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4365 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4366 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4369 // logical bounds of update region
4371 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4372 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4374 // find the cells within these bounds
4377 for ( row
= 0; row
< m_numRows
; row
++ )
4379 if ( GetRowBottom(row
) <= top
)
4382 if ( GetRowTop(row
) > bottom
)
4386 for ( col
= 0; col
< m_numCols
; col
++ )
4388 if ( GetColRight(col
) <= left
)
4391 if ( GetColLeft(col
) > right
)
4394 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4401 return cellsExposed
;
4405 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4408 wxPoint
pos( event
.GetPosition() );
4409 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4411 if ( event
.Dragging() )
4413 m_isDragging
= TRUE
;
4415 if ( event
.LeftIsDown() )
4417 switch( m_cursorMode
)
4419 case WXGRID_CURSOR_RESIZE_ROW
:
4421 int cw
, ch
, left
, dummy
;
4422 m_gridWin
->GetClientSize( &cw
, &ch
);
4423 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4425 wxClientDC
dc( m_gridWin
);
4428 GetRowTop(m_dragRowOrCol
) +
4429 GetRowMinimalHeight(m_dragRowOrCol
) );
4430 dc
.SetLogicalFunction(wxINVERT
);
4431 if ( m_dragLastPos
>= 0 )
4433 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4435 dc
.DrawLine( left
, y
, left
+cw
, y
);
4440 case WXGRID_CURSOR_SELECT_ROW
:
4441 if ( (row
= YToRow( y
)) >= 0 )
4443 m_selection
->SelectRow( row
,
4444 event
.ControlDown(),
4450 // default label to suppress warnings about "enumeration value
4451 // 'xxx' not handled in switch
4459 m_isDragging
= FALSE
;
4462 // ------------ Entering or leaving the window
4464 if ( event
.Entering() || event
.Leaving() )
4466 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4470 // ------------ Left button pressed
4472 else if ( event
.LeftDown() )
4474 // don't send a label click event for a hit on the
4475 // edge of the row label - this is probably the user
4476 // wanting to resize the row
4478 if ( YToEdgeOfRow(y
) < 0 )
4482 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4484 if ( !event
.ShiftDown() && !event
.ControlDown() )
4486 if ( event
.ShiftDown() )
4487 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4490 GetNumberCols() - 1,
4491 event
.ControlDown(),
4496 m_selection
->SelectRow( row
,
4497 event
.ControlDown(),
4501 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4506 // starting to drag-resize a row
4508 if ( CanDragRowSize() )
4509 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4514 // ------------ Left double click
4516 else if (event
.LeftDClick() )
4518 if ( YToEdgeOfRow(y
) < 0 )
4521 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4526 // ------------ Left button released
4528 else if ( event
.LeftUp() )
4530 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4532 DoEndDragResizeRow();
4534 // Note: we are ending the event *after* doing
4535 // default processing in this case
4537 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4540 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4545 // ------------ Right button down
4547 else if ( event
.RightDown() )
4550 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4552 // no default action at the moment
4557 // ------------ Right double click
4559 else if ( event
.RightDClick() )
4562 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4564 // no default action at the moment
4569 // ------------ No buttons down and mouse moving
4571 else if ( event
.Moving() )
4573 m_dragRowOrCol
= YToEdgeOfRow( y
);
4574 if ( m_dragRowOrCol
>= 0 )
4576 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4578 // don't capture the mouse yet
4579 if ( CanDragRowSize() )
4580 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4583 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4585 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4591 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4594 wxPoint
pos( event
.GetPosition() );
4595 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4597 if ( event
.Dragging() )
4599 m_isDragging
= TRUE
;
4601 if ( event
.LeftIsDown() )
4603 switch( m_cursorMode
)
4605 case WXGRID_CURSOR_RESIZE_COL
:
4607 int cw
, ch
, dummy
, top
;
4608 m_gridWin
->GetClientSize( &cw
, &ch
);
4609 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4611 wxClientDC
dc( m_gridWin
);
4614 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4615 GetColMinimalWidth(m_dragRowOrCol
));
4616 dc
.SetLogicalFunction(wxINVERT
);
4617 if ( m_dragLastPos
>= 0 )
4619 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4621 dc
.DrawLine( x
, top
, x
, top
+ch
);
4626 case WXGRID_CURSOR_SELECT_COL
:
4627 if ( (col
= XToCol( x
)) >= 0 )
4629 m_selection
->SelectCol( col
,
4630 event
.ControlDown(),
4636 // default label to suppress warnings about "enumeration value
4637 // 'xxx' not handled in switch
4645 m_isDragging
= FALSE
;
4648 // ------------ Entering or leaving the window
4650 if ( event
.Entering() || event
.Leaving() )
4652 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4656 // ------------ Left button pressed
4658 else if ( event
.LeftDown() )
4660 // don't send a label click event for a hit on the
4661 // edge of the col label - this is probably the user
4662 // wanting to resize the col
4664 if ( XToEdgeOfCol(x
) < 0 )
4668 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4670 if ( !event
.ShiftDown() && !event
.ControlDown() )
4672 if ( event
.ShiftDown() )
4673 m_selection
->SelectBlock( 0,
4674 m_currentCellCoords
.GetCol(),
4675 GetNumberRows() - 1, col
,
4676 event
.ControlDown(),
4681 m_selection
->SelectCol( col
,
4682 event
.ControlDown(),
4686 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4691 // starting to drag-resize a col
4693 if ( CanDragColSize() )
4694 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4699 // ------------ Left double click
4701 if ( event
.LeftDClick() )
4703 if ( XToEdgeOfCol(x
) < 0 )
4706 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4711 // ------------ Left button released
4713 else if ( event
.LeftUp() )
4715 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4717 DoEndDragResizeCol();
4719 // Note: we are ending the event *after* doing
4720 // default processing in this case
4722 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4725 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4730 // ------------ Right button down
4732 else if ( event
.RightDown() )
4735 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4737 // no default action at the moment
4742 // ------------ Right double click
4744 else if ( event
.RightDClick() )
4747 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4749 // no default action at the moment
4754 // ------------ No buttons down and mouse moving
4756 else if ( event
.Moving() )
4758 m_dragRowOrCol
= XToEdgeOfCol( x
);
4759 if ( m_dragRowOrCol
>= 0 )
4761 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4763 // don't capture the cursor yet
4764 if ( CanDragColSize() )
4765 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4768 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4770 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4776 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4778 if ( event
.LeftDown() )
4780 // indicate corner label by having both row and
4783 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4789 else if ( event
.LeftDClick() )
4791 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4794 else if ( event
.RightDown() )
4796 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4798 // no default action at the moment
4802 else if ( event
.RightDClick() )
4804 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4806 // no default action at the moment
4811 void wxGrid::ChangeCursorMode(CursorMode mode
,
4816 static const wxChar
*cursorModes
[] =
4825 wxLogTrace(_T("grid"),
4826 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4827 win
== m_colLabelWin
? _T("colLabelWin")
4828 : win
? _T("rowLabelWin")
4830 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4831 #endif // __WXDEBUG__
4833 if ( mode
== m_cursorMode
&&
4834 win
== m_winCapture
&&
4835 captureMouse
== (m_winCapture
!= NULL
))
4840 // by default use the grid itself
4846 m_winCapture
->ReleaseMouse();
4847 m_winCapture
= (wxWindow
*)NULL
;
4850 m_cursorMode
= mode
;
4852 switch ( m_cursorMode
)
4854 case WXGRID_CURSOR_RESIZE_ROW
:
4855 win
->SetCursor( m_rowResizeCursor
);
4858 case WXGRID_CURSOR_RESIZE_COL
:
4859 win
->SetCursor( m_colResizeCursor
);
4863 win
->SetCursor( *wxSTANDARD_CURSOR
);
4866 // we need to capture mouse when resizing
4867 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4868 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4870 if ( captureMouse
&& resize
)
4872 win
->CaptureMouse();
4877 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4880 wxPoint
pos( event
.GetPosition() );
4881 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4883 wxGridCellCoords coords
;
4884 XYToCell( x
, y
, coords
);
4886 if ( event
.Dragging() )
4888 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4890 // Don't start doing anything until the mouse has been drug at
4891 // least 3 pixels in any direction...
4894 if (m_startDragPos
== wxDefaultPosition
)
4896 m_startDragPos
= pos
;
4899 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4903 m_isDragging
= TRUE
;
4904 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4906 // Hide the edit control, so it
4907 // won't interfer with drag-shrinking.
4908 if ( IsCellEditControlShown() )
4910 HideCellEditControl();
4911 SaveEditControlValue();
4914 // Have we captured the mouse yet?
4917 m_winCapture
= m_gridWin
;
4918 m_winCapture
->CaptureMouse();
4921 if ( coords
!= wxGridNoCellCoords
)
4923 if ( event
.ControlDown() )
4925 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4926 m_selectingKeyboard
= coords
;
4927 HighlightBlock ( m_selectingKeyboard
, coords
);
4931 if ( !IsSelection() )
4933 HighlightBlock( coords
, coords
);
4937 HighlightBlock( m_currentCellCoords
, coords
);
4941 if (! IsVisible(coords
))
4943 MakeCellVisible(coords
);
4944 // TODO: need to introduce a delay or something here. The
4945 // scrolling is way to fast, at least on MSW - also on GTK.
4949 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4951 int cw
, ch
, left
, dummy
;
4952 m_gridWin
->GetClientSize( &cw
, &ch
);
4953 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4955 wxClientDC
dc( m_gridWin
);
4957 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4958 GetRowMinimalHeight(m_dragRowOrCol
) );
4959 dc
.SetLogicalFunction(wxINVERT
);
4960 if ( m_dragLastPos
>= 0 )
4962 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4964 dc
.DrawLine( left
, y
, left
+cw
, y
);
4967 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4969 int cw
, ch
, dummy
, top
;
4970 m_gridWin
->GetClientSize( &cw
, &ch
);
4971 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4973 wxClientDC
dc( m_gridWin
);
4975 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4976 GetColMinimalWidth(m_dragRowOrCol
) );
4977 dc
.SetLogicalFunction(wxINVERT
);
4978 if ( m_dragLastPos
>= 0 )
4980 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4982 dc
.DrawLine( x
, top
, x
, top
+ch
);
4989 m_isDragging
= FALSE
;
4990 m_startDragPos
= wxDefaultPosition
;
4992 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4993 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4996 if ( event
.Entering() || event
.Leaving() )
4998 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4999 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5004 // ------------ Left button pressed
5006 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5008 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5013 if ( !event
.ControlDown() )
5015 if ( event
.ShiftDown() )
5017 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5018 m_currentCellCoords
.GetCol(),
5021 event
.ControlDown(),
5026 else if ( XToEdgeOfCol(x
) < 0 &&
5027 YToEdgeOfRow(y
) < 0 )
5029 DisableCellEditControl();
5030 MakeCellVisible( coords
);
5032 // if this is the second click on this cell then start
5034 if ( m_waitForSlowClick
&&
5035 (coords
== m_currentCellCoords
) &&
5036 CanEnableCellControl())
5038 EnableCellEditControl();
5040 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5041 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5044 editor
->StartingClick();
5048 m_waitForSlowClick
= FALSE
;
5052 if ( event
.ControlDown() )
5054 m_selection
->ToggleCellSelection( coords
.GetRow(),
5056 event
.ControlDown(),
5060 m_selectingTopLeft
= wxGridNoCellCoords
;
5061 m_selectingBottomRight
= wxGridNoCellCoords
;
5062 m_selectingKeyboard
= coords
;
5066 SetCurrentCell( coords
);
5067 if ( m_selection
->GetSelectionMode()
5068 != wxGrid::wxGridSelectCells
)
5069 HighlightBlock( coords
, coords
);
5071 m_waitForSlowClick
= TRUE
;
5078 // ------------ Left double click
5080 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5082 DisableCellEditControl();
5084 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5086 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5094 // ------------ Left button released
5096 else if ( event
.LeftUp() )
5098 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5100 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5101 m_selectingBottomRight
!= wxGridNoCellCoords
)
5105 m_winCapture
->ReleaseMouse();
5106 m_winCapture
= NULL
;
5108 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5109 m_selectingTopLeft
.GetCol(),
5110 m_selectingBottomRight
.GetRow(),
5111 m_selectingBottomRight
.GetCol(),
5112 event
.ControlDown(),
5116 m_selectingTopLeft
= wxGridNoCellCoords
;
5117 m_selectingBottomRight
= wxGridNoCellCoords
;
5120 // Show the edit control, if it has been hidden for
5122 ShowCellEditControl();
5124 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5126 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5127 DoEndDragResizeRow();
5129 // Note: we are ending the event *after* doing
5130 // default processing in this case
5132 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5134 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5136 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5137 DoEndDragResizeCol();
5139 // Note: we are ending the event *after* doing
5140 // default processing in this case
5142 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5149 // ------------ Right button down
5151 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5153 DisableCellEditControl();
5154 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5159 // no default action at the moment
5164 // ------------ Right double click
5166 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5168 DisableCellEditControl();
5169 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5174 // no default action at the moment
5178 // ------------ Moving and no button action
5180 else if ( event
.Moving() && !event
.IsButton() )
5182 int dragRow
= YToEdgeOfRow( y
);
5183 int dragCol
= XToEdgeOfCol( x
);
5185 // Dragging on the corner of a cell to resize in both
5186 // directions is not implemented yet...
5188 if ( dragRow
>= 0 && dragCol
>= 0 )
5190 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5196 m_dragRowOrCol
= dragRow
;
5198 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5200 if ( CanDragRowSize() && CanDragGridSize() )
5201 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5206 m_dragRowOrCol
= dragCol
;
5214 m_dragRowOrCol
= dragCol
;
5216 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5218 if ( CanDragColSize() && CanDragGridSize() )
5219 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5225 // Neither on a row or col edge
5227 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5229 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5235 void wxGrid::DoEndDragResizeRow()
5237 if ( m_dragLastPos
>= 0 )
5239 // erase the last line and resize the row
5241 int cw
, ch
, left
, dummy
;
5242 m_gridWin
->GetClientSize( &cw
, &ch
);
5243 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5245 wxClientDC
dc( m_gridWin
);
5247 dc
.SetLogicalFunction( wxINVERT
);
5248 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5249 HideCellEditControl();
5250 SaveEditControlValue();
5252 int rowTop
= GetRowTop(m_dragRowOrCol
);
5253 SetRowSize( m_dragRowOrCol
,
5254 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5256 if ( !GetBatchCount() )
5258 // Only needed to get the correct rect.y:
5259 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5261 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5262 rect
.width
= m_rowLabelWidth
;
5263 rect
.height
= ch
- rect
.y
;
5264 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5266 m_gridWin
->Refresh( FALSE
, &rect
);
5269 ShowCellEditControl();
5274 void wxGrid::DoEndDragResizeCol()
5276 if ( m_dragLastPos
>= 0 )
5278 // erase the last line and resize the col
5280 int cw
, ch
, dummy
, top
;
5281 m_gridWin
->GetClientSize( &cw
, &ch
);
5282 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5284 wxClientDC
dc( m_gridWin
);
5286 dc
.SetLogicalFunction( wxINVERT
);
5287 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5288 HideCellEditControl();
5289 SaveEditControlValue();
5291 int colLeft
= GetColLeft(m_dragRowOrCol
);
5292 SetColSize( m_dragRowOrCol
,
5293 wxMax( m_dragLastPos
- colLeft
,
5294 GetColMinimalWidth(m_dragRowOrCol
) ) );
5296 if ( !GetBatchCount() )
5298 // Only needed to get the correct rect.x:
5299 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5301 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5302 rect
.width
= cw
- rect
.x
;
5303 rect
.height
= m_colLabelHeight
;
5304 m_colLabelWin
->Refresh( TRUE
, &rect
);
5306 m_gridWin
->Refresh( FALSE
, &rect
);
5309 ShowCellEditControl();
5316 // ------ interaction with data model
5318 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5320 switch ( msg
.GetId() )
5322 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5323 return GetModelValues();
5325 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5326 return SetModelValues();
5328 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5329 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5330 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5331 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5332 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5333 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5334 return Redimension( msg
);
5343 // The behaviour of this function depends on the grid table class
5344 // Clear() function. For the default wxGridStringTable class the
5345 // behavious is to replace all cell contents with wxEmptyString but
5346 // not to change the number of rows or cols.
5348 void wxGrid::ClearGrid()
5352 if (IsCellEditControlEnabled())
5353 DisableCellEditControl();
5356 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5361 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5363 // TODO: something with updateLabels flag
5367 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5373 if (IsCellEditControlEnabled())
5374 DisableCellEditControl();
5376 return m_table
->InsertRows( pos
, numRows
);
5378 // the table will have sent the results of the insert row
5379 // operation to this view object as a grid table message
5385 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5387 // TODO: something with updateLabels flag
5391 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5395 return ( m_table
&& m_table
->AppendRows( numRows
) );
5396 // the table will have sent the results of the append row
5397 // operation to this view object as a grid table message
5401 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5403 // TODO: something with updateLabels flag
5407 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5413 if (IsCellEditControlEnabled())
5414 DisableCellEditControl();
5416 return (m_table
->DeleteRows( pos
, numRows
));
5417 // the table will have sent the results of the delete row
5418 // operation to this view object as a grid table message
5424 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5426 // TODO: something with updateLabels flag
5430 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5436 if (IsCellEditControlEnabled())
5437 DisableCellEditControl();
5439 return m_table
->InsertCols( pos
, numCols
);
5440 // the table will have sent the results of the insert col
5441 // operation to this view object as a grid table message
5447 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5449 // TODO: something with updateLabels flag
5453 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5457 return ( m_table
&& m_table
->AppendCols( numCols
) );
5458 // the table will have sent the results of the append col
5459 // operation to this view object as a grid table message
5463 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5465 // TODO: something with updateLabels flag
5469 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5475 if (IsCellEditControlEnabled())
5476 DisableCellEditControl();
5478 return ( m_table
->DeleteCols( pos
, numCols
) );
5479 // the table will have sent the results of the delete col
5480 // operation to this view object as a grid table message
5488 // ----- event handlers
5491 // Generate a grid event based on a mouse event and
5492 // return the result of ProcessEvent()
5494 int wxGrid::SendEvent( const wxEventType type
,
5496 wxMouseEvent
& mouseEv
)
5501 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5503 int rowOrCol
= (row
== -1 ? col
: row
);
5505 wxGridSizeEvent
gridEvt( GetId(),
5509 mouseEv
.GetX() + GetRowLabelSize(),
5510 mouseEv
.GetY() + GetColLabelSize(),
5511 mouseEv
.ControlDown(),
5512 mouseEv
.ShiftDown(),
5514 mouseEv
.MetaDown() );
5516 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5517 vetoed
= !gridEvt
.IsAllowed();
5520 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5522 // Right now, it should _never_ end up here!
5523 wxGridRangeSelectEvent
gridEvt( GetId(),
5527 m_selectingBottomRight
,
5529 mouseEv
.ControlDown(),
5530 mouseEv
.ShiftDown(),
5532 mouseEv
.MetaDown() );
5534 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5535 vetoed
= !gridEvt
.IsAllowed();
5540 wxGridEvent
gridEvt( GetId(),
5544 mouseEv
.GetX() + GetRowLabelSize(),
5545 mouseEv
.GetY() + GetColLabelSize(),
5547 mouseEv
.ControlDown(),
5548 mouseEv
.ShiftDown(),
5550 mouseEv
.MetaDown() );
5551 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5552 vetoed
= !gridEvt
.IsAllowed();
5555 // A Veto'd event may not be `claimed' so test this first
5556 if (vetoed
) return -1;
5557 return claimed
? 1 : 0;
5563 // Generate a grid event of specified type and return the result
5564 // of ProcessEvent().
5566 int wxGrid::SendEvent( const wxEventType type
,
5572 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5574 int rowOrCol
= (row
== -1 ? col
: row
);
5576 wxGridSizeEvent
gridEvt( GetId(),
5581 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5582 vetoed
= !gridEvt
.IsAllowed();
5586 wxGridEvent
gridEvt( GetId(),
5591 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5592 vetoed
= !gridEvt
.IsAllowed();
5595 // A Veto'd event may not be `claimed' so test this first
5596 if (vetoed
) return -1;
5597 return claimed
? 1 : 0;
5602 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5604 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5608 // This is just here to make sure that CalcDimensions gets called when
5609 // the grid view is resized... then the size event is skipped to allow
5610 // the box sizers to handle everything
5612 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5619 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5621 if ( m_inOnKeyDown
)
5623 // shouldn't be here - we are going round in circles...
5625 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5628 m_inOnKeyDown
= TRUE
;
5630 // propagate the event up and see if it gets processed
5632 wxWindow
*parent
= GetParent();
5633 wxKeyEvent
keyEvt( event
);
5634 keyEvt
.SetEventObject( parent
);
5636 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5639 // try local handlers
5641 switch ( event
.KeyCode() )
5644 if ( event
.ControlDown() )
5646 MoveCursorUpBlock( event
.ShiftDown() );
5650 MoveCursorUp( event
.ShiftDown() );
5655 if ( event
.ControlDown() )
5657 MoveCursorDownBlock( event
.ShiftDown() );
5661 MoveCursorDown( event
.ShiftDown() );
5666 if ( event
.ControlDown() )
5668 MoveCursorLeftBlock( event
.ShiftDown() );
5672 MoveCursorLeft( event
.ShiftDown() );
5677 if ( event
.ControlDown() )
5679 MoveCursorRightBlock( event
.ShiftDown() );
5683 MoveCursorRight( event
.ShiftDown() );
5688 case WXK_NUMPAD_ENTER
:
5689 if ( event
.ControlDown() )
5691 event
.Skip(); // to let the edit control have the return
5695 if ( GetGridCursorRow() < GetNumberRows()-1 )
5697 MoveCursorDown( event
.ShiftDown() );
5701 // at the bottom of a column
5702 HideCellEditControl();
5703 SaveEditControlValue();
5713 if (event
.ShiftDown())
5715 if ( GetGridCursorCol() > 0 )
5717 MoveCursorLeft( FALSE
);
5722 HideCellEditControl();
5723 SaveEditControlValue();
5728 if ( GetGridCursorCol() < GetNumberCols()-1 )
5730 MoveCursorRight( FALSE
);
5735 HideCellEditControl();
5736 SaveEditControlValue();
5742 if ( event
.ControlDown() )
5744 MakeCellVisible( 0, 0 );
5745 SetCurrentCell( 0, 0 );
5754 if ( event
.ControlDown() )
5756 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5757 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5774 if ( event
.ControlDown() )
5776 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5777 m_currentCellCoords
.GetCol(),
5778 event
.ControlDown(),
5784 if ( !IsEditable() )
5786 MoveCursorRight( FALSE
);
5789 // Otherwise fall through to default
5792 // is it possible to edit the current cell at all?
5793 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5795 // yes, now check whether the cells editor accepts the key
5796 int row
= m_currentCellCoords
.GetRow();
5797 int col
= m_currentCellCoords
.GetCol();
5798 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5799 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5801 // <F2> is special and will always start editing, for
5802 // other keys - ask the editor itself
5803 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5804 || editor
->IsAcceptedKey(event
) )
5806 EnableCellEditControl();
5807 editor
->StartingKey(event
);
5819 // let others process char events with modifiers or all
5820 // char events for readonly cells
5827 m_inOnKeyDown
= FALSE
;
5830 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5832 // try local handlers
5834 if ( event
.KeyCode() == WXK_SHIFT
)
5836 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5837 m_selectingBottomRight
!= wxGridNoCellCoords
)
5838 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5839 m_selectingTopLeft
.GetCol(),
5840 m_selectingBottomRight
.GetRow(),
5841 m_selectingBottomRight
.GetCol(),
5842 event
.ControlDown(),
5846 m_selectingTopLeft
= wxGridNoCellCoords
;
5847 m_selectingBottomRight
= wxGridNoCellCoords
;
5848 m_selectingKeyboard
= wxGridNoCellCoords
;
5852 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5856 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5858 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5860 // the event has been intercepted - do nothing
5864 wxClientDC
dc(m_gridWin
);
5867 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5869 HideCellEditControl();
5870 DisableCellEditControl();
5872 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5875 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5876 if ( !m_gridLinesEnabled
)
5884 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5886 // Otherwise refresh redraws the highlight!
5887 m_currentCellCoords
= coords
;
5889 DrawGridCellArea(dc
,cells
);
5890 DrawAllGridLines( dc
, r
);
5894 m_currentCellCoords
= coords
;
5896 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5897 DrawCellHighlight(dc
, attr
);
5902 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5905 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5907 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5910 rightCol
= GetNumberCols() - 1;
5912 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5915 bottomRow
= GetNumberRows() - 1;
5917 if ( topRow
> bottomRow
)
5924 if ( leftCol
> rightCol
)
5931 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5932 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5934 if ( m_selectingTopLeft
!= updateTopLeft
||
5935 m_selectingBottomRight
!= updateBottomRight
)
5937 // Compute two optimal update rectangles:
5938 // Either one rectangle is a real subset of the
5939 // other, or they are (almost) disjoint!
5941 bool need_refresh
[4];
5945 need_refresh
[3] = FALSE
;
5948 // Store intermediate values
5949 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5950 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5951 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5952 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5954 // Determine the outer/inner coordinates.
5955 if (oldLeft
> leftCol
)
5961 if (oldTop
> topRow
)
5967 if (oldRight
< rightCol
)
5970 oldRight
= rightCol
;
5973 if (oldBottom
< bottomRow
)
5976 oldBottom
= bottomRow
;
5980 // Now, either the stuff marked old is the outer
5981 // rectangle or we don't have a situation where one
5982 // is contained in the other.
5984 if ( oldLeft
< leftCol
)
5986 need_refresh
[0] = TRUE
;
5987 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5989 wxGridCellCoords ( oldBottom
,
5993 if ( oldTop
< topRow
)
5995 need_refresh
[1] = TRUE
;
5996 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5998 wxGridCellCoords ( topRow
- 1,
6002 if ( oldRight
> rightCol
)
6004 need_refresh
[2] = TRUE
;
6005 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6007 wxGridCellCoords ( oldBottom
,
6011 if ( oldBottom
> bottomRow
)
6013 need_refresh
[3] = TRUE
;
6014 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6016 wxGridCellCoords ( oldBottom
,
6022 m_selectingTopLeft
= updateTopLeft
;
6023 m_selectingBottomRight
= updateBottomRight
;
6025 // various Refresh() calls
6026 for (i
= 0; i
< 4; i
++ )
6027 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6028 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6031 // never generate an event as it will be generated from
6032 // wxGridSelection::SelectBlock!
6033 // (old comment from when this was the body of SelectBlock)
6037 // ------ functions to get/send data (see also public functions)
6040 bool wxGrid::GetModelValues()
6044 // all we need to do is repaint the grid
6046 m_gridWin
->Refresh();
6054 bool wxGrid::SetModelValues()
6060 for ( row
= 0; row
< m_numRows
; row
++ )
6062 for ( col
= 0; col
< m_numCols
; col
++ )
6064 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6076 // Note - this function only draws cells that are in the list of
6077 // exposed cells (usually set from the update region by
6078 // CalcExposedCells)
6080 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6082 if ( !m_numRows
|| !m_numCols
) return;
6085 size_t numCells
= cells
.GetCount();
6087 for ( i
= 0; i
< numCells
; i
++ )
6089 DrawCell( dc
, cells
[i
] );
6094 void wxGrid::DrawGridSpace( wxDC
& dc
)
6097 m_gridWin
->GetClientSize( &cw
, &ch
);
6100 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6102 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6103 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6105 if ( right
> rightCol
|| bottom
> bottomRow
)
6108 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6110 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6111 dc
.SetPen( *wxTRANSPARENT_PEN
);
6113 if ( right
> rightCol
)
6115 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6118 if ( bottom
> bottomRow
)
6120 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6126 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6128 int row
= coords
.GetRow();
6129 int col
= coords
.GetCol();
6131 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6134 // we draw the cell border ourselves
6135 #if !WXGRID_DRAW_LINES
6136 if ( m_gridLinesEnabled
)
6137 DrawCellBorder( dc
, coords
);
6140 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6142 bool isCurrent
= coords
== m_currentCellCoords
;
6144 wxRect rect
= CellToRect( row
, col
);
6146 // if the editor is shown, we should use it and not the renderer
6147 // Note: However, only if it is really _shown_, i.e. not hidden!
6148 if ( isCurrent
&& IsCellEditControlShown() )
6150 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6151 editor
->PaintBackground(rect
, attr
);
6156 // but all the rest is drawn by the cell renderer and hence may be
6158 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6159 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6166 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6168 int row
= m_currentCellCoords
.GetRow();
6169 int col
= m_currentCellCoords
.GetCol();
6171 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6174 wxRect rect
= CellToRect(row
, col
);
6176 // hmmm... what could we do here to show that the cell is disabled?
6177 // for now, I just draw a thinner border than for the other ones, but
6178 // it doesn't look really good
6180 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6184 // The center of th drawn line is where the position/width/height of
6185 // the rectangle is actually at, (on wxMSW atr least,) so we will
6186 // reduce the size of the rectangle to compensate for the thickness of
6187 // the line. If this is too strange on non wxMSW platforms then
6188 // please #ifdef this appropriately.
6189 rect
.x
+= penWidth
/2;
6190 rect
.y
+= penWidth
/2;
6191 rect
.width
-= penWidth
-1;
6192 rect
.height
-= penWidth
-1;
6195 // Now draw the rectangle
6196 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6197 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6198 dc
.DrawRectangle(rect
);
6202 // VZ: my experiments with 3d borders...
6204 // how to properly set colours for arbitrary bg?
6205 wxCoord x1
= rect
.x
,
6207 x2
= rect
.x
+ rect
.width
-1,
6208 y2
= rect
.y
+ rect
.height
-1;
6210 dc
.SetPen(*wxWHITE_PEN
);
6211 dc
.DrawLine(x1
, y1
, x2
, y1
);
6212 dc
.DrawLine(x1
, y1
, x1
, y2
);
6214 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6215 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6217 dc
.SetPen(*wxBLACK_PEN
);
6218 dc
.DrawLine(x1
, y2
, x2
, y2
);
6219 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6224 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6226 int row
= coords
.GetRow();
6227 int col
= coords
.GetCol();
6228 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6231 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6233 // right hand border
6235 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6236 GetColRight(col
), GetRowBottom(row
) );
6240 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6241 GetColRight(col
), GetRowBottom(row
) );
6244 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6246 // This if block was previously in wxGrid::OnPaint but that doesn't
6247 // seem to get called under wxGTK - MB
6249 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6250 m_numRows
&& m_numCols
)
6252 m_currentCellCoords
.Set(0, 0);
6255 if ( IsCellEditControlShown() )
6257 // don't show highlight when the edit control is shown
6261 // if the active cell was repainted, repaint its highlight too because it
6262 // might have been damaged by the grid lines
6263 size_t count
= cells
.GetCount();
6264 for ( size_t n
= 0; n
< count
; n
++ )
6266 if ( cells
[n
] == m_currentCellCoords
)
6268 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6269 DrawCellHighlight(dc
, attr
);
6277 // TODO: remove this ???
6278 // This is used to redraw all grid lines e.g. when the grid line colour
6281 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6283 if ( !m_gridLinesEnabled
||
6285 !m_numCols
) return;
6287 int top
, bottom
, left
, right
;
6289 #if 0 //#ifndef __WXGTK__
6293 m_gridWin
->GetClientSize(&cw
, &ch
);
6295 // virtual coords of visible area
6297 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6298 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6303 reg
.GetBox(x
, y
, w
, h
);
6304 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6305 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6309 m_gridWin
->GetClientSize(&cw
, &ch
);
6310 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6311 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6314 // avoid drawing grid lines past the last row and col
6316 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6317 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6319 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6321 // horizontal grid lines
6324 for ( i
= 0; i
< m_numRows
; i
++ )
6326 int bot
= GetRowBottom(i
) - 1;
6335 dc
.DrawLine( left
, bot
, right
, bot
);
6340 // vertical grid lines
6342 for ( i
= 0; i
< m_numCols
; i
++ )
6344 int colRight
= GetColRight(i
) - 1;
6345 if ( colRight
> right
)
6350 if ( colRight
>= left
)
6352 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6358 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6360 if ( !m_numRows
) return;
6363 size_t numLabels
= rows
.GetCount();
6365 for ( i
= 0; i
< numLabels
; i
++ )
6367 DrawRowLabel( dc
, rows
[i
] );
6372 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6374 if ( GetRowHeight(row
) <= 0 )
6377 int rowTop
= GetRowTop(row
),
6378 rowBottom
= GetRowBottom(row
) - 1;
6380 dc
.SetPen( *wxBLACK_PEN
);
6381 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6382 m_rowLabelWidth
-1, rowBottom
);
6384 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6386 dc
.SetPen( *wxWHITE_PEN
);
6387 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6388 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6390 dc
.SetBackgroundMode( wxTRANSPARENT
);
6391 dc
.SetTextForeground( GetLabelTextColour() );
6392 dc
.SetFont( GetLabelFont() );
6395 GetRowLabelAlignment( &hAlign
, &vAlign
);
6399 rect
.SetY( GetRowTop(row
) + 2 );
6400 rect
.SetWidth( m_rowLabelWidth
- 4 );
6401 rect
.SetHeight( GetRowHeight(row
) - 4 );
6402 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6406 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6408 if ( !m_numCols
) return;
6411 size_t numLabels
= cols
.GetCount();
6413 for ( i
= 0; i
< numLabels
; i
++ )
6415 DrawColLabel( dc
, cols
[i
] );
6420 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6422 if ( GetColWidth(col
) <= 0 )
6425 int colLeft
= GetColLeft(col
),
6426 colRight
= GetColRight(col
) - 1;
6428 dc
.SetPen( *wxBLACK_PEN
);
6429 dc
.DrawLine( colRight
, 0,
6430 colRight
, m_colLabelHeight
-1 );
6432 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6433 colRight
, m_colLabelHeight
-1 );
6435 dc
.SetPen( *wxWHITE_PEN
);
6436 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6437 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6439 dc
.SetBackgroundMode( wxTRANSPARENT
);
6440 dc
.SetTextForeground( GetLabelTextColour() );
6441 dc
.SetFont( GetLabelFont() );
6443 dc
.SetBackgroundMode( wxTRANSPARENT
);
6444 dc
.SetTextForeground( GetLabelTextColour() );
6445 dc
.SetFont( GetLabelFont() );
6448 GetColLabelAlignment( &hAlign
, &vAlign
);
6451 rect
.SetX( colLeft
+ 2 );
6453 rect
.SetWidth( GetColWidth(col
) - 4 );
6454 rect
.SetHeight( m_colLabelHeight
- 4 );
6455 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6458 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6459 const wxString
& value
,
6464 wxArrayString lines
;
6466 dc
.SetClippingRegion( rect
);
6467 StringToLines( value
, lines
);
6470 //Forward to new API.
6471 DrawTextRectangle( dc
,
6479 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6480 const wxArrayString
& lines
,
6485 long textWidth
, textHeight
;
6486 long lineWidth
, lineHeight
;
6488 if ( lines
.GetCount() )
6490 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6491 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6494 switch ( horizAlign
)
6497 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6500 case wxALIGN_CENTRE
:
6501 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6510 switch ( vertAlign
)
6512 case wxALIGN_BOTTOM
:
6513 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6516 case wxALIGN_CENTRE
:
6517 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6526 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6528 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6533 dc
.DestroyClippingRegion();
6537 // Split multi line text up into an array of strings. Any existing
6538 // contents of the string array are preserved.
6540 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6544 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6545 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6547 while ( startPos
< (int)tVal
.Length() )
6549 pos
= tVal
.Mid(startPos
).Find( eol
);
6554 else if ( pos
== 0 )
6556 lines
.Add( wxEmptyString
);
6560 lines
.Add( value
.Mid(startPos
, pos
) );
6564 if ( startPos
< (int)value
.Length() )
6566 lines
.Add( value
.Mid( startPos
) );
6571 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6572 const wxArrayString
& lines
,
6573 long *width
, long *height
)
6580 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6582 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6583 w
= wxMax( w
, lineW
);
6592 // ------ Batch processing.
6594 void wxGrid::EndBatch()
6596 if ( m_batchCount
> 0 )
6599 if ( !m_batchCount
)
6602 m_rowLabelWin
->Refresh();
6603 m_colLabelWin
->Refresh();
6604 m_cornerLabelWin
->Refresh();
6605 m_gridWin
->Refresh();
6610 // Use this, rather than wxWindow::Refresh(), to force an immediate
6611 // repainting of the grid. Has no effect if you are already inside a
6612 // BeginBatch / EndBatch block.
6614 void wxGrid::ForceRefresh()
6622 // ------ Edit control functions
6626 void wxGrid::EnableEditing( bool edit
)
6628 // TODO: improve this ?
6630 if ( edit
!= m_editable
)
6632 if(!edit
) EnableCellEditControl(edit
);
6638 void wxGrid::EnableCellEditControl( bool enable
)
6643 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6644 SetCurrentCell( 0, 0 );
6646 if ( enable
!= m_cellEditCtrlEnabled
)
6650 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6653 // this should be checked by the caller!
6654 wxASSERT_MSG( CanEnableCellControl(),
6655 _T("can't enable editing for this cell!") );
6657 // do it before ShowCellEditControl()
6658 m_cellEditCtrlEnabled
= enable
;
6660 ShowCellEditControl();
6664 //FIXME:add veto support
6665 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
6667 HideCellEditControl();
6668 SaveEditControlValue();
6670 // do it after HideCellEditControl()
6671 m_cellEditCtrlEnabled
= enable
;
6676 bool wxGrid::IsCurrentCellReadOnly() const
6679 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6680 bool readonly
= attr
->IsReadOnly();
6686 bool wxGrid::CanEnableCellControl() const
6688 return m_editable
&& !IsCurrentCellReadOnly();
6691 bool wxGrid::IsCellEditControlEnabled() const
6693 // the cell edit control might be disable for all cells or just for the
6694 // current one if it's read only
6695 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6698 bool wxGrid::IsCellEditControlShown() const
6700 bool isShown
= FALSE
;
6702 if ( m_cellEditCtrlEnabled
)
6704 int row
= m_currentCellCoords
.GetRow();
6705 int col
= m_currentCellCoords
.GetCol();
6706 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6707 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6712 if ( editor
->IsCreated() )
6714 isShown
= editor
->GetControl()->IsShown();
6724 void wxGrid::ShowCellEditControl()
6726 if ( IsCellEditControlEnabled() )
6728 if ( !IsVisible( m_currentCellCoords
) )
6730 m_cellEditCtrlEnabled
= FALSE
;
6735 wxRect rect
= CellToRect( m_currentCellCoords
);
6736 int row
= m_currentCellCoords
.GetRow();
6737 int col
= m_currentCellCoords
.GetCol();
6739 // convert to scrolled coords
6741 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6743 // done in PaintBackground()
6745 // erase the highlight and the cell contents because the editor
6746 // might not cover the entire cell
6747 wxClientDC
dc( m_gridWin
);
6749 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6750 dc
.SetPen(*wxTRANSPARENT_PEN
);
6751 dc
.DrawRectangle(rect
);
6754 // cell is shifted by one pixel
6758 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6759 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6760 if ( !editor
->IsCreated() )
6762 editor
->Create(m_gridWin
, -1,
6763 new wxGridCellEditorEvtHandler(this, editor
));
6765 wxGridEditorCreatedEvent
evt(GetId(),
6766 wxEVT_GRID_EDITOR_CREATED
,
6770 editor
->GetControl());
6771 GetEventHandler()->ProcessEvent(evt
);
6774 editor
->Show( TRUE
, attr
);
6776 editor
->SetSize( rect
);
6778 editor
->BeginEdit(row
, col
, this);
6787 void wxGrid::HideCellEditControl()
6789 if ( IsCellEditControlEnabled() )
6791 int row
= m_currentCellCoords
.GetRow();
6792 int col
= m_currentCellCoords
.GetCol();
6794 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6795 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6796 editor
->Show( FALSE
);
6799 m_gridWin
->SetFocus();
6800 wxRect
rect( CellToRect( row
, col
) );
6801 m_gridWin
->Refresh( FALSE
, &rect
);
6806 void wxGrid::SaveEditControlValue()
6808 if ( IsCellEditControlEnabled() )
6810 int row
= m_currentCellCoords
.GetRow();
6811 int col
= m_currentCellCoords
.GetCol();
6813 wxString oldval
= GetCellValue(row
,col
);
6815 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6816 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6817 bool changed
= editor
->EndEdit(row
, col
, this);
6824 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
6825 m_currentCellCoords
.GetRow(),
6826 m_currentCellCoords
.GetCol() ) < 0 ) {
6828 //Event has been veto set the data back.
6829 SetCellValue(row
,col
,oldval
);
6837 // ------ Grid location functions
6838 // Note that all of these functions work with the logical coordinates of
6839 // grid cells and labels so you will need to convert from device
6840 // coordinates for mouse events etc.
6843 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6845 int row
= YToRow(y
);
6846 int col
= XToCol(x
);
6848 if ( row
== -1 || col
== -1 )
6850 coords
= wxGridNoCellCoords
;
6854 coords
.Set( row
, col
);
6859 int wxGrid::YToRow( int y
)
6863 for ( i
= 0; i
< m_numRows
; i
++ )
6865 if ( y
< GetRowBottom(i
) )
6873 int wxGrid::XToCol( int x
)
6877 for ( i
= 0; i
< m_numCols
; i
++ )
6879 if ( x
< GetColRight(i
) )
6887 // return the row number that that the y coord is near the edge of, or
6888 // -1 if not near an edge
6890 int wxGrid::YToEdgeOfRow( int y
)
6894 for ( i
= 0; i
< m_numRows
; i
++ )
6896 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6898 d
= abs( y
- GetRowBottom(i
) );
6899 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6908 // return the col number that that the x coord is near the edge of, or
6909 // -1 if not near an edge
6911 int wxGrid::XToEdgeOfCol( int x
)
6915 for ( i
= 0; i
< m_numCols
; i
++ )
6917 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6919 d
= abs( x
- GetColRight(i
) );
6920 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6929 wxRect
wxGrid::CellToRect( int row
, int col
)
6931 wxRect
rect( -1, -1, -1, -1 );
6933 if ( row
>= 0 && row
< m_numRows
&&
6934 col
>= 0 && col
< m_numCols
)
6936 rect
.x
= GetColLeft(col
);
6937 rect
.y
= GetRowTop(row
);
6938 rect
.width
= GetColWidth(col
);
6939 rect
.height
= GetRowHeight(row
);
6942 // if grid lines are enabled, then the area of the cell is a bit smaller
6943 if (m_gridLinesEnabled
) {
6951 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6953 // get the cell rectangle in logical coords
6955 wxRect
r( CellToRect( row
, col
) );
6957 // convert to device coords
6959 int left
, top
, right
, bottom
;
6960 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6961 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6963 // check against the client area of the grid window
6966 m_gridWin
->GetClientSize( &cw
, &ch
);
6968 if ( wholeCellVisible
)
6970 // is the cell wholly visible ?
6972 return ( left
>= 0 && right
<= cw
&&
6973 top
>= 0 && bottom
<= ch
);
6977 // is the cell partly visible ?
6979 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6980 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6985 // make the specified cell location visible by doing a minimal amount
6988 void wxGrid::MakeCellVisible( int row
, int col
)
6992 int xpos
= -1, ypos
= -1;
6994 if ( row
>= 0 && row
< m_numRows
&&
6995 col
>= 0 && col
< m_numCols
)
6997 // get the cell rectangle in logical coords
6999 wxRect
r( CellToRect( row
, col
) );
7001 // convert to device coords
7003 int left
, top
, right
, bottom
;
7004 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7005 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7008 m_gridWin
->GetClientSize( &cw
, &ch
);
7014 else if ( bottom
> ch
)
7016 int h
= r
.GetHeight();
7018 for ( i
= row
-1; i
>= 0; i
-- )
7020 int rowHeight
= GetRowHeight(i
);
7021 if ( h
+ rowHeight
> ch
)
7028 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7029 // have rounding errors (this is important, because if we do, we
7030 // might not scroll at all and some cells won't be redrawn)
7032 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7034 ypos
+= GRID_SCROLL_LINE
;
7041 else if ( right
> cw
)
7043 int w
= r
.GetWidth();
7045 for ( i
= col
-1; i
>= 0; i
-- )
7047 int colWidth
= GetColWidth(i
);
7048 if ( w
+ colWidth
> cw
)
7055 // see comment for ypos above
7056 xpos
+= GRID_SCROLL_LINE
;
7059 if ( xpos
!= -1 || ypos
!= -1 )
7061 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
7062 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
7063 Scroll( xpos
, ypos
);
7071 // ------ Grid cursor movement functions
7074 bool wxGrid::MoveCursorUp( bool expandSelection
)
7076 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7077 m_currentCellCoords
.GetRow() >= 0 )
7079 if ( expandSelection
)
7081 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7082 m_selectingKeyboard
= m_currentCellCoords
;
7083 if ( m_selectingKeyboard
.GetRow() > 0 )
7085 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7086 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7087 m_selectingKeyboard
.GetCol() );
7088 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7091 else if ( m_currentCellCoords
.GetRow() > 0 )
7094 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7095 m_currentCellCoords
.GetCol() );
7096 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7097 m_currentCellCoords
.GetCol() );
7108 bool wxGrid::MoveCursorDown( bool expandSelection
)
7110 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7111 m_currentCellCoords
.GetRow() < m_numRows
)
7113 if ( expandSelection
)
7115 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7116 m_selectingKeyboard
= m_currentCellCoords
;
7117 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7119 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7120 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7121 m_selectingKeyboard
.GetCol() );
7122 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7125 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7128 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7129 m_currentCellCoords
.GetCol() );
7130 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7131 m_currentCellCoords
.GetCol() );
7142 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7144 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7145 m_currentCellCoords
.GetCol() >= 0 )
7147 if ( expandSelection
)
7149 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7150 m_selectingKeyboard
= m_currentCellCoords
;
7151 if ( m_selectingKeyboard
.GetCol() > 0 )
7153 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7154 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7155 m_selectingKeyboard
.GetCol() );
7156 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7159 else if ( m_currentCellCoords
.GetCol() > 0 )
7162 MakeCellVisible( m_currentCellCoords
.GetRow(),
7163 m_currentCellCoords
.GetCol() - 1 );
7164 SetCurrentCell( m_currentCellCoords
.GetRow(),
7165 m_currentCellCoords
.GetCol() - 1 );
7176 bool wxGrid::MoveCursorRight( bool expandSelection
)
7178 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7179 m_currentCellCoords
.GetCol() < m_numCols
)
7181 if ( expandSelection
)
7183 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7184 m_selectingKeyboard
= m_currentCellCoords
;
7185 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7187 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7188 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7189 m_selectingKeyboard
.GetCol() );
7190 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7193 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7196 MakeCellVisible( m_currentCellCoords
.GetRow(),
7197 m_currentCellCoords
.GetCol() + 1 );
7198 SetCurrentCell( m_currentCellCoords
.GetRow(),
7199 m_currentCellCoords
.GetCol() + 1 );
7210 bool wxGrid::MovePageUp()
7212 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7214 int row
= m_currentCellCoords
.GetRow();
7218 m_gridWin
->GetClientSize( &cw
, &ch
);
7220 int y
= GetRowTop(row
);
7221 int newRow
= YToRow( y
- ch
+ 1 );
7226 else if ( newRow
== row
)
7231 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7232 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7240 bool wxGrid::MovePageDown()
7242 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7244 int row
= m_currentCellCoords
.GetRow();
7245 if ( row
< m_numRows
)
7248 m_gridWin
->GetClientSize( &cw
, &ch
);
7250 int y
= GetRowTop(row
);
7251 int newRow
= YToRow( y
+ ch
);
7254 newRow
= m_numRows
- 1;
7256 else if ( newRow
== row
)
7261 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7262 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7270 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7273 m_currentCellCoords
!= wxGridNoCellCoords
&&
7274 m_currentCellCoords
.GetRow() > 0 )
7276 int row
= m_currentCellCoords
.GetRow();
7277 int col
= m_currentCellCoords
.GetCol();
7279 if ( m_table
->IsEmptyCell(row
, col
) )
7281 // starting in an empty cell: find the next block of
7287 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7290 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7292 // starting at the top of a block: find the next block
7298 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7303 // starting within a block: find the top of the block
7308 if ( m_table
->IsEmptyCell(row
, col
) )
7316 MakeCellVisible( row
, col
);
7317 if ( expandSelection
)
7319 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7320 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7325 SetCurrentCell( row
, col
);
7333 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7336 m_currentCellCoords
!= wxGridNoCellCoords
&&
7337 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7339 int row
= m_currentCellCoords
.GetRow();
7340 int col
= m_currentCellCoords
.GetCol();
7342 if ( m_table
->IsEmptyCell(row
, col
) )
7344 // starting in an empty cell: find the next block of
7347 while ( row
< m_numRows
-1 )
7350 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7353 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7355 // starting at the bottom of a block: find the next block
7358 while ( row
< m_numRows
-1 )
7361 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7366 // starting within a block: find the bottom of the block
7368 while ( row
< m_numRows
-1 )
7371 if ( m_table
->IsEmptyCell(row
, col
) )
7379 MakeCellVisible( row
, col
);
7380 if ( expandSelection
)
7382 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7383 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7388 SetCurrentCell( row
, col
);
7397 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7400 m_currentCellCoords
!= wxGridNoCellCoords
&&
7401 m_currentCellCoords
.GetCol() > 0 )
7403 int row
= m_currentCellCoords
.GetRow();
7404 int col
= m_currentCellCoords
.GetCol();
7406 if ( m_table
->IsEmptyCell(row
, col
) )
7408 // starting in an empty cell: find the next block of
7414 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7417 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7419 // starting at the left of a block: find the next block
7425 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7430 // starting within a block: find the left of the block
7435 if ( m_table
->IsEmptyCell(row
, col
) )
7443 MakeCellVisible( row
, col
);
7444 if ( expandSelection
)
7446 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7447 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7452 SetCurrentCell( row
, col
);
7461 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7464 m_currentCellCoords
!= wxGridNoCellCoords
&&
7465 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7467 int row
= m_currentCellCoords
.GetRow();
7468 int col
= m_currentCellCoords
.GetCol();
7470 if ( m_table
->IsEmptyCell(row
, col
) )
7472 // starting in an empty cell: find the next block of
7475 while ( col
< m_numCols
-1 )
7478 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7481 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7483 // starting at the right of a block: find the next block
7486 while ( col
< m_numCols
-1 )
7489 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7494 // starting within a block: find the right of the block
7496 while ( col
< m_numCols
-1 )
7499 if ( m_table
->IsEmptyCell(row
, col
) )
7507 MakeCellVisible( row
, col
);
7508 if ( expandSelection
)
7510 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7511 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7516 SetCurrentCell( row
, col
);
7528 // ------ Label values and formatting
7531 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7533 *horiz
= m_rowLabelHorizAlign
;
7534 *vert
= m_rowLabelVertAlign
;
7537 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7539 *horiz
= m_colLabelHorizAlign
;
7540 *vert
= m_colLabelVertAlign
;
7543 wxString
wxGrid::GetRowLabelValue( int row
)
7547 return m_table
->GetRowLabelValue( row
);
7557 wxString
wxGrid::GetColLabelValue( int col
)
7561 return m_table
->GetColLabelValue( col
);
7572 void wxGrid::SetRowLabelSize( int width
)
7574 width
= wxMax( width
, 0 );
7575 if ( width
!= m_rowLabelWidth
)
7579 m_rowLabelWin
->Show( FALSE
);
7580 m_cornerLabelWin
->Show( FALSE
);
7582 else if ( m_rowLabelWidth
== 0 )
7584 m_rowLabelWin
->Show( TRUE
);
7585 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7588 m_rowLabelWidth
= width
;
7595 void wxGrid::SetColLabelSize( int height
)
7597 height
= wxMax( height
, 0 );
7598 if ( height
!= m_colLabelHeight
)
7602 m_colLabelWin
->Show( FALSE
);
7603 m_cornerLabelWin
->Show( FALSE
);
7605 else if ( m_colLabelHeight
== 0 )
7607 m_colLabelWin
->Show( TRUE
);
7608 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7611 m_colLabelHeight
= height
;
7618 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7620 if ( m_labelBackgroundColour
!= colour
)
7622 m_labelBackgroundColour
= colour
;
7623 m_rowLabelWin
->SetBackgroundColour( colour
);
7624 m_colLabelWin
->SetBackgroundColour( colour
);
7625 m_cornerLabelWin
->SetBackgroundColour( colour
);
7627 if ( !GetBatchCount() )
7629 m_rowLabelWin
->Refresh();
7630 m_colLabelWin
->Refresh();
7631 m_cornerLabelWin
->Refresh();
7636 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7638 if ( m_labelTextColour
!= colour
)
7640 m_labelTextColour
= colour
;
7641 if ( !GetBatchCount() )
7643 m_rowLabelWin
->Refresh();
7644 m_colLabelWin
->Refresh();
7649 void wxGrid::SetLabelFont( const wxFont
& font
)
7652 if ( !GetBatchCount() )
7654 m_rowLabelWin
->Refresh();
7655 m_colLabelWin
->Refresh();
7659 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7661 // allow old (incorrect) defs to be used
7664 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7665 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7666 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7671 case wxTOP
: vert
= wxALIGN_TOP
; break;
7672 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7673 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7676 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7678 m_rowLabelHorizAlign
= horiz
;
7681 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7683 m_rowLabelVertAlign
= vert
;
7686 if ( !GetBatchCount() )
7688 m_rowLabelWin
->Refresh();
7692 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7694 // allow old (incorrect) defs to be used
7697 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7698 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7699 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7704 case wxTOP
: vert
= wxALIGN_TOP
; break;
7705 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7706 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7709 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7711 m_colLabelHorizAlign
= horiz
;
7714 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7716 m_colLabelVertAlign
= vert
;
7719 if ( !GetBatchCount() )
7721 m_colLabelWin
->Refresh();
7725 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7729 m_table
->SetRowLabelValue( row
, s
);
7730 if ( !GetBatchCount() )
7732 wxRect rect
= CellToRect( row
, 0);
7733 if ( rect
.height
> 0 )
7735 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7737 rect
.width
= m_rowLabelWidth
;
7738 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7744 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7748 m_table
->SetColLabelValue( col
, s
);
7749 if ( !GetBatchCount() )
7751 wxRect rect
= CellToRect( 0, col
);
7752 if ( rect
.width
> 0 )
7754 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7756 rect
.height
= m_colLabelHeight
;
7757 m_colLabelWin
->Refresh( TRUE
, &rect
);
7763 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7765 if ( m_gridLineColour
!= colour
)
7767 m_gridLineColour
= colour
;
7769 wxClientDC
dc( m_gridWin
);
7771 DrawAllGridLines( dc
, wxRegion() );
7776 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7778 if ( m_cellHighlightColour
!= colour
)
7780 m_cellHighlightColour
= colour
;
7782 wxClientDC
dc( m_gridWin
);
7784 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7785 DrawCellHighlight(dc
, attr
);
7790 void wxGrid::SetCellHighlightPenWidth(int width
)
7792 if (m_cellHighlightPenWidth
!= width
) {
7793 m_cellHighlightPenWidth
= width
;
7795 // Just redrawing the cell highlight is not enough since that won't
7796 // make any visible change if the the thickness is getting smaller.
7797 int row
= m_currentCellCoords
.GetRow();
7798 int col
= m_currentCellCoords
.GetCol();
7799 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7801 wxRect rect
= CellToRect(row
, col
);
7802 m_gridWin
->Refresh(TRUE
, &rect
);
7806 void wxGrid::SetCellHighlightROPenWidth(int width
)
7808 if (m_cellHighlightROPenWidth
!= width
) {
7809 m_cellHighlightROPenWidth
= width
;
7811 // Just redrawing the cell highlight is not enough since that won't
7812 // make any visible change if the the thickness is getting smaller.
7813 int row
= m_currentCellCoords
.GetRow();
7814 int col
= m_currentCellCoords
.GetCol();
7815 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7817 wxRect rect
= CellToRect(row
, col
);
7818 m_gridWin
->Refresh(TRUE
, &rect
);
7822 void wxGrid::EnableGridLines( bool enable
)
7824 if ( enable
!= m_gridLinesEnabled
)
7826 m_gridLinesEnabled
= enable
;
7828 if ( !GetBatchCount() )
7832 wxClientDC
dc( m_gridWin
);
7834 DrawAllGridLines( dc
, wxRegion() );
7838 m_gridWin
->Refresh();
7845 int wxGrid::GetDefaultRowSize()
7847 return m_defaultRowHeight
;
7850 int wxGrid::GetRowSize( int row
)
7852 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7854 return GetRowHeight(row
);
7857 int wxGrid::GetDefaultColSize()
7859 return m_defaultColWidth
;
7862 int wxGrid::GetColSize( int col
)
7864 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7866 return GetColWidth(col
);
7869 // ============================================================================
7870 // access to the grid attributes: each of them has a default value in the grid
7871 // itself and may be overidden on a per-cell basis
7872 // ============================================================================
7874 // ----------------------------------------------------------------------------
7875 // setting default attributes
7876 // ----------------------------------------------------------------------------
7878 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7880 m_defaultCellAttr
->SetBackgroundColour(col
);
7882 m_gridWin
->SetBackgroundColour(col
);
7886 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7888 m_defaultCellAttr
->SetTextColour(col
);
7891 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7893 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7896 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7898 m_defaultCellAttr
->SetFont(font
);
7901 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7903 m_defaultCellAttr
->SetRenderer(renderer
);
7906 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7908 m_defaultCellAttr
->SetEditor(editor
);
7911 // ----------------------------------------------------------------------------
7912 // access to the default attrbiutes
7913 // ----------------------------------------------------------------------------
7915 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7917 return m_defaultCellAttr
->GetBackgroundColour();
7920 wxColour
wxGrid::GetDefaultCellTextColour()
7922 return m_defaultCellAttr
->GetTextColour();
7925 wxFont
wxGrid::GetDefaultCellFont()
7927 return m_defaultCellAttr
->GetFont();
7930 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7932 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7935 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7937 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7940 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7942 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7945 // ----------------------------------------------------------------------------
7946 // access to cell attributes
7947 // ----------------------------------------------------------------------------
7949 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7951 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7952 wxColour colour
= attr
->GetBackgroundColour();
7957 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7959 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7960 wxColour colour
= attr
->GetTextColour();
7965 wxFont
wxGrid::GetCellFont( int row
, int col
)
7967 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7968 wxFont font
= attr
->GetFont();
7973 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7975 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7976 attr
->GetAlignment(horiz
, vert
);
7980 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7982 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7983 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7989 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7991 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7992 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7998 bool wxGrid::IsReadOnly(int row
, int col
) const
8000 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8001 bool isReadOnly
= attr
->IsReadOnly();
8006 // ----------------------------------------------------------------------------
8007 // attribute support: cache, automatic provider creation, ...
8008 // ----------------------------------------------------------------------------
8010 bool wxGrid::CanHaveAttributes()
8017 return m_table
->CanHaveAttributes();
8020 void wxGrid::ClearAttrCache()
8022 if ( m_attrCache
.row
!= -1 )
8024 wxSafeDecRef(m_attrCache
.attr
);
8025 m_attrCache
.attr
= NULL
;
8026 m_attrCache
.row
= -1;
8030 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8032 wxGrid
*self
= (wxGrid
*)this; // const_cast
8034 self
->ClearAttrCache();
8035 self
->m_attrCache
.row
= row
;
8036 self
->m_attrCache
.col
= col
;
8037 self
->m_attrCache
.attr
= attr
;
8041 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8043 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8045 *attr
= m_attrCache
.attr
;
8046 wxSafeIncRef(m_attrCache
.attr
);
8048 #ifdef DEBUG_ATTR_CACHE
8049 gs_nAttrCacheHits
++;
8056 #ifdef DEBUG_ATTR_CACHE
8057 gs_nAttrCacheMisses
++;
8063 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8065 wxGridCellAttr
*attr
;
8066 if ( !LookupAttr(row
, col
, &attr
) )
8068 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
8069 CacheAttr(row
, col
, attr
);
8073 attr
->SetDefAttr(m_defaultCellAttr
);
8077 attr
= m_defaultCellAttr
;
8084 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8086 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8087 wxASSERT_MSG( m_table
,
8088 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8090 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8093 attr
= new wxGridCellAttr
;
8095 // artificially inc the ref count to match DecRef() in caller
8097 m_table
->SetAttr(attr
, row
, col
);
8099 attr
->SetDefAttr(m_defaultCellAttr
);
8103 // ----------------------------------------------------------------------------
8104 // setting column attributes (wrappers around SetColAttr)
8105 // ----------------------------------------------------------------------------
8107 void wxGrid::SetColFormatBool(int col
)
8109 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8112 void wxGrid::SetColFormatNumber(int col
)
8114 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8117 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8119 wxString typeName
= wxGRID_VALUE_FLOAT
;
8120 if ( (width
!= -1) || (precision
!= -1) )
8122 typeName
<< _T(':') << width
<< _T(',') << precision
;
8125 SetColFormatCustom(col
, typeName
);
8128 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8130 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8132 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8134 attr
= new wxGridCellAttr
;
8135 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8136 attr
->SetRenderer(renderer
);
8138 SetColAttr(col
, attr
);
8142 // ----------------------------------------------------------------------------
8143 // setting cell attributes: this is forwarded to the table
8144 // ----------------------------------------------------------------------------
8146 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8148 if ( CanHaveAttributes() )
8150 m_table
->SetRowAttr(attr
, row
);
8159 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8161 if ( CanHaveAttributes() )
8163 m_table
->SetColAttr(attr
, col
);
8172 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8174 if ( CanHaveAttributes() )
8176 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8177 attr
->SetBackgroundColour(colour
);
8182 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8184 if ( CanHaveAttributes() )
8186 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8187 attr
->SetTextColour(colour
);
8192 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8194 if ( CanHaveAttributes() )
8196 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8197 attr
->SetFont(font
);
8202 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8204 if ( CanHaveAttributes() )
8206 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8207 attr
->SetAlignment(horiz
, vert
);
8212 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8214 if ( CanHaveAttributes() )
8216 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8217 attr
->SetRenderer(renderer
);
8222 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8224 if ( CanHaveAttributes() )
8226 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8227 attr
->SetEditor(editor
);
8232 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8234 if ( CanHaveAttributes() )
8236 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8237 attr
->SetReadOnly(isReadOnly
);
8242 // ----------------------------------------------------------------------------
8243 // Data type registration
8244 // ----------------------------------------------------------------------------
8246 void wxGrid::RegisterDataType(const wxString
& typeName
,
8247 wxGridCellRenderer
* renderer
,
8248 wxGridCellEditor
* editor
)
8250 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8254 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8256 wxString typeName
= m_table
->GetTypeName(row
, col
);
8257 return GetDefaultEditorForType(typeName
);
8260 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8262 wxString typeName
= m_table
->GetTypeName(row
, col
);
8263 return GetDefaultRendererForType(typeName
);
8267 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8269 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8270 if ( index
== wxNOT_FOUND
)
8272 wxFAIL_MSG(wxT("Unknown data type name"));
8277 return m_typeRegistry
->GetEditor(index
);
8281 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8283 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8284 if ( index
== wxNOT_FOUND
)
8286 wxFAIL_MSG(wxT("Unknown data type name"));
8291 return m_typeRegistry
->GetRenderer(index
);
8295 // ----------------------------------------------------------------------------
8297 // ----------------------------------------------------------------------------
8299 void wxGrid::EnableDragRowSize( bool enable
)
8301 m_canDragRowSize
= enable
;
8305 void wxGrid::EnableDragColSize( bool enable
)
8307 m_canDragColSize
= enable
;
8310 void wxGrid::EnableDragGridSize( bool enable
)
8312 m_canDragGridSize
= enable
;
8316 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8318 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8320 if ( resizeExistingRows
)
8323 if ( !GetBatchCount() )
8328 void wxGrid::SetRowSize( int row
, int height
)
8330 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8332 if ( m_rowHeights
.IsEmpty() )
8334 // need to really create the array
8338 int h
= wxMax( 0, height
);
8339 int diff
= h
- m_rowHeights
[row
];
8341 m_rowHeights
[row
] = h
;
8343 for ( i
= row
; i
< m_numRows
; i
++ )
8345 m_rowBottoms
[i
] += diff
;
8347 if ( !GetBatchCount() )
8351 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8353 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8355 if ( resizeExistingCols
)
8358 if ( !GetBatchCount() )
8363 void wxGrid::SetColSize( int col
, int width
)
8365 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8367 // should we check that it's bigger than GetColMinimalWidth(col) here?
8369 if ( m_colWidths
.IsEmpty() )
8371 // need to really create the array
8375 int w
= wxMax( 0, width
);
8376 int diff
= w
- m_colWidths
[col
];
8377 m_colWidths
[col
] = w
;
8380 for ( i
= col
; i
< m_numCols
; i
++ )
8382 m_colRights
[i
] += diff
;
8384 if ( !GetBatchCount() )
8389 void wxGrid::SetColMinimalWidth( int col
, int width
)
8391 m_colMinWidths
.Put(col
, width
);
8394 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8396 m_rowMinHeights
.Put(row
, width
);
8399 int wxGrid::GetColMinimalWidth(int col
) const
8401 long value
= m_colMinWidths
.Get(col
);
8402 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8405 int wxGrid::GetRowMinimalHeight(int row
) const
8407 long value
= m_rowMinHeights
.Get(row
);
8408 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8411 // ----------------------------------------------------------------------------
8413 // ----------------------------------------------------------------------------
8415 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8417 wxClientDC
dc(m_gridWin
);
8419 // init both of them to avoid compiler warnings, even if weo nly need one
8427 wxCoord extent
, extentMax
= 0;
8428 int max
= column
? m_numRows
: m_numCols
;
8429 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8436 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8437 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8440 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8441 extent
= column
? size
.x
: size
.y
;
8442 if ( extent
> extentMax
)
8453 // now also compare with the column label extent
8455 dc
.SetFont( GetLabelFont() );
8458 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8460 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8462 extent
= column
? w
: h
;
8463 if ( extent
> extentMax
)
8470 // empty column - give default extent (notice that if extentMax is less
8471 // than default extent but != 0, it's ok)
8472 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8478 // leave some space around text
8489 SetColSize(col
, extentMax
);
8490 if ( !GetBatchCount() )
8493 m_gridWin
->GetClientSize( &cw
, &ch
);
8494 wxRect
rect ( CellToRect( 0, col
) );
8496 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8497 rect
.width
= cw
- rect
.x
;
8498 rect
.height
= m_colLabelHeight
;
8499 m_colLabelWin
->Refresh( TRUE
, &rect
);
8504 SetRowSize(row
, extentMax
);
8505 if ( !GetBatchCount() )
8508 m_gridWin
->GetClientSize( &cw
, &ch
);
8509 wxRect
rect ( CellToRect( row
, 0 ) );
8511 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8512 rect
.width
= m_rowLabelWidth
;
8513 rect
.height
= ch
- rect
.y
;
8514 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8520 SetColMinimalWidth(col
, extentMax
);
8522 SetRowMinimalHeight(row
, extentMax
);
8526 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8528 int width
= m_rowLabelWidth
;
8532 for ( int col
= 0; col
< m_numCols
; col
++ )
8536 AutoSizeColumn(col
, setAsMin
);
8539 width
+= GetColWidth(col
);
8546 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8548 int height
= m_colLabelHeight
;
8552 for ( int row
= 0; row
< m_numRows
; row
++ )
8556 AutoSizeRow(row
, setAsMin
);
8559 height
+= GetRowHeight(row
);
8566 void wxGrid::AutoSize()
8569 SetClientSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8572 wxSize
wxGrid::DoGetBestSize() const
8574 // don't set sizes, only calculate them
8575 wxGrid
*self
= (wxGrid
*)this; // const_cast
8577 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8578 self
->SetOrCalcRowSizes(TRUE
));
8587 wxPen
& wxGrid::GetDividerPen() const
8592 // ----------------------------------------------------------------------------
8593 // cell value accessor functions
8594 // ----------------------------------------------------------------------------
8596 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8600 m_table
->SetValue( row
, col
, s
);
8601 if ( !GetBatchCount() )
8603 wxClientDC
dc( m_gridWin
);
8605 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8608 if ( m_currentCellCoords
.GetRow() == row
&&
8609 m_currentCellCoords
.GetCol() == col
&&
8610 IsCellEditControlShown())
8611 // Note: If we are using IsCellEditControlEnabled,
8612 // this interacts badly with calling SetCellValue from
8613 // an EVT_GRID_CELL_CHANGE handler.
8615 HideCellEditControl();
8616 ShowCellEditControl(); // will reread data from table
8623 // ------ Block, row and col selection
8626 void wxGrid::SelectRow( int row
, bool addToSelected
)
8628 if ( IsSelection() && !addToSelected
)
8631 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8635 void wxGrid::SelectCol( int col
, bool addToSelected
)
8637 if ( IsSelection() && !addToSelected
)
8640 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8644 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8645 bool addToSelected
)
8647 if ( IsSelection() && !addToSelected
)
8650 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8651 FALSE
, addToSelected
);
8655 void wxGrid::SelectAll()
8657 if ( m_numRows
> 0 && m_numCols
> 0 )
8658 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8662 // ------ Cell, row and col deselection
8665 void wxGrid::DeselectRow( int row
)
8667 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8669 if ( m_selection
->IsInSelection(row
, 0 ) )
8670 m_selection
->ToggleCellSelection( row
, 0);
8674 int nCols
= GetNumberCols();
8675 for ( int i
= 0; i
< nCols
; i
++ )
8677 if ( m_selection
->IsInSelection(row
, i
) )
8678 m_selection
->ToggleCellSelection( row
, i
);
8683 void wxGrid::DeselectCol( int col
)
8685 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8687 if ( m_selection
->IsInSelection(0, col
) )
8688 m_selection
->ToggleCellSelection( 0, col
);
8692 int nRows
= GetNumberRows();
8693 for ( int i
= 0; i
< nRows
; i
++ )
8695 if ( m_selection
->IsInSelection(i
, col
) )
8696 m_selection
->ToggleCellSelection(i
, col
);
8701 void wxGrid::DeselectCell( int row
, int col
)
8703 if ( m_selection
->IsInSelection(row
, col
) )
8704 m_selection
->ToggleCellSelection(row
, col
);
8707 bool wxGrid::IsSelection()
8709 return ( m_selection
->IsSelection() ||
8710 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8711 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8714 bool wxGrid::IsInSelection( int row
, int col
)
8716 return ( m_selection
->IsInSelection( row
, col
) ||
8717 ( row
>= m_selectingTopLeft
.GetRow() &&
8718 col
>= m_selectingTopLeft
.GetCol() &&
8719 row
<= m_selectingBottomRight
.GetRow() &&
8720 col
<= m_selectingBottomRight
.GetCol() ) );
8723 void wxGrid::ClearSelection()
8725 m_selectingTopLeft
= wxGridNoCellCoords
;
8726 m_selectingBottomRight
= wxGridNoCellCoords
;
8727 m_selection
->ClearSelection();
8731 // This function returns the rectangle that encloses the given block
8732 // in device coords clipped to the client size of the grid window.
8734 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8735 const wxGridCellCoords
&bottomRight
)
8737 wxRect
rect( wxGridNoCellRect
);
8740 cellRect
= CellToRect( topLeft
);
8741 if ( cellRect
!= wxGridNoCellRect
)
8747 rect
= wxRect( 0, 0, 0, 0 );
8750 cellRect
= CellToRect( bottomRight
);
8751 if ( cellRect
!= wxGridNoCellRect
)
8757 return wxGridNoCellRect
;
8760 // convert to scrolled coords
8762 int left
, top
, right
, bottom
;
8763 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8764 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8767 m_gridWin
->GetClientSize( &cw
, &ch
);
8769 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8770 return wxRect( 0, 0, 0, 0);
8772 rect
.SetLeft( wxMax(0, left
) );
8773 rect
.SetTop( wxMax(0, top
) );
8774 rect
.SetRight( wxMin(cw
, right
) );
8775 rect
.SetBottom( wxMin(ch
, bottom
) );
8783 // ------ Grid event classes
8786 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8788 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8789 int row
, int col
, int x
, int y
, bool sel
,
8790 bool control
, bool shift
, bool alt
, bool meta
)
8791 : wxNotifyEvent( type
, id
)
8798 m_control
= control
;
8803 SetEventObject(obj
);
8807 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8809 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8810 int rowOrCol
, int x
, int y
,
8811 bool control
, bool shift
, bool alt
, bool meta
)
8812 : wxNotifyEvent( type
, id
)
8814 m_rowOrCol
= rowOrCol
;
8817 m_control
= control
;
8822 SetEventObject(obj
);
8826 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8828 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8829 const wxGridCellCoords
& topLeft
,
8830 const wxGridCellCoords
& bottomRight
,
8831 bool sel
, bool control
,
8832 bool shift
, bool alt
, bool meta
)
8833 : wxNotifyEvent( type
, id
)
8835 m_topLeft
= topLeft
;
8836 m_bottomRight
= bottomRight
;
8838 m_control
= control
;
8843 SetEventObject(obj
);
8847 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8849 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8850 wxObject
* obj
, int row
,
8851 int col
, wxControl
* ctrl
)
8852 : wxCommandEvent(type
, id
)
8854 SetEventObject(obj
);
8861 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
8863 #endif // wxUSE_GRID