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
& );
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 Text()->SetValue(m_startValue
);
677 void wxGridCellTextEditor::Reset()
679 wxASSERT_MSG(m_control
,
680 wxT("The wxGridCellEditor must be Created first!"));
682 DoReset(m_startValue
);
685 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
687 Text()->SetValue(startValue
);
688 Text()->SetInsertionPointEnd();
691 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
693 if ( wxGridCellEditor::IsAcceptedKey(event
) )
695 int keycode
= event
.GetKeyCode();
709 case WXK_NUMPAD_MULTIPLY
:
713 case WXK_NUMPAD_SUBTRACT
:
715 case WXK_NUMPAD_DECIMAL
:
717 case WXK_NUMPAD_DIVIDE
:
721 // accept 8 bit chars too if isprint() agrees
722 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
730 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
732 if ( !Text()->EmulateKeyPress(event
) )
738 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
739 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
741 #if defined(__WXMOTIF__) || defined(__WXGTK__)
742 // wxMotif needs a little extra help...
743 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
744 wxString
s( Text()->GetValue() );
745 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
747 Text()->SetInsertionPoint( pos
);
749 // the other ports can handle a Return key press
755 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
765 if ( !params
.ToLong(&tmp
) )
767 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
771 m_maxChars
= (size_t)tmp
;
776 // return the value in the text control
777 wxString
wxGridCellTextEditor::GetValue() const
779 return Text()->GetValue();
782 // ----------------------------------------------------------------------------
783 // wxGridCellNumberEditor
784 // ----------------------------------------------------------------------------
786 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
792 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
794 wxEvtHandler
* evtHandler
)
798 // create a spin ctrl
799 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
800 wxDefaultPosition
, wxDefaultSize
,
804 wxGridCellEditor::Create(parent
, id
, evtHandler
);
808 // just a text control
809 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
812 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
813 #endif // wxUSE_VALIDATORS
817 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
819 // first get the value
820 wxGridTableBase
*table
= grid
->GetTable();
821 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
823 m_valueOld
= table
->GetValueAsLong(row
, col
);
828 wxString sValue
= table
->GetValue(row
, col
);
829 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
831 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
838 Spin()->SetValue((int)m_valueOld
);
843 DoBeginEdit(GetString());
847 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
856 value
= Spin()->GetValue();
857 changed
= value
!= m_valueOld
;
859 text
= wxString::Format(wxT("%ld"), value
);
863 text
= Text()->GetValue();
864 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
869 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
870 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
872 grid
->GetTable()->SetValue(row
, col
, text
);
878 void wxGridCellNumberEditor::Reset()
882 Spin()->SetValue((int)m_valueOld
);
886 DoReset(GetString());
890 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
892 if ( wxGridCellEditor::IsAcceptedKey(event
) )
894 int keycode
= event
.GetKeyCode();
910 case WXK_NUMPAD_SUBTRACT
:
916 if ( (keycode
< 128) && wxIsdigit(keycode
) )
924 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
928 int keycode
= event
.GetKeyCode();
929 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
930 || keycode
== WXK_NUMPAD0
931 || keycode
== WXK_NUMPAD1
932 || keycode
== WXK_NUMPAD2
933 || keycode
== WXK_NUMPAD3
934 || keycode
== WXK_NUMPAD4
935 || keycode
== WXK_NUMPAD5
936 || keycode
== WXK_NUMPAD6
937 || keycode
== WXK_NUMPAD7
938 || keycode
== WXK_NUMPAD8
939 || keycode
== WXK_NUMPAD9
940 || keycode
== WXK_ADD
941 || keycode
== WXK_NUMPAD_ADD
942 || keycode
== WXK_SUBTRACT
943 || keycode
== WXK_NUMPAD_SUBTRACT
)
945 wxGridCellTextEditor::StartingKey(event
);
955 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
966 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
970 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
974 // skip the error message below
979 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
983 // return the value in the spin control if it is there (the text control otherwise)
984 wxString
wxGridCellNumberEditor::GetValue() const
990 long value
= Spin()->GetValue();
991 s
.Printf(wxT("%ld"), value
);
995 s
= Text()->GetValue();
1000 // ----------------------------------------------------------------------------
1001 // wxGridCellFloatEditor
1002 // ----------------------------------------------------------------------------
1004 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1007 m_precision
= precision
;
1010 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1012 wxEvtHandler
* evtHandler
)
1014 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1016 #if wxUSE_VALIDATORS
1017 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1018 #endif // wxUSE_VALIDATORS
1021 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1023 // first get the value
1024 wxGridTableBase
*table
= grid
->GetTable();
1025 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1027 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1032 wxString sValue
= table
->GetValue(row
, col
);
1033 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1035 wxFAIL_MSG( _T("this cell doesn't have float value") );
1040 DoBeginEdit(GetString());
1043 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1047 wxString
text(Text()->GetValue());
1049 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1051 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1052 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1054 grid
->GetTable()->SetValue(row
, col
, text
);
1061 void wxGridCellFloatEditor::Reset()
1063 DoReset(GetString());
1066 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1068 int keycode
= event
.GetKeyCode();
1069 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1070 || keycode
== WXK_NUMPAD0
1071 || keycode
== WXK_NUMPAD1
1072 || keycode
== WXK_NUMPAD2
1073 || keycode
== WXK_NUMPAD3
1074 || keycode
== WXK_NUMPAD4
1075 || keycode
== WXK_NUMPAD5
1076 || keycode
== WXK_NUMPAD6
1077 || keycode
== WXK_NUMPAD7
1078 || keycode
== WXK_NUMPAD8
1079 || keycode
== WXK_NUMPAD9
1080 || keycode
== WXK_ADD
1081 || keycode
== WXK_NUMPAD_ADD
1082 || keycode
== WXK_SUBTRACT
1083 || keycode
== WXK_NUMPAD_SUBTRACT
)
1085 wxGridCellTextEditor::StartingKey(event
);
1087 // skip Skip() below
1094 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1105 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1109 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1111 m_precision
= (int)tmp
;
1113 // skip the error message below
1118 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1122 wxString
wxGridCellFloatEditor::GetString() const
1125 if ( m_width
== -1 )
1127 // default width/precision
1130 else if ( m_precision
== -1 )
1132 // default precision
1133 fmt
.Printf(_T("%%%d.f"), m_width
);
1137 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1140 return wxString::Format(fmt
, m_valueOld
);
1143 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1145 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1147 int keycode
= event
.GetKeyCode();
1161 case WXK_NUMPAD_ADD
:
1163 case WXK_NUMPAD_SUBTRACT
:
1165 case WXK_NUMPAD_DECIMAL
:
1169 // additionally accept 'e' as in '1e+6'
1170 if ( (keycode
< 128) &&
1171 (wxIsdigit(keycode
) || tolower(keycode
) == 'e') )
1179 #endif // wxUSE_TEXTCTRL
1183 // ----------------------------------------------------------------------------
1184 // wxGridCellBoolEditor
1185 // ----------------------------------------------------------------------------
1187 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1189 wxEvtHandler
* evtHandler
)
1191 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1192 wxDefaultPosition
, wxDefaultSize
,
1195 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1198 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1200 bool resize
= FALSE
;
1201 wxSize size
= m_control
->GetSize();
1202 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1204 // check if the checkbox is not too big/small for this cell
1205 wxSize sizeBest
= m_control
->GetBestSize();
1206 if ( !(size
== sizeBest
) )
1208 // reset to default size if it had been made smaller
1214 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1216 // leave 1 pixel margin
1217 size
.x
= size
.y
= minSize
- 2;
1224 m_control
->SetSize(size
);
1227 // position it in the centre of the rectangle (TODO: support alignment?)
1229 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1230 // the checkbox without label still has some space to the right in wxGTK,
1231 // so shift it to the right
1233 #elif defined(__WXMSW__)
1234 // here too, but in other way
1239 int hAlign
= wxALIGN_CENTRE
;
1240 int vAlign
= wxALIGN_CENTRE
;
1242 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1245 if (hAlign
== wxALIGN_LEFT
)
1251 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1253 else if (hAlign
== wxALIGN_RIGHT
)
1255 x
= r
.x
+ r
.width
- size
.x
- 2;
1256 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1258 else if (hAlign
== wxALIGN_CENTRE
)
1260 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1261 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1264 m_control
->Move(x
, y
);
1267 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1269 m_control
->Show(show
);
1273 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1274 CBox()->SetBackgroundColour(colBg
);
1278 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1280 wxASSERT_MSG(m_control
,
1281 wxT("The wxGridCellEditor must be Created first!"));
1283 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1284 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1287 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1288 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1290 CBox()->SetValue(m_startValue
);
1294 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1297 wxASSERT_MSG(m_control
,
1298 wxT("The wxGridCellEditor must be Created first!"));
1300 bool changed
= FALSE
;
1301 bool value
= CBox()->GetValue();
1302 if ( value
!= m_startValue
)
1307 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1308 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1310 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1316 void wxGridCellBoolEditor::Reset()
1318 wxASSERT_MSG(m_control
,
1319 wxT("The wxGridCellEditor must be Created first!"));
1321 CBox()->SetValue(m_startValue
);
1324 void wxGridCellBoolEditor::StartingClick()
1326 CBox()->SetValue(!CBox()->GetValue());
1329 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1331 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1333 int keycode
= event
.GetKeyCode();
1337 case WXK_NUMPAD_MULTIPLY
:
1339 case WXK_NUMPAD_ADD
:
1341 case WXK_NUMPAD_SUBTRACT
:
1352 // return the value as "1" for true and the empty string for false
1353 wxString
wxGridCellBoolEditor::GetValue() const
1355 bool bSet
= CBox()->GetValue();
1356 return bSet
? _T("1") : wxEmptyString
;
1359 #endif // wxUSE_CHECKBOX
1363 // ----------------------------------------------------------------------------
1364 // wxGridCellChoiceEditor
1365 // ----------------------------------------------------------------------------
1367 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1369 : m_choices(choices
),
1370 m_allowOthers(allowOthers
) { }
1372 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1373 const wxString choices
[],
1375 : m_allowOthers(allowOthers
)
1379 m_choices
.Alloc(count
);
1380 for ( size_t n
= 0; n
< count
; n
++ )
1382 m_choices
.Add(choices
[n
]);
1387 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1389 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1390 editor
->m_allowOthers
= m_allowOthers
;
1391 editor
->m_choices
= m_choices
;
1396 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1398 wxEvtHandler
* evtHandler
)
1400 size_t count
= m_choices
.GetCount();
1401 wxString
*choices
= new wxString
[count
];
1402 for ( size_t n
= 0; n
< count
; n
++ )
1404 choices
[n
] = m_choices
[n
];
1407 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1408 wxDefaultPosition
, wxDefaultSize
,
1410 m_allowOthers
? 0 : wxCB_READONLY
);
1414 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1417 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1418 wxGridCellAttr
* attr
)
1420 // as we fill the entire client area, don't do anything here to minimize
1423 // TODO: It doesn't actually fill the client area since the height of a
1424 // combo always defaults to the standard... Until someone has time to
1425 // figure out the right rectangle to paint, just do it the normal way...
1426 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1429 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1431 wxASSERT_MSG(m_control
,
1432 wxT("The wxGridCellEditor must be Created first!"));
1434 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1437 Combo()->SetValue(m_startValue
);
1440 // find the right position, or default to the first if not found
1441 int pos
= Combo()->FindString(m_startValue
);
1444 Combo()->SetSelection(pos
);
1446 Combo()->SetInsertionPointEnd();
1447 Combo()->SetFocus();
1450 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1453 wxString value
= Combo()->GetValue();
1454 bool changed
= value
!= m_startValue
;
1457 grid
->GetTable()->SetValue(row
, col
, value
);
1459 m_startValue
= wxEmptyString
;
1461 Combo()->SetValue(m_startValue
);
1463 Combo()->SetSelection(0);
1468 void wxGridCellChoiceEditor::Reset()
1470 Combo()->SetValue(m_startValue
);
1471 Combo()->SetInsertionPointEnd();
1474 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1484 wxStringTokenizer
tk(params
, _T(','));
1485 while ( tk
.HasMoreTokens() )
1487 m_choices
.Add(tk
.GetNextToken());
1491 // return the value in the text control
1492 wxString
wxGridCellChoiceEditor::GetValue() const
1494 return Combo()->GetValue();
1497 #endif // wxUSE_COMBOBOX
1499 // ----------------------------------------------------------------------------
1500 // wxGridCellEditorEvtHandler
1501 // ----------------------------------------------------------------------------
1503 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1505 switch ( event
.GetKeyCode() )
1509 m_grid
->DisableCellEditControl();
1513 m_grid
->GetEventHandler()->ProcessEvent( event
);
1517 case WXK_NUMPAD_ENTER
:
1518 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1519 m_editor
->HandleReturn(event
);
1528 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1530 switch ( event
.GetKeyCode() )
1535 case WXK_NUMPAD_ENTER
:
1543 // ----------------------------------------------------------------------------
1544 // wxGridCellWorker is an (almost) empty common base class for
1545 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1546 // ----------------------------------------------------------------------------
1548 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1553 wxGridCellWorker::~wxGridCellWorker()
1557 // ============================================================================
1559 // ============================================================================
1561 // ----------------------------------------------------------------------------
1562 // wxGridCellRenderer
1563 // ----------------------------------------------------------------------------
1565 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1566 wxGridCellAttr
& attr
,
1569 int WXUNUSED(row
), int WXUNUSED(col
),
1572 dc
.SetBackgroundMode( wxSOLID
);
1574 // grey out fields if the grid is disabled
1575 if( grid
.IsEnabled() )
1579 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1583 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1588 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1591 dc
.SetPen( *wxTRANSPARENT_PEN
);
1592 dc
.DrawRectangle(rect
);
1595 // ----------------------------------------------------------------------------
1596 // wxGridCellStringRenderer
1597 // ----------------------------------------------------------------------------
1599 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1600 wxGridCellAttr
& attr
,
1604 dc
.SetBackgroundMode( wxTRANSPARENT
);
1606 // TODO some special colours for attr.IsReadOnly() case?
1608 // different coloured text when the grid is disabled
1609 if( grid
.IsEnabled() )
1613 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1614 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1618 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1619 dc
.SetTextForeground( attr
.GetTextColour() );
1624 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1625 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1628 dc
.SetFont( attr
.GetFont() );
1631 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1633 const wxString
& text
)
1635 wxCoord x
= 0, y
= 0, max_x
= 0;
1636 dc
.SetFont(attr
.GetFont());
1637 wxStringTokenizer
tk(text
, _T('\n'));
1638 while ( tk
.HasMoreTokens() )
1640 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1641 max_x
= wxMax(max_x
, x
);
1644 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1646 return wxSize(max_x
, y
);
1649 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1650 wxGridCellAttr
& attr
,
1654 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1657 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1658 wxGridCellAttr
& attr
,
1660 const wxRect
& rectCell
,
1664 wxRect rect
= rectCell
;
1667 // erase only this cells background, overflow cells should have been erased
1668 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1671 attr
.GetAlignment(&hAlign
, &vAlign
);
1673 int overflowCols
= 0;
1675 if (attr
.GetOverflow())
1677 int cols
= grid
.GetNumberCols();
1678 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1679 int cell_rows
, cell_cols
;
1680 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1681 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1683 int i
, c_cols
, c_rows
;
1684 for (i
= col
+cell_cols
; i
< cols
; i
++)
1686 bool is_empty
= TRUE
;
1687 for (int j
=row
; j
<row
+cell_rows
; j
++)
1689 // check w/ anchor cell for multicell block
1690 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1691 if (c_rows
> 0) c_rows
= 0;
1692 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1699 rect
.width
+= grid
.GetColSize(i
);
1705 if (rect
.width
>= best_width
) break;
1707 overflowCols
= i
- col
- cell_cols
+ 1;
1708 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1711 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1713 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1715 clip
.x
+= rectCell
.width
;
1716 // draw each overflow cell individually
1717 int col_end
= col
+cell_cols
+overflowCols
;
1718 if (col_end
>= grid
.GetNumberCols())
1719 col_end
= grid
.GetNumberCols() - 1;
1720 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1722 clip
.width
= grid
.GetColSize(i
) - 1;
1723 dc
.DestroyClippingRegion();
1724 dc
.SetClippingRegion(clip
);
1726 SetTextColoursAndFont(grid
, attr
, dc
,
1727 grid
.IsInSelection(row
,i
));
1729 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1730 rect
, hAlign
, vAlign
);
1731 clip
.x
+= grid
.GetColSize(i
) - 1;
1737 dc
.DestroyClippingRegion();
1741 // now we only have to draw the text
1742 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1744 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1745 rect
, hAlign
, vAlign
);
1748 // ----------------------------------------------------------------------------
1749 // wxGridCellNumberRenderer
1750 // ----------------------------------------------------------------------------
1752 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1754 wxGridTableBase
*table
= grid
.GetTable();
1756 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1758 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1762 text
= table
->GetValue(row
, col
);
1768 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1769 wxGridCellAttr
& attr
,
1771 const wxRect
& rectCell
,
1775 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1777 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1779 // draw the text right aligned by default
1781 attr
.GetAlignment(&hAlign
, &vAlign
);
1782 hAlign
= wxALIGN_RIGHT
;
1784 wxRect rect
= rectCell
;
1787 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1790 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1791 wxGridCellAttr
& attr
,
1795 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1798 // ----------------------------------------------------------------------------
1799 // wxGridCellFloatRenderer
1800 // ----------------------------------------------------------------------------
1802 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1805 SetPrecision(precision
);
1808 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1810 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1811 renderer
->m_width
= m_width
;
1812 renderer
->m_precision
= m_precision
;
1813 renderer
->m_format
= m_format
;
1818 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1820 wxGridTableBase
*table
= grid
.GetTable();
1825 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1827 val
= table
->GetValueAsDouble(row
, col
);
1832 text
= table
->GetValue(row
, col
);
1833 hasDouble
= text
.ToDouble(&val
);
1840 if ( m_width
== -1 )
1842 if ( m_precision
== -1 )
1844 // default width/precision
1845 m_format
= _T("%f");
1849 m_format
.Printf(_T("%%.%df"), m_precision
);
1852 else if ( m_precision
== -1 )
1854 // default precision
1855 m_format
.Printf(_T("%%%d.f"), m_width
);
1859 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1863 text
.Printf(m_format
, val
);
1866 //else: text already contains the string
1871 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1872 wxGridCellAttr
& attr
,
1874 const wxRect
& rectCell
,
1878 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1880 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1882 // draw the text right aligned by default
1884 attr
.GetAlignment(&hAlign
, &vAlign
);
1885 hAlign
= wxALIGN_RIGHT
;
1887 wxRect rect
= rectCell
;
1890 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1893 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1894 wxGridCellAttr
& attr
,
1898 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1901 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1905 // reset to defaults
1911 wxString tmp
= params
.BeforeFirst(_T(','));
1915 if ( tmp
.ToLong(&width
) )
1917 SetWidth((int)width
);
1921 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1925 tmp
= params
.AfterFirst(_T(','));
1929 if ( tmp
.ToLong(&precision
) )
1931 SetPrecision((int)precision
);
1935 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1943 // ----------------------------------------------------------------------------
1944 // wxGridCellBoolRenderer
1945 // ----------------------------------------------------------------------------
1947 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1949 // FIXME these checkbox size calculations are really ugly...
1951 // between checkmark and box
1952 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1954 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1955 wxGridCellAttr
& WXUNUSED(attr
),
1960 // compute it only once (no locks for MT safeness in GUI thread...)
1961 if ( !ms_sizeCheckMark
.x
)
1963 // get checkbox size
1964 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1965 wxSize size
= checkbox
->GetBestSize();
1966 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1968 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1969 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1970 checkSize
-= size
.y
/ 2;
1975 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1978 return ms_sizeCheckMark
;
1981 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1982 wxGridCellAttr
& attr
,
1988 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1990 // draw a check mark in the centre (ignoring alignment - TODO)
1991 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1993 // don't draw outside the cell
1994 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1995 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1997 // and even leave (at least) 1 pixel margin
1998 size
.x
= size
.y
= minSize
- 2;
2001 // draw a border around checkmark
2003 attr
.GetAlignment(& hAlign
, &vAlign
);
2006 if (hAlign
== wxALIGN_CENTRE
)
2008 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2009 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2010 rectBorder
.width
= size
.x
;
2011 rectBorder
.height
= size
.y
;
2013 else if (hAlign
== wxALIGN_LEFT
)
2015 rectBorder
.x
= rect
.x
+ 2;
2016 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2017 rectBorder
.width
= size
.x
;
2018 rectBorder
.height
= size
.y
;
2020 else if (hAlign
== wxALIGN_RIGHT
)
2022 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2023 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2024 rectBorder
.width
= size
.x
;
2025 rectBorder
.height
= size
.y
;
2029 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2030 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2033 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2034 value
= !( !cellval
|| (cellval
== wxT("0")) );
2039 wxRect rectMark
= rectBorder
;
2041 // MSW DrawCheckMark() is weird (and should probably be changed...)
2042 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2046 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2049 dc
.SetTextForeground(attr
.GetTextColour());
2050 dc
.DrawCheckMark(rectMark
);
2053 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2054 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2055 dc
.DrawRectangle(rectBorder
);
2058 // ----------------------------------------------------------------------------
2060 // ----------------------------------------------------------------------------
2062 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2066 m_isReadOnly
= Unset
;
2071 m_attrkind
= wxGridCellAttr::Cell
;
2073 m_sizeRows
= m_sizeCols
= 1;
2074 m_overflow
= UnsetOverflow
;
2076 SetDefAttr(attrDefault
);
2079 wxGridCellAttr
*wxGridCellAttr::Clone() const
2081 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2083 if ( HasTextColour() )
2084 attr
->SetTextColour(GetTextColour());
2085 if ( HasBackgroundColour() )
2086 attr
->SetBackgroundColour(GetBackgroundColour());
2088 attr
->SetFont(GetFont());
2089 if ( HasAlignment() )
2090 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2092 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2096 attr
->SetRenderer(m_renderer
);
2097 m_renderer
->IncRef();
2101 attr
->SetEditor(m_editor
);
2106 attr
->SetReadOnly();
2108 attr
->SetKind( m_attrkind
);
2113 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2115 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2116 SetTextColour(mergefrom
->GetTextColour());
2117 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2118 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2119 if ( !HasFont() && mergefrom
->HasFont() )
2120 SetFont(mergefrom
->GetFont());
2121 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2123 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2124 SetAlignment(hAlign
, vAlign
);
2127 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2129 // Directly access member functions as GetRender/Editor don't just return
2130 // m_renderer/m_editor
2132 // Maybe add support for merge of Render and Editor?
2133 if (!HasRenderer() && mergefrom
->HasRenderer() )
2135 m_renderer
= mergefrom
->m_renderer
;
2136 m_renderer
->IncRef();
2138 if ( !HasEditor() && mergefrom
->HasEditor() )
2140 m_editor
= mergefrom
->m_editor
;
2143 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2144 SetReadOnly(mergefrom
->IsReadOnly());
2146 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2147 SetOverflow(mergefrom
->GetOverflow());
2149 SetDefAttr(mergefrom
->m_defGridAttr
);
2152 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2154 // The size of a cell is normally 1,1
2156 // If this cell is larger (2,2) then this is the top left cell
2157 // the other cells that will be covered (lower right cells) must be
2158 // set to negative or zero values such that
2159 // row + num_rows of the covered cell points to the larger cell (this cell)
2160 // same goes for the col + num_cols.
2162 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2164 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2165 !((num_rows
<=0)&&(num_cols
>0)) ||
2166 !((num_rows
==0)&&(num_cols
==0))),
2167 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2169 m_sizeRows
= num_rows
;
2170 m_sizeCols
= num_cols
;
2173 const wxColour
& wxGridCellAttr::GetTextColour() const
2175 if (HasTextColour())
2179 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2181 return m_defGridAttr
->GetTextColour();
2185 wxFAIL_MSG(wxT("Missing default cell attribute"));
2186 return wxNullColour
;
2191 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2193 if (HasBackgroundColour())
2195 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2196 return m_defGridAttr
->GetBackgroundColour();
2199 wxFAIL_MSG(wxT("Missing default cell attribute"));
2200 return wxNullColour
;
2205 const wxFont
& wxGridCellAttr::GetFont() const
2209 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2210 return m_defGridAttr
->GetFont();
2213 wxFAIL_MSG(wxT("Missing default cell attribute"));
2219 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2223 if ( hAlign
) *hAlign
= m_hAlign
;
2224 if ( vAlign
) *vAlign
= m_vAlign
;
2226 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2227 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2230 wxFAIL_MSG(wxT("Missing default cell attribute"));
2234 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2236 if ( num_rows
) *num_rows
= m_sizeRows
;
2237 if ( num_cols
) *num_cols
= m_sizeCols
;
2240 // GetRenderer and GetEditor use a slightly different decision path about
2241 // which attribute to use. If a non-default attr object has one then it is
2242 // used, otherwise the default editor or renderer is fetched from the grid and
2243 // used. It should be the default for the data type of the cell. If it is
2244 // NULL (because the table has a type that the grid does not have in its
2245 // registry,) then the grid's default editor or renderer is used.
2247 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2249 wxGridCellRenderer
*renderer
;
2251 if ( m_renderer
&& this != m_defGridAttr
)
2253 // use the cells renderer if it has one
2254 renderer
= m_renderer
;
2257 else // no non default cell renderer
2259 // get default renderer for the data type
2262 // GetDefaultRendererForCell() will do IncRef() for us
2263 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2272 if (m_defGridAttr
&& this != m_defGridAttr
)
2274 // if we still don't have one then use the grid default
2275 // (no need for IncRef() here neither)
2276 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2278 else // default grid attr
2280 // use m_renderer which we had decided not to use initially
2281 renderer
= m_renderer
;
2288 // we're supposed to always find something
2289 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2294 // same as above, except for s/renderer/editor/g
2295 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2297 wxGridCellEditor
*editor
;
2299 if ( m_editor
&& this != m_defGridAttr
)
2301 // use the cells editor if it has one
2305 else // no non default cell editor
2307 // get default editor for the data type
2310 // GetDefaultEditorForCell() will do IncRef() for us
2311 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2320 if ( m_defGridAttr
&& this != m_defGridAttr
)
2322 // if we still don't have one then use the grid default
2323 // (no need for IncRef() here neither)
2324 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2326 else // default grid attr
2328 // use m_editor which we had decided not to use initially
2336 // we're supposed to always find something
2337 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2342 // ----------------------------------------------------------------------------
2343 // wxGridCellAttrData
2344 // ----------------------------------------------------------------------------
2346 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2348 int n
= FindIndex(row
, col
);
2349 if ( n
== wxNOT_FOUND
)
2351 // add the attribute
2352 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2356 // free the old attribute
2357 m_attrs
[(size_t)n
].attr
->DecRef();
2361 // change the attribute
2362 m_attrs
[(size_t)n
].attr
= attr
;
2366 // remove this attribute
2367 m_attrs
.RemoveAt((size_t)n
);
2372 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2374 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2376 int n
= FindIndex(row
, col
);
2377 if ( n
!= wxNOT_FOUND
)
2379 attr
= m_attrs
[(size_t)n
].attr
;
2386 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2388 size_t count
= m_attrs
.GetCount();
2389 for ( size_t n
= 0; n
< count
; n
++ )
2391 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2392 wxCoord row
= coords
.GetRow();
2393 if ((size_t)row
>= pos
)
2397 // If rows inserted, include row counter where necessary
2398 coords
.SetRow(row
+ numRows
);
2400 else if (numRows
< 0)
2402 // If rows deleted ...
2403 if ((size_t)row
>= pos
- numRows
)
2405 // ...either decrement row counter (if row still exists)...
2406 coords
.SetRow(row
+ numRows
);
2410 // ...or remove the attribute
2411 m_attrs
.RemoveAt((size_t)n
);
2419 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2421 size_t count
= m_attrs
.GetCount();
2422 for ( size_t n
= 0; n
< count
; n
++ )
2424 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2425 wxCoord col
= coords
.GetCol();
2426 if ( (size_t)col
>= pos
)
2430 // If rows inserted, include row counter where necessary
2431 coords
.SetCol(col
+ numCols
);
2433 else if (numCols
< 0)
2435 // If rows deleted ...
2436 if ((size_t)col
>= pos
- numCols
)
2438 // ...either decrement row counter (if row still exists)...
2439 coords
.SetCol(col
+ numCols
);
2443 // ...or remove the attribute
2444 m_attrs
.RemoveAt((size_t)n
);
2452 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2454 size_t count
= m_attrs
.GetCount();
2455 for ( size_t n
= 0; n
< count
; n
++ )
2457 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2458 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2467 // ----------------------------------------------------------------------------
2468 // wxGridRowOrColAttrData
2469 // ----------------------------------------------------------------------------
2471 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2473 size_t count
= m_attrs
.Count();
2474 for ( size_t n
= 0; n
< count
; n
++ )
2476 m_attrs
[n
]->DecRef();
2480 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2482 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2484 int n
= m_rowsOrCols
.Index(rowOrCol
);
2485 if ( n
!= wxNOT_FOUND
)
2487 attr
= m_attrs
[(size_t)n
];
2494 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2496 int i
= m_rowsOrCols
.Index(rowOrCol
);
2497 if ( i
== wxNOT_FOUND
)
2499 // add the attribute
2500 m_rowsOrCols
.Add(rowOrCol
);
2505 size_t n
= (size_t)i
;
2508 // change the attribute
2509 m_attrs
[n
]->DecRef();
2514 // remove this attribute
2515 m_attrs
[n
]->DecRef();
2516 m_rowsOrCols
.RemoveAt(n
);
2517 m_attrs
.RemoveAt(n
);
2522 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2524 size_t count
= m_attrs
.GetCount();
2525 for ( size_t n
= 0; n
< count
; n
++ )
2527 int & rowOrCol
= m_rowsOrCols
[n
];
2528 if ( (size_t)rowOrCol
>= pos
)
2530 if ( numRowsOrCols
> 0 )
2532 // If rows inserted, include row counter where necessary
2533 rowOrCol
+= numRowsOrCols
;
2535 else if ( numRowsOrCols
< 0)
2537 // If rows deleted, either decrement row counter (if row still exists)
2538 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2539 rowOrCol
+= numRowsOrCols
;
2542 m_rowsOrCols
.RemoveAt((size_t)n
);
2543 m_attrs
.RemoveAt((size_t)n
);
2551 // ----------------------------------------------------------------------------
2552 // wxGridCellAttrProvider
2553 // ----------------------------------------------------------------------------
2555 wxGridCellAttrProvider::wxGridCellAttrProvider()
2557 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2560 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2565 void wxGridCellAttrProvider::InitData()
2567 m_data
= new wxGridCellAttrProviderData
;
2570 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2571 wxGridCellAttr::wxAttrKind kind
) const
2573 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2578 case (wxGridCellAttr::Any
):
2579 //Get cached merge attributes.
2580 // Currenlty not used as no cache implemented as not mutiable
2581 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2584 //Basicaly implement old version.
2585 //Also check merge cache, so we don't have to re-merge every time..
2586 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2587 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2588 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2590 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2591 // Two or move are non NULL
2592 attr
= new wxGridCellAttr
;
2593 attr
->SetKind(wxGridCellAttr::Merged
);
2597 attr
->MergeWith(attrcell
);
2601 attr
->MergeWith(attrcol
);
2605 attr
->MergeWith(attrrow
);
2608 //store merge attr if cache implemented
2610 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2614 // one or none is non null return it or null.
2615 if(attrrow
) attr
= attrrow
;
2616 if(attrcol
) attr
= attrcol
;
2617 if(attrcell
) attr
= attrcell
;
2621 case (wxGridCellAttr::Cell
):
2622 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2624 case (wxGridCellAttr::Col
):
2625 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2627 case (wxGridCellAttr::Row
):
2628 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2632 // (wxGridCellAttr::Default):
2633 // (wxGridCellAttr::Merged):
2640 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2646 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2649 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2654 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2657 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2662 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2665 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2669 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2671 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2675 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2679 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2681 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2685 // ----------------------------------------------------------------------------
2686 // wxGridTypeRegistry
2687 // ----------------------------------------------------------------------------
2689 wxGridTypeRegistry::~wxGridTypeRegistry()
2691 size_t count
= m_typeinfo
.Count();
2692 for ( size_t i
= 0; i
< count
; i
++ )
2693 delete m_typeinfo
[i
];
2697 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2698 wxGridCellRenderer
* renderer
,
2699 wxGridCellEditor
* editor
)
2701 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2703 // is it already registered?
2704 int loc
= FindRegisteredDataType(typeName
);
2705 if ( loc
!= wxNOT_FOUND
)
2707 delete m_typeinfo
[loc
];
2708 m_typeinfo
[loc
] = info
;
2712 m_typeinfo
.Add(info
);
2716 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2718 size_t count
= m_typeinfo
.GetCount();
2719 for ( size_t i
= 0; i
< count
; i
++ )
2721 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2730 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2732 int index
= FindRegisteredDataType(typeName
);
2733 if ( index
== wxNOT_FOUND
)
2735 // check whether this is one of the standard ones, in which case
2736 // register it "on the fly"
2738 if ( typeName
== wxGRID_VALUE_STRING
)
2740 RegisterDataType(wxGRID_VALUE_STRING
,
2741 new wxGridCellStringRenderer
,
2742 new wxGridCellTextEditor
);
2744 #endif // wxUSE_TEXTCTRL
2746 if ( typeName
== wxGRID_VALUE_BOOL
)
2748 RegisterDataType(wxGRID_VALUE_BOOL
,
2749 new wxGridCellBoolRenderer
,
2750 new wxGridCellBoolEditor
);
2752 #endif // wxUSE_CHECKBOX
2754 if ( typeName
== wxGRID_VALUE_NUMBER
)
2756 RegisterDataType(wxGRID_VALUE_NUMBER
,
2757 new wxGridCellNumberRenderer
,
2758 new wxGridCellNumberEditor
);
2760 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2762 RegisterDataType(wxGRID_VALUE_FLOAT
,
2763 new wxGridCellFloatRenderer
,
2764 new wxGridCellFloatEditor
);
2766 #endif // wxUSE_TEXTCTRL
2768 if ( typeName
== wxGRID_VALUE_CHOICE
)
2770 RegisterDataType(wxGRID_VALUE_CHOICE
,
2771 new wxGridCellStringRenderer
,
2772 new wxGridCellChoiceEditor
);
2774 #endif // wxUSE_COMBOBOX
2779 // we get here only if just added the entry for this type, so return
2781 index
= m_typeinfo
.GetCount() - 1;
2787 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2789 int index
= FindDataType(typeName
);
2790 if ( index
== wxNOT_FOUND
)
2792 // the first part of the typename is the "real" type, anything after ':'
2793 // are the parameters for the renderer
2794 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2795 if ( index
== wxNOT_FOUND
)
2800 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2801 wxGridCellRenderer
*rendererOld
= renderer
;
2802 renderer
= renderer
->Clone();
2803 rendererOld
->DecRef();
2805 wxGridCellEditor
*editor
= GetEditor(index
);
2806 wxGridCellEditor
*editorOld
= editor
;
2807 editor
= editor
->Clone();
2808 editorOld
->DecRef();
2810 // do it even if there are no parameters to reset them to defaults
2811 wxString params
= typeName
.AfterFirst(_T(':'));
2812 renderer
->SetParameters(params
);
2813 editor
->SetParameters(params
);
2815 // register the new typename
2816 RegisterDataType(typeName
, renderer
, editor
);
2818 // we just registered it, it's the last one
2819 index
= m_typeinfo
.GetCount() - 1;
2825 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2827 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2833 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2835 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2841 // ----------------------------------------------------------------------------
2843 // ----------------------------------------------------------------------------
2845 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2848 wxGridTableBase::wxGridTableBase()
2850 m_view
= (wxGrid
*) NULL
;
2851 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2854 wxGridTableBase::~wxGridTableBase()
2856 delete m_attrProvider
;
2859 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2861 delete m_attrProvider
;
2862 m_attrProvider
= attrProvider
;
2865 bool wxGridTableBase::CanHaveAttributes()
2867 if ( ! GetAttrProvider() )
2869 // use the default attr provider by default
2870 SetAttrProvider(new wxGridCellAttrProvider
);
2875 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2877 if ( m_attrProvider
)
2878 return m_attrProvider
->GetAttr(row
, col
, kind
);
2880 return (wxGridCellAttr
*)NULL
;
2883 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2885 if ( m_attrProvider
)
2887 attr
->SetKind(wxGridCellAttr::Cell
);
2888 m_attrProvider
->SetAttr(attr
, row
, col
);
2892 // as we take ownership of the pointer and don't store it, we must
2898 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2900 if ( m_attrProvider
)
2902 attr
->SetKind(wxGridCellAttr::Row
);
2903 m_attrProvider
->SetRowAttr(attr
, row
);
2907 // as we take ownership of the pointer and don't store it, we must
2913 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2915 if ( m_attrProvider
)
2917 attr
->SetKind(wxGridCellAttr::Col
);
2918 m_attrProvider
->SetColAttr(attr
, col
);
2922 // as we take ownership of the pointer and don't store it, we must
2928 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2929 size_t WXUNUSED(numRows
) )
2931 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2936 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2938 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2943 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2944 size_t WXUNUSED(numRows
) )
2946 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2951 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2952 size_t WXUNUSED(numCols
) )
2954 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2959 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2961 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2966 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2967 size_t WXUNUSED(numCols
) )
2969 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2975 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2978 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2979 // how much it makes sense to us geeks.
2983 wxString
wxGridTableBase::GetColLabelValue( int col
)
2985 // default col labels are:
2986 // cols 0 to 25 : A-Z
2987 // cols 26 to 675 : AA-ZZ
2992 for ( n
= 1; ; n
++ )
2994 s
+= (_T('A') + (wxChar
)( col%26
));
2996 if ( col
< 0 ) break;
2999 // reverse the string...
3001 for ( i
= 0; i
< n
; i
++ )
3010 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3012 return wxGRID_VALUE_STRING
;
3015 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3016 const wxString
& typeName
)
3018 return typeName
== wxGRID_VALUE_STRING
;
3021 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3023 return CanGetValueAs(row
, col
, typeName
);
3026 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3031 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3036 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3041 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3042 long WXUNUSED(value
) )
3046 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3047 double WXUNUSED(value
) )
3051 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3052 bool WXUNUSED(value
) )
3057 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3058 const wxString
& WXUNUSED(typeName
) )
3063 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3064 const wxString
& WXUNUSED(typeName
),
3065 void* WXUNUSED(value
) )
3069 //////////////////////////////////////////////////////////////////////
3071 // Message class for the grid table to send requests and notifications
3075 wxGridTableMessage::wxGridTableMessage()
3077 m_table
= (wxGridTableBase
*) NULL
;
3083 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3084 int commandInt1
, int commandInt2
)
3088 m_comInt1
= commandInt1
;
3089 m_comInt2
= commandInt2
;
3094 //////////////////////////////////////////////////////////////////////
3096 // A basic grid table for string data. An object of this class will
3097 // created by wxGrid if you don't specify an alternative table class.
3100 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3102 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3104 wxGridStringTable::wxGridStringTable()
3109 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3112 m_data
.Alloc( numRows
);
3115 sa
.Alloc( numCols
);
3116 sa
.Add( wxEmptyString
, numCols
);
3118 m_data
.Add( sa
, numRows
);
3121 wxGridStringTable::~wxGridStringTable()
3125 int wxGridStringTable::GetNumberRows()
3127 return m_data
.GetCount();
3130 int wxGridStringTable::GetNumberCols()
3132 if ( m_data
.GetCount() > 0 )
3133 return m_data
[0].GetCount();
3138 wxString
wxGridStringTable::GetValue( int row
, int col
)
3140 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3142 _T("invalid row or column index in wxGridStringTable") );
3144 return m_data
[row
][col
];
3147 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3149 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3150 _T("invalid row or column index in wxGridStringTable") );
3152 m_data
[row
][col
] = value
;
3155 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3157 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3159 _T("invalid row or column index in wxGridStringTable") );
3161 return (m_data
[row
][col
] == wxEmptyString
);
3164 void wxGridStringTable::Clear()
3167 int numRows
, numCols
;
3169 numRows
= m_data
.GetCount();
3172 numCols
= m_data
[0].GetCount();
3174 for ( row
= 0; row
< numRows
; row
++ )
3176 for ( col
= 0; col
< numCols
; col
++ )
3178 m_data
[row
][col
] = wxEmptyString
;
3185 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3187 size_t curNumRows
= m_data
.GetCount();
3188 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3189 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3191 if ( pos
>= curNumRows
)
3193 return AppendRows( numRows
);
3197 sa
.Alloc( curNumCols
);
3198 sa
.Add( wxEmptyString
, curNumCols
);
3199 m_data
.Insert( sa
, pos
, numRows
);
3202 wxGridTableMessage
msg( this,
3203 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3207 GetView()->ProcessTableMessage( msg
);
3213 bool wxGridStringTable::AppendRows( size_t numRows
)
3215 size_t curNumRows
= m_data
.GetCount();
3216 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3217 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3220 if ( curNumCols
> 0 )
3222 sa
.Alloc( curNumCols
);
3223 sa
.Add( wxEmptyString
, curNumCols
);
3226 m_data
.Add( sa
, numRows
);
3230 wxGridTableMessage
msg( this,
3231 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3234 GetView()->ProcessTableMessage( msg
);
3240 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3242 size_t curNumRows
= m_data
.GetCount();
3244 if ( pos
>= curNumRows
)
3246 wxFAIL_MSG( wxString::Format
3248 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3250 (unsigned long)numRows
,
3251 (unsigned long)curNumRows
3257 if ( numRows
> curNumRows
- pos
)
3259 numRows
= curNumRows
- pos
;
3262 if ( numRows
>= curNumRows
)
3268 m_data
.RemoveAt( pos
, numRows
);
3272 wxGridTableMessage
msg( this,
3273 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3277 GetView()->ProcessTableMessage( msg
);
3283 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3287 size_t curNumRows
= m_data
.GetCount();
3288 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3289 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3291 if ( pos
>= curNumCols
)
3293 return AppendCols( numCols
);
3296 for ( row
= 0; row
< curNumRows
; row
++ )
3298 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3300 m_data
[row
].Insert( wxEmptyString
, col
);
3305 wxGridTableMessage
msg( this,
3306 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3310 GetView()->ProcessTableMessage( msg
);
3316 bool wxGridStringTable::AppendCols( size_t numCols
)
3320 size_t curNumRows
= m_data
.GetCount();
3324 // TODO: something better than this ?
3326 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3331 for ( row
= 0; row
< curNumRows
; row
++ )
3333 m_data
[row
].Add( wxEmptyString
, numCols
);
3338 wxGridTableMessage
msg( this,
3339 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3342 GetView()->ProcessTableMessage( msg
);
3348 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3352 size_t curNumRows
= m_data
.GetCount();
3353 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3354 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3356 if ( pos
>= curNumCols
)
3358 wxFAIL_MSG( wxString::Format
3360 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3362 (unsigned long)numCols
,
3363 (unsigned long)curNumCols
3368 if ( numCols
> curNumCols
- pos
)
3370 numCols
= curNumCols
- pos
;
3373 for ( row
= 0; row
< curNumRows
; row
++ )
3375 if ( numCols
>= curNumCols
)
3377 m_data
[row
].Clear();
3381 m_data
[row
].RemoveAt( pos
, numCols
);
3386 wxGridTableMessage
msg( this,
3387 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3391 GetView()->ProcessTableMessage( msg
);
3397 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3399 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3401 // using default label
3403 return wxGridTableBase::GetRowLabelValue( row
);
3407 return m_rowLabels
[ row
];
3411 wxString
wxGridStringTable::GetColLabelValue( int col
)
3413 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3415 // using default label
3417 return wxGridTableBase::GetColLabelValue( col
);
3421 return m_colLabels
[ col
];
3425 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3427 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3429 int n
= m_rowLabels
.GetCount();
3431 for ( i
= n
; i
<= row
; i
++ )
3433 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3437 m_rowLabels
[row
] = value
;
3440 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3442 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3444 int n
= m_colLabels
.GetCount();
3446 for ( i
= n
; i
<= col
; i
++ )
3448 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3452 m_colLabels
[col
] = value
;
3457 //////////////////////////////////////////////////////////////////////
3458 //////////////////////////////////////////////////////////////////////
3460 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3462 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3463 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3464 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3465 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3466 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3467 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3470 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3472 const wxPoint
&pos
, const wxSize
&size
)
3473 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3478 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3482 // NO - don't do this because it will set both the x and y origin
3483 // coords to match the parent scrolled window and we just want to
3484 // set the y coord - MB
3486 // m_owner->PrepareDC( dc );
3489 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3490 dc
.SetDeviceOrigin( 0, -y
);
3492 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3493 m_owner
->DrawRowLabels( dc
, rows
);
3497 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3499 m_owner
->ProcessRowLabelMouseEvent( event
);
3503 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3505 m_owner
->GetEventHandler()->ProcessEvent(event
);
3509 // This seems to be required for wxMotif otherwise the mouse
3510 // cursor must be in the cell edit control to get key events
3512 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3514 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3517 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3519 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3524 //////////////////////////////////////////////////////////////////////
3526 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3528 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3529 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3530 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3531 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3532 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3533 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3536 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3538 const wxPoint
&pos
, const wxSize
&size
)
3539 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3544 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3548 // NO - don't do this because it will set both the x and y origin
3549 // coords to match the parent scrolled window and we just want to
3550 // set the x coord - MB
3552 // m_owner->PrepareDC( dc );
3555 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3556 dc
.SetDeviceOrigin( -x
, 0 );
3558 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3559 m_owner
->DrawColLabels( dc
, cols
);
3563 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3565 m_owner
->ProcessColLabelMouseEvent( event
);
3568 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3570 m_owner
->GetEventHandler()->ProcessEvent(event
);
3574 // This seems to be required for wxMotif otherwise the mouse
3575 // cursor must be in the cell edit control to get key events
3577 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3579 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3582 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3584 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3589 //////////////////////////////////////////////////////////////////////
3591 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3593 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3594 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3595 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3596 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3597 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3598 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3601 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3603 const wxPoint
&pos
, const wxSize
&size
)
3604 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3609 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3613 int client_height
= 0;
3614 int client_width
= 0;
3615 GetClientSize( &client_width
, &client_height
);
3617 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3618 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3619 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3620 dc
.DrawLine( 0, 0, client_width
, 0 );
3621 dc
.DrawLine( 0, 0, 0, client_height
);
3623 dc
.SetPen( *wxWHITE_PEN
);
3624 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3625 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3629 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3631 m_owner
->ProcessCornerLabelMouseEvent( event
);
3635 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3637 m_owner
->GetEventHandler()->ProcessEvent(event
);
3640 // This seems to be required for wxMotif otherwise the mouse
3641 // cursor must be in the cell edit control to get key events
3643 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3645 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3648 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3650 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3655 //////////////////////////////////////////////////////////////////////
3657 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3659 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3660 EVT_PAINT( wxGridWindow::OnPaint
)
3661 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3662 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3663 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3664 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3665 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3668 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3669 wxGridRowLabelWindow
*rowLblWin
,
3670 wxGridColLabelWindow
*colLblWin
,
3673 const wxSize
&size
)
3674 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
,
3675 wxT("grid window") )
3679 m_rowLabelWin
= rowLblWin
;
3680 m_colLabelWin
= colLblWin
;
3681 SetBackgroundColour(_T("WHITE"));
3685 wxGridWindow::~wxGridWindow()
3690 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3692 wxPaintDC
dc( this );
3693 m_owner
->PrepareDC( dc
);
3694 wxRegion reg
= GetUpdateRegion();
3695 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3696 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3697 #if WXGRID_DRAW_LINES
3698 m_owner
->DrawAllGridLines( dc
, reg
);
3700 m_owner
->DrawGridSpace( dc
);
3701 m_owner
->DrawHighlight( dc
, DirtyCells
);
3705 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3707 wxWindow::ScrollWindow( dx
, dy
, rect
);
3708 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3709 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3713 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3715 m_owner
->ProcessGridCellMouseEvent( event
);
3718 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3720 m_owner
->GetEventHandler()->ProcessEvent(event
);
3723 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3724 // cursor must be in the cell edit control to get key events
3726 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3728 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3731 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3733 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3736 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3741 //////////////////////////////////////////////////////////////////////
3743 // Internal Helper function for computing row or column from some
3744 // (unscrolled) coordinate value, using either
3745 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3746 // of m_rowBottoms/m_ColRights to speed up the search!
3748 // Internal helper macros for simpler use of that function
3750 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3751 const wxArrayInt
& BorderArray
, int nMax
,
3754 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3755 m_minAcceptableColWidth, \
3756 m_colRights, m_numCols, TRUE)
3757 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3758 m_minAcceptableRowHeight, \
3759 m_rowBottoms, m_numRows, TRUE)
3760 /////////////////////////////////////////////////////////////////////
3762 #if wxUSE_EXTENDED_RTTI
3763 WX_DEFINE_FLAGS( wxGridStyle
)
3765 wxBEGIN_FLAGS( wxGridStyle
)
3766 // new style border flags, we put them first to
3767 // use them for streaming out
3768 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3769 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3770 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3771 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3772 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3773 wxFLAGS_MEMBER(wxBORDER_NONE
)
3775 // old style border flags
3776 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3777 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3778 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3779 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3780 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3781 wxFLAGS_MEMBER(wxBORDER
)
3783 // standard window styles
3784 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3785 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3786 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3787 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3788 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3789 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3790 wxFLAGS_MEMBER(wxVSCROLL
)
3791 wxFLAGS_MEMBER(wxHSCROLL
)
3793 wxEND_FLAGS( wxGridStyle
)
3795 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3797 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3798 wxHIDE_PROPERTY( Children
)
3799 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3800 wxEND_PROPERTIES_TABLE()
3802 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3803 wxEND_HANDLERS_TABLE()
3805 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3808 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
3811 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3814 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3815 EVT_PAINT( wxGrid::OnPaint
)
3816 EVT_SIZE( wxGrid::OnSize
)
3817 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3818 EVT_KEY_UP( wxGrid::OnKeyUp
)
3819 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3824 // in order to make sure that a size event is not
3825 // trigerred in a unfinished state
3826 m_cornerLabelWin
= NULL
;
3827 m_rowLabelWin
= NULL
;
3828 m_colLabelWin
= NULL
;
3832 wxGrid::wxGrid( wxWindow
*parent
,
3837 const wxString
& name
)
3838 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3839 m_colMinWidths(GRID_HASH_SIZE
),
3840 m_rowMinHeights(GRID_HASH_SIZE
)
3845 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3846 const wxPoint
& pos
, const wxSize
& size
,
3847 long style
, const wxString
& name
)
3849 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3850 style
| wxWANTS_CHARS
, name
))
3853 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3854 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3865 // Must do this or ~wxScrollHelper will pop the wrong event handler
3866 SetTargetWindow(this);
3868 wxSafeDecRef(m_defaultCellAttr
);
3870 #ifdef DEBUG_ATTR_CACHE
3871 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3872 wxPrintf(_T("wxGrid attribute cache statistics: "
3873 "total: %u, hits: %u (%u%%)\n"),
3874 total
, gs_nAttrCacheHits
,
3875 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3881 delete m_typeRegistry
;
3887 // ----- internal init and update functions
3890 void wxGrid::Create()
3892 m_created
= FALSE
; // set to TRUE by CreateGrid
3894 m_table
= (wxGridTableBase
*) NULL
;
3897 m_cellEditCtrlEnabled
= FALSE
;
3899 m_defaultCellAttr
= new wxGridCellAttr();
3901 // Set default cell attributes
3902 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3903 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3904 m_defaultCellAttr
->SetFont(GetFont());
3905 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3906 m_defaultCellAttr
->SetTextColour(
3907 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3908 m_defaultCellAttr
->SetBackgroundColour(
3909 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3910 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3911 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3916 m_currentCellCoords
= wxGridNoCellCoords
;
3918 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3919 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3921 // create the type registry
3922 m_typeRegistry
= new wxGridTypeRegistry
;
3925 // subwindow components that make up the wxGrid
3926 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3931 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3936 m_colLabelWin
= new wxGridColLabelWindow( this,
3941 m_gridWin
= new wxGridWindow( this,
3948 SetTargetWindow( m_gridWin
);
3954 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3955 wxGrid::wxGridSelectionModes selmode
)
3957 wxCHECK_MSG( !m_created
,
3959 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3961 m_numRows
= numRows
;
3962 m_numCols
= numCols
;
3964 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3965 m_table
->SetView( this );
3967 m_selection
= new wxGridSelection( this, selmode
);
3976 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3978 wxCHECK_RET( m_created
,
3979 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3981 m_selection
->SetSelectionMode( selmode
);
3984 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3986 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3987 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3989 return m_selection
->GetSelectionMode();
3992 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3993 wxGrid::wxGridSelectionModes selmode
)
3997 // stop all processing
4002 wxGridTableBase
*t
=m_table
;
4015 m_numRows
= table
->GetNumberRows();
4016 m_numCols
= table
->GetNumberCols();
4019 m_table
->SetView( this );
4022 m_selection
= new wxGridSelection( this, selmode
);
4035 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4036 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4038 if ( m_rowLabelWin
)
4040 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4044 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4047 m_labelTextColour
= wxColour( _T("BLACK") );
4050 m_attrCache
.row
= -1;
4051 m_attrCache
.col
= -1;
4052 m_attrCache
.attr
= NULL
;
4054 // TODO: something better than this ?
4056 m_labelFont
= this->GetFont();
4057 m_labelFont
.SetWeight( wxBOLD
);
4059 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4060 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4062 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4063 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4064 m_colLabelTextOrientation
= wxHORIZONTAL
;
4066 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4067 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4069 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4070 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4072 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4073 m_defaultRowHeight
+= 8;
4075 m_defaultRowHeight
+= 4;
4078 m_gridLineColour
= wxColour( 192,192,192 );
4079 m_gridLinesEnabled
= TRUE
;
4080 m_cellHighlightColour
= *wxBLACK
;
4081 m_cellHighlightPenWidth
= 2;
4082 m_cellHighlightROPenWidth
= 1;
4084 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4085 m_winCapture
= (wxWindow
*)NULL
;
4086 m_canDragRowSize
= TRUE
;
4087 m_canDragColSize
= TRUE
;
4088 m_canDragGridSize
= TRUE
;
4090 m_dragRowOrCol
= -1;
4091 m_isDragging
= FALSE
;
4092 m_startDragPos
= wxDefaultPosition
;
4094 m_waitForSlowClick
= FALSE
;
4096 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4097 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4099 m_currentCellCoords
= wxGridNoCellCoords
;
4101 m_selectingTopLeft
= wxGridNoCellCoords
;
4102 m_selectingBottomRight
= wxGridNoCellCoords
;
4103 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4104 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4106 m_editable
= TRUE
; // default for whole grid
4108 m_inOnKeyDown
= FALSE
;
4115 // ----------------------------------------------------------------------------
4116 // the idea is to call these functions only when necessary because they create
4117 // quite big arrays which eat memory mostly unnecessary - in particular, if
4118 // default widths/heights are used for all rows/columns, we may not use these
4121 // with some extra code, it should be possible to only store the
4122 // widths/heights different from default ones but this will be done later...
4123 // ----------------------------------------------------------------------------
4125 void wxGrid::InitRowHeights()
4127 m_rowHeights
.Empty();
4128 m_rowBottoms
.Empty();
4130 m_rowHeights
.Alloc( m_numRows
);
4131 m_rowBottoms
.Alloc( m_numRows
);
4135 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4137 for ( int i
= 0; i
< m_numRows
; i
++ )
4139 rowBottom
+= m_defaultRowHeight
;
4140 m_rowBottoms
.Add( rowBottom
);
4144 void wxGrid::InitColWidths()
4146 m_colWidths
.Empty();
4147 m_colRights
.Empty();
4149 m_colWidths
.Alloc( m_numCols
);
4150 m_colRights
.Alloc( m_numCols
);
4153 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4155 for ( int i
= 0; i
< m_numCols
; i
++ )
4157 colRight
+= m_defaultColWidth
;
4158 m_colRights
.Add( colRight
);
4162 int wxGrid::GetColWidth(int col
) const
4164 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4167 int wxGrid::GetColLeft(int col
) const
4169 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4170 : m_colRights
[col
] - m_colWidths
[col
];
4173 int wxGrid::GetColRight(int col
) const
4175 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4179 int wxGrid::GetRowHeight(int row
) const
4181 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4184 int wxGrid::GetRowTop(int row
) const
4186 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4187 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4190 int wxGrid::GetRowBottom(int row
) const
4192 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4193 : m_rowBottoms
[row
];
4196 void wxGrid::CalcDimensions()
4199 GetClientSize( &cw
, &ch
);
4201 if ( m_rowLabelWin
->IsShown() )
4202 cw
-= m_rowLabelWidth
;
4203 if ( m_colLabelWin
->IsShown() )
4204 ch
-= m_colLabelHeight
;
4207 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4208 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4210 // take into account editor if shown
4211 if( IsCellEditControlShown() )
4214 int r
= m_currentCellCoords
.GetRow();
4215 int c
= m_currentCellCoords
.GetCol();
4216 int x
= GetColLeft(c
);
4217 int y
= GetRowTop(r
);
4219 // how big is the editor
4220 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4221 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4222 editor
->GetControl()->GetSize(&w2
, &h2
);
4225 if( w2
> w
) w
= w2
;
4226 if( h2
> h
) h
= h2
;
4231 // preserve (more or less) the previous position
4233 GetViewStart( &x
, &y
);
4235 // maybe we don't need scrollbars at all?
4237 // also adjust the position to be valid for the new scroll rangs
4258 // do set scrollbar parameters
4259 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4260 GetScrollX(w
), GetScrollY(h
), x
, y
,
4261 GetBatchCount() != 0);
4263 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4264 // still must reposition the children
4269 void wxGrid::CalcWindowSizes()
4271 // escape if the window is has not been fully created yet
4273 if ( m_cornerLabelWin
== NULL
)
4277 GetClientSize( &cw
, &ch
);
4279 if ( m_cornerLabelWin
->IsShown() )
4280 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4282 if ( m_colLabelWin
->IsShown() )
4283 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4285 if ( m_rowLabelWin
->IsShown() )
4286 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4288 if ( m_gridWin
->IsShown() )
4289 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4293 // this is called when the grid table sends a message to say that it
4294 // has been redimensioned
4296 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4299 bool result
= FALSE
;
4301 // Clear the attribute cache as the attribute might refer to a different
4302 // cell than stored in the cache after adding/removing rows/columns.
4304 // By the same reasoning, the editor should be dismissed if columns are
4305 // added or removed. And for consistency, it should IMHO always be
4306 // removed, not only if the cell "underneath" it actually changes.
4307 // For now, I intentionally do not save the editor's content as the
4308 // cell it might want to save that stuff to might no longer exist.
4309 HideCellEditControl();
4311 // if we were using the default widths/heights so far, we must change them
4313 if ( m_colWidths
.IsEmpty() )
4318 if ( m_rowHeights
.IsEmpty() )
4324 switch ( msg
.GetId() )
4326 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4328 size_t pos
= msg
.GetCommandInt();
4329 int numRows
= msg
.GetCommandInt2();
4331 m_numRows
+= numRows
;
4333 if ( !m_rowHeights
.IsEmpty() )
4335 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4336 m_rowBottoms
.Insert( 0, pos
, numRows
);
4339 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4341 for ( i
= pos
; i
< m_numRows
; i
++ )
4343 bottom
+= m_rowHeights
[i
];
4344 m_rowBottoms
[i
] = bottom
;
4347 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4349 // if we have just inserted cols into an empty grid the current
4350 // cell will be undefined...
4352 SetCurrentCell( 0, 0 );
4356 m_selection
->UpdateRows( pos
, numRows
);
4357 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4359 attrProvider
->UpdateAttrRows( pos
, numRows
);
4361 if ( !GetBatchCount() )
4364 m_rowLabelWin
->Refresh();
4370 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4372 int numRows
= msg
.GetCommandInt();
4373 int oldNumRows
= m_numRows
;
4374 m_numRows
+= numRows
;
4376 if ( !m_rowHeights
.IsEmpty() )
4378 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4379 m_rowBottoms
.Add( 0, numRows
);
4382 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4384 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4386 bottom
+= m_rowHeights
[i
];
4387 m_rowBottoms
[i
] = bottom
;
4390 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4392 // if we have just inserted cols into an empty grid the current
4393 // cell will be undefined...
4395 SetCurrentCell( 0, 0 );
4397 if ( !GetBatchCount() )
4400 m_rowLabelWin
->Refresh();
4406 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4408 size_t pos
= msg
.GetCommandInt();
4409 int numRows
= msg
.GetCommandInt2();
4410 m_numRows
-= numRows
;
4412 if ( !m_rowHeights
.IsEmpty() )
4414 m_rowHeights
.RemoveAt( pos
, numRows
);
4415 m_rowBottoms
.RemoveAt( pos
, numRows
);
4418 for ( i
= 0; i
< m_numRows
; i
++ )
4420 h
+= m_rowHeights
[i
];
4421 m_rowBottoms
[i
] = h
;
4426 m_currentCellCoords
= wxGridNoCellCoords
;
4430 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4431 m_currentCellCoords
.Set( 0, 0 );
4435 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4436 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4438 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4439 // ifdef'd out following patch from Paul Gammans
4441 // No need to touch column attributes, unless we
4442 // removed _all_ rows, in this case, we remove
4443 // all column attributes.
4444 // I hate to do this here, but the
4445 // needed data is not available inside UpdateAttrRows.
4446 if ( !GetNumberRows() )
4447 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4450 if ( !GetBatchCount() )
4453 m_rowLabelWin
->Refresh();
4459 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4461 size_t pos
= msg
.GetCommandInt();
4462 int numCols
= msg
.GetCommandInt2();
4463 m_numCols
+= numCols
;
4465 if ( !m_colWidths
.IsEmpty() )
4467 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4468 m_colRights
.Insert( 0, pos
, numCols
);
4471 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4473 for ( i
= pos
; i
< m_numCols
; i
++ )
4475 right
+= m_colWidths
[i
];
4476 m_colRights
[i
] = right
;
4479 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4481 // if we have just inserted cols into an empty grid the current
4482 // cell will be undefined...
4484 SetCurrentCell( 0, 0 );
4488 m_selection
->UpdateCols( pos
, numCols
);
4489 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4491 attrProvider
->UpdateAttrCols( pos
, numCols
);
4492 if ( !GetBatchCount() )
4495 m_colLabelWin
->Refresh();
4502 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4504 int numCols
= msg
.GetCommandInt();
4505 int oldNumCols
= m_numCols
;
4506 m_numCols
+= numCols
;
4507 if ( !m_colWidths
.IsEmpty() )
4509 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4510 m_colRights
.Add( 0, numCols
);
4513 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4515 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4517 right
+= m_colWidths
[i
];
4518 m_colRights
[i
] = right
;
4521 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4523 // if we have just inserted cols into an empty grid the current
4524 // cell will be undefined...
4526 SetCurrentCell( 0, 0 );
4528 if ( !GetBatchCount() )
4531 m_colLabelWin
->Refresh();
4537 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4539 size_t pos
= msg
.GetCommandInt();
4540 int numCols
= msg
.GetCommandInt2();
4541 m_numCols
-= numCols
;
4543 if ( !m_colWidths
.IsEmpty() )
4545 m_colWidths
.RemoveAt( pos
, numCols
);
4546 m_colRights
.RemoveAt( pos
, numCols
);
4549 for ( i
= 0; i
< m_numCols
; i
++ )
4551 w
+= m_colWidths
[i
];
4557 m_currentCellCoords
= wxGridNoCellCoords
;
4561 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4562 m_currentCellCoords
.Set( 0, 0 );
4566 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4567 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4569 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4570 // ifdef'd out following patch from Paul Gammans
4572 // No need to touch row attributes, unless we
4573 // removed _all_ columns, in this case, we remove
4574 // all row attributes.
4575 // I hate to do this here, but the
4576 // needed data is not available inside UpdateAttrCols.
4577 if ( !GetNumberCols() )
4578 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4581 if ( !GetBatchCount() )
4584 m_colLabelWin
->Refresh();
4591 if (result
&& !GetBatchCount() )
4592 m_gridWin
->Refresh();
4597 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4599 wxRegionIterator
iter( reg
);
4602 wxArrayInt rowlabels
;
4609 // TODO: remove this when we can...
4610 // There is a bug in wxMotif that gives garbage update
4611 // rectangles if you jump-scroll a long way by clicking the
4612 // scrollbar with middle button. This is a work-around
4614 #if defined(__WXMOTIF__)
4616 m_gridWin
->GetClientSize( &cw
, &ch
);
4617 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4618 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4621 // logical bounds of update region
4624 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4625 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4627 // find the row labels within these bounds
4630 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4632 if ( GetRowBottom(row
) < top
)
4635 if ( GetRowTop(row
) > bottom
)
4638 rowlabels
.Add( row
);
4648 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4650 wxRegionIterator
iter( reg
);
4653 wxArrayInt colLabels
;
4660 // TODO: remove this when we can...
4661 // There is a bug in wxMotif that gives garbage update
4662 // rectangles if you jump-scroll a long way by clicking the
4663 // scrollbar with middle button. This is a work-around
4665 #if defined(__WXMOTIF__)
4667 m_gridWin
->GetClientSize( &cw
, &ch
);
4668 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4669 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4672 // logical bounds of update region
4675 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4676 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4678 // find the cells within these bounds
4681 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4683 if ( GetColRight(col
) < left
)
4686 if ( GetColLeft(col
) > right
)
4689 colLabels
.Add( col
);
4698 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4700 wxRegionIterator
iter( reg
);
4703 wxGridCellCoordsArray cellsExposed
;
4705 int left
, top
, right
, bottom
;
4710 // TODO: remove this when we can...
4711 // There is a bug in wxMotif that gives garbage update
4712 // rectangles if you jump-scroll a long way by clicking the
4713 // scrollbar with middle button. This is a work-around
4715 #if defined(__WXMOTIF__)
4717 m_gridWin
->GetClientSize( &cw
, &ch
);
4718 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4719 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4720 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4721 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4724 // logical bounds of update region
4726 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4727 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4729 // find the cells within these bounds
4732 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4734 if ( GetRowBottom(row
) <= top
)
4737 if ( GetRowTop(row
) > bottom
)
4740 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4742 if ( GetColRight(col
) <= left
)
4745 if ( GetColLeft(col
) > right
)
4748 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4755 return cellsExposed
;
4759 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4762 wxPoint
pos( event
.GetPosition() );
4763 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4765 if ( event
.Dragging() )
4769 m_isDragging
= TRUE
;
4770 m_rowLabelWin
->CaptureMouse();
4773 if ( event
.LeftIsDown() )
4775 switch( m_cursorMode
)
4777 case WXGRID_CURSOR_RESIZE_ROW
:
4779 int cw
, ch
, left
, dummy
;
4780 m_gridWin
->GetClientSize( &cw
, &ch
);
4781 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4783 wxClientDC
dc( m_gridWin
);
4786 GetRowTop(m_dragRowOrCol
) +
4787 GetRowMinimalHeight(m_dragRowOrCol
) );
4788 dc
.SetLogicalFunction(wxINVERT
);
4789 if ( m_dragLastPos
>= 0 )
4791 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4793 dc
.DrawLine( left
, y
, left
+cw
, y
);
4798 case WXGRID_CURSOR_SELECT_ROW
:
4799 if ( (row
= YToRow( y
)) >= 0 )
4803 m_selection
->SelectRow( row
,
4804 event
.ControlDown(),
4811 // default label to suppress warnings about "enumeration value
4812 // 'xxx' not handled in switch
4820 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4825 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4826 m_isDragging
= FALSE
;
4829 // ------------ Entering or leaving the window
4831 if ( event
.Entering() || event
.Leaving() )
4833 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4837 // ------------ Left button pressed
4839 else if ( event
.LeftDown() )
4841 // don't send a label click event for a hit on the
4842 // edge of the row label - this is probably the user
4843 // wanting to resize the row
4845 if ( YToEdgeOfRow(y
) < 0 )
4849 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4851 if ( !event
.ShiftDown() && !event
.ControlDown() )
4855 if ( event
.ShiftDown() )
4857 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4860 GetNumberCols() - 1,
4861 event
.ControlDown(),
4868 m_selection
->SelectRow( row
,
4869 event
.ControlDown(),
4876 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4881 // starting to drag-resize a row
4883 if ( CanDragRowSize() )
4884 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4889 // ------------ Left double click
4891 else if (event
.LeftDClick() )
4893 int row
= YToEdgeOfRow(y
);
4898 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4900 // no default action at the moment
4905 // adjust row height depending on label text
4906 AutoSizeRowLabelSize( row
);
4908 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4914 // ------------ Left button released
4916 else if ( event
.LeftUp() )
4918 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4920 DoEndDragResizeRow();
4922 // Note: we are ending the event *after* doing
4923 // default processing in this case
4925 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4928 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4933 // ------------ Right button down
4935 else if ( event
.RightDown() )
4939 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4941 // no default action at the moment
4946 // ------------ Right double click
4948 else if ( event
.RightDClick() )
4952 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4954 // no default action at the moment
4959 // ------------ No buttons down and mouse moving
4961 else if ( event
.Moving() )
4963 m_dragRowOrCol
= YToEdgeOfRow( y
);
4964 if ( m_dragRowOrCol
>= 0 )
4966 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4968 // don't capture the mouse yet
4969 if ( CanDragRowSize() )
4970 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4973 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4975 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4981 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4984 wxPoint
pos( event
.GetPosition() );
4985 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4987 if ( event
.Dragging() )
4991 m_isDragging
= TRUE
;
4992 m_colLabelWin
->CaptureMouse();
4995 if ( event
.LeftIsDown() )
4997 switch( m_cursorMode
)
4999 case WXGRID_CURSOR_RESIZE_COL
:
5001 int cw
, ch
, dummy
, top
;
5002 m_gridWin
->GetClientSize( &cw
, &ch
);
5003 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5005 wxClientDC
dc( m_gridWin
);
5008 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5009 GetColMinimalWidth(m_dragRowOrCol
));
5010 dc
.SetLogicalFunction(wxINVERT
);
5011 if ( m_dragLastPos
>= 0 )
5013 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5015 dc
.DrawLine( x
, top
, x
, top
+ch
);
5020 case WXGRID_CURSOR_SELECT_COL
:
5021 if ( (col
= XToCol( x
)) >= 0 )
5025 m_selection
->SelectCol( col
,
5026 event
.ControlDown(),
5033 // default label to suppress warnings about "enumeration value
5034 // 'xxx' not handled in switch
5042 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5047 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5048 m_isDragging
= FALSE
;
5051 // ------------ Entering or leaving the window
5053 if ( event
.Entering() || event
.Leaving() )
5055 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5059 // ------------ Left button pressed
5061 else if ( event
.LeftDown() )
5063 // don't send a label click event for a hit on the
5064 // edge of the col label - this is probably the user
5065 // wanting to resize the col
5067 if ( XToEdgeOfCol(x
) < 0 )
5071 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5073 if ( !event
.ShiftDown() && !event
.ControlDown() )
5077 if ( event
.ShiftDown() )
5079 m_selection
->SelectBlock( 0,
5080 m_currentCellCoords
.GetCol(),
5081 GetNumberRows() - 1, col
,
5082 event
.ControlDown(),
5089 m_selection
->SelectCol( col
,
5090 event
.ControlDown(),
5097 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5102 // starting to drag-resize a col
5104 if ( CanDragColSize() )
5105 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5110 // ------------ Left double click
5112 if ( event
.LeftDClick() )
5114 int col
= XToEdgeOfCol(x
);
5119 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5121 // no default action at the moment
5126 // adjust column width depending on label text
5127 AutoSizeColLabelSize( col
);
5129 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5135 // ------------ Left button released
5137 else if ( event
.LeftUp() )
5139 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5141 DoEndDragResizeCol();
5143 // Note: we are ending the event *after* doing
5144 // default processing in this case
5146 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5149 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5154 // ------------ Right button down
5156 else if ( event
.RightDown() )
5160 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5162 // no default action at the moment
5167 // ------------ Right double click
5169 else if ( event
.RightDClick() )
5173 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5175 // no default action at the moment
5180 // ------------ No buttons down and mouse moving
5182 else if ( event
.Moving() )
5184 m_dragRowOrCol
= XToEdgeOfCol( x
);
5185 if ( m_dragRowOrCol
>= 0 )
5187 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5189 // don't capture the cursor yet
5190 if ( CanDragColSize() )
5191 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5194 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5196 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5202 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5204 if ( event
.LeftDown() )
5206 // indicate corner label by having both row and
5209 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5215 else if ( event
.LeftDClick() )
5217 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5220 else if ( event
.RightDown() )
5222 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5224 // no default action at the moment
5228 else if ( event
.RightDClick() )
5230 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5232 // no default action at the moment
5237 void wxGrid::ChangeCursorMode(CursorMode mode
,
5242 static const wxChar
*cursorModes
[] =
5251 wxLogTrace(_T("grid"),
5252 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5253 win
== m_colLabelWin
? _T("colLabelWin")
5254 : win
? _T("rowLabelWin")
5256 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5257 #endif // __WXDEBUG__
5259 if ( mode
== m_cursorMode
&&
5260 win
== m_winCapture
&&
5261 captureMouse
== (m_winCapture
!= NULL
))
5266 // by default use the grid itself
5272 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5273 m_winCapture
= (wxWindow
*)NULL
;
5276 m_cursorMode
= mode
;
5278 switch ( m_cursorMode
)
5280 case WXGRID_CURSOR_RESIZE_ROW
:
5281 win
->SetCursor( m_rowResizeCursor
);
5284 case WXGRID_CURSOR_RESIZE_COL
:
5285 win
->SetCursor( m_colResizeCursor
);
5289 win
->SetCursor( *wxSTANDARD_CURSOR
);
5292 // we need to capture mouse when resizing
5293 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5294 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5296 if ( captureMouse
&& resize
)
5298 win
->CaptureMouse();
5303 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5306 wxPoint
pos( event
.GetPosition() );
5307 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5309 wxGridCellCoords coords
;
5310 XYToCell( x
, y
, coords
);
5312 int cell_rows
, cell_cols
;
5313 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5314 if ((cell_rows
< 0) || (cell_cols
< 0))
5316 coords
.SetRow(coords
.GetRow() + cell_rows
);
5317 coords
.SetCol(coords
.GetCol() + cell_cols
);
5320 if ( event
.Dragging() )
5322 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5324 // Don't start doing anything until the mouse has been drug at
5325 // least 3 pixels in any direction...
5328 if (m_startDragPos
== wxDefaultPosition
)
5330 m_startDragPos
= pos
;
5333 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5337 m_isDragging
= TRUE
;
5338 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5340 // Hide the edit control, so it
5341 // won't interfer with drag-shrinking.
5342 if ( IsCellEditControlShown() )
5344 HideCellEditControl();
5345 SaveEditControlValue();
5348 // Have we captured the mouse yet?
5351 m_winCapture
= m_gridWin
;
5352 m_winCapture
->CaptureMouse();
5355 if ( coords
!= wxGridNoCellCoords
)
5357 if ( event
.ControlDown() )
5359 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5360 m_selectingKeyboard
= coords
;
5361 HighlightBlock ( m_selectingKeyboard
, coords
);
5365 if ( !IsSelection() )
5367 HighlightBlock( coords
, coords
);
5371 HighlightBlock( m_currentCellCoords
, coords
);
5375 if (! IsVisible(coords
))
5377 MakeCellVisible(coords
);
5378 // TODO: need to introduce a delay or something here. The
5379 // scrolling is way to fast, at least on MSW - also on GTK.
5383 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5385 int cw
, ch
, left
, dummy
;
5386 m_gridWin
->GetClientSize( &cw
, &ch
);
5387 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5389 wxClientDC
dc( m_gridWin
);
5391 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5392 GetRowMinimalHeight(m_dragRowOrCol
) );
5393 dc
.SetLogicalFunction(wxINVERT
);
5394 if ( m_dragLastPos
>= 0 )
5396 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5398 dc
.DrawLine( left
, y
, left
+cw
, y
);
5401 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5403 int cw
, ch
, dummy
, top
;
5404 m_gridWin
->GetClientSize( &cw
, &ch
);
5405 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5407 wxClientDC
dc( m_gridWin
);
5409 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5410 GetColMinimalWidth(m_dragRowOrCol
) );
5411 dc
.SetLogicalFunction(wxINVERT
);
5412 if ( m_dragLastPos
>= 0 )
5414 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5416 dc
.DrawLine( x
, top
, x
, top
+ch
);
5423 m_isDragging
= FALSE
;
5424 m_startDragPos
= wxDefaultPosition
;
5426 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5427 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5430 if ( event
.Entering() || event
.Leaving() )
5432 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5433 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5438 // ------------ Left button pressed
5440 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5442 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5447 if ( !event
.ControlDown() )
5449 if ( event
.ShiftDown() )
5453 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5454 m_currentCellCoords
.GetCol(),
5457 event
.ControlDown(),
5463 else if ( XToEdgeOfCol(x
) < 0 &&
5464 YToEdgeOfRow(y
) < 0 )
5466 DisableCellEditControl();
5467 MakeCellVisible( coords
);
5469 if ( event
.ControlDown() )
5473 m_selection
->ToggleCellSelection( coords
.GetRow(),
5475 event
.ControlDown(),
5480 m_selectingTopLeft
= wxGridNoCellCoords
;
5481 m_selectingBottomRight
= wxGridNoCellCoords
;
5482 m_selectingKeyboard
= coords
;
5486 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5487 SetCurrentCell( coords
);
5490 if ( m_selection
->GetSelectionMode() !=
5491 wxGrid::wxGridSelectCells
)
5493 HighlightBlock( coords
, coords
);
5502 // ------------ Left double click
5504 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5506 DisableCellEditControl();
5508 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5510 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5518 // ------------ Left button released
5520 else if ( event
.LeftUp() )
5522 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5526 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5527 m_winCapture
= NULL
;
5530 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5533 EnableCellEditControl();
5535 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5536 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5537 editor
->StartingClick();
5541 m_waitForSlowClick
= FALSE
;
5543 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5544 m_selectingBottomRight
!= wxGridNoCellCoords
)
5548 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5549 m_selectingTopLeft
.GetCol(),
5550 m_selectingBottomRight
.GetRow(),
5551 m_selectingBottomRight
.GetCol(),
5552 event
.ControlDown(),
5558 m_selectingTopLeft
= wxGridNoCellCoords
;
5559 m_selectingBottomRight
= wxGridNoCellCoords
;
5561 // Show the edit control, if it has been hidden for
5563 ShowCellEditControl();
5566 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5568 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5569 DoEndDragResizeRow();
5571 // Note: we are ending the event *after* doing
5572 // default processing in this case
5574 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5576 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5578 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5579 DoEndDragResizeCol();
5581 // Note: we are ending the event *after* doing
5582 // default processing in this case
5584 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5591 // ------------ Right button down
5593 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5595 DisableCellEditControl();
5596 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5601 // no default action at the moment
5606 // ------------ Right double click
5608 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5610 DisableCellEditControl();
5611 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5616 // no default action at the moment
5620 // ------------ Moving and no button action
5622 else if ( event
.Moving() && !event
.IsButton() )
5624 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5626 // out of grid cell area
5627 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5631 int dragRow
= YToEdgeOfRow( y
);
5632 int dragCol
= XToEdgeOfCol( x
);
5634 // Dragging on the corner of a cell to resize in both
5635 // directions is not implemented yet...
5637 if ( dragRow
>= 0 && dragCol
>= 0 )
5639 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5645 m_dragRowOrCol
= dragRow
;
5647 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5649 if ( CanDragRowSize() && CanDragGridSize() )
5650 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5655 m_dragRowOrCol
= dragCol
;
5663 m_dragRowOrCol
= dragCol
;
5665 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5667 if ( CanDragColSize() && CanDragGridSize() )
5668 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5674 // Neither on a row or col edge
5676 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5678 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5684 void wxGrid::DoEndDragResizeRow()
5686 if ( m_dragLastPos
>= 0 )
5688 // erase the last line and resize the row
5690 int cw
, ch
, left
, dummy
;
5691 m_gridWin
->GetClientSize( &cw
, &ch
);
5692 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5694 wxClientDC
dc( m_gridWin
);
5696 dc
.SetLogicalFunction( wxINVERT
);
5697 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5698 HideCellEditControl();
5699 SaveEditControlValue();
5701 int rowTop
= GetRowTop(m_dragRowOrCol
);
5702 SetRowSize( m_dragRowOrCol
,
5703 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5705 if ( !GetBatchCount() )
5707 // Only needed to get the correct rect.y:
5708 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5710 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5711 rect
.width
= m_rowLabelWidth
;
5712 rect
.height
= ch
- rect
.y
;
5713 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5715 // if there is a multicell block, paint all of it
5718 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5719 int leftCol
= XToCol(left
);
5720 int rightCol
= internalXToCol(left
+cw
);
5723 for (i
=leftCol
; i
<rightCol
; i
++)
5725 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5726 if (cell_rows
< subtract_rows
)
5727 subtract_rows
= cell_rows
;
5729 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5730 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5731 rect
.height
= ch
- rect
.y
;
5734 m_gridWin
->Refresh( FALSE
, &rect
);
5737 ShowCellEditControl();
5742 void wxGrid::DoEndDragResizeCol()
5744 if ( m_dragLastPos
>= 0 )
5746 // erase the last line and resize the col
5748 int cw
, ch
, dummy
, top
;
5749 m_gridWin
->GetClientSize( &cw
, &ch
);
5750 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5752 wxClientDC
dc( m_gridWin
);
5754 dc
.SetLogicalFunction( wxINVERT
);
5755 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5756 HideCellEditControl();
5757 SaveEditControlValue();
5759 int colLeft
= GetColLeft(m_dragRowOrCol
);
5760 SetColSize( m_dragRowOrCol
,
5761 wxMax( m_dragLastPos
- colLeft
,
5762 GetColMinimalWidth(m_dragRowOrCol
) ) );
5764 if ( !GetBatchCount() )
5766 // Only needed to get the correct rect.x:
5767 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5769 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5770 rect
.width
= cw
- rect
.x
;
5771 rect
.height
= m_colLabelHeight
;
5772 m_colLabelWin
->Refresh( TRUE
, &rect
);
5774 // if there is a multicell block, paint all of it
5777 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5778 int topRow
= YToRow(top
);
5779 int bottomRow
= internalYToRow(top
+cw
);
5782 for (i
=topRow
; i
<bottomRow
; i
++)
5784 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5785 if (cell_cols
< subtract_cols
)
5786 subtract_cols
= cell_cols
;
5788 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5789 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5790 rect
.width
= cw
- rect
.x
;
5793 m_gridWin
->Refresh( FALSE
, &rect
);
5796 ShowCellEditControl();
5803 // ------ interaction with data model
5805 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5807 switch ( msg
.GetId() )
5809 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5810 return GetModelValues();
5812 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5813 return SetModelValues();
5815 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5816 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5817 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5818 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5819 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5820 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5821 return Redimension( msg
);
5830 // The behaviour of this function depends on the grid table class
5831 // Clear() function. For the default wxGridStringTable class the
5832 // behavious is to replace all cell contents with wxEmptyString but
5833 // not to change the number of rows or cols.
5835 void wxGrid::ClearGrid()
5839 if (IsCellEditControlEnabled())
5840 DisableCellEditControl();
5843 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5848 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5850 // TODO: something with updateLabels flag
5854 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5860 if (IsCellEditControlEnabled())
5861 DisableCellEditControl();
5863 bool done
= m_table
->InsertRows( pos
, numRows
);
5866 // the table will have sent the results of the insert row
5867 // operation to this view object as a grid table message
5873 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5875 // TODO: something with updateLabels flag
5879 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5885 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5887 // the table will have sent the results of the append row
5888 // operation to this view object as a grid table message
5894 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5896 // TODO: something with updateLabels flag
5900 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5906 if (IsCellEditControlEnabled())
5907 DisableCellEditControl();
5909 bool done
= m_table
->DeleteRows( pos
, numRows
);
5911 // the table will have sent the results of the delete row
5912 // operation to this view object as a grid table message
5918 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5920 // TODO: something with updateLabels flag
5924 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5930 if (IsCellEditControlEnabled())
5931 DisableCellEditControl();
5933 bool done
= m_table
->InsertCols( pos
, numCols
);
5935 // the table will have sent the results of the insert col
5936 // operation to this view object as a grid table message
5942 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5944 // TODO: something with updateLabels flag
5948 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5954 bool done
= m_table
->AppendCols( numCols
);
5956 // the table will have sent the results of the append col
5957 // operation to this view object as a grid table message
5963 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5965 // TODO: something with updateLabels flag
5969 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5975 if (IsCellEditControlEnabled())
5976 DisableCellEditControl();
5978 bool done
= m_table
->DeleteCols( pos
, numCols
);
5980 // the table will have sent the results of the delete col
5981 // operation to this view object as a grid table message
5989 // ----- event handlers
5992 // Generate a grid event based on a mouse event and
5993 // return the result of ProcessEvent()
5995 int wxGrid::SendEvent( const wxEventType type
,
5997 wxMouseEvent
& mouseEv
)
6002 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6004 int rowOrCol
= (row
== -1 ? col
: row
);
6006 wxGridSizeEvent
gridEvt( GetId(),
6010 mouseEv
.GetX() + GetRowLabelSize(),
6011 mouseEv
.GetY() + GetColLabelSize(),
6012 mouseEv
.ControlDown(),
6013 mouseEv
.ShiftDown(),
6015 mouseEv
.MetaDown() );
6017 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6018 vetoed
= !gridEvt
.IsAllowed();
6020 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6022 // Right now, it should _never_ end up here!
6023 wxGridRangeSelectEvent
gridEvt( GetId(),
6027 m_selectingBottomRight
,
6029 mouseEv
.ControlDown(),
6030 mouseEv
.ShiftDown(),
6032 mouseEv
.MetaDown() );
6034 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6035 vetoed
= !gridEvt
.IsAllowed();
6039 wxGridEvent
gridEvt( GetId(),
6043 mouseEv
.GetX() + GetRowLabelSize(),
6044 mouseEv
.GetY() + GetColLabelSize(),
6046 mouseEv
.ControlDown(),
6047 mouseEv
.ShiftDown(),
6049 mouseEv
.MetaDown() );
6050 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6051 vetoed
= !gridEvt
.IsAllowed();
6054 // A Veto'd event may not be `claimed' so test this first
6055 if (vetoed
) return -1;
6056 return claimed
? 1 : 0;
6060 // Generate a grid event of specified type and return the result
6061 // of ProcessEvent().
6063 int wxGrid::SendEvent( const wxEventType type
,
6069 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6071 int rowOrCol
= (row
== -1 ? col
: row
);
6073 wxGridSizeEvent
gridEvt( GetId(),
6078 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6079 vetoed
= !gridEvt
.IsAllowed();
6083 wxGridEvent
gridEvt( GetId(),
6088 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6089 vetoed
= !gridEvt
.IsAllowed();
6092 // A Veto'd event may not be `claimed' so test this first
6093 if (vetoed
) return -1;
6094 return claimed
? 1 : 0;
6098 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6100 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6103 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6105 // Don't do anything if between Begin/EndBatch...
6106 // EndBatch() will do all this on the last nested one anyway.
6107 if (! GetBatchCount())
6109 // Refresh to get correct scrolled position:
6110 wxScrolledWindow::Refresh(eraseb
,rect
);
6114 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6115 int width_label
, width_cell
, height_label
, height_cell
;
6118 //Copy rectangle can get scroll offsets..
6119 rect_x
= rect
->GetX();
6120 rect_y
= rect
->GetY();
6121 rectWidth
= rect
->GetWidth();
6122 rectHeight
= rect
->GetHeight();
6124 width_label
= m_rowLabelWidth
- rect_x
;
6125 if (width_label
> rectWidth
) width_label
= rectWidth
;
6127 height_label
= m_colLabelHeight
- rect_y
;
6128 if (height_label
> rectHeight
) height_label
= rectHeight
;
6130 if (rect_x
> m_rowLabelWidth
)
6132 x
= rect_x
- m_rowLabelWidth
;
6133 width_cell
= rectWidth
;
6138 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6141 if (rect_y
> m_colLabelHeight
)
6143 y
= rect_y
- m_colLabelHeight
;
6144 height_cell
= rectHeight
;
6149 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6152 // Paint corner label part intersecting rect.
6153 if ( width_label
> 0 && height_label
> 0 )
6155 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6156 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6159 // Paint col labels part intersecting rect.
6160 if ( width_cell
> 0 && height_label
> 0 )
6162 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6163 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6166 // Paint row labels part intersecting rect.
6167 if ( width_label
> 0 && height_cell
> 0 )
6169 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6170 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6173 // Paint cell area part intersecting rect.
6174 if ( width_cell
> 0 && height_cell
> 0 )
6176 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6177 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6182 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6183 m_colLabelWin
->Refresh(eraseb
, NULL
);
6184 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6185 m_gridWin
->Refresh(eraseb
, NULL
);
6190 void wxGrid::OnSize( wxSizeEvent
& event
)
6192 // position the child windows
6195 // don't call CalcDimensions() from here, the base class handles the size
6201 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6203 if ( m_inOnKeyDown
)
6205 // shouldn't be here - we are going round in circles...
6207 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6210 m_inOnKeyDown
= TRUE
;
6212 // propagate the event up and see if it gets processed
6214 wxWindow
*parent
= GetParent();
6215 wxKeyEvent
keyEvt( event
);
6216 keyEvt
.SetEventObject( parent
);
6218 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6221 // try local handlers
6223 switch ( event
.GetKeyCode() )
6226 if ( event
.ControlDown() )
6228 MoveCursorUpBlock( event
.ShiftDown() );
6232 MoveCursorUp( event
.ShiftDown() );
6237 if ( event
.ControlDown() )
6239 MoveCursorDownBlock( event
.ShiftDown() );
6243 MoveCursorDown( event
.ShiftDown() );
6248 if ( event
.ControlDown() )
6250 MoveCursorLeftBlock( event
.ShiftDown() );
6254 MoveCursorLeft( event
.ShiftDown() );
6259 if ( event
.ControlDown() )
6261 MoveCursorRightBlock( event
.ShiftDown() );
6265 MoveCursorRight( event
.ShiftDown() );
6270 case WXK_NUMPAD_ENTER
:
6271 if ( event
.ControlDown() )
6273 event
.Skip(); // to let the edit control have the return
6277 if ( GetGridCursorRow() < GetNumberRows()-1 )
6279 MoveCursorDown( event
.ShiftDown() );
6283 // at the bottom of a column
6284 HideCellEditControl();
6285 SaveEditControlValue();
6295 if (event
.ShiftDown())
6297 if ( GetGridCursorCol() > 0 )
6299 MoveCursorLeft( FALSE
);
6304 HideCellEditControl();
6305 SaveEditControlValue();
6310 if ( GetGridCursorCol() < GetNumberCols()-1 )
6312 MoveCursorRight( FALSE
);
6317 HideCellEditControl();
6318 SaveEditControlValue();
6324 if ( event
.ControlDown() )
6326 MakeCellVisible( 0, 0 );
6327 SetCurrentCell( 0, 0 );
6336 if ( event
.ControlDown() )
6338 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6339 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6356 if ( event
.ControlDown() )
6360 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6361 m_currentCellCoords
.GetCol(),
6362 event
.ControlDown(),
6369 if ( !IsEditable() )
6371 MoveCursorRight( FALSE
);
6374 // Otherwise fall through to default
6377 // is it possible to edit the current cell at all?
6378 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6380 // yes, now check whether the cells editor accepts the key
6381 int row
= m_currentCellCoords
.GetRow();
6382 int col
= m_currentCellCoords
.GetCol();
6383 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6384 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6386 // <F2> is special and will always start editing, for
6387 // other keys - ask the editor itself
6388 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6389 || editor
->IsAcceptedKey(event
) )
6391 // ensure cell is visble
6392 MakeCellVisible(row
, col
);
6393 EnableCellEditControl();
6395 // a problem can arise if the cell is not completely
6396 // visible (even after calling MakeCellVisible the
6397 // control is not created and calling StartingKey will
6399 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6411 // let others process char events with modifiers or all
6412 // char events for readonly cells
6419 m_inOnKeyDown
= FALSE
;
6422 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6424 // try local handlers
6426 if ( event
.GetKeyCode() == WXK_SHIFT
)
6428 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6429 m_selectingBottomRight
!= wxGridNoCellCoords
)
6433 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6434 m_selectingTopLeft
.GetCol(),
6435 m_selectingBottomRight
.GetRow(),
6436 m_selectingBottomRight
.GetCol(),
6437 event
.ControlDown(),
6444 m_selectingTopLeft
= wxGridNoCellCoords
;
6445 m_selectingBottomRight
= wxGridNoCellCoords
;
6446 m_selectingKeyboard
= wxGridNoCellCoords
;
6450 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6454 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6456 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6458 // the event has been intercepted - do nothing
6462 wxClientDC
dc(m_gridWin
);
6465 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6467 HideCellEditControl();
6468 DisableCellEditControl();
6470 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6473 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6474 if ( !m_gridLinesEnabled
)
6482 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6484 // Otherwise refresh redraws the highlight!
6485 m_currentCellCoords
= coords
;
6487 DrawGridCellArea(dc
,cells
);
6488 DrawAllGridLines( dc
, r
);
6492 m_currentCellCoords
= coords
;
6494 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6495 DrawCellHighlight(dc
, attr
);
6500 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6503 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6507 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6510 rightCol
= GetNumberCols() - 1;
6512 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6515 bottomRow
= GetNumberRows() - 1;
6519 if ( topRow
> bottomRow
)
6526 if ( leftCol
> rightCol
)
6533 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6534 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6536 // First the case that we selected a completely new area
6537 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6538 m_selectingBottomRight
== wxGridNoCellCoords
)
6541 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6542 wxGridCellCoords ( bottomRow
, rightCol
) );
6543 m_gridWin
->Refresh( FALSE
, &rect
);
6545 // Now handle changing an existing selection area.
6546 else if ( m_selectingTopLeft
!= updateTopLeft
||
6547 m_selectingBottomRight
!= updateBottomRight
)
6549 // Compute two optimal update rectangles:
6550 // Either one rectangle is a real subset of the
6551 // other, or they are (almost) disjoint!
6553 bool need_refresh
[4];
6557 need_refresh
[3] = FALSE
;
6560 // Store intermediate values
6561 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6562 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6563 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6564 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6566 // Determine the outer/inner coordinates.
6567 if (oldLeft
> leftCol
)
6573 if (oldTop
> topRow
)
6579 if (oldRight
< rightCol
)
6582 oldRight
= rightCol
;
6585 if (oldBottom
< bottomRow
)
6588 oldBottom
= bottomRow
;
6592 // Now, either the stuff marked old is the outer
6593 // rectangle or we don't have a situation where one
6594 // is contained in the other.
6596 if ( oldLeft
< leftCol
)
6598 // Refresh the newly selected or deselected
6599 // area to the left of the old or new selection.
6600 need_refresh
[0] = TRUE
;
6601 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6603 wxGridCellCoords ( oldBottom
,
6607 if ( oldTop
< topRow
)
6609 // Refresh the newly selected or deselected
6610 // area above the old or new selection.
6611 need_refresh
[1] = TRUE
;
6612 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6614 wxGridCellCoords ( topRow
- 1,
6618 if ( oldRight
> rightCol
)
6620 // Refresh the newly selected or deselected
6621 // area to the right of the old or new selection.
6622 need_refresh
[2] = TRUE
;
6623 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6625 wxGridCellCoords ( oldBottom
,
6629 if ( oldBottom
> bottomRow
)
6631 // Refresh the newly selected or deselected
6632 // area below the old or new selection.
6633 need_refresh
[3] = TRUE
;
6634 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6636 wxGridCellCoords ( oldBottom
,
6640 // various Refresh() calls
6641 for (i
= 0; i
< 4; i
++ )
6642 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6643 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6646 m_selectingTopLeft
= updateTopLeft
;
6647 m_selectingBottomRight
= updateBottomRight
;
6651 // ------ functions to get/send data (see also public functions)
6654 bool wxGrid::GetModelValues()
6656 // Hide the editor, so it won't hide a changed value.
6657 HideCellEditControl();
6661 // all we need to do is repaint the grid
6663 m_gridWin
->Refresh();
6671 bool wxGrid::SetModelValues()
6675 // Disable the editor, so it won't hide a changed value.
6676 // Do we also want to save the current value of the editor first?
6678 DisableCellEditControl();
6682 for ( row
= 0; row
< m_numRows
; row
++ )
6684 for ( col
= 0; col
< m_numCols
; col
++ )
6686 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6698 // Note - this function only draws cells that are in the list of
6699 // exposed cells (usually set from the update region by
6700 // CalcExposedCells)
6702 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6704 if ( !m_numRows
|| !m_numCols
) return;
6706 int i
, numCells
= cells
.GetCount();
6707 int row
, col
, cell_rows
, cell_cols
;
6708 wxGridCellCoordsArray redrawCells
;
6710 for ( i
= numCells
-1; i
>= 0; i
-- )
6712 row
= cells
[i
].GetRow();
6713 col
= cells
[i
].GetCol();
6714 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6716 // If this cell is part of a multicell block, find owner for repaint
6717 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6719 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6720 bool marked
= FALSE
;
6721 for ( int j
= 0; j
< numCells
; j
++ )
6723 if ( cell
== cells
[j
] )
6731 int count
= redrawCells
.GetCount();
6732 for (int j
= 0; j
< count
; j
++)
6734 if ( cell
== redrawCells
[j
] )
6740 if (!marked
) redrawCells
.Add( cell
);
6742 continue; // don't bother drawing this cell
6745 // If this cell is empty, find cell to left that might want to overflow
6746 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6748 for ( int l
= 0; l
< cell_rows
; l
++ )
6750 // find a cell in this row to left alreay marked for repaint
6752 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6753 if ((redrawCells
[k
].GetCol() < left
) &&
6754 (redrawCells
[k
].GetRow() == row
))
6755 left
=redrawCells
[k
].GetCol();
6757 if (left
== col
) left
= 0; // oh well
6759 for (int j
= col
-1; j
>= left
; j
--)
6761 if (!m_table
->IsEmptyCell(row
+l
, j
))
6763 if (GetCellOverflow(row
+l
, j
))
6765 wxGridCellCoords
cell(row
+l
, j
);
6766 bool marked
= FALSE
;
6768 for (int k
= 0; k
< numCells
; k
++)
6770 if ( cell
== cells
[k
] )
6778 int count
= redrawCells
.GetCount();
6779 for (int k
= 0; k
< count
; k
++)
6781 if ( cell
== redrawCells
[k
] )
6787 if (!marked
) redrawCells
.Add( cell
);
6795 DrawCell( dc
, cells
[i
] );
6798 numCells
= redrawCells
.GetCount();
6800 for ( i
= numCells
- 1; i
>= 0; i
-- )
6802 DrawCell( dc
, redrawCells
[i
] );
6807 void wxGrid::DrawGridSpace( wxDC
& dc
)
6810 m_gridWin
->GetClientSize( &cw
, &ch
);
6813 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6815 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6816 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6818 if ( right
> rightCol
|| bottom
> bottomRow
)
6821 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6823 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6824 dc
.SetPen( *wxTRANSPARENT_PEN
);
6826 if ( right
> rightCol
)
6828 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6831 if ( bottom
> bottomRow
)
6833 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6839 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6841 int row
= coords
.GetRow();
6842 int col
= coords
.GetCol();
6844 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6847 // we draw the cell border ourselves
6848 #if !WXGRID_DRAW_LINES
6849 if ( m_gridLinesEnabled
)
6850 DrawCellBorder( dc
, coords
);
6853 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6855 bool isCurrent
= coords
== m_currentCellCoords
;
6857 wxRect rect
= CellToRect( row
, col
);
6859 // if the editor is shown, we should use it and not the renderer
6860 // Note: However, only if it is really _shown_, i.e. not hidden!
6861 if ( isCurrent
&& IsCellEditControlShown() )
6863 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6864 editor
->PaintBackground(rect
, attr
);
6869 // but all the rest is drawn by the cell renderer and hence may be
6871 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6872 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6879 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6881 int row
= m_currentCellCoords
.GetRow();
6882 int col
= m_currentCellCoords
.GetCol();
6884 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6887 wxRect rect
= CellToRect(row
, col
);
6889 // hmmm... what could we do here to show that the cell is disabled?
6890 // for now, I just draw a thinner border than for the other ones, but
6891 // it doesn't look really good
6893 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6897 // The center of th drawn line is where the position/width/height of
6898 // the rectangle is actually at, (on wxMSW atr least,) so we will
6899 // reduce the size of the rectangle to compensate for the thickness of
6900 // the line. If this is too strange on non wxMSW platforms then
6901 // please #ifdef this appropriately.
6902 rect
.x
+= penWidth
/2;
6903 rect
.y
+= penWidth
/2;
6904 rect
.width
-= penWidth
-1;
6905 rect
.height
-= penWidth
-1;
6908 // Now draw the rectangle
6909 // use the cellHighlightColour if the cell is inside a selection, this
6910 // will ensure the cell is always visible.
6911 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6912 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6913 dc
.DrawRectangle(rect
);
6917 // VZ: my experiments with 3d borders...
6919 // how to properly set colours for arbitrary bg?
6920 wxCoord x1
= rect
.x
,
6922 x2
= rect
.x
+ rect
.width
-1,
6923 y2
= rect
.y
+ rect
.height
-1;
6925 dc
.SetPen(*wxWHITE_PEN
);
6926 dc
.DrawLine(x1
, y1
, x2
, y1
);
6927 dc
.DrawLine(x1
, y1
, x1
, y2
);
6929 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6930 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6932 dc
.SetPen(*wxBLACK_PEN
);
6933 dc
.DrawLine(x1
, y2
, x2
, y2
);
6934 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6939 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6941 int row
= coords
.GetRow();
6942 int col
= coords
.GetCol();
6943 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6946 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6948 wxRect rect
= CellToRect( row
, col
);
6950 // right hand border
6952 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6953 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6957 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6958 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6961 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6963 // This if block was previously in wxGrid::OnPaint but that doesn't
6964 // seem to get called under wxGTK - MB
6966 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6967 m_numRows
&& m_numCols
)
6969 m_currentCellCoords
.Set(0, 0);
6972 if ( IsCellEditControlShown() )
6974 // don't show highlight when the edit control is shown
6978 // if the active cell was repainted, repaint its highlight too because it
6979 // might have been damaged by the grid lines
6980 size_t count
= cells
.GetCount();
6981 for ( size_t n
= 0; n
< count
; n
++ )
6983 if ( cells
[n
] == m_currentCellCoords
)
6985 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6986 DrawCellHighlight(dc
, attr
);
6994 // TODO: remove this ???
6995 // This is used to redraw all grid lines e.g. when the grid line colour
6998 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7000 #if !WXGRID_DRAW_LINES
7004 if ( !m_gridLinesEnabled
||
7006 !m_numCols
) return;
7008 int top
, bottom
, left
, right
;
7010 #if 0 //#ifndef __WXGTK__
7014 m_gridWin
->GetClientSize(&cw
, &ch
);
7016 // virtual coords of visible area
7018 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7019 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7024 reg
.GetBox(x
, y
, w
, h
);
7025 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7026 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7030 m_gridWin
->GetClientSize(&cw
, &ch
);
7031 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7032 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7035 // avoid drawing grid lines past the last row and col
7037 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7038 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7040 // no gridlines inside multicells, clip them out
7041 int leftCol
= internalXToCol(left
);
7042 int topRow
= internalYToRow(top
);
7043 int rightCol
= internalXToCol(right
);
7044 int bottomRow
= internalYToRow(bottom
);
7045 wxRegion
clippedcells(0, 0, cw
, ch
);
7048 int i
, j
, cell_rows
, cell_cols
;
7051 for (j
=topRow
; j
<bottomRow
; j
++)
7053 for (i
=leftCol
; i
<rightCol
; i
++)
7055 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7056 if ((cell_rows
> 1) || (cell_cols
> 1))
7058 rect
= CellToRect(j
,i
);
7059 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7060 clippedcells
.Subtract(rect
);
7062 else if ((cell_rows
< 0) || (cell_cols
< 0))
7064 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7065 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7066 clippedcells
.Subtract(rect
);
7070 dc
.SetClippingRegion( clippedcells
);
7072 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7074 // horizontal grid lines
7076 // already declared above - int i;
7077 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7079 int bot
= GetRowBottom(i
) - 1;
7088 dc
.DrawLine( left
, bot
, right
, bot
);
7093 // vertical grid lines
7095 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7097 int colRight
= GetColRight(i
) - 1;
7098 if ( colRight
> right
)
7103 if ( colRight
>= left
)
7105 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7108 dc
.DestroyClippingRegion();
7112 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7114 if ( !m_numRows
) return;
7117 size_t numLabels
= rows
.GetCount();
7119 for ( i
= 0; i
< numLabels
; i
++ )
7121 DrawRowLabel( dc
, rows
[i
] );
7126 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7128 if ( GetRowHeight(row
) <= 0 )
7131 int rowTop
= GetRowTop(row
),
7132 rowBottom
= GetRowBottom(row
) - 1;
7134 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7135 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7136 m_rowLabelWidth
-1, rowBottom
);
7138 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7140 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7142 dc
.SetPen( *wxWHITE_PEN
);
7143 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7144 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7146 dc
.SetBackgroundMode( wxTRANSPARENT
);
7147 dc
.SetTextForeground( GetLabelTextColour() );
7148 dc
.SetFont( GetLabelFont() );
7151 GetRowLabelAlignment( &hAlign
, &vAlign
);
7155 rect
.SetY( GetRowTop(row
) + 2 );
7156 rect
.SetWidth( m_rowLabelWidth
- 4 );
7157 rect
.SetHeight( GetRowHeight(row
) - 4 );
7158 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7162 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7164 if ( !m_numCols
) return;
7167 size_t numLabels
= cols
.GetCount();
7169 for ( i
= 0; i
< numLabels
; i
++ )
7171 DrawColLabel( dc
, cols
[i
] );
7176 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7178 if ( GetColWidth(col
) <= 0 )
7181 int colLeft
= GetColLeft(col
),
7182 colRight
= GetColRight(col
) - 1;
7184 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7185 dc
.DrawLine( colRight
, 0,
7186 colRight
, m_colLabelHeight
-1 );
7188 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7190 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7191 colRight
+1, m_colLabelHeight
-1 );
7193 dc
.SetPen( *wxWHITE_PEN
);
7194 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7195 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7197 dc
.SetBackgroundMode( wxTRANSPARENT
);
7198 dc
.SetTextForeground( GetLabelTextColour() );
7199 dc
.SetFont( GetLabelFont() );
7201 int hAlign
, vAlign
, orient
;
7202 GetColLabelAlignment( &hAlign
, &vAlign
);
7203 orient
= GetColLabelTextOrientation();
7206 rect
.SetX( colLeft
+ 2 );
7208 rect
.SetWidth( GetColWidth(col
) - 4 );
7209 rect
.SetHeight( m_colLabelHeight
- 4 );
7210 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7213 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7214 const wxString
& value
,
7218 int textOrientation
)
7220 wxArrayString lines
;
7222 StringToLines( value
, lines
);
7225 //Forward to new API.
7226 DrawTextRectangle( dc
,
7235 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7236 const wxArrayString
& lines
,
7240 int textOrientation
)
7242 long textWidth
, textHeight
;
7243 long lineWidth
, lineHeight
;
7246 dc
.SetClippingRegion( rect
);
7248 nLines
= lines
.GetCount();
7252 float x
= 0.0, y
= 0.0;
7254 if( textOrientation
== wxHORIZONTAL
)
7255 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7257 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7261 case wxALIGN_BOTTOM
:
7262 if( textOrientation
== wxHORIZONTAL
)
7263 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7265 x
= rect
.x
+ rect
.width
- textWidth
;
7268 case wxALIGN_CENTRE
:
7269 if( textOrientation
== wxHORIZONTAL
)
7270 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7272 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7277 if( textOrientation
== wxHORIZONTAL
)
7284 // Align each line of a multi-line label
7285 for( l
= 0; l
< nLines
; l
++ )
7287 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7289 switch( horizAlign
)
7292 if( textOrientation
== wxHORIZONTAL
)
7293 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7295 y
= rect
.y
+ lineWidth
+ 1;
7298 case wxALIGN_CENTRE
:
7299 if( textOrientation
== wxHORIZONTAL
)
7300 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7302 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7307 if( textOrientation
== wxHORIZONTAL
)
7310 y
= rect
.y
+ rect
.height
- 1;
7314 if( textOrientation
== wxHORIZONTAL
)
7316 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7321 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7326 dc
.DestroyClippingRegion();
7330 // Split multi line text up into an array of strings. Any existing
7331 // contents of the string array are preserved.
7333 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7337 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7338 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7340 while ( startPos
< (int)tVal
.Length() )
7342 pos
= tVal
.Mid(startPos
).Find( eol
);
7347 else if ( pos
== 0 )
7349 lines
.Add( wxEmptyString
);
7353 lines
.Add( value
.Mid(startPos
, pos
) );
7357 if ( startPos
< (int)value
.Length() )
7359 lines
.Add( value
.Mid( startPos
) );
7364 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7365 const wxArrayString
& lines
,
7366 long *width
, long *height
)
7373 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7375 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7376 w
= wxMax( w
, lineW
);
7385 // ------ Batch processing.
7387 void wxGrid::EndBatch()
7389 if ( m_batchCount
> 0 )
7392 if ( !m_batchCount
)
7395 m_rowLabelWin
->Refresh();
7396 m_colLabelWin
->Refresh();
7397 m_cornerLabelWin
->Refresh();
7398 m_gridWin
->Refresh();
7403 // Use this, rather than wxWindow::Refresh(), to force an immediate
7404 // repainting of the grid. Has no effect if you are already inside a
7405 // BeginBatch / EndBatch block.
7407 void wxGrid::ForceRefresh()
7415 // ------ Edit control functions
7419 void wxGrid::EnableEditing( bool edit
)
7421 // TODO: improve this ?
7423 if ( edit
!= m_editable
)
7425 if(!edit
) EnableCellEditControl(edit
);
7431 void wxGrid::EnableCellEditControl( bool enable
)
7436 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7437 SetCurrentCell( 0, 0 );
7439 if ( enable
!= m_cellEditCtrlEnabled
)
7443 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7446 // this should be checked by the caller!
7447 wxASSERT_MSG( CanEnableCellControl(),
7448 _T("can't enable editing for this cell!") );
7450 // do it before ShowCellEditControl()
7451 m_cellEditCtrlEnabled
= enable
;
7453 ShowCellEditControl();
7457 //FIXME:add veto support
7458 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7460 HideCellEditControl();
7461 SaveEditControlValue();
7463 // do it after HideCellEditControl()
7464 m_cellEditCtrlEnabled
= enable
;
7469 bool wxGrid::IsCurrentCellReadOnly() const
7472 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7473 bool readonly
= attr
->IsReadOnly();
7479 bool wxGrid::CanEnableCellControl() const
7481 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7482 !IsCurrentCellReadOnly();
7486 bool wxGrid::IsCellEditControlEnabled() const
7488 // the cell edit control might be disable for all cells or just for the
7489 // current one if it's read only
7490 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7493 bool wxGrid::IsCellEditControlShown() const
7495 bool isShown
= FALSE
;
7497 if ( m_cellEditCtrlEnabled
)
7499 int row
= m_currentCellCoords
.GetRow();
7500 int col
= m_currentCellCoords
.GetCol();
7501 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7502 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7507 if ( editor
->IsCreated() )
7509 isShown
= editor
->GetControl()->IsShown();
7519 void wxGrid::ShowCellEditControl()
7521 if ( IsCellEditControlEnabled() )
7523 if ( !IsVisible( m_currentCellCoords
) )
7525 m_cellEditCtrlEnabled
= FALSE
;
7530 wxRect rect
= CellToRect( m_currentCellCoords
);
7531 int row
= m_currentCellCoords
.GetRow();
7532 int col
= m_currentCellCoords
.GetCol();
7534 // if this is part of a multicell, find owner (topleft)
7535 int cell_rows
, cell_cols
;
7536 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7537 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7541 m_currentCellCoords
.SetRow( row
);
7542 m_currentCellCoords
.SetCol( col
);
7545 // convert to scrolled coords
7547 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7549 // done in PaintBackground()
7551 // erase the highlight and the cell contents because the editor
7552 // might not cover the entire cell
7553 wxClientDC
dc( m_gridWin
);
7555 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7556 dc
.SetPen(*wxTRANSPARENT_PEN
);
7557 dc
.DrawRectangle(rect
);
7560 // cell is shifted by one pixel
7561 // However, don't allow x or y to become negative
7562 // since the SetSize() method interprets that as
7569 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7570 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7571 if ( !editor
->IsCreated() )
7573 editor
->Create(m_gridWin
, -1,
7574 new wxGridCellEditorEvtHandler(this, editor
));
7576 wxGridEditorCreatedEvent
evt(GetId(),
7577 wxEVT_GRID_EDITOR_CREATED
,
7581 editor
->GetControl());
7582 GetEventHandler()->ProcessEvent(evt
);
7586 // resize editor to overflow into righthand cells if allowed
7587 int maxWidth
= rect
.width
;
7588 wxString value
= GetCellValue(row
, col
);
7589 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7592 GetTextExtent(value
, &maxWidth
, &y
,
7593 NULL
, NULL
, &attr
->GetFont());
7594 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7596 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7597 if (rect
.x
+maxWidth
> client_right
)
7598 maxWidth
= client_right
- rect
.x
;
7600 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7602 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7603 // may have changed earlier
7604 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7607 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7608 // looks weird going over a multicell
7609 if (m_table
->IsEmptyCell(row
,i
) &&
7610 (rect
.width
< maxWidth
) && (c_rows
== 1))
7611 rect
.width
+= GetColWidth(i
);
7615 if (rect
.GetRight() > client_right
)
7616 rect
.SetRight(client_right
-1);
7619 editor
->SetCellAttr(attr
);
7620 editor
->SetSize( rect
);
7621 editor
->Show( TRUE
, attr
);
7623 // recalc dimensions in case we need to
7624 // expand the scrolled window to account for editor
7627 editor
->BeginEdit(row
, col
, this);
7628 editor
->SetCellAttr(NULL
);
7637 void wxGrid::HideCellEditControl()
7639 if ( IsCellEditControlEnabled() )
7641 int row
= m_currentCellCoords
.GetRow();
7642 int col
= m_currentCellCoords
.GetCol();
7644 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7645 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7646 editor
->Show( FALSE
);
7649 m_gridWin
->SetFocus();
7650 // refresh whole row to the right
7651 wxRect
rect( CellToRect(row
, col
) );
7652 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7653 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7654 m_gridWin
->Refresh( FALSE
, &rect
);
7659 void wxGrid::SaveEditControlValue()
7661 if ( IsCellEditControlEnabled() )
7663 int row
= m_currentCellCoords
.GetRow();
7664 int col
= m_currentCellCoords
.GetCol();
7666 wxString oldval
= GetCellValue(row
,col
);
7668 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7669 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7670 bool changed
= editor
->EndEdit(row
, col
, this);
7677 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7678 m_currentCellCoords
.GetRow(),
7679 m_currentCellCoords
.GetCol() ) < 0 ) {
7681 // Event has been vetoed, set the data back.
7682 SetCellValue(row
,col
,oldval
);
7690 // ------ Grid location functions
7691 // Note that all of these functions work with the logical coordinates of
7692 // grid cells and labels so you will need to convert from device
7693 // coordinates for mouse events etc.
7696 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7698 int row
= YToRow(y
);
7699 int col
= XToCol(x
);
7701 if ( row
== -1 || col
== -1 )
7703 coords
= wxGridNoCellCoords
;
7707 coords
.Set( row
, col
);
7712 // Internal Helper function for computing row or column from some
7713 // (unscrolled) coordinate value, using either
7714 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7715 // of m_rowBottoms/m_ColRights to speed up the search!
7717 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7718 const wxArrayInt
& BorderArray
, int nMax
,
7723 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7729 size_t i_max
= coord
/ defaultDist
,
7732 if (BorderArray
.IsEmpty())
7734 if((int) i_max
< nMax
)
7736 return clipToMinMax
? nMax
- 1 : -1;
7739 if ( i_max
>= BorderArray
.GetCount())
7740 i_max
= BorderArray
.GetCount() - 1;
7743 if ( coord
>= BorderArray
[i_max
])
7747 i_max
= coord
/ minDist
;
7749 i_max
= BorderArray
.GetCount() - 1;
7751 if ( i_max
>= BorderArray
.GetCount())
7752 i_max
= BorderArray
.GetCount() - 1;
7754 if ( coord
>= BorderArray
[i_max
])
7755 return clipToMinMax
? (int)i_max
: -1;
7756 if ( coord
< BorderArray
[0] )
7759 while ( i_max
- i_min
> 0 )
7761 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7762 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7763 if (coord
>= BorderArray
[ i_max
- 1])
7767 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7768 if (coord
< BorderArray
[median
])
7776 int wxGrid::YToRow( int y
)
7778 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7779 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, FALSE
);
7783 int wxGrid::XToCol( int x
)
7785 return CoordToRowOrCol(x
, m_defaultColWidth
,
7786 m_minAcceptableColWidth
, m_colRights
, m_numCols
, FALSE
);
7790 // return the row number that that the y coord is near the edge of, or
7791 // -1 if not near an edge
7793 int wxGrid::YToEdgeOfRow( int y
)
7796 i
= internalYToRow(y
);
7798 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7800 // We know that we are in row i, test whether we are
7801 // close enough to lower or upper border, respectively.
7802 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7804 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7812 // return the col number that that the x coord is near the edge of, or
7813 // -1 if not near an edge
7815 int wxGrid::XToEdgeOfCol( int x
)
7818 i
= internalXToCol(x
);
7820 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7822 // We know that we are in column i, test whether we are
7823 // close enough to right or left border, respectively.
7824 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7826 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7834 wxRect
wxGrid::CellToRect( int row
, int col
)
7836 wxRect
rect( -1, -1, -1, -1 );
7838 if ( row
>= 0 && row
< m_numRows
&&
7839 col
>= 0 && col
< m_numCols
)
7841 int i
, cell_rows
, cell_cols
;
7842 rect
.width
= rect
.height
= 0;
7843 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7844 // if negative then find multicell owner
7845 if (cell_rows
< 0) row
+= cell_rows
;
7846 if (cell_cols
< 0) col
+= cell_cols
;
7847 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7849 rect
.x
= GetColLeft(col
);
7850 rect
.y
= GetRowTop(row
);
7851 for (i
=col
; i
<col
+cell_cols
; i
++)
7852 rect
.width
+= GetColWidth(i
);
7853 for (i
=row
; i
<row
+cell_rows
; i
++)
7854 rect
.height
+= GetRowHeight(i
);
7857 // if grid lines are enabled, then the area of the cell is a bit smaller
7858 if (m_gridLinesEnabled
) {
7866 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7868 // get the cell rectangle in logical coords
7870 wxRect
r( CellToRect( row
, col
) );
7872 // convert to device coords
7874 int left
, top
, right
, bottom
;
7875 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7876 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7878 // check against the client area of the grid window
7881 m_gridWin
->GetClientSize( &cw
, &ch
);
7883 if ( wholeCellVisible
)
7885 // is the cell wholly visible ?
7887 return ( left
>= 0 && right
<= cw
&&
7888 top
>= 0 && bottom
<= ch
);
7892 // is the cell partly visible ?
7894 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7895 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7900 // make the specified cell location visible by doing a minimal amount
7903 void wxGrid::MakeCellVisible( int row
, int col
)
7907 int xpos
= -1, ypos
= -1;
7909 if ( row
>= 0 && row
< m_numRows
&&
7910 col
>= 0 && col
< m_numCols
)
7912 // get the cell rectangle in logical coords
7914 wxRect
r( CellToRect( row
, col
) );
7916 // convert to device coords
7918 int left
, top
, right
, bottom
;
7919 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7920 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7923 m_gridWin
->GetClientSize( &cw
, &ch
);
7929 else if ( bottom
> ch
)
7931 int h
= r
.GetHeight();
7933 for ( i
= row
-1; i
>= 0; i
-- )
7935 int rowHeight
= GetRowHeight(i
);
7936 if ( h
+ rowHeight
> ch
)
7943 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7944 // have rounding errors (this is important, because if we do, we
7945 // might not scroll at all and some cells won't be redrawn)
7947 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7949 ypos
+= GRID_SCROLL_LINE_Y
;
7956 else if ( right
> cw
)
7958 // position the view so that the cell is on the right
7960 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7961 xpos
= x0
+ (right
- cw
);
7963 // see comment for ypos above
7964 xpos
+= GRID_SCROLL_LINE_X
;
7967 if ( xpos
!= -1 || ypos
!= -1 )
7970 xpos
/= GRID_SCROLL_LINE_X
;
7972 ypos
/= GRID_SCROLL_LINE_Y
;
7973 Scroll( xpos
, ypos
);
7981 // ------ Grid cursor movement functions
7984 bool wxGrid::MoveCursorUp( bool expandSelection
)
7986 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7987 m_currentCellCoords
.GetRow() >= 0 )
7989 if ( expandSelection
)
7991 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7992 m_selectingKeyboard
= m_currentCellCoords
;
7993 if ( m_selectingKeyboard
.GetRow() > 0 )
7995 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7996 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7997 m_selectingKeyboard
.GetCol() );
7998 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8001 else if ( m_currentCellCoords
.GetRow() > 0 )
8004 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8005 m_currentCellCoords
.GetCol() );
8006 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8007 m_currentCellCoords
.GetCol() );
8018 bool wxGrid::MoveCursorDown( bool expandSelection
)
8020 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8021 m_currentCellCoords
.GetRow() < m_numRows
)
8023 if ( expandSelection
)
8025 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8026 m_selectingKeyboard
= m_currentCellCoords
;
8027 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8029 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8030 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8031 m_selectingKeyboard
.GetCol() );
8032 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8035 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8038 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8039 m_currentCellCoords
.GetCol() );
8040 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8041 m_currentCellCoords
.GetCol() );
8052 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8054 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8055 m_currentCellCoords
.GetCol() >= 0 )
8057 if ( expandSelection
)
8059 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8060 m_selectingKeyboard
= m_currentCellCoords
;
8061 if ( m_selectingKeyboard
.GetCol() > 0 )
8063 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8064 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8065 m_selectingKeyboard
.GetCol() );
8066 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8069 else if ( m_currentCellCoords
.GetCol() > 0 )
8072 MakeCellVisible( m_currentCellCoords
.GetRow(),
8073 m_currentCellCoords
.GetCol() - 1 );
8074 SetCurrentCell( m_currentCellCoords
.GetRow(),
8075 m_currentCellCoords
.GetCol() - 1 );
8086 bool wxGrid::MoveCursorRight( bool expandSelection
)
8088 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8089 m_currentCellCoords
.GetCol() < m_numCols
)
8091 if ( expandSelection
)
8093 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8094 m_selectingKeyboard
= m_currentCellCoords
;
8095 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8097 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8098 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8099 m_selectingKeyboard
.GetCol() );
8100 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8103 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8106 MakeCellVisible( m_currentCellCoords
.GetRow(),
8107 m_currentCellCoords
.GetCol() + 1 );
8108 SetCurrentCell( m_currentCellCoords
.GetRow(),
8109 m_currentCellCoords
.GetCol() + 1 );
8120 bool wxGrid::MovePageUp()
8122 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8124 int row
= m_currentCellCoords
.GetRow();
8128 m_gridWin
->GetClientSize( &cw
, &ch
);
8130 int y
= GetRowTop(row
);
8131 int newRow
= internalYToRow( y
- ch
+ 1 );
8133 if ( newRow
== row
)
8135 //row > 0 , so newrow can never be less than 0 here.
8139 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8140 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8148 bool wxGrid::MovePageDown()
8150 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8152 int row
= m_currentCellCoords
.GetRow();
8153 if ( (row
+1) < m_numRows
)
8156 m_gridWin
->GetClientSize( &cw
, &ch
);
8158 int y
= GetRowTop(row
);
8159 int newRow
= internalYToRow( y
+ ch
);
8160 if ( newRow
== row
)
8162 // row < m_numRows , so newrow can't overflow here.
8166 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8167 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8175 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8178 m_currentCellCoords
!= wxGridNoCellCoords
&&
8179 m_currentCellCoords
.GetRow() > 0 )
8181 int row
= m_currentCellCoords
.GetRow();
8182 int col
= m_currentCellCoords
.GetCol();
8184 if ( m_table
->IsEmptyCell(row
, col
) )
8186 // starting in an empty cell: find the next block of
8192 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8195 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8197 // starting at the top of a block: find the next block
8203 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8208 // starting within a block: find the top of the block
8213 if ( m_table
->IsEmptyCell(row
, col
) )
8221 MakeCellVisible( row
, col
);
8222 if ( expandSelection
)
8224 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8225 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8230 SetCurrentCell( row
, col
);
8238 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8241 m_currentCellCoords
!= wxGridNoCellCoords
&&
8242 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8244 int row
= m_currentCellCoords
.GetRow();
8245 int col
= m_currentCellCoords
.GetCol();
8247 if ( m_table
->IsEmptyCell(row
, col
) )
8249 // starting in an empty cell: find the next block of
8252 while ( row
< m_numRows
-1 )
8255 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8258 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8260 // starting at the bottom of a block: find the next block
8263 while ( row
< m_numRows
-1 )
8266 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8271 // starting within a block: find the bottom of the block
8273 while ( row
< m_numRows
-1 )
8276 if ( m_table
->IsEmptyCell(row
, col
) )
8284 MakeCellVisible( row
, col
);
8285 if ( expandSelection
)
8287 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8288 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8293 SetCurrentCell( row
, col
);
8302 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8305 m_currentCellCoords
!= wxGridNoCellCoords
&&
8306 m_currentCellCoords
.GetCol() > 0 )
8308 int row
= m_currentCellCoords
.GetRow();
8309 int col
= m_currentCellCoords
.GetCol();
8311 if ( m_table
->IsEmptyCell(row
, col
) )
8313 // starting in an empty cell: find the next block of
8319 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8322 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8324 // starting at the left of a block: find the next block
8330 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8335 // starting within a block: find the left of the block
8340 if ( m_table
->IsEmptyCell(row
, col
) )
8348 MakeCellVisible( row
, col
);
8349 if ( expandSelection
)
8351 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8352 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8357 SetCurrentCell( row
, col
);
8366 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8369 m_currentCellCoords
!= wxGridNoCellCoords
&&
8370 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8372 int row
= m_currentCellCoords
.GetRow();
8373 int col
= m_currentCellCoords
.GetCol();
8375 if ( m_table
->IsEmptyCell(row
, col
) )
8377 // starting in an empty cell: find the next block of
8380 while ( col
< m_numCols
-1 )
8383 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8386 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8388 // starting at the right of a block: find the next block
8391 while ( col
< m_numCols
-1 )
8394 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8399 // starting within a block: find the right of the block
8401 while ( col
< m_numCols
-1 )
8404 if ( m_table
->IsEmptyCell(row
, col
) )
8412 MakeCellVisible( row
, col
);
8413 if ( expandSelection
)
8415 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8416 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8421 SetCurrentCell( row
, col
);
8433 // ------ Label values and formatting
8436 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8438 *horiz
= m_rowLabelHorizAlign
;
8439 *vert
= m_rowLabelVertAlign
;
8442 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8444 *horiz
= m_colLabelHorizAlign
;
8445 *vert
= m_colLabelVertAlign
;
8448 int wxGrid::GetColLabelTextOrientation()
8450 return m_colLabelTextOrientation
;
8453 wxString
wxGrid::GetRowLabelValue( int row
)
8457 return m_table
->GetRowLabelValue( row
);
8467 wxString
wxGrid::GetColLabelValue( int col
)
8471 return m_table
->GetColLabelValue( col
);
8482 void wxGrid::SetRowLabelSize( int width
)
8484 width
= wxMax( width
, 0 );
8485 if ( width
!= m_rowLabelWidth
)
8489 m_rowLabelWin
->Show( FALSE
);
8490 m_cornerLabelWin
->Show( FALSE
);
8492 else if ( m_rowLabelWidth
== 0 )
8494 m_rowLabelWin
->Show( TRUE
);
8495 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8498 m_rowLabelWidth
= width
;
8500 wxScrolledWindow::Refresh( TRUE
);
8505 void wxGrid::SetColLabelSize( int height
)
8507 height
= wxMax( height
, 0 );
8508 if ( height
!= m_colLabelHeight
)
8512 m_colLabelWin
->Show( FALSE
);
8513 m_cornerLabelWin
->Show( FALSE
);
8515 else if ( m_colLabelHeight
== 0 )
8517 m_colLabelWin
->Show( TRUE
);
8518 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8521 m_colLabelHeight
= height
;
8523 wxScrolledWindow::Refresh( TRUE
);
8528 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8530 if ( m_labelBackgroundColour
!= colour
)
8532 m_labelBackgroundColour
= colour
;
8533 m_rowLabelWin
->SetBackgroundColour( colour
);
8534 m_colLabelWin
->SetBackgroundColour( colour
);
8535 m_cornerLabelWin
->SetBackgroundColour( colour
);
8537 if ( !GetBatchCount() )
8539 m_rowLabelWin
->Refresh();
8540 m_colLabelWin
->Refresh();
8541 m_cornerLabelWin
->Refresh();
8546 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8548 if ( m_labelTextColour
!= colour
)
8550 m_labelTextColour
= colour
;
8551 if ( !GetBatchCount() )
8553 m_rowLabelWin
->Refresh();
8554 m_colLabelWin
->Refresh();
8559 void wxGrid::SetLabelFont( const wxFont
& font
)
8562 if ( !GetBatchCount() )
8564 m_rowLabelWin
->Refresh();
8565 m_colLabelWin
->Refresh();
8569 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8571 // allow old (incorrect) defs to be used
8574 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8575 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8576 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8581 case wxTOP
: vert
= wxALIGN_TOP
; break;
8582 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8583 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8586 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8588 m_rowLabelHorizAlign
= horiz
;
8591 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8593 m_rowLabelVertAlign
= vert
;
8596 if ( !GetBatchCount() )
8598 m_rowLabelWin
->Refresh();
8602 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8604 // allow old (incorrect) defs to be used
8607 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8608 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8609 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8614 case wxTOP
: vert
= wxALIGN_TOP
; break;
8615 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8616 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8619 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8621 m_colLabelHorizAlign
= horiz
;
8624 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8626 m_colLabelVertAlign
= vert
;
8629 if ( !GetBatchCount() )
8631 m_colLabelWin
->Refresh();
8635 // Note: under MSW, the default column label font must be changed because it
8636 // does not support vertical printing
8638 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8639 // pGrid->SetLabelFont(font);
8640 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8642 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8644 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8646 m_colLabelTextOrientation
= textOrientation
;
8649 if ( !GetBatchCount() )
8651 m_colLabelWin
->Refresh();
8655 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8659 m_table
->SetRowLabelValue( row
, s
);
8660 if ( !GetBatchCount() )
8662 wxRect rect
= CellToRect( row
, 0);
8663 if ( rect
.height
> 0 )
8665 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8667 rect
.width
= m_rowLabelWidth
;
8668 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8674 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8678 m_table
->SetColLabelValue( col
, s
);
8679 if ( !GetBatchCount() )
8681 wxRect rect
= CellToRect( 0, col
);
8682 if ( rect
.width
> 0 )
8684 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8686 rect
.height
= m_colLabelHeight
;
8687 m_colLabelWin
->Refresh( TRUE
, &rect
);
8693 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8695 if ( m_gridLineColour
!= colour
)
8697 m_gridLineColour
= colour
;
8699 wxClientDC
dc( m_gridWin
);
8701 DrawAllGridLines( dc
, wxRegion() );
8706 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8708 if ( m_cellHighlightColour
!= colour
)
8710 m_cellHighlightColour
= colour
;
8712 wxClientDC
dc( m_gridWin
);
8714 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8715 DrawCellHighlight(dc
, attr
);
8720 void wxGrid::SetCellHighlightPenWidth(int width
)
8722 if (m_cellHighlightPenWidth
!= width
) {
8723 m_cellHighlightPenWidth
= width
;
8725 // Just redrawing the cell highlight is not enough since that won't
8726 // make any visible change if the the thickness is getting smaller.
8727 int row
= m_currentCellCoords
.GetRow();
8728 int col
= m_currentCellCoords
.GetCol();
8729 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8731 wxRect rect
= CellToRect(row
, col
);
8732 m_gridWin
->Refresh(TRUE
, &rect
);
8736 void wxGrid::SetCellHighlightROPenWidth(int width
)
8738 if (m_cellHighlightROPenWidth
!= width
) {
8739 m_cellHighlightROPenWidth
= width
;
8741 // Just redrawing the cell highlight is not enough since that won't
8742 // make any visible change if the the thickness is getting smaller.
8743 int row
= m_currentCellCoords
.GetRow();
8744 int col
= m_currentCellCoords
.GetCol();
8745 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8747 wxRect rect
= CellToRect(row
, col
);
8748 m_gridWin
->Refresh(TRUE
, &rect
);
8752 void wxGrid::EnableGridLines( bool enable
)
8754 if ( enable
!= m_gridLinesEnabled
)
8756 m_gridLinesEnabled
= enable
;
8758 if ( !GetBatchCount() )
8762 wxClientDC
dc( m_gridWin
);
8764 DrawAllGridLines( dc
, wxRegion() );
8768 m_gridWin
->Refresh();
8775 int wxGrid::GetDefaultRowSize()
8777 return m_defaultRowHeight
;
8780 int wxGrid::GetRowSize( int row
)
8782 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8784 return GetRowHeight(row
);
8787 int wxGrid::GetDefaultColSize()
8789 return m_defaultColWidth
;
8792 int wxGrid::GetColSize( int col
)
8794 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8796 return GetColWidth(col
);
8799 // ============================================================================
8800 // access to the grid attributes: each of them has a default value in the grid
8801 // itself and may be overidden on a per-cell basis
8802 // ============================================================================
8804 // ----------------------------------------------------------------------------
8805 // setting default attributes
8806 // ----------------------------------------------------------------------------
8808 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8810 m_defaultCellAttr
->SetBackgroundColour(col
);
8812 m_gridWin
->SetBackgroundColour(col
);
8816 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8818 m_defaultCellAttr
->SetTextColour(col
);
8821 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8823 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8826 void wxGrid::SetDefaultCellOverflow( bool allow
)
8828 m_defaultCellAttr
->SetOverflow(allow
);
8831 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8833 m_defaultCellAttr
->SetFont(font
);
8836 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8838 m_defaultCellAttr
->SetRenderer(renderer
);
8841 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8843 m_defaultCellAttr
->SetEditor(editor
);
8846 // ----------------------------------------------------------------------------
8847 // access to the default attrbiutes
8848 // ----------------------------------------------------------------------------
8850 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8852 return m_defaultCellAttr
->GetBackgroundColour();
8855 wxColour
wxGrid::GetDefaultCellTextColour()
8857 return m_defaultCellAttr
->GetTextColour();
8860 wxFont
wxGrid::GetDefaultCellFont()
8862 return m_defaultCellAttr
->GetFont();
8865 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8867 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8870 bool wxGrid::GetDefaultCellOverflow()
8872 return m_defaultCellAttr
->GetOverflow();
8875 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8877 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8880 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8882 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8885 // ----------------------------------------------------------------------------
8886 // access to cell attributes
8887 // ----------------------------------------------------------------------------
8889 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8891 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8892 wxColour colour
= attr
->GetBackgroundColour();
8897 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8899 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8900 wxColour colour
= attr
->GetTextColour();
8905 wxFont
wxGrid::GetCellFont( int row
, int col
)
8907 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8908 wxFont font
= attr
->GetFont();
8913 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8915 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8916 attr
->GetAlignment(horiz
, vert
);
8920 bool wxGrid::GetCellOverflow( int row
, int col
)
8922 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8923 bool allow
= attr
->GetOverflow();
8928 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8930 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8931 attr
->GetSize( num_rows
, num_cols
);
8935 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8937 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8938 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8944 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8946 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8947 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8953 bool wxGrid::IsReadOnly(int row
, int col
) const
8955 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8956 bool isReadOnly
= attr
->IsReadOnly();
8961 // ----------------------------------------------------------------------------
8962 // attribute support: cache, automatic provider creation, ...
8963 // ----------------------------------------------------------------------------
8965 bool wxGrid::CanHaveAttributes()
8972 return m_table
->CanHaveAttributes();
8975 void wxGrid::ClearAttrCache()
8977 if ( m_attrCache
.row
!= -1 )
8979 wxSafeDecRef(m_attrCache
.attr
);
8980 m_attrCache
.attr
= NULL
;
8981 m_attrCache
.row
= -1;
8985 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8989 wxGrid
*self
= (wxGrid
*)this; // const_cast
8991 self
->ClearAttrCache();
8992 self
->m_attrCache
.row
= row
;
8993 self
->m_attrCache
.col
= col
;
8994 self
->m_attrCache
.attr
= attr
;
8999 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9001 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9003 *attr
= m_attrCache
.attr
;
9004 wxSafeIncRef(m_attrCache
.attr
);
9006 #ifdef DEBUG_ATTR_CACHE
9007 gs_nAttrCacheHits
++;
9014 #ifdef DEBUG_ATTR_CACHE
9015 gs_nAttrCacheMisses
++;
9021 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9023 wxGridCellAttr
*attr
= NULL
;
9024 // Additional test to avoid looking at the cache e.g. for
9025 // wxNoCellCoords, as this will confuse memory management.
9028 if ( !LookupAttr(row
, col
, &attr
) )
9030 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9031 : (wxGridCellAttr
*)NULL
;
9032 CacheAttr(row
, col
, attr
);
9037 attr
->SetDefAttr(m_defaultCellAttr
);
9041 attr
= m_defaultCellAttr
;
9048 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9050 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9052 wxCHECK_MSG( m_table
, attr
,
9053 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
9055 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9058 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9060 // artificially inc the ref count to match DecRef() in caller
9062 m_table
->SetAttr(attr
, row
, col
);
9068 // ----------------------------------------------------------------------------
9069 // setting column attributes (wrappers around SetColAttr)
9070 // ----------------------------------------------------------------------------
9072 void wxGrid::SetColFormatBool(int col
)
9074 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9077 void wxGrid::SetColFormatNumber(int col
)
9079 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9082 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9084 wxString typeName
= wxGRID_VALUE_FLOAT
;
9085 if ( (width
!= -1) || (precision
!= -1) )
9087 typeName
<< _T(':') << width
<< _T(',') << precision
;
9090 SetColFormatCustom(col
, typeName
);
9093 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9095 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9097 attr
= new wxGridCellAttr
;
9098 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9099 attr
->SetRenderer(renderer
);
9101 SetColAttr(col
, attr
);
9105 // ----------------------------------------------------------------------------
9106 // setting cell attributes: this is forwarded to the table
9107 // ----------------------------------------------------------------------------
9109 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9111 if ( CanHaveAttributes() )
9113 m_table
->SetAttr(attr
, row
, col
);
9122 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9124 if ( CanHaveAttributes() )
9126 m_table
->SetRowAttr(attr
, row
);
9135 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9137 if ( CanHaveAttributes() )
9139 m_table
->SetColAttr(attr
, col
);
9148 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9150 if ( CanHaveAttributes() )
9152 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9153 attr
->SetBackgroundColour(colour
);
9158 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9160 if ( CanHaveAttributes() )
9162 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9163 attr
->SetTextColour(colour
);
9168 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9170 if ( CanHaveAttributes() )
9172 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9173 attr
->SetFont(font
);
9178 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9180 if ( CanHaveAttributes() )
9182 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9183 attr
->SetAlignment(horiz
, vert
);
9188 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9190 if ( CanHaveAttributes() )
9192 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9193 attr
->SetOverflow(allow
);
9198 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9200 if ( CanHaveAttributes() )
9202 int cell_rows
, cell_cols
;
9204 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9205 attr
->GetSize(&cell_rows
, &cell_cols
);
9206 attr
->SetSize(num_rows
, num_cols
);
9209 // Cannot set the size of a cell to 0 or negative values
9210 // While it is perfectly legal to do that, this function cannot
9211 // handle all the possibilies, do it by hand by getting the CellAttr.
9212 // You can only set the size of a cell to 1,1 or greater with this fn
9213 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9214 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9215 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9216 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9218 // if this was already a multicell then "turn off" the other cells first
9219 if ((cell_rows
> 1) || (cell_rows
> 1))
9222 for (j
=row
; j
<row
+cell_rows
; j
++)
9224 for (i
=col
; i
<col
+cell_cols
; i
++)
9226 if ((i
!= col
) || (j
!= row
))
9228 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9229 attr_stub
->SetSize( 1, 1 );
9230 attr_stub
->DecRef();
9236 // mark the cells that will be covered by this cell to
9237 // negative or zero values to point back at this cell
9238 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9241 for (j
=row
; j
<row
+num_rows
; j
++)
9243 for (i
=col
; i
<col
+num_cols
; i
++)
9245 if ((i
!= col
) || (j
!= row
))
9247 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9248 attr_stub
->SetSize( row
-j
, col
-i
);
9249 attr_stub
->DecRef();
9257 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9259 if ( CanHaveAttributes() )
9261 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9262 attr
->SetRenderer(renderer
);
9267 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9269 if ( CanHaveAttributes() )
9271 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9272 attr
->SetEditor(editor
);
9277 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9279 if ( CanHaveAttributes() )
9281 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9282 attr
->SetReadOnly(isReadOnly
);
9287 // ----------------------------------------------------------------------------
9288 // Data type registration
9289 // ----------------------------------------------------------------------------
9291 void wxGrid::RegisterDataType(const wxString
& typeName
,
9292 wxGridCellRenderer
* renderer
,
9293 wxGridCellEditor
* editor
)
9295 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9299 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9301 wxString typeName
= m_table
->GetTypeName(row
, col
);
9302 return GetDefaultEditorForType(typeName
);
9305 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9307 wxString typeName
= m_table
->GetTypeName(row
, col
);
9308 return GetDefaultRendererForType(typeName
);
9312 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9314 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9315 if ( index
== wxNOT_FOUND
)
9317 wxFAIL_MSG(wxT("Unknown data type name"));
9322 return m_typeRegistry
->GetEditor(index
);
9326 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9328 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9329 if ( index
== wxNOT_FOUND
)
9331 wxFAIL_MSG(wxT("Unknown data type name"));
9336 return m_typeRegistry
->GetRenderer(index
);
9340 // ----------------------------------------------------------------------------
9342 // ----------------------------------------------------------------------------
9344 void wxGrid::EnableDragRowSize( bool enable
)
9346 m_canDragRowSize
= enable
;
9350 void wxGrid::EnableDragColSize( bool enable
)
9352 m_canDragColSize
= enable
;
9355 void wxGrid::EnableDragGridSize( bool enable
)
9357 m_canDragGridSize
= enable
;
9361 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9363 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9365 if ( resizeExistingRows
)
9367 // since we are resizing all rows to the default row size,
9368 // we can simply clear the row heights and row bottoms
9369 // arrays (which also allows us to take advantage of
9370 // some speed optimisations)
9371 m_rowHeights
.Empty();
9372 m_rowBottoms
.Empty();
9373 if ( !GetBatchCount() )
9378 void wxGrid::SetRowSize( int row
, int height
)
9380 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9382 // See comment in SetColSize
9383 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9385 if ( m_rowHeights
.IsEmpty() )
9387 // need to really create the array
9391 int h
= wxMax( 0, height
);
9392 int diff
= h
- m_rowHeights
[row
];
9394 m_rowHeights
[row
] = h
;
9396 for ( i
= row
; i
< m_numRows
; i
++ )
9398 m_rowBottoms
[i
] += diff
;
9400 if ( !GetBatchCount() )
9404 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9406 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9408 if ( resizeExistingCols
)
9410 // since we are resizing all columns to the default column size,
9411 // we can simply clear the col widths and col rights
9412 // arrays (which also allows us to take advantage of
9413 // some speed optimisations)
9414 m_colWidths
.Empty();
9415 m_colRights
.Empty();
9416 if ( !GetBatchCount() )
9421 void wxGrid::SetColSize( int col
, int width
)
9423 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9425 // should we check that it's bigger than GetColMinimalWidth(col) here?
9427 // No, because it is reasonable to assume the library user know's
9428 // what he is doing. However whe should test against the weaker
9429 // constariant of minimalAcceptableWidth, as this breaks rendering
9431 // This test then fixes sf.net bug #645734
9433 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9435 if ( m_colWidths
.IsEmpty() )
9437 // need to really create the array
9441 // if < 0 calc new width from label
9445 wxArrayString lines
;
9446 wxClientDC
dc(m_colLabelWin
);
9447 dc
.SetFont(GetLabelFont());
9448 StringToLines(GetColLabelValue(col
), lines
);
9449 GetTextBoxSize(dc
, lines
, &w
, &h
);
9452 int w
= wxMax( 0, width
);
9453 int diff
= w
- m_colWidths
[col
];
9454 m_colWidths
[col
] = w
;
9457 for ( i
= col
; i
< m_numCols
; i
++ )
9459 m_colRights
[i
] += diff
;
9461 if ( !GetBatchCount() )
9466 void wxGrid::SetColMinimalWidth( int col
, int width
)
9468 if (width
> GetColMinimalAcceptableWidth()) {
9469 m_colMinWidths
[col
] = width
;
9473 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9475 if (width
> GetRowMinimalAcceptableHeight()) {
9476 m_rowMinHeights
[row
] = width
;
9480 int wxGrid::GetColMinimalWidth(int col
) const
9482 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9483 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9486 int wxGrid::GetRowMinimalHeight(int row
) const
9488 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9489 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9492 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9494 // We do allow a width of 0 since this gives us
9495 // an easy way to temporarily hidding columns.
9498 m_minAcceptableColWidth
= width
;
9501 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9503 // We do allow a height of 0 since this gives us
9504 // an easy way to temporarily hidding rows.
9507 m_minAcceptableRowHeight
= height
;
9510 int wxGrid::GetColMinimalAcceptableWidth() const
9512 return m_minAcceptableColWidth
;
9515 int wxGrid::GetRowMinimalAcceptableHeight() const
9517 return m_minAcceptableRowHeight
;
9520 // ----------------------------------------------------------------------------
9522 // ----------------------------------------------------------------------------
9524 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9526 wxClientDC
dc(m_gridWin
);
9528 // init both of them to avoid compiler warnings, even if weo nly need one
9536 wxCoord extent
, extentMax
= 0;
9537 int max
= column
? m_numRows
: m_numCols
;
9538 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9545 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9546 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9549 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9550 extent
= column
? size
.x
: size
.y
;
9551 if ( extent
> extentMax
)
9562 // now also compare with the column label extent
9564 dc
.SetFont( GetLabelFont() );
9568 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9569 if( GetColLabelTextOrientation() == wxVERTICAL
)
9573 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9575 extent
= column
? w
: h
;
9576 if ( extent
> extentMax
)
9583 // empty column - give default extent (notice that if extentMax is less
9584 // than default extent but != 0, it's ok)
9585 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9591 // leave some space around text
9602 SetColSize(col
, extentMax
);
9603 if ( !GetBatchCount() )
9606 m_gridWin
->GetClientSize( &cw
, &ch
);
9607 wxRect
rect ( CellToRect( 0, col
) );
9609 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9610 rect
.width
= cw
- rect
.x
;
9611 rect
.height
= m_colLabelHeight
;
9612 m_colLabelWin
->Refresh( TRUE
, &rect
);
9617 SetRowSize(row
, extentMax
);
9618 if ( !GetBatchCount() )
9621 m_gridWin
->GetClientSize( &cw
, &ch
);
9622 wxRect
rect ( CellToRect( row
, 0 ) );
9624 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9625 rect
.width
= m_rowLabelWidth
;
9626 rect
.height
= ch
- rect
.y
;
9627 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9633 SetColMinimalWidth(col
, extentMax
);
9635 SetRowMinimalHeight(row
, extentMax
);
9639 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9641 int width
= m_rowLabelWidth
;
9646 for ( int col
= 0; col
< m_numCols
; col
++ )
9650 AutoSizeColumn(col
, setAsMin
);
9653 width
+= GetColWidth(col
);
9662 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9664 int height
= m_colLabelHeight
;
9669 for ( int row
= 0; row
< m_numRows
; row
++ )
9673 AutoSizeRow(row
, setAsMin
);
9676 height
+= GetRowHeight(row
);
9685 void wxGrid::AutoSize()
9689 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9691 // round up the size to a multiple of scroll step - this ensures that we
9692 // won't get the scrollbars if we're sized exactly to this width
9693 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9695 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9696 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9698 // distribute the extra space between the columns/rows to avoid having
9699 // extra white space
9701 // Remove the extra m_extraWidth + 1 added above
9702 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9703 if ( diff
&& m_numCols
)
9705 // try to resize the columns uniformly
9706 wxCoord diffPerCol
= diff
/ m_numCols
;
9709 for ( int col
= 0; col
< m_numCols
; col
++ )
9711 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9715 // add remaining amount to the last columns
9716 diff
-= diffPerCol
* m_numCols
;
9719 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9721 SetColSize(col
, GetColWidth(col
) + 1);
9727 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9728 if ( diff
&& m_numRows
)
9730 // try to resize the columns uniformly
9731 wxCoord diffPerRow
= diff
/ m_numRows
;
9734 for ( int row
= 0; row
< m_numRows
; row
++ )
9736 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9740 // add remaining amount to the last rows
9741 diff
-= diffPerRow
* m_numRows
;
9744 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9746 SetRowSize(row
, GetRowHeight(row
) + 1);
9753 SetClientSize(sizeFit
);
9756 void wxGrid::AutoSizeRowLabelSize( int row
)
9758 wxArrayString lines
;
9761 // Hide the edit control, so it
9762 // won't interfer with drag-shrinking.
9763 if( IsCellEditControlShown() )
9765 HideCellEditControl();
9766 SaveEditControlValue();
9769 // autosize row height depending on label text
9770 StringToLines( GetRowLabelValue( row
), lines
);
9771 wxClientDC
dc( m_rowLabelWin
);
9772 GetTextBoxSize( dc
, lines
, &w
, &h
);
9773 if( h
< m_defaultRowHeight
)
9774 h
= m_defaultRowHeight
;
9779 void wxGrid::AutoSizeColLabelSize( int col
)
9781 wxArrayString lines
;
9784 // Hide the edit control, so it
9785 // won't interfer with drag-shrinking.
9786 if( IsCellEditControlShown() )
9788 HideCellEditControl();
9789 SaveEditControlValue();
9792 // autosize column width depending on label text
9793 StringToLines( GetColLabelValue( col
), lines
);
9794 wxClientDC
dc( m_colLabelWin
);
9795 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9796 GetTextBoxSize( dc
, lines
, &w
, &h
);
9798 GetTextBoxSize( dc
, lines
, &h
, &w
);
9799 if( w
< m_defaultColWidth
)
9800 w
= m_defaultColWidth
;
9805 wxSize
wxGrid::DoGetBestSize() const
9807 // don't set sizes, only calculate them
9808 wxGrid
*self
= (wxGrid
*)this; // const_cast
9811 width
= self
->SetOrCalcColumnSizes(TRUE
);
9812 height
= self
->SetOrCalcRowSizes(TRUE
);
9814 int maxwidth
, maxheight
;
9815 wxDisplaySize( & maxwidth
, & maxheight
);
9817 if ( width
> maxwidth
) width
= maxwidth
;
9818 if ( height
> maxheight
) height
= maxheight
;
9820 return wxSize( width
, height
);
9829 wxPen
& wxGrid::GetDividerPen() const
9834 // ----------------------------------------------------------------------------
9835 // cell value accessor functions
9836 // ----------------------------------------------------------------------------
9838 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9842 m_table
->SetValue( row
, col
, s
);
9843 if ( !GetBatchCount() )
9846 wxRect
rect( CellToRect( row
, col
) );
9848 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9849 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9850 m_gridWin
->Refresh( FALSE
, &rect
);
9853 if ( m_currentCellCoords
.GetRow() == row
&&
9854 m_currentCellCoords
.GetCol() == col
&&
9855 IsCellEditControlShown())
9856 // Note: If we are using IsCellEditControlEnabled,
9857 // this interacts badly with calling SetCellValue from
9858 // an EVT_GRID_CELL_CHANGE handler.
9860 HideCellEditControl();
9861 ShowCellEditControl(); // will reread data from table
9868 // ------ Block, row and col selection
9871 void wxGrid::SelectRow( int row
, bool addToSelected
)
9873 if ( IsSelection() && !addToSelected
)
9877 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9881 void wxGrid::SelectCol( int col
, bool addToSelected
)
9883 if ( IsSelection() && !addToSelected
)
9887 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9891 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9892 bool addToSelected
)
9894 if ( IsSelection() && !addToSelected
)
9898 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9899 FALSE
, addToSelected
);
9903 void wxGrid::SelectAll()
9905 if ( m_numRows
> 0 && m_numCols
> 0 )
9908 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9913 // ------ Cell, row and col deselection
9916 void wxGrid::DeselectRow( int row
)
9921 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9923 if ( m_selection
->IsInSelection(row
, 0 ) )
9924 m_selection
->ToggleCellSelection( row
, 0);
9928 int nCols
= GetNumberCols();
9929 for ( int i
= 0; i
< nCols
; i
++ )
9931 if ( m_selection
->IsInSelection(row
, i
) )
9932 m_selection
->ToggleCellSelection( row
, i
);
9937 void wxGrid::DeselectCol( int col
)
9942 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9944 if ( m_selection
->IsInSelection(0, col
) )
9945 m_selection
->ToggleCellSelection( 0, col
);
9949 int nRows
= GetNumberRows();
9950 for ( int i
= 0; i
< nRows
; i
++ )
9952 if ( m_selection
->IsInSelection(i
, col
) )
9953 m_selection
->ToggleCellSelection(i
, col
);
9958 void wxGrid::DeselectCell( int row
, int col
)
9960 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9961 m_selection
->ToggleCellSelection(row
, col
);
9964 bool wxGrid::IsSelection()
9966 return ( m_selection
&& (m_selection
->IsSelection() ||
9967 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9968 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9971 bool wxGrid::IsInSelection( int row
, int col
) const
9973 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9974 ( row
>= m_selectingTopLeft
.GetRow() &&
9975 col
>= m_selectingTopLeft
.GetCol() &&
9976 row
<= m_selectingBottomRight
.GetRow() &&
9977 col
<= m_selectingBottomRight
.GetCol() )) );
9980 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9982 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9983 return m_selection
->m_cellSelection
;
9985 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9987 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9988 return m_selection
->m_blockSelectionTopLeft
;
9990 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9992 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9993 return m_selection
->m_blockSelectionBottomRight
;
9995 wxArrayInt
wxGrid::GetSelectedRows() const
9997 if (!m_selection
) { wxArrayInt a
; return a
; }
9998 return m_selection
->m_rowSelection
;
10000 wxArrayInt
wxGrid::GetSelectedCols() const
10002 if (!m_selection
) { wxArrayInt a
; return a
; }
10003 return m_selection
->m_colSelection
;
10007 void wxGrid::ClearSelection()
10009 m_selectingTopLeft
= wxGridNoCellCoords
;
10010 m_selectingBottomRight
= wxGridNoCellCoords
;
10012 m_selection
->ClearSelection();
10016 // This function returns the rectangle that encloses the given block
10017 // in device coords clipped to the client size of the grid window.
10019 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10020 const wxGridCellCoords
&bottomRight
)
10022 wxRect
rect( wxGridNoCellRect
);
10025 cellRect
= CellToRect( topLeft
);
10026 if ( cellRect
!= wxGridNoCellRect
)
10032 rect
= wxRect( 0, 0, 0, 0 );
10035 cellRect
= CellToRect( bottomRight
);
10036 if ( cellRect
!= wxGridNoCellRect
)
10042 return wxGridNoCellRect
;
10046 int left
= rect
.GetLeft();
10047 int top
= rect
.GetTop();
10048 int right
= rect
.GetRight();
10049 int bottom
= rect
.GetBottom();
10051 int leftCol
= topLeft
.GetCol();
10052 int topRow
= topLeft
.GetRow();
10053 int rightCol
= bottomRight
.GetCol();
10054 int bottomRow
= bottomRight
.GetRow();
10072 topRow
= bottomRow
;
10077 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10079 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10081 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10083 cellRect
= CellToRect( j
, i
);
10085 if (cellRect
.x
< left
)
10087 if (cellRect
.y
< top
)
10089 if (cellRect
.x
+ cellRect
.width
> right
)
10090 right
= cellRect
.x
+ cellRect
.width
;
10091 if (cellRect
.y
+ cellRect
.height
> bottom
)
10092 bottom
= cellRect
.y
+ cellRect
.height
;
10094 else i
= rightCol
; // jump over inner cells.
10098 // convert to scrolled coords
10100 CalcScrolledPosition( left
, top
, &left
, &top
);
10101 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10104 m_gridWin
->GetClientSize( &cw
, &ch
);
10106 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10107 return wxRect( 0, 0, 0, 0);
10109 rect
.SetLeft( wxMax(0, left
) );
10110 rect
.SetTop( wxMax(0, top
) );
10111 rect
.SetRight( wxMin(cw
, right
) );
10112 rect
.SetBottom( wxMin(ch
, bottom
) );
10118 // ------ Grid event classes
10121 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10123 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10124 int row
, int col
, int x
, int y
, bool sel
,
10125 bool control
, bool shift
, bool alt
, bool meta
)
10126 : wxNotifyEvent( type
, id
)
10133 m_control
= control
;
10138 SetEventObject(obj
);
10142 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10144 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10145 int rowOrCol
, int x
, int y
,
10146 bool control
, bool shift
, bool alt
, bool meta
)
10147 : wxNotifyEvent( type
, id
)
10149 m_rowOrCol
= rowOrCol
;
10152 m_control
= control
;
10157 SetEventObject(obj
);
10161 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10163 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10164 const wxGridCellCoords
& topLeft
,
10165 const wxGridCellCoords
& bottomRight
,
10166 bool sel
, bool control
,
10167 bool shift
, bool alt
, bool meta
)
10168 : wxNotifyEvent( type
, id
)
10170 m_topLeft
= topLeft
;
10171 m_bottomRight
= bottomRight
;
10173 m_control
= control
;
10178 SetEventObject(obj
);
10182 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10184 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10185 wxObject
* obj
, int row
,
10186 int col
, wxControl
* ctrl
)
10187 : wxCommandEvent(type
, id
)
10189 SetEventObject(obj
);
10195 #endif // wxUSE_GRID