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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/dcclient.h"
38 #include "wx/settings.h"
40 #include "wx/textctrl.h"
41 #include "wx/checkbox.h"
42 #include "wx/combobox.h"
43 #include "wx/valtext.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
51 #include "wx/generic/gridsel.h"
53 #if defined(__WXMOTIF__)
54 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
56 #define WXUNUSED_MOTIF(identifier) identifier
59 #if defined(__WXGTK__)
60 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
62 #define WXUNUSED_GTK(identifier) identifier
65 // Required for wxIs... functions
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
73 class WXDLLIMPEXP_ADV
);
75 struct wxGridCellWithAttr
77 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
78 : coords(row
, col
), attr(attr_
)
87 wxGridCellCoords coords
;
91 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
92 // without rewriting the macros, which require a public copy constructor.
95 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
96 class WXDLLIMPEXP_ADV
);
98 #include "wx/arrimpl.cpp"
100 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
101 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
122 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
131 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
132 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
133 const wxPoint
&pos
, const wxSize
&size
);
138 void OnPaint( wxPaintEvent
& event
);
139 void OnMouseEvent( wxMouseEvent
& event
);
140 void OnMouseWheel( wxMouseEvent
& event
);
141 void OnKeyDown( wxKeyEvent
& event
);
142 void OnKeyUp( wxKeyEvent
& );
144 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
145 DECLARE_EVENT_TABLE()
146 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
150 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
153 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
154 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
155 const wxPoint
&pos
, const wxSize
&size
);
160 void OnPaint( wxPaintEvent
&event
);
161 void OnMouseEvent( wxMouseEvent
& event
);
162 void OnMouseWheel( wxMouseEvent
& event
);
163 void OnKeyDown( wxKeyEvent
& event
);
164 void OnKeyUp( wxKeyEvent
& );
166 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
167 DECLARE_EVENT_TABLE()
168 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
172 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
175 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
176 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
177 const wxPoint
&pos
, const wxSize
&size
);
182 void OnMouseEvent( wxMouseEvent
& event
);
183 void OnMouseWheel( wxMouseEvent
& event
);
184 void OnKeyDown( wxKeyEvent
& event
);
185 void OnKeyUp( wxKeyEvent
& );
186 void OnPaint( wxPaintEvent
& event
);
188 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
189 DECLARE_EVENT_TABLE()
190 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
193 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
198 m_owner
= (wxGrid
*)NULL
;
199 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
200 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
203 wxGridWindow( wxGrid
*parent
,
204 wxGridRowLabelWindow
*rowLblWin
,
205 wxGridColLabelWindow
*colLblWin
,
206 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
209 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
211 wxGrid
* GetOwner() { return m_owner
; }
215 wxGridRowLabelWindow
*m_rowLabelWin
;
216 wxGridColLabelWindow
*m_colLabelWin
;
218 void OnPaint( wxPaintEvent
&event
);
219 void OnMouseWheel( wxMouseEvent
& event
);
220 void OnMouseEvent( wxMouseEvent
& event
);
221 void OnKeyDown( wxKeyEvent
& );
222 void OnKeyUp( wxKeyEvent
& );
223 void OnEraseBackground( wxEraseEvent
& );
224 void OnFocus( wxFocusEvent
& );
226 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
227 DECLARE_EVENT_TABLE()
228 DECLARE_NO_COPY_CLASS(wxGridWindow
)
233 class wxGridCellEditorEvtHandler
: public wxEvtHandler
236 wxGridCellEditorEvtHandler()
237 : m_grid(0), m_editor(0)
239 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
240 : m_grid(grid
), m_editor(editor
)
243 void OnKeyDown(wxKeyEvent
& event
);
244 void OnChar(wxKeyEvent
& event
);
248 wxGridCellEditor
* m_editor
;
249 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
250 DECLARE_EVENT_TABLE()
251 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
255 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
256 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
257 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
258 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
263 // ----------------------------------------------------------------------------
264 // the internal data representation used by wxGridCellAttrProvider
265 // ----------------------------------------------------------------------------
267 // this class stores attributes set for cells
268 class WXDLLIMPEXP_ADV wxGridCellAttrData
271 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
272 wxGridCellAttr
*GetAttr(int row
, int col
) const;
273 void UpdateAttrRows( size_t pos
, int numRows
);
274 void UpdateAttrCols( size_t pos
, int numCols
);
277 // searches for the attr for given cell, returns wxNOT_FOUND if not found
278 int FindIndex(int row
, int col
) const;
280 wxGridCellWithAttrArray m_attrs
;
283 // this class stores attributes set for rows or columns
284 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
287 // empty ctor to suppress warnings
288 wxGridRowOrColAttrData() { }
289 ~wxGridRowOrColAttrData();
291 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
292 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
293 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
296 wxArrayInt m_rowsOrCols
;
297 wxArrayAttrs m_attrs
;
300 // NB: this is just a wrapper around 3 objects: one which stores cell
301 // attributes, and 2 others for row/col ones
302 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
305 wxGridCellAttrData m_cellAttrs
;
306 wxGridRowOrColAttrData m_rowAttrs
,
311 // ----------------------------------------------------------------------------
312 // data structures used for the data type registry
313 // ----------------------------------------------------------------------------
315 struct wxGridDataTypeInfo
317 wxGridDataTypeInfo(const wxString
& typeName
,
318 wxGridCellRenderer
* renderer
,
319 wxGridCellEditor
* editor
)
320 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
323 ~wxGridDataTypeInfo()
325 wxSafeDecRef(m_renderer
);
326 wxSafeDecRef(m_editor
);
330 wxGridCellRenderer
* m_renderer
;
331 wxGridCellEditor
* m_editor
;
333 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
337 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
338 class WXDLLIMPEXP_ADV
);
341 class WXDLLIMPEXP_ADV wxGridTypeRegistry
344 wxGridTypeRegistry() {}
345 ~wxGridTypeRegistry();
347 void RegisterDataType(const wxString
& typeName
,
348 wxGridCellRenderer
* renderer
,
349 wxGridCellEditor
* editor
);
351 // find one of already registered data types
352 int FindRegisteredDataType(const wxString
& typeName
);
354 // try to FindRegisteredDataType(), if this fails and typeName is one of
355 // standard typenames, register it and return its index
356 int FindDataType(const wxString
& typeName
);
358 // try to FindDataType(), if it fails see if it is not one of already
359 // registered data types with some params in which case clone the
360 // registered data type and set params for it
361 int FindOrCloneDataType(const wxString
& typeName
);
363 wxGridCellRenderer
* GetRenderer(int index
);
364 wxGridCellEditor
* GetEditor(int index
);
367 wxGridDataTypeInfoArray m_typeinfo
;
370 // ----------------------------------------------------------------------------
371 // conditional compilation
372 // ----------------------------------------------------------------------------
374 #ifndef WXGRID_DRAW_LINES
375 #define WXGRID_DRAW_LINES 1
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 //#define DEBUG_ATTR_CACHE
383 #ifdef DEBUG_ATTR_CACHE
384 static size_t gs_nAttrCacheHits
= 0;
385 static size_t gs_nAttrCacheMisses
= 0;
386 #endif // DEBUG_ATTR_CACHE
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
393 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
396 // TODO: this doesn't work at all, grid cells have different sizes and approx
397 // calculations don't work as because of the size mismatch scrollbars
398 // sometimes fail to be shown when they should be or vice versa
400 // The scroll bars may be a little flakey once in a while, but that is
401 // surely much less horrible than having scroll lines of only 1!!!
404 // Well, it's still seriously broken so it might be better but needs
407 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
408 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
410 // the size of hash tables used a bit everywhere (the max number of elements
411 // in these hash tables is the number of rows/columns)
412 static const int GRID_HASH_SIZE
= 100;
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 static inline int GetScrollX(int x
)
420 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
423 static inline int GetScrollY(int y
)
425 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
428 // ============================================================================
430 // ============================================================================
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
436 wxGridCellEditor::wxGridCellEditor()
443 wxGridCellEditor::~wxGridCellEditor()
448 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
449 wxWindowID
WXUNUSED(id
),
450 wxEvtHandler
* evtHandler
)
453 m_control
->PushEventHandler(evtHandler
);
456 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
457 wxGridCellAttr
*attr
)
459 // erase the background because we might not fill the cell
460 wxClientDC
dc(m_control
->GetParent());
461 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
463 gridWindow
->GetOwner()->PrepareDC(dc
);
465 dc
.SetPen(*wxTRANSPARENT_PEN
);
466 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
467 dc
.DrawRectangle(rectCell
);
469 // redraw the control we just painted over
470 m_control
->Refresh();
473 void wxGridCellEditor::Destroy()
477 m_control
->PopEventHandler(TRUE
/* delete it*/);
479 m_control
->Destroy();
484 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
486 wxASSERT_MSG(m_control
,
487 wxT("The wxGridCellEditor must be Created first!"));
488 m_control
->Show(show
);
492 // set the colours/fonts if we have any
495 m_colFgOld
= m_control
->GetForegroundColour();
496 m_control
->SetForegroundColour(attr
->GetTextColour());
498 m_colBgOld
= m_control
->GetBackgroundColour();
499 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
501 m_fontOld
= m_control
->GetFont();
502 m_control
->SetFont(attr
->GetFont());
504 // can't do anything more in the base class version, the other
505 // attributes may only be used by the derived classes
510 // restore the standard colours fonts
511 if ( m_colFgOld
.Ok() )
513 m_control
->SetForegroundColour(m_colFgOld
);
514 m_colFgOld
= wxNullColour
;
517 if ( m_colBgOld
.Ok() )
519 m_control
->SetBackgroundColour(m_colBgOld
);
520 m_colBgOld
= wxNullColour
;
523 if ( m_fontOld
.Ok() )
525 m_control
->SetFont(m_fontOld
);
526 m_fontOld
= wxNullFont
;
531 void wxGridCellEditor::SetSize(const wxRect
& rect
)
533 wxASSERT_MSG(m_control
,
534 wxT("The wxGridCellEditor must be Created first!"));
535 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
538 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
543 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
545 // accept the simple key presses, not anything with Ctrl/Alt/Meta
546 return !(event
.ControlDown() || event
.AltDown());
549 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
554 void wxGridCellEditor::StartingClick()
560 // ----------------------------------------------------------------------------
561 // wxGridCellTextEditor
562 // ----------------------------------------------------------------------------
564 wxGridCellTextEditor::wxGridCellTextEditor()
569 void wxGridCellTextEditor::Create(wxWindow
* parent
,
571 wxEvtHandler
* evtHandler
)
573 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
574 wxDefaultPosition
, wxDefaultSize
575 #if defined(__WXMSW__)
576 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
580 // set max length allowed in the textctrl, if the parameter was set
583 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
586 wxGridCellEditor::Create(parent
, id
, evtHandler
);
589 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
590 wxGridCellAttr
* WXUNUSED(attr
))
592 // as we fill the entire client area, don't do anything here to minimize
596 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
598 wxRect
rect(rectOrig
);
600 // Make the edit control large enough to allow for internal
603 // TODO: remove this if the text ctrl sizing is improved esp. for
606 #if defined(__WXGTK__)
615 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
617 // MB: treat MSW separately here otherwise the caret doesn't show
618 // when the editor is in the first row.
619 #if defined(__WXMSW__)
622 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
625 #if defined(__WXMOTIF__)
629 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
630 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
631 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
632 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
635 wxGridCellEditor::SetSize(rect
);
638 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
640 wxASSERT_MSG(m_control
,
641 wxT("The wxGridCellEditor must be Created first!"));
643 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
645 DoBeginEdit(m_startValue
);
648 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
650 Text()->SetValue(startValue
);
651 Text()->SetInsertionPointEnd();
652 Text()->SetSelection(-1,-1);
656 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
659 wxASSERT_MSG(m_control
,
660 wxT("The wxGridCellEditor must be Created first!"));
662 bool changed
= FALSE
;
663 wxString value
= Text()->GetValue();
664 if (value
!= m_startValue
)
668 grid
->GetTable()->SetValue(row
, col
, value
);
670 m_startValue
= wxEmptyString
;
671 // No point in setting the text of the hidden control
672 //Text()->SetValue(m_startValue);
678 void wxGridCellTextEditor::Reset()
680 wxASSERT_MSG(m_control
,
681 wxT("The wxGridCellEditor must be Created first!"));
683 DoReset(m_startValue
);
686 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
688 Text()->SetValue(startValue
);
689 Text()->SetInsertionPointEnd();
692 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
694 if ( wxGridCellEditor::IsAcceptedKey(event
) )
696 int keycode
= event
.GetKeyCode();
710 case WXK_NUMPAD_MULTIPLY
:
714 case WXK_NUMPAD_SUBTRACT
:
716 case WXK_NUMPAD_DECIMAL
:
718 case WXK_NUMPAD_DIVIDE
:
722 // accept 8 bit chars too if isprint() agrees
723 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
731 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
733 if ( !Text()->EmulateKeyPress(event
) )
739 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
740 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
742 #if defined(__WXMOTIF__) || defined(__WXGTK__)
743 // wxMotif needs a little extra help...
744 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
745 wxString
s( Text()->GetValue() );
746 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
748 Text()->SetInsertionPoint( pos
);
750 // the other ports can handle a Return key press
756 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
766 if ( !params
.ToLong(&tmp
) )
768 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
772 m_maxChars
= (size_t)tmp
;
777 // return the value in the text control
778 wxString
wxGridCellTextEditor::GetValue() const
780 return Text()->GetValue();
783 // ----------------------------------------------------------------------------
784 // wxGridCellNumberEditor
785 // ----------------------------------------------------------------------------
787 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
793 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
795 wxEvtHandler
* evtHandler
)
799 // create a spin ctrl
800 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
801 wxDefaultPosition
, wxDefaultSize
,
805 wxGridCellEditor::Create(parent
, id
, evtHandler
);
809 // just a text control
810 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
813 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
814 #endif // wxUSE_VALIDATORS
818 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
820 // first get the value
821 wxGridTableBase
*table
= grid
->GetTable();
822 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
824 m_valueOld
= table
->GetValueAsLong(row
, col
);
829 wxString sValue
= table
->GetValue(row
, col
);
830 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
832 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
839 Spin()->SetValue((int)m_valueOld
);
844 DoBeginEdit(GetString());
848 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
857 value
= Spin()->GetValue();
858 changed
= value
!= m_valueOld
;
860 text
= wxString::Format(wxT("%ld"), value
);
864 text
= Text()->GetValue();
865 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
870 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
871 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
873 grid
->GetTable()->SetValue(row
, col
, text
);
879 void wxGridCellNumberEditor::Reset()
883 Spin()->SetValue((int)m_valueOld
);
887 DoReset(GetString());
891 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
893 if ( wxGridCellEditor::IsAcceptedKey(event
) )
895 int keycode
= event
.GetKeyCode();
911 case WXK_NUMPAD_SUBTRACT
:
917 if ( (keycode
< 128) && wxIsdigit(keycode
) )
925 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
929 int keycode
= event
.GetKeyCode();
930 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
931 || keycode
== WXK_NUMPAD0
932 || keycode
== WXK_NUMPAD1
933 || keycode
== WXK_NUMPAD2
934 || keycode
== WXK_NUMPAD3
935 || keycode
== WXK_NUMPAD4
936 || keycode
== WXK_NUMPAD5
937 || keycode
== WXK_NUMPAD6
938 || keycode
== WXK_NUMPAD7
939 || keycode
== WXK_NUMPAD8
940 || keycode
== WXK_NUMPAD9
941 || keycode
== WXK_ADD
942 || keycode
== WXK_NUMPAD_ADD
943 || keycode
== WXK_SUBTRACT
944 || keycode
== WXK_NUMPAD_SUBTRACT
)
946 wxGridCellTextEditor::StartingKey(event
);
956 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
967 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
971 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
975 // skip the error message below
980 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
984 // return the value in the spin control if it is there (the text control otherwise)
985 wxString
wxGridCellNumberEditor::GetValue() const
991 long value
= Spin()->GetValue();
992 s
.Printf(wxT("%ld"), value
);
996 s
= Text()->GetValue();
1001 // ----------------------------------------------------------------------------
1002 // wxGridCellFloatEditor
1003 // ----------------------------------------------------------------------------
1005 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1008 m_precision
= precision
;
1011 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1013 wxEvtHandler
* evtHandler
)
1015 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1017 #if wxUSE_VALIDATORS
1018 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1019 #endif // wxUSE_VALIDATORS
1022 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1024 // first get the value
1025 wxGridTableBase
*table
= grid
->GetTable();
1026 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1028 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1033 wxString sValue
= table
->GetValue(row
, col
);
1034 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1036 wxFAIL_MSG( _T("this cell doesn't have float value") );
1041 DoBeginEdit(GetString());
1044 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1048 wxString
text(Text()->GetValue());
1050 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1052 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1053 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1055 grid
->GetTable()->SetValue(row
, col
, text
);
1062 void wxGridCellFloatEditor::Reset()
1064 DoReset(GetString());
1067 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1069 int keycode
= event
.GetKeyCode();
1070 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1071 || keycode
== WXK_NUMPAD0
1072 || keycode
== WXK_NUMPAD1
1073 || keycode
== WXK_NUMPAD2
1074 || keycode
== WXK_NUMPAD3
1075 || keycode
== WXK_NUMPAD4
1076 || keycode
== WXK_NUMPAD5
1077 || keycode
== WXK_NUMPAD6
1078 || keycode
== WXK_NUMPAD7
1079 || keycode
== WXK_NUMPAD8
1080 || keycode
== WXK_NUMPAD9
1081 || keycode
== WXK_ADD
1082 || keycode
== WXK_NUMPAD_ADD
1083 || keycode
== WXK_SUBTRACT
1084 || keycode
== WXK_NUMPAD_SUBTRACT
)
1086 wxGridCellTextEditor::StartingKey(event
);
1088 // skip Skip() below
1095 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1106 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1110 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1112 m_precision
= (int)tmp
;
1114 // skip the error message below
1119 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1123 wxString
wxGridCellFloatEditor::GetString() const
1126 if ( m_width
== -1 )
1128 // default width/precision
1131 else if ( m_precision
== -1 )
1133 // default precision
1134 fmt
.Printf(_T("%%%d.f"), m_width
);
1138 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1141 return wxString::Format(fmt
, m_valueOld
);
1144 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1146 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1148 int keycode
= event
.GetKeyCode();
1162 case WXK_NUMPAD_ADD
:
1164 case WXK_NUMPAD_SUBTRACT
:
1166 case WXK_NUMPAD_DECIMAL
:
1170 // additionally accept 'e' as in '1e+6'
1171 if ( (keycode
< 128) &&
1172 (wxIsdigit(keycode
) || tolower(keycode
) == 'e') )
1180 #endif // wxUSE_TEXTCTRL
1184 // ----------------------------------------------------------------------------
1185 // wxGridCellBoolEditor
1186 // ----------------------------------------------------------------------------
1188 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1190 wxEvtHandler
* evtHandler
)
1192 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1193 wxDefaultPosition
, wxDefaultSize
,
1196 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1199 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1201 bool resize
= FALSE
;
1202 wxSize size
= m_control
->GetSize();
1203 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1205 // check if the checkbox is not too big/small for this cell
1206 wxSize sizeBest
= m_control
->GetBestSize();
1207 if ( !(size
== sizeBest
) )
1209 // reset to default size if it had been made smaller
1215 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1217 // leave 1 pixel margin
1218 size
.x
= size
.y
= minSize
- 2;
1225 m_control
->SetSize(size
);
1228 // position it in the centre of the rectangle (TODO: support alignment?)
1230 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1231 // the checkbox without label still has some space to the right in wxGTK,
1232 // so shift it to the right
1234 #elif defined(__WXMSW__)
1235 // here too, but in other way
1240 int hAlign
= wxALIGN_CENTRE
;
1241 int vAlign
= wxALIGN_CENTRE
;
1243 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1246 if (hAlign
== wxALIGN_LEFT
)
1252 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1254 else if (hAlign
== wxALIGN_RIGHT
)
1256 x
= r
.x
+ r
.width
- size
.x
- 2;
1257 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1259 else if (hAlign
== wxALIGN_CENTRE
)
1261 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1262 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1265 m_control
->Move(x
, y
);
1268 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1270 m_control
->Show(show
);
1274 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1275 CBox()->SetBackgroundColour(colBg
);
1279 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1281 wxASSERT_MSG(m_control
,
1282 wxT("The wxGridCellEditor must be Created first!"));
1284 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1285 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1288 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1289 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1291 CBox()->SetValue(m_startValue
);
1295 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1298 wxASSERT_MSG(m_control
,
1299 wxT("The wxGridCellEditor must be Created first!"));
1301 bool changed
= FALSE
;
1302 bool value
= CBox()->GetValue();
1303 if ( value
!= m_startValue
)
1308 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1309 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1311 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1317 void wxGridCellBoolEditor::Reset()
1319 wxASSERT_MSG(m_control
,
1320 wxT("The wxGridCellEditor must be Created first!"));
1322 CBox()->SetValue(m_startValue
);
1325 void wxGridCellBoolEditor::StartingClick()
1327 CBox()->SetValue(!CBox()->GetValue());
1330 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1332 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1334 int keycode
= event
.GetKeyCode();
1338 case WXK_NUMPAD_MULTIPLY
:
1340 case WXK_NUMPAD_ADD
:
1342 case WXK_NUMPAD_SUBTRACT
:
1353 // return the value as "1" for true and the empty string for false
1354 wxString
wxGridCellBoolEditor::GetValue() const
1356 bool bSet
= CBox()->GetValue();
1357 return bSet
? _T("1") : wxEmptyString
;
1360 #endif // wxUSE_CHECKBOX
1364 // ----------------------------------------------------------------------------
1365 // wxGridCellChoiceEditor
1366 // ----------------------------------------------------------------------------
1368 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1370 : m_choices(choices
),
1371 m_allowOthers(allowOthers
) { }
1373 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1374 const wxString choices
[],
1376 : m_allowOthers(allowOthers
)
1380 m_choices
.Alloc(count
);
1381 for ( size_t n
= 0; n
< count
; n
++ )
1383 m_choices
.Add(choices
[n
]);
1388 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1390 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1391 editor
->m_allowOthers
= m_allowOthers
;
1392 editor
->m_choices
= m_choices
;
1397 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1399 wxEvtHandler
* evtHandler
)
1401 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1402 wxDefaultPosition
, wxDefaultSize
,
1404 m_allowOthers
? 0 : wxCB_READONLY
);
1406 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1409 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1410 wxGridCellAttr
* attr
)
1412 // as we fill the entire client area, don't do anything here to minimize
1415 // TODO: It doesn't actually fill the client area since the height of a
1416 // combo always defaults to the standard... Until someone has time to
1417 // figure out the right rectangle to paint, just do it the normal way...
1418 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1421 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1423 wxASSERT_MSG(m_control
,
1424 wxT("The wxGridCellEditor must be Created first!"));
1426 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1429 Combo()->SetValue(m_startValue
);
1432 // find the right position, or default to the first if not found
1433 int pos
= Combo()->FindString(m_startValue
);
1436 Combo()->SetSelection(pos
);
1438 Combo()->SetInsertionPointEnd();
1439 Combo()->SetFocus();
1442 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1445 wxString value
= Combo()->GetValue();
1446 bool changed
= value
!= m_startValue
;
1449 grid
->GetTable()->SetValue(row
, col
, value
);
1451 m_startValue
= wxEmptyString
;
1453 Combo()->SetValue(m_startValue
);
1455 Combo()->SetSelection(0);
1460 void wxGridCellChoiceEditor::Reset()
1462 Combo()->SetValue(m_startValue
);
1463 Combo()->SetInsertionPointEnd();
1466 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1476 wxStringTokenizer
tk(params
, _T(','));
1477 while ( tk
.HasMoreTokens() )
1479 m_choices
.Add(tk
.GetNextToken());
1483 // return the value in the text control
1484 wxString
wxGridCellChoiceEditor::GetValue() const
1486 return Combo()->GetValue();
1489 #endif // wxUSE_COMBOBOX
1491 // ----------------------------------------------------------------------------
1492 // wxGridCellEditorEvtHandler
1493 // ----------------------------------------------------------------------------
1495 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1497 switch ( event
.GetKeyCode() )
1501 m_grid
->DisableCellEditControl();
1505 m_grid
->GetEventHandler()->ProcessEvent( event
);
1509 case WXK_NUMPAD_ENTER
:
1510 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1511 m_editor
->HandleReturn(event
);
1520 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1522 switch ( event
.GetKeyCode() )
1527 case WXK_NUMPAD_ENTER
:
1535 // ----------------------------------------------------------------------------
1536 // wxGridCellWorker is an (almost) empty common base class for
1537 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1538 // ----------------------------------------------------------------------------
1540 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1545 wxGridCellWorker::~wxGridCellWorker()
1549 // ============================================================================
1551 // ============================================================================
1553 // ----------------------------------------------------------------------------
1554 // wxGridCellRenderer
1555 // ----------------------------------------------------------------------------
1557 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1558 wxGridCellAttr
& attr
,
1561 int WXUNUSED(row
), int WXUNUSED(col
),
1564 dc
.SetBackgroundMode( wxSOLID
);
1566 // grey out fields if the grid is disabled
1567 if( grid
.IsEnabled() )
1571 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1575 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1580 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1583 dc
.SetPen( *wxTRANSPARENT_PEN
);
1584 dc
.DrawRectangle(rect
);
1587 // ----------------------------------------------------------------------------
1588 // wxGridCellStringRenderer
1589 // ----------------------------------------------------------------------------
1591 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1592 wxGridCellAttr
& attr
,
1596 dc
.SetBackgroundMode( wxTRANSPARENT
);
1598 // TODO some special colours for attr.IsReadOnly() case?
1600 // different coloured text when the grid is disabled
1601 if( grid
.IsEnabled() )
1605 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1606 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1610 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1611 dc
.SetTextForeground( attr
.GetTextColour() );
1616 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1617 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1620 dc
.SetFont( attr
.GetFont() );
1623 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1625 const wxString
& text
)
1627 wxCoord x
= 0, y
= 0, max_x
= 0;
1628 dc
.SetFont(attr
.GetFont());
1629 wxStringTokenizer
tk(text
, _T('\n'));
1630 while ( tk
.HasMoreTokens() )
1632 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1633 max_x
= wxMax(max_x
, x
);
1636 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1638 return wxSize(max_x
, y
);
1641 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1642 wxGridCellAttr
& attr
,
1646 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1649 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1650 wxGridCellAttr
& attr
,
1652 const wxRect
& rectCell
,
1656 wxRect rect
= rectCell
;
1659 // erase only this cells background, overflow cells should have been erased
1660 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1663 attr
.GetAlignment(&hAlign
, &vAlign
);
1665 int overflowCols
= 0;
1667 if (attr
.GetOverflow())
1669 int cols
= grid
.GetNumberCols();
1670 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1671 int cell_rows
, cell_cols
;
1672 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1673 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1675 int i
, c_cols
, c_rows
;
1676 for (i
= col
+cell_cols
; i
< cols
; i
++)
1678 bool is_empty
= TRUE
;
1679 for (int j
=row
; j
<row
+cell_rows
; j
++)
1681 // check w/ anchor cell for multicell block
1682 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1683 if (c_rows
> 0) c_rows
= 0;
1684 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1691 rect
.width
+= grid
.GetColSize(i
);
1697 if (rect
.width
>= best_width
) break;
1699 overflowCols
= i
- col
- cell_cols
+ 1;
1700 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1703 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1705 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1707 clip
.x
+= rectCell
.width
;
1708 // draw each overflow cell individually
1709 int col_end
= col
+cell_cols
+overflowCols
;
1710 if (col_end
>= grid
.GetNumberCols())
1711 col_end
= grid
.GetNumberCols() - 1;
1712 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1714 clip
.width
= grid
.GetColSize(i
) - 1;
1715 dc
.DestroyClippingRegion();
1716 dc
.SetClippingRegion(clip
);
1718 SetTextColoursAndFont(grid
, attr
, dc
,
1719 grid
.IsInSelection(row
,i
));
1721 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1722 rect
, hAlign
, vAlign
);
1723 clip
.x
+= grid
.GetColSize(i
) - 1;
1729 dc
.DestroyClippingRegion();
1733 // now we only have to draw the text
1734 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1736 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1737 rect
, hAlign
, vAlign
);
1740 // ----------------------------------------------------------------------------
1741 // wxGridCellNumberRenderer
1742 // ----------------------------------------------------------------------------
1744 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1746 wxGridTableBase
*table
= grid
.GetTable();
1748 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1750 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1754 text
= table
->GetValue(row
, col
);
1760 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1761 wxGridCellAttr
& attr
,
1763 const wxRect
& rectCell
,
1767 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1769 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1771 // draw the text right aligned by default
1773 attr
.GetAlignment(&hAlign
, &vAlign
);
1774 hAlign
= wxALIGN_RIGHT
;
1776 wxRect rect
= rectCell
;
1779 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1782 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1783 wxGridCellAttr
& attr
,
1787 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1790 // ----------------------------------------------------------------------------
1791 // wxGridCellFloatRenderer
1792 // ----------------------------------------------------------------------------
1794 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1797 SetPrecision(precision
);
1800 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1802 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1803 renderer
->m_width
= m_width
;
1804 renderer
->m_precision
= m_precision
;
1805 renderer
->m_format
= m_format
;
1810 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1812 wxGridTableBase
*table
= grid
.GetTable();
1817 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1819 val
= table
->GetValueAsDouble(row
, col
);
1824 text
= table
->GetValue(row
, col
);
1825 hasDouble
= text
.ToDouble(&val
);
1832 if ( m_width
== -1 )
1834 if ( m_precision
== -1 )
1836 // default width/precision
1837 m_format
= _T("%f");
1841 m_format
.Printf(_T("%%.%df"), m_precision
);
1844 else if ( m_precision
== -1 )
1846 // default precision
1847 m_format
.Printf(_T("%%%d.f"), m_width
);
1851 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1855 text
.Printf(m_format
, val
);
1858 //else: text already contains the string
1863 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1864 wxGridCellAttr
& attr
,
1866 const wxRect
& rectCell
,
1870 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1872 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1874 // draw the text right aligned by default
1876 attr
.GetAlignment(&hAlign
, &vAlign
);
1877 hAlign
= wxALIGN_RIGHT
;
1879 wxRect rect
= rectCell
;
1882 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1885 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1886 wxGridCellAttr
& attr
,
1890 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1893 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1897 // reset to defaults
1903 wxString tmp
= params
.BeforeFirst(_T(','));
1907 if ( tmp
.ToLong(&width
) )
1909 SetWidth((int)width
);
1913 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1917 tmp
= params
.AfterFirst(_T(','));
1921 if ( tmp
.ToLong(&precision
) )
1923 SetPrecision((int)precision
);
1927 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1935 // ----------------------------------------------------------------------------
1936 // wxGridCellBoolRenderer
1937 // ----------------------------------------------------------------------------
1939 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1941 // FIXME these checkbox size calculations are really ugly...
1943 // between checkmark and box
1944 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1946 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1947 wxGridCellAttr
& WXUNUSED(attr
),
1952 // compute it only once (no locks for MT safeness in GUI thread...)
1953 if ( !ms_sizeCheckMark
.x
)
1955 // get checkbox size
1956 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1957 wxSize size
= checkbox
->GetBestSize();
1958 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1960 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1961 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1962 checkSize
-= size
.y
/ 2;
1967 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1970 return ms_sizeCheckMark
;
1973 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1974 wxGridCellAttr
& attr
,
1980 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1982 // draw a check mark in the centre (ignoring alignment - TODO)
1983 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1985 // don't draw outside the cell
1986 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1987 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1989 // and even leave (at least) 1 pixel margin
1990 size
.x
= size
.y
= minSize
- 2;
1993 // draw a border around checkmark
1995 attr
.GetAlignment(& hAlign
, &vAlign
);
1998 if (hAlign
== wxALIGN_CENTRE
)
2000 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2001 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2002 rectBorder
.width
= size
.x
;
2003 rectBorder
.height
= size
.y
;
2005 else if (hAlign
== wxALIGN_LEFT
)
2007 rectBorder
.x
= rect
.x
+ 2;
2008 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2009 rectBorder
.width
= size
.x
;
2010 rectBorder
.height
= size
.y
;
2012 else if (hAlign
== wxALIGN_RIGHT
)
2014 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2015 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2016 rectBorder
.width
= size
.x
;
2017 rectBorder
.height
= size
.y
;
2021 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2022 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2025 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2026 value
= !( !cellval
|| (cellval
== wxT("0")) );
2031 wxRect rectMark
= rectBorder
;
2033 // MSW DrawCheckMark() is weird (and should probably be changed...)
2034 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2038 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2041 dc
.SetTextForeground(attr
.GetTextColour());
2042 dc
.DrawCheckMark(rectMark
);
2045 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2046 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2047 dc
.DrawRectangle(rectBorder
);
2050 // ----------------------------------------------------------------------------
2052 // ----------------------------------------------------------------------------
2054 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2058 m_isReadOnly
= Unset
;
2063 m_attrkind
= wxGridCellAttr::Cell
;
2065 m_sizeRows
= m_sizeCols
= 1;
2066 m_overflow
= UnsetOverflow
;
2068 SetDefAttr(attrDefault
);
2071 wxGridCellAttr
*wxGridCellAttr::Clone() const
2073 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2075 if ( HasTextColour() )
2076 attr
->SetTextColour(GetTextColour());
2077 if ( HasBackgroundColour() )
2078 attr
->SetBackgroundColour(GetBackgroundColour());
2080 attr
->SetFont(GetFont());
2081 if ( HasAlignment() )
2082 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2084 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2088 attr
->SetRenderer(m_renderer
);
2089 m_renderer
->IncRef();
2093 attr
->SetEditor(m_editor
);
2098 attr
->SetReadOnly();
2100 attr
->SetKind( m_attrkind
);
2105 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2107 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2108 SetTextColour(mergefrom
->GetTextColour());
2109 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2110 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2111 if ( !HasFont() && mergefrom
->HasFont() )
2112 SetFont(mergefrom
->GetFont());
2113 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2115 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2116 SetAlignment(hAlign
, vAlign
);
2119 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2121 // Directly access member functions as GetRender/Editor don't just return
2122 // m_renderer/m_editor
2124 // Maybe add support for merge of Render and Editor?
2125 if (!HasRenderer() && mergefrom
->HasRenderer() )
2127 m_renderer
= mergefrom
->m_renderer
;
2128 m_renderer
->IncRef();
2130 if ( !HasEditor() && mergefrom
->HasEditor() )
2132 m_editor
= mergefrom
->m_editor
;
2135 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2136 SetReadOnly(mergefrom
->IsReadOnly());
2138 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2139 SetOverflow(mergefrom
->GetOverflow());
2141 SetDefAttr(mergefrom
->m_defGridAttr
);
2144 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2146 // The size of a cell is normally 1,1
2148 // If this cell is larger (2,2) then this is the top left cell
2149 // the other cells that will be covered (lower right cells) must be
2150 // set to negative or zero values such that
2151 // row + num_rows of the covered cell points to the larger cell (this cell)
2152 // same goes for the col + num_cols.
2154 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2156 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2157 !((num_rows
<=0)&&(num_cols
>0)) ||
2158 !((num_rows
==0)&&(num_cols
==0))),
2159 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2161 m_sizeRows
= num_rows
;
2162 m_sizeCols
= num_cols
;
2165 const wxColour
& wxGridCellAttr::GetTextColour() const
2167 if (HasTextColour())
2171 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2173 return m_defGridAttr
->GetTextColour();
2177 wxFAIL_MSG(wxT("Missing default cell attribute"));
2178 return wxNullColour
;
2183 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2185 if (HasBackgroundColour())
2187 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2188 return m_defGridAttr
->GetBackgroundColour();
2191 wxFAIL_MSG(wxT("Missing default cell attribute"));
2192 return wxNullColour
;
2197 const wxFont
& wxGridCellAttr::GetFont() const
2201 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2202 return m_defGridAttr
->GetFont();
2205 wxFAIL_MSG(wxT("Missing default cell attribute"));
2211 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2215 if ( hAlign
) *hAlign
= m_hAlign
;
2216 if ( vAlign
) *vAlign
= m_vAlign
;
2218 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2219 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2222 wxFAIL_MSG(wxT("Missing default cell attribute"));
2226 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2228 if ( num_rows
) *num_rows
= m_sizeRows
;
2229 if ( num_cols
) *num_cols
= m_sizeCols
;
2232 // GetRenderer and GetEditor use a slightly different decision path about
2233 // which attribute to use. If a non-default attr object has one then it is
2234 // used, otherwise the default editor or renderer is fetched from the grid and
2235 // used. It should be the default for the data type of the cell. If it is
2236 // NULL (because the table has a type that the grid does not have in its
2237 // registry,) then the grid's default editor or renderer is used.
2239 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2241 wxGridCellRenderer
*renderer
;
2243 if ( m_renderer
&& this != m_defGridAttr
)
2245 // use the cells renderer if it has one
2246 renderer
= m_renderer
;
2249 else // no non default cell renderer
2251 // get default renderer for the data type
2254 // GetDefaultRendererForCell() will do IncRef() for us
2255 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2264 if (m_defGridAttr
&& this != m_defGridAttr
)
2266 // if we still don't have one then use the grid default
2267 // (no need for IncRef() here neither)
2268 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2270 else // default grid attr
2272 // use m_renderer which we had decided not to use initially
2273 renderer
= m_renderer
;
2280 // we're supposed to always find something
2281 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2286 // same as above, except for s/renderer/editor/g
2287 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2289 wxGridCellEditor
*editor
;
2291 if ( m_editor
&& this != m_defGridAttr
)
2293 // use the cells editor if it has one
2297 else // no non default cell editor
2299 // get default editor for the data type
2302 // GetDefaultEditorForCell() will do IncRef() for us
2303 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2312 if ( m_defGridAttr
&& this != m_defGridAttr
)
2314 // if we still don't have one then use the grid default
2315 // (no need for IncRef() here neither)
2316 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2318 else // default grid attr
2320 // use m_editor which we had decided not to use initially
2328 // we're supposed to always find something
2329 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2334 // ----------------------------------------------------------------------------
2335 // wxGridCellAttrData
2336 // ----------------------------------------------------------------------------
2338 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2340 int n
= FindIndex(row
, col
);
2341 if ( n
== wxNOT_FOUND
)
2343 // add the attribute
2344 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2348 // free the old attribute
2349 m_attrs
[(size_t)n
].attr
->DecRef();
2353 // change the attribute
2354 m_attrs
[(size_t)n
].attr
= attr
;
2358 // remove this attribute
2359 m_attrs
.RemoveAt((size_t)n
);
2364 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2366 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2368 int n
= FindIndex(row
, col
);
2369 if ( n
!= wxNOT_FOUND
)
2371 attr
= m_attrs
[(size_t)n
].attr
;
2378 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2380 size_t count
= m_attrs
.GetCount();
2381 for ( size_t n
= 0; n
< count
; n
++ )
2383 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2384 wxCoord row
= coords
.GetRow();
2385 if ((size_t)row
>= pos
)
2389 // If rows inserted, include row counter where necessary
2390 coords
.SetRow(row
+ numRows
);
2392 else if (numRows
< 0)
2394 // If rows deleted ...
2395 if ((size_t)row
>= pos
- numRows
)
2397 // ...either decrement row counter (if row still exists)...
2398 coords
.SetRow(row
+ numRows
);
2402 // ...or remove the attribute
2403 m_attrs
.RemoveAt((size_t)n
);
2411 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2413 size_t count
= m_attrs
.GetCount();
2414 for ( size_t n
= 0; n
< count
; n
++ )
2416 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2417 wxCoord col
= coords
.GetCol();
2418 if ( (size_t)col
>= pos
)
2422 // If rows inserted, include row counter where necessary
2423 coords
.SetCol(col
+ numCols
);
2425 else if (numCols
< 0)
2427 // If rows deleted ...
2428 if ((size_t)col
>= pos
- numCols
)
2430 // ...either decrement row counter (if row still exists)...
2431 coords
.SetCol(col
+ numCols
);
2435 // ...or remove the attribute
2436 m_attrs
.RemoveAt((size_t)n
);
2444 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2446 size_t count
= m_attrs
.GetCount();
2447 for ( size_t n
= 0; n
< count
; n
++ )
2449 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2450 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2459 // ----------------------------------------------------------------------------
2460 // wxGridRowOrColAttrData
2461 // ----------------------------------------------------------------------------
2463 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2465 size_t count
= m_attrs
.Count();
2466 for ( size_t n
= 0; n
< count
; n
++ )
2468 m_attrs
[n
]->DecRef();
2472 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2474 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2476 int n
= m_rowsOrCols
.Index(rowOrCol
);
2477 if ( n
!= wxNOT_FOUND
)
2479 attr
= m_attrs
[(size_t)n
];
2486 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2488 int i
= m_rowsOrCols
.Index(rowOrCol
);
2489 if ( i
== wxNOT_FOUND
)
2491 // add the attribute
2492 m_rowsOrCols
.Add(rowOrCol
);
2497 size_t n
= (size_t)i
;
2500 // change the attribute
2501 m_attrs
[n
]->DecRef();
2506 // remove this attribute
2507 m_attrs
[n
]->DecRef();
2508 m_rowsOrCols
.RemoveAt(n
);
2509 m_attrs
.RemoveAt(n
);
2514 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2516 size_t count
= m_attrs
.GetCount();
2517 for ( size_t n
= 0; n
< count
; n
++ )
2519 int & rowOrCol
= m_rowsOrCols
[n
];
2520 if ( (size_t)rowOrCol
>= pos
)
2522 if ( numRowsOrCols
> 0 )
2524 // If rows inserted, include row counter where necessary
2525 rowOrCol
+= numRowsOrCols
;
2527 else if ( numRowsOrCols
< 0)
2529 // If rows deleted, either decrement row counter (if row still exists)
2530 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2531 rowOrCol
+= numRowsOrCols
;
2534 m_rowsOrCols
.RemoveAt((size_t)n
);
2535 m_attrs
.RemoveAt((size_t)n
);
2543 // ----------------------------------------------------------------------------
2544 // wxGridCellAttrProvider
2545 // ----------------------------------------------------------------------------
2547 wxGridCellAttrProvider::wxGridCellAttrProvider()
2549 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2552 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2557 void wxGridCellAttrProvider::InitData()
2559 m_data
= new wxGridCellAttrProviderData
;
2562 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2563 wxGridCellAttr::wxAttrKind kind
) const
2565 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2570 case (wxGridCellAttr::Any
):
2571 //Get cached merge attributes.
2572 // Currenlty not used as no cache implemented as not mutiable
2573 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2576 //Basicaly implement old version.
2577 //Also check merge cache, so we don't have to re-merge every time..
2578 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2579 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2580 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2582 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2583 // Two or move are non NULL
2584 attr
= new wxGridCellAttr
;
2585 attr
->SetKind(wxGridCellAttr::Merged
);
2589 attr
->MergeWith(attrcell
);
2593 attr
->MergeWith(attrcol
);
2597 attr
->MergeWith(attrrow
);
2600 //store merge attr if cache implemented
2602 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2606 // one or none is non null return it or null.
2607 if(attrrow
) attr
= attrrow
;
2608 if(attrcol
) attr
= attrcol
;
2609 if(attrcell
) attr
= attrcell
;
2613 case (wxGridCellAttr::Cell
):
2614 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2616 case (wxGridCellAttr::Col
):
2617 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2619 case (wxGridCellAttr::Row
):
2620 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2624 // (wxGridCellAttr::Default):
2625 // (wxGridCellAttr::Merged):
2632 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2638 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2641 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2646 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2649 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2654 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2657 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2661 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2663 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2667 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2671 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2673 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2677 // ----------------------------------------------------------------------------
2678 // wxGridTypeRegistry
2679 // ----------------------------------------------------------------------------
2681 wxGridTypeRegistry::~wxGridTypeRegistry()
2683 size_t count
= m_typeinfo
.Count();
2684 for ( size_t i
= 0; i
< count
; i
++ )
2685 delete m_typeinfo
[i
];
2689 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2690 wxGridCellRenderer
* renderer
,
2691 wxGridCellEditor
* editor
)
2693 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2695 // is it already registered?
2696 int loc
= FindRegisteredDataType(typeName
);
2697 if ( loc
!= wxNOT_FOUND
)
2699 delete m_typeinfo
[loc
];
2700 m_typeinfo
[loc
] = info
;
2704 m_typeinfo
.Add(info
);
2708 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2710 size_t count
= m_typeinfo
.GetCount();
2711 for ( size_t i
= 0; i
< count
; i
++ )
2713 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2722 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2724 int index
= FindRegisteredDataType(typeName
);
2725 if ( index
== wxNOT_FOUND
)
2727 // check whether this is one of the standard ones, in which case
2728 // register it "on the fly"
2730 if ( typeName
== wxGRID_VALUE_STRING
)
2732 RegisterDataType(wxGRID_VALUE_STRING
,
2733 new wxGridCellStringRenderer
,
2734 new wxGridCellTextEditor
);
2736 #endif // wxUSE_TEXTCTRL
2738 if ( typeName
== wxGRID_VALUE_BOOL
)
2740 RegisterDataType(wxGRID_VALUE_BOOL
,
2741 new wxGridCellBoolRenderer
,
2742 new wxGridCellBoolEditor
);
2744 #endif // wxUSE_CHECKBOX
2746 if ( typeName
== wxGRID_VALUE_NUMBER
)
2748 RegisterDataType(wxGRID_VALUE_NUMBER
,
2749 new wxGridCellNumberRenderer
,
2750 new wxGridCellNumberEditor
);
2752 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2754 RegisterDataType(wxGRID_VALUE_FLOAT
,
2755 new wxGridCellFloatRenderer
,
2756 new wxGridCellFloatEditor
);
2758 #endif // wxUSE_TEXTCTRL
2760 if ( typeName
== wxGRID_VALUE_CHOICE
)
2762 RegisterDataType(wxGRID_VALUE_CHOICE
,
2763 new wxGridCellStringRenderer
,
2764 new wxGridCellChoiceEditor
);
2766 #endif // wxUSE_COMBOBOX
2771 // we get here only if just added the entry for this type, so return
2773 index
= m_typeinfo
.GetCount() - 1;
2779 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2781 int index
= FindDataType(typeName
);
2782 if ( index
== wxNOT_FOUND
)
2784 // the first part of the typename is the "real" type, anything after ':'
2785 // are the parameters for the renderer
2786 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2787 if ( index
== wxNOT_FOUND
)
2792 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2793 wxGridCellRenderer
*rendererOld
= renderer
;
2794 renderer
= renderer
->Clone();
2795 rendererOld
->DecRef();
2797 wxGridCellEditor
*editor
= GetEditor(index
);
2798 wxGridCellEditor
*editorOld
= editor
;
2799 editor
= editor
->Clone();
2800 editorOld
->DecRef();
2802 // do it even if there are no parameters to reset them to defaults
2803 wxString params
= typeName
.AfterFirst(_T(':'));
2804 renderer
->SetParameters(params
);
2805 editor
->SetParameters(params
);
2807 // register the new typename
2808 RegisterDataType(typeName
, renderer
, editor
);
2810 // we just registered it, it's the last one
2811 index
= m_typeinfo
.GetCount() - 1;
2817 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2819 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2825 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2827 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2833 // ----------------------------------------------------------------------------
2835 // ----------------------------------------------------------------------------
2837 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2840 wxGridTableBase::wxGridTableBase()
2842 m_view
= (wxGrid
*) NULL
;
2843 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2846 wxGridTableBase::~wxGridTableBase()
2848 delete m_attrProvider
;
2851 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2853 delete m_attrProvider
;
2854 m_attrProvider
= attrProvider
;
2857 bool wxGridTableBase::CanHaveAttributes()
2859 if ( ! GetAttrProvider() )
2861 // use the default attr provider by default
2862 SetAttrProvider(new wxGridCellAttrProvider
);
2867 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2869 if ( m_attrProvider
)
2870 return m_attrProvider
->GetAttr(row
, col
, kind
);
2872 return (wxGridCellAttr
*)NULL
;
2875 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2877 if ( m_attrProvider
)
2879 attr
->SetKind(wxGridCellAttr::Cell
);
2880 m_attrProvider
->SetAttr(attr
, row
, col
);
2884 // as we take ownership of the pointer and don't store it, we must
2890 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2892 if ( m_attrProvider
)
2894 attr
->SetKind(wxGridCellAttr::Row
);
2895 m_attrProvider
->SetRowAttr(attr
, row
);
2899 // as we take ownership of the pointer and don't store it, we must
2905 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2907 if ( m_attrProvider
)
2909 attr
->SetKind(wxGridCellAttr::Col
);
2910 m_attrProvider
->SetColAttr(attr
, col
);
2914 // as we take ownership of the pointer and don't store it, we must
2920 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2921 size_t WXUNUSED(numRows
) )
2923 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2928 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2930 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2935 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2936 size_t WXUNUSED(numRows
) )
2938 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2943 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2944 size_t WXUNUSED(numCols
) )
2946 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2951 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2953 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2958 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2959 size_t WXUNUSED(numCols
) )
2961 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2967 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2970 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2971 // how much it makes sense to us geeks.
2975 wxString
wxGridTableBase::GetColLabelValue( int col
)
2977 // default col labels are:
2978 // cols 0 to 25 : A-Z
2979 // cols 26 to 675 : AA-ZZ
2984 for ( n
= 1; ; n
++ )
2986 s
+= (_T('A') + (wxChar
)( col%26
));
2988 if ( col
< 0 ) break;
2991 // reverse the string...
2993 for ( i
= 0; i
< n
; i
++ )
3002 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3004 return wxGRID_VALUE_STRING
;
3007 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3008 const wxString
& typeName
)
3010 return typeName
== wxGRID_VALUE_STRING
;
3013 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3015 return CanGetValueAs(row
, col
, typeName
);
3018 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3023 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3028 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3033 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3034 long WXUNUSED(value
) )
3038 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3039 double WXUNUSED(value
) )
3043 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3044 bool WXUNUSED(value
) )
3049 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3050 const wxString
& WXUNUSED(typeName
) )
3055 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3056 const wxString
& WXUNUSED(typeName
),
3057 void* WXUNUSED(value
) )
3061 //////////////////////////////////////////////////////////////////////
3063 // Message class for the grid table to send requests and notifications
3067 wxGridTableMessage::wxGridTableMessage()
3069 m_table
= (wxGridTableBase
*) NULL
;
3075 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3076 int commandInt1
, int commandInt2
)
3080 m_comInt1
= commandInt1
;
3081 m_comInt2
= commandInt2
;
3086 //////////////////////////////////////////////////////////////////////
3088 // A basic grid table for string data. An object of this class will
3089 // created by wxGrid if you don't specify an alternative table class.
3092 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3094 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3096 wxGridStringTable::wxGridStringTable()
3101 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3104 m_data
.Alloc( numRows
);
3107 sa
.Alloc( numCols
);
3108 sa
.Add( wxEmptyString
, numCols
);
3110 m_data
.Add( sa
, numRows
);
3113 wxGridStringTable::~wxGridStringTable()
3117 int wxGridStringTable::GetNumberRows()
3119 return m_data
.GetCount();
3122 int wxGridStringTable::GetNumberCols()
3124 if ( m_data
.GetCount() > 0 )
3125 return m_data
[0].GetCount();
3130 wxString
wxGridStringTable::GetValue( int row
, int col
)
3132 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3134 _T("invalid row or column index in wxGridStringTable") );
3136 return m_data
[row
][col
];
3139 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3141 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3142 _T("invalid row or column index in wxGridStringTable") );
3144 m_data
[row
][col
] = value
;
3147 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3149 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3151 _T("invalid row or column index in wxGridStringTable") );
3153 return (m_data
[row
][col
] == wxEmptyString
);
3156 void wxGridStringTable::Clear()
3159 int numRows
, numCols
;
3161 numRows
= m_data
.GetCount();
3164 numCols
= m_data
[0].GetCount();
3166 for ( row
= 0; row
< numRows
; row
++ )
3168 for ( col
= 0; col
< numCols
; col
++ )
3170 m_data
[row
][col
] = wxEmptyString
;
3177 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3179 size_t curNumRows
= m_data
.GetCount();
3180 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3181 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3183 if ( pos
>= curNumRows
)
3185 return AppendRows( numRows
);
3189 sa
.Alloc( curNumCols
);
3190 sa
.Add( wxEmptyString
, curNumCols
);
3191 m_data
.Insert( sa
, pos
, numRows
);
3194 wxGridTableMessage
msg( this,
3195 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3199 GetView()->ProcessTableMessage( msg
);
3205 bool wxGridStringTable::AppendRows( size_t numRows
)
3207 size_t curNumRows
= m_data
.GetCount();
3208 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3209 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3212 if ( curNumCols
> 0 )
3214 sa
.Alloc( curNumCols
);
3215 sa
.Add( wxEmptyString
, curNumCols
);
3218 m_data
.Add( sa
, numRows
);
3222 wxGridTableMessage
msg( this,
3223 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3226 GetView()->ProcessTableMessage( msg
);
3232 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3234 size_t curNumRows
= m_data
.GetCount();
3236 if ( pos
>= curNumRows
)
3238 wxFAIL_MSG( wxString::Format
3240 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3242 (unsigned long)numRows
,
3243 (unsigned long)curNumRows
3249 if ( numRows
> curNumRows
- pos
)
3251 numRows
= curNumRows
- pos
;
3254 if ( numRows
>= curNumRows
)
3260 m_data
.RemoveAt( pos
, numRows
);
3264 wxGridTableMessage
msg( this,
3265 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3269 GetView()->ProcessTableMessage( msg
);
3275 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3279 size_t curNumRows
= m_data
.GetCount();
3280 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3281 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3283 if ( pos
>= curNumCols
)
3285 return AppendCols( numCols
);
3288 for ( row
= 0; row
< curNumRows
; row
++ )
3290 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3292 m_data
[row
].Insert( wxEmptyString
, col
);
3297 wxGridTableMessage
msg( this,
3298 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3302 GetView()->ProcessTableMessage( msg
);
3308 bool wxGridStringTable::AppendCols( size_t numCols
)
3312 size_t curNumRows
= m_data
.GetCount();
3316 // TODO: something better than this ?
3318 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3323 for ( row
= 0; row
< curNumRows
; row
++ )
3325 m_data
[row
].Add( wxEmptyString
, numCols
);
3330 wxGridTableMessage
msg( this,
3331 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3334 GetView()->ProcessTableMessage( msg
);
3340 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3344 size_t curNumRows
= m_data
.GetCount();
3345 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3346 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3348 if ( pos
>= curNumCols
)
3350 wxFAIL_MSG( wxString::Format
3352 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3354 (unsigned long)numCols
,
3355 (unsigned long)curNumCols
3360 if ( numCols
> curNumCols
- pos
)
3362 numCols
= curNumCols
- pos
;
3365 for ( row
= 0; row
< curNumRows
; row
++ )
3367 if ( numCols
>= curNumCols
)
3369 m_data
[row
].Clear();
3373 m_data
[row
].RemoveAt( pos
, numCols
);
3378 wxGridTableMessage
msg( this,
3379 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3383 GetView()->ProcessTableMessage( msg
);
3389 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3391 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3393 // using default label
3395 return wxGridTableBase::GetRowLabelValue( row
);
3399 return m_rowLabels
[ row
];
3403 wxString
wxGridStringTable::GetColLabelValue( int col
)
3405 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3407 // using default label
3409 return wxGridTableBase::GetColLabelValue( col
);
3413 return m_colLabels
[ col
];
3417 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3419 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3421 int n
= m_rowLabels
.GetCount();
3423 for ( i
= n
; i
<= row
; i
++ )
3425 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3429 m_rowLabels
[row
] = value
;
3432 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3434 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3436 int n
= m_colLabels
.GetCount();
3438 for ( i
= n
; i
<= col
; i
++ )
3440 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3444 m_colLabels
[col
] = value
;
3449 //////////////////////////////////////////////////////////////////////
3450 //////////////////////////////////////////////////////////////////////
3452 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3454 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3455 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3456 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3457 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3458 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3459 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3462 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3464 const wxPoint
&pos
, const wxSize
&size
)
3465 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3470 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3474 // NO - don't do this because it will set both the x and y origin
3475 // coords to match the parent scrolled window and we just want to
3476 // set the y coord - MB
3478 // m_owner->PrepareDC( dc );
3481 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3482 dc
.SetDeviceOrigin( 0, -y
);
3484 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3485 m_owner
->DrawRowLabels( dc
, rows
);
3489 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3491 m_owner
->ProcessRowLabelMouseEvent( event
);
3495 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3497 m_owner
->GetEventHandler()->ProcessEvent(event
);
3501 // This seems to be required for wxMotif otherwise the mouse
3502 // cursor must be in the cell edit control to get key events
3504 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3506 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3509 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3511 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3516 //////////////////////////////////////////////////////////////////////
3518 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3520 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3521 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3522 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3523 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3524 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3525 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3528 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3530 const wxPoint
&pos
, const wxSize
&size
)
3531 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3536 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3540 // NO - don't do this because it will set both the x and y origin
3541 // coords to match the parent scrolled window and we just want to
3542 // set the x coord - MB
3544 // m_owner->PrepareDC( dc );
3547 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3548 dc
.SetDeviceOrigin( -x
, 0 );
3550 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3551 m_owner
->DrawColLabels( dc
, cols
);
3555 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3557 m_owner
->ProcessColLabelMouseEvent( event
);
3560 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3562 m_owner
->GetEventHandler()->ProcessEvent(event
);
3566 // This seems to be required for wxMotif otherwise the mouse
3567 // cursor must be in the cell edit control to get key events
3569 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3571 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3574 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3576 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3581 //////////////////////////////////////////////////////////////////////
3583 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3585 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3586 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3587 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3588 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3589 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3590 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3593 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3595 const wxPoint
&pos
, const wxSize
&size
)
3596 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3601 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3605 int client_height
= 0;
3606 int client_width
= 0;
3607 GetClientSize( &client_width
, &client_height
);
3609 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3610 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3611 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3612 dc
.DrawLine( 0, 0, client_width
, 0 );
3613 dc
.DrawLine( 0, 0, 0, client_height
);
3615 dc
.SetPen( *wxWHITE_PEN
);
3616 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3617 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3621 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3623 m_owner
->ProcessCornerLabelMouseEvent( event
);
3627 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3629 m_owner
->GetEventHandler()->ProcessEvent(event
);
3632 // This seems to be required for wxMotif otherwise the mouse
3633 // cursor must be in the cell edit control to get key events
3635 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3637 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3640 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3642 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3647 //////////////////////////////////////////////////////////////////////
3649 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3651 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3652 EVT_PAINT( wxGridWindow::OnPaint
)
3653 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3654 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3655 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3656 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3657 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3658 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3659 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3662 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3663 wxGridRowLabelWindow
*rowLblWin
,
3664 wxGridColLabelWindow
*colLblWin
,
3667 const wxSize
&size
)
3668 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
,
3669 wxT("grid window") )
3673 m_rowLabelWin
= rowLblWin
;
3674 m_colLabelWin
= colLblWin
;
3675 SetBackgroundColour(_T("WHITE"));
3679 wxGridWindow::~wxGridWindow()
3684 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3686 wxPaintDC
dc( this );
3687 m_owner
->PrepareDC( dc
);
3688 wxRegion reg
= GetUpdateRegion();
3689 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3690 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3691 #if WXGRID_DRAW_LINES
3692 m_owner
->DrawAllGridLines( dc
, reg
);
3694 m_owner
->DrawGridSpace( dc
);
3695 m_owner
->DrawHighlight( dc
, DirtyCells
);
3699 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3701 wxWindow::ScrollWindow( dx
, dy
, rect
);
3702 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3703 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3707 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3709 m_owner
->ProcessGridCellMouseEvent( event
);
3712 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3714 m_owner
->GetEventHandler()->ProcessEvent(event
);
3717 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3718 // cursor must be in the cell edit control to get key events
3720 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3722 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3725 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3727 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3730 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3734 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3736 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3740 //////////////////////////////////////////////////////////////////////
3742 // Internal Helper function for computing row or column from some
3743 // (unscrolled) coordinate value, using either
3744 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3745 // of m_rowBottoms/m_ColRights to speed up the search!
3747 // Internal helper macros for simpler use of that function
3749 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3750 const wxArrayInt
& BorderArray
, int nMax
,
3753 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3754 m_minAcceptableColWidth, \
3755 m_colRights, m_numCols, TRUE)
3756 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3757 m_minAcceptableRowHeight, \
3758 m_rowBottoms, m_numRows, TRUE)
3759 /////////////////////////////////////////////////////////////////////
3761 #if wxUSE_EXTENDED_RTTI
3762 WX_DEFINE_FLAGS( wxGridStyle
)
3764 wxBEGIN_FLAGS( wxGridStyle
)
3765 // new style border flags, we put them first to
3766 // use them for streaming out
3767 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3768 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3769 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3770 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3771 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3772 wxFLAGS_MEMBER(wxBORDER_NONE
)
3774 // old style border flags
3775 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3776 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3777 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3778 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3779 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3780 wxFLAGS_MEMBER(wxBORDER
)
3782 // standard window styles
3783 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3784 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3785 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3786 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3787 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3788 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3789 wxFLAGS_MEMBER(wxVSCROLL
)
3790 wxFLAGS_MEMBER(wxHSCROLL
)
3792 wxEND_FLAGS( wxGridStyle
)
3794 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3796 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3797 wxHIDE_PROPERTY( Children
)
3798 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3799 wxEND_PROPERTIES_TABLE()
3801 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3802 wxEND_HANDLERS_TABLE()
3804 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3807 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
3810 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3813 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3814 EVT_PAINT( wxGrid::OnPaint
)
3815 EVT_SIZE( wxGrid::OnSize
)
3816 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3817 EVT_KEY_UP( wxGrid::OnKeyUp
)
3818 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3823 // in order to make sure that a size event is not
3824 // trigerred in a unfinished state
3825 m_cornerLabelWin
= NULL
;
3826 m_rowLabelWin
= NULL
;
3827 m_colLabelWin
= NULL
;
3831 wxGrid::wxGrid( wxWindow
*parent
,
3836 const wxString
& name
)
3837 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3838 m_colMinWidths(GRID_HASH_SIZE
),
3839 m_rowMinHeights(GRID_HASH_SIZE
)
3844 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3845 const wxPoint
& pos
, const wxSize
& size
,
3846 long style
, const wxString
& name
)
3848 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3849 style
| wxWANTS_CHARS
, name
))
3852 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3853 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3864 // Must do this or ~wxScrollHelper will pop the wrong event handler
3865 SetTargetWindow(this);
3867 wxSafeDecRef(m_defaultCellAttr
);
3869 #ifdef DEBUG_ATTR_CACHE
3870 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3871 wxPrintf(_T("wxGrid attribute cache statistics: "
3872 "total: %u, hits: %u (%u%%)\n"),
3873 total
, gs_nAttrCacheHits
,
3874 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3880 delete m_typeRegistry
;
3886 // ----- internal init and update functions
3889 void wxGrid::Create()
3891 m_created
= FALSE
; // set to TRUE by CreateGrid
3893 m_table
= (wxGridTableBase
*) NULL
;
3896 m_cellEditCtrlEnabled
= FALSE
;
3898 m_defaultCellAttr
= new wxGridCellAttr();
3900 // Set default cell attributes
3901 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3902 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3903 m_defaultCellAttr
->SetFont(GetFont());
3904 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3905 m_defaultCellAttr
->SetTextColour(
3906 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3907 m_defaultCellAttr
->SetBackgroundColour(
3908 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3909 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3910 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3915 m_currentCellCoords
= wxGridNoCellCoords
;
3917 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3918 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3920 // create the type registry
3921 m_typeRegistry
= new wxGridTypeRegistry
;
3924 // subwindow components that make up the wxGrid
3925 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3930 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3935 m_colLabelWin
= new wxGridColLabelWindow( this,
3940 m_gridWin
= new wxGridWindow( this,
3947 SetTargetWindow( m_gridWin
);
3953 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3954 wxGrid::wxGridSelectionModes selmode
)
3956 wxCHECK_MSG( !m_created
,
3958 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3960 m_numRows
= numRows
;
3961 m_numCols
= numCols
;
3963 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3964 m_table
->SetView( this );
3966 m_selection
= new wxGridSelection( this, selmode
);
3975 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3977 wxCHECK_RET( m_created
,
3978 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3980 m_selection
->SetSelectionMode( selmode
);
3983 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3985 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3986 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3988 return m_selection
->GetSelectionMode();
3991 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3992 wxGrid::wxGridSelectionModes selmode
)
3996 // stop all processing
4001 wxGridTableBase
*t
=m_table
;
4014 m_numRows
= table
->GetNumberRows();
4015 m_numCols
= table
->GetNumberCols();
4018 m_table
->SetView( this );
4021 m_selection
= new wxGridSelection( this, selmode
);
4034 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4035 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4037 if ( m_rowLabelWin
)
4039 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4043 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4046 m_labelTextColour
= wxColour( _T("BLACK") );
4049 m_attrCache
.row
= -1;
4050 m_attrCache
.col
= -1;
4051 m_attrCache
.attr
= NULL
;
4053 // TODO: something better than this ?
4055 m_labelFont
= this->GetFont();
4056 m_labelFont
.SetWeight( wxBOLD
);
4058 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4059 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4061 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4062 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4063 m_colLabelTextOrientation
= wxHORIZONTAL
;
4065 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4066 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4068 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4069 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4071 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4072 m_defaultRowHeight
+= 8;
4074 m_defaultRowHeight
+= 4;
4077 m_gridLineColour
= wxColour( 192,192,192 );
4078 m_gridLinesEnabled
= TRUE
;
4079 m_cellHighlightColour
= *wxBLACK
;
4080 m_cellHighlightPenWidth
= 2;
4081 m_cellHighlightROPenWidth
= 1;
4083 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4084 m_winCapture
= (wxWindow
*)NULL
;
4085 m_canDragRowSize
= TRUE
;
4086 m_canDragColSize
= TRUE
;
4087 m_canDragGridSize
= TRUE
;
4089 m_dragRowOrCol
= -1;
4090 m_isDragging
= FALSE
;
4091 m_startDragPos
= wxDefaultPosition
;
4093 m_waitForSlowClick
= FALSE
;
4095 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4096 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4098 m_currentCellCoords
= wxGridNoCellCoords
;
4100 m_selectingTopLeft
= wxGridNoCellCoords
;
4101 m_selectingBottomRight
= wxGridNoCellCoords
;
4102 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4103 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4105 m_editable
= TRUE
; // default for whole grid
4107 m_inOnKeyDown
= FALSE
;
4114 // ----------------------------------------------------------------------------
4115 // the idea is to call these functions only when necessary because they create
4116 // quite big arrays which eat memory mostly unnecessary - in particular, if
4117 // default widths/heights are used for all rows/columns, we may not use these
4120 // with some extra code, it should be possible to only store the
4121 // widths/heights different from default ones but this will be done later...
4122 // ----------------------------------------------------------------------------
4124 void wxGrid::InitRowHeights()
4126 m_rowHeights
.Empty();
4127 m_rowBottoms
.Empty();
4129 m_rowHeights
.Alloc( m_numRows
);
4130 m_rowBottoms
.Alloc( m_numRows
);
4134 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4136 for ( int i
= 0; i
< m_numRows
; i
++ )
4138 rowBottom
+= m_defaultRowHeight
;
4139 m_rowBottoms
.Add( rowBottom
);
4143 void wxGrid::InitColWidths()
4145 m_colWidths
.Empty();
4146 m_colRights
.Empty();
4148 m_colWidths
.Alloc( m_numCols
);
4149 m_colRights
.Alloc( m_numCols
);
4152 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4154 for ( int i
= 0; i
< m_numCols
; i
++ )
4156 colRight
+= m_defaultColWidth
;
4157 m_colRights
.Add( colRight
);
4161 int wxGrid::GetColWidth(int col
) const
4163 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4166 int wxGrid::GetColLeft(int col
) const
4168 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4169 : m_colRights
[col
] - m_colWidths
[col
];
4172 int wxGrid::GetColRight(int col
) const
4174 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4178 int wxGrid::GetRowHeight(int row
) const
4180 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4183 int wxGrid::GetRowTop(int row
) const
4185 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4186 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4189 int wxGrid::GetRowBottom(int row
) const
4191 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4192 : m_rowBottoms
[row
];
4195 void wxGrid::CalcDimensions()
4198 GetClientSize( &cw
, &ch
);
4200 if ( m_rowLabelWin
->IsShown() )
4201 cw
-= m_rowLabelWidth
;
4202 if ( m_colLabelWin
->IsShown() )
4203 ch
-= m_colLabelHeight
;
4206 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4207 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4209 // take into account editor if shown
4210 if( IsCellEditControlShown() )
4213 int r
= m_currentCellCoords
.GetRow();
4214 int c
= m_currentCellCoords
.GetCol();
4215 int x
= GetColLeft(c
);
4216 int y
= GetRowTop(r
);
4218 // how big is the editor
4219 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4220 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4221 editor
->GetControl()->GetSize(&w2
, &h2
);
4224 if( w2
> w
) w
= w2
;
4225 if( h2
> h
) h
= h2
;
4230 // preserve (more or less) the previous position
4232 GetViewStart( &x
, &y
);
4234 // ensure the position is valid for the new scroll ranges
4236 x
= wxMax( w
- 1, 0 );
4238 y
= wxMax( h
- 1, 0 );
4240 // do set scrollbar parameters
4241 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4242 GetScrollX(w
), GetScrollY(h
), x
, y
,
4243 GetBatchCount() != 0);
4245 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4246 // still must reposition the children
4251 void wxGrid::CalcWindowSizes()
4253 // escape if the window is has not been fully created yet
4255 if ( m_cornerLabelWin
== NULL
)
4259 GetClientSize( &cw
, &ch
);
4261 if ( m_cornerLabelWin
->IsShown() )
4262 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4264 if ( m_colLabelWin
->IsShown() )
4265 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4267 if ( m_rowLabelWin
->IsShown() )
4268 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4270 if ( m_gridWin
->IsShown() )
4271 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4275 // this is called when the grid table sends a message to say that it
4276 // has been redimensioned
4278 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4281 bool result
= FALSE
;
4283 // Clear the attribute cache as the attribute might refer to a different
4284 // cell than stored in the cache after adding/removing rows/columns.
4286 // By the same reasoning, the editor should be dismissed if columns are
4287 // added or removed. And for consistency, it should IMHO always be
4288 // removed, not only if the cell "underneath" it actually changes.
4289 // For now, I intentionally do not save the editor's content as the
4290 // cell it might want to save that stuff to might no longer exist.
4291 HideCellEditControl();
4293 // if we were using the default widths/heights so far, we must change them
4295 if ( m_colWidths
.IsEmpty() )
4300 if ( m_rowHeights
.IsEmpty() )
4306 switch ( msg
.GetId() )
4308 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4310 size_t pos
= msg
.GetCommandInt();
4311 int numRows
= msg
.GetCommandInt2();
4313 m_numRows
+= numRows
;
4315 if ( !m_rowHeights
.IsEmpty() )
4317 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4318 m_rowBottoms
.Insert( 0, pos
, numRows
);
4321 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4323 for ( i
= pos
; i
< m_numRows
; i
++ )
4325 bottom
+= m_rowHeights
[i
];
4326 m_rowBottoms
[i
] = bottom
;
4329 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4331 // if we have just inserted cols into an empty grid the current
4332 // cell will be undefined...
4334 SetCurrentCell( 0, 0 );
4338 m_selection
->UpdateRows( pos
, numRows
);
4339 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4341 attrProvider
->UpdateAttrRows( pos
, numRows
);
4343 if ( !GetBatchCount() )
4346 m_rowLabelWin
->Refresh();
4352 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4354 int numRows
= msg
.GetCommandInt();
4355 int oldNumRows
= m_numRows
;
4356 m_numRows
+= numRows
;
4358 if ( !m_rowHeights
.IsEmpty() )
4360 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4361 m_rowBottoms
.Add( 0, numRows
);
4364 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4366 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4368 bottom
+= m_rowHeights
[i
];
4369 m_rowBottoms
[i
] = bottom
;
4372 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4374 // if we have just inserted cols into an empty grid the current
4375 // cell will be undefined...
4377 SetCurrentCell( 0, 0 );
4379 if ( !GetBatchCount() )
4382 m_rowLabelWin
->Refresh();
4388 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4390 size_t pos
= msg
.GetCommandInt();
4391 int numRows
= msg
.GetCommandInt2();
4392 m_numRows
-= numRows
;
4394 if ( !m_rowHeights
.IsEmpty() )
4396 m_rowHeights
.RemoveAt( pos
, numRows
);
4397 m_rowBottoms
.RemoveAt( pos
, numRows
);
4400 for ( i
= 0; i
< m_numRows
; i
++ )
4402 h
+= m_rowHeights
[i
];
4403 m_rowBottoms
[i
] = h
;
4408 m_currentCellCoords
= wxGridNoCellCoords
;
4412 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4413 m_currentCellCoords
.Set( 0, 0 );
4417 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4418 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4420 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4421 // ifdef'd out following patch from Paul Gammans
4423 // No need to touch column attributes, unless we
4424 // removed _all_ rows, in this case, we remove
4425 // all column attributes.
4426 // I hate to do this here, but the
4427 // needed data is not available inside UpdateAttrRows.
4428 if ( !GetNumberRows() )
4429 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4432 if ( !GetBatchCount() )
4435 m_rowLabelWin
->Refresh();
4441 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4443 size_t pos
= msg
.GetCommandInt();
4444 int numCols
= msg
.GetCommandInt2();
4445 m_numCols
+= numCols
;
4447 if ( !m_colWidths
.IsEmpty() )
4449 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4450 m_colRights
.Insert( 0, pos
, numCols
);
4453 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4455 for ( i
= pos
; i
< m_numCols
; i
++ )
4457 right
+= m_colWidths
[i
];
4458 m_colRights
[i
] = right
;
4461 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4463 // if we have just inserted cols into an empty grid the current
4464 // cell will be undefined...
4466 SetCurrentCell( 0, 0 );
4470 m_selection
->UpdateCols( pos
, numCols
);
4471 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4473 attrProvider
->UpdateAttrCols( pos
, numCols
);
4474 if ( !GetBatchCount() )
4477 m_colLabelWin
->Refresh();
4484 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4486 int numCols
= msg
.GetCommandInt();
4487 int oldNumCols
= m_numCols
;
4488 m_numCols
+= numCols
;
4489 if ( !m_colWidths
.IsEmpty() )
4491 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4492 m_colRights
.Add( 0, numCols
);
4495 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4497 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4499 right
+= m_colWidths
[i
];
4500 m_colRights
[i
] = right
;
4503 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4505 // if we have just inserted cols into an empty grid the current
4506 // cell will be undefined...
4508 SetCurrentCell( 0, 0 );
4510 if ( !GetBatchCount() )
4513 m_colLabelWin
->Refresh();
4519 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4521 size_t pos
= msg
.GetCommandInt();
4522 int numCols
= msg
.GetCommandInt2();
4523 m_numCols
-= numCols
;
4525 if ( !m_colWidths
.IsEmpty() )
4527 m_colWidths
.RemoveAt( pos
, numCols
);
4528 m_colRights
.RemoveAt( pos
, numCols
);
4531 for ( i
= 0; i
< m_numCols
; i
++ )
4533 w
+= m_colWidths
[i
];
4539 m_currentCellCoords
= wxGridNoCellCoords
;
4543 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4544 m_currentCellCoords
.Set( 0, 0 );
4548 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4549 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4551 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4552 // ifdef'd out following patch from Paul Gammans
4554 // No need to touch row attributes, unless we
4555 // removed _all_ columns, in this case, we remove
4556 // all row attributes.
4557 // I hate to do this here, but the
4558 // needed data is not available inside UpdateAttrCols.
4559 if ( !GetNumberCols() )
4560 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4563 if ( !GetBatchCount() )
4566 m_colLabelWin
->Refresh();
4573 if (result
&& !GetBatchCount() )
4574 m_gridWin
->Refresh();
4579 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4581 wxRegionIterator
iter( reg
);
4584 wxArrayInt rowlabels
;
4591 // TODO: remove this when we can...
4592 // There is a bug in wxMotif that gives garbage update
4593 // rectangles if you jump-scroll a long way by clicking the
4594 // scrollbar with middle button. This is a work-around
4596 #if defined(__WXMOTIF__)
4598 m_gridWin
->GetClientSize( &cw
, &ch
);
4599 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4600 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4603 // logical bounds of update region
4606 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4607 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4609 // find the row labels within these bounds
4612 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4614 if ( GetRowBottom(row
) < top
)
4617 if ( GetRowTop(row
) > bottom
)
4620 rowlabels
.Add( row
);
4630 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4632 wxRegionIterator
iter( reg
);
4635 wxArrayInt colLabels
;
4642 // TODO: remove this when we can...
4643 // There is a bug in wxMotif that gives garbage update
4644 // rectangles if you jump-scroll a long way by clicking the
4645 // scrollbar with middle button. This is a work-around
4647 #if defined(__WXMOTIF__)
4649 m_gridWin
->GetClientSize( &cw
, &ch
);
4650 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4651 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4654 // logical bounds of update region
4657 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4658 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4660 // find the cells within these bounds
4663 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4665 if ( GetColRight(col
) < left
)
4668 if ( GetColLeft(col
) > right
)
4671 colLabels
.Add( col
);
4680 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4682 wxRegionIterator
iter( reg
);
4685 wxGridCellCoordsArray cellsExposed
;
4687 int left
, top
, right
, bottom
;
4692 // TODO: remove this when we can...
4693 // There is a bug in wxMotif that gives garbage update
4694 // rectangles if you jump-scroll a long way by clicking the
4695 // scrollbar with middle button. This is a work-around
4697 #if defined(__WXMOTIF__)
4699 m_gridWin
->GetClientSize( &cw
, &ch
);
4700 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4701 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4702 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4703 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4706 // logical bounds of update region
4708 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4709 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4711 // find the cells within these bounds
4714 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4716 if ( GetRowBottom(row
) <= top
)
4719 if ( GetRowTop(row
) > bottom
)
4722 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4724 if ( GetColRight(col
) <= left
)
4727 if ( GetColLeft(col
) > right
)
4730 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4737 return cellsExposed
;
4741 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4744 wxPoint
pos( event
.GetPosition() );
4745 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4747 if ( event
.Dragging() )
4751 m_isDragging
= TRUE
;
4752 m_rowLabelWin
->CaptureMouse();
4755 if ( event
.LeftIsDown() )
4757 switch( m_cursorMode
)
4759 case WXGRID_CURSOR_RESIZE_ROW
:
4761 int cw
, ch
, left
, dummy
;
4762 m_gridWin
->GetClientSize( &cw
, &ch
);
4763 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4765 wxClientDC
dc( m_gridWin
);
4768 GetRowTop(m_dragRowOrCol
) +
4769 GetRowMinimalHeight(m_dragRowOrCol
) );
4770 dc
.SetLogicalFunction(wxINVERT
);
4771 if ( m_dragLastPos
>= 0 )
4773 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4775 dc
.DrawLine( left
, y
, left
+cw
, y
);
4780 case WXGRID_CURSOR_SELECT_ROW
:
4781 if ( (row
= YToRow( y
)) >= 0 )
4785 m_selection
->SelectRow( row
,
4786 event
.ControlDown(),
4793 // default label to suppress warnings about "enumeration value
4794 // 'xxx' not handled in switch
4802 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4807 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4808 m_isDragging
= FALSE
;
4811 // ------------ Entering or leaving the window
4813 if ( event
.Entering() || event
.Leaving() )
4815 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4819 // ------------ Left button pressed
4821 else if ( event
.LeftDown() )
4823 // don't send a label click event for a hit on the
4824 // edge of the row label - this is probably the user
4825 // wanting to resize the row
4827 if ( YToEdgeOfRow(y
) < 0 )
4831 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4833 if ( !event
.ShiftDown() && !event
.ControlDown() )
4837 if ( event
.ShiftDown() )
4839 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4842 GetNumberCols() - 1,
4843 event
.ControlDown(),
4850 m_selection
->SelectRow( row
,
4851 event
.ControlDown(),
4858 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4863 // starting to drag-resize a row
4865 if ( CanDragRowSize() )
4866 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4871 // ------------ Left double click
4873 else if (event
.LeftDClick() )
4875 int row
= YToEdgeOfRow(y
);
4880 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4882 // no default action at the moment
4887 // adjust row height depending on label text
4888 AutoSizeRowLabelSize( row
);
4890 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4896 // ------------ Left button released
4898 else if ( event
.LeftUp() )
4900 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4902 DoEndDragResizeRow();
4904 // Note: we are ending the event *after* doing
4905 // default processing in this case
4907 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4910 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4915 // ------------ Right button down
4917 else if ( event
.RightDown() )
4921 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4923 // no default action at the moment
4928 // ------------ Right double click
4930 else if ( event
.RightDClick() )
4934 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4936 // no default action at the moment
4941 // ------------ No buttons down and mouse moving
4943 else if ( event
.Moving() )
4945 m_dragRowOrCol
= YToEdgeOfRow( y
);
4946 if ( m_dragRowOrCol
>= 0 )
4948 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4950 // don't capture the mouse yet
4951 if ( CanDragRowSize() )
4952 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4955 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4957 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4963 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4966 wxPoint
pos( event
.GetPosition() );
4967 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4969 if ( event
.Dragging() )
4973 m_isDragging
= TRUE
;
4974 m_colLabelWin
->CaptureMouse();
4977 if ( event
.LeftIsDown() )
4979 switch( m_cursorMode
)
4981 case WXGRID_CURSOR_RESIZE_COL
:
4983 int cw
, ch
, dummy
, top
;
4984 m_gridWin
->GetClientSize( &cw
, &ch
);
4985 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4987 wxClientDC
dc( m_gridWin
);
4990 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4991 GetColMinimalWidth(m_dragRowOrCol
));
4992 dc
.SetLogicalFunction(wxINVERT
);
4993 if ( m_dragLastPos
>= 0 )
4995 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4997 dc
.DrawLine( x
, top
, x
, top
+ch
);
5002 case WXGRID_CURSOR_SELECT_COL
:
5003 if ( (col
= XToCol( x
)) >= 0 )
5007 m_selection
->SelectCol( col
,
5008 event
.ControlDown(),
5015 // default label to suppress warnings about "enumeration value
5016 // 'xxx' not handled in switch
5024 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5029 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5030 m_isDragging
= FALSE
;
5033 // ------------ Entering or leaving the window
5035 if ( event
.Entering() || event
.Leaving() )
5037 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5041 // ------------ Left button pressed
5043 else if ( event
.LeftDown() )
5045 // don't send a label click event for a hit on the
5046 // edge of the col label - this is probably the user
5047 // wanting to resize the col
5049 if ( XToEdgeOfCol(x
) < 0 )
5053 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5055 if ( !event
.ShiftDown() && !event
.ControlDown() )
5059 if ( event
.ShiftDown() )
5061 m_selection
->SelectBlock( 0,
5062 m_currentCellCoords
.GetCol(),
5063 GetNumberRows() - 1, col
,
5064 event
.ControlDown(),
5071 m_selection
->SelectCol( col
,
5072 event
.ControlDown(),
5079 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5084 // starting to drag-resize a col
5086 if ( CanDragColSize() )
5087 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5092 // ------------ Left double click
5094 if ( event
.LeftDClick() )
5096 int col
= XToEdgeOfCol(x
);
5101 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5103 // no default action at the moment
5108 // adjust column width depending on label text
5109 AutoSizeColLabelSize( col
);
5111 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5117 // ------------ Left button released
5119 else if ( event
.LeftUp() )
5121 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5123 DoEndDragResizeCol();
5125 // Note: we are ending the event *after* doing
5126 // default processing in this case
5128 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5131 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5136 // ------------ Right button down
5138 else if ( event
.RightDown() )
5142 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5144 // no default action at the moment
5149 // ------------ Right double click
5151 else if ( event
.RightDClick() )
5155 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5157 // no default action at the moment
5162 // ------------ No buttons down and mouse moving
5164 else if ( event
.Moving() )
5166 m_dragRowOrCol
= XToEdgeOfCol( x
);
5167 if ( m_dragRowOrCol
>= 0 )
5169 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5171 // don't capture the cursor yet
5172 if ( CanDragColSize() )
5173 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5176 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5178 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5184 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5186 if ( event
.LeftDown() )
5188 // indicate corner label by having both row and
5191 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5197 else if ( event
.LeftDClick() )
5199 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5202 else if ( event
.RightDown() )
5204 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5206 // no default action at the moment
5210 else if ( event
.RightDClick() )
5212 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5214 // no default action at the moment
5219 void wxGrid::ChangeCursorMode(CursorMode mode
,
5224 static const wxChar
*cursorModes
[] =
5233 wxLogTrace(_T("grid"),
5234 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5235 win
== m_colLabelWin
? _T("colLabelWin")
5236 : win
? _T("rowLabelWin")
5238 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5239 #endif // __WXDEBUG__
5241 if ( mode
== m_cursorMode
&&
5242 win
== m_winCapture
&&
5243 captureMouse
== (m_winCapture
!= NULL
))
5248 // by default use the grid itself
5254 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5255 m_winCapture
= (wxWindow
*)NULL
;
5258 m_cursorMode
= mode
;
5260 switch ( m_cursorMode
)
5262 case WXGRID_CURSOR_RESIZE_ROW
:
5263 win
->SetCursor( m_rowResizeCursor
);
5266 case WXGRID_CURSOR_RESIZE_COL
:
5267 win
->SetCursor( m_colResizeCursor
);
5271 win
->SetCursor( *wxSTANDARD_CURSOR
);
5274 // we need to capture mouse when resizing
5275 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5276 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5278 if ( captureMouse
&& resize
)
5280 win
->CaptureMouse();
5285 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5288 wxPoint
pos( event
.GetPosition() );
5289 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5291 wxGridCellCoords coords
;
5292 XYToCell( x
, y
, coords
);
5294 int cell_rows
, cell_cols
;
5295 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5296 if ((cell_rows
< 0) || (cell_cols
< 0))
5298 coords
.SetRow(coords
.GetRow() + cell_rows
);
5299 coords
.SetCol(coords
.GetCol() + cell_cols
);
5302 if ( event
.Dragging() )
5304 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5306 // Don't start doing anything until the mouse has been drug at
5307 // least 3 pixels in any direction...
5310 if (m_startDragPos
== wxDefaultPosition
)
5312 m_startDragPos
= pos
;
5315 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5319 m_isDragging
= TRUE
;
5320 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5322 // Hide the edit control, so it
5323 // won't interfer with drag-shrinking.
5324 if ( IsCellEditControlShown() )
5326 HideCellEditControl();
5327 SaveEditControlValue();
5330 // Have we captured the mouse yet?
5333 m_winCapture
= m_gridWin
;
5334 m_winCapture
->CaptureMouse();
5337 if ( coords
!= wxGridNoCellCoords
)
5339 if ( event
.ControlDown() )
5341 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5342 m_selectingKeyboard
= coords
;
5343 HighlightBlock ( m_selectingKeyboard
, coords
);
5347 if ( !IsSelection() )
5349 HighlightBlock( coords
, coords
);
5353 HighlightBlock( m_currentCellCoords
, coords
);
5357 if (! IsVisible(coords
))
5359 MakeCellVisible(coords
);
5360 // TODO: need to introduce a delay or something here. The
5361 // scrolling is way to fast, at least on MSW - also on GTK.
5365 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5367 int cw
, ch
, left
, dummy
;
5368 m_gridWin
->GetClientSize( &cw
, &ch
);
5369 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5371 wxClientDC
dc( m_gridWin
);
5373 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5374 GetRowMinimalHeight(m_dragRowOrCol
) );
5375 dc
.SetLogicalFunction(wxINVERT
);
5376 if ( m_dragLastPos
>= 0 )
5378 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5380 dc
.DrawLine( left
, y
, left
+cw
, y
);
5383 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5385 int cw
, ch
, dummy
, top
;
5386 m_gridWin
->GetClientSize( &cw
, &ch
);
5387 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5389 wxClientDC
dc( m_gridWin
);
5391 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5392 GetColMinimalWidth(m_dragRowOrCol
) );
5393 dc
.SetLogicalFunction(wxINVERT
);
5394 if ( m_dragLastPos
>= 0 )
5396 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5398 dc
.DrawLine( x
, top
, x
, top
+ch
);
5405 m_isDragging
= FALSE
;
5406 m_startDragPos
= wxDefaultPosition
;
5408 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5409 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5412 if ( event
.Entering() || event
.Leaving() )
5414 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5415 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5420 // ------------ Left button pressed
5422 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5424 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5429 if ( !event
.ControlDown() )
5431 if ( event
.ShiftDown() )
5435 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5436 m_currentCellCoords
.GetCol(),
5439 event
.ControlDown(),
5445 else if ( XToEdgeOfCol(x
) < 0 &&
5446 YToEdgeOfRow(y
) < 0 )
5448 DisableCellEditControl();
5449 MakeCellVisible( coords
);
5451 if ( event
.ControlDown() )
5455 m_selection
->ToggleCellSelection( coords
.GetRow(),
5457 event
.ControlDown(),
5462 m_selectingTopLeft
= wxGridNoCellCoords
;
5463 m_selectingBottomRight
= wxGridNoCellCoords
;
5464 m_selectingKeyboard
= coords
;
5468 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5469 SetCurrentCell( coords
);
5472 if ( m_selection
->GetSelectionMode() !=
5473 wxGrid::wxGridSelectCells
)
5475 HighlightBlock( coords
, coords
);
5484 // ------------ Left double click
5486 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5488 DisableCellEditControl();
5490 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5492 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5500 // ------------ Left button released
5502 else if ( event
.LeftUp() )
5504 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5508 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5509 m_winCapture
= NULL
;
5512 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5515 EnableCellEditControl();
5517 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5518 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5519 editor
->StartingClick();
5523 m_waitForSlowClick
= FALSE
;
5525 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5526 m_selectingBottomRight
!= wxGridNoCellCoords
)
5530 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5531 m_selectingTopLeft
.GetCol(),
5532 m_selectingBottomRight
.GetRow(),
5533 m_selectingBottomRight
.GetCol(),
5534 event
.ControlDown(),
5540 m_selectingTopLeft
= wxGridNoCellCoords
;
5541 m_selectingBottomRight
= wxGridNoCellCoords
;
5543 // Show the edit control, if it has been hidden for
5545 ShowCellEditControl();
5548 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5550 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5551 DoEndDragResizeRow();
5553 // Note: we are ending the event *after* doing
5554 // default processing in this case
5556 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5558 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5560 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5561 DoEndDragResizeCol();
5563 // Note: we are ending the event *after* doing
5564 // default processing in this case
5566 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5573 // ------------ Right button down
5575 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5577 DisableCellEditControl();
5578 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5583 // no default action at the moment
5588 // ------------ Right double click
5590 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5592 DisableCellEditControl();
5593 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5598 // no default action at the moment
5602 // ------------ Moving and no button action
5604 else if ( event
.Moving() && !event
.IsButton() )
5606 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5608 // out of grid cell area
5609 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5613 int dragRow
= YToEdgeOfRow( y
);
5614 int dragCol
= XToEdgeOfCol( x
);
5616 // Dragging on the corner of a cell to resize in both
5617 // directions is not implemented yet...
5619 if ( dragRow
>= 0 && dragCol
>= 0 )
5621 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5627 m_dragRowOrCol
= dragRow
;
5629 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5631 if ( CanDragRowSize() && CanDragGridSize() )
5632 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5637 m_dragRowOrCol
= dragCol
;
5645 m_dragRowOrCol
= dragCol
;
5647 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5649 if ( CanDragColSize() && CanDragGridSize() )
5650 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5656 // Neither on a row or col edge
5658 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5660 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5666 void wxGrid::DoEndDragResizeRow()
5668 if ( m_dragLastPos
>= 0 )
5670 // erase the last line and resize the row
5672 int cw
, ch
, left
, dummy
;
5673 m_gridWin
->GetClientSize( &cw
, &ch
);
5674 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5676 wxClientDC
dc( m_gridWin
);
5678 dc
.SetLogicalFunction( wxINVERT
);
5679 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5680 HideCellEditControl();
5681 SaveEditControlValue();
5683 int rowTop
= GetRowTop(m_dragRowOrCol
);
5684 SetRowSize( m_dragRowOrCol
,
5685 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5687 if ( !GetBatchCount() )
5689 // Only needed to get the correct rect.y:
5690 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5692 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5693 rect
.width
= m_rowLabelWidth
;
5694 rect
.height
= ch
- rect
.y
;
5695 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5697 // if there is a multicell block, paint all of it
5700 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5701 int leftCol
= XToCol(left
);
5702 int rightCol
= internalXToCol(left
+cw
);
5705 for (i
=leftCol
; i
<rightCol
; i
++)
5707 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5708 if (cell_rows
< subtract_rows
)
5709 subtract_rows
= cell_rows
;
5711 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5712 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5713 rect
.height
= ch
- rect
.y
;
5716 m_gridWin
->Refresh( FALSE
, &rect
);
5719 ShowCellEditControl();
5724 void wxGrid::DoEndDragResizeCol()
5726 if ( m_dragLastPos
>= 0 )
5728 // erase the last line and resize the col
5730 int cw
, ch
, dummy
, top
;
5731 m_gridWin
->GetClientSize( &cw
, &ch
);
5732 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5734 wxClientDC
dc( m_gridWin
);
5736 dc
.SetLogicalFunction( wxINVERT
);
5737 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5738 HideCellEditControl();
5739 SaveEditControlValue();
5741 int colLeft
= GetColLeft(m_dragRowOrCol
);
5742 SetColSize( m_dragRowOrCol
,
5743 wxMax( m_dragLastPos
- colLeft
,
5744 GetColMinimalWidth(m_dragRowOrCol
) ) );
5746 if ( !GetBatchCount() )
5748 // Only needed to get the correct rect.x:
5749 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5751 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5752 rect
.width
= cw
- rect
.x
;
5753 rect
.height
= m_colLabelHeight
;
5754 m_colLabelWin
->Refresh( TRUE
, &rect
);
5756 // if there is a multicell block, paint all of it
5759 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5760 int topRow
= YToRow(top
);
5761 int bottomRow
= internalYToRow(top
+cw
);
5764 for (i
=topRow
; i
<bottomRow
; i
++)
5766 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5767 if (cell_cols
< subtract_cols
)
5768 subtract_cols
= cell_cols
;
5770 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5771 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5772 rect
.width
= cw
- rect
.x
;
5775 m_gridWin
->Refresh( FALSE
, &rect
);
5778 ShowCellEditControl();
5785 // ------ interaction with data model
5787 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5789 switch ( msg
.GetId() )
5791 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5792 return GetModelValues();
5794 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5795 return SetModelValues();
5797 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5798 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5799 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5800 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5801 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5802 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5803 return Redimension( msg
);
5812 // The behaviour of this function depends on the grid table class
5813 // Clear() function. For the default wxGridStringTable class the
5814 // behavious is to replace all cell contents with wxEmptyString but
5815 // not to change the number of rows or cols.
5817 void wxGrid::ClearGrid()
5821 if (IsCellEditControlEnabled())
5822 DisableCellEditControl();
5825 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5830 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5832 // TODO: something with updateLabels flag
5836 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5842 if (IsCellEditControlEnabled())
5843 DisableCellEditControl();
5845 bool done
= m_table
->InsertRows( pos
, numRows
);
5848 // the table will have sent the results of the insert row
5849 // operation to this view object as a grid table message
5855 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5857 // TODO: something with updateLabels flag
5861 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5867 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5869 // the table will have sent the results of the append row
5870 // operation to this view object as a grid table message
5876 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5878 // TODO: something with updateLabels flag
5882 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5888 if (IsCellEditControlEnabled())
5889 DisableCellEditControl();
5891 bool done
= m_table
->DeleteRows( pos
, numRows
);
5893 // the table will have sent the results of the delete row
5894 // operation to this view object as a grid table message
5900 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5902 // TODO: something with updateLabels flag
5906 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5912 if (IsCellEditControlEnabled())
5913 DisableCellEditControl();
5915 bool done
= m_table
->InsertCols( pos
, numCols
);
5917 // the table will have sent the results of the insert col
5918 // operation to this view object as a grid table message
5924 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5926 // TODO: something with updateLabels flag
5930 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5936 bool done
= m_table
->AppendCols( numCols
);
5938 // the table will have sent the results of the append col
5939 // operation to this view object as a grid table message
5945 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5947 // TODO: something with updateLabels flag
5951 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5957 if (IsCellEditControlEnabled())
5958 DisableCellEditControl();
5960 bool done
= m_table
->DeleteCols( pos
, numCols
);
5962 // the table will have sent the results of the delete col
5963 // operation to this view object as a grid table message
5971 // ----- event handlers
5974 // Generate a grid event based on a mouse event and
5975 // return the result of ProcessEvent()
5977 int wxGrid::SendEvent( const wxEventType type
,
5979 wxMouseEvent
& mouseEv
)
5984 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5986 int rowOrCol
= (row
== -1 ? col
: row
);
5988 wxGridSizeEvent
gridEvt( GetId(),
5992 mouseEv
.GetX() + GetRowLabelSize(),
5993 mouseEv
.GetY() + GetColLabelSize(),
5994 mouseEv
.ControlDown(),
5995 mouseEv
.ShiftDown(),
5997 mouseEv
.MetaDown() );
5999 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6000 vetoed
= !gridEvt
.IsAllowed();
6002 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6004 // Right now, it should _never_ end up here!
6005 wxGridRangeSelectEvent
gridEvt( GetId(),
6009 m_selectingBottomRight
,
6011 mouseEv
.ControlDown(),
6012 mouseEv
.ShiftDown(),
6014 mouseEv
.MetaDown() );
6016 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6017 vetoed
= !gridEvt
.IsAllowed();
6021 wxGridEvent
gridEvt( GetId(),
6025 mouseEv
.GetX() + GetRowLabelSize(),
6026 mouseEv
.GetY() + GetColLabelSize(),
6028 mouseEv
.ControlDown(),
6029 mouseEv
.ShiftDown(),
6031 mouseEv
.MetaDown() );
6032 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6033 vetoed
= !gridEvt
.IsAllowed();
6036 // A Veto'd event may not be `claimed' so test this first
6037 if (vetoed
) return -1;
6038 return claimed
? 1 : 0;
6042 // Generate a grid event of specified type and return the result
6043 // of ProcessEvent().
6045 int wxGrid::SendEvent( const wxEventType type
,
6051 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6053 int rowOrCol
= (row
== -1 ? col
: row
);
6055 wxGridSizeEvent
gridEvt( GetId(),
6060 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6061 vetoed
= !gridEvt
.IsAllowed();
6065 wxGridEvent
gridEvt( GetId(),
6070 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6071 vetoed
= !gridEvt
.IsAllowed();
6074 // A Veto'd event may not be `claimed' so test this first
6075 if (vetoed
) return -1;
6076 return claimed
? 1 : 0;
6080 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6082 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6085 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6087 // Don't do anything if between Begin/EndBatch...
6088 // EndBatch() will do all this on the last nested one anyway.
6089 if (! GetBatchCount())
6091 // Refresh to get correct scrolled position:
6092 wxScrolledWindow::Refresh(eraseb
,rect
);
6096 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6097 int width_label
, width_cell
, height_label
, height_cell
;
6100 //Copy rectangle can get scroll offsets..
6101 rect_x
= rect
->GetX();
6102 rect_y
= rect
->GetY();
6103 rectWidth
= rect
->GetWidth();
6104 rectHeight
= rect
->GetHeight();
6106 width_label
= m_rowLabelWidth
- rect_x
;
6107 if (width_label
> rectWidth
) width_label
= rectWidth
;
6109 height_label
= m_colLabelHeight
- rect_y
;
6110 if (height_label
> rectHeight
) height_label
= rectHeight
;
6112 if (rect_x
> m_rowLabelWidth
)
6114 x
= rect_x
- m_rowLabelWidth
;
6115 width_cell
= rectWidth
;
6120 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6123 if (rect_y
> m_colLabelHeight
)
6125 y
= rect_y
- m_colLabelHeight
;
6126 height_cell
= rectHeight
;
6131 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6134 // Paint corner label part intersecting rect.
6135 if ( width_label
> 0 && height_label
> 0 )
6137 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6138 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6141 // Paint col labels part intersecting rect.
6142 if ( width_cell
> 0 && height_label
> 0 )
6144 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6145 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6148 // Paint row labels part intersecting rect.
6149 if ( width_label
> 0 && height_cell
> 0 )
6151 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6152 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6155 // Paint cell area part intersecting rect.
6156 if ( width_cell
> 0 && height_cell
> 0 )
6158 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6159 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6164 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6165 m_colLabelWin
->Refresh(eraseb
, NULL
);
6166 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6167 m_gridWin
->Refresh(eraseb
, NULL
);
6172 void wxGrid::OnSize( wxSizeEvent
& event
)
6174 // position the child windows
6177 // don't call CalcDimensions() from here, the base class handles the size
6183 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6185 if ( m_inOnKeyDown
)
6187 // shouldn't be here - we are going round in circles...
6189 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6192 m_inOnKeyDown
= TRUE
;
6194 // propagate the event up and see if it gets processed
6196 wxWindow
*parent
= GetParent();
6197 wxKeyEvent
keyEvt( event
);
6198 keyEvt
.SetEventObject( parent
);
6200 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6203 // try local handlers
6205 switch ( event
.GetKeyCode() )
6208 if ( event
.ControlDown() )
6210 MoveCursorUpBlock( event
.ShiftDown() );
6214 MoveCursorUp( event
.ShiftDown() );
6219 if ( event
.ControlDown() )
6221 MoveCursorDownBlock( event
.ShiftDown() );
6225 MoveCursorDown( event
.ShiftDown() );
6230 if ( event
.ControlDown() )
6232 MoveCursorLeftBlock( event
.ShiftDown() );
6236 MoveCursorLeft( event
.ShiftDown() );
6241 if ( event
.ControlDown() )
6243 MoveCursorRightBlock( event
.ShiftDown() );
6247 MoveCursorRight( event
.ShiftDown() );
6252 case WXK_NUMPAD_ENTER
:
6253 if ( event
.ControlDown() )
6255 event
.Skip(); // to let the edit control have the return
6259 if ( GetGridCursorRow() < GetNumberRows()-1 )
6261 MoveCursorDown( event
.ShiftDown() );
6265 // at the bottom of a column
6266 HideCellEditControl();
6267 SaveEditControlValue();
6277 if (event
.ShiftDown())
6279 if ( GetGridCursorCol() > 0 )
6281 MoveCursorLeft( FALSE
);
6286 HideCellEditControl();
6287 SaveEditControlValue();
6292 if ( GetGridCursorCol() < GetNumberCols()-1 )
6294 MoveCursorRight( FALSE
);
6299 HideCellEditControl();
6300 SaveEditControlValue();
6306 if ( event
.ControlDown() )
6308 MakeCellVisible( 0, 0 );
6309 SetCurrentCell( 0, 0 );
6318 if ( event
.ControlDown() )
6320 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6321 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6338 if ( event
.ControlDown() )
6342 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6343 m_currentCellCoords
.GetCol(),
6344 event
.ControlDown(),
6351 if ( !IsEditable() )
6353 MoveCursorRight( FALSE
);
6356 // Otherwise fall through to default
6359 // is it possible to edit the current cell at all?
6360 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6362 // yes, now check whether the cells editor accepts the key
6363 int row
= m_currentCellCoords
.GetRow();
6364 int col
= m_currentCellCoords
.GetCol();
6365 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6366 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6368 // <F2> is special and will always start editing, for
6369 // other keys - ask the editor itself
6370 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6371 || editor
->IsAcceptedKey(event
) )
6373 // ensure cell is visble
6374 MakeCellVisible(row
, col
);
6375 EnableCellEditControl();
6377 // a problem can arise if the cell is not completely
6378 // visible (even after calling MakeCellVisible the
6379 // control is not created and calling StartingKey will
6381 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6393 // let others process char events with modifiers or all
6394 // char events for readonly cells
6401 m_inOnKeyDown
= FALSE
;
6404 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6406 // try local handlers
6408 if ( event
.GetKeyCode() == WXK_SHIFT
)
6410 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6411 m_selectingBottomRight
!= wxGridNoCellCoords
)
6415 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6416 m_selectingTopLeft
.GetCol(),
6417 m_selectingBottomRight
.GetRow(),
6418 m_selectingBottomRight
.GetCol(),
6419 event
.ControlDown(),
6426 m_selectingTopLeft
= wxGridNoCellCoords
;
6427 m_selectingBottomRight
= wxGridNoCellCoords
;
6428 m_selectingKeyboard
= wxGridNoCellCoords
;
6432 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6436 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6438 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6440 // the event has been intercepted - do nothing
6444 wxClientDC
dc(m_gridWin
);
6447 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6449 HideCellEditControl();
6450 DisableCellEditControl();
6452 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6455 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6456 if ( !m_gridLinesEnabled
)
6464 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6466 // Otherwise refresh redraws the highlight!
6467 m_currentCellCoords
= coords
;
6469 DrawGridCellArea(dc
,cells
);
6470 DrawAllGridLines( dc
, r
);
6474 m_currentCellCoords
= coords
;
6476 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6477 DrawCellHighlight(dc
, attr
);
6482 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6485 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6489 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6492 rightCol
= GetNumberCols() - 1;
6494 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6497 bottomRow
= GetNumberRows() - 1;
6501 if ( topRow
> bottomRow
)
6508 if ( leftCol
> rightCol
)
6515 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6516 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6518 // First the case that we selected a completely new area
6519 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6520 m_selectingBottomRight
== wxGridNoCellCoords
)
6523 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6524 wxGridCellCoords ( bottomRow
, rightCol
) );
6525 m_gridWin
->Refresh( FALSE
, &rect
);
6527 // Now handle changing an existing selection area.
6528 else if ( m_selectingTopLeft
!= updateTopLeft
||
6529 m_selectingBottomRight
!= updateBottomRight
)
6531 // Compute two optimal update rectangles:
6532 // Either one rectangle is a real subset of the
6533 // other, or they are (almost) disjoint!
6535 bool need_refresh
[4];
6539 need_refresh
[3] = FALSE
;
6542 // Store intermediate values
6543 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6544 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6545 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6546 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6548 // Determine the outer/inner coordinates.
6549 if (oldLeft
> leftCol
)
6555 if (oldTop
> topRow
)
6561 if (oldRight
< rightCol
)
6564 oldRight
= rightCol
;
6567 if (oldBottom
< bottomRow
)
6570 oldBottom
= bottomRow
;
6574 // Now, either the stuff marked old is the outer
6575 // rectangle or we don't have a situation where one
6576 // is contained in the other.
6578 if ( oldLeft
< leftCol
)
6580 // Refresh the newly selected or deselected
6581 // area to the left of the old or new selection.
6582 need_refresh
[0] = TRUE
;
6583 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6585 wxGridCellCoords ( oldBottom
,
6589 if ( oldTop
< topRow
)
6591 // Refresh the newly selected or deselected
6592 // area above the old or new selection.
6593 need_refresh
[1] = TRUE
;
6594 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6596 wxGridCellCoords ( topRow
- 1,
6600 if ( oldRight
> rightCol
)
6602 // Refresh the newly selected or deselected
6603 // area to the right of the old or new selection.
6604 need_refresh
[2] = TRUE
;
6605 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6607 wxGridCellCoords ( oldBottom
,
6611 if ( oldBottom
> bottomRow
)
6613 // Refresh the newly selected or deselected
6614 // area below the old or new selection.
6615 need_refresh
[3] = TRUE
;
6616 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6618 wxGridCellCoords ( oldBottom
,
6622 // various Refresh() calls
6623 for (i
= 0; i
< 4; i
++ )
6624 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6625 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6628 m_selectingTopLeft
= updateTopLeft
;
6629 m_selectingBottomRight
= updateBottomRight
;
6633 // ------ functions to get/send data (see also public functions)
6636 bool wxGrid::GetModelValues()
6638 // Hide the editor, so it won't hide a changed value.
6639 HideCellEditControl();
6643 // all we need to do is repaint the grid
6645 m_gridWin
->Refresh();
6653 bool wxGrid::SetModelValues()
6657 // Disable the editor, so it won't hide a changed value.
6658 // Do we also want to save the current value of the editor first?
6660 DisableCellEditControl();
6664 for ( row
= 0; row
< m_numRows
; row
++ )
6666 for ( col
= 0; col
< m_numCols
; col
++ )
6668 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6680 // Note - this function only draws cells that are in the list of
6681 // exposed cells (usually set from the update region by
6682 // CalcExposedCells)
6684 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6686 if ( !m_numRows
|| !m_numCols
) return;
6688 int i
, numCells
= cells
.GetCount();
6689 int row
, col
, cell_rows
, cell_cols
;
6690 wxGridCellCoordsArray redrawCells
;
6692 for ( i
= numCells
-1; i
>= 0; i
-- )
6694 row
= cells
[i
].GetRow();
6695 col
= cells
[i
].GetCol();
6696 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6698 // If this cell is part of a multicell block, find owner for repaint
6699 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6701 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6702 bool marked
= FALSE
;
6703 for ( int j
= 0; j
< numCells
; j
++ )
6705 if ( cell
== cells
[j
] )
6713 int count
= redrawCells
.GetCount();
6714 for (int j
= 0; j
< count
; j
++)
6716 if ( cell
== redrawCells
[j
] )
6722 if (!marked
) redrawCells
.Add( cell
);
6724 continue; // don't bother drawing this cell
6727 // If this cell is empty, find cell to left that might want to overflow
6728 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6730 for ( int l
= 0; l
< cell_rows
; l
++ )
6732 // find a cell in this row to left alreay marked for repaint
6734 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6735 if ((redrawCells
[k
].GetCol() < left
) &&
6736 (redrawCells
[k
].GetRow() == row
))
6737 left
=redrawCells
[k
].GetCol();
6739 if (left
== col
) left
= 0; // oh well
6741 for (int j
= col
-1; j
>= left
; j
--)
6743 if (!m_table
->IsEmptyCell(row
+l
, j
))
6745 if (GetCellOverflow(row
+l
, j
))
6747 wxGridCellCoords
cell(row
+l
, j
);
6748 bool marked
= FALSE
;
6750 for (int k
= 0; k
< numCells
; k
++)
6752 if ( cell
== cells
[k
] )
6760 int count
= redrawCells
.GetCount();
6761 for (int k
= 0; k
< count
; k
++)
6763 if ( cell
== redrawCells
[k
] )
6769 if (!marked
) redrawCells
.Add( cell
);
6777 DrawCell( dc
, cells
[i
] );
6780 numCells
= redrawCells
.GetCount();
6782 for ( i
= numCells
- 1; i
>= 0; i
-- )
6784 DrawCell( dc
, redrawCells
[i
] );
6789 void wxGrid::DrawGridSpace( wxDC
& dc
)
6792 m_gridWin
->GetClientSize( &cw
, &ch
);
6795 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6797 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6798 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6800 if ( right
> rightCol
|| bottom
> bottomRow
)
6803 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6805 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6806 dc
.SetPen( *wxTRANSPARENT_PEN
);
6808 if ( right
> rightCol
)
6810 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6813 if ( bottom
> bottomRow
)
6815 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6821 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6823 int row
= coords
.GetRow();
6824 int col
= coords
.GetCol();
6826 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6829 // we draw the cell border ourselves
6830 #if !WXGRID_DRAW_LINES
6831 if ( m_gridLinesEnabled
)
6832 DrawCellBorder( dc
, coords
);
6835 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6837 bool isCurrent
= coords
== m_currentCellCoords
;
6839 wxRect rect
= CellToRect( row
, col
);
6841 // if the editor is shown, we should use it and not the renderer
6842 // Note: However, only if it is really _shown_, i.e. not hidden!
6843 if ( isCurrent
&& IsCellEditControlShown() )
6845 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6846 editor
->PaintBackground(rect
, attr
);
6851 // but all the rest is drawn by the cell renderer and hence may be
6853 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6854 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6861 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6863 int row
= m_currentCellCoords
.GetRow();
6864 int col
= m_currentCellCoords
.GetCol();
6866 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6869 wxRect rect
= CellToRect(row
, col
);
6871 // hmmm... what could we do here to show that the cell is disabled?
6872 // for now, I just draw a thinner border than for the other ones, but
6873 // it doesn't look really good
6875 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6879 // The center of th drawn line is where the position/width/height of
6880 // the rectangle is actually at, (on wxMSW atr least,) so we will
6881 // reduce the size of the rectangle to compensate for the thickness of
6882 // the line. If this is too strange on non wxMSW platforms then
6883 // please #ifdef this appropriately.
6884 rect
.x
+= penWidth
/2;
6885 rect
.y
+= penWidth
/2;
6886 rect
.width
-= penWidth
-1;
6887 rect
.height
-= penWidth
-1;
6890 // Now draw the rectangle
6891 // use the cellHighlightColour if the cell is inside a selection, this
6892 // will ensure the cell is always visible.
6893 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6894 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6895 dc
.DrawRectangle(rect
);
6899 // VZ: my experiments with 3d borders...
6901 // how to properly set colours for arbitrary bg?
6902 wxCoord x1
= rect
.x
,
6904 x2
= rect
.x
+ rect
.width
-1,
6905 y2
= rect
.y
+ rect
.height
-1;
6907 dc
.SetPen(*wxWHITE_PEN
);
6908 dc
.DrawLine(x1
, y1
, x2
, y1
);
6909 dc
.DrawLine(x1
, y1
, x1
, y2
);
6911 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6912 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6914 dc
.SetPen(*wxBLACK_PEN
);
6915 dc
.DrawLine(x1
, y2
, x2
, y2
);
6916 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6921 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6923 int row
= coords
.GetRow();
6924 int col
= coords
.GetCol();
6925 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6928 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6930 wxRect rect
= CellToRect( row
, col
);
6932 // right hand border
6934 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6935 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6939 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6940 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6943 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6945 // This if block was previously in wxGrid::OnPaint but that doesn't
6946 // seem to get called under wxGTK - MB
6948 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6949 m_numRows
&& m_numCols
)
6951 m_currentCellCoords
.Set(0, 0);
6954 if ( IsCellEditControlShown() )
6956 // don't show highlight when the edit control is shown
6960 // if the active cell was repainted, repaint its highlight too because it
6961 // might have been damaged by the grid lines
6962 size_t count
= cells
.GetCount();
6963 for ( size_t n
= 0; n
< count
; n
++ )
6965 if ( cells
[n
] == m_currentCellCoords
)
6967 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6968 DrawCellHighlight(dc
, attr
);
6976 // TODO: remove this ???
6977 // This is used to redraw all grid lines e.g. when the grid line colour
6980 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6982 #if !WXGRID_DRAW_LINES
6986 if ( !m_gridLinesEnabled
||
6988 !m_numCols
) return;
6990 int top
, bottom
, left
, right
;
6992 #if 0 //#ifndef __WXGTK__
6996 m_gridWin
->GetClientSize(&cw
, &ch
);
6998 // virtual coords of visible area
7000 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7001 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7006 reg
.GetBox(x
, y
, w
, h
);
7007 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7008 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7012 m_gridWin
->GetClientSize(&cw
, &ch
);
7013 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7014 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7017 // avoid drawing grid lines past the last row and col
7019 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7020 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7022 // no gridlines inside multicells, clip them out
7023 int leftCol
= internalXToCol(left
);
7024 int topRow
= internalYToRow(top
);
7025 int rightCol
= internalXToCol(right
);
7026 int bottomRow
= internalYToRow(bottom
);
7027 wxRegion
clippedcells(0, 0, cw
, ch
);
7030 int i
, j
, cell_rows
, cell_cols
;
7033 for (j
=topRow
; j
<bottomRow
; j
++)
7035 for (i
=leftCol
; i
<rightCol
; i
++)
7037 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7038 if ((cell_rows
> 1) || (cell_cols
> 1))
7040 rect
= CellToRect(j
,i
);
7041 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7042 clippedcells
.Subtract(rect
);
7044 else if ((cell_rows
< 0) || (cell_cols
< 0))
7046 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7047 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7048 clippedcells
.Subtract(rect
);
7052 dc
.SetClippingRegion( clippedcells
);
7054 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7056 // horizontal grid lines
7058 // already declared above - int i;
7059 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7061 int bot
= GetRowBottom(i
) - 1;
7070 dc
.DrawLine( left
, bot
, right
, bot
);
7075 // vertical grid lines
7077 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7079 int colRight
= GetColRight(i
) - 1;
7080 if ( colRight
> right
)
7085 if ( colRight
>= left
)
7087 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7090 dc
.DestroyClippingRegion();
7094 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7096 if ( !m_numRows
) return;
7099 size_t numLabels
= rows
.GetCount();
7101 for ( i
= 0; i
< numLabels
; i
++ )
7103 DrawRowLabel( dc
, rows
[i
] );
7108 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7110 if ( GetRowHeight(row
) <= 0 )
7113 int rowTop
= GetRowTop(row
),
7114 rowBottom
= GetRowBottom(row
) - 1;
7116 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7117 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7118 m_rowLabelWidth
-1, rowBottom
);
7120 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7122 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7124 dc
.SetPen( *wxWHITE_PEN
);
7125 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7126 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7128 dc
.SetBackgroundMode( wxTRANSPARENT
);
7129 dc
.SetTextForeground( GetLabelTextColour() );
7130 dc
.SetFont( GetLabelFont() );
7133 GetRowLabelAlignment( &hAlign
, &vAlign
);
7137 rect
.SetY( GetRowTop(row
) + 2 );
7138 rect
.SetWidth( m_rowLabelWidth
- 4 );
7139 rect
.SetHeight( GetRowHeight(row
) - 4 );
7140 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7144 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7146 if ( !m_numCols
) return;
7149 size_t numLabels
= cols
.GetCount();
7151 for ( i
= 0; i
< numLabels
; i
++ )
7153 DrawColLabel( dc
, cols
[i
] );
7158 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7160 if ( GetColWidth(col
) <= 0 )
7163 int colLeft
= GetColLeft(col
),
7164 colRight
= GetColRight(col
) - 1;
7166 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7167 dc
.DrawLine( colRight
, 0,
7168 colRight
, m_colLabelHeight
-1 );
7170 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7172 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7173 colRight
+1, m_colLabelHeight
-1 );
7175 dc
.SetPen( *wxWHITE_PEN
);
7176 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7177 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7179 dc
.SetBackgroundMode( wxTRANSPARENT
);
7180 dc
.SetTextForeground( GetLabelTextColour() );
7181 dc
.SetFont( GetLabelFont() );
7183 int hAlign
, vAlign
, orient
;
7184 GetColLabelAlignment( &hAlign
, &vAlign
);
7185 orient
= GetColLabelTextOrientation();
7188 rect
.SetX( colLeft
+ 2 );
7190 rect
.SetWidth( GetColWidth(col
) - 4 );
7191 rect
.SetHeight( m_colLabelHeight
- 4 );
7192 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7195 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7196 const wxString
& value
,
7200 int textOrientation
)
7202 wxArrayString lines
;
7204 StringToLines( value
, lines
);
7207 //Forward to new API.
7208 DrawTextRectangle( dc
,
7217 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7218 const wxArrayString
& lines
,
7222 int textOrientation
)
7224 long textWidth
, textHeight
;
7225 long lineWidth
, lineHeight
;
7228 dc
.SetClippingRegion( rect
);
7230 nLines
= lines
.GetCount();
7234 float x
= 0.0, y
= 0.0;
7236 if( textOrientation
== wxHORIZONTAL
)
7237 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7239 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7243 case wxALIGN_BOTTOM
:
7244 if( textOrientation
== wxHORIZONTAL
)
7245 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7247 x
= rect
.x
+ rect
.width
- textWidth
;
7250 case wxALIGN_CENTRE
:
7251 if( textOrientation
== wxHORIZONTAL
)
7252 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7254 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7259 if( textOrientation
== wxHORIZONTAL
)
7266 // Align each line of a multi-line label
7267 for( l
= 0; l
< nLines
; l
++ )
7269 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7271 switch( horizAlign
)
7274 if( textOrientation
== wxHORIZONTAL
)
7275 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7277 y
= rect
.y
+ lineWidth
+ 1;
7280 case wxALIGN_CENTRE
:
7281 if( textOrientation
== wxHORIZONTAL
)
7282 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7284 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7289 if( textOrientation
== wxHORIZONTAL
)
7292 y
= rect
.y
+ rect
.height
- 1;
7296 if( textOrientation
== wxHORIZONTAL
)
7298 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7303 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7308 dc
.DestroyClippingRegion();
7312 // Split multi line text up into an array of strings. Any existing
7313 // contents of the string array are preserved.
7315 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7319 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7320 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7322 while ( startPos
< (int)tVal
.Length() )
7324 pos
= tVal
.Mid(startPos
).Find( eol
);
7329 else if ( pos
== 0 )
7331 lines
.Add( wxEmptyString
);
7335 lines
.Add( value
.Mid(startPos
, pos
) );
7339 if ( startPos
< (int)value
.Length() )
7341 lines
.Add( value
.Mid( startPos
) );
7346 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7347 const wxArrayString
& lines
,
7348 long *width
, long *height
)
7355 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7357 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7358 w
= wxMax( w
, lineW
);
7367 // ------ Batch processing.
7369 void wxGrid::EndBatch()
7371 if ( m_batchCount
> 0 )
7374 if ( !m_batchCount
)
7377 m_rowLabelWin
->Refresh();
7378 m_colLabelWin
->Refresh();
7379 m_cornerLabelWin
->Refresh();
7380 m_gridWin
->Refresh();
7385 // Use this, rather than wxWindow::Refresh(), to force an immediate
7386 // repainting of the grid. Has no effect if you are already inside a
7387 // BeginBatch / EndBatch block.
7389 void wxGrid::ForceRefresh()
7397 // ------ Edit control functions
7401 void wxGrid::EnableEditing( bool edit
)
7403 // TODO: improve this ?
7405 if ( edit
!= m_editable
)
7407 if(!edit
) EnableCellEditControl(edit
);
7413 void wxGrid::EnableCellEditControl( bool enable
)
7418 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7419 SetCurrentCell( 0, 0 );
7421 if ( enable
!= m_cellEditCtrlEnabled
)
7425 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7428 // this should be checked by the caller!
7429 wxASSERT_MSG( CanEnableCellControl(),
7430 _T("can't enable editing for this cell!") );
7432 // do it before ShowCellEditControl()
7433 m_cellEditCtrlEnabled
= enable
;
7435 ShowCellEditControl();
7439 //FIXME:add veto support
7440 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7442 HideCellEditControl();
7443 SaveEditControlValue();
7445 // do it after HideCellEditControl()
7446 m_cellEditCtrlEnabled
= enable
;
7451 bool wxGrid::IsCurrentCellReadOnly() const
7454 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7455 bool readonly
= attr
->IsReadOnly();
7461 bool wxGrid::CanEnableCellControl() const
7463 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7464 !IsCurrentCellReadOnly();
7468 bool wxGrid::IsCellEditControlEnabled() const
7470 // the cell edit control might be disable for all cells or just for the
7471 // current one if it's read only
7472 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7475 bool wxGrid::IsCellEditControlShown() const
7477 bool isShown
= FALSE
;
7479 if ( m_cellEditCtrlEnabled
)
7481 int row
= m_currentCellCoords
.GetRow();
7482 int col
= m_currentCellCoords
.GetCol();
7483 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7484 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7489 if ( editor
->IsCreated() )
7491 isShown
= editor
->GetControl()->IsShown();
7501 void wxGrid::ShowCellEditControl()
7503 if ( IsCellEditControlEnabled() )
7505 if ( !IsVisible( m_currentCellCoords
) )
7507 m_cellEditCtrlEnabled
= FALSE
;
7512 wxRect rect
= CellToRect( m_currentCellCoords
);
7513 int row
= m_currentCellCoords
.GetRow();
7514 int col
= m_currentCellCoords
.GetCol();
7516 // if this is part of a multicell, find owner (topleft)
7517 int cell_rows
, cell_cols
;
7518 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7519 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7523 m_currentCellCoords
.SetRow( row
);
7524 m_currentCellCoords
.SetCol( col
);
7527 // convert to scrolled coords
7529 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7531 // done in PaintBackground()
7533 // erase the highlight and the cell contents because the editor
7534 // might not cover the entire cell
7535 wxClientDC
dc( m_gridWin
);
7537 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7538 dc
.SetPen(*wxTRANSPARENT_PEN
);
7539 dc
.DrawRectangle(rect
);
7542 // cell is shifted by one pixel
7543 // However, don't allow x or y to become negative
7544 // since the SetSize() method interprets that as
7551 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7552 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7553 if ( !editor
->IsCreated() )
7555 editor
->Create(m_gridWin
, -1,
7556 new wxGridCellEditorEvtHandler(this, editor
));
7558 wxGridEditorCreatedEvent
evt(GetId(),
7559 wxEVT_GRID_EDITOR_CREATED
,
7563 editor
->GetControl());
7564 GetEventHandler()->ProcessEvent(evt
);
7568 // resize editor to overflow into righthand cells if allowed
7569 int maxWidth
= rect
.width
;
7570 wxString value
= GetCellValue(row
, col
);
7571 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7574 GetTextExtent(value
, &maxWidth
, &y
,
7575 NULL
, NULL
, &attr
->GetFont());
7576 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7578 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7579 if (rect
.x
+maxWidth
> client_right
)
7580 maxWidth
= client_right
- rect
.x
;
7582 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7584 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7585 // may have changed earlier
7586 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7589 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7590 // looks weird going over a multicell
7591 if (m_table
->IsEmptyCell(row
,i
) &&
7592 (rect
.width
< maxWidth
) && (c_rows
== 1))
7593 rect
.width
+= GetColWidth(i
);
7597 if (rect
.GetRight() > client_right
)
7598 rect
.SetRight(client_right
-1);
7601 editor
->SetCellAttr(attr
);
7602 editor
->SetSize( rect
);
7603 editor
->Show( TRUE
, attr
);
7605 // recalc dimensions in case we need to
7606 // expand the scrolled window to account for editor
7609 editor
->BeginEdit(row
, col
, this);
7610 editor
->SetCellAttr(NULL
);
7619 void wxGrid::HideCellEditControl()
7621 if ( IsCellEditControlEnabled() )
7623 int row
= m_currentCellCoords
.GetRow();
7624 int col
= m_currentCellCoords
.GetCol();
7626 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7627 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7628 editor
->Show( FALSE
);
7631 m_gridWin
->SetFocus();
7632 // refresh whole row to the right
7633 wxRect
rect( CellToRect(row
, col
) );
7634 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7635 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7636 m_gridWin
->Refresh( FALSE
, &rect
);
7641 void wxGrid::SaveEditControlValue()
7643 if ( IsCellEditControlEnabled() )
7645 int row
= m_currentCellCoords
.GetRow();
7646 int col
= m_currentCellCoords
.GetCol();
7648 wxString oldval
= GetCellValue(row
,col
);
7650 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7651 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7652 bool changed
= editor
->EndEdit(row
, col
, this);
7659 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7660 m_currentCellCoords
.GetRow(),
7661 m_currentCellCoords
.GetCol() ) < 0 ) {
7663 // Event has been vetoed, set the data back.
7664 SetCellValue(row
,col
,oldval
);
7672 // ------ Grid location functions
7673 // Note that all of these functions work with the logical coordinates of
7674 // grid cells and labels so you will need to convert from device
7675 // coordinates for mouse events etc.
7678 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7680 int row
= YToRow(y
);
7681 int col
= XToCol(x
);
7683 if ( row
== -1 || col
== -1 )
7685 coords
= wxGridNoCellCoords
;
7689 coords
.Set( row
, col
);
7694 // Internal Helper function for computing row or column from some
7695 // (unscrolled) coordinate value, using either
7696 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7697 // of m_rowBottoms/m_ColRights to speed up the search!
7699 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7700 const wxArrayInt
& BorderArray
, int nMax
,
7705 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7711 size_t i_max
= coord
/ defaultDist
,
7714 if (BorderArray
.IsEmpty())
7716 if((int) i_max
< nMax
)
7718 return clipToMinMax
? nMax
- 1 : -1;
7721 if ( i_max
>= BorderArray
.GetCount())
7722 i_max
= BorderArray
.GetCount() - 1;
7725 if ( coord
>= BorderArray
[i_max
])
7729 i_max
= coord
/ minDist
;
7731 i_max
= BorderArray
.GetCount() - 1;
7733 if ( i_max
>= BorderArray
.GetCount())
7734 i_max
= BorderArray
.GetCount() - 1;
7736 if ( coord
>= BorderArray
[i_max
])
7737 return clipToMinMax
? (int)i_max
: -1;
7738 if ( coord
< BorderArray
[0] )
7741 while ( i_max
- i_min
> 0 )
7743 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7744 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7745 if (coord
>= BorderArray
[ i_max
- 1])
7749 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7750 if (coord
< BorderArray
[median
])
7758 int wxGrid::YToRow( int y
)
7760 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7761 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, FALSE
);
7765 int wxGrid::XToCol( int x
)
7767 return CoordToRowOrCol(x
, m_defaultColWidth
,
7768 m_minAcceptableColWidth
, m_colRights
, m_numCols
, FALSE
);
7772 // return the row number that that the y coord is near the edge of, or
7773 // -1 if not near an edge
7775 int wxGrid::YToEdgeOfRow( int y
)
7778 i
= internalYToRow(y
);
7780 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7782 // We know that we are in row i, test whether we are
7783 // close enough to lower or upper border, respectively.
7784 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7786 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7794 // return the col number that that the x coord is near the edge of, or
7795 // -1 if not near an edge
7797 int wxGrid::XToEdgeOfCol( int x
)
7800 i
= internalXToCol(x
);
7802 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7804 // We know that we are in column i, test whether we are
7805 // close enough to right or left border, respectively.
7806 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7808 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7816 wxRect
wxGrid::CellToRect( int row
, int col
)
7818 wxRect
rect( -1, -1, -1, -1 );
7820 if ( row
>= 0 && row
< m_numRows
&&
7821 col
>= 0 && col
< m_numCols
)
7823 int i
, cell_rows
, cell_cols
;
7824 rect
.width
= rect
.height
= 0;
7825 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7826 // if negative then find multicell owner
7827 if (cell_rows
< 0) row
+= cell_rows
;
7828 if (cell_cols
< 0) col
+= cell_cols
;
7829 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7831 rect
.x
= GetColLeft(col
);
7832 rect
.y
= GetRowTop(row
);
7833 for (i
=col
; i
<col
+cell_cols
; i
++)
7834 rect
.width
+= GetColWidth(i
);
7835 for (i
=row
; i
<row
+cell_rows
; i
++)
7836 rect
.height
+= GetRowHeight(i
);
7839 // if grid lines are enabled, then the area of the cell is a bit smaller
7840 if (m_gridLinesEnabled
) {
7848 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7850 // get the cell rectangle in logical coords
7852 wxRect
r( CellToRect( row
, col
) );
7854 // convert to device coords
7856 int left
, top
, right
, bottom
;
7857 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7858 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7860 // check against the client area of the grid window
7863 m_gridWin
->GetClientSize( &cw
, &ch
);
7865 if ( wholeCellVisible
)
7867 // is the cell wholly visible ?
7869 return ( left
>= 0 && right
<= cw
&&
7870 top
>= 0 && bottom
<= ch
);
7874 // is the cell partly visible ?
7876 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7877 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7882 // make the specified cell location visible by doing a minimal amount
7885 void wxGrid::MakeCellVisible( int row
, int col
)
7889 int xpos
= -1, ypos
= -1;
7891 if ( row
>= 0 && row
< m_numRows
&&
7892 col
>= 0 && col
< m_numCols
)
7894 // get the cell rectangle in logical coords
7896 wxRect
r( CellToRect( row
, col
) );
7898 // convert to device coords
7900 int left
, top
, right
, bottom
;
7901 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7902 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7905 m_gridWin
->GetClientSize( &cw
, &ch
);
7911 else if ( bottom
> ch
)
7913 int h
= r
.GetHeight();
7915 for ( i
= row
-1; i
>= 0; i
-- )
7917 int rowHeight
= GetRowHeight(i
);
7918 if ( h
+ rowHeight
> ch
)
7925 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7926 // have rounding errors (this is important, because if we do, we
7927 // might not scroll at all and some cells won't be redrawn)
7929 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7931 ypos
+= GRID_SCROLL_LINE_Y
;
7938 else if ( right
> cw
)
7940 // position the view so that the cell is on the right
7942 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7943 xpos
= x0
+ (right
- cw
);
7945 // see comment for ypos above
7946 xpos
+= GRID_SCROLL_LINE_X
;
7949 if ( xpos
!= -1 || ypos
!= -1 )
7952 xpos
/= GRID_SCROLL_LINE_X
;
7954 ypos
/= GRID_SCROLL_LINE_Y
;
7955 Scroll( xpos
, ypos
);
7963 // ------ Grid cursor movement functions
7966 bool wxGrid::MoveCursorUp( bool expandSelection
)
7968 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7969 m_currentCellCoords
.GetRow() >= 0 )
7971 if ( expandSelection
)
7973 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7974 m_selectingKeyboard
= m_currentCellCoords
;
7975 if ( m_selectingKeyboard
.GetRow() > 0 )
7977 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7978 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7979 m_selectingKeyboard
.GetCol() );
7980 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7983 else if ( m_currentCellCoords
.GetRow() > 0 )
7986 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7987 m_currentCellCoords
.GetCol() );
7988 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7989 m_currentCellCoords
.GetCol() );
8000 bool wxGrid::MoveCursorDown( bool expandSelection
)
8002 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8003 m_currentCellCoords
.GetRow() < m_numRows
)
8005 if ( expandSelection
)
8007 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8008 m_selectingKeyboard
= m_currentCellCoords
;
8009 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8011 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8012 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8013 m_selectingKeyboard
.GetCol() );
8014 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8017 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8020 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8021 m_currentCellCoords
.GetCol() );
8022 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8023 m_currentCellCoords
.GetCol() );
8034 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8036 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8037 m_currentCellCoords
.GetCol() >= 0 )
8039 if ( expandSelection
)
8041 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8042 m_selectingKeyboard
= m_currentCellCoords
;
8043 if ( m_selectingKeyboard
.GetCol() > 0 )
8045 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8046 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8047 m_selectingKeyboard
.GetCol() );
8048 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8051 else if ( m_currentCellCoords
.GetCol() > 0 )
8054 MakeCellVisible( m_currentCellCoords
.GetRow(),
8055 m_currentCellCoords
.GetCol() - 1 );
8056 SetCurrentCell( m_currentCellCoords
.GetRow(),
8057 m_currentCellCoords
.GetCol() - 1 );
8068 bool wxGrid::MoveCursorRight( bool expandSelection
)
8070 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8071 m_currentCellCoords
.GetCol() < m_numCols
)
8073 if ( expandSelection
)
8075 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8076 m_selectingKeyboard
= m_currentCellCoords
;
8077 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8079 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8080 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8081 m_selectingKeyboard
.GetCol() );
8082 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8085 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8088 MakeCellVisible( m_currentCellCoords
.GetRow(),
8089 m_currentCellCoords
.GetCol() + 1 );
8090 SetCurrentCell( m_currentCellCoords
.GetRow(),
8091 m_currentCellCoords
.GetCol() + 1 );
8102 bool wxGrid::MovePageUp()
8104 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8106 int row
= m_currentCellCoords
.GetRow();
8110 m_gridWin
->GetClientSize( &cw
, &ch
);
8112 int y
= GetRowTop(row
);
8113 int newRow
= internalYToRow( y
- ch
+ 1 );
8115 if ( newRow
== row
)
8117 //row > 0 , so newrow can never be less than 0 here.
8121 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8122 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8130 bool wxGrid::MovePageDown()
8132 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8134 int row
= m_currentCellCoords
.GetRow();
8135 if ( (row
+1) < m_numRows
)
8138 m_gridWin
->GetClientSize( &cw
, &ch
);
8140 int y
= GetRowTop(row
);
8141 int newRow
= internalYToRow( y
+ ch
);
8142 if ( newRow
== row
)
8144 // row < m_numRows , so newrow can't overflow here.
8148 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8149 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8157 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8160 m_currentCellCoords
!= wxGridNoCellCoords
&&
8161 m_currentCellCoords
.GetRow() > 0 )
8163 int row
= m_currentCellCoords
.GetRow();
8164 int col
= m_currentCellCoords
.GetCol();
8166 if ( m_table
->IsEmptyCell(row
, col
) )
8168 // starting in an empty cell: find the next block of
8174 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8177 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8179 // starting at the top of a block: find the next block
8185 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8190 // starting within a block: find the top of the block
8195 if ( m_table
->IsEmptyCell(row
, col
) )
8203 MakeCellVisible( row
, col
);
8204 if ( expandSelection
)
8206 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8207 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8212 SetCurrentCell( row
, col
);
8220 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8223 m_currentCellCoords
!= wxGridNoCellCoords
&&
8224 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8226 int row
= m_currentCellCoords
.GetRow();
8227 int col
= m_currentCellCoords
.GetCol();
8229 if ( m_table
->IsEmptyCell(row
, col
) )
8231 // starting in an empty cell: find the next block of
8234 while ( row
< m_numRows
-1 )
8237 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8240 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8242 // starting at the bottom of a block: find the next block
8245 while ( row
< m_numRows
-1 )
8248 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8253 // starting within a block: find the bottom of the block
8255 while ( row
< m_numRows
-1 )
8258 if ( m_table
->IsEmptyCell(row
, col
) )
8266 MakeCellVisible( row
, col
);
8267 if ( expandSelection
)
8269 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8270 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8275 SetCurrentCell( row
, col
);
8284 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8287 m_currentCellCoords
!= wxGridNoCellCoords
&&
8288 m_currentCellCoords
.GetCol() > 0 )
8290 int row
= m_currentCellCoords
.GetRow();
8291 int col
= m_currentCellCoords
.GetCol();
8293 if ( m_table
->IsEmptyCell(row
, col
) )
8295 // starting in an empty cell: find the next block of
8301 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8304 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8306 // starting at the left of a block: find the next block
8312 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8317 // starting within a block: find the left of the block
8322 if ( m_table
->IsEmptyCell(row
, col
) )
8330 MakeCellVisible( row
, col
);
8331 if ( expandSelection
)
8333 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8334 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8339 SetCurrentCell( row
, col
);
8348 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8351 m_currentCellCoords
!= wxGridNoCellCoords
&&
8352 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8354 int row
= m_currentCellCoords
.GetRow();
8355 int col
= m_currentCellCoords
.GetCol();
8357 if ( m_table
->IsEmptyCell(row
, col
) )
8359 // starting in an empty cell: find the next block of
8362 while ( col
< m_numCols
-1 )
8365 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8368 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8370 // starting at the right of a block: find the next block
8373 while ( col
< m_numCols
-1 )
8376 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8381 // starting within a block: find the right of the block
8383 while ( col
< m_numCols
-1 )
8386 if ( m_table
->IsEmptyCell(row
, col
) )
8394 MakeCellVisible( row
, col
);
8395 if ( expandSelection
)
8397 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8398 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8403 SetCurrentCell( row
, col
);
8415 // ------ Label values and formatting
8418 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8420 *horiz
= m_rowLabelHorizAlign
;
8421 *vert
= m_rowLabelVertAlign
;
8424 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8426 *horiz
= m_colLabelHorizAlign
;
8427 *vert
= m_colLabelVertAlign
;
8430 int wxGrid::GetColLabelTextOrientation()
8432 return m_colLabelTextOrientation
;
8435 wxString
wxGrid::GetRowLabelValue( int row
)
8439 return m_table
->GetRowLabelValue( row
);
8449 wxString
wxGrid::GetColLabelValue( int col
)
8453 return m_table
->GetColLabelValue( col
);
8464 void wxGrid::SetRowLabelSize( int width
)
8466 width
= wxMax( width
, 0 );
8467 if ( width
!= m_rowLabelWidth
)
8471 m_rowLabelWin
->Show( FALSE
);
8472 m_cornerLabelWin
->Show( FALSE
);
8474 else if ( m_rowLabelWidth
== 0 )
8476 m_rowLabelWin
->Show( TRUE
);
8477 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8480 m_rowLabelWidth
= width
;
8482 wxScrolledWindow::Refresh( TRUE
);
8487 void wxGrid::SetColLabelSize( int height
)
8489 height
= wxMax( height
, 0 );
8490 if ( height
!= m_colLabelHeight
)
8494 m_colLabelWin
->Show( FALSE
);
8495 m_cornerLabelWin
->Show( FALSE
);
8497 else if ( m_colLabelHeight
== 0 )
8499 m_colLabelWin
->Show( TRUE
);
8500 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8503 m_colLabelHeight
= height
;
8505 wxScrolledWindow::Refresh( TRUE
);
8510 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8512 if ( m_labelBackgroundColour
!= colour
)
8514 m_labelBackgroundColour
= colour
;
8515 m_rowLabelWin
->SetBackgroundColour( colour
);
8516 m_colLabelWin
->SetBackgroundColour( colour
);
8517 m_cornerLabelWin
->SetBackgroundColour( colour
);
8519 if ( !GetBatchCount() )
8521 m_rowLabelWin
->Refresh();
8522 m_colLabelWin
->Refresh();
8523 m_cornerLabelWin
->Refresh();
8528 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8530 if ( m_labelTextColour
!= colour
)
8532 m_labelTextColour
= colour
;
8533 if ( !GetBatchCount() )
8535 m_rowLabelWin
->Refresh();
8536 m_colLabelWin
->Refresh();
8541 void wxGrid::SetLabelFont( const wxFont
& font
)
8544 if ( !GetBatchCount() )
8546 m_rowLabelWin
->Refresh();
8547 m_colLabelWin
->Refresh();
8551 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8553 // allow old (incorrect) defs to be used
8556 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8557 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8558 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8563 case wxTOP
: vert
= wxALIGN_TOP
; break;
8564 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8565 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8568 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8570 m_rowLabelHorizAlign
= horiz
;
8573 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8575 m_rowLabelVertAlign
= vert
;
8578 if ( !GetBatchCount() )
8580 m_rowLabelWin
->Refresh();
8584 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8586 // allow old (incorrect) defs to be used
8589 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8590 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8591 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8596 case wxTOP
: vert
= wxALIGN_TOP
; break;
8597 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8598 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8601 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8603 m_colLabelHorizAlign
= horiz
;
8606 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8608 m_colLabelVertAlign
= vert
;
8611 if ( !GetBatchCount() )
8613 m_colLabelWin
->Refresh();
8617 // Note: under MSW, the default column label font must be changed because it
8618 // does not support vertical printing
8620 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8621 // pGrid->SetLabelFont(font);
8622 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8624 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8626 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8628 m_colLabelTextOrientation
= textOrientation
;
8631 if ( !GetBatchCount() )
8633 m_colLabelWin
->Refresh();
8637 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8641 m_table
->SetRowLabelValue( row
, s
);
8642 if ( !GetBatchCount() )
8644 wxRect rect
= CellToRect( row
, 0);
8645 if ( rect
.height
> 0 )
8647 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8649 rect
.width
= m_rowLabelWidth
;
8650 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8656 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8660 m_table
->SetColLabelValue( col
, s
);
8661 if ( !GetBatchCount() )
8663 wxRect rect
= CellToRect( 0, col
);
8664 if ( rect
.width
> 0 )
8666 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8668 rect
.height
= m_colLabelHeight
;
8669 m_colLabelWin
->Refresh( TRUE
, &rect
);
8675 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8677 if ( m_gridLineColour
!= colour
)
8679 m_gridLineColour
= colour
;
8681 wxClientDC
dc( m_gridWin
);
8683 DrawAllGridLines( dc
, wxRegion() );
8688 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8690 if ( m_cellHighlightColour
!= colour
)
8692 m_cellHighlightColour
= colour
;
8694 wxClientDC
dc( m_gridWin
);
8696 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8697 DrawCellHighlight(dc
, attr
);
8702 void wxGrid::SetCellHighlightPenWidth(int width
)
8704 if (m_cellHighlightPenWidth
!= width
) {
8705 m_cellHighlightPenWidth
= width
;
8707 // Just redrawing the cell highlight is not enough since that won't
8708 // make any visible change if the the thickness is getting smaller.
8709 int row
= m_currentCellCoords
.GetRow();
8710 int col
= m_currentCellCoords
.GetCol();
8711 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8713 wxRect rect
= CellToRect(row
, col
);
8714 m_gridWin
->Refresh(TRUE
, &rect
);
8718 void wxGrid::SetCellHighlightROPenWidth(int width
)
8720 if (m_cellHighlightROPenWidth
!= width
) {
8721 m_cellHighlightROPenWidth
= width
;
8723 // Just redrawing the cell highlight is not enough since that won't
8724 // make any visible change if the the thickness is getting smaller.
8725 int row
= m_currentCellCoords
.GetRow();
8726 int col
= m_currentCellCoords
.GetCol();
8727 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8729 wxRect rect
= CellToRect(row
, col
);
8730 m_gridWin
->Refresh(TRUE
, &rect
);
8734 void wxGrid::EnableGridLines( bool enable
)
8736 if ( enable
!= m_gridLinesEnabled
)
8738 m_gridLinesEnabled
= enable
;
8740 if ( !GetBatchCount() )
8744 wxClientDC
dc( m_gridWin
);
8746 DrawAllGridLines( dc
, wxRegion() );
8750 m_gridWin
->Refresh();
8757 int wxGrid::GetDefaultRowSize()
8759 return m_defaultRowHeight
;
8762 int wxGrid::GetRowSize( int row
)
8764 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8766 return GetRowHeight(row
);
8769 int wxGrid::GetDefaultColSize()
8771 return m_defaultColWidth
;
8774 int wxGrid::GetColSize( int col
)
8776 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8778 return GetColWidth(col
);
8781 // ============================================================================
8782 // access to the grid attributes: each of them has a default value in the grid
8783 // itself and may be overidden on a per-cell basis
8784 // ============================================================================
8786 // ----------------------------------------------------------------------------
8787 // setting default attributes
8788 // ----------------------------------------------------------------------------
8790 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8792 m_defaultCellAttr
->SetBackgroundColour(col
);
8794 m_gridWin
->SetBackgroundColour(col
);
8798 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8800 m_defaultCellAttr
->SetTextColour(col
);
8803 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8805 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8808 void wxGrid::SetDefaultCellOverflow( bool allow
)
8810 m_defaultCellAttr
->SetOverflow(allow
);
8813 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8815 m_defaultCellAttr
->SetFont(font
);
8818 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8820 m_defaultCellAttr
->SetRenderer(renderer
);
8823 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8825 m_defaultCellAttr
->SetEditor(editor
);
8828 // ----------------------------------------------------------------------------
8829 // access to the default attrbiutes
8830 // ----------------------------------------------------------------------------
8832 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8834 return m_defaultCellAttr
->GetBackgroundColour();
8837 wxColour
wxGrid::GetDefaultCellTextColour()
8839 return m_defaultCellAttr
->GetTextColour();
8842 wxFont
wxGrid::GetDefaultCellFont()
8844 return m_defaultCellAttr
->GetFont();
8847 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8849 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8852 bool wxGrid::GetDefaultCellOverflow()
8854 return m_defaultCellAttr
->GetOverflow();
8857 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8859 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8862 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8864 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8867 // ----------------------------------------------------------------------------
8868 // access to cell attributes
8869 // ----------------------------------------------------------------------------
8871 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8873 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8874 wxColour colour
= attr
->GetBackgroundColour();
8879 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8881 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8882 wxColour colour
= attr
->GetTextColour();
8887 wxFont
wxGrid::GetCellFont( int row
, int col
)
8889 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8890 wxFont font
= attr
->GetFont();
8895 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8897 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8898 attr
->GetAlignment(horiz
, vert
);
8902 bool wxGrid::GetCellOverflow( int row
, int col
)
8904 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8905 bool allow
= attr
->GetOverflow();
8910 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8912 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8913 attr
->GetSize( num_rows
, num_cols
);
8917 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8919 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8920 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8926 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8928 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8929 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8935 bool wxGrid::IsReadOnly(int row
, int col
) const
8937 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8938 bool isReadOnly
= attr
->IsReadOnly();
8943 // ----------------------------------------------------------------------------
8944 // attribute support: cache, automatic provider creation, ...
8945 // ----------------------------------------------------------------------------
8947 bool wxGrid::CanHaveAttributes()
8954 return m_table
->CanHaveAttributes();
8957 void wxGrid::ClearAttrCache()
8959 if ( m_attrCache
.row
!= -1 )
8961 wxSafeDecRef(m_attrCache
.attr
);
8962 m_attrCache
.attr
= NULL
;
8963 m_attrCache
.row
= -1;
8967 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8971 wxGrid
*self
= (wxGrid
*)this; // const_cast
8973 self
->ClearAttrCache();
8974 self
->m_attrCache
.row
= row
;
8975 self
->m_attrCache
.col
= col
;
8976 self
->m_attrCache
.attr
= attr
;
8981 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8983 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8985 *attr
= m_attrCache
.attr
;
8986 wxSafeIncRef(m_attrCache
.attr
);
8988 #ifdef DEBUG_ATTR_CACHE
8989 gs_nAttrCacheHits
++;
8996 #ifdef DEBUG_ATTR_CACHE
8997 gs_nAttrCacheMisses
++;
9003 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9005 wxGridCellAttr
*attr
= NULL
;
9006 // Additional test to avoid looking at the cache e.g. for
9007 // wxNoCellCoords, as this will confuse memory management.
9010 if ( !LookupAttr(row
, col
, &attr
) )
9012 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9013 : (wxGridCellAttr
*)NULL
;
9014 CacheAttr(row
, col
, attr
);
9019 attr
->SetDefAttr(m_defaultCellAttr
);
9023 attr
= m_defaultCellAttr
;
9030 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9032 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9034 wxCHECK_MSG( m_table
, attr
,
9035 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
9037 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9040 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9042 // artificially inc the ref count to match DecRef() in caller
9044 m_table
->SetAttr(attr
, row
, col
);
9050 // ----------------------------------------------------------------------------
9051 // setting column attributes (wrappers around SetColAttr)
9052 // ----------------------------------------------------------------------------
9054 void wxGrid::SetColFormatBool(int col
)
9056 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9059 void wxGrid::SetColFormatNumber(int col
)
9061 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9064 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9066 wxString typeName
= wxGRID_VALUE_FLOAT
;
9067 if ( (width
!= -1) || (precision
!= -1) )
9069 typeName
<< _T(':') << width
<< _T(',') << precision
;
9072 SetColFormatCustom(col
, typeName
);
9075 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9077 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9079 attr
= new wxGridCellAttr
;
9080 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9081 attr
->SetRenderer(renderer
);
9083 SetColAttr(col
, attr
);
9087 // ----------------------------------------------------------------------------
9088 // setting cell attributes: this is forwarded to the table
9089 // ----------------------------------------------------------------------------
9091 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9093 if ( CanHaveAttributes() )
9095 m_table
->SetAttr(attr
, row
, col
);
9104 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9106 if ( CanHaveAttributes() )
9108 m_table
->SetRowAttr(attr
, row
);
9117 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9119 if ( CanHaveAttributes() )
9121 m_table
->SetColAttr(attr
, col
);
9130 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9132 if ( CanHaveAttributes() )
9134 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9135 attr
->SetBackgroundColour(colour
);
9140 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9142 if ( CanHaveAttributes() )
9144 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9145 attr
->SetTextColour(colour
);
9150 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9152 if ( CanHaveAttributes() )
9154 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9155 attr
->SetFont(font
);
9160 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9162 if ( CanHaveAttributes() )
9164 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9165 attr
->SetAlignment(horiz
, vert
);
9170 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9172 if ( CanHaveAttributes() )
9174 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9175 attr
->SetOverflow(allow
);
9180 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9182 if ( CanHaveAttributes() )
9184 int cell_rows
, cell_cols
;
9186 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9187 attr
->GetSize(&cell_rows
, &cell_cols
);
9188 attr
->SetSize(num_rows
, num_cols
);
9191 // Cannot set the size of a cell to 0 or negative values
9192 // While it is perfectly legal to do that, this function cannot
9193 // handle all the possibilies, do it by hand by getting the CellAttr.
9194 // You can only set the size of a cell to 1,1 or greater with this fn
9195 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9196 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9197 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9198 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9200 // if this was already a multicell then "turn off" the other cells first
9201 if ((cell_rows
> 1) || (cell_rows
> 1))
9204 for (j
=row
; j
<row
+cell_rows
; j
++)
9206 for (i
=col
; i
<col
+cell_cols
; i
++)
9208 if ((i
!= col
) || (j
!= row
))
9210 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9211 attr_stub
->SetSize( 1, 1 );
9212 attr_stub
->DecRef();
9218 // mark the cells that will be covered by this cell to
9219 // negative or zero values to point back at this cell
9220 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9223 for (j
=row
; j
<row
+num_rows
; j
++)
9225 for (i
=col
; i
<col
+num_cols
; i
++)
9227 if ((i
!= col
) || (j
!= row
))
9229 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9230 attr_stub
->SetSize( row
-j
, col
-i
);
9231 attr_stub
->DecRef();
9239 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9241 if ( CanHaveAttributes() )
9243 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9244 attr
->SetRenderer(renderer
);
9249 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9251 if ( CanHaveAttributes() )
9253 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9254 attr
->SetEditor(editor
);
9259 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9261 if ( CanHaveAttributes() )
9263 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9264 attr
->SetReadOnly(isReadOnly
);
9269 // ----------------------------------------------------------------------------
9270 // Data type registration
9271 // ----------------------------------------------------------------------------
9273 void wxGrid::RegisterDataType(const wxString
& typeName
,
9274 wxGridCellRenderer
* renderer
,
9275 wxGridCellEditor
* editor
)
9277 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9281 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9283 wxString typeName
= m_table
->GetTypeName(row
, col
);
9284 return GetDefaultEditorForType(typeName
);
9287 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9289 wxString typeName
= m_table
->GetTypeName(row
, col
);
9290 return GetDefaultRendererForType(typeName
);
9294 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9296 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9297 if ( index
== wxNOT_FOUND
)
9299 wxFAIL_MSG(wxT("Unknown data type name"));
9304 return m_typeRegistry
->GetEditor(index
);
9308 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9310 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9311 if ( index
== wxNOT_FOUND
)
9313 wxFAIL_MSG(wxT("Unknown data type name"));
9318 return m_typeRegistry
->GetRenderer(index
);
9322 // ----------------------------------------------------------------------------
9324 // ----------------------------------------------------------------------------
9326 void wxGrid::EnableDragRowSize( bool enable
)
9328 m_canDragRowSize
= enable
;
9332 void wxGrid::EnableDragColSize( bool enable
)
9334 m_canDragColSize
= enable
;
9337 void wxGrid::EnableDragGridSize( bool enable
)
9339 m_canDragGridSize
= enable
;
9343 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9345 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9347 if ( resizeExistingRows
)
9349 // since we are resizing all rows to the default row size,
9350 // we can simply clear the row heights and row bottoms
9351 // arrays (which also allows us to take advantage of
9352 // some speed optimisations)
9353 m_rowHeights
.Empty();
9354 m_rowBottoms
.Empty();
9355 if ( !GetBatchCount() )
9360 void wxGrid::SetRowSize( int row
, int height
)
9362 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9364 // See comment in SetColSize
9365 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9367 if ( m_rowHeights
.IsEmpty() )
9369 // need to really create the array
9373 int h
= wxMax( 0, height
);
9374 int diff
= h
- m_rowHeights
[row
];
9376 m_rowHeights
[row
] = h
;
9378 for ( i
= row
; i
< m_numRows
; i
++ )
9380 m_rowBottoms
[i
] += diff
;
9382 if ( !GetBatchCount() )
9386 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9388 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9390 if ( resizeExistingCols
)
9392 // since we are resizing all columns to the default column size,
9393 // we can simply clear the col widths and col rights
9394 // arrays (which also allows us to take advantage of
9395 // some speed optimisations)
9396 m_colWidths
.Empty();
9397 m_colRights
.Empty();
9398 if ( !GetBatchCount() )
9403 void wxGrid::SetColSize( int col
, int width
)
9405 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9407 // should we check that it's bigger than GetColMinimalWidth(col) here?
9409 // No, because it is reasonable to assume the library user know's
9410 // what he is doing. However whe should test against the weaker
9411 // constariant of minimalAcceptableWidth, as this breaks rendering
9413 // This test then fixes sf.net bug #645734
9415 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9417 if ( m_colWidths
.IsEmpty() )
9419 // need to really create the array
9423 // if < 0 calc new width from label
9427 wxArrayString lines
;
9428 wxClientDC
dc(m_colLabelWin
);
9429 dc
.SetFont(GetLabelFont());
9430 StringToLines(GetColLabelValue(col
), lines
);
9431 GetTextBoxSize(dc
, lines
, &w
, &h
);
9434 int w
= wxMax( 0, width
);
9435 int diff
= w
- m_colWidths
[col
];
9436 m_colWidths
[col
] = w
;
9439 for ( i
= col
; i
< m_numCols
; i
++ )
9441 m_colRights
[i
] += diff
;
9443 if ( !GetBatchCount() )
9448 void wxGrid::SetColMinimalWidth( int col
, int width
)
9450 if (width
> GetColMinimalAcceptableWidth()) {
9451 m_colMinWidths
[col
] = width
;
9455 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9457 if (width
> GetRowMinimalAcceptableHeight()) {
9458 m_rowMinHeights
[row
] = width
;
9462 int wxGrid::GetColMinimalWidth(int col
) const
9464 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9465 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9468 int wxGrid::GetRowMinimalHeight(int row
) const
9470 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9471 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9474 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9476 // We do allow a width of 0 since this gives us
9477 // an easy way to temporarily hidding columns.
9480 m_minAcceptableColWidth
= width
;
9483 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9485 // We do allow a height of 0 since this gives us
9486 // an easy way to temporarily hidding rows.
9489 m_minAcceptableRowHeight
= height
;
9492 int wxGrid::GetColMinimalAcceptableWidth() const
9494 return m_minAcceptableColWidth
;
9497 int wxGrid::GetRowMinimalAcceptableHeight() const
9499 return m_minAcceptableRowHeight
;
9502 // ----------------------------------------------------------------------------
9504 // ----------------------------------------------------------------------------
9506 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9508 wxClientDC
dc(m_gridWin
);
9510 // init both of them to avoid compiler warnings, even if weo nly need one
9518 wxCoord extent
, extentMax
= 0;
9519 int max
= column
? m_numRows
: m_numCols
;
9520 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9527 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9528 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9531 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9532 extent
= column
? size
.x
: size
.y
;
9533 if ( extent
> extentMax
)
9544 // now also compare with the column label extent
9546 dc
.SetFont( GetLabelFont() );
9550 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9551 if( GetColLabelTextOrientation() == wxVERTICAL
)
9555 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9557 extent
= column
? w
: h
;
9558 if ( extent
> extentMax
)
9565 // empty column - give default extent (notice that if extentMax is less
9566 // than default extent but != 0, it's ok)
9567 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9573 // leave some space around text
9584 SetColSize(col
, extentMax
);
9585 if ( !GetBatchCount() )
9588 m_gridWin
->GetClientSize( &cw
, &ch
);
9589 wxRect
rect ( CellToRect( 0, col
) );
9591 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9592 rect
.width
= cw
- rect
.x
;
9593 rect
.height
= m_colLabelHeight
;
9594 m_colLabelWin
->Refresh( TRUE
, &rect
);
9599 SetRowSize(row
, extentMax
);
9600 if ( !GetBatchCount() )
9603 m_gridWin
->GetClientSize( &cw
, &ch
);
9604 wxRect
rect ( CellToRect( row
, 0 ) );
9606 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9607 rect
.width
= m_rowLabelWidth
;
9608 rect
.height
= ch
- rect
.y
;
9609 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9615 SetColMinimalWidth(col
, extentMax
);
9617 SetRowMinimalHeight(row
, extentMax
);
9621 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9623 int width
= m_rowLabelWidth
;
9628 for ( int col
= 0; col
< m_numCols
; col
++ )
9632 AutoSizeColumn(col
, setAsMin
);
9635 width
+= GetColWidth(col
);
9644 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9646 int height
= m_colLabelHeight
;
9651 for ( int row
= 0; row
< m_numRows
; row
++ )
9655 AutoSizeRow(row
, setAsMin
);
9658 height
+= GetRowHeight(row
);
9667 void wxGrid::AutoSize()
9671 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9673 // round up the size to a multiple of scroll step - this ensures that we
9674 // won't get the scrollbars if we're sized exactly to this width
9675 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9677 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9678 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9680 // distribute the extra space between the columns/rows to avoid having
9681 // extra white space
9683 // Remove the extra m_extraWidth + 1 added above
9684 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9685 if ( diff
&& m_numCols
)
9687 // try to resize the columns uniformly
9688 wxCoord diffPerCol
= diff
/ m_numCols
;
9691 for ( int col
= 0; col
< m_numCols
; col
++ )
9693 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9697 // add remaining amount to the last columns
9698 diff
-= diffPerCol
* m_numCols
;
9701 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9703 SetColSize(col
, GetColWidth(col
) + 1);
9709 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9710 if ( diff
&& m_numRows
)
9712 // try to resize the columns uniformly
9713 wxCoord diffPerRow
= diff
/ m_numRows
;
9716 for ( int row
= 0; row
< m_numRows
; row
++ )
9718 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9722 // add remaining amount to the last rows
9723 diff
-= diffPerRow
* m_numRows
;
9726 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9728 SetRowSize(row
, GetRowHeight(row
) + 1);
9735 SetClientSize(sizeFit
);
9738 void wxGrid::AutoSizeRowLabelSize( int row
)
9740 wxArrayString lines
;
9743 // Hide the edit control, so it
9744 // won't interfer with drag-shrinking.
9745 if( IsCellEditControlShown() )
9747 HideCellEditControl();
9748 SaveEditControlValue();
9751 // autosize row height depending on label text
9752 StringToLines( GetRowLabelValue( row
), lines
);
9753 wxClientDC
dc( m_rowLabelWin
);
9754 GetTextBoxSize( dc
, lines
, &w
, &h
);
9755 if( h
< m_defaultRowHeight
)
9756 h
= m_defaultRowHeight
;
9761 void wxGrid::AutoSizeColLabelSize( int col
)
9763 wxArrayString lines
;
9766 // Hide the edit control, so it
9767 // won't interfer with drag-shrinking.
9768 if( IsCellEditControlShown() )
9770 HideCellEditControl();
9771 SaveEditControlValue();
9774 // autosize column width depending on label text
9775 StringToLines( GetColLabelValue( col
), lines
);
9776 wxClientDC
dc( m_colLabelWin
);
9777 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9778 GetTextBoxSize( dc
, lines
, &w
, &h
);
9780 GetTextBoxSize( dc
, lines
, &h
, &w
);
9781 if( w
< m_defaultColWidth
)
9782 w
= m_defaultColWidth
;
9787 wxSize
wxGrid::DoGetBestSize() const
9789 // don't set sizes, only calculate them
9790 wxGrid
*self
= (wxGrid
*)this; // const_cast
9793 width
= self
->SetOrCalcColumnSizes(TRUE
);
9794 height
= self
->SetOrCalcRowSizes(TRUE
);
9796 int maxwidth
, maxheight
;
9797 wxDisplaySize( & maxwidth
, & maxheight
);
9799 if ( width
> maxwidth
) width
= maxwidth
;
9800 if ( height
> maxheight
) height
= maxheight
;
9802 return wxSize( width
, height
);
9811 wxPen
& wxGrid::GetDividerPen() const
9816 // ----------------------------------------------------------------------------
9817 // cell value accessor functions
9818 // ----------------------------------------------------------------------------
9820 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9824 m_table
->SetValue( row
, col
, s
);
9825 if ( !GetBatchCount() )
9828 wxRect
rect( CellToRect( row
, col
) );
9830 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9831 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9832 m_gridWin
->Refresh( FALSE
, &rect
);
9835 if ( m_currentCellCoords
.GetRow() == row
&&
9836 m_currentCellCoords
.GetCol() == col
&&
9837 IsCellEditControlShown())
9838 // Note: If we are using IsCellEditControlEnabled,
9839 // this interacts badly with calling SetCellValue from
9840 // an EVT_GRID_CELL_CHANGE handler.
9842 HideCellEditControl();
9843 ShowCellEditControl(); // will reread data from table
9850 // ------ Block, row and col selection
9853 void wxGrid::SelectRow( int row
, bool addToSelected
)
9855 if ( IsSelection() && !addToSelected
)
9859 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9863 void wxGrid::SelectCol( int col
, bool addToSelected
)
9865 if ( IsSelection() && !addToSelected
)
9869 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9873 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9874 bool addToSelected
)
9876 if ( IsSelection() && !addToSelected
)
9880 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9881 FALSE
, addToSelected
);
9885 void wxGrid::SelectAll()
9887 if ( m_numRows
> 0 && m_numCols
> 0 )
9890 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9895 // ------ Cell, row and col deselection
9898 void wxGrid::DeselectRow( int row
)
9903 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9905 if ( m_selection
->IsInSelection(row
, 0 ) )
9906 m_selection
->ToggleCellSelection( row
, 0);
9910 int nCols
= GetNumberCols();
9911 for ( int i
= 0; i
< nCols
; i
++ )
9913 if ( m_selection
->IsInSelection(row
, i
) )
9914 m_selection
->ToggleCellSelection( row
, i
);
9919 void wxGrid::DeselectCol( int col
)
9924 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9926 if ( m_selection
->IsInSelection(0, col
) )
9927 m_selection
->ToggleCellSelection( 0, col
);
9931 int nRows
= GetNumberRows();
9932 for ( int i
= 0; i
< nRows
; i
++ )
9934 if ( m_selection
->IsInSelection(i
, col
) )
9935 m_selection
->ToggleCellSelection(i
, col
);
9940 void wxGrid::DeselectCell( int row
, int col
)
9942 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9943 m_selection
->ToggleCellSelection(row
, col
);
9946 bool wxGrid::IsSelection()
9948 return ( m_selection
&& (m_selection
->IsSelection() ||
9949 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9950 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9953 bool wxGrid::IsInSelection( int row
, int col
) const
9955 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9956 ( row
>= m_selectingTopLeft
.GetRow() &&
9957 col
>= m_selectingTopLeft
.GetCol() &&
9958 row
<= m_selectingBottomRight
.GetRow() &&
9959 col
<= m_selectingBottomRight
.GetCol() )) );
9962 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9964 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9965 return m_selection
->m_cellSelection
;
9967 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9969 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9970 return m_selection
->m_blockSelectionTopLeft
;
9972 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9974 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9975 return m_selection
->m_blockSelectionBottomRight
;
9977 wxArrayInt
wxGrid::GetSelectedRows() const
9979 if (!m_selection
) { wxArrayInt a
; return a
; }
9980 return m_selection
->m_rowSelection
;
9982 wxArrayInt
wxGrid::GetSelectedCols() const
9984 if (!m_selection
) { wxArrayInt a
; return a
; }
9985 return m_selection
->m_colSelection
;
9989 void wxGrid::ClearSelection()
9991 m_selectingTopLeft
= wxGridNoCellCoords
;
9992 m_selectingBottomRight
= wxGridNoCellCoords
;
9994 m_selection
->ClearSelection();
9998 // This function returns the rectangle that encloses the given block
9999 // in device coords clipped to the client size of the grid window.
10001 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10002 const wxGridCellCoords
&bottomRight
)
10004 wxRect
rect( wxGridNoCellRect
);
10007 cellRect
= CellToRect( topLeft
);
10008 if ( cellRect
!= wxGridNoCellRect
)
10014 rect
= wxRect( 0, 0, 0, 0 );
10017 cellRect
= CellToRect( bottomRight
);
10018 if ( cellRect
!= wxGridNoCellRect
)
10024 return wxGridNoCellRect
;
10028 int left
= rect
.GetLeft();
10029 int top
= rect
.GetTop();
10030 int right
= rect
.GetRight();
10031 int bottom
= rect
.GetBottom();
10033 int leftCol
= topLeft
.GetCol();
10034 int topRow
= topLeft
.GetRow();
10035 int rightCol
= bottomRight
.GetCol();
10036 int bottomRow
= bottomRight
.GetRow();
10054 topRow
= bottomRow
;
10059 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10061 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10063 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10065 cellRect
= CellToRect( j
, i
);
10067 if (cellRect
.x
< left
)
10069 if (cellRect
.y
< top
)
10071 if (cellRect
.x
+ cellRect
.width
> right
)
10072 right
= cellRect
.x
+ cellRect
.width
;
10073 if (cellRect
.y
+ cellRect
.height
> bottom
)
10074 bottom
= cellRect
.y
+ cellRect
.height
;
10076 else i
= rightCol
; // jump over inner cells.
10080 // convert to scrolled coords
10082 CalcScrolledPosition( left
, top
, &left
, &top
);
10083 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10086 m_gridWin
->GetClientSize( &cw
, &ch
);
10088 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10089 return wxRect( 0, 0, 0, 0);
10091 rect
.SetLeft( wxMax(0, left
) );
10092 rect
.SetTop( wxMax(0, top
) );
10093 rect
.SetRight( wxMin(cw
, right
) );
10094 rect
.SetBottom( wxMin(ch
, bottom
) );
10100 // ------ Grid event classes
10103 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10105 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10106 int row
, int col
, int x
, int y
, bool sel
,
10107 bool control
, bool shift
, bool alt
, bool meta
)
10108 : wxNotifyEvent( type
, id
)
10115 m_control
= control
;
10120 SetEventObject(obj
);
10124 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10126 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10127 int rowOrCol
, int x
, int y
,
10128 bool control
, bool shift
, bool alt
, bool meta
)
10129 : wxNotifyEvent( type
, id
)
10131 m_rowOrCol
= rowOrCol
;
10134 m_control
= control
;
10139 SetEventObject(obj
);
10143 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10145 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10146 const wxGridCellCoords
& topLeft
,
10147 const wxGridCellCoords
& bottomRight
,
10148 bool sel
, bool control
,
10149 bool shift
, bool alt
, bool meta
)
10150 : wxNotifyEvent( type
, id
)
10152 m_topLeft
= topLeft
;
10153 m_bottomRight
= bottomRight
;
10155 m_control
= control
;
10160 SetEventObject(obj
);
10164 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10166 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10167 wxObject
* obj
, int row
,
10168 int col
, wxControl
* ctrl
)
10169 : wxCommandEvent(type
, id
)
10171 SetEventObject(obj
);
10177 #endif // wxUSE_GRID