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
= NULL
;
2023 if ( m_defGridAttr
!= this || grid
== NULL
)
2025 renderer
= m_renderer
; // use local attribute
2030 if ( !renderer
&& grid
) // get renderer for the data type
2032 // GetDefaultRendererForCell() will do IncRef() for us
2033 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2038 // if we still don't have one then use the grid default
2039 // (no need for IncRef() here neither)
2040 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
2045 wxFAIL_MSG(wxT("Missing default cell attribute"));
2051 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2053 wxGridCellEditor
* editor
= NULL
;
2055 if ( m_defGridAttr
!= this || grid
== NULL
)
2057 editor
= m_editor
; // use local attribute
2062 if ( !editor
&& grid
) // get renderer for the data type
2063 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2066 // if we still don't have one then use the grid default
2067 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
2071 wxFAIL_MSG(wxT("Missing default cell attribute"));
2077 // ----------------------------------------------------------------------------
2078 // wxGridCellAttrData
2079 // ----------------------------------------------------------------------------
2081 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2083 int n
= FindIndex(row
, col
);
2084 if ( n
== wxNOT_FOUND
)
2086 // add the attribute
2087 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2091 // free the old attribute
2092 m_attrs
[(size_t)n
].attr
->DecRef();
2096 // change the attribute
2097 m_attrs
[(size_t)n
].attr
= attr
;
2101 // remove this attribute
2102 m_attrs
.RemoveAt((size_t)n
);
2107 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2109 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2111 int n
= FindIndex(row
, col
);
2112 if ( n
!= wxNOT_FOUND
)
2114 attr
= m_attrs
[(size_t)n
].attr
;
2121 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2123 size_t count
= m_attrs
.GetCount();
2124 for ( size_t n
= 0; n
< count
; n
++ )
2126 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2127 wxCoord row
= coords
.GetRow();
2128 if ((size_t)row
>= pos
)
2132 // If rows inserted, include row counter where necessary
2133 coords
.SetRow(row
+ numRows
);
2135 else if (numRows
< 0)
2137 // If rows deleted ...
2138 if ((size_t)row
>= pos
- numRows
)
2140 // ...either decrement row counter (if row still exists)...
2141 coords
.SetRow(row
+ numRows
);
2145 // ...or remove the attribute
2146 m_attrs
.RemoveAt((size_t)n
);
2154 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2156 size_t count
= m_attrs
.GetCount();
2157 for ( size_t n
= 0; n
< count
; n
++ )
2159 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2160 wxCoord col
= coords
.GetCol();
2161 if ( (size_t)col
>= pos
)
2165 // If rows inserted, include row counter where necessary
2166 coords
.SetCol(col
+ numCols
);
2168 else if (numCols
< 0)
2170 // If rows deleted ...
2171 if ((size_t)col
>= pos
- numCols
)
2173 // ...either decrement row counter (if row still exists)...
2174 coords
.SetCol(col
+ numCols
);
2178 // ...or remove the attribute
2179 m_attrs
.RemoveAt((size_t)n
);
2187 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2189 size_t count
= m_attrs
.GetCount();
2190 for ( size_t n
= 0; n
< count
; n
++ )
2192 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2193 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2202 // ----------------------------------------------------------------------------
2203 // wxGridRowOrColAttrData
2204 // ----------------------------------------------------------------------------
2206 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2208 size_t count
= m_attrs
.Count();
2209 for ( size_t n
= 0; n
< count
; n
++ )
2211 m_attrs
[n
]->DecRef();
2215 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2217 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2219 int n
= m_rowsOrCols
.Index(rowOrCol
);
2220 if ( n
!= wxNOT_FOUND
)
2222 attr
= m_attrs
[(size_t)n
];
2229 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2231 int i
= m_rowsOrCols
.Index(rowOrCol
);
2232 if ( i
== wxNOT_FOUND
)
2234 // add the attribute
2235 m_rowsOrCols
.Add(rowOrCol
);
2240 size_t n
= (size_t)i
;
2243 // change the attribute
2244 m_attrs
[n
]->DecRef();
2249 // remove this attribute
2250 m_attrs
[n
]->DecRef();
2251 m_rowsOrCols
.RemoveAt(n
);
2252 m_attrs
.RemoveAt(n
);
2257 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2259 size_t count
= m_attrs
.GetCount();
2260 for ( size_t n
= 0; n
< count
; n
++ )
2262 int & rowOrCol
= m_rowsOrCols
[n
];
2263 if ( (size_t)rowOrCol
>= pos
)
2265 if ( numRowsOrCols
> 0 )
2267 // If rows inserted, include row counter where necessary
2268 rowOrCol
+= numRowsOrCols
;
2270 else if ( numRowsOrCols
< 0)
2272 // If rows deleted, either decrement row counter (if row still exists)
2273 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2274 rowOrCol
+= numRowsOrCols
;
2277 m_rowsOrCols
.RemoveAt((size_t)n
);
2278 m_attrs
.RemoveAt((size_t)n
);
2286 // ----------------------------------------------------------------------------
2287 // wxGridCellAttrProvider
2288 // ----------------------------------------------------------------------------
2290 wxGridCellAttrProvider::wxGridCellAttrProvider()
2292 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2295 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2300 void wxGridCellAttrProvider::InitData()
2302 m_data
= new wxGridCellAttrProviderData
;
2305 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2306 wxGridCellAttr::wxAttrKind kind
) const
2308 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2313 case (wxGridCellAttr::Any
):
2314 //Get cached merge attributes.
2315 // Currenlty not used as no cache implemented as not mutiable
2316 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2319 //Basicaly implement old version.
2320 //Also check merge cache, so we don't have to re-merge every time..
2321 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2322 *attrrow
= (wxGridCellAttr
*)NULL
,
2323 *attrcol
= (wxGridCellAttr
*)NULL
;
2325 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2326 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2327 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2329 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2330 // Two or move are non NULL
2331 attr
= new wxGridCellAttr
;
2332 attr
->SetKind(wxGridCellAttr::Merged
);
2336 attr
->MergeWith(attrcell
);
2340 attr
->MergeWith(attrcol
);
2344 attr
->MergeWith(attrrow
);
2347 //store merge attr if cache implemented
2349 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2353 // one or none is non null return it or null.
2354 if(attrrow
) attr
= attrrow
;
2355 if(attrcol
) attr
= attrcol
;
2356 if(attrcell
) attr
= attrcell
;
2360 case (wxGridCellAttr::Cell
):
2361 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2363 case (wxGridCellAttr::Col
):
2364 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2366 case (wxGridCellAttr::Row
):
2367 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2371 // (wxGridCellAttr::Default):
2372 // (wxGridCellAttr::Merged):
2379 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2385 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2388 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2393 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2396 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2401 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2404 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2408 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2410 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2414 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2418 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2420 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2424 // ----------------------------------------------------------------------------
2425 // wxGridTypeRegistry
2426 // ----------------------------------------------------------------------------
2428 wxGridTypeRegistry::~wxGridTypeRegistry()
2430 size_t count
= m_typeinfo
.Count();
2431 for ( size_t i
= 0; i
< count
; i
++ )
2432 delete m_typeinfo
[i
];
2436 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2437 wxGridCellRenderer
* renderer
,
2438 wxGridCellEditor
* editor
)
2440 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2442 // is it already registered?
2443 int loc
= FindRegisteredDataType(typeName
);
2444 if ( loc
!= wxNOT_FOUND
)
2446 delete m_typeinfo
[loc
];
2447 m_typeinfo
[loc
] = info
;
2451 m_typeinfo
.Add(info
);
2455 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2457 size_t count
= m_typeinfo
.GetCount();
2458 for ( size_t i
= 0; i
< count
; i
++ )
2460 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2469 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2471 int index
= FindRegisteredDataType(typeName
);
2472 if ( index
== wxNOT_FOUND
)
2474 // check whether this is one of the standard ones, in which case
2475 // register it "on the fly"
2477 if ( typeName
== wxGRID_VALUE_STRING
)
2479 RegisterDataType(wxGRID_VALUE_STRING
,
2480 new wxGridCellStringRenderer
,
2481 new wxGridCellTextEditor
);
2483 #endif // wxUSE_TEXTCTRL
2485 if ( typeName
== wxGRID_VALUE_BOOL
)
2487 RegisterDataType(wxGRID_VALUE_BOOL
,
2488 new wxGridCellBoolRenderer
,
2489 new wxGridCellBoolEditor
);
2491 #endif // wxUSE_CHECKBOX
2493 if ( typeName
== wxGRID_VALUE_NUMBER
)
2495 RegisterDataType(wxGRID_VALUE_NUMBER
,
2496 new wxGridCellNumberRenderer
,
2497 new wxGridCellNumberEditor
);
2499 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2501 RegisterDataType(wxGRID_VALUE_FLOAT
,
2502 new wxGridCellFloatRenderer
,
2503 new wxGridCellFloatEditor
);
2505 #endif // wxUSE_TEXTCTRL
2507 if ( typeName
== wxGRID_VALUE_CHOICE
)
2509 RegisterDataType(wxGRID_VALUE_CHOICE
,
2510 new wxGridCellStringRenderer
,
2511 new wxGridCellChoiceEditor
);
2513 #endif // wxUSE_COMBOBOX
2518 // we get here only if just added the entry for this type, so return
2520 index
= m_typeinfo
.GetCount() - 1;
2526 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2528 int index
= FindDataType(typeName
);
2529 if ( index
== wxNOT_FOUND
)
2531 // the first part of the typename is the "real" type, anything after ':'
2532 // are the parameters for the renderer
2533 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2534 if ( index
== wxNOT_FOUND
)
2539 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2540 wxGridCellRenderer
*rendererOld
= renderer
;
2541 renderer
= renderer
->Clone();
2542 rendererOld
->DecRef();
2544 wxGridCellEditor
*editor
= GetEditor(index
);
2545 wxGridCellEditor
*editorOld
= editor
;
2546 editor
= editor
->Clone();
2547 editorOld
->DecRef();
2549 // do it even if there are no parameters to reset them to defaults
2550 wxString params
= typeName
.AfterFirst(_T(':'));
2551 renderer
->SetParameters(params
);
2552 editor
->SetParameters(params
);
2554 // register the new typename
2555 RegisterDataType(typeName
, renderer
, editor
);
2557 // we just registered it, it's the last one
2558 index
= m_typeinfo
.GetCount() - 1;
2564 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2566 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2572 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2574 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2580 // ----------------------------------------------------------------------------
2582 // ----------------------------------------------------------------------------
2584 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2587 wxGridTableBase::wxGridTableBase()
2589 m_view
= (wxGrid
*) NULL
;
2590 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2593 wxGridTableBase::~wxGridTableBase()
2595 delete m_attrProvider
;
2598 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2600 delete m_attrProvider
;
2601 m_attrProvider
= attrProvider
;
2604 bool wxGridTableBase::CanHaveAttributes()
2606 if ( ! GetAttrProvider() )
2608 // use the default attr provider by default
2609 SetAttrProvider(new wxGridCellAttrProvider
);
2614 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2616 if ( m_attrProvider
)
2617 return m_attrProvider
->GetAttr(row
, col
, kind
);
2619 return (wxGridCellAttr
*)NULL
;
2622 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2624 if ( m_attrProvider
)
2626 attr
->SetKind(wxGridCellAttr::Cell
);
2627 m_attrProvider
->SetAttr(attr
, row
, col
);
2631 // as we take ownership of the pointer and don't store it, we must
2637 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2639 if ( m_attrProvider
)
2641 attr
->SetKind(wxGridCellAttr::Row
);
2642 m_attrProvider
->SetRowAttr(attr
, row
);
2646 // as we take ownership of the pointer and don't store it, we must
2652 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2654 if ( m_attrProvider
)
2656 attr
->SetKind(wxGridCellAttr::Col
);
2657 m_attrProvider
->SetColAttr(attr
, col
);
2661 // as we take ownership of the pointer and don't store it, we must
2667 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2668 size_t WXUNUSED(numRows
) )
2670 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2675 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2677 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2682 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2683 size_t WXUNUSED(numRows
) )
2685 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2690 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2691 size_t WXUNUSED(numCols
) )
2693 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2698 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2700 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2705 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2706 size_t WXUNUSED(numCols
) )
2708 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2714 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2717 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2718 // how much it makes sense to us geeks.
2722 wxString
wxGridTableBase::GetColLabelValue( int col
)
2724 // default col labels are:
2725 // cols 0 to 25 : A-Z
2726 // cols 26 to 675 : AA-ZZ
2731 for ( n
= 1; ; n
++ )
2733 s
+= (_T('A') + (wxChar
)( col%26
));
2735 if ( col
< 0 ) break;
2738 // reverse the string...
2740 for ( i
= 0; i
< n
; i
++ )
2749 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2751 return wxGRID_VALUE_STRING
;
2754 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2755 const wxString
& typeName
)
2757 return typeName
== wxGRID_VALUE_STRING
;
2760 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2762 return CanGetValueAs(row
, col
, typeName
);
2765 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2770 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2775 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2780 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2781 long WXUNUSED(value
) )
2785 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2786 double WXUNUSED(value
) )
2790 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2791 bool WXUNUSED(value
) )
2796 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2797 const wxString
& WXUNUSED(typeName
) )
2802 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2803 const wxString
& WXUNUSED(typeName
),
2804 void* WXUNUSED(value
) )
2808 //////////////////////////////////////////////////////////////////////
2810 // Message class for the grid table to send requests and notifications
2814 wxGridTableMessage::wxGridTableMessage()
2816 m_table
= (wxGridTableBase
*) NULL
;
2822 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2823 int commandInt1
, int commandInt2
)
2827 m_comInt1
= commandInt1
;
2828 m_comInt2
= commandInt2
;
2833 //////////////////////////////////////////////////////////////////////
2835 // A basic grid table for string data. An object of this class will
2836 // created by wxGrid if you don't specify an alternative table class.
2839 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2841 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2843 wxGridStringTable::wxGridStringTable()
2848 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2853 m_data
.Alloc( numRows
);
2856 sa
.Alloc( numCols
);
2857 for ( col
= 0; col
< numCols
; col
++ )
2859 sa
.Add( wxEmptyString
);
2862 for ( row
= 0; row
< numRows
; row
++ )
2868 wxGridStringTable::~wxGridStringTable()
2872 int wxGridStringTable::GetNumberRows()
2874 return m_data
.GetCount();
2877 int wxGridStringTable::GetNumberCols()
2879 if ( m_data
.GetCount() > 0 )
2880 return m_data
[0].GetCount();
2885 wxString
wxGridStringTable::GetValue( int row
, int col
)
2887 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2888 _T("invalid row or column index in wxGridStringTable") );
2890 return m_data
[row
][col
];
2893 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2895 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2896 _T("invalid row or column index in wxGridStringTable") );
2898 m_data
[row
][col
] = value
;
2901 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2903 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2904 _T("invalid row or column index in wxGridStringTable") );
2906 return (m_data
[row
][col
] == wxEmptyString
);
2909 void wxGridStringTable::Clear()
2912 int numRows
, numCols
;
2914 numRows
= m_data
.GetCount();
2917 numCols
= m_data
[0].GetCount();
2919 for ( row
= 0; row
< numRows
; row
++ )
2921 for ( col
= 0; col
< numCols
; col
++ )
2923 m_data
[row
][col
] = wxEmptyString
;
2930 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2934 size_t curNumRows
= m_data
.GetCount();
2935 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2936 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2938 if ( pos
>= curNumRows
)
2940 return AppendRows( numRows
);
2944 sa
.Alloc( curNumCols
);
2945 for ( col
= 0; col
< curNumCols
; col
++ )
2947 sa
.Add( wxEmptyString
);
2950 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2952 m_data
.Insert( sa
, row
);
2956 wxGridTableMessage
msg( this,
2957 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2961 GetView()->ProcessTableMessage( msg
);
2967 bool wxGridStringTable::AppendRows( 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 ) );
2976 if ( curNumCols
> 0 )
2978 sa
.Alloc( curNumCols
);
2979 for ( col
= 0; col
< curNumCols
; col
++ )
2981 sa
.Add( wxEmptyString
);
2985 for ( row
= 0; row
< numRows
; row
++ )
2992 wxGridTableMessage
msg( this,
2993 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2996 GetView()->ProcessTableMessage( msg
);
3002 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3006 size_t curNumRows
= m_data
.GetCount();
3008 if ( pos
>= curNumRows
)
3011 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3012 pos
, numRows
, curNumRows
);
3013 wxFAIL_MSG( errmsg
);
3017 if ( numRows
> curNumRows
- pos
)
3019 numRows
= curNumRows
- pos
;
3022 if ( numRows
>= curNumRows
)
3024 m_data
.Empty(); // don't release memory just yet
3028 for ( n
= 0; n
< numRows
; n
++ )
3030 m_data
.RemoveAt( pos
);
3035 wxGridTableMessage
msg( this,
3036 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3040 GetView()->ProcessTableMessage( msg
);
3046 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3050 size_t curNumRows
= m_data
.GetCount();
3051 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3052 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3054 if ( pos
>= curNumCols
)
3056 return AppendCols( numCols
);
3059 for ( row
= 0; row
< curNumRows
; row
++ )
3061 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3063 m_data
[row
].Insert( wxEmptyString
, col
);
3068 wxGridTableMessage
msg( this,
3069 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3073 GetView()->ProcessTableMessage( msg
);
3079 bool wxGridStringTable::AppendCols( size_t numCols
)
3083 size_t curNumRows
= m_data
.GetCount();
3087 // TODO: something better than this ?
3089 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3094 for ( row
= 0; row
< curNumRows
; row
++ )
3096 for ( n
= 0; n
< numCols
; n
++ )
3098 m_data
[row
].Add( wxEmptyString
);
3104 wxGridTableMessage
msg( this,
3105 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3108 GetView()->ProcessTableMessage( msg
);
3114 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3118 size_t curNumRows
= m_data
.GetCount();
3119 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3120 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3122 if ( pos
>= curNumCols
)
3125 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3126 pos
, numCols
, curNumCols
);
3127 wxFAIL_MSG( errmsg
);
3131 if ( numCols
> curNumCols
- pos
)
3133 numCols
= curNumCols
- pos
;
3136 for ( row
= 0; row
< curNumRows
; row
++ )
3138 if ( numCols
>= curNumCols
)
3140 m_data
[row
].Clear();
3144 for ( n
= 0; n
< numCols
; n
++ )
3146 m_data
[row
].RemoveAt( pos
);
3152 wxGridTableMessage
msg( this,
3153 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3157 GetView()->ProcessTableMessage( msg
);
3163 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3165 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3167 // using default label
3169 return wxGridTableBase::GetRowLabelValue( row
);
3173 return m_rowLabels
[ row
];
3177 wxString
wxGridStringTable::GetColLabelValue( int col
)
3179 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3181 // using default label
3183 return wxGridTableBase::GetColLabelValue( col
);
3187 return m_colLabels
[ col
];
3191 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3193 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3195 int n
= m_rowLabels
.GetCount();
3197 for ( i
= n
; i
<= row
; i
++ )
3199 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3203 m_rowLabels
[row
] = value
;
3206 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3208 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3210 int n
= m_colLabels
.GetCount();
3212 for ( i
= n
; i
<= col
; i
++ )
3214 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3218 m_colLabels
[col
] = value
;
3223 //////////////////////////////////////////////////////////////////////
3224 //////////////////////////////////////////////////////////////////////
3226 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3228 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3229 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3230 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3231 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3232 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3233 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3236 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3238 const wxPoint
&pos
, const wxSize
&size
)
3239 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3244 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3248 // NO - don't do this because it will set both the x and y origin
3249 // coords to match the parent scrolled window and we just want to
3250 // set the y coord - MB
3252 // m_owner->PrepareDC( dc );
3255 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3256 dc
.SetDeviceOrigin( 0, -y
);
3258 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3259 m_owner
->DrawRowLabels( dc
, rows
);
3263 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3265 m_owner
->ProcessRowLabelMouseEvent( event
);
3269 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3271 m_owner
->GetEventHandler()->ProcessEvent(event
);
3275 // This seems to be required for wxMotif otherwise the mouse
3276 // cursor must be in the cell edit control to get key events
3278 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3280 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3283 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3285 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3290 //////////////////////////////////////////////////////////////////////
3292 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3294 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3295 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3296 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3297 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3298 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3299 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3302 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3304 const wxPoint
&pos
, const wxSize
&size
)
3305 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3310 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3314 // NO - don't do this because it will set both the x and y origin
3315 // coords to match the parent scrolled window and we just want to
3316 // set the x coord - MB
3318 // m_owner->PrepareDC( dc );
3321 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3322 dc
.SetDeviceOrigin( -x
, 0 );
3324 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3325 m_owner
->DrawColLabels( dc
, cols
);
3329 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3331 m_owner
->ProcessColLabelMouseEvent( event
);
3334 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3336 m_owner
->GetEventHandler()->ProcessEvent(event
);
3340 // This seems to be required for wxMotif otherwise the mouse
3341 // cursor must be in the cell edit control to get key events
3343 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3345 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3348 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3350 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3355 //////////////////////////////////////////////////////////////////////
3357 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3359 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3360 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3361 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3362 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3363 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3364 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3367 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3369 const wxPoint
&pos
, const wxSize
&size
)
3370 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3375 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3379 int client_height
= 0;
3380 int client_width
= 0;
3381 GetClientSize( &client_width
, &client_height
);
3383 dc
.SetPen( *wxBLACK_PEN
);
3384 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3385 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3387 dc
.SetPen( *wxWHITE_PEN
);
3388 dc
.DrawLine( 0, 0, client_width
, 0 );
3389 dc
.DrawLine( 0, 0, 0, client_height
);
3393 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3395 m_owner
->ProcessCornerLabelMouseEvent( event
);
3399 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3401 m_owner
->GetEventHandler()->ProcessEvent(event
);
3404 // This seems to be required for wxMotif otherwise the mouse
3405 // cursor must be in the cell edit control to get key events
3407 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3409 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3412 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3414 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3419 //////////////////////////////////////////////////////////////////////
3421 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3423 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3424 EVT_PAINT( wxGridWindow::OnPaint
)
3425 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3426 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3427 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3428 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3429 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3432 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3433 wxGridRowLabelWindow
*rowLblWin
,
3434 wxGridColLabelWindow
*colLblWin
,
3435 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3436 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3439 m_rowLabelWin
= rowLblWin
;
3440 m_colLabelWin
= colLblWin
;
3441 SetBackgroundColour(_T("WHITE"));
3445 wxGridWindow::~wxGridWindow()
3450 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3452 wxPaintDC
dc( this );
3453 m_owner
->PrepareDC( dc
);
3454 wxRegion reg
= GetUpdateRegion();
3455 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3456 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3457 #if WXGRID_DRAW_LINES
3458 m_owner
->DrawAllGridLines( dc
, reg
);
3460 m_owner
->DrawGridSpace( dc
);
3461 m_owner
->DrawHighlight( dc
, DirtyCells
);
3465 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3467 wxPanel::ScrollWindow( dx
, dy
, rect
);
3468 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3469 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3473 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3475 m_owner
->ProcessGridCellMouseEvent( event
);
3478 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3480 m_owner
->GetEventHandler()->ProcessEvent(event
);
3483 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3484 // cursor must be in the cell edit control to get key events
3486 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3488 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3491 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3493 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3496 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3501 //////////////////////////////////////////////////////////////////////
3504 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3506 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3507 EVT_PAINT( wxGrid::OnPaint
)
3508 EVT_SIZE( wxGrid::OnSize
)
3509 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3510 EVT_KEY_UP( wxGrid::OnKeyUp
)
3511 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3514 wxGrid::wxGrid( wxWindow
*parent
,
3519 const wxString
& name
)
3520 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3521 m_colMinWidths(GRID_HASH_SIZE
),
3522 m_rowMinHeights(GRID_HASH_SIZE
)
3530 // Must do this or ~wxScrollHelper will pop the wrong event handler
3531 SetTargetWindow(this);
3533 wxSafeDecRef(m_defaultCellAttr
);
3535 #ifdef DEBUG_ATTR_CACHE
3536 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3537 wxPrintf(_T("wxGrid attribute cache statistics: "
3538 "total: %u, hits: %u (%u%%)\n"),
3539 total
, gs_nAttrCacheHits
,
3540 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3546 delete m_typeRegistry
;
3552 // ----- internal init and update functions
3555 void wxGrid::Create()
3557 m_created
= FALSE
; // set to TRUE by CreateGrid
3559 m_table
= (wxGridTableBase
*) NULL
;
3562 m_cellEditCtrlEnabled
= FALSE
;
3564 m_defaultCellAttr
= new wxGridCellAttr
;
3565 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3567 // Set default cell attributes
3568 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3569 m_defaultCellAttr
->SetFont(GetFont());
3570 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3571 m_defaultCellAttr
->SetTextColour(
3572 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3573 m_defaultCellAttr
->SetBackgroundColour(
3574 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3575 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3576 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3581 m_currentCellCoords
= wxGridNoCellCoords
;
3583 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3584 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3586 // create the type registry
3587 m_typeRegistry
= new wxGridTypeRegistry
;
3589 // subwindow components that make up the wxGrid
3590 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3595 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3600 m_colLabelWin
= new wxGridColLabelWindow( this,
3605 m_gridWin
= new wxGridWindow( this,
3612 SetTargetWindow( m_gridWin
);
3618 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3619 wxGrid::wxGridSelectionModes selmode
)
3621 wxCHECK_MSG( !m_created
,
3623 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3625 m_numRows
= numRows
;
3626 m_numCols
= numCols
;
3628 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3629 m_table
->SetView( this );
3631 m_selection
= new wxGridSelection( this, selmode
);
3640 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3642 wxCHECK_RET( m_created
,
3643 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3645 m_selection
->SetSelectionMode( selmode
);
3648 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3649 wxGrid::wxGridSelectionModes selmode
)
3653 // RD: Actually, this should probably be allowed. I think it would be
3654 // nice to be able to switch multiple Tables in and out of a single
3655 // View at runtime. Is there anything in the implmentation that would
3658 // At least, you now have to cope with m_selection
3659 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3664 m_numRows
= table
->GetNumberRows();
3665 m_numCols
= table
->GetNumberCols();
3668 m_table
->SetView( this );
3671 m_selection
= new wxGridSelection( this, selmode
);
3684 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3685 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3687 if ( m_rowLabelWin
)
3689 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3693 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3696 m_labelTextColour
= wxColour( _T("BLACK") );
3699 m_attrCache
.row
= -1;
3701 // TODO: something better than this ?
3703 m_labelFont
= this->GetFont();
3704 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3706 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3707 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3709 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3710 m_colLabelVertAlign
= wxALIGN_TOP
;
3712 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3713 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3715 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3716 m_defaultRowHeight
+= 8;
3718 m_defaultRowHeight
+= 4;
3721 m_gridLineColour
= wxColour( 128, 128, 255 );
3722 m_gridLinesEnabled
= TRUE
;
3723 m_cellHighlightColour
= m_gridLineColour
;
3724 m_cellHighlightPenWidth
= 2;
3725 m_cellHighlightROPenWidth
= 1;
3727 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3728 m_winCapture
= (wxWindow
*)NULL
;
3729 m_canDragRowSize
= TRUE
;
3730 m_canDragColSize
= TRUE
;
3731 m_canDragGridSize
= TRUE
;
3733 m_dragRowOrCol
= -1;
3734 m_isDragging
= FALSE
;
3735 m_startDragPos
= wxDefaultPosition
;
3737 m_waitForSlowClick
= FALSE
;
3739 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3740 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3742 m_currentCellCoords
= wxGridNoCellCoords
;
3744 m_selectingTopLeft
= wxGridNoCellCoords
;
3745 m_selectingBottomRight
= wxGridNoCellCoords
;
3746 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3747 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3749 m_editable
= TRUE
; // default for whole grid
3751 m_inOnKeyDown
= FALSE
;
3758 // ----------------------------------------------------------------------------
3759 // the idea is to call these functions only when necessary because they create
3760 // quite big arrays which eat memory mostly unnecessary - in particular, if
3761 // default widths/heights are used for all rows/columns, we may not use these
3764 // with some extra code, it should be possible to only store the
3765 // widths/heights different from default ones but this will be done later...
3766 // ----------------------------------------------------------------------------
3768 void wxGrid::InitRowHeights()
3770 m_rowHeights
.Empty();
3771 m_rowBottoms
.Empty();
3773 m_rowHeights
.Alloc( m_numRows
);
3774 m_rowBottoms
.Alloc( m_numRows
);
3777 for ( int i
= 0; i
< m_numRows
; i
++ )
3779 m_rowHeights
.Add( m_defaultRowHeight
);
3780 rowBottom
+= m_defaultRowHeight
;
3781 m_rowBottoms
.Add( rowBottom
);
3785 void wxGrid::InitColWidths()
3787 m_colWidths
.Empty();
3788 m_colRights
.Empty();
3790 m_colWidths
.Alloc( m_numCols
);
3791 m_colRights
.Alloc( m_numCols
);
3793 for ( int i
= 0; i
< m_numCols
; i
++ )
3795 m_colWidths
.Add( m_defaultColWidth
);
3796 colRight
+= m_defaultColWidth
;
3797 m_colRights
.Add( colRight
);
3801 int wxGrid::GetColWidth(int col
) const
3803 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3806 int wxGrid::GetColLeft(int col
) const
3808 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3809 : m_colRights
[col
] - m_colWidths
[col
];
3812 int wxGrid::GetColRight(int col
) const
3814 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3818 int wxGrid::GetRowHeight(int row
) const
3820 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3823 int wxGrid::GetRowTop(int row
) const
3825 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3826 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3829 int wxGrid::GetRowBottom(int row
) const
3831 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3832 : m_rowBottoms
[row
];
3835 void wxGrid::CalcDimensions()
3838 GetClientSize( &cw
, &ch
);
3840 if ( m_colLabelWin
->IsShown() )
3841 cw
-= m_rowLabelWidth
;
3842 if ( m_rowLabelWin
->IsShown() )
3843 ch
-= m_colLabelHeight
;
3846 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3847 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3849 // preserve (more or less) the previous position
3851 GetViewStart( &x
, &y
);
3852 // maybe we don't need scrollbars at all? and if we do, transform w and h
3853 // from pixels into logical units
3860 w
= (w
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3870 h
= (h
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3875 // do set scrollbar parameters
3876 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3877 w
, h
, x
, y
, (GetBatchCount() != 0));
3881 void wxGrid::CalcWindowSizes()
3884 GetClientSize( &cw
, &ch
);
3886 if ( m_cornerLabelWin
->IsShown() )
3887 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3889 if ( m_colLabelWin
->IsShown() )
3890 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3892 if ( m_rowLabelWin
->IsShown() )
3893 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3895 if ( m_gridWin
->IsShown() )
3896 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3900 // this is called when the grid table sends a message to say that it
3901 // has been redimensioned
3903 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3906 bool result
= FALSE
;
3909 // if we were using the default widths/heights so far, we must change them
3911 if ( m_colWidths
.IsEmpty() )
3916 if ( m_rowHeights
.IsEmpty() )
3922 switch ( msg
.GetId() )
3924 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3926 size_t pos
= msg
.GetCommandInt();
3927 int numRows
= msg
.GetCommandInt2();
3929 m_numRows
+= numRows
;
3931 if ( !m_rowHeights
.IsEmpty() )
3933 for ( i
= 0; i
< numRows
; i
++ )
3935 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3936 m_rowBottoms
.Insert( 0, pos
);
3940 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3942 for ( i
= pos
; i
< m_numRows
; i
++ )
3944 bottom
+= m_rowHeights
[i
];
3945 m_rowBottoms
[i
] = bottom
;
3948 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3950 // if we have just inserted cols into an empty grid the current
3951 // cell will be undefined...
3953 SetCurrentCell( 0, 0 );
3955 m_selection
->UpdateRows( pos
, numRows
);
3956 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3958 attrProvider
->UpdateAttrRows( pos
, numRows
);
3960 if ( !GetBatchCount() )
3963 m_rowLabelWin
->Refresh();
3969 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3971 int numRows
= msg
.GetCommandInt();
3972 int oldNumRows
= m_numRows
;
3973 m_numRows
+= numRows
;
3975 if ( !m_rowHeights
.IsEmpty() )
3977 for ( i
= 0; i
< numRows
; i
++ )
3979 m_rowHeights
.Add( m_defaultRowHeight
);
3980 m_rowBottoms
.Add( 0 );
3984 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3986 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3988 bottom
+= m_rowHeights
[i
];
3989 m_rowBottoms
[i
] = bottom
;
3992 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3994 // if we have just inserted cols into an empty grid the current
3995 // cell will be undefined...
3997 SetCurrentCell( 0, 0 );
3999 if ( !GetBatchCount() )
4002 m_rowLabelWin
->Refresh();
4008 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4010 size_t pos
= msg
.GetCommandInt();
4011 int numRows
= msg
.GetCommandInt2();
4012 m_numRows
-= numRows
;
4014 if ( !m_rowHeights
.IsEmpty() )
4016 for ( i
= 0; i
< numRows
; i
++ )
4018 m_rowHeights
.RemoveAt( pos
);
4019 m_rowBottoms
.RemoveAt( pos
);
4023 for ( i
= 0; i
< m_numRows
; i
++ )
4025 h
+= m_rowHeights
[i
];
4026 m_rowBottoms
[i
] = h
;
4031 m_currentCellCoords
= wxGridNoCellCoords
;
4035 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4036 m_currentCellCoords
.Set( 0, 0 );
4038 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4039 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4041 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4042 // ifdef'd out following patch from Paul Gammans
4044 // No need to touch column attributes, unless we
4045 // removed _all_ rows, in this case, we remove
4046 // all column attributes.
4047 // I hate to do this here, but the
4048 // needed data is not available inside UpdateAttrRows.
4049 if ( !GetNumberRows() )
4050 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4053 if ( !GetBatchCount() )
4056 m_rowLabelWin
->Refresh();
4062 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4064 size_t pos
= msg
.GetCommandInt();
4065 int numCols
= msg
.GetCommandInt2();
4066 m_numCols
+= numCols
;
4068 if ( !m_colWidths
.IsEmpty() )
4070 for ( i
= 0; i
< numCols
; i
++ )
4072 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4073 m_colRights
.Insert( 0, pos
);
4077 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4079 for ( i
= pos
; i
< m_numCols
; i
++ )
4081 right
+= m_colWidths
[i
];
4082 m_colRights
[i
] = right
;
4085 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4087 // if we have just inserted cols into an empty grid the current
4088 // cell will be undefined...
4090 SetCurrentCell( 0, 0 );
4092 m_selection
->UpdateCols( pos
, numCols
);
4093 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4095 attrProvider
->UpdateAttrCols( pos
, numCols
);
4096 if ( !GetBatchCount() )
4099 m_colLabelWin
->Refresh();
4106 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4108 int numCols
= msg
.GetCommandInt();
4109 int oldNumCols
= m_numCols
;
4110 m_numCols
+= numCols
;
4111 if ( !m_colWidths
.IsEmpty() )
4113 for ( i
= 0; i
< numCols
; i
++ )
4115 m_colWidths
.Add( m_defaultColWidth
);
4116 m_colRights
.Add( 0 );
4120 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4122 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4124 right
+= m_colWidths
[i
];
4125 m_colRights
[i
] = right
;
4128 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4130 // if we have just inserted cols into an empty grid the current
4131 // cell will be undefined...
4133 SetCurrentCell( 0, 0 );
4135 if ( !GetBatchCount() )
4138 m_colLabelWin
->Refresh();
4144 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4146 size_t pos
= msg
.GetCommandInt();
4147 int numCols
= msg
.GetCommandInt2();
4148 m_numCols
-= numCols
;
4150 if ( !m_colWidths
.IsEmpty() )
4152 for ( i
= 0; i
< numCols
; i
++ )
4154 m_colWidths
.RemoveAt( pos
);
4155 m_colRights
.RemoveAt( pos
);
4159 for ( i
= 0; i
< m_numCols
; i
++ )
4161 w
+= m_colWidths
[i
];
4167 m_currentCellCoords
= wxGridNoCellCoords
;
4171 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4172 m_currentCellCoords
.Set( 0, 0 );
4174 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4175 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4177 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4178 // ifdef'd out following patch from Paul Gammans
4180 // No need to touch row attributes, unless we
4181 // removed _all_ columns, in this case, we remove
4182 // all row attributes.
4183 // I hate to do this here, but the
4184 // needed data is not available inside UpdateAttrCols.
4185 if ( !GetNumberCols() )
4186 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4189 if ( !GetBatchCount() )
4192 m_colLabelWin
->Refresh();
4199 if (result
&& !GetBatchCount() )
4200 m_gridWin
->Refresh();
4205 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4207 wxRegionIterator
iter( reg
);
4210 wxArrayInt rowlabels
;
4217 // TODO: remove this when we can...
4218 // There is a bug in wxMotif that gives garbage update
4219 // rectangles if you jump-scroll a long way by clicking the
4220 // scrollbar with middle button. This is a work-around
4222 #if defined(__WXMOTIF__)
4224 m_gridWin
->GetClientSize( &cw
, &ch
);
4225 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4226 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4229 // logical bounds of update region
4232 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4233 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4235 // find the row labels within these bounds
4238 for ( row
= 0; row
< m_numRows
; row
++ )
4240 if ( GetRowBottom(row
) < top
)
4243 if ( GetRowTop(row
) > bottom
)
4246 rowlabels
.Add( row
);
4256 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4258 wxRegionIterator
iter( reg
);
4261 wxArrayInt colLabels
;
4268 // TODO: remove this when we can...
4269 // There is a bug in wxMotif that gives garbage update
4270 // rectangles if you jump-scroll a long way by clicking the
4271 // scrollbar with middle button. This is a work-around
4273 #if defined(__WXMOTIF__)
4275 m_gridWin
->GetClientSize( &cw
, &ch
);
4276 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4277 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4280 // logical bounds of update region
4283 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4284 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4286 // find the cells within these bounds
4289 for ( col
= 0; col
< m_numCols
; col
++ )
4291 if ( GetColRight(col
) < left
)
4294 if ( GetColLeft(col
) > right
)
4297 colLabels
.Add( col
);
4306 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4308 wxRegionIterator
iter( reg
);
4311 wxGridCellCoordsArray cellsExposed
;
4313 int left
, top
, right
, bottom
;
4318 // TODO: remove this when we can...
4319 // There is a bug in wxMotif that gives garbage update
4320 // rectangles if you jump-scroll a long way by clicking the
4321 // scrollbar with middle button. This is a work-around
4323 #if defined(__WXMOTIF__)
4325 m_gridWin
->GetClientSize( &cw
, &ch
);
4326 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4327 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4328 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4329 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4332 // logical bounds of update region
4334 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4335 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4337 // find the cells within these bounds
4340 for ( row
= 0; row
< m_numRows
; row
++ )
4342 if ( GetRowBottom(row
) <= top
)
4345 if ( GetRowTop(row
) > bottom
)
4349 for ( col
= 0; col
< m_numCols
; col
++ )
4351 if ( GetColRight(col
) <= left
)
4354 if ( GetColLeft(col
) > right
)
4357 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4364 return cellsExposed
;
4368 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4371 wxPoint
pos( event
.GetPosition() );
4372 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4374 if ( event
.Dragging() )
4376 m_isDragging
= TRUE
;
4378 if ( event
.LeftIsDown() )
4380 switch( m_cursorMode
)
4382 case WXGRID_CURSOR_RESIZE_ROW
:
4384 int cw
, ch
, left
, dummy
;
4385 m_gridWin
->GetClientSize( &cw
, &ch
);
4386 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4388 wxClientDC
dc( m_gridWin
);
4391 GetRowTop(m_dragRowOrCol
) +
4392 GetRowMinimalHeight(m_dragRowOrCol
) );
4393 dc
.SetLogicalFunction(wxINVERT
);
4394 if ( m_dragLastPos
>= 0 )
4396 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4398 dc
.DrawLine( left
, y
, left
+cw
, y
);
4403 case WXGRID_CURSOR_SELECT_ROW
:
4404 if ( (row
= YToRow( y
)) >= 0 )
4406 m_selection
->SelectRow( row
,
4407 event
.ControlDown(),
4413 // default label to suppress warnings about "enumeration value
4414 // 'xxx' not handled in switch
4422 m_isDragging
= FALSE
;
4425 // ------------ Entering or leaving the window
4427 if ( event
.Entering() || event
.Leaving() )
4429 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4433 // ------------ Left button pressed
4435 else if ( event
.LeftDown() )
4437 // don't send a label click event for a hit on the
4438 // edge of the row label - this is probably the user
4439 // wanting to resize the row
4441 if ( YToEdgeOfRow(y
) < 0 )
4445 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4447 if ( !event
.ShiftDown() && !event
.ControlDown() )
4449 if ( event
.ShiftDown() )
4450 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4453 GetNumberCols() - 1,
4454 event
.ControlDown(),
4459 m_selection
->SelectRow( row
,
4460 event
.ControlDown(),
4464 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4469 // starting to drag-resize a row
4471 if ( CanDragRowSize() )
4472 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4477 // ------------ Left double click
4479 else if (event
.LeftDClick() )
4481 if ( YToEdgeOfRow(y
) < 0 )
4484 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4489 // ------------ Left button released
4491 else if ( event
.LeftUp() )
4493 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4495 DoEndDragResizeRow();
4497 // Note: we are ending the event *after* doing
4498 // default processing in this case
4500 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4503 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4508 // ------------ Right button down
4510 else if ( event
.RightDown() )
4513 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4515 // no default action at the moment
4520 // ------------ Right double click
4522 else if ( event
.RightDClick() )
4525 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4527 // no default action at the moment
4532 // ------------ No buttons down and mouse moving
4534 else if ( event
.Moving() )
4536 m_dragRowOrCol
= YToEdgeOfRow( y
);
4537 if ( m_dragRowOrCol
>= 0 )
4539 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4541 // don't capture the mouse yet
4542 if ( CanDragRowSize() )
4543 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4546 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4548 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4554 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4557 wxPoint
pos( event
.GetPosition() );
4558 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4560 if ( event
.Dragging() )
4562 m_isDragging
= TRUE
;
4564 if ( event
.LeftIsDown() )
4566 switch( m_cursorMode
)
4568 case WXGRID_CURSOR_RESIZE_COL
:
4570 int cw
, ch
, dummy
, top
;
4571 m_gridWin
->GetClientSize( &cw
, &ch
);
4572 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4574 wxClientDC
dc( m_gridWin
);
4577 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4578 GetColMinimalWidth(m_dragRowOrCol
));
4579 dc
.SetLogicalFunction(wxINVERT
);
4580 if ( m_dragLastPos
>= 0 )
4582 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4584 dc
.DrawLine( x
, top
, x
, top
+ch
);
4589 case WXGRID_CURSOR_SELECT_COL
:
4590 if ( (col
= XToCol( x
)) >= 0 )
4592 m_selection
->SelectCol( col
,
4593 event
.ControlDown(),
4599 // default label to suppress warnings about "enumeration value
4600 // 'xxx' not handled in switch
4608 m_isDragging
= FALSE
;
4611 // ------------ Entering or leaving the window
4613 if ( event
.Entering() || event
.Leaving() )
4615 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4619 // ------------ Left button pressed
4621 else if ( event
.LeftDown() )
4623 // don't send a label click event for a hit on the
4624 // edge of the col label - this is probably the user
4625 // wanting to resize the col
4627 if ( XToEdgeOfCol(x
) < 0 )
4631 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4633 if ( !event
.ShiftDown() && !event
.ControlDown() )
4635 if ( event
.ShiftDown() )
4636 m_selection
->SelectBlock( 0,
4637 m_currentCellCoords
.GetCol(),
4638 GetNumberRows() - 1, col
,
4639 event
.ControlDown(),
4644 m_selection
->SelectCol( col
,
4645 event
.ControlDown(),
4649 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4654 // starting to drag-resize a col
4656 if ( CanDragColSize() )
4657 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4662 // ------------ Left double click
4664 if ( event
.LeftDClick() )
4666 if ( XToEdgeOfCol(x
) < 0 )
4669 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4674 // ------------ Left button released
4676 else if ( event
.LeftUp() )
4678 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4680 DoEndDragResizeCol();
4682 // Note: we are ending the event *after* doing
4683 // default processing in this case
4685 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4688 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4693 // ------------ Right button down
4695 else if ( event
.RightDown() )
4698 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4700 // no default action at the moment
4705 // ------------ Right double click
4707 else if ( event
.RightDClick() )
4710 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4712 // no default action at the moment
4717 // ------------ No buttons down and mouse moving
4719 else if ( event
.Moving() )
4721 m_dragRowOrCol
= XToEdgeOfCol( x
);
4722 if ( m_dragRowOrCol
>= 0 )
4724 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4726 // don't capture the cursor yet
4727 if ( CanDragColSize() )
4728 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4731 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4733 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4739 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4741 if ( event
.LeftDown() )
4743 // indicate corner label by having both row and
4746 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4752 else if ( event
.LeftDClick() )
4754 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4757 else if ( event
.RightDown() )
4759 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4761 // no default action at the moment
4765 else if ( event
.RightDClick() )
4767 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4769 // no default action at the moment
4774 void wxGrid::ChangeCursorMode(CursorMode mode
,
4779 static const wxChar
*cursorModes
[] =
4788 wxLogTrace(_T("grid"),
4789 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4790 win
== m_colLabelWin
? _T("colLabelWin")
4791 : win
? _T("rowLabelWin")
4793 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4794 #endif // __WXDEBUG__
4796 if ( mode
== m_cursorMode
&&
4797 win
== m_winCapture
&&
4798 captureMouse
== (m_winCapture
!= NULL
))
4803 // by default use the grid itself
4809 m_winCapture
->ReleaseMouse();
4810 m_winCapture
= (wxWindow
*)NULL
;
4813 m_cursorMode
= mode
;
4815 switch ( m_cursorMode
)
4817 case WXGRID_CURSOR_RESIZE_ROW
:
4818 win
->SetCursor( m_rowResizeCursor
);
4821 case WXGRID_CURSOR_RESIZE_COL
:
4822 win
->SetCursor( m_colResizeCursor
);
4826 win
->SetCursor( *wxSTANDARD_CURSOR
);
4829 // we need to capture mouse when resizing
4830 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4831 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4833 if ( captureMouse
&& resize
)
4835 win
->CaptureMouse();
4840 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4843 wxPoint
pos( event
.GetPosition() );
4844 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4846 wxGridCellCoords coords
;
4847 XYToCell( x
, y
, coords
);
4849 if ( event
.Dragging() )
4851 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4853 // Don't start doing anything until the mouse has been drug at
4854 // least 3 pixels in any direction...
4857 if (m_startDragPos
== wxDefaultPosition
)
4859 m_startDragPos
= pos
;
4862 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4866 m_isDragging
= TRUE
;
4867 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4869 // Hide the edit control, so it
4870 // won't interfer with drag-shrinking.
4871 if ( IsCellEditControlShown() )
4873 HideCellEditControl();
4874 SaveEditControlValue();
4877 // Have we captured the mouse yet?
4880 m_winCapture
= m_gridWin
;
4881 m_winCapture
->CaptureMouse();
4884 if ( coords
!= wxGridNoCellCoords
)
4886 if ( event
.ControlDown() )
4888 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4889 m_selectingKeyboard
= coords
;
4890 HighlightBlock ( m_selectingKeyboard
, coords
);
4894 if ( !IsSelection() )
4896 HighlightBlock( coords
, coords
);
4900 HighlightBlock( m_currentCellCoords
, coords
);
4904 if (! IsVisible(coords
))
4906 MakeCellVisible(coords
);
4907 // TODO: need to introduce a delay or something here. The
4908 // scrolling is way to fast, at least on MSW - also on GTK.
4912 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4914 int cw
, ch
, left
, dummy
;
4915 m_gridWin
->GetClientSize( &cw
, &ch
);
4916 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4918 wxClientDC
dc( m_gridWin
);
4920 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4921 GetRowMinimalHeight(m_dragRowOrCol
) );
4922 dc
.SetLogicalFunction(wxINVERT
);
4923 if ( m_dragLastPos
>= 0 )
4925 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4927 dc
.DrawLine( left
, y
, left
+cw
, y
);
4930 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4932 int cw
, ch
, dummy
, top
;
4933 m_gridWin
->GetClientSize( &cw
, &ch
);
4934 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4936 wxClientDC
dc( m_gridWin
);
4938 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4939 GetColMinimalWidth(m_dragRowOrCol
) );
4940 dc
.SetLogicalFunction(wxINVERT
);
4941 if ( m_dragLastPos
>= 0 )
4943 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4945 dc
.DrawLine( x
, top
, x
, top
+ch
);
4952 m_isDragging
= FALSE
;
4953 m_startDragPos
= wxDefaultPosition
;
4955 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4956 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4959 if ( event
.Entering() || event
.Leaving() )
4961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4962 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4967 // ------------ Left button pressed
4969 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4971 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4976 if ( !event
.ControlDown() )
4978 if ( event
.ShiftDown() )
4980 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4981 m_currentCellCoords
.GetCol(),
4984 event
.ControlDown(),
4989 else if ( XToEdgeOfCol(x
) < 0 &&
4990 YToEdgeOfRow(y
) < 0 )
4992 DisableCellEditControl();
4993 MakeCellVisible( coords
);
4995 // if this is the second click on this cell then start
4997 if ( m_waitForSlowClick
&&
4998 (coords
== m_currentCellCoords
) &&
4999 CanEnableCellControl())
5001 EnableCellEditControl();
5003 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5004 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5007 editor
->StartingClick();
5011 m_waitForSlowClick
= FALSE
;
5015 if ( event
.ControlDown() )
5017 m_selection
->ToggleCellSelection( coords
.GetRow(),
5019 event
.ControlDown(),
5023 m_selectingTopLeft
= wxGridNoCellCoords
;
5024 m_selectingBottomRight
= wxGridNoCellCoords
;
5025 m_selectingKeyboard
= coords
;
5029 SetCurrentCell( coords
);
5030 if ( m_selection
->GetSelectionMode()
5031 != wxGrid::wxGridSelectCells
)
5032 HighlightBlock( coords
, coords
);
5034 m_waitForSlowClick
= TRUE
;
5041 // ------------ Left double click
5043 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5045 DisableCellEditControl();
5047 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5049 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5057 // ------------ Left button released
5059 else if ( event
.LeftUp() )
5061 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5063 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5064 m_selectingBottomRight
!= wxGridNoCellCoords
)
5068 m_winCapture
->ReleaseMouse();
5069 m_winCapture
= NULL
;
5071 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5072 m_selectingTopLeft
.GetCol(),
5073 m_selectingBottomRight
.GetRow(),
5074 m_selectingBottomRight
.GetCol(),
5075 event
.ControlDown(),
5079 m_selectingTopLeft
= wxGridNoCellCoords
;
5080 m_selectingBottomRight
= wxGridNoCellCoords
;
5083 // Show the edit control, if it has been hidden for
5085 ShowCellEditControl();
5087 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5089 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5090 DoEndDragResizeRow();
5092 // Note: we are ending the event *after* doing
5093 // default processing in this case
5095 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5097 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5099 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5100 DoEndDragResizeCol();
5102 // Note: we are ending the event *after* doing
5103 // default processing in this case
5105 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5112 // ------------ Right button down
5114 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5116 DisableCellEditControl();
5117 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5122 // no default action at the moment
5127 // ------------ Right double click
5129 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5131 DisableCellEditControl();
5132 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5137 // no default action at the moment
5141 // ------------ Moving and no button action
5143 else if ( event
.Moving() && !event
.IsButton() )
5145 int dragRow
= YToEdgeOfRow( y
);
5146 int dragCol
= XToEdgeOfCol( x
);
5148 // Dragging on the corner of a cell to resize in both
5149 // directions is not implemented yet...
5151 if ( dragRow
>= 0 && dragCol
>= 0 )
5153 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5159 m_dragRowOrCol
= dragRow
;
5161 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5163 if ( CanDragRowSize() && CanDragGridSize() )
5164 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5169 m_dragRowOrCol
= dragCol
;
5177 m_dragRowOrCol
= dragCol
;
5179 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5181 if ( CanDragColSize() && CanDragGridSize() )
5182 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5188 // Neither on a row or col edge
5190 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5192 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5198 void wxGrid::DoEndDragResizeRow()
5200 if ( m_dragLastPos
>= 0 )
5202 // erase the last line and resize the row
5204 int cw
, ch
, left
, dummy
;
5205 m_gridWin
->GetClientSize( &cw
, &ch
);
5206 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5208 wxClientDC
dc( m_gridWin
);
5210 dc
.SetLogicalFunction( wxINVERT
);
5211 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5212 HideCellEditControl();
5213 SaveEditControlValue();
5215 int rowTop
= GetRowTop(m_dragRowOrCol
);
5216 SetRowSize( m_dragRowOrCol
,
5217 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5219 if ( !GetBatchCount() )
5221 // Only needed to get the correct rect.y:
5222 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5224 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5225 rect
.width
= m_rowLabelWidth
;
5226 rect
.height
= ch
- rect
.y
;
5227 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5229 m_gridWin
->Refresh( FALSE
, &rect
);
5232 ShowCellEditControl();
5237 void wxGrid::DoEndDragResizeCol()
5239 if ( m_dragLastPos
>= 0 )
5241 // erase the last line and resize the col
5243 int cw
, ch
, dummy
, top
;
5244 m_gridWin
->GetClientSize( &cw
, &ch
);
5245 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5247 wxClientDC
dc( m_gridWin
);
5249 dc
.SetLogicalFunction( wxINVERT
);
5250 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5251 HideCellEditControl();
5252 SaveEditControlValue();
5254 int colLeft
= GetColLeft(m_dragRowOrCol
);
5255 SetColSize( m_dragRowOrCol
,
5256 wxMax( m_dragLastPos
- colLeft
,
5257 GetColMinimalWidth(m_dragRowOrCol
) ) );
5259 if ( !GetBatchCount() )
5261 // Only needed to get the correct rect.x:
5262 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5264 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5265 rect
.width
= cw
- rect
.x
;
5266 rect
.height
= m_colLabelHeight
;
5267 m_colLabelWin
->Refresh( TRUE
, &rect
);
5269 m_gridWin
->Refresh( FALSE
, &rect
);
5272 ShowCellEditControl();
5279 // ------ interaction with data model
5281 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5283 switch ( msg
.GetId() )
5285 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5286 return GetModelValues();
5288 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5289 return SetModelValues();
5291 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5292 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5293 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5294 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5295 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5296 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5297 return Redimension( msg
);
5306 // The behaviour of this function depends on the grid table class
5307 // Clear() function. For the default wxGridStringTable class the
5308 // behavious is to replace all cell contents with wxEmptyString but
5309 // not to change the number of rows or cols.
5311 void wxGrid::ClearGrid()
5315 if (IsCellEditControlEnabled())
5316 DisableCellEditControl();
5319 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5324 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5326 // TODO: something with updateLabels flag
5330 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5336 if (IsCellEditControlEnabled())
5337 DisableCellEditControl();
5339 return m_table
->InsertRows( pos
, numRows
);
5341 // the table will have sent the results of the insert row
5342 // operation to this view object as a grid table message
5348 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5350 // TODO: something with updateLabels flag
5354 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5358 return ( m_table
&& m_table
->AppendRows( numRows
) );
5359 // the table will have sent the results of the append row
5360 // operation to this view object as a grid table message
5364 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5366 // TODO: something with updateLabels flag
5370 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5376 if (IsCellEditControlEnabled())
5377 DisableCellEditControl();
5379 return (m_table
->DeleteRows( pos
, numRows
));
5380 // the table will have sent the results of the delete row
5381 // operation to this view object as a grid table message
5387 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5389 // TODO: something with updateLabels flag
5393 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5399 if (IsCellEditControlEnabled())
5400 DisableCellEditControl();
5402 return m_table
->InsertCols( pos
, numCols
);
5403 // the table will have sent the results of the insert col
5404 // operation to this view object as a grid table message
5410 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5412 // TODO: something with updateLabels flag
5416 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5420 return ( m_table
&& m_table
->AppendCols( numCols
) );
5421 // the table will have sent the results of the append col
5422 // operation to this view object as a grid table message
5426 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5428 // TODO: something with updateLabels flag
5432 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5438 if (IsCellEditControlEnabled())
5439 DisableCellEditControl();
5441 return ( m_table
->DeleteCols( pos
, numCols
) );
5442 // the table will have sent the results of the delete col
5443 // operation to this view object as a grid table message
5451 // ----- event handlers
5454 // Generate a grid event based on a mouse event and
5455 // return the result of ProcessEvent()
5457 int wxGrid::SendEvent( const wxEventType type
,
5459 wxMouseEvent
& mouseEv
)
5464 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5466 int rowOrCol
= (row
== -1 ? col
: row
);
5468 wxGridSizeEvent
gridEvt( GetId(),
5472 mouseEv
.GetX() + GetRowLabelSize(),
5473 mouseEv
.GetY() + GetColLabelSize(),
5474 mouseEv
.ControlDown(),
5475 mouseEv
.ShiftDown(),
5477 mouseEv
.MetaDown() );
5479 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5480 vetoed
= !gridEvt
.IsAllowed();
5483 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5485 // Right now, it should _never_ end up here!
5486 wxGridRangeSelectEvent
gridEvt( GetId(),
5490 m_selectingBottomRight
,
5492 mouseEv
.ControlDown(),
5493 mouseEv
.ShiftDown(),
5495 mouseEv
.MetaDown() );
5497 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5498 vetoed
= !gridEvt
.IsAllowed();
5503 wxGridEvent
gridEvt( GetId(),
5507 mouseEv
.GetX() + GetRowLabelSize(),
5508 mouseEv
.GetY() + GetColLabelSize(),
5510 mouseEv
.ControlDown(),
5511 mouseEv
.ShiftDown(),
5513 mouseEv
.MetaDown() );
5514 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5515 vetoed
= !gridEvt
.IsAllowed();
5518 // A Veto'd event may not be `claimed' so test this first
5519 if (vetoed
) return -1;
5520 return claimed
? 1 : 0;
5526 // Generate a grid event of specified type and return the result
5527 // of ProcessEvent().
5529 int wxGrid::SendEvent( const wxEventType type
,
5535 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5537 int rowOrCol
= (row
== -1 ? col
: row
);
5539 wxGridSizeEvent
gridEvt( GetId(),
5544 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5545 vetoed
= !gridEvt
.IsAllowed();
5549 wxGridEvent
gridEvt( GetId(),
5554 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5555 vetoed
= !gridEvt
.IsAllowed();
5558 // A Veto'd event may not be `claimed' so test this first
5559 if (vetoed
) return -1;
5560 return claimed
? 1 : 0;
5565 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5567 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5571 // This is just here to make sure that CalcDimensions gets called when
5572 // the grid view is resized... then the size event is skipped to allow
5573 // the box sizers to handle everything
5575 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5582 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5584 if ( m_inOnKeyDown
)
5586 // shouldn't be here - we are going round in circles...
5588 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5591 m_inOnKeyDown
= TRUE
;
5593 // propagate the event up and see if it gets processed
5595 wxWindow
*parent
= GetParent();
5596 wxKeyEvent
keyEvt( event
);
5597 keyEvt
.SetEventObject( parent
);
5599 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5602 // try local handlers
5604 switch ( event
.KeyCode() )
5607 if ( event
.ControlDown() )
5609 MoveCursorUpBlock( event
.ShiftDown() );
5613 MoveCursorUp( event
.ShiftDown() );
5618 if ( event
.ControlDown() )
5620 MoveCursorDownBlock( event
.ShiftDown() );
5624 MoveCursorDown( event
.ShiftDown() );
5629 if ( event
.ControlDown() )
5631 MoveCursorLeftBlock( event
.ShiftDown() );
5635 MoveCursorLeft( event
.ShiftDown() );
5640 if ( event
.ControlDown() )
5642 MoveCursorRightBlock( event
.ShiftDown() );
5646 MoveCursorRight( event
.ShiftDown() );
5651 case WXK_NUMPAD_ENTER
:
5652 if ( event
.ControlDown() )
5654 event
.Skip(); // to let the edit control have the return
5658 if ( GetGridCursorRow() < GetNumberRows()-1 )
5660 MoveCursorDown( event
.ShiftDown() );
5664 // at the bottom of a column
5665 HideCellEditControl();
5666 SaveEditControlValue();
5676 if (event
.ShiftDown())
5678 if ( GetGridCursorCol() > 0 )
5680 MoveCursorLeft( FALSE
);
5685 HideCellEditControl();
5686 SaveEditControlValue();
5691 if ( GetGridCursorCol() < GetNumberCols()-1 )
5693 MoveCursorRight( FALSE
);
5698 HideCellEditControl();
5699 SaveEditControlValue();
5705 if ( event
.ControlDown() )
5707 MakeCellVisible( 0, 0 );
5708 SetCurrentCell( 0, 0 );
5717 if ( event
.ControlDown() )
5719 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5720 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5737 if ( event
.ControlDown() )
5739 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5740 m_currentCellCoords
.GetCol(),
5741 event
.ControlDown(),
5747 if ( !IsEditable() )
5749 MoveCursorRight( FALSE
);
5752 // Otherwise fall through to default
5755 // is it possible to edit the current cell at all?
5756 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5758 // yes, now check whether the cells editor accepts the key
5759 int row
= m_currentCellCoords
.GetRow();
5760 int col
= m_currentCellCoords
.GetCol();
5761 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5762 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5764 // <F2> is special and will always start editing, for
5765 // other keys - ask the editor itself
5766 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5767 || editor
->IsAcceptedKey(event
) )
5769 EnableCellEditControl();
5770 editor
->StartingKey(event
);
5782 // let others process char events with modifiers or all
5783 // char events for readonly cells
5790 m_inOnKeyDown
= FALSE
;
5793 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5795 // try local handlers
5797 if ( event
.KeyCode() == WXK_SHIFT
)
5799 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5800 m_selectingBottomRight
!= wxGridNoCellCoords
)
5801 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5802 m_selectingTopLeft
.GetCol(),
5803 m_selectingBottomRight
.GetRow(),
5804 m_selectingBottomRight
.GetCol(),
5805 event
.ControlDown(),
5809 m_selectingTopLeft
= wxGridNoCellCoords
;
5810 m_selectingBottomRight
= wxGridNoCellCoords
;
5811 m_selectingKeyboard
= wxGridNoCellCoords
;
5815 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5819 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5821 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5823 // the event has been intercepted - do nothing
5827 wxClientDC
dc(m_gridWin
);
5830 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5832 HideCellEditControl();
5833 DisableCellEditControl();
5835 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5838 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5839 if ( !m_gridLinesEnabled
)
5847 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5849 // Otherwise refresh redraws the highlight!
5850 m_currentCellCoords
= coords
;
5852 DrawGridCellArea(dc
,cells
);
5853 DrawAllGridLines( dc
, r
);
5857 m_currentCellCoords
= coords
;
5859 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5860 DrawCellHighlight(dc
, attr
);
5865 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5868 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5870 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5873 rightCol
= GetNumberCols() - 1;
5875 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5878 bottomRow
= GetNumberRows() - 1;
5880 if ( topRow
> bottomRow
)
5887 if ( leftCol
> rightCol
)
5894 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5895 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5897 if ( m_selectingTopLeft
!= updateTopLeft
||
5898 m_selectingBottomRight
!= updateBottomRight
)
5900 // Compute two optimal update rectangles:
5901 // Either one rectangle is a real subset of the
5902 // other, or they are (almost) disjoint!
5904 bool need_refresh
[4];
5908 need_refresh
[3] = FALSE
;
5911 // Store intermediate values
5912 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5913 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5914 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5915 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5917 // Determine the outer/inner coordinates.
5918 if (oldLeft
> leftCol
)
5924 if (oldTop
> topRow
)
5930 if (oldRight
< rightCol
)
5933 oldRight
= rightCol
;
5936 if (oldBottom
< bottomRow
)
5939 oldBottom
= bottomRow
;
5943 // Now, either the stuff marked old is the outer
5944 // rectangle or we don't have a situation where one
5945 // is contained in the other.
5947 if ( oldLeft
< leftCol
)
5949 need_refresh
[0] = TRUE
;
5950 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5952 wxGridCellCoords ( oldBottom
,
5956 if ( oldTop
< topRow
)
5958 need_refresh
[1] = TRUE
;
5959 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5961 wxGridCellCoords ( topRow
- 1,
5965 if ( oldRight
> rightCol
)
5967 need_refresh
[2] = TRUE
;
5968 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5970 wxGridCellCoords ( oldBottom
,
5974 if ( oldBottom
> bottomRow
)
5976 need_refresh
[3] = TRUE
;
5977 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5979 wxGridCellCoords ( oldBottom
,
5985 m_selectingTopLeft
= updateTopLeft
;
5986 m_selectingBottomRight
= updateBottomRight
;
5988 // various Refresh() calls
5989 for (i
= 0; i
< 4; i
++ )
5990 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5991 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5994 // never generate an event as it will be generated from
5995 // wxGridSelection::SelectBlock!
5996 // (old comment from when this was the body of SelectBlock)
6000 // ------ functions to get/send data (see also public functions)
6003 bool wxGrid::GetModelValues()
6007 // all we need to do is repaint the grid
6009 m_gridWin
->Refresh();
6017 bool wxGrid::SetModelValues()
6023 for ( row
= 0; row
< m_numRows
; row
++ )
6025 for ( col
= 0; col
< m_numCols
; col
++ )
6027 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6039 // Note - this function only draws cells that are in the list of
6040 // exposed cells (usually set from the update region by
6041 // CalcExposedCells)
6043 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6045 if ( !m_numRows
|| !m_numCols
) return;
6048 size_t numCells
= cells
.GetCount();
6050 for ( i
= 0; i
< numCells
; i
++ )
6052 DrawCell( dc
, cells
[i
] );
6057 void wxGrid::DrawGridSpace( wxDC
& dc
)
6060 m_gridWin
->GetClientSize( &cw
, &ch
);
6063 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6065 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6066 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6068 if ( right
> rightCol
|| bottom
> bottomRow
)
6071 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6073 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6074 dc
.SetPen( *wxTRANSPARENT_PEN
);
6076 if ( right
> rightCol
)
6078 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6081 if ( bottom
> bottomRow
)
6083 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6089 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6091 int row
= coords
.GetRow();
6092 int col
= coords
.GetCol();
6094 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6097 // we draw the cell border ourselves
6098 #if !WXGRID_DRAW_LINES
6099 if ( m_gridLinesEnabled
)
6100 DrawCellBorder( dc
, coords
);
6103 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6105 bool isCurrent
= coords
== m_currentCellCoords
;
6107 wxRect rect
= CellToRect( row
, col
);
6109 // if the editor is shown, we should use it and not the renderer
6110 // Note: However, only if it is really _shown_, i.e. not hidden!
6111 if ( isCurrent
&& IsCellEditControlShown() )
6113 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6114 editor
->PaintBackground(rect
, attr
);
6119 // but all the rest is drawn by the cell renderer and hence may be
6121 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6122 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6129 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6131 int row
= m_currentCellCoords
.GetRow();
6132 int col
= m_currentCellCoords
.GetCol();
6134 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6137 wxRect rect
= CellToRect(row
, col
);
6139 // hmmm... what could we do here to show that the cell is disabled?
6140 // for now, I just draw a thinner border than for the other ones, but
6141 // it doesn't look really good
6143 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6147 // The center of th drawn line is where the position/width/height of
6148 // the rectangle is actually at, (on wxMSW atr least,) so we will
6149 // reduce the size of the rectangle to compensate for the thickness of
6150 // the line. If this is too strange on non wxMSW platforms then
6151 // please #ifdef this appropriately.
6152 rect
.x
+= penWidth
/2;
6153 rect
.y
+= penWidth
/2;
6154 rect
.width
-= penWidth
-1;
6155 rect
.height
-= penWidth
-1;
6158 // Now draw the rectangle
6159 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6160 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6161 dc
.DrawRectangle(rect
);
6165 // VZ: my experiments with 3d borders...
6167 // how to properly set colours for arbitrary bg?
6168 wxCoord x1
= rect
.x
,
6170 x2
= rect
.x
+ rect
.width
-1,
6171 y2
= rect
.y
+ rect
.height
-1;
6173 dc
.SetPen(*wxWHITE_PEN
);
6174 dc
.DrawLine(x1
, y1
, x2
, y1
);
6175 dc
.DrawLine(x1
, y1
, x1
, y2
);
6177 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6178 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6180 dc
.SetPen(*wxBLACK_PEN
);
6181 dc
.DrawLine(x1
, y2
, x2
, y2
);
6182 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6187 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6189 int row
= coords
.GetRow();
6190 int col
= coords
.GetCol();
6191 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6194 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6196 // right hand border
6198 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6199 GetColRight(col
), GetRowBottom(row
) );
6203 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6204 GetColRight(col
), GetRowBottom(row
) );
6207 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6209 // This if block was previously in wxGrid::OnPaint but that doesn't
6210 // seem to get called under wxGTK - MB
6212 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6213 m_numRows
&& m_numCols
)
6215 m_currentCellCoords
.Set(0, 0);
6218 if ( IsCellEditControlShown() )
6220 // don't show highlight when the edit control is shown
6224 // if the active cell was repainted, repaint its highlight too because it
6225 // might have been damaged by the grid lines
6226 size_t count
= cells
.GetCount();
6227 for ( size_t n
= 0; n
< count
; n
++ )
6229 if ( cells
[n
] == m_currentCellCoords
)
6231 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6232 DrawCellHighlight(dc
, attr
);
6240 // TODO: remove this ???
6241 // This is used to redraw all grid lines e.g. when the grid line colour
6244 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6246 if ( !m_gridLinesEnabled
||
6248 !m_numCols
) return;
6250 int top
, bottom
, left
, right
;
6252 #if 0 //#ifndef __WXGTK__
6256 m_gridWin
->GetClientSize(&cw
, &ch
);
6258 // virtual coords of visible area
6260 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6261 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6266 reg
.GetBox(x
, y
, w
, h
);
6267 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6268 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6272 m_gridWin
->GetClientSize(&cw
, &ch
);
6273 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6274 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6277 // avoid drawing grid lines past the last row and col
6279 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6280 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6282 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6284 // horizontal grid lines
6287 for ( i
= 0; i
< m_numRows
; i
++ )
6289 int bot
= GetRowBottom(i
) - 1;
6298 dc
.DrawLine( left
, bot
, right
, bot
);
6303 // vertical grid lines
6305 for ( i
= 0; i
< m_numCols
; i
++ )
6307 int colRight
= GetColRight(i
) - 1;
6308 if ( colRight
> right
)
6313 if ( colRight
>= left
)
6315 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6321 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6323 if ( !m_numRows
) return;
6326 size_t numLabels
= rows
.GetCount();
6328 for ( i
= 0; i
< numLabels
; i
++ )
6330 DrawRowLabel( dc
, rows
[i
] );
6335 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6337 if ( GetRowHeight(row
) <= 0 )
6340 int rowTop
= GetRowTop(row
),
6341 rowBottom
= GetRowBottom(row
) - 1;
6343 dc
.SetPen( *wxBLACK_PEN
);
6344 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6345 m_rowLabelWidth
-1, rowBottom
);
6347 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6349 dc
.SetPen( *wxWHITE_PEN
);
6350 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6351 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6353 dc
.SetBackgroundMode( wxTRANSPARENT
);
6354 dc
.SetTextForeground( GetLabelTextColour() );
6355 dc
.SetFont( GetLabelFont() );
6358 GetRowLabelAlignment( &hAlign
, &vAlign
);
6362 rect
.SetY( GetRowTop(row
) + 2 );
6363 rect
.SetWidth( m_rowLabelWidth
- 4 );
6364 rect
.SetHeight( GetRowHeight(row
) - 4 );
6365 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6369 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6371 if ( !m_numCols
) return;
6374 size_t numLabels
= cols
.GetCount();
6376 for ( i
= 0; i
< numLabels
; i
++ )
6378 DrawColLabel( dc
, cols
[i
] );
6383 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6385 if ( GetColWidth(col
) <= 0 )
6388 int colLeft
= GetColLeft(col
),
6389 colRight
= GetColRight(col
) - 1;
6391 dc
.SetPen( *wxBLACK_PEN
);
6392 dc
.DrawLine( colRight
, 0,
6393 colRight
, m_colLabelHeight
-1 );
6395 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6396 colRight
, m_colLabelHeight
-1 );
6398 dc
.SetPen( *wxWHITE_PEN
);
6399 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6400 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6402 dc
.SetBackgroundMode( wxTRANSPARENT
);
6403 dc
.SetTextForeground( GetLabelTextColour() );
6404 dc
.SetFont( GetLabelFont() );
6406 dc
.SetBackgroundMode( wxTRANSPARENT
);
6407 dc
.SetTextForeground( GetLabelTextColour() );
6408 dc
.SetFont( GetLabelFont() );
6411 GetColLabelAlignment( &hAlign
, &vAlign
);
6414 rect
.SetX( colLeft
+ 2 );
6416 rect
.SetWidth( GetColWidth(col
) - 4 );
6417 rect
.SetHeight( m_colLabelHeight
- 4 );
6418 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6421 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6422 const wxString
& value
,
6427 wxArrayString lines
;
6429 dc
.SetClippingRegion( rect
);
6430 StringToLines( value
, lines
);
6433 //Forward to new API.
6434 DrawTextRectangle( dc
,
6442 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6443 const wxArrayString
& lines
,
6448 long textWidth
, textHeight
;
6449 long lineWidth
, lineHeight
;
6451 if ( lines
.GetCount() )
6453 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6454 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6457 switch ( horizAlign
)
6460 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6463 case wxALIGN_CENTRE
:
6464 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6473 switch ( vertAlign
)
6475 case wxALIGN_BOTTOM
:
6476 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6479 case wxALIGN_CENTRE
:
6480 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6489 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6491 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6496 dc
.DestroyClippingRegion();
6500 // Split multi line text up into an array of strings. Any existing
6501 // contents of the string array are preserved.
6503 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6507 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6508 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6510 while ( startPos
< (int)tVal
.Length() )
6512 pos
= tVal
.Mid(startPos
).Find( eol
);
6517 else if ( pos
== 0 )
6519 lines
.Add( wxEmptyString
);
6523 lines
.Add( value
.Mid(startPos
, pos
) );
6527 if ( startPos
< (int)value
.Length() )
6529 lines
.Add( value
.Mid( startPos
) );
6534 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6535 const wxArrayString
& lines
,
6536 long *width
, long *height
)
6543 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6545 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6546 w
= wxMax( w
, lineW
);
6555 // ------ Batch processing.
6557 void wxGrid::EndBatch()
6559 if ( m_batchCount
> 0 )
6562 if ( !m_batchCount
)
6565 m_rowLabelWin
->Refresh();
6566 m_colLabelWin
->Refresh();
6567 m_cornerLabelWin
->Refresh();
6568 m_gridWin
->Refresh();
6573 // Use this, rather than wxWindow::Refresh(), to force an immediate
6574 // repainting of the grid. Has no effect if you are already inside a
6575 // BeginBatch / EndBatch block.
6577 void wxGrid::ForceRefresh()
6585 // ------ Edit control functions
6589 void wxGrid::EnableEditing( bool edit
)
6591 // TODO: improve this ?
6593 if ( edit
!= m_editable
)
6595 if(!edit
) EnableCellEditControl(edit
);
6601 void wxGrid::EnableCellEditControl( bool enable
)
6606 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6607 SetCurrentCell( 0, 0 );
6609 if ( enable
!= m_cellEditCtrlEnabled
)
6613 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6616 // this should be checked by the caller!
6617 wxASSERT_MSG( CanEnableCellControl(),
6618 _T("can't enable editing for this cell!") );
6620 // do it before ShowCellEditControl()
6621 m_cellEditCtrlEnabled
= enable
;
6623 ShowCellEditControl();
6627 //FIXME:add veto support
6628 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
6630 HideCellEditControl();
6631 SaveEditControlValue();
6633 // do it after HideCellEditControl()
6634 m_cellEditCtrlEnabled
= enable
;
6639 bool wxGrid::IsCurrentCellReadOnly() const
6642 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6643 bool readonly
= attr
->IsReadOnly();
6649 bool wxGrid::CanEnableCellControl() const
6651 return m_editable
&& !IsCurrentCellReadOnly();
6654 bool wxGrid::IsCellEditControlEnabled() const
6656 // the cell edit control might be disable for all cells or just for the
6657 // current one if it's read only
6658 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6661 bool wxGrid::IsCellEditControlShown() const
6663 bool isShown
= FALSE
;
6665 if ( m_cellEditCtrlEnabled
)
6667 int row
= m_currentCellCoords
.GetRow();
6668 int col
= m_currentCellCoords
.GetCol();
6669 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6670 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6675 if ( editor
->IsCreated() )
6677 isShown
= editor
->GetControl()->IsShown();
6687 void wxGrid::ShowCellEditControl()
6689 if ( IsCellEditControlEnabled() )
6691 if ( !IsVisible( m_currentCellCoords
) )
6693 m_cellEditCtrlEnabled
= FALSE
;
6698 wxRect rect
= CellToRect( m_currentCellCoords
);
6699 int row
= m_currentCellCoords
.GetRow();
6700 int col
= m_currentCellCoords
.GetCol();
6702 // convert to scrolled coords
6704 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6706 // done in PaintBackground()
6708 // erase the highlight and the cell contents because the editor
6709 // might not cover the entire cell
6710 wxClientDC
dc( m_gridWin
);
6712 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6713 dc
.SetPen(*wxTRANSPARENT_PEN
);
6714 dc
.DrawRectangle(rect
);
6717 // cell is shifted by one pixel
6721 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6722 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6723 if ( !editor
->IsCreated() )
6725 editor
->Create(m_gridWin
, -1,
6726 new wxGridCellEditorEvtHandler(this, editor
));
6728 wxGridEditorCreatedEvent
evt(GetId(),
6729 wxEVT_GRID_EDITOR_CREATED
,
6733 editor
->GetControl());
6734 GetEventHandler()->ProcessEvent(evt
);
6737 editor
->Show( TRUE
, attr
);
6739 editor
->SetSize( rect
);
6741 editor
->BeginEdit(row
, col
, this);
6750 void wxGrid::HideCellEditControl()
6752 if ( IsCellEditControlEnabled() )
6754 int row
= m_currentCellCoords
.GetRow();
6755 int col
= m_currentCellCoords
.GetCol();
6757 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6758 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6759 editor
->Show( FALSE
);
6762 m_gridWin
->SetFocus();
6763 wxRect
rect( CellToRect( row
, col
) );
6764 m_gridWin
->Refresh( FALSE
, &rect
);
6769 void wxGrid::SaveEditControlValue()
6771 if ( IsCellEditControlEnabled() )
6773 int row
= m_currentCellCoords
.GetRow();
6774 int col
= m_currentCellCoords
.GetCol();
6776 wxString oldval
= GetCellValue(row
,col
);
6778 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6779 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6780 bool changed
= editor
->EndEdit(row
, col
, this);
6787 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
6788 m_currentCellCoords
.GetRow(),
6789 m_currentCellCoords
.GetCol() ) < 0 ) {
6791 //Event has been veto set the data back.
6792 SetCellValue(row
,col
,oldval
);
6800 // ------ Grid location functions
6801 // Note that all of these functions work with the logical coordinates of
6802 // grid cells and labels so you will need to convert from device
6803 // coordinates for mouse events etc.
6806 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6808 int row
= YToRow(y
);
6809 int col
= XToCol(x
);
6811 if ( row
== -1 || col
== -1 )
6813 coords
= wxGridNoCellCoords
;
6817 coords
.Set( row
, col
);
6822 int wxGrid::YToRow( int y
)
6826 for ( i
= 0; i
< m_numRows
; i
++ )
6828 if ( y
< GetRowBottom(i
) )
6836 int wxGrid::XToCol( int x
)
6840 for ( i
= 0; i
< m_numCols
; i
++ )
6842 if ( x
< GetColRight(i
) )
6850 // return the row number that that the y coord is near the edge of, or
6851 // -1 if not near an edge
6853 int wxGrid::YToEdgeOfRow( int y
)
6857 for ( i
= 0; i
< m_numRows
; i
++ )
6859 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6861 d
= abs( y
- GetRowBottom(i
) );
6862 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6871 // return the col number that that the x coord is near the edge of, or
6872 // -1 if not near an edge
6874 int wxGrid::XToEdgeOfCol( int x
)
6878 for ( i
= 0; i
< m_numCols
; i
++ )
6880 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6882 d
= abs( x
- GetColRight(i
) );
6883 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6892 wxRect
wxGrid::CellToRect( int row
, int col
)
6894 wxRect
rect( -1, -1, -1, -1 );
6896 if ( row
>= 0 && row
< m_numRows
&&
6897 col
>= 0 && col
< m_numCols
)
6899 rect
.x
= GetColLeft(col
);
6900 rect
.y
= GetRowTop(row
);
6901 rect
.width
= GetColWidth(col
);
6902 rect
.height
= GetRowHeight(row
);
6905 // if grid lines are enabled, then the area of the cell is a bit smaller
6906 if (m_gridLinesEnabled
) {
6914 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6916 // get the cell rectangle in logical coords
6918 wxRect
r( CellToRect( row
, col
) );
6920 // convert to device coords
6922 int left
, top
, right
, bottom
;
6923 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6924 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6926 // check against the client area of the grid window
6929 m_gridWin
->GetClientSize( &cw
, &ch
);
6931 if ( wholeCellVisible
)
6933 // is the cell wholly visible ?
6935 return ( left
>= 0 && right
<= cw
&&
6936 top
>= 0 && bottom
<= ch
);
6940 // is the cell partly visible ?
6942 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6943 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6948 // make the specified cell location visible by doing a minimal amount
6951 void wxGrid::MakeCellVisible( int row
, int col
)
6955 int xpos
= -1, ypos
= -1;
6957 if ( row
>= 0 && row
< m_numRows
&&
6958 col
>= 0 && col
< m_numCols
)
6960 // get the cell rectangle in logical coords
6962 wxRect
r( CellToRect( row
, col
) );
6964 // convert to device coords
6966 int left
, top
, right
, bottom
;
6967 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6968 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6971 m_gridWin
->GetClientSize( &cw
, &ch
);
6977 else if ( bottom
> ch
)
6979 int h
= r
.GetHeight();
6981 for ( i
= row
-1; i
>= 0; i
-- )
6983 int rowHeight
= GetRowHeight(i
);
6984 if ( h
+ rowHeight
> ch
)
6991 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6992 // have rounding errors (this is important, because if we do, we
6993 // might not scroll at all and some cells won't be redrawn)
6995 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
6997 ypos
+= GRID_SCROLL_LINE
;
7004 else if ( right
> cw
)
7006 int w
= r
.GetWidth();
7008 for ( i
= col
-1; i
>= 0; i
-- )
7010 int colWidth
= GetColWidth(i
);
7011 if ( w
+ colWidth
> cw
)
7018 // see comment for ypos above
7019 xpos
+= GRID_SCROLL_LINE
;
7022 if ( xpos
!= -1 || ypos
!= -1 )
7024 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
7025 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
7026 Scroll( xpos
, ypos
);
7034 // ------ Grid cursor movement functions
7037 bool wxGrid::MoveCursorUp( bool expandSelection
)
7039 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7040 m_currentCellCoords
.GetRow() >= 0 )
7042 if ( expandSelection
)
7044 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7045 m_selectingKeyboard
= m_currentCellCoords
;
7046 if ( m_selectingKeyboard
.GetRow() > 0 )
7048 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7049 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7050 m_selectingKeyboard
.GetCol() );
7051 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7054 else if ( m_currentCellCoords
.GetRow() > 0 )
7057 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7058 m_currentCellCoords
.GetCol() );
7059 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7060 m_currentCellCoords
.GetCol() );
7071 bool wxGrid::MoveCursorDown( bool expandSelection
)
7073 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7074 m_currentCellCoords
.GetRow() < m_numRows
)
7076 if ( expandSelection
)
7078 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7079 m_selectingKeyboard
= m_currentCellCoords
;
7080 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7082 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7083 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7084 m_selectingKeyboard
.GetCol() );
7085 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7088 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7091 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7092 m_currentCellCoords
.GetCol() );
7093 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7094 m_currentCellCoords
.GetCol() );
7105 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7107 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7108 m_currentCellCoords
.GetCol() >= 0 )
7110 if ( expandSelection
)
7112 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7113 m_selectingKeyboard
= m_currentCellCoords
;
7114 if ( m_selectingKeyboard
.GetCol() > 0 )
7116 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7117 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7118 m_selectingKeyboard
.GetCol() );
7119 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7122 else if ( m_currentCellCoords
.GetCol() > 0 )
7125 MakeCellVisible( m_currentCellCoords
.GetRow(),
7126 m_currentCellCoords
.GetCol() - 1 );
7127 SetCurrentCell( m_currentCellCoords
.GetRow(),
7128 m_currentCellCoords
.GetCol() - 1 );
7139 bool wxGrid::MoveCursorRight( bool expandSelection
)
7141 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7142 m_currentCellCoords
.GetCol() < m_numCols
)
7144 if ( expandSelection
)
7146 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7147 m_selectingKeyboard
= m_currentCellCoords
;
7148 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7150 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7151 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7152 m_selectingKeyboard
.GetCol() );
7153 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7156 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7159 MakeCellVisible( m_currentCellCoords
.GetRow(),
7160 m_currentCellCoords
.GetCol() + 1 );
7161 SetCurrentCell( m_currentCellCoords
.GetRow(),
7162 m_currentCellCoords
.GetCol() + 1 );
7173 bool wxGrid::MovePageUp()
7175 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7177 int row
= m_currentCellCoords
.GetRow();
7181 m_gridWin
->GetClientSize( &cw
, &ch
);
7183 int y
= GetRowTop(row
);
7184 int newRow
= YToRow( y
- ch
+ 1 );
7189 else if ( newRow
== row
)
7194 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7195 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7203 bool wxGrid::MovePageDown()
7205 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7207 int row
= m_currentCellCoords
.GetRow();
7208 if ( row
< m_numRows
)
7211 m_gridWin
->GetClientSize( &cw
, &ch
);
7213 int y
= GetRowTop(row
);
7214 int newRow
= YToRow( y
+ ch
);
7217 newRow
= m_numRows
- 1;
7219 else if ( newRow
== row
)
7224 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7225 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7233 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7236 m_currentCellCoords
!= wxGridNoCellCoords
&&
7237 m_currentCellCoords
.GetRow() > 0 )
7239 int row
= m_currentCellCoords
.GetRow();
7240 int col
= m_currentCellCoords
.GetCol();
7242 if ( m_table
->IsEmptyCell(row
, col
) )
7244 // starting in an empty cell: find the next block of
7250 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7253 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7255 // starting at the top of a block: find the next block
7261 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7266 // starting within a block: find the top of the block
7271 if ( m_table
->IsEmptyCell(row
, col
) )
7279 MakeCellVisible( row
, col
);
7280 if ( expandSelection
)
7282 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7283 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7288 SetCurrentCell( row
, col
);
7296 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7299 m_currentCellCoords
!= wxGridNoCellCoords
&&
7300 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7302 int row
= m_currentCellCoords
.GetRow();
7303 int col
= m_currentCellCoords
.GetCol();
7305 if ( m_table
->IsEmptyCell(row
, col
) )
7307 // starting in an empty cell: find the next block of
7310 while ( row
< m_numRows
-1 )
7313 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7316 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7318 // starting at the bottom of a block: find the next block
7321 while ( row
< m_numRows
-1 )
7324 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7329 // starting within a block: find the bottom of the block
7331 while ( row
< m_numRows
-1 )
7334 if ( m_table
->IsEmptyCell(row
, col
) )
7342 MakeCellVisible( row
, col
);
7343 if ( expandSelection
)
7345 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7346 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7351 SetCurrentCell( row
, col
);
7360 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7363 m_currentCellCoords
!= wxGridNoCellCoords
&&
7364 m_currentCellCoords
.GetCol() > 0 )
7366 int row
= m_currentCellCoords
.GetRow();
7367 int col
= m_currentCellCoords
.GetCol();
7369 if ( m_table
->IsEmptyCell(row
, col
) )
7371 // starting in an empty cell: find the next block of
7377 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7380 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7382 // starting at the left of a block: find the next block
7388 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7393 // starting within a block: find the left of the block
7398 if ( m_table
->IsEmptyCell(row
, col
) )
7406 MakeCellVisible( row
, col
);
7407 if ( expandSelection
)
7409 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7410 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7415 SetCurrentCell( row
, col
);
7424 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7427 m_currentCellCoords
!= wxGridNoCellCoords
&&
7428 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7430 int row
= m_currentCellCoords
.GetRow();
7431 int col
= m_currentCellCoords
.GetCol();
7433 if ( m_table
->IsEmptyCell(row
, col
) )
7435 // starting in an empty cell: find the next block of
7438 while ( col
< m_numCols
-1 )
7441 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7444 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7446 // starting at the right of a block: find the next block
7449 while ( col
< m_numCols
-1 )
7452 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7457 // starting within a block: find the right of the block
7459 while ( col
< m_numCols
-1 )
7462 if ( m_table
->IsEmptyCell(row
, col
) )
7470 MakeCellVisible( row
, col
);
7471 if ( expandSelection
)
7473 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7474 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7479 SetCurrentCell( row
, col
);
7491 // ------ Label values and formatting
7494 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7496 *horiz
= m_rowLabelHorizAlign
;
7497 *vert
= m_rowLabelVertAlign
;
7500 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7502 *horiz
= m_colLabelHorizAlign
;
7503 *vert
= m_colLabelVertAlign
;
7506 wxString
wxGrid::GetRowLabelValue( int row
)
7510 return m_table
->GetRowLabelValue( row
);
7520 wxString
wxGrid::GetColLabelValue( int col
)
7524 return m_table
->GetColLabelValue( col
);
7535 void wxGrid::SetRowLabelSize( int width
)
7537 width
= wxMax( width
, 0 );
7538 if ( width
!= m_rowLabelWidth
)
7542 m_rowLabelWin
->Show( FALSE
);
7543 m_cornerLabelWin
->Show( FALSE
);
7545 else if ( m_rowLabelWidth
== 0 )
7547 m_rowLabelWin
->Show( TRUE
);
7548 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7551 m_rowLabelWidth
= width
;
7558 void wxGrid::SetColLabelSize( int height
)
7560 height
= wxMax( height
, 0 );
7561 if ( height
!= m_colLabelHeight
)
7565 m_colLabelWin
->Show( FALSE
);
7566 m_cornerLabelWin
->Show( FALSE
);
7568 else if ( m_colLabelHeight
== 0 )
7570 m_colLabelWin
->Show( TRUE
);
7571 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7574 m_colLabelHeight
= height
;
7581 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7583 if ( m_labelBackgroundColour
!= colour
)
7585 m_labelBackgroundColour
= colour
;
7586 m_rowLabelWin
->SetBackgroundColour( colour
);
7587 m_colLabelWin
->SetBackgroundColour( colour
);
7588 m_cornerLabelWin
->SetBackgroundColour( colour
);
7590 if ( !GetBatchCount() )
7592 m_rowLabelWin
->Refresh();
7593 m_colLabelWin
->Refresh();
7594 m_cornerLabelWin
->Refresh();
7599 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7601 if ( m_labelTextColour
!= colour
)
7603 m_labelTextColour
= colour
;
7604 if ( !GetBatchCount() )
7606 m_rowLabelWin
->Refresh();
7607 m_colLabelWin
->Refresh();
7612 void wxGrid::SetLabelFont( const wxFont
& font
)
7615 if ( !GetBatchCount() )
7617 m_rowLabelWin
->Refresh();
7618 m_colLabelWin
->Refresh();
7622 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7624 // allow old (incorrect) defs to be used
7627 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7628 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7629 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7634 case wxTOP
: vert
= wxALIGN_TOP
; break;
7635 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7636 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7639 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7641 m_rowLabelHorizAlign
= horiz
;
7644 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7646 m_rowLabelVertAlign
= vert
;
7649 if ( !GetBatchCount() )
7651 m_rowLabelWin
->Refresh();
7655 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7657 // allow old (incorrect) defs to be used
7660 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7661 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7662 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7667 case wxTOP
: vert
= wxALIGN_TOP
; break;
7668 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7669 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7672 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7674 m_colLabelHorizAlign
= horiz
;
7677 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7679 m_colLabelVertAlign
= vert
;
7682 if ( !GetBatchCount() )
7684 m_colLabelWin
->Refresh();
7688 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7692 m_table
->SetRowLabelValue( row
, s
);
7693 if ( !GetBatchCount() )
7695 wxRect rect
= CellToRect( row
, 0);
7696 if ( rect
.height
> 0 )
7698 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7700 rect
.width
= m_rowLabelWidth
;
7701 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7707 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7711 m_table
->SetColLabelValue( col
, s
);
7712 if ( !GetBatchCount() )
7714 wxRect rect
= CellToRect( 0, col
);
7715 if ( rect
.width
> 0 )
7717 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7719 rect
.height
= m_colLabelHeight
;
7720 m_colLabelWin
->Refresh( TRUE
, &rect
);
7726 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7728 if ( m_gridLineColour
!= colour
)
7730 m_gridLineColour
= colour
;
7732 wxClientDC
dc( m_gridWin
);
7734 DrawAllGridLines( dc
, wxRegion() );
7739 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7741 if ( m_cellHighlightColour
!= colour
)
7743 m_cellHighlightColour
= colour
;
7745 wxClientDC
dc( m_gridWin
);
7747 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7748 DrawCellHighlight(dc
, attr
);
7753 void wxGrid::SetCellHighlightPenWidth(int width
)
7755 if (m_cellHighlightPenWidth
!= width
) {
7756 m_cellHighlightPenWidth
= width
;
7758 // Just redrawing the cell highlight is not enough since that won't
7759 // make any visible change if the the thickness is getting smaller.
7760 int row
= m_currentCellCoords
.GetRow();
7761 int col
= m_currentCellCoords
.GetCol();
7762 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7764 wxRect rect
= CellToRect(row
, col
);
7765 m_gridWin
->Refresh(TRUE
, &rect
);
7769 void wxGrid::SetCellHighlightROPenWidth(int width
)
7771 if (m_cellHighlightROPenWidth
!= width
) {
7772 m_cellHighlightROPenWidth
= width
;
7774 // Just redrawing the cell highlight is not enough since that won't
7775 // make any visible change if the the thickness is getting smaller.
7776 int row
= m_currentCellCoords
.GetRow();
7777 int col
= m_currentCellCoords
.GetCol();
7778 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7780 wxRect rect
= CellToRect(row
, col
);
7781 m_gridWin
->Refresh(TRUE
, &rect
);
7785 void wxGrid::EnableGridLines( bool enable
)
7787 if ( enable
!= m_gridLinesEnabled
)
7789 m_gridLinesEnabled
= enable
;
7791 if ( !GetBatchCount() )
7795 wxClientDC
dc( m_gridWin
);
7797 DrawAllGridLines( dc
, wxRegion() );
7801 m_gridWin
->Refresh();
7808 int wxGrid::GetDefaultRowSize()
7810 return m_defaultRowHeight
;
7813 int wxGrid::GetRowSize( int row
)
7815 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7817 return GetRowHeight(row
);
7820 int wxGrid::GetDefaultColSize()
7822 return m_defaultColWidth
;
7825 int wxGrid::GetColSize( int col
)
7827 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7829 return GetColWidth(col
);
7832 // ============================================================================
7833 // access to the grid attributes: each of them has a default value in the grid
7834 // itself and may be overidden on a per-cell basis
7835 // ============================================================================
7837 // ----------------------------------------------------------------------------
7838 // setting default attributes
7839 // ----------------------------------------------------------------------------
7841 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7843 m_defaultCellAttr
->SetBackgroundColour(col
);
7845 m_gridWin
->SetBackgroundColour(col
);
7849 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7851 m_defaultCellAttr
->SetTextColour(col
);
7854 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7856 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7859 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7861 m_defaultCellAttr
->SetFont(font
);
7864 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7866 m_defaultCellAttr
->SetRenderer(renderer
);
7869 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7871 m_defaultCellAttr
->SetEditor(editor
);
7874 // ----------------------------------------------------------------------------
7875 // access to the default attrbiutes
7876 // ----------------------------------------------------------------------------
7878 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7880 return m_defaultCellAttr
->GetBackgroundColour();
7883 wxColour
wxGrid::GetDefaultCellTextColour()
7885 return m_defaultCellAttr
->GetTextColour();
7888 wxFont
wxGrid::GetDefaultCellFont()
7890 return m_defaultCellAttr
->GetFont();
7893 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7895 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7898 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7900 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7903 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7905 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7908 // ----------------------------------------------------------------------------
7909 // access to cell attributes
7910 // ----------------------------------------------------------------------------
7912 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7914 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7915 wxColour colour
= attr
->GetBackgroundColour();
7920 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7922 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7923 wxColour colour
= attr
->GetTextColour();
7928 wxFont
wxGrid::GetCellFont( int row
, int col
)
7930 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7931 wxFont font
= attr
->GetFont();
7936 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7938 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7939 attr
->GetAlignment(horiz
, vert
);
7943 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7945 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7946 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7952 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7954 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7955 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7961 bool wxGrid::IsReadOnly(int row
, int col
) const
7963 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7964 bool isReadOnly
= attr
->IsReadOnly();
7969 // ----------------------------------------------------------------------------
7970 // attribute support: cache, automatic provider creation, ...
7971 // ----------------------------------------------------------------------------
7973 bool wxGrid::CanHaveAttributes()
7980 return m_table
->CanHaveAttributes();
7983 void wxGrid::ClearAttrCache()
7985 if ( m_attrCache
.row
!= -1 )
7987 wxSafeDecRef(m_attrCache
.attr
);
7988 m_attrCache
.attr
= NULL
;
7989 m_attrCache
.row
= -1;
7993 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7995 wxGrid
*self
= (wxGrid
*)this; // const_cast
7997 self
->ClearAttrCache();
7998 self
->m_attrCache
.row
= row
;
7999 self
->m_attrCache
.col
= col
;
8000 self
->m_attrCache
.attr
= attr
;
8004 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8006 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8008 *attr
= m_attrCache
.attr
;
8009 wxSafeIncRef(m_attrCache
.attr
);
8011 #ifdef DEBUG_ATTR_CACHE
8012 gs_nAttrCacheHits
++;
8019 #ifdef DEBUG_ATTR_CACHE
8020 gs_nAttrCacheMisses
++;
8026 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8028 wxGridCellAttr
*attr
;
8029 if ( !LookupAttr(row
, col
, &attr
) )
8031 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
8032 CacheAttr(row
, col
, attr
);
8036 attr
->SetDefAttr(m_defaultCellAttr
);
8040 attr
= m_defaultCellAttr
;
8047 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8049 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8050 wxASSERT_MSG( m_table
,
8051 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8053 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8056 attr
= new wxGridCellAttr
;
8058 // artificially inc the ref count to match DecRef() in caller
8060 m_table
->SetAttr(attr
, row
, col
);
8062 attr
->SetDefAttr(m_defaultCellAttr
);
8066 // ----------------------------------------------------------------------------
8067 // setting column attributes (wrappers around SetColAttr)
8068 // ----------------------------------------------------------------------------
8070 void wxGrid::SetColFormatBool(int col
)
8072 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8075 void wxGrid::SetColFormatNumber(int col
)
8077 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8080 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8082 wxString typeName
= wxGRID_VALUE_FLOAT
;
8083 if ( (width
!= -1) || (precision
!= -1) )
8085 typeName
<< _T(':') << width
<< _T(',') << precision
;
8088 SetColFormatCustom(col
, typeName
);
8091 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8093 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8095 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8097 attr
= new wxGridCellAttr
;
8098 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8099 attr
->SetRenderer(renderer
);
8101 SetColAttr(col
, attr
);
8105 // ----------------------------------------------------------------------------
8106 // setting cell attributes: this is forwarded to the table
8107 // ----------------------------------------------------------------------------
8109 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8111 if ( CanHaveAttributes() )
8113 m_table
->SetRowAttr(attr
, row
);
8122 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8124 if ( CanHaveAttributes() )
8126 m_table
->SetColAttr(attr
, col
);
8135 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8137 if ( CanHaveAttributes() )
8139 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8140 attr
->SetBackgroundColour(colour
);
8145 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8147 if ( CanHaveAttributes() )
8149 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8150 attr
->SetTextColour(colour
);
8155 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8157 if ( CanHaveAttributes() )
8159 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8160 attr
->SetFont(font
);
8165 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8167 if ( CanHaveAttributes() )
8169 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8170 attr
->SetAlignment(horiz
, vert
);
8175 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8177 if ( CanHaveAttributes() )
8179 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8180 attr
->SetRenderer(renderer
);
8185 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8187 if ( CanHaveAttributes() )
8189 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8190 attr
->SetEditor(editor
);
8195 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8197 if ( CanHaveAttributes() )
8199 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8200 attr
->SetReadOnly(isReadOnly
);
8205 // ----------------------------------------------------------------------------
8206 // Data type registration
8207 // ----------------------------------------------------------------------------
8209 void wxGrid::RegisterDataType(const wxString
& typeName
,
8210 wxGridCellRenderer
* renderer
,
8211 wxGridCellEditor
* editor
)
8213 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8217 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8219 wxString typeName
= m_table
->GetTypeName(row
, col
);
8220 return GetDefaultEditorForType(typeName
);
8223 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8225 wxString typeName
= m_table
->GetTypeName(row
, col
);
8226 return GetDefaultRendererForType(typeName
);
8230 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8232 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8233 if ( index
== wxNOT_FOUND
)
8235 wxFAIL_MSG(wxT("Unknown data type name"));
8240 return m_typeRegistry
->GetEditor(index
);
8244 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8246 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8247 if ( index
== wxNOT_FOUND
)
8249 wxFAIL_MSG(wxT("Unknown data type name"));
8254 return m_typeRegistry
->GetRenderer(index
);
8258 // ----------------------------------------------------------------------------
8260 // ----------------------------------------------------------------------------
8262 void wxGrid::EnableDragRowSize( bool enable
)
8264 m_canDragRowSize
= enable
;
8268 void wxGrid::EnableDragColSize( bool enable
)
8270 m_canDragColSize
= enable
;
8273 void wxGrid::EnableDragGridSize( bool enable
)
8275 m_canDragGridSize
= enable
;
8279 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8281 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8283 if ( resizeExistingRows
)
8286 if ( !GetBatchCount() )
8291 void wxGrid::SetRowSize( int row
, int height
)
8293 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8295 if ( m_rowHeights
.IsEmpty() )
8297 // need to really create the array
8301 int h
= wxMax( 0, height
);
8302 int diff
= h
- m_rowHeights
[row
];
8304 m_rowHeights
[row
] = h
;
8306 for ( i
= row
; i
< m_numRows
; i
++ )
8308 m_rowBottoms
[i
] += diff
;
8310 if ( !GetBatchCount() )
8314 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8316 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8318 if ( resizeExistingCols
)
8321 if ( !GetBatchCount() )
8326 void wxGrid::SetColSize( int col
, int width
)
8328 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8330 // should we check that it's bigger than GetColMinimalWidth(col) here?
8332 if ( m_colWidths
.IsEmpty() )
8334 // need to really create the array
8338 int w
= wxMax( 0, width
);
8339 int diff
= w
- m_colWidths
[col
];
8340 m_colWidths
[col
] = w
;
8343 for ( i
= col
; i
< m_numCols
; i
++ )
8345 m_colRights
[i
] += diff
;
8347 if ( !GetBatchCount() )
8352 void wxGrid::SetColMinimalWidth( int col
, int width
)
8354 m_colMinWidths
.Put(col
, width
);
8357 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8359 m_rowMinHeights
.Put(row
, width
);
8362 int wxGrid::GetColMinimalWidth(int col
) const
8364 long value
= m_colMinWidths
.Get(col
);
8365 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8368 int wxGrid::GetRowMinimalHeight(int row
) const
8370 long value
= m_rowMinHeights
.Get(row
);
8371 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8374 // ----------------------------------------------------------------------------
8376 // ----------------------------------------------------------------------------
8378 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8380 wxClientDC
dc(m_gridWin
);
8382 // init both of them to avoid compiler warnings, even if weo nly need one
8390 wxCoord extent
, extentMax
= 0;
8391 int max
= column
? m_numRows
: m_numCols
;
8392 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8399 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8400 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8403 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8404 extent
= column
? size
.x
: size
.y
;
8405 if ( extent
> extentMax
)
8416 // now also compare with the column label extent
8418 dc
.SetFont( GetLabelFont() );
8421 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8423 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8425 extent
= column
? w
: h
;
8426 if ( extent
> extentMax
)
8433 // empty column - give default extent (notice that if extentMax is less
8434 // than default extent but != 0, it's ok)
8435 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8441 // leave some space around text
8452 SetColSize(col
, extentMax
);
8453 if ( !GetBatchCount() )
8456 m_gridWin
->GetClientSize( &cw
, &ch
);
8457 wxRect
rect ( CellToRect( 0, col
) );
8459 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8460 rect
.width
= cw
- rect
.x
;
8461 rect
.height
= m_colLabelHeight
;
8462 m_colLabelWin
->Refresh( TRUE
, &rect
);
8467 SetRowSize(row
, extentMax
);
8468 if ( !GetBatchCount() )
8471 m_gridWin
->GetClientSize( &cw
, &ch
);
8472 wxRect
rect ( CellToRect( row
, 0 ) );
8474 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8475 rect
.width
= m_rowLabelWidth
;
8476 rect
.height
= ch
- rect
.y
;
8477 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8483 SetColMinimalWidth(col
, extentMax
);
8485 SetRowMinimalHeight(row
, extentMax
);
8489 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8491 int width
= m_rowLabelWidth
;
8495 for ( int col
= 0; col
< m_numCols
; col
++ )
8499 AutoSizeColumn(col
, setAsMin
);
8502 width
+= GetColWidth(col
);
8509 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8511 int height
= m_colLabelHeight
;
8515 for ( int row
= 0; row
< m_numRows
; row
++ )
8519 AutoSizeRow(row
, setAsMin
);
8522 height
+= GetRowHeight(row
);
8529 void wxGrid::AutoSize()
8532 SetClientSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8535 wxSize
wxGrid::DoGetBestSize() const
8537 // don't set sizes, only calculate them
8538 wxGrid
*self
= (wxGrid
*)this; // const_cast
8540 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8541 self
->SetOrCalcRowSizes(TRUE
));
8550 wxPen
& wxGrid::GetDividerPen() const
8555 // ----------------------------------------------------------------------------
8556 // cell value accessor functions
8557 // ----------------------------------------------------------------------------
8559 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8563 m_table
->SetValue( row
, col
, s
);
8564 if ( !GetBatchCount() )
8566 wxClientDC
dc( m_gridWin
);
8568 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8571 if ( m_currentCellCoords
.GetRow() == row
&&
8572 m_currentCellCoords
.GetCol() == col
&&
8573 IsCellEditControlShown())
8574 // Note: If we are using IsCellEditControlEnabled,
8575 // this interacts badly with calling SetCellValue from
8576 // an EVT_GRID_CELL_CHANGE handler.
8578 HideCellEditControl();
8579 ShowCellEditControl(); // will reread data from table
8586 // ------ Block, row and col selection
8589 void wxGrid::SelectRow( int row
, bool addToSelected
)
8591 if ( IsSelection() && !addToSelected
)
8594 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8598 void wxGrid::SelectCol( int col
, bool addToSelected
)
8600 if ( IsSelection() && !addToSelected
)
8603 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8607 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8608 bool addToSelected
)
8610 if ( IsSelection() && !addToSelected
)
8613 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8614 FALSE
, addToSelected
);
8618 void wxGrid::SelectAll()
8620 if ( m_numRows
> 0 && m_numCols
> 0 )
8621 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8625 // ------ Cell, row and col deselection
8628 void wxGrid::DeselectRow( int row
)
8630 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8632 if ( m_selection
->IsInSelection(row
, 0 ) )
8633 m_selection
->ToggleCellSelection( row
, 0);
8637 int nCols
= GetNumberCols();
8638 for ( int i
= 0; i
< nCols
; i
++ )
8640 if ( m_selection
->IsInSelection(row
, i
) )
8641 m_selection
->ToggleCellSelection( row
, i
);
8646 void wxGrid::DeselectCol( int col
)
8648 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8650 if ( m_selection
->IsInSelection(0, col
) )
8651 m_selection
->ToggleCellSelection( 0, col
);
8655 int nRows
= GetNumberRows();
8656 for ( int i
= 0; i
< nRows
; i
++ )
8658 if ( m_selection
->IsInSelection(i
, col
) )
8659 m_selection
->ToggleCellSelection(i
, col
);
8664 void wxGrid::DeselectCell( int row
, int col
)
8666 if ( m_selection
->IsInSelection(row
, col
) )
8667 m_selection
->ToggleCellSelection(row
, col
);
8670 bool wxGrid::IsSelection()
8672 return ( m_selection
->IsSelection() ||
8673 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8674 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8677 bool wxGrid::IsInSelection( int row
, int col
)
8679 return ( m_selection
->IsInSelection( row
, col
) ||
8680 ( row
>= m_selectingTopLeft
.GetRow() &&
8681 col
>= m_selectingTopLeft
.GetCol() &&
8682 row
<= m_selectingBottomRight
.GetRow() &&
8683 col
<= m_selectingBottomRight
.GetCol() ) );
8686 void wxGrid::ClearSelection()
8688 m_selectingTopLeft
= wxGridNoCellCoords
;
8689 m_selectingBottomRight
= wxGridNoCellCoords
;
8690 m_selection
->ClearSelection();
8694 // This function returns the rectangle that encloses the given block
8695 // in device coords clipped to the client size of the grid window.
8697 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8698 const wxGridCellCoords
&bottomRight
)
8700 wxRect
rect( wxGridNoCellRect
);
8703 cellRect
= CellToRect( topLeft
);
8704 if ( cellRect
!= wxGridNoCellRect
)
8710 rect
= wxRect( 0, 0, 0, 0 );
8713 cellRect
= CellToRect( bottomRight
);
8714 if ( cellRect
!= wxGridNoCellRect
)
8720 return wxGridNoCellRect
;
8723 // convert to scrolled coords
8725 int left
, top
, right
, bottom
;
8726 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8727 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8730 m_gridWin
->GetClientSize( &cw
, &ch
);
8732 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8733 return wxRect( 0, 0, 0, 0);
8735 rect
.SetLeft( wxMax(0, left
) );
8736 rect
.SetTop( wxMax(0, top
) );
8737 rect
.SetRight( wxMin(cw
, right
) );
8738 rect
.SetBottom( wxMin(ch
, bottom
) );
8746 // ------ Grid event classes
8749 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8751 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8752 int row
, int col
, int x
, int y
, bool sel
,
8753 bool control
, bool shift
, bool alt
, bool meta
)
8754 : wxNotifyEvent( type
, id
)
8761 m_control
= control
;
8766 SetEventObject(obj
);
8770 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8772 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8773 int rowOrCol
, int x
, int y
,
8774 bool control
, bool shift
, bool alt
, bool meta
)
8775 : wxNotifyEvent( type
, id
)
8777 m_rowOrCol
= rowOrCol
;
8780 m_control
= control
;
8785 SetEventObject(obj
);
8789 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8791 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8792 const wxGridCellCoords
& topLeft
,
8793 const wxGridCellCoords
& bottomRight
,
8794 bool sel
, bool control
,
8795 bool shift
, bool alt
, bool meta
)
8796 : wxNotifyEvent( type
, id
)
8798 m_topLeft
= topLeft
;
8799 m_bottomRight
= bottomRight
;
8801 m_control
= control
;
8806 SetEventObject(obj
);
8810 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8812 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8813 wxObject
* obj
, int row
,
8814 int col
, wxControl
* ctrl
)
8815 : wxCommandEvent(type
, id
)
8817 SetEventObject(obj
);
8824 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
8826 #endif // wxUSE_GRID