1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
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 wxWindow
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!!!
392 // Well, it's still seriously broken so it might be better but needs
395 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
396 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
398 // the size of hash tables used a bit everywhere (the max number of elements
399 // in these hash tables is the number of rows/columns)
400 static const int GRID_HASH_SIZE
= 100;
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 static inline int GetScrollX(int x
)
408 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
411 static inline int GetScrollY(int y
)
413 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
416 // ============================================================================
418 // ============================================================================
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxGridCellEditor::wxGridCellEditor()
430 wxGridCellEditor::~wxGridCellEditor()
435 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
436 wxWindowID
WXUNUSED(id
),
437 wxEvtHandler
* evtHandler
)
440 m_control
->PushEventHandler(evtHandler
);
443 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
444 wxGridCellAttr
*attr
)
446 // erase the background because we might not fill the cell
447 wxClientDC
dc(m_control
->GetParent());
448 dc
.SetPen(*wxTRANSPARENT_PEN
);
449 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
450 dc
.DrawRectangle(rectCell
);
452 // redraw the control we just painted over
453 m_control
->Refresh();
456 void wxGridCellEditor::Destroy()
460 m_control
->PopEventHandler(TRUE
/* delete it*/);
462 m_control
->Destroy();
467 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
469 wxASSERT_MSG(m_control
,
470 wxT("The wxGridCellEditor must be Created first!"));
471 m_control
->Show(show
);
475 // set the colours/fonts if we have any
478 m_colFgOld
= m_control
->GetForegroundColour();
479 m_control
->SetForegroundColour(attr
->GetTextColour());
481 m_colBgOld
= m_control
->GetBackgroundColour();
482 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
484 m_fontOld
= m_control
->GetFont();
485 m_control
->SetFont(attr
->GetFont());
487 // can't do anything more in the base class version, the other
488 // attributes may only be used by the derived classes
493 // restore the standard colours fonts
494 if ( m_colFgOld
.Ok() )
496 m_control
->SetForegroundColour(m_colFgOld
);
497 m_colFgOld
= wxNullColour
;
500 if ( m_colBgOld
.Ok() )
502 m_control
->SetBackgroundColour(m_colBgOld
);
503 m_colBgOld
= wxNullColour
;
506 if ( m_fontOld
.Ok() )
508 m_control
->SetFont(m_fontOld
);
509 m_fontOld
= wxNullFont
;
514 void wxGridCellEditor::SetSize(const wxRect
& rect
)
516 wxASSERT_MSG(m_control
,
517 wxT("The wxGridCellEditor must be Created first!"));
518 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
521 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
526 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
528 // accept the simple key presses, not anything with Ctrl/Alt/Meta
529 return !(event
.ControlDown() || event
.AltDown());
532 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
537 void wxGridCellEditor::StartingClick()
543 // ----------------------------------------------------------------------------
544 // wxGridCellTextEditor
545 // ----------------------------------------------------------------------------
547 wxGridCellTextEditor::wxGridCellTextEditor()
552 void wxGridCellTextEditor::Create(wxWindow
* parent
,
554 wxEvtHandler
* evtHandler
)
556 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
557 wxDefaultPosition
, wxDefaultSize
558 #if defined(__WXMSW__)
559 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
560 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
564 // TODO: use m_maxChars
566 wxGridCellEditor::Create(parent
, id
, evtHandler
);
569 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
570 wxGridCellAttr
* WXUNUSED(attr
))
572 // as we fill the entire client area, don't do anything here to minimize
576 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
578 wxRect
rect(rectOrig
);
580 // Make the edit control large enough to allow for internal
583 // TODO: remove this if the text ctrl sizing is improved esp. for
586 #if defined(__WXGTK__)
595 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
597 // MB: treat MSW separately here otherwise the caret doesn't show
598 // when the editor is in the first row.
599 #if defined(__WXMSW__)
602 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
605 #if defined(__WXMOTIF__)
609 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
610 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
611 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
612 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
615 wxGridCellEditor::SetSize(rect
);
618 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
620 wxASSERT_MSG(m_control
,
621 wxT("The wxGridCellEditor must be Created first!"));
623 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
625 DoBeginEdit(m_startValue
);
628 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
630 Text()->SetValue(startValue
);
631 Text()->SetInsertionPointEnd();
635 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
638 wxASSERT_MSG(m_control
,
639 wxT("The wxGridCellEditor must be Created first!"));
641 bool changed
= FALSE
;
642 wxString value
= Text()->GetValue();
643 if (value
!= m_startValue
)
647 grid
->GetTable()->SetValue(row
, col
, value
);
649 m_startValue
= wxEmptyString
;
650 Text()->SetValue(m_startValue
);
656 void wxGridCellTextEditor::Reset()
658 wxASSERT_MSG(m_control
,
659 wxT("The wxGridCellEditor must be Created first!"));
661 DoReset(m_startValue
);
664 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
666 Text()->SetValue(startValue
);
667 Text()->SetInsertionPointEnd();
670 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
672 if ( wxGridCellEditor::IsAcceptedKey(event
) )
674 int keycode
= event
.GetKeyCode();
688 case WXK_NUMPAD_MULTIPLY
:
692 case WXK_NUMPAD_SUBTRACT
:
694 case WXK_NUMPAD_DECIMAL
:
696 case WXK_NUMPAD_DIVIDE
:
700 // accept 8 bit chars too if isprint() agrees
701 if ( (keycode
< 255) && (isprint(keycode
)) )
709 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
711 if ( !Text()->EmulateKeyPress(event
) )
717 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
718 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
720 #if defined(__WXMOTIF__) || defined(__WXGTK__)
721 // wxMotif needs a little extra help...
722 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
723 wxString
s( Text()->GetValue() );
724 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
726 Text()->SetInsertionPoint( pos
);
728 // the other ports can handle a Return key press
734 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
744 if ( !params
.ToLong(&tmp
) )
746 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
750 m_maxChars
= (size_t)tmp
;
755 // ----------------------------------------------------------------------------
756 // wxGridCellNumberEditor
757 // ----------------------------------------------------------------------------
759 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
765 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
767 wxEvtHandler
* evtHandler
)
771 // create a spin ctrl
772 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
773 wxDefaultPosition
, wxDefaultSize
,
777 wxGridCellEditor::Create(parent
, id
, evtHandler
);
781 // just a text control
782 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
785 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
786 #endif // wxUSE_VALIDATORS
790 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
792 // first get the value
793 wxGridTableBase
*table
= grid
->GetTable();
794 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
796 m_valueOld
= table
->GetValueAsLong(row
, col
);
801 wxString sValue
= table
->GetValue(row
, col
);
802 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
804 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
811 Spin()->SetValue((int)m_valueOld
);
816 DoBeginEdit(GetString());
820 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
829 value
= Spin()->GetValue();
830 changed
= value
!= m_valueOld
;
832 text
= wxString::Format(wxT("%ld"), value
);
836 text
= Text()->GetValue();
837 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
842 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
843 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
845 grid
->GetTable()->SetValue(row
, col
, text
);
851 void wxGridCellNumberEditor::Reset()
855 Spin()->SetValue((int)m_valueOld
);
859 DoReset(GetString());
863 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
865 if ( wxGridCellEditor::IsAcceptedKey(event
) )
867 int keycode
= event
.GetKeyCode();
883 case WXK_NUMPAD_SUBTRACT
:
889 if ( (keycode
< 128) && isdigit(keycode
) )
897 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
901 int keycode
= (int) event
.KeyCode();
902 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
904 wxGridCellTextEditor::StartingKey(event
);
914 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
925 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
929 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
933 // skip the error message below
938 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
942 // ----------------------------------------------------------------------------
943 // wxGridCellFloatEditor
944 // ----------------------------------------------------------------------------
946 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
949 m_precision
= precision
;
952 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
954 wxEvtHandler
* evtHandler
)
956 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
959 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
960 #endif // wxUSE_VALIDATORS
963 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
965 // first get the value
966 wxGridTableBase
*table
= grid
->GetTable();
967 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
969 m_valueOld
= table
->GetValueAsDouble(row
, col
);
974 wxString sValue
= table
->GetValue(row
, col
);
975 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
977 wxFAIL_MSG( _T("this cell doesn't have float value") );
982 DoBeginEdit(GetString());
985 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
989 wxString
text(Text()->GetValue());
991 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
993 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
994 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
996 grid
->GetTable()->SetValue(row
, col
, text
);
1006 void wxGridCellFloatEditor::Reset()
1008 DoReset(GetString());
1011 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1013 int keycode
= (int)event
.KeyCode();
1014 if ( isdigit(keycode
) ||
1015 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1017 wxGridCellTextEditor::StartingKey(event
);
1019 // skip Skip() below
1026 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1037 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1041 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1043 m_precision
= (int)tmp
;
1045 // skip the error message below
1050 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1054 wxString
wxGridCellFloatEditor::GetString() const
1057 if ( m_width
== -1 )
1059 // default width/precision
1062 else if ( m_precision
== -1 )
1064 // default precision
1065 fmt
.Printf(_T("%%%d.f"), m_width
);
1069 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1072 return wxString::Format(fmt
, m_valueOld
);
1075 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1077 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1079 int keycode
= event
.GetKeyCode();
1093 case WXK_NUMPAD_ADD
:
1095 case WXK_NUMPAD_SUBTRACT
:
1097 case WXK_NUMPAD_DECIMAL
:
1101 // additionally accept 'e' as in '1e+6'
1102 if ( (keycode
< 128) &&
1103 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1111 #endif // wxUSE_TEXTCTRL
1115 // ----------------------------------------------------------------------------
1116 // wxGridCellBoolEditor
1117 // ----------------------------------------------------------------------------
1119 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1121 wxEvtHandler
* evtHandler
)
1123 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1124 wxDefaultPosition
, wxDefaultSize
,
1127 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1130 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1132 bool resize
= FALSE
;
1133 wxSize size
= m_control
->GetSize();
1134 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1136 // check if the checkbox is not too big/small for this cell
1137 wxSize sizeBest
= m_control
->GetBestSize();
1138 if ( !(size
== sizeBest
) )
1140 // reset to default size if it had been made smaller
1146 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1148 // leave 1 pixel margin
1149 size
.x
= size
.y
= minSize
- 2;
1156 m_control
->SetSize(size
);
1159 // position it in the centre of the rectangle (TODO: support alignment?)
1161 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1162 // the checkbox without label still has some space to the right in wxGTK,
1163 // so shift it to the right
1165 #elif defined(__WXMSW__)
1166 // here too, but in other way
1171 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1174 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1176 m_control
->Show(show
);
1180 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1181 CBox()->SetBackgroundColour(colBg
);
1185 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1187 wxASSERT_MSG(m_control
,
1188 wxT("The wxGridCellEditor must be Created first!"));
1190 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1191 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1194 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1195 m_startValue
= !( !cellval
|| (cellval
== "0") );
1197 CBox()->SetValue(m_startValue
);
1201 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1204 wxASSERT_MSG(m_control
,
1205 wxT("The wxGridCellEditor must be Created first!"));
1207 bool changed
= FALSE
;
1208 bool value
= CBox()->GetValue();
1209 if ( value
!= m_startValue
)
1214 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1215 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1217 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1223 void wxGridCellBoolEditor::Reset()
1225 wxASSERT_MSG(m_control
,
1226 wxT("The wxGridCellEditor must be Created first!"));
1228 CBox()->SetValue(m_startValue
);
1231 void wxGridCellBoolEditor::StartingClick()
1233 CBox()->SetValue(!CBox()->GetValue());
1236 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1238 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1240 int keycode
= event
.GetKeyCode();
1244 case WXK_NUMPAD_MULTIPLY
:
1246 case WXK_NUMPAD_ADD
:
1248 case WXK_NUMPAD_SUBTRACT
:
1259 #endif // wxUSE_CHECKBOX
1263 // ----------------------------------------------------------------------------
1264 // wxGridCellChoiceEditor
1265 // ----------------------------------------------------------------------------
1267 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1268 const wxString choices
[],
1270 : m_allowOthers(allowOthers
)
1274 m_choices
.Alloc(count
);
1275 for ( size_t n
= 0; n
< count
; n
++ )
1277 m_choices
.Add(choices
[n
]);
1282 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1284 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1285 editor
->m_allowOthers
= m_allowOthers
;
1286 editor
->m_choices
= m_choices
;
1291 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1293 wxEvtHandler
* evtHandler
)
1295 size_t count
= m_choices
.GetCount();
1296 wxString
*choices
= new wxString
[count
];
1297 for ( size_t n
= 0; n
< count
; n
++ )
1299 choices
[n
] = m_choices
[n
];
1302 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1303 wxDefaultPosition
, wxDefaultSize
,
1305 m_allowOthers
? 0 : wxCB_READONLY
);
1309 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1312 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1313 wxGridCellAttr
* attr
)
1315 // as we fill the entire client area, don't do anything here to minimize
1318 // TODO: It doesn't actually fill the client area since the height of a
1319 // combo always defaults to the standard... Until someone has time to
1320 // figure out the right rectangle to paint, just do it the normal way...
1321 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1324 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1326 wxASSERT_MSG(m_control
,
1327 wxT("The wxGridCellEditor must be Created first!"));
1329 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1331 Combo()->SetValue(m_startValue
);
1332 size_t count
= m_choices
.GetCount();
1333 for (size_t i
=0; i
<count
; i
++)
1335 if (m_startValue
== m_choices
[i
])
1337 Combo()->SetSelection(i
);
1341 Combo()->SetInsertionPointEnd();
1342 Combo()->SetFocus();
1345 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1348 wxString value
= Combo()->GetValue();
1349 bool changed
= value
!= m_startValue
;
1352 grid
->GetTable()->SetValue(row
, col
, value
);
1354 m_startValue
= wxEmptyString
;
1355 Combo()->SetValue(m_startValue
);
1360 void wxGridCellChoiceEditor::Reset()
1362 Combo()->SetValue(m_startValue
);
1363 Combo()->SetInsertionPointEnd();
1366 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1376 wxStringTokenizer
tk(params
, _T(','));
1377 while ( tk
.HasMoreTokens() )
1379 m_choices
.Add(tk
.GetNextToken());
1383 #endif // wxUSE_COMBOBOX
1385 // ----------------------------------------------------------------------------
1386 // wxGridCellEditorEvtHandler
1387 // ----------------------------------------------------------------------------
1389 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1391 switch ( event
.KeyCode() )
1395 m_grid
->DisableCellEditControl();
1399 m_grid
->GetEventHandler()->ProcessEvent( event
);
1403 case WXK_NUMPAD_ENTER
:
1404 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1405 m_editor
->HandleReturn(event
);
1414 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1416 switch ( event
.KeyCode() )
1421 case WXK_NUMPAD_ENTER
:
1429 // ----------------------------------------------------------------------------
1430 // wxGridCellWorker is an (almost) empty common base class for
1431 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1432 // ----------------------------------------------------------------------------
1434 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1439 wxGridCellWorker::~wxGridCellWorker()
1443 // ============================================================================
1445 // ============================================================================
1447 // ----------------------------------------------------------------------------
1448 // wxGridCellRenderer
1449 // ----------------------------------------------------------------------------
1451 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1452 wxGridCellAttr
& attr
,
1455 int WXUNUSED(row
), int WXUNUSED(col
),
1458 dc
.SetBackgroundMode( wxSOLID
);
1462 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1466 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1469 dc
.SetPen( *wxTRANSPARENT_PEN
);
1470 dc
.DrawRectangle(rect
);
1473 // ----------------------------------------------------------------------------
1474 // wxGridCellStringRenderer
1475 // ----------------------------------------------------------------------------
1477 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1478 wxGridCellAttr
& attr
,
1482 dc
.SetBackgroundMode( wxTRANSPARENT
);
1484 // TODO some special colours for attr.IsReadOnly() case?
1488 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1489 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1493 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1494 dc
.SetTextForeground( attr
.GetTextColour() );
1497 dc
.SetFont( attr
.GetFont() );
1500 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1502 const wxString
& text
)
1504 wxCoord x
= 0, y
= 0, max_x
= 0;
1505 dc
.SetFont(attr
.GetFont());
1506 wxStringTokenizer
tk(text
, _T('\n'));
1507 while ( tk
.HasMoreTokens() )
1509 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1510 max_x
= wxMax(max_x
, x
);
1513 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1515 return wxSize(max_x
, y
);
1518 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1519 wxGridCellAttr
& attr
,
1523 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1526 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1527 wxGridCellAttr
& attr
,
1529 const wxRect
& rectCell
,
1533 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1535 // now we only have to draw the text
1536 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1539 attr
.GetAlignment(&hAlign
, &vAlign
);
1541 wxRect rect
= rectCell
;
1544 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1545 rect
, hAlign
, vAlign
);
1548 // ----------------------------------------------------------------------------
1549 // wxGridCellNumberRenderer
1550 // ----------------------------------------------------------------------------
1552 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1554 wxGridTableBase
*table
= grid
.GetTable();
1556 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1558 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1562 text
= table
->GetValue(row
, col
);
1568 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1569 wxGridCellAttr
& attr
,
1571 const wxRect
& rectCell
,
1575 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1577 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1579 // draw the text right aligned by default
1581 attr
.GetAlignment(&hAlign
, &vAlign
);
1582 hAlign
= wxALIGN_RIGHT
;
1584 wxRect rect
= rectCell
;
1587 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1590 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1591 wxGridCellAttr
& attr
,
1595 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1598 // ----------------------------------------------------------------------------
1599 // wxGridCellFloatRenderer
1600 // ----------------------------------------------------------------------------
1602 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1605 SetPrecision(precision
);
1608 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1610 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1611 renderer
->m_width
= m_width
;
1612 renderer
->m_precision
= m_precision
;
1613 renderer
->m_format
= m_format
;
1618 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1620 wxGridTableBase
*table
= grid
.GetTable();
1625 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1627 val
= table
->GetValueAsDouble(row
, col
);
1632 text
= table
->GetValue(row
, col
);
1633 hasDouble
= text
.ToDouble(&val
);
1640 if ( m_width
== -1 )
1642 if ( m_precision
== -1 )
1644 // default width/precision
1645 m_format
= _T("%f");
1649 m_format
.Printf(_T("%%.%df"), m_precision
);
1652 else if ( m_precision
== -1 )
1654 // default precision
1655 m_format
.Printf(_T("%%%d.f"), m_width
);
1659 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1663 text
.Printf(m_format
, val
);
1666 //else: text already contains the string
1671 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1672 wxGridCellAttr
& attr
,
1674 const wxRect
& rectCell
,
1678 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1680 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1682 // draw the text right aligned by default
1684 attr
.GetAlignment(&hAlign
, &vAlign
);
1685 hAlign
= wxALIGN_RIGHT
;
1687 wxRect rect
= rectCell
;
1690 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1693 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1694 wxGridCellAttr
& attr
,
1698 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1701 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1705 // reset to defaults
1711 wxString tmp
= params
.BeforeFirst(_T(','));
1715 if ( tmp
.ToLong(&width
) )
1717 SetWidth((int)width
);
1721 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1725 tmp
= params
.AfterFirst(_T(','));
1729 if ( tmp
.ToLong(&precision
) )
1731 SetPrecision((int)precision
);
1735 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1743 // ----------------------------------------------------------------------------
1744 // wxGridCellBoolRenderer
1745 // ----------------------------------------------------------------------------
1747 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1749 // FIXME these checkbox size calculations are really ugly...
1751 // between checkmark and box
1752 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1754 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1755 wxGridCellAttr
& WXUNUSED(attr
),
1760 // compute it only once (no locks for MT safeness in GUI thread...)
1761 if ( !ms_sizeCheckMark
.x
)
1763 // get checkbox size
1764 wxCoord checkSize
= 0;
1765 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1766 wxSize size
= checkbox
->GetBestSize();
1767 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1769 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1770 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1771 checkSize
-= size
.y
/ 2;
1776 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1779 return ms_sizeCheckMark
;
1782 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1783 wxGridCellAttr
& attr
,
1789 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1791 // draw a check mark in the centre (ignoring alignment - TODO)
1792 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1794 // don't draw outside the cell
1795 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1796 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1798 // and even leave (at least) 1 pixel margin
1799 size
.x
= size
.y
= minSize
- 2;
1802 // draw a border around checkmark
1804 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1805 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1806 rectBorder
.width
= size
.x
;
1807 rectBorder
.height
= size
.y
;
1810 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1811 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1814 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1815 value
= !( !cellval
|| (cellval
== "0") );
1820 wxRect rectMark
= rectBorder
;
1822 // MSW DrawCheckMark() is weird (and should probably be changed...)
1823 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1827 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1830 dc
.SetTextForeground(attr
.GetTextColour());
1831 dc
.DrawCheckMark(rectMark
);
1834 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1835 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1836 dc
.DrawRectangle(rectBorder
);
1839 // ----------------------------------------------------------------------------
1841 // ----------------------------------------------------------------------------
1843 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1847 m_isReadOnly
= Unset
;
1852 m_attrkind
= wxGridCellAttr::Cell
;
1854 SetDefAttr(attrDefault
);
1857 wxGridCellAttr
*wxGridCellAttr::Clone() const
1859 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
1861 if ( HasTextColour() )
1862 attr
->SetTextColour(GetTextColour());
1863 if ( HasBackgroundColour() )
1864 attr
->SetBackgroundColour(GetBackgroundColour());
1866 attr
->SetFont(GetFont());
1867 if ( HasAlignment() )
1868 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1872 attr
->SetRenderer(m_renderer
);
1873 m_renderer
->IncRef();
1877 attr
->SetEditor(m_editor
);
1882 attr
->SetReadOnly();
1884 attr
->SetKind( m_attrkind
);
1889 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1891 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1892 SetTextColour(mergefrom
->GetTextColour());
1893 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1894 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1895 if ( !HasFont() && mergefrom
->HasFont() )
1896 SetFont(mergefrom
->GetFont());
1897 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1899 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1900 SetAlignment(hAlign
, vAlign
);
1903 // Directly access member functions as GetRender/Editor don't just return
1904 // m_renderer/m_editor
1906 // Maybe add support for merge of Render and Editor?
1907 if (!HasRenderer() && mergefrom
->HasRenderer() )
1909 m_renderer
= mergefrom
->m_renderer
;
1910 m_renderer
->IncRef();
1912 if ( !HasEditor() && mergefrom
->HasEditor() )
1914 m_editor
= mergefrom
->m_editor
;
1917 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1918 SetReadOnly(mergefrom
->IsReadOnly());
1920 SetDefAttr(mergefrom
->m_defGridAttr
);
1923 const wxColour
& wxGridCellAttr::GetTextColour() const
1925 if (HasTextColour())
1929 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1931 return m_defGridAttr
->GetTextColour();
1935 wxFAIL_MSG(wxT("Missing default cell attribute"));
1936 return wxNullColour
;
1941 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1943 if (HasBackgroundColour())
1945 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1946 return m_defGridAttr
->GetBackgroundColour();
1949 wxFAIL_MSG(wxT("Missing default cell attribute"));
1950 return wxNullColour
;
1955 const wxFont
& wxGridCellAttr::GetFont() const
1959 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1960 return m_defGridAttr
->GetFont();
1963 wxFAIL_MSG(wxT("Missing default cell attribute"));
1969 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1973 if ( hAlign
) *hAlign
= m_hAlign
;
1974 if ( vAlign
) *vAlign
= m_vAlign
;
1976 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1977 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1980 wxFAIL_MSG(wxT("Missing default cell attribute"));
1985 // GetRenderer and GetEditor use a slightly different decision path about
1986 // which attribute to use. If a non-default attr object has one then it is
1987 // used, otherwise the default editor or renderer is fetched from the grid and
1988 // used. It should be the default for the data type of the cell. If it is
1989 // NULL (because the table has a type that the grid does not have in its
1990 // registry,) then the grid's default editor or renderer is used.
1992 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1994 wxGridCellRenderer
*renderer
;
1996 if ( m_renderer
&& this != m_defGridAttr
)
1998 // use the cells renderer if it has one
1999 renderer
= m_renderer
;
2002 else // no non default cell renderer
2004 // get default renderer for the data type
2007 // GetDefaultRendererForCell() will do IncRef() for us
2008 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2017 if (m_defGridAttr
&& this != m_defGridAttr
)
2019 // if we still don't have one then use the grid default
2020 // (no need for IncRef() here neither)
2021 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2023 else // default grid attr
2025 // use m_renderer which we had decided not to use initially
2026 renderer
= m_renderer
;
2033 // we're supposed to always find something
2034 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2039 // same as above, except for s/renderer/editor/g
2040 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2042 wxGridCellEditor
*editor
;
2044 if ( m_editor
&& this != m_defGridAttr
)
2046 // use the cells editor if it has one
2050 else // no non default cell editor
2052 // get default editor for the data type
2055 // GetDefaultEditorForCell() will do IncRef() for us
2056 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2065 if ( m_defGridAttr
&& this != m_defGridAttr
)
2067 // if we still don't have one then use the grid default
2068 // (no need for IncRef() here neither)
2069 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2071 else // default grid attr
2073 // use m_editor which we had decided not to use initially
2081 // we're supposed to always find something
2082 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2087 // ----------------------------------------------------------------------------
2088 // wxGridCellAttrData
2089 // ----------------------------------------------------------------------------
2091 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2093 int n
= FindIndex(row
, col
);
2094 if ( n
== wxNOT_FOUND
)
2096 // add the attribute
2097 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2101 // free the old attribute
2102 m_attrs
[(size_t)n
].attr
->DecRef();
2106 // change the attribute
2107 m_attrs
[(size_t)n
].attr
= attr
;
2111 // remove this attribute
2112 m_attrs
.RemoveAt((size_t)n
);
2117 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2119 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2121 int n
= FindIndex(row
, col
);
2122 if ( n
!= wxNOT_FOUND
)
2124 attr
= m_attrs
[(size_t)n
].attr
;
2131 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2133 size_t count
= m_attrs
.GetCount();
2134 for ( size_t n
= 0; n
< count
; n
++ )
2136 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2137 wxCoord row
= coords
.GetRow();
2138 if ((size_t)row
>= pos
)
2142 // If rows inserted, include row counter where necessary
2143 coords
.SetRow(row
+ numRows
);
2145 else if (numRows
< 0)
2147 // If rows deleted ...
2148 if ((size_t)row
>= pos
- numRows
)
2150 // ...either decrement row counter (if row still exists)...
2151 coords
.SetRow(row
+ numRows
);
2155 // ...or remove the attribute
2156 m_attrs
.RemoveAt((size_t)n
);
2164 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2166 size_t count
= m_attrs
.GetCount();
2167 for ( size_t n
= 0; n
< count
; n
++ )
2169 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2170 wxCoord col
= coords
.GetCol();
2171 if ( (size_t)col
>= pos
)
2175 // If rows inserted, include row counter where necessary
2176 coords
.SetCol(col
+ numCols
);
2178 else if (numCols
< 0)
2180 // If rows deleted ...
2181 if ((size_t)col
>= pos
- numCols
)
2183 // ...either decrement row counter (if row still exists)...
2184 coords
.SetCol(col
+ numCols
);
2188 // ...or remove the attribute
2189 m_attrs
.RemoveAt((size_t)n
);
2197 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2199 size_t count
= m_attrs
.GetCount();
2200 for ( size_t n
= 0; n
< count
; n
++ )
2202 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2203 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2212 // ----------------------------------------------------------------------------
2213 // wxGridRowOrColAttrData
2214 // ----------------------------------------------------------------------------
2216 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2218 size_t count
= m_attrs
.Count();
2219 for ( size_t n
= 0; n
< count
; n
++ )
2221 m_attrs
[n
]->DecRef();
2225 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2227 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2229 int n
= m_rowsOrCols
.Index(rowOrCol
);
2230 if ( n
!= wxNOT_FOUND
)
2232 attr
= m_attrs
[(size_t)n
];
2239 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2241 int i
= m_rowsOrCols
.Index(rowOrCol
);
2242 if ( i
== wxNOT_FOUND
)
2244 // add the attribute
2245 m_rowsOrCols
.Add(rowOrCol
);
2250 size_t n
= (size_t)i
;
2253 // change the attribute
2254 m_attrs
[n
]->DecRef();
2259 // remove this attribute
2260 m_attrs
[n
]->DecRef();
2261 m_rowsOrCols
.RemoveAt(n
);
2262 m_attrs
.RemoveAt(n
);
2267 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2269 size_t count
= m_attrs
.GetCount();
2270 for ( size_t n
= 0; n
< count
; n
++ )
2272 int & rowOrCol
= m_rowsOrCols
[n
];
2273 if ( (size_t)rowOrCol
>= pos
)
2275 if ( numRowsOrCols
> 0 )
2277 // If rows inserted, include row counter where necessary
2278 rowOrCol
+= numRowsOrCols
;
2280 else if ( numRowsOrCols
< 0)
2282 // If rows deleted, either decrement row counter (if row still exists)
2283 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2284 rowOrCol
+= numRowsOrCols
;
2287 m_rowsOrCols
.RemoveAt((size_t)n
);
2288 m_attrs
.RemoveAt((size_t)n
);
2296 // ----------------------------------------------------------------------------
2297 // wxGridCellAttrProvider
2298 // ----------------------------------------------------------------------------
2300 wxGridCellAttrProvider::wxGridCellAttrProvider()
2302 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2305 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2310 void wxGridCellAttrProvider::InitData()
2312 m_data
= new wxGridCellAttrProviderData
;
2315 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2316 wxGridCellAttr::wxAttrKind kind
) const
2318 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2323 case (wxGridCellAttr::Any
):
2324 //Get cached merge attributes.
2325 // Currenlty not used as no cache implemented as not mutiable
2326 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2329 //Basicaly implement old version.
2330 //Also check merge cache, so we don't have to re-merge every time..
2331 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2332 *attrrow
= (wxGridCellAttr
*)NULL
,
2333 *attrcol
= (wxGridCellAttr
*)NULL
;
2335 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2336 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2337 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2339 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2340 // Two or move are non NULL
2341 attr
= new wxGridCellAttr
;
2342 attr
->SetKind(wxGridCellAttr::Merged
);
2346 attr
->MergeWith(attrcell
);
2350 attr
->MergeWith(attrcol
);
2354 attr
->MergeWith(attrrow
);
2357 //store merge attr if cache implemented
2359 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2363 // one or none is non null return it or null.
2364 if(attrrow
) attr
= attrrow
;
2365 if(attrcol
) attr
= attrcol
;
2366 if(attrcell
) attr
= attrcell
;
2370 case (wxGridCellAttr::Cell
):
2371 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2373 case (wxGridCellAttr::Col
):
2374 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2376 case (wxGridCellAttr::Row
):
2377 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2381 // (wxGridCellAttr::Default):
2382 // (wxGridCellAttr::Merged):
2389 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2395 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2398 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2403 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2406 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2411 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2414 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2418 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2420 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2424 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2428 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2430 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2434 // ----------------------------------------------------------------------------
2435 // wxGridTypeRegistry
2436 // ----------------------------------------------------------------------------
2438 wxGridTypeRegistry::~wxGridTypeRegistry()
2440 size_t count
= m_typeinfo
.Count();
2441 for ( size_t i
= 0; i
< count
; i
++ )
2442 delete m_typeinfo
[i
];
2446 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2447 wxGridCellRenderer
* renderer
,
2448 wxGridCellEditor
* editor
)
2450 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2452 // is it already registered?
2453 int loc
= FindRegisteredDataType(typeName
);
2454 if ( loc
!= wxNOT_FOUND
)
2456 delete m_typeinfo
[loc
];
2457 m_typeinfo
[loc
] = info
;
2461 m_typeinfo
.Add(info
);
2465 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2467 size_t count
= m_typeinfo
.GetCount();
2468 for ( size_t i
= 0; i
< count
; i
++ )
2470 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2479 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2481 int index
= FindRegisteredDataType(typeName
);
2482 if ( index
== wxNOT_FOUND
)
2484 // check whether this is one of the standard ones, in which case
2485 // register it "on the fly"
2487 if ( typeName
== wxGRID_VALUE_STRING
)
2489 RegisterDataType(wxGRID_VALUE_STRING
,
2490 new wxGridCellStringRenderer
,
2491 new wxGridCellTextEditor
);
2493 #endif // wxUSE_TEXTCTRL
2495 if ( typeName
== wxGRID_VALUE_BOOL
)
2497 RegisterDataType(wxGRID_VALUE_BOOL
,
2498 new wxGridCellBoolRenderer
,
2499 new wxGridCellBoolEditor
);
2501 #endif // wxUSE_CHECKBOX
2503 if ( typeName
== wxGRID_VALUE_NUMBER
)
2505 RegisterDataType(wxGRID_VALUE_NUMBER
,
2506 new wxGridCellNumberRenderer
,
2507 new wxGridCellNumberEditor
);
2509 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2511 RegisterDataType(wxGRID_VALUE_FLOAT
,
2512 new wxGridCellFloatRenderer
,
2513 new wxGridCellFloatEditor
);
2515 #endif // wxUSE_TEXTCTRL
2517 if ( typeName
== wxGRID_VALUE_CHOICE
)
2519 RegisterDataType(wxGRID_VALUE_CHOICE
,
2520 new wxGridCellStringRenderer
,
2521 new wxGridCellChoiceEditor
);
2523 #endif // wxUSE_COMBOBOX
2528 // we get here only if just added the entry for this type, so return
2530 index
= m_typeinfo
.GetCount() - 1;
2536 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2538 int index
= FindDataType(typeName
);
2539 if ( index
== wxNOT_FOUND
)
2541 // the first part of the typename is the "real" type, anything after ':'
2542 // are the parameters for the renderer
2543 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2544 if ( index
== wxNOT_FOUND
)
2549 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2550 wxGridCellRenderer
*rendererOld
= renderer
;
2551 renderer
= renderer
->Clone();
2552 rendererOld
->DecRef();
2554 wxGridCellEditor
*editor
= GetEditor(index
);
2555 wxGridCellEditor
*editorOld
= editor
;
2556 editor
= editor
->Clone();
2557 editorOld
->DecRef();
2559 // do it even if there are no parameters to reset them to defaults
2560 wxString params
= typeName
.AfterFirst(_T(':'));
2561 renderer
->SetParameters(params
);
2562 editor
->SetParameters(params
);
2564 // register the new typename
2565 RegisterDataType(typeName
, renderer
, editor
);
2567 // we just registered it, it's the last one
2568 index
= m_typeinfo
.GetCount() - 1;
2574 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2576 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2582 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2584 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2590 // ----------------------------------------------------------------------------
2592 // ----------------------------------------------------------------------------
2594 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2597 wxGridTableBase::wxGridTableBase()
2599 m_view
= (wxGrid
*) NULL
;
2600 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2603 wxGridTableBase::~wxGridTableBase()
2605 delete m_attrProvider
;
2608 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2610 delete m_attrProvider
;
2611 m_attrProvider
= attrProvider
;
2614 bool wxGridTableBase::CanHaveAttributes()
2616 if ( ! GetAttrProvider() )
2618 // use the default attr provider by default
2619 SetAttrProvider(new wxGridCellAttrProvider
);
2624 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2626 if ( m_attrProvider
)
2627 return m_attrProvider
->GetAttr(row
, col
, kind
);
2629 return (wxGridCellAttr
*)NULL
;
2632 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2634 if ( m_attrProvider
)
2636 attr
->SetKind(wxGridCellAttr::Cell
);
2637 m_attrProvider
->SetAttr(attr
, row
, col
);
2641 // as we take ownership of the pointer and don't store it, we must
2647 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2649 if ( m_attrProvider
)
2651 attr
->SetKind(wxGridCellAttr::Row
);
2652 m_attrProvider
->SetRowAttr(attr
, row
);
2656 // as we take ownership of the pointer and don't store it, we must
2662 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2664 if ( m_attrProvider
)
2666 attr
->SetKind(wxGridCellAttr::Col
);
2667 m_attrProvider
->SetColAttr(attr
, col
);
2671 // as we take ownership of the pointer and don't store it, we must
2677 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2678 size_t WXUNUSED(numRows
) )
2680 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2685 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2687 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2692 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2693 size_t WXUNUSED(numRows
) )
2695 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2700 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2701 size_t WXUNUSED(numCols
) )
2703 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2708 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2710 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2715 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2716 size_t WXUNUSED(numCols
) )
2718 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2724 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2727 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2728 // how much it makes sense to us geeks.
2732 wxString
wxGridTableBase::GetColLabelValue( int col
)
2734 // default col labels are:
2735 // cols 0 to 25 : A-Z
2736 // cols 26 to 675 : AA-ZZ
2741 for ( n
= 1; ; n
++ )
2743 s
+= (_T('A') + (wxChar
)( col%26
));
2745 if ( col
< 0 ) break;
2748 // reverse the string...
2750 for ( i
= 0; i
< n
; i
++ )
2759 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2761 return wxGRID_VALUE_STRING
;
2764 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2765 const wxString
& typeName
)
2767 return typeName
== wxGRID_VALUE_STRING
;
2770 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2772 return CanGetValueAs(row
, col
, typeName
);
2775 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2780 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2785 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2790 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2791 long WXUNUSED(value
) )
2795 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2796 double WXUNUSED(value
) )
2800 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2801 bool WXUNUSED(value
) )
2806 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2807 const wxString
& WXUNUSED(typeName
) )
2812 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2813 const wxString
& WXUNUSED(typeName
),
2814 void* WXUNUSED(value
) )
2818 //////////////////////////////////////////////////////////////////////
2820 // Message class for the grid table to send requests and notifications
2824 wxGridTableMessage::wxGridTableMessage()
2826 m_table
= (wxGridTableBase
*) NULL
;
2832 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2833 int commandInt1
, int commandInt2
)
2837 m_comInt1
= commandInt1
;
2838 m_comInt2
= commandInt2
;
2843 //////////////////////////////////////////////////////////////////////
2845 // A basic grid table for string data. An object of this class will
2846 // created by wxGrid if you don't specify an alternative table class.
2849 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2851 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2853 wxGridStringTable::wxGridStringTable()
2858 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2863 m_data
.Alloc( numRows
);
2866 sa
.Alloc( numCols
);
2867 for ( col
= 0; col
< numCols
; col
++ )
2869 sa
.Add( wxEmptyString
);
2872 for ( row
= 0; row
< numRows
; row
++ )
2878 wxGridStringTable::~wxGridStringTable()
2882 int wxGridStringTable::GetNumberRows()
2884 return m_data
.GetCount();
2887 int wxGridStringTable::GetNumberCols()
2889 if ( m_data
.GetCount() > 0 )
2890 return m_data
[0].GetCount();
2895 wxString
wxGridStringTable::GetValue( int row
, int col
)
2897 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2898 _T("invalid row or column index in wxGridStringTable") );
2900 return m_data
[row
][col
];
2903 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2905 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2906 _T("invalid row or column index in wxGridStringTable") );
2908 m_data
[row
][col
] = value
;
2911 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2913 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2914 _T("invalid row or column index in wxGridStringTable") );
2916 return (m_data
[row
][col
] == wxEmptyString
);
2919 void wxGridStringTable::Clear()
2922 int numRows
, numCols
;
2924 numRows
= m_data
.GetCount();
2927 numCols
= m_data
[0].GetCount();
2929 for ( row
= 0; row
< numRows
; row
++ )
2931 for ( col
= 0; col
< numCols
; col
++ )
2933 m_data
[row
][col
] = wxEmptyString
;
2940 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2944 size_t curNumRows
= m_data
.GetCount();
2945 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2946 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2948 if ( pos
>= curNumRows
)
2950 return AppendRows( numRows
);
2954 sa
.Alloc( curNumCols
);
2955 for ( col
= 0; col
< curNumCols
; col
++ )
2957 sa
.Add( wxEmptyString
);
2960 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2962 m_data
.Insert( sa
, row
);
2966 wxGridTableMessage
msg( this,
2967 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2971 GetView()->ProcessTableMessage( msg
);
2977 bool wxGridStringTable::AppendRows( size_t numRows
)
2981 size_t curNumRows
= m_data
.GetCount();
2982 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2983 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2986 if ( curNumCols
> 0 )
2988 sa
.Alloc( curNumCols
);
2989 for ( col
= 0; col
< curNumCols
; col
++ )
2991 sa
.Add( wxEmptyString
);
2995 for ( row
= 0; row
< numRows
; row
++ )
3002 wxGridTableMessage
msg( this,
3003 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3006 GetView()->ProcessTableMessage( msg
);
3012 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3016 size_t curNumRows
= m_data
.GetCount();
3018 if ( pos
>= curNumRows
)
3021 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3022 pos
, numRows
, curNumRows
);
3023 wxFAIL_MSG( errmsg
);
3027 if ( numRows
> curNumRows
- pos
)
3029 numRows
= curNumRows
- pos
;
3032 if ( numRows
>= curNumRows
)
3034 m_data
.Empty(); // don't release memory just yet
3038 for ( n
= 0; n
< numRows
; n
++ )
3040 m_data
.RemoveAt( pos
);
3045 wxGridTableMessage
msg( this,
3046 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3050 GetView()->ProcessTableMessage( msg
);
3056 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3060 size_t curNumRows
= m_data
.GetCount();
3061 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3062 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3064 if ( pos
>= curNumCols
)
3066 return AppendCols( numCols
);
3069 for ( row
= 0; row
< curNumRows
; row
++ )
3071 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3073 m_data
[row
].Insert( wxEmptyString
, col
);
3078 wxGridTableMessage
msg( this,
3079 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3083 GetView()->ProcessTableMessage( msg
);
3089 bool wxGridStringTable::AppendCols( size_t numCols
)
3093 size_t curNumRows
= m_data
.GetCount();
3097 // TODO: something better than this ?
3099 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3104 for ( row
= 0; row
< curNumRows
; row
++ )
3106 for ( n
= 0; n
< numCols
; n
++ )
3108 m_data
[row
].Add( wxEmptyString
);
3114 wxGridTableMessage
msg( this,
3115 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3118 GetView()->ProcessTableMessage( msg
);
3124 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3128 size_t curNumRows
= m_data
.GetCount();
3129 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3130 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3132 if ( pos
>= curNumCols
)
3135 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3136 pos
, numCols
, curNumCols
);
3137 wxFAIL_MSG( errmsg
);
3141 if ( numCols
> curNumCols
- pos
)
3143 numCols
= curNumCols
- pos
;
3146 for ( row
= 0; row
< curNumRows
; row
++ )
3148 if ( numCols
>= curNumCols
)
3150 m_data
[row
].Clear();
3154 for ( n
= 0; n
< numCols
; n
++ )
3156 m_data
[row
].RemoveAt( pos
);
3162 wxGridTableMessage
msg( this,
3163 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3167 GetView()->ProcessTableMessage( msg
);
3173 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3175 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3177 // using default label
3179 return wxGridTableBase::GetRowLabelValue( row
);
3183 return m_rowLabels
[ row
];
3187 wxString
wxGridStringTable::GetColLabelValue( int col
)
3189 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3191 // using default label
3193 return wxGridTableBase::GetColLabelValue( col
);
3197 return m_colLabels
[ col
];
3201 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3203 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3205 int n
= m_rowLabels
.GetCount();
3207 for ( i
= n
; i
<= row
; i
++ )
3209 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3213 m_rowLabels
[row
] = value
;
3216 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3218 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3220 int n
= m_colLabels
.GetCount();
3222 for ( i
= n
; i
<= col
; i
++ )
3224 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3228 m_colLabels
[col
] = value
;
3233 //////////////////////////////////////////////////////////////////////
3234 //////////////////////////////////////////////////////////////////////
3236 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3238 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3239 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3240 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3241 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3242 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3243 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3246 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3248 const wxPoint
&pos
, const wxSize
&size
)
3249 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3254 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3258 // NO - don't do this because it will set both the x and y origin
3259 // coords to match the parent scrolled window and we just want to
3260 // set the y coord - MB
3262 // m_owner->PrepareDC( dc );
3265 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3266 dc
.SetDeviceOrigin( 0, -y
);
3268 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3269 m_owner
->DrawRowLabels( dc
, rows
);
3273 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3275 m_owner
->ProcessRowLabelMouseEvent( event
);
3279 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3281 m_owner
->GetEventHandler()->ProcessEvent(event
);
3285 // This seems to be required for wxMotif otherwise the mouse
3286 // cursor must be in the cell edit control to get key events
3288 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3290 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3293 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3295 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3300 //////////////////////////////////////////////////////////////////////
3302 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3304 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3305 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3306 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3307 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3308 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3309 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3312 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3314 const wxPoint
&pos
, const wxSize
&size
)
3315 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3320 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3324 // NO - don't do this because it will set both the x and y origin
3325 // coords to match the parent scrolled window and we just want to
3326 // set the x coord - MB
3328 // m_owner->PrepareDC( dc );
3331 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3332 dc
.SetDeviceOrigin( -x
, 0 );
3334 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3335 m_owner
->DrawColLabels( dc
, cols
);
3339 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3341 m_owner
->ProcessColLabelMouseEvent( event
);
3344 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3346 m_owner
->GetEventHandler()->ProcessEvent(event
);
3350 // This seems to be required for wxMotif otherwise the mouse
3351 // cursor must be in the cell edit control to get key events
3353 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3355 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3358 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3360 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3365 //////////////////////////////////////////////////////////////////////
3367 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3369 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3370 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3371 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3372 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3373 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3374 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3377 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3379 const wxPoint
&pos
, const wxSize
&size
)
3380 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3385 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3389 int client_height
= 0;
3390 int client_width
= 0;
3391 GetClientSize( &client_width
, &client_height
);
3393 dc
.SetPen( *wxBLACK_PEN
);
3394 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3395 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3397 dc
.SetPen( *wxWHITE_PEN
);
3398 dc
.DrawLine( 0, 0, client_width
, 0 );
3399 dc
.DrawLine( 0, 0, 0, client_height
);
3403 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3405 m_owner
->ProcessCornerLabelMouseEvent( event
);
3409 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3411 m_owner
->GetEventHandler()->ProcessEvent(event
);
3414 // This seems to be required for wxMotif otherwise the mouse
3415 // cursor must be in the cell edit control to get key events
3417 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3419 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3422 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3424 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3429 //////////////////////////////////////////////////////////////////////
3431 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3433 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3434 EVT_PAINT( wxGridWindow::OnPaint
)
3435 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3436 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3437 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3438 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3439 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3442 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3443 wxGridRowLabelWindow
*rowLblWin
,
3444 wxGridColLabelWindow
*colLblWin
,
3445 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3446 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3449 m_rowLabelWin
= rowLblWin
;
3450 m_colLabelWin
= colLblWin
;
3451 SetBackgroundColour(_T("WHITE"));
3455 wxGridWindow::~wxGridWindow()
3460 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3462 wxPaintDC
dc( this );
3463 m_owner
->PrepareDC( dc
);
3464 wxRegion reg
= GetUpdateRegion();
3465 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3466 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3467 #if WXGRID_DRAW_LINES
3468 m_owner
->DrawAllGridLines( dc
, reg
);
3470 m_owner
->DrawGridSpace( dc
);
3471 m_owner
->DrawHighlight( dc
, DirtyCells
);
3475 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3477 wxWindow::ScrollWindow( dx
, dy
, rect
);
3478 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3479 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3483 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3485 m_owner
->ProcessGridCellMouseEvent( event
);
3488 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3490 m_owner
->GetEventHandler()->ProcessEvent(event
);
3493 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3494 // cursor must be in the cell edit control to get key events
3496 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3498 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3501 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3503 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3506 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3511 //////////////////////////////////////////////////////////////////////
3513 // Internal Helper function for computing row or column from some
3514 // (unscrolled) coordinate value, using either
3515 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3516 // of m_rowBottoms/m_ColRights to speed up the search!
3518 // Internal helper macros for simpler use of that function
3520 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3521 const wxArrayInt
& BorderArray
, bool maxOnOverflow
);
3523 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3524 WXGRID_MIN_COL_WIDTH, \
3526 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3527 WXGRID_MIN_ROW_HEIGHT, \
3529 /////////////////////////////////////////////////////////////////////
3531 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3533 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3534 EVT_PAINT( wxGrid::OnPaint
)
3535 EVT_SIZE( wxGrid::OnSize
)
3536 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3537 EVT_KEY_UP( wxGrid::OnKeyUp
)
3538 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3541 wxGrid::wxGrid( wxWindow
*parent
,
3546 const wxString
& name
)
3547 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3548 m_colMinWidths(GRID_HASH_SIZE
),
3549 m_rowMinHeights(GRID_HASH_SIZE
)
3557 // Must do this or ~wxScrollHelper will pop the wrong event handler
3558 SetTargetWindow(this);
3560 wxSafeDecRef(m_defaultCellAttr
);
3562 #ifdef DEBUG_ATTR_CACHE
3563 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3564 wxPrintf(_T("wxGrid attribute cache statistics: "
3565 "total: %u, hits: %u (%u%%)\n"),
3566 total
, gs_nAttrCacheHits
,
3567 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3573 delete m_typeRegistry
;
3579 // ----- internal init and update functions
3582 void wxGrid::Create()
3584 m_created
= FALSE
; // set to TRUE by CreateGrid
3586 m_table
= (wxGridTableBase
*) NULL
;
3589 m_cellEditCtrlEnabled
= FALSE
;
3591 m_defaultCellAttr
= new wxGridCellAttr();
3593 // Set default cell attributes
3594 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3595 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3596 m_defaultCellAttr
->SetFont(GetFont());
3597 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3598 m_defaultCellAttr
->SetTextColour(
3599 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3600 m_defaultCellAttr
->SetBackgroundColour(
3601 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3602 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3603 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3608 m_currentCellCoords
= wxGridNoCellCoords
;
3610 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3611 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3613 // create the type registry
3614 m_typeRegistry
= new wxGridTypeRegistry
;
3617 // subwindow components that make up the wxGrid
3618 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3623 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3628 m_colLabelWin
= new wxGridColLabelWindow( this,
3633 m_gridWin
= new wxGridWindow( this,
3640 SetTargetWindow( m_gridWin
);
3646 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3647 wxGrid::wxGridSelectionModes selmode
)
3649 wxCHECK_MSG( !m_created
,
3651 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3653 m_numRows
= numRows
;
3654 m_numCols
= numCols
;
3656 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3657 m_table
->SetView( this );
3659 m_selection
= new wxGridSelection( this, selmode
);
3668 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3670 wxCHECK_RET( m_created
,
3671 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3673 m_selection
->SetSelectionMode( selmode
);
3676 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3677 wxGrid::wxGridSelectionModes selmode
)
3681 // RD: Actually, this should probably be allowed. I think it would be
3682 // nice to be able to switch multiple Tables in and out of a single
3683 // View at runtime. Is there anything in the implementation that
3684 // would prevent this?
3686 // At least, you now have to cope with m_selection
3687 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3692 m_numRows
= table
->GetNumberRows();
3693 m_numCols
= table
->GetNumberCols();
3696 m_table
->SetView( this );
3699 m_selection
= new wxGridSelection( this, selmode
);
3712 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3713 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3715 if ( m_rowLabelWin
)
3717 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3721 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3724 m_labelTextColour
= wxColour( _T("BLACK") );
3727 m_attrCache
.row
= -1;
3729 // TODO: something better than this ?
3731 m_labelFont
= this->GetFont();
3732 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3734 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3735 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3737 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3738 m_colLabelVertAlign
= wxALIGN_TOP
;
3740 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3741 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3743 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3744 m_defaultRowHeight
+= 8;
3746 m_defaultRowHeight
+= 4;
3749 m_gridLineColour
= wxColour( 128, 128, 255 );
3750 m_gridLinesEnabled
= TRUE
;
3751 m_cellHighlightColour
= m_gridLineColour
;
3752 m_cellHighlightPenWidth
= 2;
3753 m_cellHighlightROPenWidth
= 1;
3755 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3756 m_winCapture
= (wxWindow
*)NULL
;
3757 m_canDragRowSize
= TRUE
;
3758 m_canDragColSize
= TRUE
;
3759 m_canDragGridSize
= TRUE
;
3761 m_dragRowOrCol
= -1;
3762 m_isDragging
= FALSE
;
3763 m_startDragPos
= wxDefaultPosition
;
3765 m_waitForSlowClick
= FALSE
;
3767 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3768 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3770 m_currentCellCoords
= wxGridNoCellCoords
;
3772 m_selectingTopLeft
= wxGridNoCellCoords
;
3773 m_selectingBottomRight
= wxGridNoCellCoords
;
3774 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3775 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3777 m_editable
= TRUE
; // default for whole grid
3779 m_inOnKeyDown
= FALSE
;
3786 // ----------------------------------------------------------------------------
3787 // the idea is to call these functions only when necessary because they create
3788 // quite big arrays which eat memory mostly unnecessary - in particular, if
3789 // default widths/heights are used for all rows/columns, we may not use these
3792 // with some extra code, it should be possible to only store the
3793 // widths/heights different from default ones but this will be done later...
3794 // ----------------------------------------------------------------------------
3796 void wxGrid::InitRowHeights()
3798 m_rowHeights
.Empty();
3799 m_rowBottoms
.Empty();
3801 m_rowHeights
.Alloc( m_numRows
);
3802 m_rowBottoms
.Alloc( m_numRows
);
3805 for ( int i
= 0; i
< m_numRows
; i
++ )
3807 m_rowHeights
.Add( m_defaultRowHeight
);
3808 rowBottom
+= m_defaultRowHeight
;
3809 m_rowBottoms
.Add( rowBottom
);
3813 void wxGrid::InitColWidths()
3815 m_colWidths
.Empty();
3816 m_colRights
.Empty();
3818 m_colWidths
.Alloc( m_numCols
);
3819 m_colRights
.Alloc( m_numCols
);
3821 for ( int i
= 0; i
< m_numCols
; i
++ )
3823 m_colWidths
.Add( m_defaultColWidth
);
3824 colRight
+= m_defaultColWidth
;
3825 m_colRights
.Add( colRight
);
3829 int wxGrid::GetColWidth(int col
) const
3831 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3834 int wxGrid::GetColLeft(int col
) const
3836 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3837 : m_colRights
[col
] - m_colWidths
[col
];
3840 int wxGrid::GetColRight(int col
) const
3842 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3846 int wxGrid::GetRowHeight(int row
) const
3848 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3851 int wxGrid::GetRowTop(int row
) const
3853 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3854 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3857 int wxGrid::GetRowBottom(int row
) const
3859 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3860 : m_rowBottoms
[row
];
3863 void wxGrid::CalcDimensions()
3866 GetClientSize( &cw
, &ch
);
3868 if ( m_rowLabelWin
->IsShown() )
3869 cw
-= m_rowLabelWidth
;
3870 if ( m_colLabelWin
->IsShown() )
3871 ch
-= m_colLabelHeight
;
3874 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3875 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3877 // preserve (more or less) the previous position
3879 GetViewStart( &x
, &y
);
3881 // maybe we don't need scrollbars at all?
3883 // also adjust the position to be valid for the new scroll rangs
3904 // do set scrollbar parameters
3905 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
3906 GetScrollX(w
), GetScrollY(h
), x
, y
,
3907 GetBatchCount() != 0);
3909 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3910 // still must reposition the children
3915 void wxGrid::CalcWindowSizes()
3918 GetClientSize( &cw
, &ch
);
3920 if ( m_cornerLabelWin
->IsShown() )
3921 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3923 if ( m_colLabelWin
->IsShown() )
3924 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3926 if ( m_rowLabelWin
->IsShown() )
3927 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3929 if ( m_gridWin
->IsShown() )
3930 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3934 // this is called when the grid table sends a message to say that it
3935 // has been redimensioned
3937 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3940 bool result
= FALSE
;
3943 // if we were using the default widths/heights so far, we must change them
3945 if ( m_colWidths
.IsEmpty() )
3950 if ( m_rowHeights
.IsEmpty() )
3956 switch ( msg
.GetId() )
3958 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3960 size_t pos
= msg
.GetCommandInt();
3961 int numRows
= msg
.GetCommandInt2();
3963 m_numRows
+= numRows
;
3965 if ( !m_rowHeights
.IsEmpty() )
3967 for ( i
= 0; i
< numRows
; i
++ )
3969 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3970 m_rowBottoms
.Insert( 0, pos
);
3974 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3976 for ( i
= pos
; i
< m_numRows
; i
++ )
3978 bottom
+= m_rowHeights
[i
];
3979 m_rowBottoms
[i
] = bottom
;
3982 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3984 // if we have just inserted cols into an empty grid the current
3985 // cell will be undefined...
3987 SetCurrentCell( 0, 0 );
3991 m_selection
->UpdateRows( pos
, numRows
);
3992 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3994 attrProvider
->UpdateAttrRows( pos
, numRows
);
3996 if ( !GetBatchCount() )
3999 m_rowLabelWin
->Refresh();
4005 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4007 int numRows
= msg
.GetCommandInt();
4008 int oldNumRows
= m_numRows
;
4009 m_numRows
+= numRows
;
4011 if ( !m_rowHeights
.IsEmpty() )
4013 for ( i
= 0; i
< numRows
; i
++ )
4015 m_rowHeights
.Add( m_defaultRowHeight
);
4016 m_rowBottoms
.Add( 0 );
4020 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4022 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4024 bottom
+= m_rowHeights
[i
];
4025 m_rowBottoms
[i
] = bottom
;
4028 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4030 // if we have just inserted cols into an empty grid the current
4031 // cell will be undefined...
4033 SetCurrentCell( 0, 0 );
4035 if ( !GetBatchCount() )
4038 m_rowLabelWin
->Refresh();
4044 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4046 size_t pos
= msg
.GetCommandInt();
4047 int numRows
= msg
.GetCommandInt2();
4048 m_numRows
-= numRows
;
4050 if ( !m_rowHeights
.IsEmpty() )
4052 for ( i
= 0; i
< numRows
; i
++ )
4054 m_rowHeights
.RemoveAt( pos
);
4055 m_rowBottoms
.RemoveAt( pos
);
4059 for ( i
= 0; i
< m_numRows
; i
++ )
4061 h
+= m_rowHeights
[i
];
4062 m_rowBottoms
[i
] = h
;
4067 m_currentCellCoords
= wxGridNoCellCoords
;
4071 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4072 m_currentCellCoords
.Set( 0, 0 );
4076 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4077 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4079 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4080 // ifdef'd out following patch from Paul Gammans
4082 // No need to touch column attributes, unless we
4083 // removed _all_ rows, in this case, we remove
4084 // all column attributes.
4085 // I hate to do this here, but the
4086 // needed data is not available inside UpdateAttrRows.
4087 if ( !GetNumberRows() )
4088 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4091 if ( !GetBatchCount() )
4094 m_rowLabelWin
->Refresh();
4100 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4102 size_t pos
= msg
.GetCommandInt();
4103 int numCols
= msg
.GetCommandInt2();
4104 m_numCols
+= numCols
;
4106 if ( !m_colWidths
.IsEmpty() )
4108 for ( i
= 0; i
< numCols
; i
++ )
4110 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4111 m_colRights
.Insert( 0, pos
);
4115 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4117 for ( i
= pos
; i
< m_numCols
; i
++ )
4119 right
+= m_colWidths
[i
];
4120 m_colRights
[i
] = right
;
4123 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4125 // if we have just inserted cols into an empty grid the current
4126 // cell will be undefined...
4128 SetCurrentCell( 0, 0 );
4132 m_selection
->UpdateCols( pos
, numCols
);
4133 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4135 attrProvider
->UpdateAttrCols( pos
, numCols
);
4136 if ( !GetBatchCount() )
4139 m_colLabelWin
->Refresh();
4146 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4148 int numCols
= msg
.GetCommandInt();
4149 int oldNumCols
= m_numCols
;
4150 m_numCols
+= numCols
;
4151 if ( !m_colWidths
.IsEmpty() )
4153 for ( i
= 0; i
< numCols
; i
++ )
4155 m_colWidths
.Add( m_defaultColWidth
);
4156 m_colRights
.Add( 0 );
4160 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4162 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4164 right
+= m_colWidths
[i
];
4165 m_colRights
[i
] = right
;
4168 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4170 // if we have just inserted cols into an empty grid the current
4171 // cell will be undefined...
4173 SetCurrentCell( 0, 0 );
4175 if ( !GetBatchCount() )
4178 m_colLabelWin
->Refresh();
4184 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4186 size_t pos
= msg
.GetCommandInt();
4187 int numCols
= msg
.GetCommandInt2();
4188 m_numCols
-= numCols
;
4190 if ( !m_colWidths
.IsEmpty() )
4192 for ( i
= 0; i
< numCols
; i
++ )
4194 m_colWidths
.RemoveAt( pos
);
4195 m_colRights
.RemoveAt( pos
);
4199 for ( i
= 0; i
< m_numCols
; i
++ )
4201 w
+= m_colWidths
[i
];
4207 m_currentCellCoords
= wxGridNoCellCoords
;
4211 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4212 m_currentCellCoords
.Set( 0, 0 );
4216 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4217 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4219 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4220 // ifdef'd out following patch from Paul Gammans
4222 // No need to touch row attributes, unless we
4223 // removed _all_ columns, in this case, we remove
4224 // all row attributes.
4225 // I hate to do this here, but the
4226 // needed data is not available inside UpdateAttrCols.
4227 if ( !GetNumberCols() )
4228 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4231 if ( !GetBatchCount() )
4234 m_colLabelWin
->Refresh();
4241 if (result
&& !GetBatchCount() )
4242 m_gridWin
->Refresh();
4247 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4249 wxRegionIterator
iter( reg
);
4252 wxArrayInt rowlabels
;
4259 // TODO: remove this when we can...
4260 // There is a bug in wxMotif that gives garbage update
4261 // rectangles if you jump-scroll a long way by clicking the
4262 // scrollbar with middle button. This is a work-around
4264 #if defined(__WXMOTIF__)
4266 m_gridWin
->GetClientSize( &cw
, &ch
);
4267 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4268 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4271 // logical bounds of update region
4274 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4275 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4277 // find the row labels within these bounds
4280 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4282 if ( GetRowBottom(row
) < top
)
4285 if ( GetRowTop(row
) > bottom
)
4288 rowlabels
.Add( row
);
4298 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4300 wxRegionIterator
iter( reg
);
4303 wxArrayInt colLabels
;
4310 // TODO: remove this when we can...
4311 // There is a bug in wxMotif that gives garbage update
4312 // rectangles if you jump-scroll a long way by clicking the
4313 // scrollbar with middle button. This is a work-around
4315 #if defined(__WXMOTIF__)
4317 m_gridWin
->GetClientSize( &cw
, &ch
);
4318 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4319 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4322 // logical bounds of update region
4325 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4326 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4328 // find the cells within these bounds
4331 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4333 if ( GetColRight(col
) < left
)
4336 if ( GetColLeft(col
) > right
)
4339 colLabels
.Add( col
);
4348 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4350 wxRegionIterator
iter( reg
);
4353 wxGridCellCoordsArray cellsExposed
;
4355 int left
, top
, right
, bottom
;
4360 // TODO: remove this when we can...
4361 // There is a bug in wxMotif that gives garbage update
4362 // rectangles if you jump-scroll a long way by clicking the
4363 // scrollbar with middle button. This is a work-around
4365 #if defined(__WXMOTIF__)
4367 m_gridWin
->GetClientSize( &cw
, &ch
);
4368 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4369 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4370 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4371 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4374 // logical bounds of update region
4376 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4377 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4379 // find the cells within these bounds
4382 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4384 if ( GetRowBottom(row
) <= top
)
4387 if ( GetRowTop(row
) > bottom
)
4390 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4392 if ( GetColRight(col
) <= left
)
4395 if ( GetColLeft(col
) > right
)
4398 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4405 return cellsExposed
;
4409 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4412 wxPoint
pos( event
.GetPosition() );
4413 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4415 if ( event
.Dragging() )
4419 m_isDragging
= TRUE
;
4420 m_rowLabelWin
->CaptureMouse();
4423 if ( event
.LeftIsDown() )
4425 switch( m_cursorMode
)
4427 case WXGRID_CURSOR_RESIZE_ROW
:
4429 int cw
, ch
, left
, dummy
;
4430 m_gridWin
->GetClientSize( &cw
, &ch
);
4431 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4433 wxClientDC
dc( m_gridWin
);
4436 GetRowTop(m_dragRowOrCol
) +
4437 GetRowMinimalHeight(m_dragRowOrCol
) );
4438 dc
.SetLogicalFunction(wxINVERT
);
4439 if ( m_dragLastPos
>= 0 )
4441 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4443 dc
.DrawLine( left
, y
, left
+cw
, y
);
4448 case WXGRID_CURSOR_SELECT_ROW
:
4449 if ( (row
= YToRow( y
)) >= 0 )
4453 m_selection
->SelectRow( row
,
4454 event
.ControlDown(),
4461 // default label to suppress warnings about "enumeration value
4462 // 'xxx' not handled in switch
4470 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4475 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4476 m_isDragging
= FALSE
;
4479 // ------------ Entering or leaving the window
4481 if ( event
.Entering() || event
.Leaving() )
4483 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4487 // ------------ Left button pressed
4489 else if ( event
.LeftDown() )
4491 // don't send a label click event for a hit on the
4492 // edge of the row label - this is probably the user
4493 // wanting to resize the row
4495 if ( YToEdgeOfRow(y
) < 0 )
4499 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4501 if ( !event
.ShiftDown() && !event
.ControlDown() )
4503 else if ( m_selection
)
4505 if ( event
.ShiftDown() )
4507 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4510 GetNumberCols() - 1,
4511 event
.ControlDown(),
4518 m_selection
->SelectRow( row
,
4519 event
.ControlDown(),
4526 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4531 // starting to drag-resize a row
4533 if ( CanDragRowSize() )
4534 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4539 // ------------ Left double click
4541 else if (event
.LeftDClick() )
4543 if ( YToEdgeOfRow(y
) < 0 )
4546 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4551 // ------------ Left button released
4553 else if ( event
.LeftUp() )
4555 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4557 DoEndDragResizeRow();
4559 // Note: we are ending the event *after* doing
4560 // default processing in this case
4562 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4565 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4570 // ------------ Right button down
4572 else if ( event
.RightDown() )
4575 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4577 // no default action at the moment
4582 // ------------ Right double click
4584 else if ( event
.RightDClick() )
4587 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4589 // no default action at the moment
4594 // ------------ No buttons down and mouse moving
4596 else if ( event
.Moving() )
4598 m_dragRowOrCol
= YToEdgeOfRow( y
);
4599 if ( m_dragRowOrCol
>= 0 )
4601 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4603 // don't capture the mouse yet
4604 if ( CanDragRowSize() )
4605 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4608 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4610 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4616 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4619 wxPoint
pos( event
.GetPosition() );
4620 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4622 if ( event
.Dragging() )
4626 m_isDragging
= TRUE
;
4627 m_colLabelWin
->CaptureMouse();
4630 if ( event
.LeftIsDown() )
4632 switch( m_cursorMode
)
4634 case WXGRID_CURSOR_RESIZE_COL
:
4636 int cw
, ch
, dummy
, top
;
4637 m_gridWin
->GetClientSize( &cw
, &ch
);
4638 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4640 wxClientDC
dc( m_gridWin
);
4643 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4644 GetColMinimalWidth(m_dragRowOrCol
));
4645 dc
.SetLogicalFunction(wxINVERT
);
4646 if ( m_dragLastPos
>= 0 )
4648 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4650 dc
.DrawLine( x
, top
, x
, top
+ch
);
4655 case WXGRID_CURSOR_SELECT_COL
:
4656 if ( (col
= XToCol( x
)) >= 0 )
4660 m_selection
->SelectCol( col
,
4661 event
.ControlDown(),
4668 // default label to suppress warnings about "enumeration value
4669 // 'xxx' not handled in switch
4677 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4682 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4683 m_isDragging
= FALSE
;
4686 // ------------ Entering or leaving the window
4688 if ( event
.Entering() || event
.Leaving() )
4690 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4694 // ------------ Left button pressed
4696 else if ( event
.LeftDown() )
4698 // don't send a label click event for a hit on the
4699 // edge of the col label - this is probably the user
4700 // wanting to resize the col
4702 if ( XToEdgeOfCol(x
) < 0 )
4706 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4708 if ( !event
.ShiftDown() && !event
.ControlDown() )
4712 if ( event
.ShiftDown() )
4714 m_selection
->SelectBlock( 0,
4715 m_currentCellCoords
.GetCol(),
4716 GetNumberRows() - 1, col
,
4717 event
.ControlDown(),
4724 m_selection
->SelectCol( col
,
4725 event
.ControlDown(),
4732 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4737 // starting to drag-resize a col
4739 if ( CanDragColSize() )
4740 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4745 // ------------ Left double click
4747 if ( event
.LeftDClick() )
4749 if ( XToEdgeOfCol(x
) < 0 )
4752 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4757 // ------------ Left button released
4759 else if ( event
.LeftUp() )
4761 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4763 DoEndDragResizeCol();
4765 // Note: we are ending the event *after* doing
4766 // default processing in this case
4768 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4771 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4776 // ------------ Right button down
4778 else if ( event
.RightDown() )
4781 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4783 // no default action at the moment
4788 // ------------ Right double click
4790 else if ( event
.RightDClick() )
4793 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4795 // no default action at the moment
4800 // ------------ No buttons down and mouse moving
4802 else if ( event
.Moving() )
4804 m_dragRowOrCol
= XToEdgeOfCol( x
);
4805 if ( m_dragRowOrCol
>= 0 )
4807 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4809 // don't capture the cursor yet
4810 if ( CanDragColSize() )
4811 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4814 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4816 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4822 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4824 if ( event
.LeftDown() )
4826 // indicate corner label by having both row and
4829 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4835 else if ( event
.LeftDClick() )
4837 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4840 else if ( event
.RightDown() )
4842 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4844 // no default action at the moment
4848 else if ( event
.RightDClick() )
4850 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4852 // no default action at the moment
4857 void wxGrid::ChangeCursorMode(CursorMode mode
,
4862 static const wxChar
*cursorModes
[] =
4871 wxLogTrace(_T("grid"),
4872 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4873 win
== m_colLabelWin
? _T("colLabelWin")
4874 : win
? _T("rowLabelWin")
4876 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4877 #endif // __WXDEBUG__
4879 if ( mode
== m_cursorMode
&&
4880 win
== m_winCapture
&&
4881 captureMouse
== (m_winCapture
!= NULL
))
4886 // by default use the grid itself
4892 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4893 m_winCapture
= (wxWindow
*)NULL
;
4896 m_cursorMode
= mode
;
4898 switch ( m_cursorMode
)
4900 case WXGRID_CURSOR_RESIZE_ROW
:
4901 win
->SetCursor( m_rowResizeCursor
);
4904 case WXGRID_CURSOR_RESIZE_COL
:
4905 win
->SetCursor( m_colResizeCursor
);
4909 win
->SetCursor( *wxSTANDARD_CURSOR
);
4912 // we need to capture mouse when resizing
4913 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4914 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4916 if ( captureMouse
&& resize
)
4918 win
->CaptureMouse();
4923 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4926 wxPoint
pos( event
.GetPosition() );
4927 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4929 wxGridCellCoords coords
;
4930 XYToCell( x
, y
, coords
);
4932 if ( event
.Dragging() )
4934 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4936 // Don't start doing anything until the mouse has been drug at
4937 // least 3 pixels in any direction...
4940 if (m_startDragPos
== wxDefaultPosition
)
4942 m_startDragPos
= pos
;
4945 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4949 m_isDragging
= TRUE
;
4950 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4952 // Hide the edit control, so it
4953 // won't interfer with drag-shrinking.
4954 if ( IsCellEditControlShown() )
4956 HideCellEditControl();
4957 SaveEditControlValue();
4960 // Have we captured the mouse yet?
4963 m_winCapture
= m_gridWin
;
4964 m_winCapture
->CaptureMouse();
4967 if ( coords
!= wxGridNoCellCoords
)
4969 if ( event
.ControlDown() )
4971 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4972 m_selectingKeyboard
= coords
;
4973 HighlightBlock ( m_selectingKeyboard
, coords
);
4977 if ( !IsSelection() )
4979 HighlightBlock( coords
, coords
);
4983 HighlightBlock( m_currentCellCoords
, coords
);
4987 if (! IsVisible(coords
))
4989 MakeCellVisible(coords
);
4990 // TODO: need to introduce a delay or something here. The
4991 // scrolling is way to fast, at least on MSW - also on GTK.
4995 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4997 int cw
, ch
, left
, dummy
;
4998 m_gridWin
->GetClientSize( &cw
, &ch
);
4999 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5001 wxClientDC
dc( m_gridWin
);
5003 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5004 GetRowMinimalHeight(m_dragRowOrCol
) );
5005 dc
.SetLogicalFunction(wxINVERT
);
5006 if ( m_dragLastPos
>= 0 )
5008 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5010 dc
.DrawLine( left
, y
, left
+cw
, y
);
5013 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5015 int cw
, ch
, dummy
, top
;
5016 m_gridWin
->GetClientSize( &cw
, &ch
);
5017 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5019 wxClientDC
dc( m_gridWin
);
5021 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5022 GetColMinimalWidth(m_dragRowOrCol
) );
5023 dc
.SetLogicalFunction(wxINVERT
);
5024 if ( m_dragLastPos
>= 0 )
5026 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5028 dc
.DrawLine( x
, top
, x
, top
+ch
);
5035 m_isDragging
= FALSE
;
5036 m_startDragPos
= wxDefaultPosition
;
5038 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5039 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5042 if ( event
.Entering() || event
.Leaving() )
5044 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5045 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5050 // ------------ Left button pressed
5052 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5054 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5059 if ( !event
.ControlDown() )
5061 if ( event
.ShiftDown() )
5065 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5066 m_currentCellCoords
.GetCol(),
5069 event
.ControlDown(),
5075 else if ( XToEdgeOfCol(x
) < 0 &&
5076 YToEdgeOfRow(y
) < 0 )
5078 DisableCellEditControl();
5079 MakeCellVisible( coords
);
5081 // if this is the second click on this cell then start
5083 if ( m_waitForSlowClick
&&
5084 (coords
== m_currentCellCoords
) &&
5085 CanEnableCellControl())
5087 EnableCellEditControl();
5089 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5090 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5093 editor
->StartingClick();
5097 m_waitForSlowClick
= FALSE
;
5101 if ( event
.ControlDown() )
5105 m_selection
->ToggleCellSelection( coords
.GetRow(),
5107 event
.ControlDown(),
5112 m_selectingTopLeft
= wxGridNoCellCoords
;
5113 m_selectingBottomRight
= wxGridNoCellCoords
;
5114 m_selectingKeyboard
= coords
;
5118 SetCurrentCell( coords
);
5121 if ( m_selection
->GetSelectionMode() !=
5122 wxGrid::wxGridSelectCells
)
5124 HighlightBlock( coords
, coords
);
5128 m_waitForSlowClick
= TRUE
;
5135 // ------------ Left double click
5137 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5139 DisableCellEditControl();
5141 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5143 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5151 // ------------ Left button released
5153 else if ( event
.LeftUp() )
5155 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5157 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5158 m_selectingBottomRight
!= wxGridNoCellCoords
)
5162 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5163 m_winCapture
= NULL
;
5168 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5169 m_selectingTopLeft
.GetCol(),
5170 m_selectingBottomRight
.GetRow(),
5171 m_selectingBottomRight
.GetCol(),
5172 event
.ControlDown(),
5178 m_selectingTopLeft
= wxGridNoCellCoords
;
5179 m_selectingBottomRight
= wxGridNoCellCoords
;
5182 // Show the edit control, if it has been hidden for
5184 ShowCellEditControl();
5186 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5188 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5189 DoEndDragResizeRow();
5191 // Note: we are ending the event *after* doing
5192 // default processing in this case
5194 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5196 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5198 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5199 DoEndDragResizeCol();
5201 // Note: we are ending the event *after* doing
5202 // default processing in this case
5204 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5211 // ------------ Right button down
5213 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5215 DisableCellEditControl();
5216 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5221 // no default action at the moment
5226 // ------------ Right double click
5228 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5230 DisableCellEditControl();
5231 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5236 // no default action at the moment
5240 // ------------ Moving and no button action
5242 else if ( event
.Moving() && !event
.IsButton() )
5244 int dragRow
= YToEdgeOfRow( y
);
5245 int dragCol
= XToEdgeOfCol( x
);
5247 // Dragging on the corner of a cell to resize in both
5248 // directions is not implemented yet...
5250 if ( dragRow
>= 0 && dragCol
>= 0 )
5252 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5258 m_dragRowOrCol
= dragRow
;
5260 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5262 if ( CanDragRowSize() && CanDragGridSize() )
5263 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5268 m_dragRowOrCol
= dragCol
;
5276 m_dragRowOrCol
= dragCol
;
5278 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5280 if ( CanDragColSize() && CanDragGridSize() )
5281 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5287 // Neither on a row or col edge
5289 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5291 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5297 void wxGrid::DoEndDragResizeRow()
5299 if ( m_dragLastPos
>= 0 )
5301 // erase the last line and resize the row
5303 int cw
, ch
, left
, dummy
;
5304 m_gridWin
->GetClientSize( &cw
, &ch
);
5305 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5307 wxClientDC
dc( m_gridWin
);
5309 dc
.SetLogicalFunction( wxINVERT
);
5310 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5311 HideCellEditControl();
5312 SaveEditControlValue();
5314 int rowTop
= GetRowTop(m_dragRowOrCol
);
5315 SetRowSize( m_dragRowOrCol
,
5316 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5318 if ( !GetBatchCount() )
5320 // Only needed to get the correct rect.y:
5321 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5323 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5324 rect
.width
= m_rowLabelWidth
;
5325 rect
.height
= ch
- rect
.y
;
5326 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5328 m_gridWin
->Refresh( FALSE
, &rect
);
5331 ShowCellEditControl();
5336 void wxGrid::DoEndDragResizeCol()
5338 if ( m_dragLastPos
>= 0 )
5340 // erase the last line and resize the col
5342 int cw
, ch
, dummy
, top
;
5343 m_gridWin
->GetClientSize( &cw
, &ch
);
5344 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5346 wxClientDC
dc( m_gridWin
);
5348 dc
.SetLogicalFunction( wxINVERT
);
5349 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5350 HideCellEditControl();
5351 SaveEditControlValue();
5353 int colLeft
= GetColLeft(m_dragRowOrCol
);
5354 SetColSize( m_dragRowOrCol
,
5355 wxMax( m_dragLastPos
- colLeft
,
5356 GetColMinimalWidth(m_dragRowOrCol
) ) );
5358 if ( !GetBatchCount() )
5360 // Only needed to get the correct rect.x:
5361 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5363 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5364 rect
.width
= cw
- rect
.x
;
5365 rect
.height
= m_colLabelHeight
;
5366 m_colLabelWin
->Refresh( TRUE
, &rect
);
5368 m_gridWin
->Refresh( FALSE
, &rect
);
5371 ShowCellEditControl();
5378 // ------ interaction with data model
5380 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5382 switch ( msg
.GetId() )
5384 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5385 return GetModelValues();
5387 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5388 return SetModelValues();
5390 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5391 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5392 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5393 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5394 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5395 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5396 return Redimension( msg
);
5405 // The behaviour of this function depends on the grid table class
5406 // Clear() function. For the default wxGridStringTable class the
5407 // behavious is to replace all cell contents with wxEmptyString but
5408 // not to change the number of rows or cols.
5410 void wxGrid::ClearGrid()
5414 if (IsCellEditControlEnabled())
5415 DisableCellEditControl();
5418 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5423 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5425 // TODO: something with updateLabels flag
5429 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5435 if (IsCellEditControlEnabled())
5436 DisableCellEditControl();
5438 return m_table
->InsertRows( pos
, numRows
);
5440 // the table will have sent the results of the insert row
5441 // operation to this view object as a grid table message
5447 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5449 // TODO: something with updateLabels flag
5453 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5457 return ( m_table
&& m_table
->AppendRows( numRows
) );
5458 // the table will have sent the results of the append row
5459 // operation to this view object as a grid table message
5463 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5465 // TODO: something with updateLabels flag
5469 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5475 if (IsCellEditControlEnabled())
5476 DisableCellEditControl();
5478 return (m_table
->DeleteRows( pos
, numRows
));
5479 // the table will have sent the results of the delete row
5480 // operation to this view object as a grid table message
5486 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5488 // TODO: something with updateLabels flag
5492 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5498 if (IsCellEditControlEnabled())
5499 DisableCellEditControl();
5501 return m_table
->InsertCols( pos
, numCols
);
5502 // the table will have sent the results of the insert col
5503 // operation to this view object as a grid table message
5509 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5511 // TODO: something with updateLabels flag
5515 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5519 return ( m_table
&& m_table
->AppendCols( numCols
) );
5520 // the table will have sent the results of the append col
5521 // operation to this view object as a grid table message
5525 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5527 // TODO: something with updateLabels flag
5531 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5537 if (IsCellEditControlEnabled())
5538 DisableCellEditControl();
5540 return ( m_table
->DeleteCols( pos
, numCols
) );
5541 // the table will have sent the results of the delete col
5542 // operation to this view object as a grid table message
5550 // ----- event handlers
5553 // Generate a grid event based on a mouse event and
5554 // return the result of ProcessEvent()
5556 int wxGrid::SendEvent( const wxEventType type
,
5558 wxMouseEvent
& mouseEv
)
5563 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5565 int rowOrCol
= (row
== -1 ? col
: row
);
5567 wxGridSizeEvent
gridEvt( GetId(),
5571 mouseEv
.GetX() + GetRowLabelSize(),
5572 mouseEv
.GetY() + GetColLabelSize(),
5573 mouseEv
.ControlDown(),
5574 mouseEv
.ShiftDown(),
5576 mouseEv
.MetaDown() );
5578 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5579 vetoed
= !gridEvt
.IsAllowed();
5581 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5583 // Right now, it should _never_ end up here!
5584 wxGridRangeSelectEvent
gridEvt( GetId(),
5588 m_selectingBottomRight
,
5590 mouseEv
.ControlDown(),
5591 mouseEv
.ShiftDown(),
5593 mouseEv
.MetaDown() );
5595 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5596 vetoed
= !gridEvt
.IsAllowed();
5600 wxGridEvent
gridEvt( GetId(),
5604 mouseEv
.GetX() + GetRowLabelSize(),
5605 mouseEv
.GetY() + GetColLabelSize(),
5607 mouseEv
.ControlDown(),
5608 mouseEv
.ShiftDown(),
5610 mouseEv
.MetaDown() );
5611 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5612 vetoed
= !gridEvt
.IsAllowed();
5615 // A Veto'd event may not be `claimed' so test this first
5616 if (vetoed
) return -1;
5617 return claimed
? 1 : 0;
5621 // Generate a grid event of specified type and return the result
5622 // of ProcessEvent().
5624 int wxGrid::SendEvent( const wxEventType type
,
5630 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5632 int rowOrCol
= (row
== -1 ? col
: row
);
5634 wxGridSizeEvent
gridEvt( GetId(),
5639 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5640 vetoed
= !gridEvt
.IsAllowed();
5644 wxGridEvent
gridEvt( GetId(),
5649 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5650 vetoed
= !gridEvt
.IsAllowed();
5653 // A Veto'd event may not be `claimed' so test this first
5654 if (vetoed
) return -1;
5655 return claimed
? 1 : 0;
5659 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5661 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5665 void wxGrid::OnSize( wxSizeEvent
& event
)
5667 // position the child windows
5670 // don't call CalcDimensions() from here, the base class handles the size
5676 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5678 if ( m_inOnKeyDown
)
5680 // shouldn't be here - we are going round in circles...
5682 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5685 m_inOnKeyDown
= TRUE
;
5687 // propagate the event up and see if it gets processed
5689 wxWindow
*parent
= GetParent();
5690 wxKeyEvent
keyEvt( event
);
5691 keyEvt
.SetEventObject( parent
);
5693 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5696 // try local handlers
5698 switch ( event
.KeyCode() )
5701 if ( event
.ControlDown() )
5703 MoveCursorUpBlock( event
.ShiftDown() );
5707 MoveCursorUp( event
.ShiftDown() );
5712 if ( event
.ControlDown() )
5714 MoveCursorDownBlock( event
.ShiftDown() );
5718 MoveCursorDown( event
.ShiftDown() );
5723 if ( event
.ControlDown() )
5725 MoveCursorLeftBlock( event
.ShiftDown() );
5729 MoveCursorLeft( event
.ShiftDown() );
5734 if ( event
.ControlDown() )
5736 MoveCursorRightBlock( event
.ShiftDown() );
5740 MoveCursorRight( event
.ShiftDown() );
5745 case WXK_NUMPAD_ENTER
:
5746 if ( event
.ControlDown() )
5748 event
.Skip(); // to let the edit control have the return
5752 if ( GetGridCursorRow() < GetNumberRows()-1 )
5754 MoveCursorDown( event
.ShiftDown() );
5758 // at the bottom of a column
5759 HideCellEditControl();
5760 SaveEditControlValue();
5770 if (event
.ShiftDown())
5772 if ( GetGridCursorCol() > 0 )
5774 MoveCursorLeft( FALSE
);
5779 HideCellEditControl();
5780 SaveEditControlValue();
5785 if ( GetGridCursorCol() < GetNumberCols()-1 )
5787 MoveCursorRight( FALSE
);
5792 HideCellEditControl();
5793 SaveEditControlValue();
5799 if ( event
.ControlDown() )
5801 MakeCellVisible( 0, 0 );
5802 SetCurrentCell( 0, 0 );
5811 if ( event
.ControlDown() )
5813 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5814 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5831 if ( event
.ControlDown() )
5835 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5836 m_currentCellCoords
.GetCol(),
5837 event
.ControlDown(),
5844 if ( !IsEditable() )
5846 MoveCursorRight( FALSE
);
5849 // Otherwise fall through to default
5852 // is it possible to edit the current cell at all?
5853 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5855 // yes, now check whether the cells editor accepts the key
5856 int row
= m_currentCellCoords
.GetRow();
5857 int col
= m_currentCellCoords
.GetCol();
5858 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5859 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5861 // <F2> is special and will always start editing, for
5862 // other keys - ask the editor itself
5863 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5864 || editor
->IsAcceptedKey(event
) )
5866 EnableCellEditControl();
5868 // the editor could be not shown for a variety of
5869 // reasons (i.e. blocked by the app or whatever), so
5870 // check if it really was created
5871 if ( m_cellEditCtrlEnabled
)
5873 editor
->StartingKey(event
);
5886 // let others process char events with modifiers or all
5887 // char events for readonly cells
5894 m_inOnKeyDown
= FALSE
;
5897 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5899 // try local handlers
5901 if ( event
.KeyCode() == WXK_SHIFT
)
5903 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5904 m_selectingBottomRight
!= wxGridNoCellCoords
)
5908 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5909 m_selectingTopLeft
.GetCol(),
5910 m_selectingBottomRight
.GetRow(),
5911 m_selectingBottomRight
.GetCol(),
5912 event
.ControlDown(),
5919 m_selectingTopLeft
= wxGridNoCellCoords
;
5920 m_selectingBottomRight
= wxGridNoCellCoords
;
5921 m_selectingKeyboard
= wxGridNoCellCoords
;
5925 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5929 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5931 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5933 // the event has been intercepted - do nothing
5937 wxClientDC
dc(m_gridWin
);
5940 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5942 HideCellEditControl();
5943 DisableCellEditControl();
5945 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5948 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5949 if ( !m_gridLinesEnabled
)
5957 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5959 // Otherwise refresh redraws the highlight!
5960 m_currentCellCoords
= coords
;
5962 DrawGridCellArea(dc
,cells
);
5963 DrawAllGridLines( dc
, r
);
5967 m_currentCellCoords
= coords
;
5969 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5970 DrawCellHighlight(dc
, attr
);
5975 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5978 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5982 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5985 rightCol
= GetNumberCols() - 1;
5987 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5990 bottomRow
= GetNumberRows() - 1;
5994 if ( topRow
> bottomRow
)
6001 if ( leftCol
> rightCol
)
6008 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6009 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6011 if ( m_selectingTopLeft
!= updateTopLeft
||
6012 m_selectingBottomRight
!= updateBottomRight
)
6014 // Compute two optimal update rectangles:
6015 // Either one rectangle is a real subset of the
6016 // other, or they are (almost) disjoint!
6018 bool need_refresh
[4];
6022 need_refresh
[3] = FALSE
;
6025 // Store intermediate values
6026 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6027 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6028 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6029 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6031 // Determine the outer/inner coordinates.
6032 if (oldLeft
> leftCol
)
6038 if (oldTop
> topRow
)
6044 if (oldRight
< rightCol
)
6047 oldRight
= rightCol
;
6050 if (oldBottom
< bottomRow
)
6053 oldBottom
= bottomRow
;
6057 // Now, either the stuff marked old is the outer
6058 // rectangle or we don't have a situation where one
6059 // is contained in the other.
6061 if ( oldLeft
< leftCol
)
6063 need_refresh
[0] = TRUE
;
6064 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6066 wxGridCellCoords ( oldBottom
,
6070 if ( oldTop
< topRow
)
6072 need_refresh
[1] = TRUE
;
6073 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6075 wxGridCellCoords ( topRow
- 1,
6079 if ( oldRight
> rightCol
)
6081 need_refresh
[2] = TRUE
;
6082 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6084 wxGridCellCoords ( oldBottom
,
6088 if ( oldBottom
> bottomRow
)
6090 need_refresh
[3] = TRUE
;
6091 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6093 wxGridCellCoords ( oldBottom
,
6099 m_selectingTopLeft
= updateTopLeft
;
6100 m_selectingBottomRight
= updateBottomRight
;
6102 // various Refresh() calls
6103 for (i
= 0; i
< 4; i
++ )
6104 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6105 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6108 // never generate an event as it will be generated from
6109 // wxGridSelection::SelectBlock!
6110 // (old comment from when this was the body of SelectBlock)
6114 // ------ functions to get/send data (see also public functions)
6117 bool wxGrid::GetModelValues()
6121 // all we need to do is repaint the grid
6123 m_gridWin
->Refresh();
6131 bool wxGrid::SetModelValues()
6137 for ( row
= 0; row
< m_numRows
; row
++ )
6139 for ( col
= 0; col
< m_numCols
; col
++ )
6141 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6153 // Note - this function only draws cells that are in the list of
6154 // exposed cells (usually set from the update region by
6155 // CalcExposedCells)
6157 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6159 if ( !m_numRows
|| !m_numCols
) return;
6162 size_t numCells
= cells
.GetCount();
6164 for ( i
= 0; i
< numCells
; i
++ )
6166 DrawCell( dc
, cells
[i
] );
6171 void wxGrid::DrawGridSpace( wxDC
& dc
)
6174 m_gridWin
->GetClientSize( &cw
, &ch
);
6177 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6179 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6180 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6182 if ( right
> rightCol
|| bottom
> bottomRow
)
6185 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6187 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6188 dc
.SetPen( *wxTRANSPARENT_PEN
);
6190 if ( right
> rightCol
)
6192 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6195 if ( bottom
> bottomRow
)
6197 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6203 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6205 int row
= coords
.GetRow();
6206 int col
= coords
.GetCol();
6208 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6211 // we draw the cell border ourselves
6212 #if !WXGRID_DRAW_LINES
6213 if ( m_gridLinesEnabled
)
6214 DrawCellBorder( dc
, coords
);
6217 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6219 bool isCurrent
= coords
== m_currentCellCoords
;
6221 wxRect rect
= CellToRect( row
, col
);
6223 // if the editor is shown, we should use it and not the renderer
6224 // Note: However, only if it is really _shown_, i.e. not hidden!
6225 if ( isCurrent
&& IsCellEditControlShown() )
6227 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6228 editor
->PaintBackground(rect
, attr
);
6233 // but all the rest is drawn by the cell renderer and hence may be
6235 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6236 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6243 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6245 int row
= m_currentCellCoords
.GetRow();
6246 int col
= m_currentCellCoords
.GetCol();
6248 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6251 wxRect rect
= CellToRect(row
, col
);
6253 // hmmm... what could we do here to show that the cell is disabled?
6254 // for now, I just draw a thinner border than for the other ones, but
6255 // it doesn't look really good
6257 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6261 // The center of th drawn line is where the position/width/height of
6262 // the rectangle is actually at, (on wxMSW atr least,) so we will
6263 // reduce the size of the rectangle to compensate for the thickness of
6264 // the line. If this is too strange on non wxMSW platforms then
6265 // please #ifdef this appropriately.
6266 rect
.x
+= penWidth
/2;
6267 rect
.y
+= penWidth
/2;
6268 rect
.width
-= penWidth
-1;
6269 rect
.height
-= penWidth
-1;
6272 // Now draw the rectangle
6273 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6274 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6275 dc
.DrawRectangle(rect
);
6279 // VZ: my experiments with 3d borders...
6281 // how to properly set colours for arbitrary bg?
6282 wxCoord x1
= rect
.x
,
6284 x2
= rect
.x
+ rect
.width
-1,
6285 y2
= rect
.y
+ rect
.height
-1;
6287 dc
.SetPen(*wxWHITE_PEN
);
6288 dc
.DrawLine(x1
, y1
, x2
, y1
);
6289 dc
.DrawLine(x1
, y1
, x1
, y2
);
6291 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6292 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6294 dc
.SetPen(*wxBLACK_PEN
);
6295 dc
.DrawLine(x1
, y2
, x2
, y2
);
6296 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6301 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6303 int row
= coords
.GetRow();
6304 int col
= coords
.GetCol();
6305 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6308 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6310 // right hand border
6312 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6313 GetColRight(col
), GetRowBottom(row
) );
6317 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6318 GetColRight(col
), GetRowBottom(row
) );
6321 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6323 // This if block was previously in wxGrid::OnPaint but that doesn't
6324 // seem to get called under wxGTK - MB
6326 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6327 m_numRows
&& m_numCols
)
6329 m_currentCellCoords
.Set(0, 0);
6332 if ( IsCellEditControlShown() )
6334 // don't show highlight when the edit control is shown
6338 // if the active cell was repainted, repaint its highlight too because it
6339 // might have been damaged by the grid lines
6340 size_t count
= cells
.GetCount();
6341 for ( size_t n
= 0; n
< count
; n
++ )
6343 if ( cells
[n
] == m_currentCellCoords
)
6345 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6346 DrawCellHighlight(dc
, attr
);
6354 // TODO: remove this ???
6355 // This is used to redraw all grid lines e.g. when the grid line colour
6358 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6360 if ( !m_gridLinesEnabled
||
6362 !m_numCols
) return;
6364 int top
, bottom
, left
, right
;
6366 #if 0 //#ifndef __WXGTK__
6370 m_gridWin
->GetClientSize(&cw
, &ch
);
6372 // virtual coords of visible area
6374 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6375 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6380 reg
.GetBox(x
, y
, w
, h
);
6381 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6382 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6386 m_gridWin
->GetClientSize(&cw
, &ch
);
6387 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6388 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6391 // avoid drawing grid lines past the last row and col
6393 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6394 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6396 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6398 // horizontal grid lines
6401 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6403 int bot
= GetRowBottom(i
) - 1;
6412 dc
.DrawLine( left
, bot
, right
, bot
);
6417 // vertical grid lines
6419 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6421 int colRight
= GetColRight(i
) - 1;
6422 if ( colRight
> right
)
6427 if ( colRight
>= left
)
6429 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6435 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6437 if ( !m_numRows
) return;
6440 size_t numLabels
= rows
.GetCount();
6442 for ( i
= 0; i
< numLabels
; i
++ )
6444 DrawRowLabel( dc
, rows
[i
] );
6449 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6451 if ( GetRowHeight(row
) <= 0 )
6454 int rowTop
= GetRowTop(row
),
6455 rowBottom
= GetRowBottom(row
) - 1;
6457 dc
.SetPen( *wxBLACK_PEN
);
6458 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6459 m_rowLabelWidth
-1, rowBottom
);
6461 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6463 dc
.SetPen( *wxWHITE_PEN
);
6464 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6465 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6467 dc
.SetBackgroundMode( wxTRANSPARENT
);
6468 dc
.SetTextForeground( GetLabelTextColour() );
6469 dc
.SetFont( GetLabelFont() );
6472 GetRowLabelAlignment( &hAlign
, &vAlign
);
6476 rect
.SetY( GetRowTop(row
) + 2 );
6477 rect
.SetWidth( m_rowLabelWidth
- 4 );
6478 rect
.SetHeight( GetRowHeight(row
) - 4 );
6479 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6483 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6485 if ( !m_numCols
) return;
6488 size_t numLabels
= cols
.GetCount();
6490 for ( i
= 0; i
< numLabels
; i
++ )
6492 DrawColLabel( dc
, cols
[i
] );
6497 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6499 if ( GetColWidth(col
) <= 0 )
6502 int colLeft
= GetColLeft(col
),
6503 colRight
= GetColRight(col
) - 1;
6505 dc
.SetPen( *wxBLACK_PEN
);
6506 dc
.DrawLine( colRight
, 0,
6507 colRight
, m_colLabelHeight
-1 );
6509 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6510 colRight
, m_colLabelHeight
-1 );
6512 dc
.SetPen( *wxWHITE_PEN
);
6513 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6514 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6516 dc
.SetBackgroundMode( wxTRANSPARENT
);
6517 dc
.SetTextForeground( GetLabelTextColour() );
6518 dc
.SetFont( GetLabelFont() );
6520 dc
.SetBackgroundMode( wxTRANSPARENT
);
6521 dc
.SetTextForeground( GetLabelTextColour() );
6522 dc
.SetFont( GetLabelFont() );
6525 GetColLabelAlignment( &hAlign
, &vAlign
);
6528 rect
.SetX( colLeft
+ 2 );
6530 rect
.SetWidth( GetColWidth(col
) - 4 );
6531 rect
.SetHeight( m_colLabelHeight
- 4 );
6532 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6535 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6536 const wxString
& value
,
6541 wxArrayString lines
;
6543 dc
.SetClippingRegion( rect
);
6544 StringToLines( value
, lines
);
6547 //Forward to new API.
6548 DrawTextRectangle( dc
,
6556 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6557 const wxArrayString
& lines
,
6562 long textWidth
, textHeight
;
6563 long lineWidth
, lineHeight
;
6565 if ( lines
.GetCount() )
6567 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6568 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6571 switch ( horizAlign
)
6574 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6577 case wxALIGN_CENTRE
:
6578 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6587 switch ( vertAlign
)
6589 case wxALIGN_BOTTOM
:
6590 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6593 case wxALIGN_CENTRE
:
6594 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6603 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6605 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6610 dc
.DestroyClippingRegion();
6614 // Split multi line text up into an array of strings. Any existing
6615 // contents of the string array are preserved.
6617 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6621 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6622 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6624 while ( startPos
< (int)tVal
.Length() )
6626 pos
= tVal
.Mid(startPos
).Find( eol
);
6631 else if ( pos
== 0 )
6633 lines
.Add( wxEmptyString
);
6637 lines
.Add( value
.Mid(startPos
, pos
) );
6641 if ( startPos
< (int)value
.Length() )
6643 lines
.Add( value
.Mid( startPos
) );
6648 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6649 const wxArrayString
& lines
,
6650 long *width
, long *height
)
6657 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6659 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6660 w
= wxMax( w
, lineW
);
6669 // ------ Batch processing.
6671 void wxGrid::EndBatch()
6673 if ( m_batchCount
> 0 )
6676 if ( !m_batchCount
)
6679 m_rowLabelWin
->Refresh();
6680 m_colLabelWin
->Refresh();
6681 m_cornerLabelWin
->Refresh();
6682 m_gridWin
->Refresh();
6687 // Use this, rather than wxWindow::Refresh(), to force an immediate
6688 // repainting of the grid. Has no effect if you are already inside a
6689 // BeginBatch / EndBatch block.
6691 void wxGrid::ForceRefresh()
6699 // ------ Edit control functions
6703 void wxGrid::EnableEditing( bool edit
)
6705 // TODO: improve this ?
6707 if ( edit
!= m_editable
)
6709 if(!edit
) EnableCellEditControl(edit
);
6715 void wxGrid::EnableCellEditControl( bool enable
)
6720 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6721 SetCurrentCell( 0, 0 );
6723 if ( enable
!= m_cellEditCtrlEnabled
)
6727 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6730 // this should be checked by the caller!
6731 wxASSERT_MSG( CanEnableCellControl(),
6732 _T("can't enable editing for this cell!") );
6734 // do it before ShowCellEditControl()
6735 m_cellEditCtrlEnabled
= enable
;
6737 ShowCellEditControl();
6741 //FIXME:add veto support
6742 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
6744 HideCellEditControl();
6745 SaveEditControlValue();
6747 // do it after HideCellEditControl()
6748 m_cellEditCtrlEnabled
= enable
;
6753 bool wxGrid::IsCurrentCellReadOnly() const
6756 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6757 bool readonly
= attr
->IsReadOnly();
6763 bool wxGrid::CanEnableCellControl() const
6765 return m_editable
&& !IsCurrentCellReadOnly();
6768 bool wxGrid::IsCellEditControlEnabled() const
6770 // the cell edit control might be disable for all cells or just for the
6771 // current one if it's read only
6772 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6775 bool wxGrid::IsCellEditControlShown() const
6777 bool isShown
= FALSE
;
6779 if ( m_cellEditCtrlEnabled
)
6781 int row
= m_currentCellCoords
.GetRow();
6782 int col
= m_currentCellCoords
.GetCol();
6783 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6784 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6789 if ( editor
->IsCreated() )
6791 isShown
= editor
->GetControl()->IsShown();
6801 void wxGrid::ShowCellEditControl()
6803 if ( IsCellEditControlEnabled() )
6805 if ( !IsVisible( m_currentCellCoords
) )
6807 m_cellEditCtrlEnabled
= FALSE
;
6812 wxRect rect
= CellToRect( m_currentCellCoords
);
6813 int row
= m_currentCellCoords
.GetRow();
6814 int col
= m_currentCellCoords
.GetCol();
6816 // convert to scrolled coords
6818 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6820 // done in PaintBackground()
6822 // erase the highlight and the cell contents because the editor
6823 // might not cover the entire cell
6824 wxClientDC
dc( m_gridWin
);
6826 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6827 dc
.SetPen(*wxTRANSPARENT_PEN
);
6828 dc
.DrawRectangle(rect
);
6831 // cell is shifted by one pixel
6832 // However, don't allow x or y to become negative
6833 // since the SetSize() method interprets that as
6840 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6841 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6842 if ( !editor
->IsCreated() )
6844 editor
->Create(m_gridWin
, -1,
6845 new wxGridCellEditorEvtHandler(this, editor
));
6847 wxGridEditorCreatedEvent
evt(GetId(),
6848 wxEVT_GRID_EDITOR_CREATED
,
6852 editor
->GetControl());
6853 GetEventHandler()->ProcessEvent(evt
);
6856 editor
->Show( TRUE
, attr
);
6858 editor
->SetSize( rect
);
6860 editor
->BeginEdit(row
, col
, this);
6869 void wxGrid::HideCellEditControl()
6871 if ( IsCellEditControlEnabled() )
6873 int row
= m_currentCellCoords
.GetRow();
6874 int col
= m_currentCellCoords
.GetCol();
6876 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6877 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6878 editor
->Show( FALSE
);
6881 m_gridWin
->SetFocus();
6882 wxRect
rect( CellToRect( row
, col
) );
6883 m_gridWin
->Refresh( FALSE
, &rect
);
6888 void wxGrid::SaveEditControlValue()
6890 if ( IsCellEditControlEnabled() )
6892 int row
= m_currentCellCoords
.GetRow();
6893 int col
= m_currentCellCoords
.GetCol();
6895 wxString oldval
= GetCellValue(row
,col
);
6897 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6898 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6899 bool changed
= editor
->EndEdit(row
, col
, this);
6906 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
6907 m_currentCellCoords
.GetRow(),
6908 m_currentCellCoords
.GetCol() ) < 0 ) {
6910 // Event has been vetoed, set the data back.
6911 SetCellValue(row
,col
,oldval
);
6919 // ------ Grid location functions
6920 // Note that all of these functions work with the logical coordinates of
6921 // grid cells and labels so you will need to convert from device
6922 // coordinates for mouse events etc.
6925 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6927 int row
= YToRow(y
);
6928 int col
= XToCol(x
);
6930 if ( row
== -1 || col
== -1 )
6932 coords
= wxGridNoCellCoords
;
6936 coords
.Set( row
, col
);
6941 // Internal Helper function for computing row or column from some
6942 // (unscrolled) coordinate value, using either
6943 // m_defaultRowHeight/m_defaultColWidth or binary search on array
6944 // of m_rowBottoms/m_ColRights to speed up the search!
6946 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
6947 const wxArrayInt
& BorderArray
, bool maxOnOverflow
)
6951 size_t i_max
= coord
/ defaultDist
,
6953 if (BorderArray
.IsEmpty())
6958 if ( i_max
>= BorderArray
.GetCount())
6959 i_max
= BorderArray
.GetCount() - 1;
6962 if ( coord
>= BorderArray
[i_max
])
6965 i_max
= coord
/ minDist
;
6967 if ( i_max
>= BorderArray
.GetCount())
6968 i_max
= BorderArray
.GetCount() - 1;
6970 if ( coord
>= BorderArray
[i_max
])
6971 return maxOnOverflow
? (int)i_max
: -1;
6972 if ( coord
< BorderArray
[0] )
6975 while ( i_max
- i_min
> 0 )
6977 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
6978 0, _T("wxGrid: internal error in CoordToRowOrCol"));
6979 if (coord
>= BorderArray
[ i_max
- 1])
6983 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
6984 if (coord
< BorderArray
[median
])
6992 int wxGrid::YToRow( int y
)
6994 return CoordToRowOrCol(y
, m_defaultRowHeight
,
6995 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, FALSE
);
6999 int wxGrid::XToCol( int x
)
7001 return CoordToRowOrCol(x
, m_defaultColWidth
,
7002 WXGRID_MIN_COL_WIDTH
, m_colRights
, FALSE
);
7006 // return the row number that that the y coord is near the edge of, or
7007 // -1 if not near an edge
7009 int wxGrid::YToEdgeOfRow( int y
)
7012 i
= internalYToRow(y
);
7014 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7016 // We know that we are in row i, test whether we are
7017 // close enough to lower or upper border, respectively.
7018 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7020 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7028 // return the col number that that the x coord is near the edge of, or
7029 // -1 if not near an edge
7031 int wxGrid::XToEdgeOfCol( int x
)
7034 i
= internalXToCol(x
);
7036 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7038 // We know that we are in column i, test whether we are
7039 // close enough to right or left border, respectively.
7040 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7042 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7050 wxRect
wxGrid::CellToRect( int row
, int col
)
7052 wxRect
rect( -1, -1, -1, -1 );
7054 if ( row
>= 0 && row
< m_numRows
&&
7055 col
>= 0 && col
< m_numCols
)
7057 rect
.x
= GetColLeft(col
);
7058 rect
.y
= GetRowTop(row
);
7059 rect
.width
= GetColWidth(col
);
7060 rect
.height
= GetRowHeight(row
);
7063 // if grid lines are enabled, then the area of the cell is a bit smaller
7064 if (m_gridLinesEnabled
) {
7072 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7074 // get the cell rectangle in logical coords
7076 wxRect
r( CellToRect( row
, col
) );
7078 // convert to device coords
7080 int left
, top
, right
, bottom
;
7081 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7082 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7084 // check against the client area of the grid window
7087 m_gridWin
->GetClientSize( &cw
, &ch
);
7089 if ( wholeCellVisible
)
7091 // is the cell wholly visible ?
7093 return ( left
>= 0 && right
<= cw
&&
7094 top
>= 0 && bottom
<= ch
);
7098 // is the cell partly visible ?
7100 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7101 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7106 // make the specified cell location visible by doing a minimal amount
7109 void wxGrid::MakeCellVisible( int row
, int col
)
7113 int xpos
= -1, ypos
= -1;
7115 if ( row
>= 0 && row
< m_numRows
&&
7116 col
>= 0 && col
< m_numCols
)
7118 // get the cell rectangle in logical coords
7120 wxRect
r( CellToRect( row
, col
) );
7122 // convert to device coords
7124 int left
, top
, right
, bottom
;
7125 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7126 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7129 m_gridWin
->GetClientSize( &cw
, &ch
);
7135 else if ( bottom
> ch
)
7137 int h
= r
.GetHeight();
7139 for ( i
= row
-1; i
>= 0; i
-- )
7141 int rowHeight
= GetRowHeight(i
);
7142 if ( h
+ rowHeight
> ch
)
7149 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7150 // have rounding errors (this is important, because if we do, we
7151 // might not scroll at all and some cells won't be redrawn)
7153 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7155 ypos
+= GRID_SCROLL_LINE_Y
;
7162 else if ( right
> cw
)
7164 int w
= r
.GetWidth();
7166 for ( i
= col
-1; i
>= 0; i
-- )
7168 int colWidth
= GetColWidth(i
);
7169 if ( w
+ colWidth
> cw
)
7176 // see comment for ypos above
7177 xpos
+= GRID_SCROLL_LINE_X
;
7180 if ( xpos
!= -1 || ypos
!= -1 )
7183 xpos
/= GRID_SCROLL_LINE_X
;
7185 ypos
/= GRID_SCROLL_LINE_Y
;
7186 Scroll( xpos
, ypos
);
7194 // ------ Grid cursor movement functions
7197 bool wxGrid::MoveCursorUp( bool expandSelection
)
7199 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7200 m_currentCellCoords
.GetRow() >= 0 )
7202 if ( expandSelection
)
7204 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7205 m_selectingKeyboard
= m_currentCellCoords
;
7206 if ( m_selectingKeyboard
.GetRow() > 0 )
7208 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7209 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7210 m_selectingKeyboard
.GetCol() );
7211 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7214 else if ( m_currentCellCoords
.GetRow() > 0 )
7217 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7218 m_currentCellCoords
.GetCol() );
7219 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7220 m_currentCellCoords
.GetCol() );
7231 bool wxGrid::MoveCursorDown( bool expandSelection
)
7233 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7234 m_currentCellCoords
.GetRow() < m_numRows
)
7236 if ( expandSelection
)
7238 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7239 m_selectingKeyboard
= m_currentCellCoords
;
7240 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7242 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7243 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7244 m_selectingKeyboard
.GetCol() );
7245 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7248 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7251 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7252 m_currentCellCoords
.GetCol() );
7253 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7254 m_currentCellCoords
.GetCol() );
7265 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7267 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7268 m_currentCellCoords
.GetCol() >= 0 )
7270 if ( expandSelection
)
7272 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7273 m_selectingKeyboard
= m_currentCellCoords
;
7274 if ( m_selectingKeyboard
.GetCol() > 0 )
7276 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7277 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7278 m_selectingKeyboard
.GetCol() );
7279 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7282 else if ( m_currentCellCoords
.GetCol() > 0 )
7285 MakeCellVisible( m_currentCellCoords
.GetRow(),
7286 m_currentCellCoords
.GetCol() - 1 );
7287 SetCurrentCell( m_currentCellCoords
.GetRow(),
7288 m_currentCellCoords
.GetCol() - 1 );
7299 bool wxGrid::MoveCursorRight( bool expandSelection
)
7301 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7302 m_currentCellCoords
.GetCol() < m_numCols
)
7304 if ( expandSelection
)
7306 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7307 m_selectingKeyboard
= m_currentCellCoords
;
7308 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7310 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7311 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7312 m_selectingKeyboard
.GetCol() );
7313 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7316 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7319 MakeCellVisible( m_currentCellCoords
.GetRow(),
7320 m_currentCellCoords
.GetCol() + 1 );
7321 SetCurrentCell( m_currentCellCoords
.GetRow(),
7322 m_currentCellCoords
.GetCol() + 1 );
7333 bool wxGrid::MovePageUp()
7335 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7337 int row
= m_currentCellCoords
.GetRow();
7341 m_gridWin
->GetClientSize( &cw
, &ch
);
7343 int y
= GetRowTop(row
);
7344 int newRow
= YToRow( y
- ch
+ 1 );
7349 else if ( newRow
== row
)
7354 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7355 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7363 bool wxGrid::MovePageDown()
7365 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7367 int row
= m_currentCellCoords
.GetRow();
7368 if ( row
< m_numRows
)
7371 m_gridWin
->GetClientSize( &cw
, &ch
);
7373 int y
= GetRowTop(row
);
7374 int newRow
= YToRow( y
+ ch
);
7377 newRow
= m_numRows
- 1;
7379 else if ( newRow
== row
)
7384 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7385 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7393 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7396 m_currentCellCoords
!= wxGridNoCellCoords
&&
7397 m_currentCellCoords
.GetRow() > 0 )
7399 int row
= m_currentCellCoords
.GetRow();
7400 int col
= m_currentCellCoords
.GetCol();
7402 if ( m_table
->IsEmptyCell(row
, col
) )
7404 // starting in an empty cell: find the next block of
7410 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7413 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7415 // starting at the top of a block: find the next block
7421 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7426 // starting within a block: find the top of the block
7431 if ( m_table
->IsEmptyCell(row
, col
) )
7439 MakeCellVisible( row
, col
);
7440 if ( expandSelection
)
7442 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7443 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7448 SetCurrentCell( row
, col
);
7456 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7459 m_currentCellCoords
!= wxGridNoCellCoords
&&
7460 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7462 int row
= m_currentCellCoords
.GetRow();
7463 int col
= m_currentCellCoords
.GetCol();
7465 if ( m_table
->IsEmptyCell(row
, col
) )
7467 // starting in an empty cell: find the next block of
7470 while ( row
< m_numRows
-1 )
7473 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7476 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7478 // starting at the bottom of a block: find the next block
7481 while ( row
< m_numRows
-1 )
7484 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7489 // starting within a block: find the bottom of the block
7491 while ( row
< m_numRows
-1 )
7494 if ( m_table
->IsEmptyCell(row
, col
) )
7502 MakeCellVisible( row
, col
);
7503 if ( expandSelection
)
7505 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7506 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7511 SetCurrentCell( row
, col
);
7520 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7523 m_currentCellCoords
!= wxGridNoCellCoords
&&
7524 m_currentCellCoords
.GetCol() > 0 )
7526 int row
= m_currentCellCoords
.GetRow();
7527 int col
= m_currentCellCoords
.GetCol();
7529 if ( m_table
->IsEmptyCell(row
, col
) )
7531 // starting in an empty cell: find the next block of
7537 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7540 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7542 // starting at the left of a block: find the next block
7548 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7553 // starting within a block: find the left of the block
7558 if ( m_table
->IsEmptyCell(row
, col
) )
7566 MakeCellVisible( row
, col
);
7567 if ( expandSelection
)
7569 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7570 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7575 SetCurrentCell( row
, col
);
7584 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7587 m_currentCellCoords
!= wxGridNoCellCoords
&&
7588 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7590 int row
= m_currentCellCoords
.GetRow();
7591 int col
= m_currentCellCoords
.GetCol();
7593 if ( m_table
->IsEmptyCell(row
, col
) )
7595 // starting in an empty cell: find the next block of
7598 while ( col
< m_numCols
-1 )
7601 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7604 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7606 // starting at the right of a block: find the next block
7609 while ( col
< m_numCols
-1 )
7612 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7617 // starting within a block: find the right of the block
7619 while ( col
< m_numCols
-1 )
7622 if ( m_table
->IsEmptyCell(row
, col
) )
7630 MakeCellVisible( row
, col
);
7631 if ( expandSelection
)
7633 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7634 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7639 SetCurrentCell( row
, col
);
7651 // ------ Label values and formatting
7654 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7656 *horiz
= m_rowLabelHorizAlign
;
7657 *vert
= m_rowLabelVertAlign
;
7660 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7662 *horiz
= m_colLabelHorizAlign
;
7663 *vert
= m_colLabelVertAlign
;
7666 wxString
wxGrid::GetRowLabelValue( int row
)
7670 return m_table
->GetRowLabelValue( row
);
7680 wxString
wxGrid::GetColLabelValue( int col
)
7684 return m_table
->GetColLabelValue( col
);
7695 void wxGrid::SetRowLabelSize( int width
)
7697 width
= wxMax( width
, 0 );
7698 if ( width
!= m_rowLabelWidth
)
7702 m_rowLabelWin
->Show( FALSE
);
7703 m_cornerLabelWin
->Show( FALSE
);
7705 else if ( m_rowLabelWidth
== 0 )
7707 m_rowLabelWin
->Show( TRUE
);
7708 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7711 m_rowLabelWidth
= width
;
7718 void wxGrid::SetColLabelSize( int height
)
7720 height
= wxMax( height
, 0 );
7721 if ( height
!= m_colLabelHeight
)
7725 m_colLabelWin
->Show( FALSE
);
7726 m_cornerLabelWin
->Show( FALSE
);
7728 else if ( m_colLabelHeight
== 0 )
7730 m_colLabelWin
->Show( TRUE
);
7731 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7734 m_colLabelHeight
= height
;
7741 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7743 if ( m_labelBackgroundColour
!= colour
)
7745 m_labelBackgroundColour
= colour
;
7746 m_rowLabelWin
->SetBackgroundColour( colour
);
7747 m_colLabelWin
->SetBackgroundColour( colour
);
7748 m_cornerLabelWin
->SetBackgroundColour( colour
);
7750 if ( !GetBatchCount() )
7752 m_rowLabelWin
->Refresh();
7753 m_colLabelWin
->Refresh();
7754 m_cornerLabelWin
->Refresh();
7759 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7761 if ( m_labelTextColour
!= colour
)
7763 m_labelTextColour
= colour
;
7764 if ( !GetBatchCount() )
7766 m_rowLabelWin
->Refresh();
7767 m_colLabelWin
->Refresh();
7772 void wxGrid::SetLabelFont( const wxFont
& font
)
7775 if ( !GetBatchCount() )
7777 m_rowLabelWin
->Refresh();
7778 m_colLabelWin
->Refresh();
7782 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7784 // allow old (incorrect) defs to be used
7787 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7788 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7789 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7794 case wxTOP
: vert
= wxALIGN_TOP
; break;
7795 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7796 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7799 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7801 m_rowLabelHorizAlign
= horiz
;
7804 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7806 m_rowLabelVertAlign
= vert
;
7809 if ( !GetBatchCount() )
7811 m_rowLabelWin
->Refresh();
7815 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7817 // allow old (incorrect) defs to be used
7820 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7821 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7822 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7827 case wxTOP
: vert
= wxALIGN_TOP
; break;
7828 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7829 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7832 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7834 m_colLabelHorizAlign
= horiz
;
7837 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7839 m_colLabelVertAlign
= vert
;
7842 if ( !GetBatchCount() )
7844 m_colLabelWin
->Refresh();
7848 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7852 m_table
->SetRowLabelValue( row
, s
);
7853 if ( !GetBatchCount() )
7855 wxRect rect
= CellToRect( row
, 0);
7856 if ( rect
.height
> 0 )
7858 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7860 rect
.width
= m_rowLabelWidth
;
7861 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7867 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7871 m_table
->SetColLabelValue( col
, s
);
7872 if ( !GetBatchCount() )
7874 wxRect rect
= CellToRect( 0, col
);
7875 if ( rect
.width
> 0 )
7877 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7879 rect
.height
= m_colLabelHeight
;
7880 m_colLabelWin
->Refresh( TRUE
, &rect
);
7886 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7888 if ( m_gridLineColour
!= colour
)
7890 m_gridLineColour
= colour
;
7892 wxClientDC
dc( m_gridWin
);
7894 DrawAllGridLines( dc
, wxRegion() );
7899 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7901 if ( m_cellHighlightColour
!= colour
)
7903 m_cellHighlightColour
= colour
;
7905 wxClientDC
dc( m_gridWin
);
7907 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7908 DrawCellHighlight(dc
, attr
);
7913 void wxGrid::SetCellHighlightPenWidth(int width
)
7915 if (m_cellHighlightPenWidth
!= width
) {
7916 m_cellHighlightPenWidth
= width
;
7918 // Just redrawing the cell highlight is not enough since that won't
7919 // make any visible change if the the thickness is getting smaller.
7920 int row
= m_currentCellCoords
.GetRow();
7921 int col
= m_currentCellCoords
.GetCol();
7922 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7924 wxRect rect
= CellToRect(row
, col
);
7925 m_gridWin
->Refresh(TRUE
, &rect
);
7929 void wxGrid::SetCellHighlightROPenWidth(int width
)
7931 if (m_cellHighlightROPenWidth
!= width
) {
7932 m_cellHighlightROPenWidth
= width
;
7934 // Just redrawing the cell highlight is not enough since that won't
7935 // make any visible change if the the thickness is getting smaller.
7936 int row
= m_currentCellCoords
.GetRow();
7937 int col
= m_currentCellCoords
.GetCol();
7938 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7940 wxRect rect
= CellToRect(row
, col
);
7941 m_gridWin
->Refresh(TRUE
, &rect
);
7945 void wxGrid::EnableGridLines( bool enable
)
7947 if ( enable
!= m_gridLinesEnabled
)
7949 m_gridLinesEnabled
= enable
;
7951 if ( !GetBatchCount() )
7955 wxClientDC
dc( m_gridWin
);
7957 DrawAllGridLines( dc
, wxRegion() );
7961 m_gridWin
->Refresh();
7968 int wxGrid::GetDefaultRowSize()
7970 return m_defaultRowHeight
;
7973 int wxGrid::GetRowSize( int row
)
7975 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7977 return GetRowHeight(row
);
7980 int wxGrid::GetDefaultColSize()
7982 return m_defaultColWidth
;
7985 int wxGrid::GetColSize( int col
)
7987 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7989 return GetColWidth(col
);
7992 // ============================================================================
7993 // access to the grid attributes: each of them has a default value in the grid
7994 // itself and may be overidden on a per-cell basis
7995 // ============================================================================
7997 // ----------------------------------------------------------------------------
7998 // setting default attributes
7999 // ----------------------------------------------------------------------------
8001 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8003 m_defaultCellAttr
->SetBackgroundColour(col
);
8005 m_gridWin
->SetBackgroundColour(col
);
8009 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8011 m_defaultCellAttr
->SetTextColour(col
);
8014 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8016 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8019 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8021 m_defaultCellAttr
->SetFont(font
);
8024 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8026 m_defaultCellAttr
->SetRenderer(renderer
);
8029 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8031 m_defaultCellAttr
->SetEditor(editor
);
8034 // ----------------------------------------------------------------------------
8035 // access to the default attrbiutes
8036 // ----------------------------------------------------------------------------
8038 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8040 return m_defaultCellAttr
->GetBackgroundColour();
8043 wxColour
wxGrid::GetDefaultCellTextColour()
8045 return m_defaultCellAttr
->GetTextColour();
8048 wxFont
wxGrid::GetDefaultCellFont()
8050 return m_defaultCellAttr
->GetFont();
8053 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8055 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8058 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8060 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8063 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8065 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8068 // ----------------------------------------------------------------------------
8069 // access to cell attributes
8070 // ----------------------------------------------------------------------------
8072 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8074 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8075 wxColour colour
= attr
->GetBackgroundColour();
8080 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8082 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8083 wxColour colour
= attr
->GetTextColour();
8088 wxFont
wxGrid::GetCellFont( int row
, int col
)
8090 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8091 wxFont font
= attr
->GetFont();
8096 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8098 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8099 attr
->GetAlignment(horiz
, vert
);
8103 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8105 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8106 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8112 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8114 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8115 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8121 bool wxGrid::IsReadOnly(int row
, int col
) const
8123 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8124 bool isReadOnly
= attr
->IsReadOnly();
8129 // ----------------------------------------------------------------------------
8130 // attribute support: cache, automatic provider creation, ...
8131 // ----------------------------------------------------------------------------
8133 bool wxGrid::CanHaveAttributes()
8140 return m_table
->CanHaveAttributes();
8143 void wxGrid::ClearAttrCache()
8145 if ( m_attrCache
.row
!= -1 )
8147 wxSafeDecRef(m_attrCache
.attr
);
8148 m_attrCache
.attr
= NULL
;
8149 m_attrCache
.row
= -1;
8153 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8155 wxGrid
*self
= (wxGrid
*)this; // const_cast
8157 self
->ClearAttrCache();
8158 self
->m_attrCache
.row
= row
;
8159 self
->m_attrCache
.col
= col
;
8160 self
->m_attrCache
.attr
= attr
;
8164 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8166 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8168 *attr
= m_attrCache
.attr
;
8169 wxSafeIncRef(m_attrCache
.attr
);
8171 #ifdef DEBUG_ATTR_CACHE
8172 gs_nAttrCacheHits
++;
8179 #ifdef DEBUG_ATTR_CACHE
8180 gs_nAttrCacheMisses
++;
8186 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8188 wxGridCellAttr
*attr
;
8189 if ( !LookupAttr(row
, col
, &attr
) )
8191 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
8192 CacheAttr(row
, col
, attr
);
8196 attr
->SetDefAttr(m_defaultCellAttr
);
8200 attr
= m_defaultCellAttr
;
8207 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8209 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8211 wxCHECK_MSG( m_table
, attr
,
8212 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8214 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8217 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8219 // artificially inc the ref count to match DecRef() in caller
8221 m_table
->SetAttr(attr
, row
, col
);
8227 // ----------------------------------------------------------------------------
8228 // setting column attributes (wrappers around SetColAttr)
8229 // ----------------------------------------------------------------------------
8231 void wxGrid::SetColFormatBool(int col
)
8233 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8236 void wxGrid::SetColFormatNumber(int col
)
8238 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8241 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8243 wxString typeName
= wxGRID_VALUE_FLOAT
;
8244 if ( (width
!= -1) || (precision
!= -1) )
8246 typeName
<< _T(':') << width
<< _T(',') << precision
;
8249 SetColFormatCustom(col
, typeName
);
8252 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8254 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8256 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8258 attr
= new wxGridCellAttr
;
8259 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8260 attr
->SetRenderer(renderer
);
8262 SetColAttr(col
, attr
);
8266 // ----------------------------------------------------------------------------
8267 // setting cell attributes: this is forwarded to the table
8268 // ----------------------------------------------------------------------------
8270 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8272 if ( CanHaveAttributes() )
8274 m_table
->SetRowAttr(attr
, row
);
8283 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8285 if ( CanHaveAttributes() )
8287 m_table
->SetColAttr(attr
, col
);
8296 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8298 if ( CanHaveAttributes() )
8300 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8301 attr
->SetBackgroundColour(colour
);
8306 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8308 if ( CanHaveAttributes() )
8310 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8311 attr
->SetTextColour(colour
);
8316 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8318 if ( CanHaveAttributes() )
8320 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8321 attr
->SetFont(font
);
8326 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8328 if ( CanHaveAttributes() )
8330 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8331 attr
->SetAlignment(horiz
, vert
);
8336 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8338 if ( CanHaveAttributes() )
8340 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8341 attr
->SetRenderer(renderer
);
8346 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8348 if ( CanHaveAttributes() )
8350 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8351 attr
->SetEditor(editor
);
8356 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8358 if ( CanHaveAttributes() )
8360 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8361 attr
->SetReadOnly(isReadOnly
);
8366 // ----------------------------------------------------------------------------
8367 // Data type registration
8368 // ----------------------------------------------------------------------------
8370 void wxGrid::RegisterDataType(const wxString
& typeName
,
8371 wxGridCellRenderer
* renderer
,
8372 wxGridCellEditor
* editor
)
8374 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8378 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8380 wxString typeName
= m_table
->GetTypeName(row
, col
);
8381 return GetDefaultEditorForType(typeName
);
8384 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8386 wxString typeName
= m_table
->GetTypeName(row
, col
);
8387 return GetDefaultRendererForType(typeName
);
8391 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8393 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8394 if ( index
== wxNOT_FOUND
)
8396 wxFAIL_MSG(wxT("Unknown data type name"));
8401 return m_typeRegistry
->GetEditor(index
);
8405 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8407 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8408 if ( index
== wxNOT_FOUND
)
8410 wxFAIL_MSG(wxT("Unknown data type name"));
8415 return m_typeRegistry
->GetRenderer(index
);
8419 // ----------------------------------------------------------------------------
8421 // ----------------------------------------------------------------------------
8423 void wxGrid::EnableDragRowSize( bool enable
)
8425 m_canDragRowSize
= enable
;
8429 void wxGrid::EnableDragColSize( bool enable
)
8431 m_canDragColSize
= enable
;
8434 void wxGrid::EnableDragGridSize( bool enable
)
8436 m_canDragGridSize
= enable
;
8440 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8442 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8444 if ( resizeExistingRows
)
8446 // since we are resizing all rows to the default row size,
8447 // we can simply clear the row heights and row bottoms
8448 // arrays (which also allows us to take advantage of
8449 // some speed optimisations)
8450 m_rowHeights
.Empty();
8451 m_rowBottoms
.Empty();
8452 if ( !GetBatchCount() )
8457 void wxGrid::SetRowSize( int row
, int height
)
8459 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8461 if ( m_rowHeights
.IsEmpty() )
8463 // need to really create the array
8467 int h
= wxMax( 0, height
);
8468 int diff
= h
- m_rowHeights
[row
];
8470 m_rowHeights
[row
] = h
;
8472 for ( i
= row
; i
< m_numRows
; i
++ )
8474 m_rowBottoms
[i
] += diff
;
8476 if ( !GetBatchCount() )
8480 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8482 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8484 if ( resizeExistingCols
)
8486 // since we are resizing all columns to the default column size,
8487 // we can simply clear the col widths and col rights
8488 // arrays (which also allows us to take advantage of
8489 // some speed optimisations)
8490 m_colWidths
.Empty();
8491 m_colRights
.Empty();
8492 if ( !GetBatchCount() )
8497 void wxGrid::SetColSize( int col
, int width
)
8499 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8501 // should we check that it's bigger than GetColMinimalWidth(col) here?
8503 if ( m_colWidths
.IsEmpty() )
8505 // need to really create the array
8509 int w
= wxMax( 0, width
);
8510 int diff
= w
- m_colWidths
[col
];
8511 m_colWidths
[col
] = w
;
8514 for ( i
= col
; i
< m_numCols
; i
++ )
8516 m_colRights
[i
] += diff
;
8518 if ( !GetBatchCount() )
8523 void wxGrid::SetColMinimalWidth( int col
, int width
)
8525 m_colMinWidths
.Put(col
, width
);
8528 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8530 m_rowMinHeights
.Put(row
, width
);
8533 int wxGrid::GetColMinimalWidth(int col
) const
8535 long value
= m_colMinWidths
.Get(col
);
8536 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8539 int wxGrid::GetRowMinimalHeight(int row
) const
8541 long value
= m_rowMinHeights
.Get(row
);
8542 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8545 // ----------------------------------------------------------------------------
8547 // ----------------------------------------------------------------------------
8549 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8551 wxClientDC
dc(m_gridWin
);
8553 // init both of them to avoid compiler warnings, even if weo nly need one
8561 wxCoord extent
, extentMax
= 0;
8562 int max
= column
? m_numRows
: m_numCols
;
8563 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8570 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8571 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8574 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8575 extent
= column
? size
.x
: size
.y
;
8576 if ( extent
> extentMax
)
8587 // now also compare with the column label extent
8589 dc
.SetFont( GetLabelFont() );
8592 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8594 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8596 extent
= column
? w
: h
;
8597 if ( extent
> extentMax
)
8604 // empty column - give default extent (notice that if extentMax is less
8605 // than default extent but != 0, it's ok)
8606 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8612 // leave some space around text
8623 SetColSize(col
, extentMax
);
8624 if ( !GetBatchCount() )
8627 m_gridWin
->GetClientSize( &cw
, &ch
);
8628 wxRect
rect ( CellToRect( 0, col
) );
8630 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8631 rect
.width
= cw
- rect
.x
;
8632 rect
.height
= m_colLabelHeight
;
8633 m_colLabelWin
->Refresh( TRUE
, &rect
);
8638 SetRowSize(row
, extentMax
);
8639 if ( !GetBatchCount() )
8642 m_gridWin
->GetClientSize( &cw
, &ch
);
8643 wxRect
rect ( CellToRect( row
, 0 ) );
8645 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8646 rect
.width
= m_rowLabelWidth
;
8647 rect
.height
= ch
- rect
.y
;
8648 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8654 SetColMinimalWidth(col
, extentMax
);
8656 SetRowMinimalHeight(row
, extentMax
);
8660 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8662 int width
= m_rowLabelWidth
;
8667 for ( int col
= 0; col
< m_numCols
; col
++ )
8671 AutoSizeColumn(col
, setAsMin
);
8674 width
+= GetColWidth(col
);
8683 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8685 int height
= m_colLabelHeight
;
8690 for ( int row
= 0; row
< m_numRows
; row
++ )
8694 AutoSizeRow(row
, setAsMin
);
8697 height
+= GetRowHeight(row
);
8706 void wxGrid::AutoSize()
8710 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8712 // round up the size to a multiple of scroll step - this ensures that we
8713 // won't get the scrollbars if we're sized exactly to this width
8714 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
8715 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
8717 // distribute the extra space between teh columns/rows to avoid having
8718 // extra white space
8719 wxCoord diff
= sizeFit
.x
- size
.x
;
8722 // try to resize the columns uniformly
8723 wxCoord diffPerCol
= diff
/ m_numCols
;
8726 for ( int col
= 0; col
< m_numCols
; col
++ )
8728 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
8732 // add remaining amount to the last columns
8733 diff
-= diffPerCol
* m_numCols
;
8736 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
8738 SetColSize(col
, GetColWidth(col
) + 1);
8744 diff
= sizeFit
.y
- size
.y
;
8747 // try to resize the columns uniformly
8748 wxCoord diffPerRow
= diff
/ m_numRows
;
8751 for ( int row
= 0; row
< m_numRows
; row
++ )
8753 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
8757 // add remaining amount to the last rows
8758 diff
-= diffPerRow
* m_numRows
;
8761 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
8763 SetRowSize(row
, GetRowHeight(row
) + 1);
8770 SetClientSize(sizeFit
);
8773 wxSize
wxGrid::DoGetBestSize() const
8775 // don't set sizes, only calculate them
8776 wxGrid
*self
= (wxGrid
*)this; // const_cast
8778 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8779 self
->SetOrCalcRowSizes(TRUE
));
8788 wxPen
& wxGrid::GetDividerPen() const
8793 // ----------------------------------------------------------------------------
8794 // cell value accessor functions
8795 // ----------------------------------------------------------------------------
8797 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8801 m_table
->SetValue( row
, col
, s
);
8802 if ( !GetBatchCount() )
8804 wxClientDC
dc( m_gridWin
);
8806 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8809 if ( m_currentCellCoords
.GetRow() == row
&&
8810 m_currentCellCoords
.GetCol() == col
&&
8811 IsCellEditControlShown())
8812 // Note: If we are using IsCellEditControlEnabled,
8813 // this interacts badly with calling SetCellValue from
8814 // an EVT_GRID_CELL_CHANGE handler.
8816 HideCellEditControl();
8817 ShowCellEditControl(); // will reread data from table
8824 // ------ Block, row and col selection
8827 void wxGrid::SelectRow( int row
, bool addToSelected
)
8829 if ( IsSelection() && !addToSelected
)
8833 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8837 void wxGrid::SelectCol( int col
, bool addToSelected
)
8839 if ( IsSelection() && !addToSelected
)
8843 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8847 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8848 bool addToSelected
)
8850 if ( IsSelection() && !addToSelected
)
8854 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8855 FALSE
, addToSelected
);
8859 void wxGrid::SelectAll()
8861 if ( m_numRows
> 0 && m_numCols
> 0 )
8864 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8869 // ------ Cell, row and col deselection
8872 void wxGrid::DeselectRow( int row
)
8877 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8879 if ( m_selection
->IsInSelection(row
, 0 ) )
8880 m_selection
->ToggleCellSelection( row
, 0);
8884 int nCols
= GetNumberCols();
8885 for ( int i
= 0; i
< nCols
; i
++ )
8887 if ( m_selection
->IsInSelection(row
, i
) )
8888 m_selection
->ToggleCellSelection( row
, i
);
8893 void wxGrid::DeselectCol( int col
)
8898 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8900 if ( m_selection
->IsInSelection(0, col
) )
8901 m_selection
->ToggleCellSelection( 0, col
);
8905 int nRows
= GetNumberRows();
8906 for ( int i
= 0; i
< nRows
; i
++ )
8908 if ( m_selection
->IsInSelection(i
, col
) )
8909 m_selection
->ToggleCellSelection(i
, col
);
8914 void wxGrid::DeselectCell( int row
, int col
)
8916 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8917 m_selection
->ToggleCellSelection(row
, col
);
8920 bool wxGrid::IsSelection()
8922 return ( m_selection
&& (m_selection
->IsSelection() ||
8923 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8924 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
8927 bool wxGrid::IsInSelection( int row
, int col
)
8929 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8930 ( row
>= m_selectingTopLeft
.GetRow() &&
8931 col
>= m_selectingTopLeft
.GetCol() &&
8932 row
<= m_selectingBottomRight
.GetRow() &&
8933 col
<= m_selectingBottomRight
.GetCol() )) );
8936 void wxGrid::ClearSelection()
8938 m_selectingTopLeft
= wxGridNoCellCoords
;
8939 m_selectingBottomRight
= wxGridNoCellCoords
;
8941 m_selection
->ClearSelection();
8945 // This function returns the rectangle that encloses the given block
8946 // in device coords clipped to the client size of the grid window.
8948 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8949 const wxGridCellCoords
&bottomRight
)
8951 wxRect
rect( wxGridNoCellRect
);
8954 cellRect
= CellToRect( topLeft
);
8955 if ( cellRect
!= wxGridNoCellRect
)
8961 rect
= wxRect( 0, 0, 0, 0 );
8964 cellRect
= CellToRect( bottomRight
);
8965 if ( cellRect
!= wxGridNoCellRect
)
8971 return wxGridNoCellRect
;
8974 // convert to scrolled coords
8976 int left
, top
, right
, bottom
;
8977 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8978 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8981 m_gridWin
->GetClientSize( &cw
, &ch
);
8983 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8984 return wxRect( 0, 0, 0, 0);
8986 rect
.SetLeft( wxMax(0, left
) );
8987 rect
.SetTop( wxMax(0, top
) );
8988 rect
.SetRight( wxMin(cw
, right
) );
8989 rect
.SetBottom( wxMin(ch
, bottom
) );
8997 // ------ Grid event classes
9000 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9002 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9003 int row
, int col
, int x
, int y
, bool sel
,
9004 bool control
, bool shift
, bool alt
, bool meta
)
9005 : wxNotifyEvent( type
, id
)
9012 m_control
= control
;
9017 SetEventObject(obj
);
9021 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9023 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9024 int rowOrCol
, int x
, int y
,
9025 bool control
, bool shift
, bool alt
, bool meta
)
9026 : wxNotifyEvent( type
, id
)
9028 m_rowOrCol
= rowOrCol
;
9031 m_control
= control
;
9036 SetEventObject(obj
);
9040 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9042 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9043 const wxGridCellCoords
& topLeft
,
9044 const wxGridCellCoords
& bottomRight
,
9045 bool sel
, bool control
,
9046 bool shift
, bool alt
, bool meta
)
9047 : wxNotifyEvent( type
, id
)
9049 m_topLeft
= topLeft
;
9050 m_bottomRight
= bottomRight
;
9052 m_control
= control
;
9057 SetEventObject(obj
);
9061 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9063 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9064 wxObject
* obj
, int row
,
9065 int col
, wxControl
* ctrl
)
9066 : wxCommandEvent(type
, id
)
9068 SetEventObject(obj
);
9075 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9077 #endif // wxUSE_GRID