1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/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, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
80 wxGridCellCoords coords
;
84 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
85 // without rewriting the macros, which require a public copy constructor.
88 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
89 class WXDLLIMPEXP_ADV
);
91 #include "wx/arrimpl.cpp"
93 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
94 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
126 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
);
133 void OnPaint( wxPaintEvent
& event
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnMouseWheel( wxMouseEvent
& event
);
136 void OnKeyDown( wxKeyEvent
& event
);
137 void OnKeyUp( wxKeyEvent
& );
138 void OnChar( wxKeyEvent
& );
140 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
141 DECLARE_EVENT_TABLE()
142 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
146 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
149 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
150 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
151 const wxPoint
&pos
, const wxSize
&size
);
156 void OnPaint( wxPaintEvent
& event
);
157 void OnMouseEvent( wxMouseEvent
& event
);
158 void OnMouseWheel( wxMouseEvent
& event
);
159 void OnKeyDown( wxKeyEvent
& event
);
160 void OnKeyUp( wxKeyEvent
& );
161 void OnChar( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
165 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
169 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
172 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
173 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
174 const wxPoint
&pos
, const wxSize
&size
);
179 void OnMouseEvent( wxMouseEvent
& event
);
180 void OnMouseWheel( wxMouseEvent
& event
);
181 void OnKeyDown( wxKeyEvent
& event
);
182 void OnKeyUp( wxKeyEvent
& );
183 void OnChar( wxKeyEvent
& );
184 void OnPaint( wxPaintEvent
& event
);
186 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
187 DECLARE_EVENT_TABLE()
188 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
191 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
197 m_rowLabelWin
= NULL
;
198 m_colLabelWin
= NULL
;
201 wxGridWindow( wxGrid
*parent
,
202 wxGridRowLabelWindow
*rowLblWin
,
203 wxGridColLabelWindow
*colLblWin
,
204 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
205 virtual ~wxGridWindow() {}
207 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
209 wxGrid
* GetOwner() { return m_owner
; }
213 wxGridRowLabelWindow
*m_rowLabelWin
;
214 wxGridColLabelWindow
*m_colLabelWin
;
216 void OnPaint( wxPaintEvent
&event
);
217 void OnMouseWheel( wxMouseEvent
& event
);
218 void OnMouseEvent( wxMouseEvent
& event
);
219 void OnKeyDown( wxKeyEvent
& );
220 void OnKeyUp( wxKeyEvent
& );
221 void OnChar( wxKeyEvent
& );
222 void OnEraseBackground( wxEraseEvent
& );
223 void OnFocus( wxFocusEvent
& );
225 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
226 DECLARE_EVENT_TABLE()
227 DECLARE_NO_COPY_CLASS(wxGridWindow
)
231 class wxGridCellEditorEvtHandler
: public wxEvtHandler
234 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
241 void OnKillFocus(wxFocusEvent
& event
);
242 void OnKeyDown(wxKeyEvent
& event
);
243 void OnChar(wxKeyEvent
& event
);
245 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
249 wxGridCellEditor
*m_editor
;
251 // Work around the fact that a focus kill event can be sent to
252 // a combobox within a set focus event.
255 DECLARE_EVENT_TABLE()
256 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
257 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
261 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
263 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
264 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
265 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
266 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
270 // ----------------------------------------------------------------------------
271 // the internal data representation used by wxGridCellAttrProvider
272 // ----------------------------------------------------------------------------
274 // this class stores attributes set for cells
275 class WXDLLIMPEXP_ADV wxGridCellAttrData
278 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
279 wxGridCellAttr
*GetAttr(int row
, int col
) const;
280 void UpdateAttrRows( size_t pos
, int numRows
);
281 void UpdateAttrCols( size_t pos
, int numCols
);
284 // searches for the attr for given cell, returns wxNOT_FOUND if not found
285 int FindIndex(int row
, int col
) const;
287 wxGridCellWithAttrArray m_attrs
;
290 // this class stores attributes set for rows or columns
291 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
294 // empty ctor to suppress warnings
295 wxGridRowOrColAttrData() {}
296 ~wxGridRowOrColAttrData();
298 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
299 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
300 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
303 wxArrayInt m_rowsOrCols
;
304 wxArrayAttrs m_attrs
;
307 // NB: this is just a wrapper around 3 objects: one which stores cell
308 // attributes, and 2 others for row/col ones
309 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
312 wxGridCellAttrData m_cellAttrs
;
313 wxGridRowOrColAttrData m_rowAttrs
,
318 // ----------------------------------------------------------------------------
319 // data structures used for the data type registry
320 // ----------------------------------------------------------------------------
322 struct wxGridDataTypeInfo
324 wxGridDataTypeInfo(const wxString
& typeName
,
325 wxGridCellRenderer
* renderer
,
326 wxGridCellEditor
* editor
)
327 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
330 ~wxGridDataTypeInfo()
332 wxSafeDecRef(m_renderer
);
333 wxSafeDecRef(m_editor
);
337 wxGridCellRenderer
* m_renderer
;
338 wxGridCellEditor
* m_editor
;
340 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
344 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
345 class WXDLLIMPEXP_ADV
);
348 class WXDLLIMPEXP_ADV wxGridTypeRegistry
351 wxGridTypeRegistry() {}
352 ~wxGridTypeRegistry();
354 void RegisterDataType(const wxString
& typeName
,
355 wxGridCellRenderer
* renderer
,
356 wxGridCellEditor
* editor
);
358 // find one of already registered data types
359 int FindRegisteredDataType(const wxString
& typeName
);
361 // try to FindRegisteredDataType(), if this fails and typeName is one of
362 // standard typenames, register it and return its index
363 int FindDataType(const wxString
& typeName
);
365 // try to FindDataType(), if it fails see if it is not one of already
366 // registered data types with some params in which case clone the
367 // registered data type and set params for it
368 int FindOrCloneDataType(const wxString
& typeName
);
370 wxGridCellRenderer
* GetRenderer(int index
);
371 wxGridCellEditor
* GetEditor(int index
);
374 wxGridDataTypeInfoArray m_typeinfo
;
378 // ----------------------------------------------------------------------------
379 // conditional compilation
380 // ----------------------------------------------------------------------------
382 #ifndef WXGRID_DRAW_LINES
383 #define WXGRID_DRAW_LINES 1
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 //#define DEBUG_ATTR_CACHE
391 #ifdef DEBUG_ATTR_CACHE
392 static size_t gs_nAttrCacheHits
= 0;
393 static size_t gs_nAttrCacheMisses
= 0;
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
401 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
404 // TODO: this doesn't work at all, grid cells have different sizes and approx
405 // calculations don't work as because of the size mismatch scrollbars
406 // sometimes fail to be shown when they should be or vice versa
408 // The scroll bars may be a little flakey once in a while, but that is
409 // surely much less horrible than having scroll lines of only 1!!!
412 // Well, it's still seriously broken so it might be better but needs
415 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
416 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
418 // the size of hash tables used a bit everywhere (the max number of elements
419 // in these hash tables is the number of rows/columns)
420 static const int GRID_HASH_SIZE
= 100;
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 static inline int GetScrollX(int x
)
429 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
432 static inline int GetScrollY(int y
)
434 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
438 // ============================================================================
440 // ============================================================================
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 wxGridCellEditor::wxGridCellEditor()
452 wxGridCellEditor::~wxGridCellEditor()
457 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
458 wxWindowID
WXUNUSED(id
),
459 wxEvtHandler
* evtHandler
)
462 m_control
->PushEventHandler(evtHandler
);
465 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
466 wxGridCellAttr
*attr
)
468 // erase the background because we might not fill the cell
469 wxClientDC
dc(m_control
->GetParent());
470 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
472 gridWindow
->GetOwner()->PrepareDC(dc
);
474 dc
.SetPen(*wxTRANSPARENT_PEN
);
475 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
476 dc
.DrawRectangle(rectCell
);
478 // redraw the control we just painted over
479 m_control
->Refresh();
482 void wxGridCellEditor::Destroy()
486 m_control
->PopEventHandler( true /* delete it*/ );
488 m_control
->Destroy();
493 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
495 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
497 m_control
->Show(show
);
501 // set the colours/fonts if we have any
504 m_colFgOld
= m_control
->GetForegroundColour();
505 m_control
->SetForegroundColour(attr
->GetTextColour());
507 m_colBgOld
= m_control
->GetBackgroundColour();
508 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
510 // Workaround for GTK+1 font setting problem on some platforms
511 #if !defined(__WXGTK__) || defined(__WXGTK20__)
512 m_fontOld
= m_control
->GetFont();
513 m_control
->SetFont(attr
->GetFont());
516 // can't do anything more in the base class version, the other
517 // attributes may only be used by the derived classes
522 // restore the standard colours fonts
523 if ( m_colFgOld
.Ok() )
525 m_control
->SetForegroundColour(m_colFgOld
);
526 m_colFgOld
= wxNullColour
;
529 if ( m_colBgOld
.Ok() )
531 m_control
->SetBackgroundColour(m_colBgOld
);
532 m_colBgOld
= wxNullColour
;
535 // Workaround for GTK+1 font setting problem on some platforms
536 #if !defined(__WXGTK__) || defined(__WXGTK20__)
537 if ( m_fontOld
.Ok() )
539 m_control
->SetFont(m_fontOld
);
540 m_fontOld
= wxNullFont
;
546 void wxGridCellEditor::SetSize(const wxRect
& rect
)
548 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
550 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
553 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
558 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
560 bool ctrl
= event
.ControlDown();
561 bool alt
= event
.AltDown();
564 // On the Mac the Alt key is more like shift and is used for entry of
565 // valid characters, so check for Ctrl and Meta instead.
566 alt
= event
.MetaDown();
569 // Assume it's not a valid char if ctrl or alt is down, but if both are
570 // down then it may be because of an AltGr key combination, so let them
571 // through in that case.
572 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
579 // If it's a F-Key or other special key then it shouldn't start the
581 if (event
.GetKeyCode() >= WXK_START
)
585 // if the unicode key code is not really a unicode character (it may
586 // be a function key or etc., the platforms appear to always give us a
587 // small value in this case) then fallback to the ASCII key code but
588 // don't do anything for function keys or etc.
589 key
= event
.GetUnicodeKey();
592 key
= event
.GetKeyCode();
593 keyOk
= (key
<= 127);
596 key
= event
.GetKeyCode();
597 keyOk
= (key
<= 255);
603 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
608 void wxGridCellEditor::StartingClick()
614 // ----------------------------------------------------------------------------
615 // wxGridCellTextEditor
616 // ----------------------------------------------------------------------------
618 wxGridCellTextEditor::wxGridCellTextEditor()
623 void wxGridCellTextEditor::Create(wxWindow
* parent
,
625 wxEvtHandler
* evtHandler
)
627 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
628 wxDefaultPosition
, wxDefaultSize
629 #if defined(__WXMSW__)
638 // set max length allowed in the textctrl, if the parameter was set
641 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
644 wxGridCellEditor::Create(parent
, id
, evtHandler
);
647 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
648 wxGridCellAttr
* WXUNUSED(attr
))
650 // as we fill the entire client area,
651 // don't do anything here to minimize flicker
654 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
656 wxRect
rect(rectOrig
);
658 // Make the edit control large enough to allow for internal margins
660 // TODO: remove this if the text ctrl sizing is improved esp. for unix
662 #if defined(__WXGTK__)
670 #elif defined(__WXMSW__)
684 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
685 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
687 #if defined(__WXMOTIF__)
692 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
693 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
694 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
695 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
698 wxGridCellEditor::SetSize(rect
);
701 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
703 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
705 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
707 DoBeginEdit(m_startValue
);
710 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
712 Text()->SetValue(startValue
);
713 Text()->SetInsertionPointEnd();
714 Text()->SetSelection(-1, -1);
718 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
720 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
722 bool changed
= false;
723 wxString value
= Text()->GetValue();
724 if (value
!= m_startValue
)
728 grid
->GetTable()->SetValue(row
, col
, value
);
730 m_startValue
= wxEmptyString
;
732 // No point in setting the text of the hidden control
733 //Text()->SetValue(m_startValue);
738 void wxGridCellTextEditor::Reset()
740 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
742 DoReset(m_startValue
);
745 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
747 Text()->SetValue(startValue
);
748 Text()->SetInsertionPointEnd();
751 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
753 return wxGridCellEditor::IsAcceptedKey(event
);
756 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
758 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
759 // longer an appropriate way to get the character into the text control.
760 // Do it ourselves instead. We know that if we get this far that we have
761 // a valid character, so not a whole lot of testing needs to be done.
763 wxTextCtrl
* tc
= Text();
768 ch
= event
.GetUnicodeKey();
770 ch
= (wxChar
)event
.GetKeyCode();
772 ch
= (wxChar
)event
.GetKeyCode();
778 // delete the character at the cursor
779 pos
= tc
->GetInsertionPoint();
780 if (pos
< tc
->GetLastPosition())
781 tc
->Remove(pos
, pos
+ 1);
785 // delete the character before the cursor
786 pos
= tc
->GetInsertionPoint();
788 tc
->Remove(pos
- 1, pos
);
797 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
798 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
800 #if defined(__WXMOTIF__) || defined(__WXGTK__)
801 // wxMotif needs a little extra help...
802 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
803 wxString
s( Text()->GetValue() );
804 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
806 Text()->SetInsertionPoint( pos
);
808 // the other ports can handle a Return key press
814 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
824 if ( params
.ToLong(&tmp
) )
826 m_maxChars
= (size_t)tmp
;
830 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
835 // return the value in the text control
836 wxString
wxGridCellTextEditor::GetValue() const
838 return Text()->GetValue();
841 // ----------------------------------------------------------------------------
842 // wxGridCellNumberEditor
843 // ----------------------------------------------------------------------------
845 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
851 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
853 wxEvtHandler
* evtHandler
)
858 // create a spin ctrl
859 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
860 wxDefaultPosition
, wxDefaultSize
,
864 wxGridCellEditor::Create(parent
, id
, evtHandler
);
869 // just a text control
870 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
873 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
878 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
880 // first get the value
881 wxGridTableBase
*table
= grid
->GetTable();
882 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
884 m_valueOld
= table
->GetValueAsLong(row
, col
);
889 wxString sValue
= table
->GetValue(row
, col
);
890 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
892 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
900 Spin()->SetValue((int)m_valueOld
);
906 DoBeginEdit(GetString());
910 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
920 value
= Spin()->GetValue();
921 changed
= value
!= m_valueOld
;
923 text
= wxString::Format(wxT("%ld"), value
);
928 text
= Text()->GetValue();
929 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
934 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
935 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
937 grid
->GetTable()->SetValue(row
, col
, text
);
943 void wxGridCellNumberEditor::Reset()
948 Spin()->SetValue((int)m_valueOld
);
953 DoReset(GetString());
957 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
959 if ( wxGridCellEditor::IsAcceptedKey(event
) )
961 int keycode
= event
.GetKeyCode();
962 if ( (keycode
< 128) &&
963 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
972 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
974 int keycode
= event
.GetKeyCode();
977 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
979 wxGridCellTextEditor::StartingKey(event
);
988 if ( wxIsdigit(keycode
) )
990 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
991 spin
->SetValue(keycode
- '0');
992 spin
->SetSelection(1,1);
1001 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1012 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1016 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1020 // skip the error message below
1025 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1029 // return the value in the spin control if it is there (the text control otherwise)
1030 wxString
wxGridCellNumberEditor::GetValue() const
1037 long value
= Spin()->GetValue();
1038 s
.Printf(wxT("%ld"), value
);
1043 s
= Text()->GetValue();
1049 // ----------------------------------------------------------------------------
1050 // wxGridCellFloatEditor
1051 // ----------------------------------------------------------------------------
1053 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1056 m_precision
= precision
;
1059 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1061 wxEvtHandler
* evtHandler
)
1063 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1065 #if wxUSE_VALIDATORS
1066 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1070 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1072 // first get the value
1073 wxGridTableBase
*table
= grid
->GetTable();
1074 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1076 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1081 wxString sValue
= table
->GetValue(row
, col
);
1082 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1084 wxFAIL_MSG( _T("this cell doesn't have float value") );
1089 DoBeginEdit(GetString());
1092 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1096 wxString
text(Text()->GetValue());
1098 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1099 !wxIsSameDouble(value
, m_valueOld
) )
1101 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1102 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1104 grid
->GetTable()->SetValue(row
, col
, text
);
1112 void wxGridCellFloatEditor::Reset()
1114 DoReset(GetString());
1117 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1119 int keycode
= event
.GetKeyCode();
1121 tmpbuf
[0] = (char) keycode
;
1123 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1126 bool is_decimal_point
= ( strbuf
==
1127 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1129 bool is_decimal_point
= ( strbuf
== _T(".") );
1132 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1133 || is_decimal_point
)
1135 wxGridCellTextEditor::StartingKey(event
);
1137 // skip Skip() below
1144 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1155 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1159 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1161 m_precision
= (int)tmp
;
1163 // skip the error message below
1168 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1172 wxString
wxGridCellFloatEditor::GetString() const
1175 if ( m_precision
== -1 && m_width
!= -1)
1177 // default precision
1178 fmt
.Printf(_T("%%%d.f"), m_width
);
1180 else if ( m_precision
!= -1 && m_width
== -1)
1183 fmt
.Printf(_T("%%.%df"), m_precision
);
1185 else if ( m_precision
!= -1 && m_width
!= -1 )
1187 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1191 // default width/precision
1195 return wxString::Format(fmt
, m_valueOld
);
1198 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1200 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1202 const int keycode
= event
.GetKeyCode();
1203 if ( isascii(keycode
) )
1206 tmpbuf
[0] = (char) keycode
;
1208 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1211 const wxString decimalPoint
=
1212 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1214 const wxString
decimalPoint(_T('.'));
1217 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1218 if ( wxIsdigit(keycode
) ||
1219 tolower(keycode
) == 'e' ||
1220 keycode
== decimalPoint
||
1232 #endif // wxUSE_TEXTCTRL
1236 // ----------------------------------------------------------------------------
1237 // wxGridCellBoolEditor
1238 // ----------------------------------------------------------------------------
1240 // the default values for GetValue()
1241 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1243 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1245 wxEvtHandler
* evtHandler
)
1247 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1248 wxDefaultPosition
, wxDefaultSize
,
1251 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1254 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1256 bool resize
= false;
1257 wxSize size
= m_control
->GetSize();
1258 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1260 // check if the checkbox is not too big/small for this cell
1261 wxSize sizeBest
= m_control
->GetBestSize();
1262 if ( !(size
== sizeBest
) )
1264 // reset to default size if it had been made smaller
1270 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1272 // leave 1 pixel margin
1273 size
.x
= size
.y
= minSize
- 2;
1280 m_control
->SetSize(size
);
1283 // position it in the centre of the rectangle (TODO: support alignment?)
1285 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1286 // the checkbox without label still has some space to the right in wxGTK,
1287 // so shift it to the right
1289 #elif defined(__WXMSW__)
1290 // here too, but in other way
1295 int hAlign
= wxALIGN_CENTRE
;
1296 int vAlign
= wxALIGN_CENTRE
;
1298 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1301 if (hAlign
== wxALIGN_LEFT
)
1309 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1311 else if (hAlign
== wxALIGN_RIGHT
)
1313 x
= r
.x
+ r
.width
- size
.x
- 2;
1314 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1316 else if (hAlign
== wxALIGN_CENTRE
)
1318 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1319 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1322 m_control
->Move(x
, y
);
1325 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1327 m_control
->Show(show
);
1331 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1332 CBox()->SetBackgroundColour(colBg
);
1336 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1338 wxASSERT_MSG(m_control
,
1339 wxT("The wxGridCellEditor must be created first!"));
1341 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1343 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1347 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1349 if ( cellval
== ms_stringValues
[false] )
1350 m_startValue
= false;
1351 else if ( cellval
== ms_stringValues
[true] )
1352 m_startValue
= true;
1355 // do not try to be smart here and convert it to true or false
1356 // because we'll still overwrite it with something different and
1357 // this risks to be very surprising for the user code, let them
1359 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1363 CBox()->SetValue(m_startValue
);
1367 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1370 wxASSERT_MSG(m_control
,
1371 wxT("The wxGridCellEditor must be created first!"));
1373 bool changed
= false;
1374 bool value
= CBox()->GetValue();
1375 if ( value
!= m_startValue
)
1380 wxGridTableBase
* const table
= grid
->GetTable();
1381 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1382 table
->SetValueAsBool(row
, col
, value
);
1384 table
->SetValue(row
, col
, GetValue());
1390 void wxGridCellBoolEditor::Reset()
1392 wxASSERT_MSG(m_control
,
1393 wxT("The wxGridCellEditor must be created first!"));
1395 CBox()->SetValue(m_startValue
);
1398 void wxGridCellBoolEditor::StartingClick()
1400 CBox()->SetValue(!CBox()->GetValue());
1403 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1405 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1407 int keycode
= event
.GetKeyCode();
1420 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1422 int keycode
= event
.GetKeyCode();
1426 CBox()->SetValue(!CBox()->GetValue());
1430 CBox()->SetValue(true);
1434 CBox()->SetValue(false);
1439 wxString
wxGridCellBoolEditor::GetValue() const
1441 return ms_stringValues
[CBox()->GetValue()];
1445 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1446 const wxString
& valueFalse
)
1448 ms_stringValues
[false] = valueFalse
;
1449 ms_stringValues
[true] = valueTrue
;
1453 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1455 return value
== ms_stringValues
[true];
1458 #endif // wxUSE_CHECKBOX
1462 // ----------------------------------------------------------------------------
1463 // wxGridCellChoiceEditor
1464 // ----------------------------------------------------------------------------
1466 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1468 : m_choices(choices
),
1469 m_allowOthers(allowOthers
) { }
1471 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1472 const wxString choices
[],
1474 : m_allowOthers(allowOthers
)
1478 m_choices
.Alloc(count
);
1479 for ( size_t n
= 0; n
< count
; n
++ )
1481 m_choices
.Add(choices
[n
]);
1486 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1488 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1489 editor
->m_allowOthers
= m_allowOthers
;
1490 editor
->m_choices
= m_choices
;
1495 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1497 wxEvtHandler
* evtHandler
)
1499 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1500 wxDefaultPosition
, wxDefaultSize
,
1502 m_allowOthers
? 0 : wxCB_READONLY
);
1504 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1507 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1508 wxGridCellAttr
* attr
)
1510 // as we fill the entire client area, don't do anything here to minimize
1513 // TODO: It doesn't actually fill the client area since the height of a
1514 // combo always defaults to the standard. Until someone has time to
1515 // figure out the right rectangle to paint, just do it the normal way.
1516 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1519 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1521 wxASSERT_MSG(m_control
,
1522 wxT("The wxGridCellEditor must be created first!"));
1524 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1526 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1528 // Don't immediately end if we get a kill focus event within BeginEdit
1530 evtHandler
->SetInSetFocus(true);
1532 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1536 Combo()->SetValue(m_startValue
);
1540 // find the right position, or default to the first if not found
1541 int pos
= Combo()->FindString(m_startValue
);
1542 if (pos
== wxNOT_FOUND
)
1544 Combo()->SetSelection(pos
);
1547 Combo()->SetInsertionPointEnd();
1548 Combo()->SetFocus();
1552 // When dropping down the menu, a kill focus event
1553 // happens after this point, so we can't reset the flag yet.
1554 #if !defined(__WXGTK20__)
1555 evtHandler
->SetInSetFocus(false);
1560 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1563 wxString value
= Combo()->GetValue();
1564 if ( value
== m_startValue
)
1567 grid
->GetTable()->SetValue(row
, col
, value
);
1572 void wxGridCellChoiceEditor::Reset()
1574 Combo()->SetValue(m_startValue
);
1575 Combo()->SetInsertionPointEnd();
1578 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1588 wxStringTokenizer
tk(params
, _T(','));
1589 while ( tk
.HasMoreTokens() )
1591 m_choices
.Add(tk
.GetNextToken());
1595 // return the value in the text control
1596 wxString
wxGridCellChoiceEditor::GetValue() const
1598 return Combo()->GetValue();
1601 #endif // wxUSE_COMBOBOX
1603 // ----------------------------------------------------------------------------
1604 // wxGridCellEditorEvtHandler
1605 // ----------------------------------------------------------------------------
1607 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1609 // Don't disable the cell if we're just starting to edit it
1614 m_grid
->DisableCellEditControl();
1619 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1621 switch ( event
.GetKeyCode() )
1625 m_grid
->DisableCellEditControl();
1629 m_grid
->GetEventHandler()->ProcessEvent( event
);
1633 case WXK_NUMPAD_ENTER
:
1634 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1635 m_editor
->HandleReturn(event
);
1644 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1646 int row
= m_grid
->GetGridCursorRow();
1647 int col
= m_grid
->GetGridCursorCol();
1648 wxRect rect
= m_grid
->CellToRect( row
, col
);
1650 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1652 // if cell width is smaller than grid client area, cell is wholly visible
1653 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1655 switch ( event
.GetKeyCode() )
1660 case WXK_NUMPAD_ENTER
:
1665 if ( wholeCellVisible
)
1667 // no special processing needed...
1672 // do special processing for partly visible cell...
1674 // get the widths of all cells previous to this one
1676 for ( int i
= 0; i
< col
; i
++ )
1678 colXPos
+= m_grid
->GetColSize(i
);
1681 int xUnit
= 1, yUnit
= 1;
1682 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1685 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1689 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1697 if ( wholeCellVisible
)
1699 // no special processing needed...
1704 // do special processing for partly visible cell...
1707 wxString value
= m_grid
->GetCellValue(row
, col
);
1708 if ( wxEmptyString
!= value
)
1710 // get width of cell CONTENTS (text)
1712 wxFont font
= m_grid
->GetCellFont(row
, col
);
1713 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1715 // try to RIGHT align the text by scrolling
1716 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1718 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1719 // otherwise the last part of the cell content might be hidden below the scroll bar
1720 // FIXME: maybe there is a more suitable correction?
1721 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1722 if ( textWidth
< 0 )
1728 // get the widths of all cells previous to this one
1730 for ( int i
= 0; i
< col
; i
++ )
1732 colXPos
+= m_grid
->GetColSize(i
);
1735 // and add the (modified) text width of the cell contents
1736 // as we'd like to see the last part of the cell contents
1737 colXPos
+= textWidth
;
1739 int xUnit
= 1, yUnit
= 1;
1740 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1741 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1752 // ----------------------------------------------------------------------------
1753 // wxGridCellWorker is an (almost) empty common base class for
1754 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1755 // ----------------------------------------------------------------------------
1757 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1762 wxGridCellWorker::~wxGridCellWorker()
1766 // ============================================================================
1768 // ============================================================================
1770 // ----------------------------------------------------------------------------
1771 // wxGridCellRenderer
1772 // ----------------------------------------------------------------------------
1774 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1775 wxGridCellAttr
& attr
,
1778 int WXUNUSED(row
), int WXUNUSED(col
),
1781 dc
.SetBackgroundMode( wxSOLID
);
1783 // grey out fields if the grid is disabled
1784 if ( grid
.IsEnabled() )
1788 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1792 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1797 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1800 dc
.SetPen( *wxTRANSPARENT_PEN
);
1801 dc
.DrawRectangle(rect
);
1804 // ----------------------------------------------------------------------------
1805 // wxGridCellStringRenderer
1806 // ----------------------------------------------------------------------------
1808 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1809 const wxGridCellAttr
& attr
,
1813 dc
.SetBackgroundMode( wxTRANSPARENT
);
1815 // TODO some special colours for attr.IsReadOnly() case?
1817 // different coloured text when the grid is disabled
1818 if ( grid
.IsEnabled() )
1822 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1823 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1827 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1828 dc
.SetTextForeground( attr
.GetTextColour() );
1833 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1834 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1837 dc
.SetFont( attr
.GetFont() );
1840 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1842 const wxString
& text
)
1844 wxCoord x
= 0, y
= 0, max_x
= 0;
1845 dc
.SetFont(attr
.GetFont());
1846 wxStringTokenizer
tk(text
, _T('\n'));
1847 while ( tk
.HasMoreTokens() )
1849 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1850 max_x
= wxMax(max_x
, x
);
1853 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1855 return wxSize(max_x
, y
);
1858 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1859 wxGridCellAttr
& attr
,
1863 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1866 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1867 wxGridCellAttr
& attr
,
1869 const wxRect
& rectCell
,
1873 wxRect rect
= rectCell
;
1876 // erase only this cells background, overflow cells should have been erased
1877 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1880 attr
.GetAlignment(&hAlign
, &vAlign
);
1882 int overflowCols
= 0;
1884 if (attr
.GetOverflow())
1886 int cols
= grid
.GetNumberCols();
1887 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1888 int cell_rows
, cell_cols
;
1889 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1890 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1892 int i
, c_cols
, c_rows
;
1893 for (i
= col
+cell_cols
; i
< cols
; i
++)
1895 bool is_empty
= true;
1896 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1898 // check w/ anchor cell for multicell block
1899 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1902 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1911 rect
.width
+= grid
.GetColSize(i
);
1919 if (rect
.width
>= best_width
)
1923 overflowCols
= i
- col
- cell_cols
+ 1;
1924 if (overflowCols
>= cols
)
1925 overflowCols
= cols
- 1;
1928 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1930 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1932 clip
.x
+= rectCell
.width
;
1933 // draw each overflow cell individually
1934 int col_end
= col
+ cell_cols
+ overflowCols
;
1935 if (col_end
>= grid
.GetNumberCols())
1936 col_end
= grid
.GetNumberCols() - 1;
1937 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1939 clip
.width
= grid
.GetColSize(i
) - 1;
1940 dc
.DestroyClippingRegion();
1941 dc
.SetClippingRegion(clip
);
1943 SetTextColoursAndFont(grid
, attr
, dc
,
1944 grid
.IsInSelection(row
,i
));
1946 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1947 rect
, hAlign
, vAlign
);
1948 clip
.x
+= grid
.GetColSize(i
) - 1;
1954 dc
.DestroyClippingRegion();
1958 // now we only have to draw the text
1959 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1961 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1962 rect
, hAlign
, vAlign
);
1965 // ----------------------------------------------------------------------------
1966 // wxGridCellNumberRenderer
1967 // ----------------------------------------------------------------------------
1969 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1971 wxGridTableBase
*table
= grid
.GetTable();
1973 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1975 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1979 text
= table
->GetValue(row
, col
);
1985 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1986 wxGridCellAttr
& attr
,
1988 const wxRect
& rectCell
,
1992 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1994 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1996 // draw the text right aligned by default
1998 attr
.GetAlignment(&hAlign
, &vAlign
);
1999 hAlign
= wxALIGN_RIGHT
;
2001 wxRect rect
= rectCell
;
2004 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2007 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2008 wxGridCellAttr
& attr
,
2012 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2015 // ----------------------------------------------------------------------------
2016 // wxGridCellFloatRenderer
2017 // ----------------------------------------------------------------------------
2019 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2022 SetPrecision(precision
);
2025 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2027 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2028 renderer
->m_width
= m_width
;
2029 renderer
->m_precision
= m_precision
;
2030 renderer
->m_format
= m_format
;
2035 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2037 wxGridTableBase
*table
= grid
.GetTable();
2042 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2044 val
= table
->GetValueAsDouble(row
, col
);
2049 text
= table
->GetValue(row
, col
);
2050 hasDouble
= text
.ToDouble(&val
);
2057 if ( m_width
== -1 )
2059 if ( m_precision
== -1 )
2061 // default width/precision
2062 m_format
= _T("%f");
2066 m_format
.Printf(_T("%%.%df"), m_precision
);
2069 else if ( m_precision
== -1 )
2071 // default precision
2072 m_format
.Printf(_T("%%%d.f"), m_width
);
2076 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2080 text
.Printf(m_format
, val
);
2083 //else: text already contains the string
2088 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2089 wxGridCellAttr
& attr
,
2091 const wxRect
& rectCell
,
2095 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2097 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2099 // draw the text right aligned by default
2101 attr
.GetAlignment(&hAlign
, &vAlign
);
2102 hAlign
= wxALIGN_RIGHT
;
2104 wxRect rect
= rectCell
;
2107 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2110 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2111 wxGridCellAttr
& attr
,
2115 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2118 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2122 // reset to defaults
2128 wxString tmp
= params
.BeforeFirst(_T(','));
2132 if ( tmp
.ToLong(&width
) )
2134 SetWidth((int)width
);
2138 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2142 tmp
= params
.AfterFirst(_T(','));
2146 if ( tmp
.ToLong(&precision
) )
2148 SetPrecision((int)precision
);
2152 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2158 // ----------------------------------------------------------------------------
2159 // wxGridCellBoolRenderer
2160 // ----------------------------------------------------------------------------
2162 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2164 // FIXME these checkbox size calculations are really ugly...
2166 // between checkmark and box
2167 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2169 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2170 wxGridCellAttr
& WXUNUSED(attr
),
2175 // compute it only once (no locks for MT safeness in GUI thread...)
2176 if ( !ms_sizeCheckMark
.x
)
2178 // get checkbox size
2179 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2180 wxSize size
= checkbox
->GetBestSize();
2181 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2183 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2184 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2185 checkSize
-= size
.y
/ 2;
2190 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2193 return ms_sizeCheckMark
;
2196 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2197 wxGridCellAttr
& attr
,
2203 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2205 // draw a check mark in the centre (ignoring alignment - TODO)
2206 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2208 // don't draw outside the cell
2209 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2210 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2212 // and even leave (at least) 1 pixel margin
2213 size
.x
= size
.y
= minSize
- 2;
2216 // draw a border around checkmark
2218 attr
.GetAlignment(&hAlign
, &vAlign
);
2221 if (hAlign
== wxALIGN_CENTRE
)
2223 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2224 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2225 rectBorder
.width
= size
.x
;
2226 rectBorder
.height
= size
.y
;
2228 else if (hAlign
== wxALIGN_LEFT
)
2230 rectBorder
.x
= rect
.x
+ 2;
2231 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2232 rectBorder
.width
= size
.x
;
2233 rectBorder
.height
= size
.y
;
2235 else if (hAlign
== wxALIGN_RIGHT
)
2237 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2238 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2239 rectBorder
.width
= size
.x
;
2240 rectBorder
.height
= size
.y
;
2244 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2246 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2250 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2251 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2256 wxRect rectMark
= rectBorder
;
2259 // MSW DrawCheckMark() is weird (and should probably be changed...)
2260 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2264 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2267 dc
.SetTextForeground(attr
.GetTextColour());
2268 dc
.DrawCheckMark(rectMark
);
2271 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2272 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2273 dc
.DrawRectangle(rectBorder
);
2276 // ----------------------------------------------------------------------------
2278 // ----------------------------------------------------------------------------
2280 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2284 m_isReadOnly
= Unset
;
2289 m_attrkind
= wxGridCellAttr::Cell
;
2291 m_sizeRows
= m_sizeCols
= 1;
2292 m_overflow
= UnsetOverflow
;
2294 SetDefAttr(attrDefault
);
2297 wxGridCellAttr
*wxGridCellAttr::Clone() const
2299 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2301 if ( HasTextColour() )
2302 attr
->SetTextColour(GetTextColour());
2303 if ( HasBackgroundColour() )
2304 attr
->SetBackgroundColour(GetBackgroundColour());
2306 attr
->SetFont(GetFont());
2307 if ( HasAlignment() )
2308 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2310 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2314 attr
->SetRenderer(m_renderer
);
2315 m_renderer
->IncRef();
2319 attr
->SetEditor(m_editor
);
2324 attr
->SetReadOnly();
2326 attr
->SetOverflow( m_overflow
== Overflow
);
2327 attr
->SetKind( m_attrkind
);
2332 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2334 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2335 SetTextColour(mergefrom
->GetTextColour());
2336 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2337 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2338 if ( !HasFont() && mergefrom
->HasFont() )
2339 SetFont(mergefrom
->GetFont());
2340 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2343 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2344 SetAlignment(hAlign
, vAlign
);
2346 if ( !HasSize() && mergefrom
->HasSize() )
2347 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2349 // Directly access member functions as GetRender/Editor don't just return
2350 // m_renderer/m_editor
2352 // Maybe add support for merge of Render and Editor?
2353 if (!HasRenderer() && mergefrom
->HasRenderer() )
2355 m_renderer
= mergefrom
->m_renderer
;
2356 m_renderer
->IncRef();
2358 if ( !HasEditor() && mergefrom
->HasEditor() )
2360 m_editor
= mergefrom
->m_editor
;
2363 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2364 SetReadOnly(mergefrom
->IsReadOnly());
2366 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2367 SetOverflow(mergefrom
->GetOverflow());
2369 SetDefAttr(mergefrom
->m_defGridAttr
);
2372 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2374 // The size of a cell is normally 1,1
2376 // If this cell is larger (2,2) then this is the top left cell
2377 // the other cells that will be covered (lower right cells) must be
2378 // set to negative or zero values such that
2379 // row + num_rows of the covered cell points to the larger cell (this cell)
2380 // same goes for the col + num_cols.
2382 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2384 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2385 !((num_rows
<= 0) && (num_cols
> 0)) ||
2386 !((num_rows
== 0) && (num_cols
== 0))),
2387 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2389 m_sizeRows
= num_rows
;
2390 m_sizeCols
= num_cols
;
2393 const wxColour
& wxGridCellAttr::GetTextColour() const
2395 if (HasTextColour())
2399 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2401 return m_defGridAttr
->GetTextColour();
2405 wxFAIL_MSG(wxT("Missing default cell attribute"));
2406 return wxNullColour
;
2410 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2412 if (HasBackgroundColour())
2416 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2418 return m_defGridAttr
->GetBackgroundColour();
2422 wxFAIL_MSG(wxT("Missing default cell attribute"));
2423 return wxNullColour
;
2427 const wxFont
& wxGridCellAttr::GetFont() const
2433 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2435 return m_defGridAttr
->GetFont();
2439 wxFAIL_MSG(wxT("Missing default cell attribute"));
2444 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2453 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2455 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2459 wxFAIL_MSG(wxT("Missing default cell attribute"));
2463 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2466 *num_rows
= m_sizeRows
;
2468 *num_cols
= m_sizeCols
;
2471 // GetRenderer and GetEditor use a slightly different decision path about
2472 // which attribute to use. If a non-default attr object has one then it is
2473 // used, otherwise the default editor or renderer is fetched from the grid and
2474 // used. It should be the default for the data type of the cell. If it is
2475 // NULL (because the table has a type that the grid does not have in its
2476 // registry), then the grid's default editor or renderer is used.
2478 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2480 wxGridCellRenderer
*renderer
= NULL
;
2482 if ( m_renderer
&& this != m_defGridAttr
)
2484 // use the cells renderer if it has one
2485 renderer
= m_renderer
;
2488 else // no non-default cell renderer
2490 // get default renderer for the data type
2493 // GetDefaultRendererForCell() will do IncRef() for us
2494 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2497 if ( renderer
== NULL
)
2499 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2501 // if we still don't have one then use the grid default
2502 // (no need for IncRef() here neither)
2503 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2505 else // default grid attr
2507 // use m_renderer which we had decided not to use initially
2508 renderer
= m_renderer
;
2515 // we're supposed to always find something
2516 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2521 // same as above, except for s/renderer/editor/g
2522 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2524 wxGridCellEditor
*editor
= NULL
;
2526 if ( m_editor
&& this != m_defGridAttr
)
2528 // use the cells editor if it has one
2532 else // no non default cell editor
2534 // get default editor for the data type
2537 // GetDefaultEditorForCell() will do IncRef() for us
2538 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2541 if ( editor
== NULL
)
2543 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2545 // if we still don't have one then use the grid default
2546 // (no need for IncRef() here neither)
2547 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2549 else // default grid attr
2551 // use m_editor which we had decided not to use initially
2559 // we're supposed to always find something
2560 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2565 // ----------------------------------------------------------------------------
2566 // wxGridCellAttrData
2567 // ----------------------------------------------------------------------------
2569 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2571 int n
= FindIndex(row
, col
);
2572 if ( n
== wxNOT_FOUND
)
2574 // add the attribute
2575 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2579 // free the old attribute
2580 m_attrs
[(size_t)n
].attr
->DecRef();
2584 // change the attribute
2585 m_attrs
[(size_t)n
].attr
= attr
;
2589 // remove this attribute
2590 m_attrs
.RemoveAt((size_t)n
);
2595 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2597 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2599 int n
= FindIndex(row
, col
);
2600 if ( n
!= wxNOT_FOUND
)
2602 attr
= m_attrs
[(size_t)n
].attr
;
2609 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2611 size_t count
= m_attrs
.GetCount();
2612 for ( size_t n
= 0; n
< count
; n
++ )
2614 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2615 wxCoord row
= coords
.GetRow();
2616 if ((size_t)row
>= pos
)
2620 // If rows inserted, include row counter where necessary
2621 coords
.SetRow(row
+ numRows
);
2623 else if (numRows
< 0)
2625 // If rows deleted ...
2626 if ((size_t)row
>= pos
- numRows
)
2628 // ...either decrement row counter (if row still exists)...
2629 coords
.SetRow(row
+ numRows
);
2633 // ...or remove the attribute
2634 // No need to DecRef the attribute itself since this is
2635 // done be wxGridCellWithAttr's destructor!
2636 m_attrs
.RemoveAt(n
);
2645 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2647 size_t count
= m_attrs
.GetCount();
2648 for ( size_t n
= 0; n
< count
; n
++ )
2650 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2651 wxCoord col
= coords
.GetCol();
2652 if ( (size_t)col
>= pos
)
2656 // If rows inserted, include row counter where necessary
2657 coords
.SetCol(col
+ numCols
);
2659 else if (numCols
< 0)
2661 // If rows deleted ...
2662 if ((size_t)col
>= pos
- numCols
)
2664 // ...either decrement row counter (if row still exists)...
2665 coords
.SetCol(col
+ numCols
);
2669 // ...or remove the attribute
2670 // No need to DecRef the attribute itself since this is
2671 // done be wxGridCellWithAttr's destructor!
2672 m_attrs
.RemoveAt(n
);
2681 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2683 size_t count
= m_attrs
.GetCount();
2684 for ( size_t n
= 0; n
< count
; n
++ )
2686 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2687 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2696 // ----------------------------------------------------------------------------
2697 // wxGridRowOrColAttrData
2698 // ----------------------------------------------------------------------------
2700 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2702 size_t count
= m_attrs
.Count();
2703 for ( size_t n
= 0; n
< count
; n
++ )
2705 m_attrs
[n
]->DecRef();
2709 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2711 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2713 int n
= m_rowsOrCols
.Index(rowOrCol
);
2714 if ( n
!= wxNOT_FOUND
)
2716 attr
= m_attrs
[(size_t)n
];
2723 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2725 int i
= m_rowsOrCols
.Index(rowOrCol
);
2726 if ( i
== wxNOT_FOUND
)
2728 // add the attribute
2729 m_rowsOrCols
.Add(rowOrCol
);
2734 size_t n
= (size_t)i
;
2737 // change the attribute
2738 m_attrs
[n
]->DecRef();
2743 // remove this attribute
2744 m_attrs
[n
]->DecRef();
2745 m_rowsOrCols
.RemoveAt(n
);
2746 m_attrs
.RemoveAt(n
);
2751 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2753 size_t count
= m_attrs
.GetCount();
2754 for ( size_t n
= 0; n
< count
; n
++ )
2756 int & rowOrCol
= m_rowsOrCols
[n
];
2757 if ( (size_t)rowOrCol
>= pos
)
2759 if ( numRowsOrCols
> 0 )
2761 // If rows inserted, include row counter where necessary
2762 rowOrCol
+= numRowsOrCols
;
2764 else if ( numRowsOrCols
< 0)
2766 // If rows deleted, either decrement row counter (if row still exists)
2767 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2768 rowOrCol
+= numRowsOrCols
;
2771 m_rowsOrCols
.RemoveAt(n
);
2772 m_attrs
[n
]->DecRef();
2773 m_attrs
.RemoveAt(n
);
2782 // ----------------------------------------------------------------------------
2783 // wxGridCellAttrProvider
2784 // ----------------------------------------------------------------------------
2786 wxGridCellAttrProvider::wxGridCellAttrProvider()
2788 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2791 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2796 void wxGridCellAttrProvider::InitData()
2798 m_data
= new wxGridCellAttrProviderData
;
2801 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2802 wxGridCellAttr::wxAttrKind kind
) const
2804 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2809 case (wxGridCellAttr::Any
):
2810 // Get cached merge attributes.
2811 // Currently not used as no cache implemented as not mutable
2812 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2815 // Basically implement old version.
2816 // Also check merge cache, so we don't have to re-merge every time..
2817 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2818 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2819 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2821 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2823 // Two or more are non NULL
2824 attr
= new wxGridCellAttr
;
2825 attr
->SetKind(wxGridCellAttr::Merged
);
2827 // Order is important..
2830 attr
->MergeWith(attrcell
);
2835 attr
->MergeWith(attrcol
);
2840 attr
->MergeWith(attrrow
);
2844 // store merge attr if cache implemented
2846 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2850 // one or none is non null return it or null.
2869 case (wxGridCellAttr::Cell
):
2870 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2873 case (wxGridCellAttr::Col
):
2874 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2877 case (wxGridCellAttr::Row
):
2878 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2883 // (wxGridCellAttr::Default):
2884 // (wxGridCellAttr::Merged):
2892 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2898 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2901 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2906 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2909 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2914 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2917 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2921 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2923 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2927 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2931 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2933 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2937 // ----------------------------------------------------------------------------
2938 // wxGridTypeRegistry
2939 // ----------------------------------------------------------------------------
2941 wxGridTypeRegistry::~wxGridTypeRegistry()
2943 size_t count
= m_typeinfo
.Count();
2944 for ( size_t i
= 0; i
< count
; i
++ )
2945 delete m_typeinfo
[i
];
2948 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2949 wxGridCellRenderer
* renderer
,
2950 wxGridCellEditor
* editor
)
2952 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2954 // is it already registered?
2955 int loc
= FindRegisteredDataType(typeName
);
2956 if ( loc
!= wxNOT_FOUND
)
2958 delete m_typeinfo
[loc
];
2959 m_typeinfo
[loc
] = info
;
2963 m_typeinfo
.Add(info
);
2967 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2969 size_t count
= m_typeinfo
.GetCount();
2970 for ( size_t i
= 0; i
< count
; i
++ )
2972 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2981 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2983 int index
= FindRegisteredDataType(typeName
);
2984 if ( index
== wxNOT_FOUND
)
2986 // check whether this is one of the standard ones, in which case
2987 // register it "on the fly"
2989 if ( typeName
== wxGRID_VALUE_STRING
)
2991 RegisterDataType(wxGRID_VALUE_STRING
,
2992 new wxGridCellStringRenderer
,
2993 new wxGridCellTextEditor
);
2996 #endif // wxUSE_TEXTCTRL
2998 if ( typeName
== wxGRID_VALUE_BOOL
)
3000 RegisterDataType(wxGRID_VALUE_BOOL
,
3001 new wxGridCellBoolRenderer
,
3002 new wxGridCellBoolEditor
);
3005 #endif // wxUSE_CHECKBOX
3007 if ( typeName
== wxGRID_VALUE_NUMBER
)
3009 RegisterDataType(wxGRID_VALUE_NUMBER
,
3010 new wxGridCellNumberRenderer
,
3011 new wxGridCellNumberEditor
);
3013 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3015 RegisterDataType(wxGRID_VALUE_FLOAT
,
3016 new wxGridCellFloatRenderer
,
3017 new wxGridCellFloatEditor
);
3020 #endif // wxUSE_TEXTCTRL
3022 if ( typeName
== wxGRID_VALUE_CHOICE
)
3024 RegisterDataType(wxGRID_VALUE_CHOICE
,
3025 new wxGridCellStringRenderer
,
3026 new wxGridCellChoiceEditor
);
3029 #endif // wxUSE_COMBOBOX
3034 // we get here only if just added the entry for this type, so return
3036 index
= m_typeinfo
.GetCount() - 1;
3042 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3044 int index
= FindDataType(typeName
);
3045 if ( index
== wxNOT_FOUND
)
3047 // the first part of the typename is the "real" type, anything after ':'
3048 // are the parameters for the renderer
3049 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3050 if ( index
== wxNOT_FOUND
)
3055 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3056 wxGridCellRenderer
*rendererOld
= renderer
;
3057 renderer
= renderer
->Clone();
3058 rendererOld
->DecRef();
3060 wxGridCellEditor
*editor
= GetEditor(index
);
3061 wxGridCellEditor
*editorOld
= editor
;
3062 editor
= editor
->Clone();
3063 editorOld
->DecRef();
3065 // do it even if there are no parameters to reset them to defaults
3066 wxString params
= typeName
.AfterFirst(_T(':'));
3067 renderer
->SetParameters(params
);
3068 editor
->SetParameters(params
);
3070 // register the new typename
3071 RegisterDataType(typeName
, renderer
, editor
);
3073 // we just registered it, it's the last one
3074 index
= m_typeinfo
.GetCount() - 1;
3080 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3082 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3089 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3091 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3098 // ----------------------------------------------------------------------------
3100 // ----------------------------------------------------------------------------
3102 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3104 wxGridTableBase::wxGridTableBase()
3106 m_view
= (wxGrid
*) NULL
;
3107 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3110 wxGridTableBase::~wxGridTableBase()
3112 delete m_attrProvider
;
3115 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3117 delete m_attrProvider
;
3118 m_attrProvider
= attrProvider
;
3121 bool wxGridTableBase::CanHaveAttributes()
3123 if ( ! GetAttrProvider() )
3125 // use the default attr provider by default
3126 SetAttrProvider(new wxGridCellAttrProvider
);
3132 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3134 if ( m_attrProvider
)
3135 return m_attrProvider
->GetAttr(row
, col
, kind
);
3137 return (wxGridCellAttr
*)NULL
;
3140 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3142 if ( m_attrProvider
)
3144 attr
->SetKind(wxGridCellAttr::Cell
);
3145 m_attrProvider
->SetAttr(attr
, row
, col
);
3149 // as we take ownership of the pointer and don't store it, we must
3155 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3157 if ( m_attrProvider
)
3159 attr
->SetKind(wxGridCellAttr::Row
);
3160 m_attrProvider
->SetRowAttr(attr
, row
);
3164 // as we take ownership of the pointer and don't store it, we must
3170 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3172 if ( m_attrProvider
)
3174 attr
->SetKind(wxGridCellAttr::Col
);
3175 m_attrProvider
->SetColAttr(attr
, col
);
3179 // as we take ownership of the pointer and don't store it, we must
3185 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3186 size_t WXUNUSED(numRows
) )
3188 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3193 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3195 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3200 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3201 size_t WXUNUSED(numRows
) )
3203 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3208 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3209 size_t WXUNUSED(numCols
) )
3211 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3216 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3218 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3223 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3224 size_t WXUNUSED(numCols
) )
3226 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3231 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3235 // RD: Starting the rows at zero confuses users,
3236 // no matter how much it makes sense to us geeks.
3242 wxString
wxGridTableBase::GetColLabelValue( int col
)
3244 // default col labels are:
3245 // cols 0 to 25 : A-Z
3246 // cols 26 to 675 : AA-ZZ
3251 for ( n
= 1; ; n
++ )
3253 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3259 // reverse the string...
3261 for ( i
= 0; i
< n
; i
++ )
3269 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3271 return wxGRID_VALUE_STRING
;
3274 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3275 const wxString
& typeName
)
3277 return typeName
== wxGRID_VALUE_STRING
;
3280 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3282 return CanGetValueAs(row
, col
, typeName
);
3285 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3290 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3295 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3300 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3301 long WXUNUSED(value
) )
3305 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3306 double WXUNUSED(value
) )
3310 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3311 bool WXUNUSED(value
) )
3315 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3316 const wxString
& WXUNUSED(typeName
) )
3321 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3322 const wxString
& WXUNUSED(typeName
),
3323 void* WXUNUSED(value
) )
3327 //////////////////////////////////////////////////////////////////////
3329 // Message class for the grid table to send requests and notifications
3333 wxGridTableMessage::wxGridTableMessage()
3335 m_table
= (wxGridTableBase
*) NULL
;
3341 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3342 int commandInt1
, int commandInt2
)
3346 m_comInt1
= commandInt1
;
3347 m_comInt2
= commandInt2
;
3350 //////////////////////////////////////////////////////////////////////
3352 // A basic grid table for string data. An object of this class will
3353 // created by wxGrid if you don't specify an alternative table class.
3356 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3358 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3360 wxGridStringTable::wxGridStringTable()
3365 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3368 m_data
.Alloc( numRows
);
3371 sa
.Alloc( numCols
);
3372 sa
.Add( wxEmptyString
, numCols
);
3374 m_data
.Add( sa
, numRows
);
3377 wxGridStringTable::~wxGridStringTable()
3381 int wxGridStringTable::GetNumberRows()
3383 return m_data
.GetCount();
3386 int wxGridStringTable::GetNumberCols()
3388 if ( m_data
.GetCount() > 0 )
3389 return m_data
[0].GetCount();
3394 wxString
wxGridStringTable::GetValue( int row
, int col
)
3396 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3398 _T("invalid row or column index in wxGridStringTable") );
3400 return m_data
[row
][col
];
3403 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3405 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3406 _T("invalid row or column index in wxGridStringTable") );
3408 m_data
[row
][col
] = value
;
3411 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3413 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3415 _T("invalid row or column index in wxGridStringTable") );
3417 return (m_data
[row
][col
] == wxEmptyString
);
3420 void wxGridStringTable::Clear()
3423 int numRows
, numCols
;
3425 numRows
= m_data
.GetCount();
3428 numCols
= m_data
[0].GetCount();
3430 for ( row
= 0; row
< numRows
; row
++ )
3432 for ( col
= 0; col
< numCols
; col
++ )
3434 m_data
[row
][col
] = wxEmptyString
;
3440 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3442 size_t curNumRows
= m_data
.GetCount();
3443 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3444 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3446 if ( pos
>= curNumRows
)
3448 return AppendRows( numRows
);
3452 sa
.Alloc( curNumCols
);
3453 sa
.Add( wxEmptyString
, curNumCols
);
3454 m_data
.Insert( sa
, pos
, numRows
);
3458 wxGridTableMessage
msg( this,
3459 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3463 GetView()->ProcessTableMessage( msg
);
3469 bool wxGridStringTable::AppendRows( size_t numRows
)
3471 size_t curNumRows
= m_data
.GetCount();
3472 size_t curNumCols
= ( curNumRows
> 0
3473 ? m_data
[0].GetCount()
3474 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3477 if ( curNumCols
> 0 )
3479 sa
.Alloc( curNumCols
);
3480 sa
.Add( wxEmptyString
, curNumCols
);
3483 m_data
.Add( sa
, numRows
);
3487 wxGridTableMessage
msg( this,
3488 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3491 GetView()->ProcessTableMessage( msg
);
3497 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3499 size_t curNumRows
= m_data
.GetCount();
3501 if ( pos
>= curNumRows
)
3503 wxFAIL_MSG( wxString::Format
3505 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3507 (unsigned long)numRows
,
3508 (unsigned long)curNumRows
3514 if ( numRows
> curNumRows
- pos
)
3516 numRows
= curNumRows
- pos
;
3519 if ( numRows
>= curNumRows
)
3525 m_data
.RemoveAt( pos
, numRows
);
3530 wxGridTableMessage
msg( this,
3531 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3535 GetView()->ProcessTableMessage( msg
);
3541 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3545 size_t curNumRows
= m_data
.GetCount();
3546 size_t curNumCols
= ( curNumRows
> 0
3547 ? m_data
[0].GetCount()
3548 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3550 if ( pos
>= curNumCols
)
3552 return AppendCols( numCols
);
3555 if ( !m_colLabels
.IsEmpty() )
3557 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3560 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3561 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3564 for ( row
= 0; row
< curNumRows
; row
++ )
3566 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3568 m_data
[row
].Insert( wxEmptyString
, col
);
3574 wxGridTableMessage
msg( this,
3575 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3579 GetView()->ProcessTableMessage( msg
);
3585 bool wxGridStringTable::AppendCols( size_t numCols
)
3589 size_t curNumRows
= m_data
.GetCount();
3594 // TODO: something better than this ?
3596 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3601 for ( row
= 0; row
< curNumRows
; row
++ )
3603 m_data
[row
].Add( wxEmptyString
, numCols
);
3608 wxGridTableMessage
msg( this,
3609 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3612 GetView()->ProcessTableMessage( msg
);
3618 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3622 size_t curNumRows
= m_data
.GetCount();
3623 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3624 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3626 if ( pos
>= curNumCols
)
3628 wxFAIL_MSG( wxString::Format
3630 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3632 (unsigned long)numCols
,
3633 (unsigned long)curNumCols
3640 colID
= GetView()->GetColAt( pos
);
3644 if ( numCols
> curNumCols
- colID
)
3646 numCols
= curNumCols
- colID
;
3649 if ( !m_colLabels
.IsEmpty() )
3651 // m_colLabels stores just as many elements as it needs, e.g. if only
3652 // the label of the first column had been set it would have only one
3653 // element and not numCols, so account for it
3654 int nToRm
= m_colLabels
.size() - colID
;
3656 m_colLabels
.RemoveAt( colID
, nToRm
);
3659 for ( row
= 0; row
< curNumRows
; row
++ )
3661 if ( numCols
>= curNumCols
)
3663 m_data
[row
].Clear();
3667 m_data
[row
].RemoveAt( colID
, numCols
);
3673 wxGridTableMessage
msg( this,
3674 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3678 GetView()->ProcessTableMessage( msg
);
3684 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3686 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3688 // using default label
3690 return wxGridTableBase::GetRowLabelValue( row
);
3694 return m_rowLabels
[row
];
3698 wxString
wxGridStringTable::GetColLabelValue( int col
)
3700 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3702 // using default label
3704 return wxGridTableBase::GetColLabelValue( col
);
3708 return m_colLabels
[col
];
3712 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3714 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3716 int n
= m_rowLabels
.GetCount();
3719 for ( i
= n
; i
<= row
; i
++ )
3721 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3725 m_rowLabels
[row
] = value
;
3728 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3730 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3732 int n
= m_colLabels
.GetCount();
3735 for ( i
= n
; i
<= col
; i
++ )
3737 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3741 m_colLabels
[col
] = value
;
3745 //////////////////////////////////////////////////////////////////////
3746 //////////////////////////////////////////////////////////////////////
3748 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3750 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3751 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3752 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3753 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3754 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3755 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3756 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3759 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3761 const wxPoint
&pos
, const wxSize
&size
)
3762 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3767 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3771 // NO - don't do this because it will set both the x and y origin
3772 // coords to match the parent scrolled window and we just want to
3773 // set the y coord - MB
3775 // m_owner->PrepareDC( dc );
3778 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3779 wxPoint pt
= dc
.GetDeviceOrigin();
3780 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3782 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3783 m_owner
->DrawRowLabels( dc
, rows
);
3786 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3788 m_owner
->ProcessRowLabelMouseEvent( event
);
3791 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3793 m_owner
->GetEventHandler()->ProcessEvent( event
);
3796 // This seems to be required for wxMotif otherwise the mouse
3797 // cursor must be in the cell edit control to get key events
3799 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3801 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3805 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3807 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3811 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3813 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3817 //////////////////////////////////////////////////////////////////////
3819 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3821 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3822 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3823 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3824 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3825 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3826 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3827 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3830 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3832 const wxPoint
&pos
, const wxSize
&size
)
3833 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3838 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3842 // NO - don't do this because it will set both the x and y origin
3843 // coords to match the parent scrolled window and we just want to
3844 // set the x coord - MB
3846 // m_owner->PrepareDC( dc );
3849 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3850 wxPoint pt
= dc
.GetDeviceOrigin();
3851 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3852 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3854 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3856 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3857 m_owner
->DrawColLabels( dc
, cols
);
3860 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3862 m_owner
->ProcessColLabelMouseEvent( event
);
3865 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3867 m_owner
->GetEventHandler()->ProcessEvent( event
);
3870 // This seems to be required for wxMotif otherwise the mouse
3871 // cursor must be in the cell edit control to get key events
3873 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3875 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3879 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3881 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3885 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3887 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3891 //////////////////////////////////////////////////////////////////////
3893 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3895 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3896 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3897 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3898 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3899 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3900 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3901 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3904 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3906 const wxPoint
&pos
, const wxSize
&size
)
3907 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3912 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3916 int client_height
= 0;
3917 int client_width
= 0;
3918 GetClientSize( &client_width
, &client_height
);
3920 // VZ: any reason for this ifdef? (FIXME)
3926 rect
.SetWidth( client_width
- 2 );
3927 rect
.SetHeight( client_height
- 2 );
3929 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3931 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3932 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3933 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3934 dc
.DrawLine( 0, 0, client_width
, 0 );
3935 dc
.DrawLine( 0, 0, 0, client_height
);
3937 dc
.SetPen( *wxWHITE_PEN
);
3938 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3939 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3943 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3945 m_owner
->ProcessCornerLabelMouseEvent( event
);
3948 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3950 m_owner
->GetEventHandler()->ProcessEvent(event
);
3953 // This seems to be required for wxMotif otherwise the mouse
3954 // cursor must be in the cell edit control to get key events
3956 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3958 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3962 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3964 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3968 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3970 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3974 //////////////////////////////////////////////////////////////////////
3976 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3978 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3979 EVT_PAINT( wxGridWindow::OnPaint
)
3980 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3981 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3982 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3983 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3984 EVT_CHAR( wxGridWindow::OnChar
)
3985 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3986 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3987 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3990 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3991 wxGridRowLabelWindow
*rowLblWin
,
3992 wxGridColLabelWindow
*colLblWin
,
3995 const wxSize
&size
)
3997 parent
, id
, pos
, size
,
3998 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3999 wxT("grid window") )
4002 m_rowLabelWin
= rowLblWin
;
4003 m_colLabelWin
= colLblWin
;
4006 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4008 wxPaintDC
dc( this );
4009 m_owner
->PrepareDC( dc
);
4010 wxRegion reg
= GetUpdateRegion();
4011 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4012 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4014 #if WXGRID_DRAW_LINES
4015 m_owner
->DrawAllGridLines( dc
, reg
);
4018 m_owner
->DrawGridSpace( dc
);
4019 m_owner
->DrawHighlight( dc
, dirtyCells
);
4022 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4024 wxWindow::ScrollWindow( dx
, dy
, rect
);
4025 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4026 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4029 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4031 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4034 m_owner
->ProcessGridCellMouseEvent( event
);
4037 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4039 m_owner
->GetEventHandler()->ProcessEvent( event
);
4042 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4043 // cursor must be in the cell edit control to get key events
4045 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4047 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4051 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4053 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4057 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4059 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4063 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4067 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4069 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4073 //////////////////////////////////////////////////////////////////////
4075 // Internal Helper function for computing row or column from some
4076 // (unscrolled) coordinate value, using either
4077 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4078 // of m_rowBottoms/m_ColRights to speed up the search!
4080 // Internal helper macros for simpler use of that function
4082 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4083 const wxArrayInt
& BorderArray
, int nMax
,
4086 #define internalXToCol(x) XToCol(x, true)
4087 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4088 m_minAcceptableRowHeight, \
4089 m_rowBottoms, m_numRows, true)
4091 /////////////////////////////////////////////////////////////////////
4093 #if wxUSE_EXTENDED_RTTI
4094 WX_DEFINE_FLAGS( wxGridStyle
)
4096 wxBEGIN_FLAGS( wxGridStyle
)
4097 // new style border flags, we put them first to
4098 // use them for streaming out
4099 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4100 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4101 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4102 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4103 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4104 wxFLAGS_MEMBER(wxBORDER_NONE
)
4106 // old style border flags
4107 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4108 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4109 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4110 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4111 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4112 wxFLAGS_MEMBER(wxBORDER
)
4114 // standard window styles
4115 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4116 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4117 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4118 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4119 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4120 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4121 wxFLAGS_MEMBER(wxVSCROLL
)
4122 wxFLAGS_MEMBER(wxHSCROLL
)
4124 wxEND_FLAGS( wxGridStyle
)
4126 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4128 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4129 wxHIDE_PROPERTY( Children
)
4130 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4131 wxEND_PROPERTIES_TABLE()
4133 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4134 wxEND_HANDLERS_TABLE()
4136 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4139 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4142 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4145 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4146 EVT_PAINT( wxGrid::OnPaint
)
4147 EVT_SIZE( wxGrid::OnSize
)
4148 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4149 EVT_KEY_UP( wxGrid::OnKeyUp
)
4150 EVT_CHAR ( wxGrid::OnChar
)
4151 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4156 // in order to make sure that a size event is not
4157 // trigerred in a unfinished state
4158 m_cornerLabelWin
= NULL
;
4159 m_rowLabelWin
= NULL
;
4160 m_colLabelWin
= NULL
;
4164 wxGrid::wxGrid( wxWindow
*parent
,
4169 const wxString
& name
)
4170 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4171 m_colMinWidths(GRID_HASH_SIZE
),
4172 m_rowMinHeights(GRID_HASH_SIZE
)
4175 SetInitialSize(size
);
4178 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4179 const wxPoint
& pos
, const wxSize
& size
,
4180 long style
, const wxString
& name
)
4182 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4183 style
| wxWANTS_CHARS
, name
))
4186 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4187 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4190 SetInitialSize(size
);
4197 // Must do this or ~wxScrollHelper will pop the wrong event handler
4198 SetTargetWindow(this);
4200 wxSafeDecRef(m_defaultCellAttr
);
4202 #ifdef DEBUG_ATTR_CACHE
4203 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4204 wxPrintf(_T("wxGrid attribute cache statistics: "
4205 "total: %u, hits: %u (%u%%)\n"),
4206 total
, gs_nAttrCacheHits
,
4207 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4210 // if we own the table, just delete it, otherwise at least don't leave it
4211 // with dangling view pointer
4214 else if ( m_table
&& m_table
->GetView() == this )
4215 m_table
->SetView(NULL
);
4217 delete m_typeRegistry
;
4222 // ----- internal init and update functions
4225 // NOTE: If using the default visual attributes works everywhere then this can
4226 // be removed as well as the #else cases below.
4227 #define _USE_VISATTR 0
4229 void wxGrid::Create()
4231 // set to true by CreateGrid
4234 // create the type registry
4235 m_typeRegistry
= new wxGridTypeRegistry
;
4238 m_table
= (wxGridTableBase
*) NULL
;
4241 m_cellEditCtrlEnabled
= false;
4243 m_defaultCellAttr
= new wxGridCellAttr();
4245 // Set default cell attributes
4246 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4247 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4248 m_defaultCellAttr
->SetFont(GetFont());
4249 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4250 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4251 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4254 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4255 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4257 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4258 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4261 m_defaultCellAttr
->SetTextColour(
4262 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4263 m_defaultCellAttr
->SetBackgroundColour(
4264 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4269 m_currentCellCoords
= wxGridNoCellCoords
;
4271 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4272 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4274 // subwindow components that make up the wxGrid
4275 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4280 m_colLabelWin
= new wxGridColLabelWindow( this,
4285 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4290 m_gridWin
= new wxGridWindow( this,
4297 SetTargetWindow( m_gridWin
);
4300 wxColour gfg
= gva
.colFg
;
4301 wxColour gbg
= gva
.colBg
;
4302 wxColour lfg
= lva
.colFg
;
4303 wxColour lbg
= lva
.colBg
;
4305 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4306 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4307 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4308 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4311 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4312 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4313 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4314 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4315 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4316 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4318 m_gridWin
->SetOwnForegroundColour(gfg
);
4319 m_gridWin
->SetOwnBackgroundColour(gbg
);
4324 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4325 wxGrid::wxGridSelectionModes selmode
)
4327 wxCHECK_MSG( !m_created
,
4329 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4331 m_numRows
= numRows
;
4332 m_numCols
= numCols
;
4334 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4335 m_table
->SetView( this );
4337 m_selection
= new wxGridSelection( this, selmode
);
4346 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4348 wxCHECK_RET( m_created
,
4349 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4351 m_selection
->SetSelectionMode( selmode
);
4354 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4356 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4357 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4359 return m_selection
->GetSelectionMode();
4362 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4363 wxGrid::wxGridSelectionModes selmode
)
4365 bool checkSelection
= false;
4368 // stop all processing
4373 m_table
->SetView(0);
4385 checkSelection
= true;
4387 // kill row and column size arrays
4388 m_colWidths
.Empty();
4389 m_colRights
.Empty();
4390 m_rowHeights
.Empty();
4391 m_rowBottoms
.Empty();
4396 m_numRows
= table
->GetNumberRows();
4397 m_numCols
= table
->GetNumberCols();
4400 m_table
->SetView( this );
4401 m_ownTable
= takeOwnership
;
4402 m_selection
= new wxGridSelection( this, selmode
);
4405 // If the newly set table is smaller than the
4406 // original one current cell and selection regions
4407 // might be invalid,
4408 m_selectingKeyboard
= wxGridNoCellCoords
;
4409 m_currentCellCoords
=
4410 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4411 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4412 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4413 m_selectingTopLeft
.GetCol() >= m_numCols
)
4415 m_selectingTopLeft
= wxGridNoCellCoords
;
4416 m_selectingBottomRight
= wxGridNoCellCoords
;
4419 m_selectingBottomRight
=
4420 wxGridCellCoords(wxMin(m_numRows
,
4421 m_selectingBottomRight
.GetRow()),
4423 m_selectingBottomRight
.GetCol()));
4435 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4436 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4438 if ( m_rowLabelWin
)
4440 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4444 m_labelBackgroundColour
= *wxWHITE
;
4447 m_labelTextColour
= *wxBLACK
;
4450 m_attrCache
.row
= -1;
4451 m_attrCache
.col
= -1;
4452 m_attrCache
.attr
= NULL
;
4454 // TODO: something better than this ?
4456 m_labelFont
= this->GetFont();
4457 m_labelFont
.SetWeight( wxBOLD
);
4459 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4460 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4462 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4463 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4464 m_colLabelTextOrientation
= wxHORIZONTAL
;
4466 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4467 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4469 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4470 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4472 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4473 m_defaultRowHeight
+= 8;
4475 m_defaultRowHeight
+= 4;
4478 m_gridLineColour
= wxColour( 192,192,192 );
4479 m_gridLinesEnabled
= true;
4480 m_cellHighlightColour
= *wxBLACK
;
4481 m_cellHighlightPenWidth
= 2;
4482 m_cellHighlightROPenWidth
= 1;
4484 m_canDragColMove
= false;
4486 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4487 m_winCapture
= (wxWindow
*)NULL
;
4488 m_canDragRowSize
= true;
4489 m_canDragColSize
= true;
4490 m_canDragGridSize
= true;
4491 m_canDragCell
= false;
4493 m_dragRowOrCol
= -1;
4494 m_isDragging
= false;
4495 m_startDragPos
= wxDefaultPosition
;
4497 m_waitForSlowClick
= false;
4499 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4500 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4502 m_currentCellCoords
= wxGridNoCellCoords
;
4506 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4507 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4509 m_editable
= true; // default for whole grid
4511 m_inOnKeyDown
= false;
4517 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4518 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4521 // ----------------------------------------------------------------------------
4522 // the idea is to call these functions only when necessary because they create
4523 // quite big arrays which eat memory mostly unnecessary - in particular, if
4524 // default widths/heights are used for all rows/columns, we may not use these
4527 // with some extra code, it should be possible to only store the widths/heights
4528 // different from default ones (resulting in space savings for huge grids) but
4529 // this is not done currently
4530 // ----------------------------------------------------------------------------
4532 void wxGrid::InitRowHeights()
4534 m_rowHeights
.Empty();
4535 m_rowBottoms
.Empty();
4537 m_rowHeights
.Alloc( m_numRows
);
4538 m_rowBottoms
.Alloc( m_numRows
);
4540 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4543 for ( int i
= 0; i
< m_numRows
; i
++ )
4545 rowBottom
+= m_defaultRowHeight
;
4546 m_rowBottoms
.Add( rowBottom
);
4550 void wxGrid::InitColWidths()
4552 m_colWidths
.Empty();
4553 m_colRights
.Empty();
4555 m_colWidths
.Alloc( m_numCols
);
4556 m_colRights
.Alloc( m_numCols
);
4558 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4561 for ( int i
= 0; i
< m_numCols
; i
++ )
4563 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4564 m_colRights
.Add( colRight
);
4568 int wxGrid::GetColWidth(int col
) const
4570 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4573 int wxGrid::GetColLeft(int col
) const
4575 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4576 : m_colRights
[col
] - m_colWidths
[col
];
4579 int wxGrid::GetColRight(int col
) const
4581 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4585 int wxGrid::GetRowHeight(int row
) const
4587 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4590 int wxGrid::GetRowTop(int row
) const
4592 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4593 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4596 int wxGrid::GetRowBottom(int row
) const
4598 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4599 : m_rowBottoms
[row
];
4602 void wxGrid::CalcDimensions()
4604 // compute the size of the scrollable area
4605 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4606 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4611 // take into account editor if shown
4612 if ( IsCellEditControlShown() )
4615 int r
= m_currentCellCoords
.GetRow();
4616 int c
= m_currentCellCoords
.GetCol();
4617 int x
= GetColLeft(c
);
4618 int y
= GetRowTop(r
);
4620 // how big is the editor
4621 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4622 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4623 editor
->GetControl()->GetSize(&w2
, &h2
);
4634 // preserve (more or less) the previous position
4636 GetViewStart( &x
, &y
);
4638 // ensure the position is valid for the new scroll ranges
4640 x
= wxMax( w
- 1, 0 );
4642 y
= wxMax( h
- 1, 0 );
4644 // do set scrollbar parameters
4645 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4646 GetScrollX(w
), GetScrollY(h
),
4648 GetBatchCount() != 0);
4650 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4651 // still must reposition the children
4655 void wxGrid::CalcWindowSizes()
4657 // escape if the window is has not been fully created yet
4659 if ( m_cornerLabelWin
== NULL
)
4663 GetClientSize( &cw
, &ch
);
4665 // this block of code tries to work around the following problem: the grid
4666 // could have been just resized to have enough space to show the full grid
4667 // window contents without the scrollbars, but its client size could be
4668 // not big enough because the grid has the scrollbars right now and so the
4669 // scrollbars would remain even though we don't need them any more
4671 // to prevent this from happening, check if we have enough space for
4672 // everything without the scrollbars and explicitly disable them then
4673 wxSize size
= GetSize() - GetWindowBorderSize();
4674 if ( size
!= wxSize(cw
, ch
) )
4676 // check if we have enough space for grid window after accounting for
4677 // the fixed size elements
4678 size
.x
-= m_rowLabelWidth
;
4679 size
.y
-= m_colLabelHeight
;
4681 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4683 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4685 // yes, we do, so remove the scrollbars and use the new client size
4686 // (which should be the same as full window size - borders now)
4687 SetScrollbars(0, 0, 0, 0);
4688 GetClientSize(&cw
, &ch
);
4692 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4693 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4695 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4696 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4698 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4699 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4701 if ( m_gridWin
&& m_gridWin
->IsShown() )
4702 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4705 // this is called when the grid table sends a message
4706 // to indicate that it has been redimensioned
4708 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4711 bool result
= false;
4713 // Clear the attribute cache as the attribute might refer to a different
4714 // cell than stored in the cache after adding/removing rows/columns.
4717 // By the same reasoning, the editor should be dismissed if columns are
4718 // added or removed. And for consistency, it should IMHO always be
4719 // removed, not only if the cell "underneath" it actually changes.
4720 // For now, I intentionally do not save the editor's content as the
4721 // cell it might want to save that stuff to might no longer exist.
4722 HideCellEditControl();
4725 // if we were using the default widths/heights so far, we must change them
4727 if ( m_colWidths
.IsEmpty() )
4732 if ( m_rowHeights
.IsEmpty() )
4738 switch ( msg
.GetId() )
4740 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4742 size_t pos
= msg
.GetCommandInt();
4743 int numRows
= msg
.GetCommandInt2();
4745 m_numRows
+= numRows
;
4747 if ( !m_rowHeights
.IsEmpty() )
4749 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4750 m_rowBottoms
.Insert( 0, pos
, numRows
);
4754 bottom
= m_rowBottoms
[pos
- 1];
4756 for ( i
= pos
; i
< m_numRows
; i
++ )
4758 bottom
+= m_rowHeights
[i
];
4759 m_rowBottoms
[i
] = bottom
;
4763 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4765 // if we have just inserted cols into an empty grid the current
4766 // cell will be undefined...
4768 SetCurrentCell( 0, 0 );
4772 m_selection
->UpdateRows( pos
, numRows
);
4773 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4775 attrProvider
->UpdateAttrRows( pos
, numRows
);
4777 if ( !GetBatchCount() )
4780 m_rowLabelWin
->Refresh();
4786 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4788 int numRows
= msg
.GetCommandInt();
4789 int oldNumRows
= m_numRows
;
4790 m_numRows
+= numRows
;
4792 if ( !m_rowHeights
.IsEmpty() )
4794 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4795 m_rowBottoms
.Add( 0, numRows
);
4798 if ( oldNumRows
> 0 )
4799 bottom
= m_rowBottoms
[oldNumRows
- 1];
4801 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4803 bottom
+= m_rowHeights
[i
];
4804 m_rowBottoms
[i
] = bottom
;
4808 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4810 // if we have just inserted cols into an empty grid the current
4811 // cell will be undefined...
4813 SetCurrentCell( 0, 0 );
4816 if ( !GetBatchCount() )
4819 m_rowLabelWin
->Refresh();
4825 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4827 size_t pos
= msg
.GetCommandInt();
4828 int numRows
= msg
.GetCommandInt2();
4829 m_numRows
-= numRows
;
4831 if ( !m_rowHeights
.IsEmpty() )
4833 m_rowHeights
.RemoveAt( pos
, numRows
);
4834 m_rowBottoms
.RemoveAt( pos
, numRows
);
4837 for ( i
= 0; i
< m_numRows
; i
++ )
4839 h
+= m_rowHeights
[i
];
4840 m_rowBottoms
[i
] = h
;
4846 m_currentCellCoords
= wxGridNoCellCoords
;
4850 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4851 m_currentCellCoords
.Set( 0, 0 );
4855 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4856 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4859 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4861 // ifdef'd out following patch from Paul Gammans
4863 // No need to touch column attributes, unless we
4864 // removed _all_ rows, in this case, we remove
4865 // all column attributes.
4866 // I hate to do this here, but the
4867 // needed data is not available inside UpdateAttrRows.
4868 if ( !GetNumberRows() )
4869 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4873 if ( !GetBatchCount() )
4876 m_rowLabelWin
->Refresh();
4882 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4884 size_t pos
= msg
.GetCommandInt();
4885 int numCols
= msg
.GetCommandInt2();
4886 m_numCols
+= numCols
;
4888 if ( !m_colAt
.IsEmpty() )
4890 //Shift the column IDs
4892 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4894 if ( m_colAt
[i
] >= (int)pos
)
4895 m_colAt
[i
] += numCols
;
4898 m_colAt
.Insert( pos
, pos
, numCols
);
4900 //Set the new columns' positions
4901 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4907 if ( !m_colWidths
.IsEmpty() )
4909 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4910 m_colRights
.Insert( 0, pos
, numCols
);
4914 right
= m_colRights
[GetColAt( pos
- 1 )];
4917 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4919 i
= GetColAt( colPos
);
4921 right
+= m_colWidths
[i
];
4922 m_colRights
[i
] = right
;
4926 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4928 // if we have just inserted cols into an empty grid the current
4929 // cell will be undefined...
4931 SetCurrentCell( 0, 0 );
4935 m_selection
->UpdateCols( pos
, numCols
);
4936 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4938 attrProvider
->UpdateAttrCols( pos
, numCols
);
4939 if ( !GetBatchCount() )
4942 m_colLabelWin
->Refresh();
4948 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4950 int numCols
= msg
.GetCommandInt();
4951 int oldNumCols
= m_numCols
;
4952 m_numCols
+= numCols
;
4954 if ( !m_colAt
.IsEmpty() )
4956 m_colAt
.Add( 0, numCols
);
4958 //Set the new columns' positions
4960 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4966 if ( !m_colWidths
.IsEmpty() )
4968 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4969 m_colRights
.Add( 0, numCols
);
4972 if ( oldNumCols
> 0 )
4973 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4976 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4978 i
= GetColAt( colPos
);
4980 right
+= m_colWidths
[i
];
4981 m_colRights
[i
] = right
;
4985 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4987 // if we have just inserted cols into an empty grid the current
4988 // cell will be undefined...
4990 SetCurrentCell( 0, 0 );
4992 if ( !GetBatchCount() )
4995 m_colLabelWin
->Refresh();
5001 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5003 size_t pos
= msg
.GetCommandInt();
5004 int numCols
= msg
.GetCommandInt2();
5005 m_numCols
-= numCols
;
5007 if ( !m_colAt
.IsEmpty() )
5009 int colID
= GetColAt( pos
);
5011 m_colAt
.RemoveAt( pos
, numCols
);
5013 //Shift the column IDs
5015 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5017 if ( m_colAt
[colPos
] > colID
)
5018 m_colAt
[colPos
] -= numCols
;
5022 if ( !m_colWidths
.IsEmpty() )
5024 m_colWidths
.RemoveAt( pos
, numCols
);
5025 m_colRights
.RemoveAt( pos
, numCols
);
5029 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5031 i
= GetColAt( colPos
);
5033 w
+= m_colWidths
[i
];
5040 m_currentCellCoords
= wxGridNoCellCoords
;
5044 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5045 m_currentCellCoords
.Set( 0, 0 );
5049 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5050 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5053 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5055 // ifdef'd out following patch from Paul Gammans
5057 // No need to touch row attributes, unless we
5058 // removed _all_ columns, in this case, we remove
5059 // all row attributes.
5060 // I hate to do this here, but the
5061 // needed data is not available inside UpdateAttrCols.
5062 if ( !GetNumberCols() )
5063 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5067 if ( !GetBatchCount() )
5070 m_colLabelWin
->Refresh();
5077 if (result
&& !GetBatchCount() )
5078 m_gridWin
->Refresh();
5083 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5085 wxRegionIterator
iter( reg
);
5088 wxArrayInt rowlabels
;
5095 // TODO: remove this when we can...
5096 // There is a bug in wxMotif that gives garbage update
5097 // rectangles if you jump-scroll a long way by clicking the
5098 // scrollbar with middle button. This is a work-around
5100 #if defined(__WXMOTIF__)
5102 m_gridWin
->GetClientSize( &cw
, &ch
);
5103 if ( r
.GetTop() > ch
)
5105 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5108 // logical bounds of update region
5111 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5112 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5114 // find the row labels within these bounds
5117 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5119 if ( GetRowBottom(row
) < top
)
5122 if ( GetRowTop(row
) > bottom
)
5125 rowlabels
.Add( row
);
5134 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5136 wxRegionIterator
iter( reg
);
5139 wxArrayInt colLabels
;
5146 // TODO: remove this when we can...
5147 // There is a bug in wxMotif that gives garbage update
5148 // rectangles if you jump-scroll a long way by clicking the
5149 // scrollbar with middle button. This is a work-around
5151 #if defined(__WXMOTIF__)
5153 m_gridWin
->GetClientSize( &cw
, &ch
);
5154 if ( r
.GetLeft() > cw
)
5156 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5159 // logical bounds of update region
5162 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5163 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5165 // find the cells within these bounds
5169 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5171 col
= GetColAt( colPos
);
5173 if ( GetColRight(col
) < left
)
5176 if ( GetColLeft(col
) > right
)
5179 colLabels
.Add( col
);
5188 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5190 wxRegionIterator
iter( reg
);
5193 wxGridCellCoordsArray cellsExposed
;
5195 int left
, top
, right
, bottom
;
5200 // TODO: remove this when we can...
5201 // There is a bug in wxMotif that gives garbage update
5202 // rectangles if you jump-scroll a long way by clicking the
5203 // scrollbar with middle button. This is a work-around
5205 #if defined(__WXMOTIF__)
5207 m_gridWin
->GetClientSize( &cw
, &ch
);
5208 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5209 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5210 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5211 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5214 // logical bounds of update region
5216 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5217 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5219 // find the cells within these bounds
5222 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5224 if ( GetRowBottom(row
) <= top
)
5227 if ( GetRowTop(row
) > bottom
)
5231 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5233 col
= GetColAt( colPos
);
5235 if ( GetColRight(col
) <= left
)
5238 if ( GetColLeft(col
) > right
)
5241 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5248 return cellsExposed
;
5252 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5255 wxPoint
pos( event
.GetPosition() );
5256 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5258 if ( event
.Dragging() )
5262 m_isDragging
= true;
5263 m_rowLabelWin
->CaptureMouse();
5266 if ( event
.LeftIsDown() )
5268 switch ( m_cursorMode
)
5270 case WXGRID_CURSOR_RESIZE_ROW
:
5272 int cw
, ch
, left
, dummy
;
5273 m_gridWin
->GetClientSize( &cw
, &ch
);
5274 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5276 wxClientDC
dc( m_gridWin
);
5279 GetRowTop(m_dragRowOrCol
) +
5280 GetRowMinimalHeight(m_dragRowOrCol
) );
5281 dc
.SetLogicalFunction(wxINVERT
);
5282 if ( m_dragLastPos
>= 0 )
5284 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5286 dc
.DrawLine( left
, y
, left
+cw
, y
);
5291 case WXGRID_CURSOR_SELECT_ROW
:
5293 if ( (row
= YToRow( y
)) >= 0 )
5297 m_selection
->SelectRow( row
,
5298 event
.ControlDown(),
5307 // default label to suppress warnings about "enumeration value
5308 // 'xxx' not handled in switch
5316 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5321 if (m_rowLabelWin
->HasCapture())
5322 m_rowLabelWin
->ReleaseMouse();
5323 m_isDragging
= false;
5326 // ------------ Entering or leaving the window
5328 if ( event
.Entering() || event
.Leaving() )
5330 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5333 // ------------ Left button pressed
5335 else if ( event
.LeftDown() )
5337 // don't send a label click event for a hit on the
5338 // edge of the row label - this is probably the user
5339 // wanting to resize the row
5341 if ( YToEdgeOfRow(y
) < 0 )
5345 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5347 if ( !event
.ShiftDown() && !event
.CmdDown() )
5351 if ( event
.ShiftDown() )
5353 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5356 GetNumberCols() - 1,
5357 event
.ControlDown(),
5364 m_selection
->SelectRow( row
,
5365 event
.ControlDown(),
5372 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5377 // starting to drag-resize a row
5378 if ( CanDragRowSize() )
5379 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5383 // ------------ Left double click
5385 else if (event
.LeftDClick() )
5387 row
= YToEdgeOfRow(y
);
5392 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5394 // no default action at the moment
5399 // adjust row height depending on label text
5400 AutoSizeRowLabelSize( row
);
5402 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5407 // ------------ Left button released
5409 else if ( event
.LeftUp() )
5411 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5413 DoEndDragResizeRow();
5415 // Note: we are ending the event *after* doing
5416 // default processing in this case
5418 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5421 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5425 // ------------ Right button down
5427 else if ( event
.RightDown() )
5431 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5433 // no default action at the moment
5437 // ------------ Right double click
5439 else if ( event
.RightDClick() )
5443 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5445 // no default action at the moment
5449 // ------------ No buttons down and mouse moving
5451 else if ( event
.Moving() )
5453 m_dragRowOrCol
= YToEdgeOfRow( y
);
5454 if ( m_dragRowOrCol
>= 0 )
5456 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5458 // don't capture the mouse yet
5459 if ( CanDragRowSize() )
5460 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5463 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5465 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5470 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5473 wxPoint
pos( event
.GetPosition() );
5474 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5476 if ( event
.Dragging() )
5480 m_isDragging
= true;
5481 m_colLabelWin
->CaptureMouse();
5483 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5484 m_dragRowOrCol
= XToCol( x
);
5487 if ( event
.LeftIsDown() )
5489 switch ( m_cursorMode
)
5491 case WXGRID_CURSOR_RESIZE_COL
:
5493 int cw
, ch
, dummy
, top
;
5494 m_gridWin
->GetClientSize( &cw
, &ch
);
5495 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5497 wxClientDC
dc( m_gridWin
);
5500 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5501 GetColMinimalWidth(m_dragRowOrCol
));
5502 dc
.SetLogicalFunction(wxINVERT
);
5503 if ( m_dragLastPos
>= 0 )
5505 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5507 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5512 case WXGRID_CURSOR_SELECT_COL
:
5514 if ( (col
= XToCol( x
)) >= 0 )
5518 m_selection
->SelectCol( col
,
5519 event
.ControlDown(),
5528 case WXGRID_CURSOR_MOVE_COL
:
5531 m_moveToCol
= GetColAt( 0 );
5533 m_moveToCol
= XToCol( x
);
5537 if ( m_moveToCol
< 0 )
5538 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5540 markerX
= GetColLeft( m_moveToCol
);
5542 if ( markerX
!= m_dragLastPos
)
5544 wxClientDC
dc( m_colLabelWin
);
5547 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5551 //Clean up the last indicator
5552 if ( m_dragLastPos
>= 0 )
5554 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5556 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5557 dc
.SetPen(wxNullPen
);
5559 if ( XToCol( m_dragLastPos
) != -1 )
5560 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5563 //Moving to the same place? Don't draw a marker
5564 if ( (m_moveToCol
== m_dragRowOrCol
)
5565 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5566 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5573 wxPen
pen( *wxBLUE
, 2 );
5576 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5578 dc
.SetPen(wxNullPen
);
5580 m_dragLastPos
= markerX
- 1;
5585 // default label to suppress warnings about "enumeration value
5586 // 'xxx' not handled in switch
5594 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5599 if (m_colLabelWin
->HasCapture())
5600 m_colLabelWin
->ReleaseMouse();
5601 m_isDragging
= false;
5604 // ------------ Entering or leaving the window
5606 if ( event
.Entering() || event
.Leaving() )
5608 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5611 // ------------ Left button pressed
5613 else if ( event
.LeftDown() )
5615 // don't send a label click event for a hit on the
5616 // edge of the col label - this is probably the user
5617 // wanting to resize the col
5619 if ( XToEdgeOfCol(x
) < 0 )
5623 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5625 if ( m_canDragColMove
)
5627 //Show button as pressed
5628 wxClientDC
dc( m_colLabelWin
);
5629 int colLeft
= GetColLeft( col
);
5630 int colRight
= GetColRight( col
) - 1;
5631 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5632 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5633 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5635 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5639 if ( !event
.ShiftDown() && !event
.CmdDown() )
5643 if ( event
.ShiftDown() )
5645 m_selection
->SelectBlock( 0,
5646 m_currentCellCoords
.GetCol(),
5647 GetNumberRows() - 1, col
,
5648 event
.ControlDown(),
5655 m_selection
->SelectCol( col
,
5656 event
.ControlDown(),
5663 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5669 // starting to drag-resize a col
5671 if ( CanDragColSize() )
5672 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5676 // ------------ Left double click
5678 if ( event
.LeftDClick() )
5680 col
= XToEdgeOfCol(x
);
5685 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5687 // no default action at the moment
5692 // adjust column width depending on label text
5693 AutoSizeColLabelSize( col
);
5695 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5700 // ------------ Left button released
5702 else if ( event
.LeftUp() )
5704 switch ( m_cursorMode
)
5706 case WXGRID_CURSOR_RESIZE_COL
:
5707 DoEndDragResizeCol();
5709 // Note: we are ending the event *after* doing
5710 // default processing in this case
5712 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5715 case WXGRID_CURSOR_MOVE_COL
:
5718 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5721 case WXGRID_CURSOR_SELECT_COL
:
5722 case WXGRID_CURSOR_SELECT_CELL
:
5723 case WXGRID_CURSOR_RESIZE_ROW
:
5724 case WXGRID_CURSOR_SELECT_ROW
:
5725 // nothing to do (?)
5729 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5733 // ------------ Right button down
5735 else if ( event
.RightDown() )
5739 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5741 // no default action at the moment
5745 // ------------ Right double click
5747 else if ( event
.RightDClick() )
5751 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5753 // no default action at the moment
5757 // ------------ No buttons down and mouse moving
5759 else if ( event
.Moving() )
5761 m_dragRowOrCol
= XToEdgeOfCol( x
);
5762 if ( m_dragRowOrCol
>= 0 )
5764 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5766 // don't capture the cursor yet
5767 if ( CanDragColSize() )
5768 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5771 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5773 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5778 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5780 if ( event
.LeftDown() )
5782 // indicate corner label by having both row and
5785 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5790 else if ( event
.LeftDClick() )
5792 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5794 else if ( event
.RightDown() )
5796 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5798 // no default action at the moment
5801 else if ( event
.RightDClick() )
5803 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5805 // no default action at the moment
5810 void wxGrid::ChangeCursorMode(CursorMode mode
,
5815 static const wxChar
*cursorModes
[] =
5825 wxLogTrace(_T("grid"),
5826 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5827 win
== m_colLabelWin
? _T("colLabelWin")
5828 : win
? _T("rowLabelWin")
5830 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5833 if ( mode
== m_cursorMode
&&
5834 win
== m_winCapture
&&
5835 captureMouse
== (m_winCapture
!= NULL
))
5840 // by default use the grid itself
5846 if (m_winCapture
->HasCapture())
5847 m_winCapture
->ReleaseMouse();
5848 m_winCapture
= (wxWindow
*)NULL
;
5851 m_cursorMode
= mode
;
5853 switch ( m_cursorMode
)
5855 case WXGRID_CURSOR_RESIZE_ROW
:
5856 win
->SetCursor( m_rowResizeCursor
);
5859 case WXGRID_CURSOR_RESIZE_COL
:
5860 win
->SetCursor( m_colResizeCursor
);
5863 case WXGRID_CURSOR_MOVE_COL
:
5864 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5868 win
->SetCursor( *wxSTANDARD_CURSOR
);
5872 // we need to capture mouse when resizing
5873 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5874 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5876 if ( captureMouse
&& resize
)
5878 win
->CaptureMouse();
5883 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5886 wxPoint
pos( event
.GetPosition() );
5887 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5889 wxGridCellCoords coords
;
5890 XYToCell( x
, y
, coords
);
5892 int cell_rows
, cell_cols
;
5893 bool isFirstDrag
= !m_isDragging
;
5894 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5895 if ((cell_rows
< 0) || (cell_cols
< 0))
5897 coords
.SetRow(coords
.GetRow() + cell_rows
);
5898 coords
.SetCol(coords
.GetCol() + cell_cols
);
5901 if ( event
.Dragging() )
5903 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5905 // Don't start doing anything until the mouse has been dragged at
5906 // least 3 pixels in any direction...
5909 if (m_startDragPos
== wxDefaultPosition
)
5911 m_startDragPos
= pos
;
5914 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5918 m_isDragging
= true;
5919 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5921 // Hide the edit control, so it
5922 // won't interfere with drag-shrinking.
5923 if ( IsCellEditControlShown() )
5925 HideCellEditControl();
5926 SaveEditControlValue();
5929 if ( coords
!= wxGridNoCellCoords
)
5931 if ( event
.CmdDown() )
5933 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5934 m_selectingKeyboard
= coords
;
5935 HighlightBlock( m_selectingKeyboard
, coords
);
5937 else if ( CanDragCell() )
5941 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5942 m_selectingKeyboard
= coords
;
5944 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5953 if ( !IsSelection() )
5955 HighlightBlock( coords
, coords
);
5959 HighlightBlock( m_currentCellCoords
, coords
);
5963 if (! IsVisible(coords
))
5965 MakeCellVisible(coords
);
5966 // TODO: need to introduce a delay or something here. The
5967 // scrolling is way to fast, at least on MSW - also on GTK.
5970 // Have we captured the mouse yet?
5973 m_winCapture
= m_gridWin
;
5974 m_winCapture
->CaptureMouse();
5979 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5981 int cw
, ch
, left
, dummy
;
5982 m_gridWin
->GetClientSize( &cw
, &ch
);
5983 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5985 wxClientDC
dc( m_gridWin
);
5987 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5988 GetRowMinimalHeight(m_dragRowOrCol
) );
5989 dc
.SetLogicalFunction(wxINVERT
);
5990 if ( m_dragLastPos
>= 0 )
5992 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5994 dc
.DrawLine( left
, y
, left
+cw
, y
);
5997 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5999 int cw
, ch
, dummy
, top
;
6000 m_gridWin
->GetClientSize( &cw
, &ch
);
6001 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6003 wxClientDC
dc( m_gridWin
);
6005 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6006 GetColMinimalWidth(m_dragRowOrCol
) );
6007 dc
.SetLogicalFunction(wxINVERT
);
6008 if ( m_dragLastPos
>= 0 )
6010 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6012 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6019 m_isDragging
= false;
6020 m_startDragPos
= wxDefaultPosition
;
6022 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6023 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6026 if ( event
.Entering() || event
.Leaving() )
6028 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6029 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6034 // ------------ Left button pressed
6036 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6038 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6043 if ( !event
.CmdDown() )
6045 if ( event
.ShiftDown() )
6049 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6050 m_currentCellCoords
.GetCol(),
6053 event
.ControlDown(),
6059 else if ( XToEdgeOfCol(x
) < 0 &&
6060 YToEdgeOfRow(y
) < 0 )
6062 DisableCellEditControl();
6063 MakeCellVisible( coords
);
6065 if ( event
.CmdDown() )
6069 m_selection
->ToggleCellSelection( coords
.GetRow(),
6071 event
.ControlDown(),
6076 m_selectingTopLeft
= wxGridNoCellCoords
;
6077 m_selectingBottomRight
= wxGridNoCellCoords
;
6078 m_selectingKeyboard
= coords
;
6082 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6083 SetCurrentCell( coords
);
6086 if ( m_selection
->GetSelectionMode() !=
6087 wxGrid::wxGridSelectCells
)
6089 HighlightBlock( coords
, coords
);
6097 // ------------ Left double click
6099 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6101 DisableCellEditControl();
6103 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6105 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6110 // we want double click to select a cell and start editing
6111 // (i.e. to behave in same way as sequence of two slow clicks):
6112 m_waitForSlowClick
= true;
6117 // ------------ Left button released
6119 else if ( event
.LeftUp() )
6121 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6125 if (m_winCapture
->HasCapture())
6126 m_winCapture
->ReleaseMouse();
6127 m_winCapture
= NULL
;
6130 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6133 EnableCellEditControl();
6135 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6136 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6137 editor
->StartingClick();
6141 m_waitForSlowClick
= false;
6143 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6144 m_selectingBottomRight
!= wxGridNoCellCoords
)
6148 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6149 m_selectingTopLeft
.GetCol(),
6150 m_selectingBottomRight
.GetRow(),
6151 m_selectingBottomRight
.GetCol(),
6152 event
.ControlDown(),
6158 m_selectingTopLeft
= wxGridNoCellCoords
;
6159 m_selectingBottomRight
= wxGridNoCellCoords
;
6161 // Show the edit control, if it has been hidden for
6163 ShowCellEditControl();
6166 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6168 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6169 DoEndDragResizeRow();
6171 // Note: we are ending the event *after* doing
6172 // default processing in this case
6174 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6176 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6178 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6179 DoEndDragResizeCol();
6181 // Note: we are ending the event *after* doing
6182 // default processing in this case
6184 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6190 // ------------ Right button down
6192 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6194 DisableCellEditControl();
6195 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6200 // no default action at the moment
6204 // ------------ Right double click
6206 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6208 DisableCellEditControl();
6209 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6214 // no default action at the moment
6218 // ------------ Moving and no button action
6220 else if ( event
.Moving() && !event
.IsButton() )
6222 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6224 // out of grid cell area
6225 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6229 int dragRow
= YToEdgeOfRow( y
);
6230 int dragCol
= XToEdgeOfCol( x
);
6232 // Dragging on the corner of a cell to resize in both
6233 // directions is not implemented yet...
6235 if ( dragRow
>= 0 && dragCol
>= 0 )
6237 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6243 m_dragRowOrCol
= dragRow
;
6245 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6247 if ( CanDragRowSize() && CanDragGridSize() )
6248 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6251 else if ( dragCol
>= 0 )
6253 m_dragRowOrCol
= dragCol
;
6255 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6257 if ( CanDragColSize() && CanDragGridSize() )
6258 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6261 else // Neither on a row or col edge
6263 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6265 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6271 void wxGrid::DoEndDragResizeRow()
6273 if ( m_dragLastPos
>= 0 )
6275 // erase the last line and resize the row
6277 int cw
, ch
, left
, dummy
;
6278 m_gridWin
->GetClientSize( &cw
, &ch
);
6279 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6281 wxClientDC
dc( m_gridWin
);
6283 dc
.SetLogicalFunction( wxINVERT
);
6284 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6285 HideCellEditControl();
6286 SaveEditControlValue();
6288 int rowTop
= GetRowTop(m_dragRowOrCol
);
6289 SetRowSize( m_dragRowOrCol
,
6290 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6292 if ( !GetBatchCount() )
6294 // Only needed to get the correct rect.y:
6295 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6297 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6298 rect
.width
= m_rowLabelWidth
;
6299 rect
.height
= ch
- rect
.y
;
6300 m_rowLabelWin
->Refresh( true, &rect
);
6303 // if there is a multicell block, paint all of it
6306 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6307 int leftCol
= XToCol(left
);
6308 int rightCol
= internalXToCol(left
+ cw
);
6311 for (i
=leftCol
; i
<rightCol
; i
++)
6313 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6314 if (cell_rows
< subtract_rows
)
6315 subtract_rows
= cell_rows
;
6317 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6318 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6319 rect
.height
= ch
- rect
.y
;
6322 m_gridWin
->Refresh( false, &rect
);
6325 ShowCellEditControl();
6330 void wxGrid::DoEndDragResizeCol()
6332 if ( m_dragLastPos
>= 0 )
6334 // erase the last line and resize the col
6336 int cw
, ch
, dummy
, top
;
6337 m_gridWin
->GetClientSize( &cw
, &ch
);
6338 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6340 wxClientDC
dc( m_gridWin
);
6342 dc
.SetLogicalFunction( wxINVERT
);
6343 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6344 HideCellEditControl();
6345 SaveEditControlValue();
6347 int colLeft
= GetColLeft(m_dragRowOrCol
);
6348 SetColSize( m_dragRowOrCol
,
6349 wxMax( m_dragLastPos
- colLeft
,
6350 GetColMinimalWidth(m_dragRowOrCol
) ) );
6352 if ( !GetBatchCount() )
6354 // Only needed to get the correct rect.x:
6355 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6357 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6358 rect
.width
= cw
- rect
.x
;
6359 rect
.height
= m_colLabelHeight
;
6360 m_colLabelWin
->Refresh( true, &rect
);
6363 // if there is a multicell block, paint all of it
6366 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6367 int topRow
= YToRow(top
);
6368 int bottomRow
= internalYToRow(top
+ cw
);
6371 for (i
=topRow
; i
<bottomRow
; i
++)
6373 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6374 if (cell_cols
< subtract_cols
)
6375 subtract_cols
= cell_cols
;
6378 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6379 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6380 rect
.width
= cw
- rect
.x
;
6384 m_gridWin
->Refresh( false, &rect
);
6387 ShowCellEditControl();
6391 void wxGrid::DoEndDragMoveCol()
6393 //The user clicked on the column but didn't actually drag
6394 if ( m_dragLastPos
< 0 )
6396 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6401 if ( m_moveToCol
== -1 )
6402 newPos
= m_numCols
- 1;
6405 newPos
= GetColPos( m_moveToCol
);
6406 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6410 SetColPos( m_dragRowOrCol
, newPos
);
6413 void wxGrid::SetColPos( int colID
, int newPos
)
6415 if ( m_colAt
.IsEmpty() )
6417 m_colAt
.Alloc( m_numCols
);
6420 for ( i
= 0; i
< m_numCols
; i
++ )
6426 int oldPos
= GetColPos( colID
);
6428 //Reshuffle the m_colAt array
6429 if ( newPos
> oldPos
)
6432 for ( i
= oldPos
; i
< newPos
; i
++ )
6434 m_colAt
[i
] = m_colAt
[i
+1];
6440 for ( i
= oldPos
; i
> newPos
; i
-- )
6442 m_colAt
[i
] = m_colAt
[i
-1];
6446 m_colAt
[newPos
] = colID
;
6448 //Recalculate the column rights
6449 if ( !m_colWidths
.IsEmpty() )
6453 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6455 int colID
= GetColAt( colPos
);
6457 colRight
+= m_colWidths
[colID
];
6458 m_colRights
[colID
] = colRight
;
6462 m_colLabelWin
->Refresh();
6463 m_gridWin
->Refresh();
6468 void wxGrid::EnableDragColMove( bool enable
)
6470 if ( m_canDragColMove
== enable
)
6473 m_canDragColMove
= enable
;
6475 if ( !m_canDragColMove
)
6479 //Recalculate the column rights
6480 if ( !m_colWidths
.IsEmpty() )
6484 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6486 colRight
+= m_colWidths
[colPos
];
6487 m_colRights
[colPos
] = colRight
;
6491 m_colLabelWin
->Refresh();
6492 m_gridWin
->Refresh();
6498 // ------ interaction with data model
6500 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6502 switch ( msg
.GetId() )
6504 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6505 return GetModelValues();
6507 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6508 return SetModelValues();
6510 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6511 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6512 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6513 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6514 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6515 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6516 return Redimension( msg
);
6523 // The behaviour of this function depends on the grid table class
6524 // Clear() function. For the default wxGridStringTable class the
6525 // behavious is to replace all cell contents with wxEmptyString but
6526 // not to change the number of rows or cols.
6528 void wxGrid::ClearGrid()
6532 if (IsCellEditControlEnabled())
6533 DisableCellEditControl();
6536 if (!GetBatchCount())
6537 m_gridWin
->Refresh();
6541 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6543 // TODO: something with updateLabels flag
6547 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6553 if (IsCellEditControlEnabled())
6554 DisableCellEditControl();
6556 bool done
= m_table
->InsertRows( pos
, numRows
);
6559 // the table will have sent the results of the insert row
6560 // operation to this view object as a grid table message
6566 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6568 // TODO: something with updateLabels flag
6572 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6578 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6581 // the table will have sent the results of the append row
6582 // operation to this view object as a grid table message
6588 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6590 // TODO: something with updateLabels flag
6594 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6600 if (IsCellEditControlEnabled())
6601 DisableCellEditControl();
6603 bool done
= m_table
->DeleteRows( pos
, numRows
);
6605 // the table will have sent the results of the delete row
6606 // operation to this view object as a grid table message
6612 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6614 // TODO: something with updateLabels flag
6618 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6624 if (IsCellEditControlEnabled())
6625 DisableCellEditControl();
6627 bool done
= m_table
->InsertCols( pos
, numCols
);
6629 // the table will have sent the results of the insert col
6630 // operation to this view object as a grid table message
6636 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6638 // TODO: something with updateLabels flag
6642 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6648 bool done
= m_table
->AppendCols( numCols
);
6650 // the table will have sent the results of the append col
6651 // operation to this view object as a grid table message
6657 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6659 // TODO: something with updateLabels flag
6663 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6669 if (IsCellEditControlEnabled())
6670 DisableCellEditControl();
6672 bool done
= m_table
->DeleteCols( pos
, numCols
);
6674 // the table will have sent the results of the delete col
6675 // operation to this view object as a grid table message
6682 // ----- event handlers
6685 // Generate a grid event based on a mouse event and
6686 // return the result of ProcessEvent()
6688 int wxGrid::SendEvent( const wxEventType type
,
6690 wxMouseEvent
& mouseEv
)
6692 bool claimed
, vetoed
;
6694 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6696 int rowOrCol
= (row
== -1 ? col
: row
);
6698 wxGridSizeEvent
gridEvt( GetId(),
6702 mouseEv
.GetX() + GetRowLabelSize(),
6703 mouseEv
.GetY() + GetColLabelSize(),
6704 mouseEv
.ControlDown(),
6705 mouseEv
.ShiftDown(),
6707 mouseEv
.MetaDown() );
6709 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6710 vetoed
= !gridEvt
.IsAllowed();
6712 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6714 // Right now, it should _never_ end up here!
6715 wxGridRangeSelectEvent
gridEvt( GetId(),
6719 m_selectingBottomRight
,
6721 mouseEv
.ControlDown(),
6722 mouseEv
.ShiftDown(),
6724 mouseEv
.MetaDown() );
6726 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6727 vetoed
= !gridEvt
.IsAllowed();
6729 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6730 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6731 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6732 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6734 wxPoint pos
= mouseEv
.GetPosition();
6736 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6737 pos
.y
+= GetColLabelSize();
6738 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6739 pos
.x
+= GetRowLabelSize();
6741 wxGridEvent
gridEvt( GetId(),
6748 mouseEv
.ControlDown(),
6749 mouseEv
.ShiftDown(),
6751 mouseEv
.MetaDown() );
6752 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6753 vetoed
= !gridEvt
.IsAllowed();
6757 wxGridEvent
gridEvt( GetId(),
6761 mouseEv
.GetX() + GetRowLabelSize(),
6762 mouseEv
.GetY() + GetColLabelSize(),
6764 mouseEv
.ControlDown(),
6765 mouseEv
.ShiftDown(),
6767 mouseEv
.MetaDown() );
6768 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6769 vetoed
= !gridEvt
.IsAllowed();
6772 // A Veto'd event may not be `claimed' so test this first
6776 return claimed
? 1 : 0;
6779 // Generate a grid event of specified type and return the result
6780 // of ProcessEvent().
6782 int wxGrid::SendEvent( const wxEventType type
,
6785 bool claimed
, vetoed
;
6787 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6789 int rowOrCol
= (row
== -1 ? col
: row
);
6791 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6793 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6794 vetoed
= !gridEvt
.IsAllowed();
6798 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6800 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6801 vetoed
= !gridEvt
.IsAllowed();
6804 // A Veto'd event may not be `claimed' so test this first
6808 return claimed
? 1 : 0;
6811 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6813 // needed to prevent zillions of paint events on MSW
6817 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6819 // Don't do anything if between Begin/EndBatch...
6820 // EndBatch() will do all this on the last nested one anyway.
6821 if (! GetBatchCount())
6823 // Refresh to get correct scrolled position:
6824 wxScrolledWindow::Refresh(eraseb
, rect
);
6828 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6829 int width_label
, width_cell
, height_label
, height_cell
;
6832 // Copy rectangle can get scroll offsets..
6833 rect_x
= rect
->GetX();
6834 rect_y
= rect
->GetY();
6835 rectWidth
= rect
->GetWidth();
6836 rectHeight
= rect
->GetHeight();
6838 width_label
= m_rowLabelWidth
- rect_x
;
6839 if (width_label
> rectWidth
)
6840 width_label
= rectWidth
;
6842 height_label
= m_colLabelHeight
- rect_y
;
6843 if (height_label
> rectHeight
)
6844 height_label
= rectHeight
;
6846 if (rect_x
> m_rowLabelWidth
)
6848 x
= rect_x
- m_rowLabelWidth
;
6849 width_cell
= rectWidth
;
6854 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6857 if (rect_y
> m_colLabelHeight
)
6859 y
= rect_y
- m_colLabelHeight
;
6860 height_cell
= rectHeight
;
6865 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6868 // Paint corner label part intersecting rect.
6869 if ( width_label
> 0 && height_label
> 0 )
6871 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6872 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6875 // Paint col labels part intersecting rect.
6876 if ( width_cell
> 0 && height_label
> 0 )
6878 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6879 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6882 // Paint row labels part intersecting rect.
6883 if ( width_label
> 0 && height_cell
> 0 )
6885 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6886 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6889 // Paint cell area part intersecting rect.
6890 if ( width_cell
> 0 && height_cell
> 0 )
6892 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6893 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6898 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6899 m_colLabelWin
->Refresh(eraseb
, NULL
);
6900 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6901 m_gridWin
->Refresh(eraseb
, NULL
);
6906 void wxGrid::OnSize( wxSizeEvent
& event
)
6908 // position the child windows
6911 // don't call CalcDimensions() from here, the base class handles the size
6916 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6918 if ( m_inOnKeyDown
)
6920 // shouldn't be here - we are going round in circles...
6922 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6925 m_inOnKeyDown
= true;
6927 // propagate the event up and see if it gets processed
6928 wxWindow
*parent
= GetParent();
6929 wxKeyEvent
keyEvt( event
);
6930 keyEvt
.SetEventObject( parent
);
6932 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6934 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6936 if (event
.GetKeyCode() == WXK_RIGHT
)
6937 event
.m_keyCode
= WXK_LEFT
;
6938 else if (event
.GetKeyCode() == WXK_LEFT
)
6939 event
.m_keyCode
= WXK_RIGHT
;
6942 // try local handlers
6943 switch ( event
.GetKeyCode() )
6946 if ( event
.ControlDown() )
6947 MoveCursorUpBlock( event
.ShiftDown() );
6949 MoveCursorUp( event
.ShiftDown() );
6953 if ( event
.ControlDown() )
6954 MoveCursorDownBlock( event
.ShiftDown() );
6956 MoveCursorDown( event
.ShiftDown() );
6960 if ( event
.ControlDown() )
6961 MoveCursorLeftBlock( event
.ShiftDown() );
6963 MoveCursorLeft( event
.ShiftDown() );
6967 if ( event
.ControlDown() )
6968 MoveCursorRightBlock( event
.ShiftDown() );
6970 MoveCursorRight( event
.ShiftDown() );
6974 case WXK_NUMPAD_ENTER
:
6975 if ( event
.ControlDown() )
6977 event
.Skip(); // to let the edit control have the return
6981 if ( GetGridCursorRow() < GetNumberRows()-1 )
6983 MoveCursorDown( event
.ShiftDown() );
6987 // at the bottom of a column
6988 DisableCellEditControl();
6998 if (event
.ShiftDown())
7000 if ( GetGridCursorCol() > 0 )
7002 MoveCursorLeft( false );
7007 DisableCellEditControl();
7012 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7014 MoveCursorRight( false );
7019 DisableCellEditControl();
7025 if ( event
.ControlDown() )
7027 MakeCellVisible( 0, 0 );
7028 SetCurrentCell( 0, 0 );
7037 if ( event
.ControlDown() )
7039 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7040 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7057 if ( event
.ControlDown() )
7061 m_selection
->ToggleCellSelection(
7062 m_currentCellCoords
.GetRow(),
7063 m_currentCellCoords
.GetCol(),
7064 event
.ControlDown(),
7072 if ( !IsEditable() )
7073 MoveCursorRight( false );
7084 m_inOnKeyDown
= false;
7087 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7089 // try local handlers
7091 if ( event
.GetKeyCode() == WXK_SHIFT
)
7093 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7094 m_selectingBottomRight
!= wxGridNoCellCoords
)
7098 m_selection
->SelectBlock(
7099 m_selectingTopLeft
.GetRow(),
7100 m_selectingTopLeft
.GetCol(),
7101 m_selectingBottomRight
.GetRow(),
7102 m_selectingBottomRight
.GetCol(),
7103 event
.ControlDown(),
7110 m_selectingTopLeft
= wxGridNoCellCoords
;
7111 m_selectingBottomRight
= wxGridNoCellCoords
;
7112 m_selectingKeyboard
= wxGridNoCellCoords
;
7116 void wxGrid::OnChar( wxKeyEvent
& event
)
7118 // is it possible to edit the current cell at all?
7119 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7121 // yes, now check whether the cells editor accepts the key
7122 int row
= m_currentCellCoords
.GetRow();
7123 int col
= m_currentCellCoords
.GetCol();
7124 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7125 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7127 // <F2> is special and will always start editing, for
7128 // other keys - ask the editor itself
7129 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7130 || editor
->IsAcceptedKey(event
) )
7132 // ensure cell is visble
7133 MakeCellVisible(row
, col
);
7134 EnableCellEditControl();
7136 // a problem can arise if the cell is not completely
7137 // visible (even after calling MakeCellVisible the
7138 // control is not created and calling StartingKey will
7140 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7141 editor
->StartingKey(event
);
7157 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7161 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7163 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7165 // the event has been intercepted - do nothing
7169 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7170 wxClientDC
dc( m_gridWin
);
7174 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7176 DisableCellEditControl();
7178 if ( IsVisible( m_currentCellCoords
, false ) )
7181 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7182 if ( !m_gridLinesEnabled
)
7190 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7192 // Otherwise refresh redraws the highlight!
7193 m_currentCellCoords
= coords
;
7195 #if defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS
7196 m_gridWin
->Refresh(true /*, & r */);
7198 DrawGridCellArea( dc
, cells
);
7199 DrawAllGridLines( dc
, r
);
7204 m_currentCellCoords
= coords
;
7206 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7207 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7208 DrawCellHighlight( dc
, attr
);
7213 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7216 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7220 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7223 rightCol
= GetNumberCols() - 1;
7225 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7228 bottomRow
= GetNumberRows() - 1;
7232 if ( topRow
> bottomRow
)
7239 if ( leftCol
> rightCol
)
7246 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7247 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7249 // First the case that we selected a completely new area
7250 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7251 m_selectingBottomRight
== wxGridNoCellCoords
)
7254 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7255 wxGridCellCoords ( bottomRow
, rightCol
) );
7256 m_gridWin
->Refresh( false, &rect
);
7259 // Now handle changing an existing selection area.
7260 else if ( m_selectingTopLeft
!= updateTopLeft
||
7261 m_selectingBottomRight
!= updateBottomRight
)
7263 // Compute two optimal update rectangles:
7264 // Either one rectangle is a real subset of the
7265 // other, or they are (almost) disjoint!
7267 bool need_refresh
[4];
7271 need_refresh
[3] = false;
7274 // Store intermediate values
7275 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7276 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7277 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7278 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7280 // Determine the outer/inner coordinates.
7281 if (oldLeft
> leftCol
)
7287 if (oldTop
> topRow
)
7293 if (oldRight
< rightCol
)
7296 oldRight
= rightCol
;
7299 if (oldBottom
< bottomRow
)
7302 oldBottom
= bottomRow
;
7306 // Now, either the stuff marked old is the outer
7307 // rectangle or we don't have a situation where one
7308 // is contained in the other.
7310 if ( oldLeft
< leftCol
)
7312 // Refresh the newly selected or deselected
7313 // area to the left of the old or new selection.
7314 need_refresh
[0] = true;
7315 rect
[0] = BlockToDeviceRect(
7316 wxGridCellCoords( oldTop
, oldLeft
),
7317 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7320 if ( oldTop
< topRow
)
7322 // Refresh the newly selected or deselected
7323 // area above the old or new selection.
7324 need_refresh
[1] = true;
7325 rect
[1] = BlockToDeviceRect(
7326 wxGridCellCoords( oldTop
, leftCol
),
7327 wxGridCellCoords( topRow
- 1, rightCol
) );
7330 if ( oldRight
> rightCol
)
7332 // Refresh the newly selected or deselected
7333 // area to the right of the old or new selection.
7334 need_refresh
[2] = true;
7335 rect
[2] = BlockToDeviceRect(
7336 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7337 wxGridCellCoords( oldBottom
, oldRight
) );
7340 if ( oldBottom
> bottomRow
)
7342 // Refresh the newly selected or deselected
7343 // area below the old or new selection.
7344 need_refresh
[3] = true;
7345 rect
[3] = BlockToDeviceRect(
7346 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7347 wxGridCellCoords( oldBottom
, rightCol
) );
7350 // various Refresh() calls
7351 for (i
= 0; i
< 4; i
++ )
7352 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7353 m_gridWin
->Refresh( false, &(rect
[i
]) );
7357 m_selectingTopLeft
= updateTopLeft
;
7358 m_selectingBottomRight
= updateBottomRight
;
7362 // ------ functions to get/send data (see also public functions)
7365 bool wxGrid::GetModelValues()
7367 // Hide the editor, so it won't hide a changed value.
7368 HideCellEditControl();
7372 // all we need to do is repaint the grid
7374 m_gridWin
->Refresh();
7381 bool wxGrid::SetModelValues()
7385 // Disable the editor, so it won't hide a changed value.
7386 // Do we also want to save the current value of the editor first?
7388 DisableCellEditControl();
7392 for ( row
= 0; row
< m_numRows
; row
++ )
7394 for ( col
= 0; col
< m_numCols
; col
++ )
7396 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7406 // Note - this function only draws cells that are in the list of
7407 // exposed cells (usually set from the update region by
7408 // CalcExposedCells)
7410 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7412 if ( !m_numRows
|| !m_numCols
)
7415 int i
, numCells
= cells
.GetCount();
7416 int row
, col
, cell_rows
, cell_cols
;
7417 wxGridCellCoordsArray redrawCells
;
7419 for ( i
= numCells
- 1; i
>= 0; i
-- )
7421 row
= cells
[i
].GetRow();
7422 col
= cells
[i
].GetCol();
7423 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7425 // If this cell is part of a multicell block, find owner for repaint
7426 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7428 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7429 bool marked
= false;
7430 for ( int j
= 0; j
< numCells
; j
++ )
7432 if ( cell
== cells
[j
] )
7441 int count
= redrawCells
.GetCount();
7442 for (int j
= 0; j
< count
; j
++)
7444 if ( cell
== redrawCells
[j
] )
7452 redrawCells
.Add( cell
);
7455 // don't bother drawing this cell
7459 // If this cell is empty, find cell to left that might want to overflow
7460 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7462 for ( int l
= 0; l
< cell_rows
; l
++ )
7464 // find a cell in this row to leave already marked for repaint
7466 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7467 if ((redrawCells
[k
].GetCol() < left
) &&
7468 (redrawCells
[k
].GetRow() == row
))
7470 left
= redrawCells
[k
].GetCol();
7474 left
= 0; // oh well
7476 for (int j
= col
- 1; j
>= left
; j
--)
7478 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7480 if (GetCellOverflow(row
+ l
, j
))
7482 wxGridCellCoords
cell(row
+ l
, j
);
7483 bool marked
= false;
7485 for (int k
= 0; k
< numCells
; k
++)
7487 if ( cell
== cells
[k
] )
7496 int count
= redrawCells
.GetCount();
7497 for (int k
= 0; k
< count
; k
++)
7499 if ( cell
== redrawCells
[k
] )
7506 redrawCells
.Add( cell
);
7515 DrawCell( dc
, cells
[i
] );
7518 numCells
= redrawCells
.GetCount();
7520 for ( i
= numCells
- 1; i
>= 0; i
-- )
7522 DrawCell( dc
, redrawCells
[i
] );
7526 void wxGrid::DrawGridSpace( wxDC
& dc
)
7529 m_gridWin
->GetClientSize( &cw
, &ch
);
7532 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7534 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7535 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7537 if ( right
> rightCol
|| bottom
> bottomRow
)
7540 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7542 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7543 dc
.SetPen( *wxTRANSPARENT_PEN
);
7545 if ( right
> rightCol
)
7547 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7550 if ( bottom
> bottomRow
)
7552 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7557 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7559 int row
= coords
.GetRow();
7560 int col
= coords
.GetCol();
7562 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7565 // we draw the cell border ourselves
7566 #if !WXGRID_DRAW_LINES
7567 if ( m_gridLinesEnabled
)
7568 DrawCellBorder( dc
, coords
);
7571 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7573 bool isCurrent
= coords
== m_currentCellCoords
;
7575 wxRect rect
= CellToRect( row
, col
);
7577 // if the editor is shown, we should use it and not the renderer
7578 // Note: However, only if it is really _shown_, i.e. not hidden!
7579 if ( isCurrent
&& IsCellEditControlShown() )
7581 // NB: this "#if..." is temporary and fixes a problem where the
7582 // edit control is erased by this code after being rendered.
7583 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7584 // implicitly, causing this out-of order render.
7585 #if !defined(__WXMAC__)
7586 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7587 editor
->PaintBackground(rect
, attr
);
7593 // but all the rest is drawn by the cell renderer and hence may be customized
7594 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7595 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7602 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7604 int row
= m_currentCellCoords
.GetRow();
7605 int col
= m_currentCellCoords
.GetCol();
7607 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7610 wxRect rect
= CellToRect(row
, col
);
7612 // hmmm... what could we do here to show that the cell is disabled?
7613 // for now, I just draw a thinner border than for the other ones, but
7614 // it doesn't look really good
7616 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7620 // The center of the drawn line is where the position/width/height of
7621 // the rectangle is actually at (on wxMSW at least), so the
7622 // size of the rectangle is reduced to compensate for the thickness of
7623 // the line. If this is too strange on non-wxMSW platforms then
7624 // please #ifdef this appropriately.
7625 rect
.x
+= penWidth
/ 2;
7626 rect
.y
+= penWidth
/ 2;
7627 rect
.width
-= penWidth
- 1;
7628 rect
.height
-= penWidth
- 1;
7630 // Now draw the rectangle
7631 // use the cellHighlightColour if the cell is inside a selection, this
7632 // will ensure the cell is always visible.
7633 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7634 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7635 dc
.DrawRectangle(rect
);
7639 // VZ: my experiments with 3D borders...
7641 // how to properly set colours for arbitrary bg?
7642 wxCoord x1
= rect
.x
,
7644 x2
= rect
.x
+ rect
.width
- 1,
7645 y2
= rect
.y
+ rect
.height
- 1;
7647 dc
.SetPen(*wxWHITE_PEN
);
7648 dc
.DrawLine(x1
, y1
, x2
, y1
);
7649 dc
.DrawLine(x1
, y1
, x1
, y2
);
7651 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7652 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7654 dc
.SetPen(*wxBLACK_PEN
);
7655 dc
.DrawLine(x1
, y2
, x2
, y2
);
7656 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7660 wxPen
wxGrid::GetDefaultGridLinePen()
7662 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7665 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7667 return GetDefaultGridLinePen();
7670 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7672 return GetDefaultGridLinePen();
7675 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7677 int row
= coords
.GetRow();
7678 int col
= coords
.GetCol();
7679 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7683 wxRect rect
= CellToRect( row
, col
);
7685 // right hand border
7686 dc
.SetPen( GetColGridLinePen(col
) );
7687 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7688 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7691 dc
.SetPen( GetRowGridLinePen(row
) );
7692 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7693 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7696 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7698 // This if block was previously in wxGrid::OnPaint but that doesn't
7699 // seem to get called under wxGTK - MB
7701 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7702 m_numRows
&& m_numCols
)
7704 m_currentCellCoords
.Set(0, 0);
7707 if ( IsCellEditControlShown() )
7709 // don't show highlight when the edit control is shown
7713 // if the active cell was repainted, repaint its highlight too because it
7714 // might have been damaged by the grid lines
7715 size_t count
= cells
.GetCount();
7716 for ( size_t n
= 0; n
< count
; n
++ )
7718 if ( cells
[n
] == m_currentCellCoords
)
7720 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7721 DrawCellHighlight(dc
, attr
);
7729 // TODO: remove this ???
7730 // This is used to redraw all grid lines e.g. when the grid line colour
7733 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7735 #if !WXGRID_DRAW_LINES
7739 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7742 int top
, bottom
, left
, right
;
7744 #if 0 //#ifndef __WXGTK__
7748 m_gridWin
->GetClientSize(&cw
, &ch
);
7750 // virtual coords of visible area
7752 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7753 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7758 reg
.GetBox(x
, y
, w
, h
);
7759 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7760 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7764 m_gridWin
->GetClientSize(&cw
, &ch
);
7765 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7766 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7769 // avoid drawing grid lines past the last row and col
7771 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7772 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7774 // no gridlines inside multicells, clip them out
7775 int leftCol
= GetColPos( internalXToCol(left
) );
7776 int topRow
= internalYToRow(top
);
7777 int rightCol
= GetColPos( internalXToCol(right
) );
7778 int bottomRow
= internalYToRow(bottom
);
7780 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7781 wxRegion
clippedcells(0, 0, cw
, ch
);
7783 int i
, j
, cell_rows
, cell_cols
;
7786 for (j
=topRow
; j
<=bottomRow
; j
++)
7789 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7791 i
= GetColAt( colPos
);
7793 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7794 if ((cell_rows
> 1) || (cell_cols
> 1))
7796 rect
= CellToRect(j
,i
);
7797 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7798 clippedcells
.Subtract(rect
);
7800 else if ((cell_rows
< 0) || (cell_cols
< 0))
7802 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7803 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7804 clippedcells
.Subtract(rect
);
7809 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7811 int i
, j
, cell_rows
, cell_cols
;
7814 for (j
=topRow
; j
<=bottomRow
; j
++)
7816 for (i
=leftCol
; i
<=rightCol
; i
++)
7818 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7819 if ((cell_rows
> 1) || (cell_cols
> 1))
7821 rect
= CellToRect(j
, i
);
7822 clippedcells
.Subtract(rect
);
7824 else if ((cell_rows
< 0) || (cell_cols
< 0))
7826 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7827 clippedcells
.Subtract(rect
);
7833 dc
.SetClippingRegion( clippedcells
);
7836 // horizontal grid lines
7838 // already declared above - int i;
7839 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7841 int bot
= GetRowBottom(i
) - 1;
7850 dc
.SetPen( GetRowGridLinePen(i
) );
7851 dc
.DrawLine( left
, bot
, right
, bot
);
7855 // vertical grid lines
7858 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7860 i
= GetColAt( colPos
);
7862 int colRight
= GetColRight(i
);
7864 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7868 if ( colRight
> right
)
7873 if ( colRight
>= left
)
7875 dc
.SetPen( GetColGridLinePen(i
) );
7876 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7880 dc
.DestroyClippingRegion();
7883 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7889 size_t numLabels
= rows
.GetCount();
7891 for ( i
= 0; i
< numLabels
; i
++ )
7893 DrawRowLabel( dc
, rows
[i
] );
7897 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7899 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7907 rect
.SetY( GetRowTop(row
) + 1 );
7908 rect
.SetWidth( m_rowLabelWidth
- 2 );
7909 rect
.SetHeight( GetRowHeight(row
) - 2 );
7911 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7913 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7915 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7917 int rowTop
= GetRowTop(row
),
7918 rowBottom
= GetRowBottom(row
) - 1;
7920 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7921 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7922 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7923 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7925 dc
.SetPen( *wxWHITE_PEN
);
7926 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7927 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7930 dc
.SetBackgroundMode( wxTRANSPARENT
);
7931 dc
.SetTextForeground( GetLabelTextColour() );
7932 dc
.SetFont( GetLabelFont() );
7935 GetRowLabelAlignment( &hAlign
, &vAlign
);
7938 rect
.SetY( GetRowTop(row
) + 2 );
7939 rect
.SetWidth( m_rowLabelWidth
- 4 );
7940 rect
.SetHeight( GetRowHeight(row
) - 4 );
7941 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7944 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7950 size_t numLabels
= cols
.GetCount();
7952 for ( i
= 0; i
< numLabels
; i
++ )
7954 DrawColLabel( dc
, cols
[i
] );
7958 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7960 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7963 int colLeft
= GetColLeft(col
);
7969 rect
.SetX( colLeft
+ 1 );
7971 rect
.SetWidth( GetColWidth(col
) - 2 );
7972 rect
.SetHeight( m_colLabelHeight
- 2 );
7974 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7976 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7978 int colRight
= GetColRight(col
) - 1;
7980 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7981 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7982 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7983 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7984 colRight
+ 1, m_colLabelHeight
- 1 );
7986 dc
.SetPen( *wxWHITE_PEN
);
7987 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7988 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7991 dc
.SetBackgroundMode( wxTRANSPARENT
);
7992 dc
.SetTextForeground( GetLabelTextColour() );
7993 dc
.SetFont( GetLabelFont() );
7995 int hAlign
, vAlign
, orient
;
7996 GetColLabelAlignment( &hAlign
, &vAlign
);
7997 orient
= GetColLabelTextOrientation();
7999 rect
.SetX( colLeft
+ 2 );
8001 rect
.SetWidth( GetColWidth(col
) - 4 );
8002 rect
.SetHeight( m_colLabelHeight
- 4 );
8003 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8006 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8007 const wxString
& value
,
8011 int textOrientation
)
8013 wxArrayString lines
;
8015 StringToLines( value
, lines
);
8017 // Forward to new API.
8018 DrawTextRectangle( dc
,
8026 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8027 // add textOrientation support
8028 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8029 const wxArrayString
& lines
,
8033 int textOrientation
)
8035 if ( lines
.empty() )
8038 wxDCClipper
clip(dc
, rect
);
8043 if ( textOrientation
== wxHORIZONTAL
)
8044 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8046 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8050 switch ( vertAlign
)
8052 case wxALIGN_BOTTOM
:
8053 if ( textOrientation
== wxHORIZONTAL
)
8054 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8056 x
= rect
.x
+ rect
.width
- textWidth
;
8059 case wxALIGN_CENTRE
:
8060 if ( textOrientation
== wxHORIZONTAL
)
8061 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8063 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8068 if ( textOrientation
== wxHORIZONTAL
)
8075 // Align each line of a multi-line label
8076 size_t nLines
= lines
.GetCount();
8077 for ( size_t l
= 0; l
< nLines
; l
++ )
8079 const wxString
& line
= lines
[l
];
8083 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8089 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8091 switch ( horizAlign
)
8094 if ( textOrientation
== wxHORIZONTAL
)
8095 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8097 y
= rect
.y
+ lineWidth
+ 1;
8100 case wxALIGN_CENTRE
:
8101 if ( textOrientation
== wxHORIZONTAL
)
8102 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8104 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8109 if ( textOrientation
== wxHORIZONTAL
)
8112 y
= rect
.y
+ rect
.height
- 1;
8116 if ( textOrientation
== wxHORIZONTAL
)
8118 dc
.DrawText( line
, x
, y
);
8123 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8129 // Split multi-line text up into an array of strings.
8130 // Any existing contents of the string array are preserved.
8132 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8136 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8137 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8139 while ( startPos
< (int)tVal
.length() )
8141 pos
= tVal
.Mid(startPos
).Find( eol
);
8146 else if ( pos
== 0 )
8148 lines
.Add( wxEmptyString
);
8152 lines
.Add( value
.Mid(startPos
, pos
) );
8155 startPos
+= pos
+ 1;
8158 if ( startPos
< (int)value
.length() )
8160 lines
.Add( value
.Mid( startPos
) );
8164 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8165 const wxArrayString
& lines
,
8166 long *width
, long *height
) const
8170 long lineW
= 0, lineH
= 0;
8173 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8175 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8176 w
= wxMax( w
, lineW
);
8185 // ------ Batch processing.
8187 void wxGrid::EndBatch()
8189 if ( m_batchCount
> 0 )
8192 if ( !m_batchCount
)
8195 m_rowLabelWin
->Refresh();
8196 m_colLabelWin
->Refresh();
8197 m_cornerLabelWin
->Refresh();
8198 m_gridWin
->Refresh();
8203 // Use this, rather than wxWindow::Refresh(), to force an immediate
8204 // repainting of the grid. Has no effect if you are already inside a
8205 // BeginBatch / EndBatch block.
8207 void wxGrid::ForceRefresh()
8213 bool wxGrid::Enable(bool enable
)
8215 if ( !wxScrolledWindow::Enable(enable
) )
8218 // redraw in the new state
8219 m_gridWin
->Refresh();
8225 // ------ Edit control functions
8228 void wxGrid::EnableEditing( bool edit
)
8230 // TODO: improve this ?
8232 if ( edit
!= m_editable
)
8235 EnableCellEditControl(edit
);
8240 void wxGrid::EnableCellEditControl( bool enable
)
8245 if ( enable
!= m_cellEditCtrlEnabled
)
8249 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8252 // this should be checked by the caller!
8253 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8255 // do it before ShowCellEditControl()
8256 m_cellEditCtrlEnabled
= enable
;
8258 ShowCellEditControl();
8262 //FIXME:add veto support
8263 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8265 HideCellEditControl();
8266 SaveEditControlValue();
8268 // do it after HideCellEditControl()
8269 m_cellEditCtrlEnabled
= enable
;
8274 bool wxGrid::IsCurrentCellReadOnly() const
8277 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8278 bool readonly
= attr
->IsReadOnly();
8284 bool wxGrid::CanEnableCellControl() const
8286 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8287 !IsCurrentCellReadOnly();
8290 bool wxGrid::IsCellEditControlEnabled() const
8292 // the cell edit control might be disable for all cells or just for the
8293 // current one if it's read only
8294 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8297 bool wxGrid::IsCellEditControlShown() const
8299 bool isShown
= false;
8301 if ( m_cellEditCtrlEnabled
)
8303 int row
= m_currentCellCoords
.GetRow();
8304 int col
= m_currentCellCoords
.GetCol();
8305 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8306 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8311 if ( editor
->IsCreated() )
8313 isShown
= editor
->GetControl()->IsShown();
8323 void wxGrid::ShowCellEditControl()
8325 if ( IsCellEditControlEnabled() )
8327 if ( !IsVisible( m_currentCellCoords
, false ) )
8329 m_cellEditCtrlEnabled
= false;
8334 wxRect rect
= CellToRect( m_currentCellCoords
);
8335 int row
= m_currentCellCoords
.GetRow();
8336 int col
= m_currentCellCoords
.GetCol();
8338 // if this is part of a multicell, find owner (topleft)
8339 int cell_rows
, cell_cols
;
8340 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8341 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8345 m_currentCellCoords
.SetRow( row
);
8346 m_currentCellCoords
.SetCol( col
);
8349 // erase the highlight and the cell contents because the editor
8350 // might not cover the entire cell
8351 wxClientDC
dc( m_gridWin
);
8353 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8354 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
8355 dc
.SetPen(*wxTRANSPARENT_PEN
);
8356 dc
.DrawRectangle(rect
);
8358 // convert to scrolled coords
8359 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8365 // cell is shifted by one pixel
8366 // However, don't allow x or y to become negative
8367 // since the SetSize() method interprets that as
8374 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8375 if ( !editor
->IsCreated() )
8377 editor
->Create(m_gridWin
, wxID_ANY
,
8378 new wxGridCellEditorEvtHandler(this, editor
));
8380 wxGridEditorCreatedEvent
evt(GetId(),
8381 wxEVT_GRID_EDITOR_CREATED
,
8385 editor
->GetControl());
8386 GetEventHandler()->ProcessEvent(evt
);
8389 // resize editor to overflow into righthand cells if allowed
8390 int maxWidth
= rect
.width
;
8391 wxString value
= GetCellValue(row
, col
);
8392 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8395 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8396 if (maxWidth
< rect
.width
)
8397 maxWidth
= rect
.width
;
8400 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8401 if (rect
.x
+ maxWidth
> client_right
)
8402 maxWidth
= client_right
- rect
.x
;
8404 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8406 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8407 // may have changed earlier
8408 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8411 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8413 // looks weird going over a multicell
8414 if (m_table
->IsEmptyCell( row
, i
) &&
8415 (rect
.width
< maxWidth
) && (c_rows
== 1))
8417 rect
.width
+= GetColWidth( i
);
8423 if (rect
.GetRight() > client_right
)
8424 rect
.SetRight( client_right
- 1 );
8427 editor
->SetCellAttr( attr
);
8428 editor
->SetSize( rect
);
8430 editor
->GetControl()->Move(
8431 editor
->GetControl()->GetPosition().x
+ nXMove
,
8432 editor
->GetControl()->GetPosition().y
);
8433 editor
->Show( true, attr
);
8435 // recalc dimensions in case we need to
8436 // expand the scrolled window to account for editor
8439 editor
->BeginEdit(row
, col
, this);
8440 editor
->SetCellAttr(NULL
);
8448 void wxGrid::HideCellEditControl()
8450 if ( IsCellEditControlEnabled() )
8452 int row
= m_currentCellCoords
.GetRow();
8453 int col
= m_currentCellCoords
.GetCol();
8455 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8456 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8457 editor
->Show( false );
8461 m_gridWin
->SetFocus();
8463 // refresh whole row to the right
8464 wxRect
rect( CellToRect(row
, col
) );
8465 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8466 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8469 // ensure that the pixels under the focus ring get refreshed as well
8470 rect
.Inflate(10, 10);
8473 m_gridWin
->Refresh( false, &rect
);
8477 void wxGrid::SaveEditControlValue()
8479 if ( IsCellEditControlEnabled() )
8481 int row
= m_currentCellCoords
.GetRow();
8482 int col
= m_currentCellCoords
.GetCol();
8484 wxString oldval
= GetCellValue(row
, col
);
8486 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8487 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8488 bool changed
= editor
->EndEdit(row
, col
, this);
8495 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8496 m_currentCellCoords
.GetRow(),
8497 m_currentCellCoords
.GetCol() ) < 0 )
8499 // Event has been vetoed, set the data back.
8500 SetCellValue(row
, col
, oldval
);
8507 // ------ Grid location functions
8508 // Note that all of these functions work with the logical coordinates of
8509 // grid cells and labels so you will need to convert from device
8510 // coordinates for mouse events etc.
8513 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8515 int row
= YToRow(y
);
8516 int col
= XToCol(x
);
8518 if ( row
== -1 || col
== -1 )
8520 coords
= wxGridNoCellCoords
;
8524 coords
.Set( row
, col
);
8528 // Internal Helper function for computing row or column from some
8529 // (unscrolled) coordinate value, using either
8530 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8531 // of m_rowBottoms/m_ColRights to speed up the search!
8533 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8534 const wxArrayInt
& BorderArray
, int nMax
,
8538 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8543 size_t i_max
= coord
/ defaultDist
,
8546 if (BorderArray
.IsEmpty())
8548 if ((int) i_max
< nMax
)
8550 return clipToMinMax
? nMax
- 1 : -1;
8553 if ( i_max
>= BorderArray
.GetCount())
8555 i_max
= BorderArray
.GetCount() - 1;
8559 if ( coord
>= BorderArray
[i_max
])
8563 i_max
= coord
/ minDist
;
8565 i_max
= BorderArray
.GetCount() - 1;
8568 if ( i_max
>= BorderArray
.GetCount())
8569 i_max
= BorderArray
.GetCount() - 1;
8572 if ( coord
>= BorderArray
[i_max
])
8573 return clipToMinMax
? (int)i_max
: -1;
8574 if ( coord
< BorderArray
[0] )
8577 while ( i_max
- i_min
> 0 )
8579 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8580 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8581 if (coord
>= BorderArray
[ i_max
- 1])
8585 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8586 if (coord
< BorderArray
[median
])
8595 int wxGrid::YToRow( int y
) const
8597 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8598 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8601 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8604 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8606 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8608 int maxPos
= x
/ m_defaultColWidth
;
8611 if (m_colRights
.IsEmpty())
8613 if(maxPos
< m_numCols
)
8614 return GetColAt( maxPos
);
8615 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8618 if ( maxPos
>= m_numCols
)
8619 maxPos
= m_numCols
- 1;
8622 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8625 if (m_minAcceptableColWidth
)
8626 maxPos
= x
/ m_minAcceptableColWidth
;
8628 maxPos
= m_numCols
- 1;
8630 if ( maxPos
>= m_numCols
)
8631 maxPos
= m_numCols
- 1;
8634 //X is beyond the last column
8635 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8636 return clipToMinMax
? GetColAt( maxPos
) : -1;
8638 //X is before the first column
8639 if ( x
< m_colRights
[GetColAt( 0 )] )
8640 return GetColAt( 0 );
8642 //Perform a binary search
8643 while ( maxPos
- minPos
> 0 )
8645 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8646 0, _T("wxGrid: internal error in XToCol"));
8648 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8649 return GetColAt( maxPos
);
8652 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8653 if (x
< m_colRights
[GetColAt( median
)])
8658 return GetColAt( maxPos
);
8661 // return the row number that that the y coord is near
8662 // the edge of, or -1 if not near an edge.
8663 // coords can only possibly be near an edge if
8664 // (a) the row/column is large enough to still allow for an "inner" area
8665 // that is _not_ nead the edge (i.e., if the height/width is smaller
8666 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8669 // (b) resizing rows/columns (the thing for which edge detection is
8670 // relevant at all) is enabled.
8672 int wxGrid::YToEdgeOfRow( int y
) const
8675 i
= internalYToRow(y
);
8677 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8679 // We know that we are in row i, test whether we are
8680 // close enough to lower or upper border, respectively.
8681 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8683 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8690 // return the col number that that the x coord is near the edge of, or
8691 // -1 if not near an edge
8692 // See comment at YToEdgeOfRow for conditions on edge detection.
8694 int wxGrid::XToEdgeOfCol( int x
) const
8697 i
= internalXToCol(x
);
8699 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8701 // We know that we are in column i; test whether we are
8702 // close enough to right or left border, respectively.
8703 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8705 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8712 wxRect
wxGrid::CellToRect( int row
, int col
) const
8714 wxRect
rect( -1, -1, -1, -1 );
8716 if ( row
>= 0 && row
< m_numRows
&&
8717 col
>= 0 && col
< m_numCols
)
8719 int i
, cell_rows
, cell_cols
;
8720 rect
.width
= rect
.height
= 0;
8721 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8722 // if negative then find multicell owner
8727 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8729 rect
.x
= GetColLeft(col
);
8730 rect
.y
= GetRowTop(row
);
8731 for (i
=col
; i
< col
+ cell_cols
; i
++)
8732 rect
.width
+= GetColWidth(i
);
8733 for (i
=row
; i
< row
+ cell_rows
; i
++)
8734 rect
.height
+= GetRowHeight(i
);
8737 // if grid lines are enabled, then the area of the cell is a bit smaller
8738 if (m_gridLinesEnabled
)
8747 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8749 // get the cell rectangle in logical coords
8751 wxRect
r( CellToRect( row
, col
) );
8753 // convert to device coords
8755 int left
, top
, right
, bottom
;
8756 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8757 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8759 // check against the client area of the grid window
8761 m_gridWin
->GetClientSize( &cw
, &ch
);
8763 if ( wholeCellVisible
)
8765 // is the cell wholly visible ?
8766 return ( left
>= 0 && right
<= cw
&&
8767 top
>= 0 && bottom
<= ch
);
8771 // is the cell partly visible ?
8773 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8774 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8778 // make the specified cell location visible by doing a minimal amount
8781 void wxGrid::MakeCellVisible( int row
, int col
)
8784 int xpos
= -1, ypos
= -1;
8786 if ( row
>= 0 && row
< m_numRows
&&
8787 col
>= 0 && col
< m_numCols
)
8789 // get the cell rectangle in logical coords
8790 wxRect
r( CellToRect( row
, col
) );
8792 // convert to device coords
8793 int left
, top
, right
, bottom
;
8794 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8795 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8798 m_gridWin
->GetClientSize( &cw
, &ch
);
8804 else if ( bottom
> ch
)
8806 int h
= r
.GetHeight();
8808 for ( i
= row
- 1; i
>= 0; i
-- )
8810 int rowHeight
= GetRowHeight(i
);
8811 if ( h
+ rowHeight
> ch
)
8818 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8819 // have rounding errors (this is important, because if we do,
8820 // we might not scroll at all and some cells won't be redrawn)
8822 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8823 // so just add a full scroll unit...
8824 ypos
+= m_scrollLineY
;
8827 // special handling for wide cells - show always left part of the cell!
8828 // Otherwise, e.g. when stepping from row to row, it would jump between
8829 // left and right part of the cell on every step!
8831 if ( left
< 0 || (right
- left
) >= cw
)
8835 else if ( right
> cw
)
8837 // position the view so that the cell is on the right
8839 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8840 xpos
= x0
+ (right
- cw
);
8842 // see comment for ypos above
8843 xpos
+= m_scrollLineX
;
8846 if ( xpos
!= -1 || ypos
!= -1 )
8849 xpos
/= m_scrollLineX
;
8851 ypos
/= m_scrollLineY
;
8852 Scroll( xpos
, ypos
);
8859 // ------ Grid cursor movement functions
8862 bool wxGrid::MoveCursorUp( bool expandSelection
)
8864 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8865 m_currentCellCoords
.GetRow() >= 0 )
8867 if ( expandSelection
)
8869 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8870 m_selectingKeyboard
= m_currentCellCoords
;
8871 if ( m_selectingKeyboard
.GetRow() > 0 )
8873 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8874 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8875 m_selectingKeyboard
.GetCol() );
8876 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8879 else if ( m_currentCellCoords
.GetRow() > 0 )
8881 int row
= m_currentCellCoords
.GetRow() - 1;
8882 int col
= m_currentCellCoords
.GetCol();
8884 MakeCellVisible( row
, col
);
8885 SetCurrentCell( row
, col
);
8896 bool wxGrid::MoveCursorDown( bool expandSelection
)
8898 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8899 m_currentCellCoords
.GetRow() < m_numRows
)
8901 if ( expandSelection
)
8903 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8904 m_selectingKeyboard
= m_currentCellCoords
;
8905 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8907 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8908 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8909 m_selectingKeyboard
.GetCol() );
8910 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8913 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8915 int row
= m_currentCellCoords
.GetRow() + 1;
8916 int col
= m_currentCellCoords
.GetCol();
8918 MakeCellVisible( row
, col
);
8919 SetCurrentCell( row
, col
);
8930 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8932 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8933 m_currentCellCoords
.GetCol() >= 0 )
8935 if ( expandSelection
)
8937 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8938 m_selectingKeyboard
= m_currentCellCoords
;
8939 if ( m_selectingKeyboard
.GetCol() > 0 )
8941 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8942 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8943 m_selectingKeyboard
.GetCol() );
8944 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8947 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8949 int row
= m_currentCellCoords
.GetRow();
8950 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8953 MakeCellVisible( row
, col
);
8954 SetCurrentCell( row
, col
);
8965 bool wxGrid::MoveCursorRight( bool expandSelection
)
8967 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8968 m_currentCellCoords
.GetCol() < m_numCols
)
8970 if ( expandSelection
)
8972 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8973 m_selectingKeyboard
= m_currentCellCoords
;
8974 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8976 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8977 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8978 m_selectingKeyboard
.GetCol() );
8979 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8982 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8984 int row
= m_currentCellCoords
.GetRow();
8985 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8988 MakeCellVisible( row
, col
);
8989 SetCurrentCell( row
, col
);
9000 bool wxGrid::MovePageUp()
9002 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9005 int row
= m_currentCellCoords
.GetRow();
9009 m_gridWin
->GetClientSize( &cw
, &ch
);
9011 int y
= GetRowTop(row
);
9012 int newRow
= internalYToRow( y
- ch
+ 1 );
9014 if ( newRow
== row
)
9016 // row > 0, so newRow can never be less than 0 here.
9020 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9021 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9029 bool wxGrid::MovePageDown()
9031 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9034 int row
= m_currentCellCoords
.GetRow();
9035 if ( (row
+ 1) < m_numRows
)
9038 m_gridWin
->GetClientSize( &cw
, &ch
);
9040 int y
= GetRowTop(row
);
9041 int newRow
= internalYToRow( y
+ ch
);
9042 if ( newRow
== row
)
9044 // row < m_numRows, so newRow can't overflow here.
9048 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9049 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9057 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9060 m_currentCellCoords
!= wxGridNoCellCoords
&&
9061 m_currentCellCoords
.GetRow() > 0 )
9063 int row
= m_currentCellCoords
.GetRow();
9064 int col
= m_currentCellCoords
.GetCol();
9066 if ( m_table
->IsEmptyCell(row
, col
) )
9068 // starting in an empty cell: find the next block of
9074 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9078 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9080 // starting at the top of a block: find the next block
9086 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9092 // starting within a block: find the top of the block
9097 if ( m_table
->IsEmptyCell(row
, col
) )
9105 MakeCellVisible( row
, col
);
9106 if ( expandSelection
)
9108 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9109 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9114 SetCurrentCell( row
, col
);
9123 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9126 m_currentCellCoords
!= wxGridNoCellCoords
&&
9127 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9129 int row
= m_currentCellCoords
.GetRow();
9130 int col
= m_currentCellCoords
.GetCol();
9132 if ( m_table
->IsEmptyCell(row
, col
) )
9134 // starting in an empty cell: find the next block of
9137 while ( row
< m_numRows
- 1 )
9140 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9144 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9146 // starting at the bottom of a block: find the next block
9149 while ( row
< m_numRows
- 1 )
9152 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9158 // starting within a block: find the bottom of the block
9160 while ( row
< m_numRows
- 1 )
9163 if ( m_table
->IsEmptyCell(row
, col
) )
9171 MakeCellVisible( row
, col
);
9172 if ( expandSelection
)
9174 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9175 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9180 SetCurrentCell( row
, col
);
9189 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9192 m_currentCellCoords
!= wxGridNoCellCoords
&&
9193 m_currentCellCoords
.GetCol() > 0 )
9195 int row
= m_currentCellCoords
.GetRow();
9196 int col
= m_currentCellCoords
.GetCol();
9198 if ( m_table
->IsEmptyCell(row
, col
) )
9200 // starting in an empty cell: find the next block of
9206 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9210 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9212 // starting at the left of a block: find the next block
9218 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9224 // starting within a block: find the left of the block
9229 if ( m_table
->IsEmptyCell(row
, col
) )
9237 MakeCellVisible( row
, col
);
9238 if ( expandSelection
)
9240 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9241 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9246 SetCurrentCell( row
, col
);
9255 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9258 m_currentCellCoords
!= wxGridNoCellCoords
&&
9259 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9261 int row
= m_currentCellCoords
.GetRow();
9262 int col
= m_currentCellCoords
.GetCol();
9264 if ( m_table
->IsEmptyCell(row
, col
) )
9266 // starting in an empty cell: find the next block of
9269 while ( col
< m_numCols
- 1 )
9272 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9276 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9278 // starting at the right of a block: find the next block
9281 while ( col
< m_numCols
- 1 )
9284 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9290 // starting within a block: find the right of the block
9292 while ( col
< m_numCols
- 1 )
9295 if ( m_table
->IsEmptyCell(row
, col
) )
9303 MakeCellVisible( row
, col
);
9304 if ( expandSelection
)
9306 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9307 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9312 SetCurrentCell( row
, col
);
9322 // ------ Label values and formatting
9325 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9328 *horiz
= m_rowLabelHorizAlign
;
9330 *vert
= m_rowLabelVertAlign
;
9333 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9336 *horiz
= m_colLabelHorizAlign
;
9338 *vert
= m_colLabelVertAlign
;
9341 int wxGrid::GetColLabelTextOrientation() const
9343 return m_colLabelTextOrientation
;
9346 wxString
wxGrid::GetRowLabelValue( int row
) const
9350 return m_table
->GetRowLabelValue( row
);
9360 wxString
wxGrid::GetColLabelValue( int col
) const
9364 return m_table
->GetColLabelValue( col
);
9374 void wxGrid::SetRowLabelSize( int width
)
9376 width
= wxMax( width
, 0 );
9377 if ( width
!= m_rowLabelWidth
)
9381 m_rowLabelWin
->Show( false );
9382 m_cornerLabelWin
->Show( false );
9384 else if ( m_rowLabelWidth
== 0 )
9386 m_rowLabelWin
->Show( true );
9387 if ( m_colLabelHeight
> 0 )
9388 m_cornerLabelWin
->Show( true );
9391 m_rowLabelWidth
= width
;
9393 wxScrolledWindow::Refresh( true );
9397 void wxGrid::SetColLabelSize( int height
)
9399 height
= wxMax( height
, 0 );
9400 if ( height
!= m_colLabelHeight
)
9404 m_colLabelWin
->Show( false );
9405 m_cornerLabelWin
->Show( false );
9407 else if ( m_colLabelHeight
== 0 )
9409 m_colLabelWin
->Show( true );
9410 if ( m_rowLabelWidth
> 0 )
9411 m_cornerLabelWin
->Show( true );
9414 m_colLabelHeight
= height
;
9416 wxScrolledWindow::Refresh( true );
9420 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9422 if ( m_labelBackgroundColour
!= colour
)
9424 m_labelBackgroundColour
= colour
;
9425 m_rowLabelWin
->SetBackgroundColour( colour
);
9426 m_colLabelWin
->SetBackgroundColour( colour
);
9427 m_cornerLabelWin
->SetBackgroundColour( colour
);
9429 if ( !GetBatchCount() )
9431 m_rowLabelWin
->Refresh();
9432 m_colLabelWin
->Refresh();
9433 m_cornerLabelWin
->Refresh();
9438 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9440 if ( m_labelTextColour
!= colour
)
9442 m_labelTextColour
= colour
;
9443 if ( !GetBatchCount() )
9445 m_rowLabelWin
->Refresh();
9446 m_colLabelWin
->Refresh();
9451 void wxGrid::SetLabelFont( const wxFont
& font
)
9454 if ( !GetBatchCount() )
9456 m_rowLabelWin
->Refresh();
9457 m_colLabelWin
->Refresh();
9461 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9463 // allow old (incorrect) defs to be used
9466 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9467 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9468 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9473 case wxTOP
: vert
= wxALIGN_TOP
; break;
9474 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9475 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9478 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9480 m_rowLabelHorizAlign
= horiz
;
9483 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9485 m_rowLabelVertAlign
= vert
;
9488 if ( !GetBatchCount() )
9490 m_rowLabelWin
->Refresh();
9494 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9496 // allow old (incorrect) defs to be used
9499 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9500 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9501 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9506 case wxTOP
: vert
= wxALIGN_TOP
; break;
9507 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9508 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9511 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9513 m_colLabelHorizAlign
= horiz
;
9516 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9518 m_colLabelVertAlign
= vert
;
9521 if ( !GetBatchCount() )
9523 m_colLabelWin
->Refresh();
9527 // Note: under MSW, the default column label font must be changed because it
9528 // does not support vertical printing
9530 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9531 // pGrid->SetLabelFont(font);
9532 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9534 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9536 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9537 m_colLabelTextOrientation
= textOrientation
;
9539 if ( !GetBatchCount() )
9540 m_colLabelWin
->Refresh();
9543 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9547 m_table
->SetRowLabelValue( row
, s
);
9548 if ( !GetBatchCount() )
9550 wxRect rect
= CellToRect( row
, 0 );
9551 if ( rect
.height
> 0 )
9553 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9555 rect
.width
= m_rowLabelWidth
;
9556 m_rowLabelWin
->Refresh( true, &rect
);
9562 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9566 m_table
->SetColLabelValue( col
, s
);
9567 if ( !GetBatchCount() )
9569 wxRect rect
= CellToRect( 0, col
);
9570 if ( rect
.width
> 0 )
9572 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9574 rect
.height
= m_colLabelHeight
;
9575 m_colLabelWin
->Refresh( true, &rect
);
9581 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9583 if ( m_gridLineColour
!= colour
)
9585 m_gridLineColour
= colour
;
9587 wxClientDC
dc( m_gridWin
);
9589 DrawAllGridLines( dc
, wxRegion() );
9593 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9595 if ( m_cellHighlightColour
!= colour
)
9597 m_cellHighlightColour
= colour
;
9599 wxClientDC
dc( m_gridWin
);
9601 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9602 DrawCellHighlight(dc
, attr
);
9607 void wxGrid::SetCellHighlightPenWidth(int width
)
9609 if (m_cellHighlightPenWidth
!= width
)
9611 m_cellHighlightPenWidth
= width
;
9613 // Just redrawing the cell highlight is not enough since that won't
9614 // make any visible change if the the thickness is getting smaller.
9615 int row
= m_currentCellCoords
.GetRow();
9616 int col
= m_currentCellCoords
.GetCol();
9617 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9620 wxRect rect
= CellToRect(row
, col
);
9621 m_gridWin
->Refresh(true, &rect
);
9625 void wxGrid::SetCellHighlightROPenWidth(int width
)
9627 if (m_cellHighlightROPenWidth
!= width
)
9629 m_cellHighlightROPenWidth
= width
;
9631 // Just redrawing the cell highlight is not enough since that won't
9632 // make any visible change if the the thickness is getting smaller.
9633 int row
= m_currentCellCoords
.GetRow();
9634 int col
= m_currentCellCoords
.GetCol();
9635 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9638 wxRect rect
= CellToRect(row
, col
);
9639 m_gridWin
->Refresh(true, &rect
);
9643 void wxGrid::EnableGridLines( bool enable
)
9645 if ( enable
!= m_gridLinesEnabled
)
9647 m_gridLinesEnabled
= enable
;
9649 if ( !GetBatchCount() )
9653 wxClientDC
dc( m_gridWin
);
9655 DrawAllGridLines( dc
, wxRegion() );
9659 m_gridWin
->Refresh();
9665 int wxGrid::GetDefaultRowSize() const
9667 return m_defaultRowHeight
;
9670 int wxGrid::GetRowSize( int row
) const
9672 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9674 return GetRowHeight(row
);
9677 int wxGrid::GetDefaultColSize() const
9679 return m_defaultColWidth
;
9682 int wxGrid::GetColSize( int col
) const
9684 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9686 return GetColWidth(col
);
9689 // ============================================================================
9690 // access to the grid attributes: each of them has a default value in the grid
9691 // itself and may be overidden on a per-cell basis
9692 // ============================================================================
9694 // ----------------------------------------------------------------------------
9695 // setting default attributes
9696 // ----------------------------------------------------------------------------
9698 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9700 m_defaultCellAttr
->SetBackgroundColour(col
);
9702 m_gridWin
->SetBackgroundColour(col
);
9706 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9708 m_defaultCellAttr
->SetTextColour(col
);
9711 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9713 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9716 void wxGrid::SetDefaultCellOverflow( bool allow
)
9718 m_defaultCellAttr
->SetOverflow(allow
);
9721 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9723 m_defaultCellAttr
->SetFont(font
);
9726 // For editors and renderers the type registry takes precedence over the
9727 // default attr, so we need to register the new editor/renderer for the string
9728 // data type in order to make setting a default editor/renderer appear to
9731 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9733 RegisterDataType(wxGRID_VALUE_STRING
,
9735 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9738 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9740 RegisterDataType(wxGRID_VALUE_STRING
,
9741 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9745 // ----------------------------------------------------------------------------
9746 // access to the default attributes
9747 // ----------------------------------------------------------------------------
9749 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9751 return m_defaultCellAttr
->GetBackgroundColour();
9754 wxColour
wxGrid::GetDefaultCellTextColour() const
9756 return m_defaultCellAttr
->GetTextColour();
9759 wxFont
wxGrid::GetDefaultCellFont() const
9761 return m_defaultCellAttr
->GetFont();
9764 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9766 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9769 bool wxGrid::GetDefaultCellOverflow() const
9771 return m_defaultCellAttr
->GetOverflow();
9774 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9776 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9779 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9781 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9784 // ----------------------------------------------------------------------------
9785 // access to cell attributes
9786 // ----------------------------------------------------------------------------
9788 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9790 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9791 wxColour colour
= attr
->GetBackgroundColour();
9797 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9799 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9800 wxColour colour
= attr
->GetTextColour();
9806 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9808 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9809 wxFont font
= attr
->GetFont();
9815 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9817 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9818 attr
->GetAlignment(horiz
, vert
);
9822 bool wxGrid::GetCellOverflow( int row
, int col
) const
9824 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9825 bool allow
= attr
->GetOverflow();
9831 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9833 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9834 attr
->GetSize( num_rows
, num_cols
);
9838 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9840 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9841 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9847 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9849 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9850 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9856 bool wxGrid::IsReadOnly(int row
, int col
) const
9858 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9859 bool isReadOnly
= attr
->IsReadOnly();
9865 // ----------------------------------------------------------------------------
9866 // attribute support: cache, automatic provider creation, ...
9867 // ----------------------------------------------------------------------------
9869 bool wxGrid::CanHaveAttributes() const
9876 return m_table
->CanHaveAttributes();
9879 void wxGrid::ClearAttrCache()
9881 if ( m_attrCache
.row
!= -1 )
9883 wxSafeDecRef(m_attrCache
.attr
);
9884 m_attrCache
.attr
= NULL
;
9885 m_attrCache
.row
= -1;
9889 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9893 wxGrid
*self
= (wxGrid
*)this; // const_cast
9895 self
->ClearAttrCache();
9896 self
->m_attrCache
.row
= row
;
9897 self
->m_attrCache
.col
= col
;
9898 self
->m_attrCache
.attr
= attr
;
9903 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9905 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9907 *attr
= m_attrCache
.attr
;
9908 wxSafeIncRef(m_attrCache
.attr
);
9910 #ifdef DEBUG_ATTR_CACHE
9911 gs_nAttrCacheHits
++;
9918 #ifdef DEBUG_ATTR_CACHE
9919 gs_nAttrCacheMisses
++;
9926 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9928 wxGridCellAttr
*attr
= NULL
;
9929 // Additional test to avoid looking at the cache e.g. for
9930 // wxNoCellCoords, as this will confuse memory management.
9933 if ( !LookupAttr(row
, col
, &attr
) )
9935 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9936 : (wxGridCellAttr
*)NULL
;
9937 CacheAttr(row
, col
, attr
);
9943 attr
->SetDefAttr(m_defaultCellAttr
);
9947 attr
= m_defaultCellAttr
;
9954 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9956 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9957 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9959 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9960 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9962 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9965 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9967 // artificially inc the ref count to match DecRef() in caller
9969 m_table
->SetAttr(attr
, row
, col
);
9975 // ----------------------------------------------------------------------------
9976 // setting column attributes (wrappers around SetColAttr)
9977 // ----------------------------------------------------------------------------
9979 void wxGrid::SetColFormatBool(int col
)
9981 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9984 void wxGrid::SetColFormatNumber(int col
)
9986 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9989 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9991 wxString typeName
= wxGRID_VALUE_FLOAT
;
9992 if ( (width
!= -1) || (precision
!= -1) )
9994 typeName
<< _T(':') << width
<< _T(',') << precision
;
9997 SetColFormatCustom(col
, typeName
);
10000 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10002 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10004 attr
= new wxGridCellAttr
;
10005 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10006 attr
->SetRenderer(renderer
);
10008 SetColAttr(col
, attr
);
10012 // ----------------------------------------------------------------------------
10013 // setting cell attributes: this is forwarded to the table
10014 // ----------------------------------------------------------------------------
10016 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10018 if ( CanHaveAttributes() )
10020 m_table
->SetAttr(attr
, row
, col
);
10025 wxSafeDecRef(attr
);
10029 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10031 if ( CanHaveAttributes() )
10033 m_table
->SetRowAttr(attr
, row
);
10038 wxSafeDecRef(attr
);
10042 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10044 if ( CanHaveAttributes() )
10046 m_table
->SetColAttr(attr
, col
);
10051 wxSafeDecRef(attr
);
10055 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10057 if ( CanHaveAttributes() )
10059 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10060 attr
->SetBackgroundColour(colour
);
10065 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10067 if ( CanHaveAttributes() )
10069 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10070 attr
->SetTextColour(colour
);
10075 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10077 if ( CanHaveAttributes() )
10079 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10080 attr
->SetFont(font
);
10085 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10087 if ( CanHaveAttributes() )
10089 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10090 attr
->SetAlignment(horiz
, vert
);
10095 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10097 if ( CanHaveAttributes() )
10099 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10100 attr
->SetOverflow(allow
);
10105 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10107 if ( CanHaveAttributes() )
10109 int cell_rows
, cell_cols
;
10111 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10112 attr
->GetSize(&cell_rows
, &cell_cols
);
10113 attr
->SetSize(num_rows
, num_cols
);
10116 // Cannot set the size of a cell to 0 or negative values
10117 // While it is perfectly legal to do that, this function cannot
10118 // handle all the possibilies, do it by hand by getting the CellAttr.
10119 // You can only set the size of a cell to 1,1 or greater with this fn
10120 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10121 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10122 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10123 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10125 // if this was already a multicell then "turn off" the other cells first
10126 if ((cell_rows
> 1) || (cell_rows
> 1))
10129 for (j
=row
; j
< row
+ cell_rows
; j
++)
10131 for (i
=col
; i
< col
+ cell_cols
; i
++)
10133 if ((i
!= col
) || (j
!= row
))
10135 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10136 attr_stub
->SetSize( 1, 1 );
10137 attr_stub
->DecRef();
10143 // mark the cells that will be covered by this cell to
10144 // negative or zero values to point back at this cell
10145 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10148 for (j
=row
; j
< row
+ num_rows
; j
++)
10150 for (i
=col
; i
< col
+ num_cols
; i
++)
10152 if ((i
!= col
) || (j
!= row
))
10154 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10155 attr_stub
->SetSize( row
- j
, col
- i
);
10156 attr_stub
->DecRef();
10164 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10166 if ( CanHaveAttributes() )
10168 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10169 attr
->SetRenderer(renderer
);
10174 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10176 if ( CanHaveAttributes() )
10178 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10179 attr
->SetEditor(editor
);
10184 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10186 if ( CanHaveAttributes() )
10188 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10189 attr
->SetReadOnly(isReadOnly
);
10194 // ----------------------------------------------------------------------------
10195 // Data type registration
10196 // ----------------------------------------------------------------------------
10198 void wxGrid::RegisterDataType(const wxString
& typeName
,
10199 wxGridCellRenderer
* renderer
,
10200 wxGridCellEditor
* editor
)
10202 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10206 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10208 wxString typeName
= m_table
->GetTypeName(row
, col
);
10209 return GetDefaultEditorForType(typeName
);
10212 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10214 wxString typeName
= m_table
->GetTypeName(row
, col
);
10215 return GetDefaultRendererForType(typeName
);
10218 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10220 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10221 if ( index
== wxNOT_FOUND
)
10225 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10226 wxFAIL_MSG(errStr
.c_str());
10231 return m_typeRegistry
->GetEditor(index
);
10234 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10236 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10237 if ( index
== wxNOT_FOUND
)
10241 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10242 wxFAIL_MSG(errStr
.c_str());
10247 return m_typeRegistry
->GetRenderer(index
);
10250 // ----------------------------------------------------------------------------
10252 // ----------------------------------------------------------------------------
10254 void wxGrid::EnableDragRowSize( bool enable
)
10256 m_canDragRowSize
= enable
;
10259 void wxGrid::EnableDragColSize( bool enable
)
10261 m_canDragColSize
= enable
;
10264 void wxGrid::EnableDragGridSize( bool enable
)
10266 m_canDragGridSize
= enable
;
10269 void wxGrid::EnableDragCell( bool enable
)
10271 m_canDragCell
= enable
;
10274 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10276 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10278 if ( resizeExistingRows
)
10280 // since we are resizing all rows to the default row size,
10281 // we can simply clear the row heights and row bottoms
10282 // arrays (which also allows us to take advantage of
10283 // some speed optimisations)
10284 m_rowHeights
.Empty();
10285 m_rowBottoms
.Empty();
10286 if ( !GetBatchCount() )
10291 void wxGrid::SetRowSize( int row
, int height
)
10293 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10295 // See comment in SetColSize
10296 if ( height
< GetRowMinimalAcceptableHeight())
10299 if ( m_rowHeights
.IsEmpty() )
10301 // need to really create the array
10305 int h
= wxMax( 0, height
);
10306 int diff
= h
- m_rowHeights
[row
];
10308 m_rowHeights
[row
] = h
;
10309 for ( int i
= row
; i
< m_numRows
; i
++ )
10311 m_rowBottoms
[i
] += diff
;
10314 if ( !GetBatchCount() )
10318 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10320 // we dont allow zero default column width
10321 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10323 if ( resizeExistingCols
)
10325 // since we are resizing all columns to the default column size,
10326 // we can simply clear the col widths and col rights
10327 // arrays (which also allows us to take advantage of
10328 // some speed optimisations)
10329 m_colWidths
.Empty();
10330 m_colRights
.Empty();
10331 if ( !GetBatchCount() )
10336 void wxGrid::SetColSize( int col
, int width
)
10338 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10340 // should we check that it's bigger than GetColMinimalWidth(col) here?
10342 // No, because it is reasonable to assume the library user know's
10343 // what he is doing. However we should test against the weaker
10344 // constraint of minimalAcceptableWidth, as this breaks rendering
10346 // This test then fixes sf.net bug #645734
10348 if ( width
< GetColMinimalAcceptableWidth() )
10351 if ( m_colWidths
.IsEmpty() )
10353 // need to really create the array
10357 // if < 0 then calculate new width from label
10361 wxArrayString lines
;
10362 wxClientDC
dc(m_colLabelWin
);
10363 dc
.SetFont(GetLabelFont());
10364 StringToLines(GetColLabelValue(col
), lines
);
10365 GetTextBoxSize(dc
, lines
, &w
, &h
);
10369 int w
= wxMax( 0, width
);
10370 int diff
= w
- m_colWidths
[col
];
10371 m_colWidths
[col
] = w
;
10373 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10375 m_colRights
[GetColAt(colPos
)] += diff
;
10378 if ( !GetBatchCount() )
10382 void wxGrid::SetColMinimalWidth( int col
, int width
)
10384 if (width
> GetColMinimalAcceptableWidth())
10386 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10387 m_colMinWidths
[key
] = width
;
10391 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10393 if (width
> GetRowMinimalAcceptableHeight())
10395 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10396 m_rowMinHeights
[key
] = width
;
10400 int wxGrid::GetColMinimalWidth(int col
) const
10402 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10403 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10405 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10408 int wxGrid::GetRowMinimalHeight(int row
) const
10410 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10411 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10413 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10416 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10418 // We do allow a width of 0 since this gives us
10419 // an easy way to temporarily hiding columns.
10421 m_minAcceptableColWidth
= width
;
10424 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10426 // We do allow a height of 0 since this gives us
10427 // an easy way to temporarily hiding rows.
10429 m_minAcceptableRowHeight
= height
;
10432 int wxGrid::GetColMinimalAcceptableWidth() const
10434 return m_minAcceptableColWidth
;
10437 int wxGrid::GetRowMinimalAcceptableHeight() const
10439 return m_minAcceptableRowHeight
;
10442 // ----------------------------------------------------------------------------
10444 // ----------------------------------------------------------------------------
10446 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
10448 wxClientDC
dc(m_gridWin
);
10450 // cancel editing of cell
10451 HideCellEditControl();
10452 SaveEditControlValue();
10454 // init both of them to avoid compiler warnings, even if we only need one
10462 wxCoord extent
, extentMax
= 0;
10463 int max
= column
? m_numRows
: m_numCols
;
10464 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10471 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10472 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10475 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10476 extent
= column
? size
.x
: size
.y
;
10477 if ( extent
> extentMax
)
10478 extentMax
= extent
;
10480 renderer
->DecRef();
10486 // now also compare with the column label extent
10488 dc
.SetFont( GetLabelFont() );
10492 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10493 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10497 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10499 extent
= column
? w
: h
;
10500 if ( extent
> extentMax
)
10501 extentMax
= extent
;
10505 // empty column - give default extent (notice that if extentMax is less
10506 // than default extent but != 0, it's OK)
10507 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10512 // leave some space around text
10520 SetColSize( col
, extentMax
);
10521 if ( !GetBatchCount() )
10524 m_gridWin
->GetClientSize( &cw
, &ch
);
10525 wxRect
rect ( CellToRect( 0, col
) );
10527 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10528 rect
.width
= cw
- rect
.x
;
10529 rect
.height
= m_colLabelHeight
;
10530 m_colLabelWin
->Refresh( true, &rect
);
10535 SetRowSize(row
, extentMax
);
10536 if ( !GetBatchCount() )
10539 m_gridWin
->GetClientSize( &cw
, &ch
);
10540 wxRect
rect( CellToRect( row
, 0 ) );
10542 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10543 rect
.width
= m_rowLabelWidth
;
10544 rect
.height
= ch
- rect
.y
;
10545 m_rowLabelWin
->Refresh( true, &rect
);
10552 SetColMinimalWidth(col
, extentMax
);
10554 SetRowMinimalHeight(row
, extentMax
);
10558 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10560 int width
= m_rowLabelWidth
;
10565 for ( int col
= 0; col
< m_numCols
; col
++ )
10568 AutoSizeColumn(col
, setAsMin
);
10570 width
+= GetColWidth(col
);
10579 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10581 int height
= m_colLabelHeight
;
10586 for ( int row
= 0; row
< m_numRows
; row
++ )
10589 AutoSizeRow(row
, setAsMin
);
10591 height
+= GetRowHeight(row
);
10600 void wxGrid::AutoSize()
10604 // we need to round up the size of the scrollable area to a multiple of
10605 // scroll step to ensure that we don't get the scrollbars when we're sized
10606 // exactly to fit our contents
10607 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10608 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10609 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10610 GetScrollY(size
.y
) * GetScrollLineY());
10612 // distribute the extra space between the columns/rows to avoid having
10613 // extra white space
10614 wxCoord diff
= sizeFit
.x
- size
.x
;
10615 if ( diff
&& m_numCols
)
10617 // try to resize the columns uniformly
10618 wxCoord diffPerCol
= diff
/ m_numCols
;
10621 for ( int col
= 0; col
< m_numCols
; col
++ )
10623 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10627 // add remaining amount to the last columns
10628 diff
-= diffPerCol
* m_numCols
;
10631 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10633 SetColSize(col
, GetColWidth(col
) + 1);
10639 diff
= sizeFit
.y
- size
.y
;
10640 if ( diff
&& m_numRows
)
10642 // try to resize the columns uniformly
10643 wxCoord diffPerRow
= diff
/ m_numRows
;
10646 for ( int row
= 0; row
< m_numRows
; row
++ )
10648 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10652 // add remaining amount to the last rows
10653 diff
-= diffPerRow
* m_numRows
;
10656 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10658 SetRowSize(row
, GetRowHeight(row
) + 1);
10663 // we know that we're not going to have scrollbars so disable them now to
10664 // avoid trouble in SetClientSize() which can otherwise set the correct
10665 // client size but also leave space for (not needed any more) scrollbars
10666 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10667 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10672 void wxGrid::AutoSizeRowLabelSize( int row
)
10674 wxArrayString lines
;
10677 // Hide the edit control, so it
10678 // won't interfere with drag-shrinking.
10679 if ( IsCellEditControlShown() )
10681 HideCellEditControl();
10682 SaveEditControlValue();
10685 // autosize row height depending on label text
10686 StringToLines( GetRowLabelValue( row
), lines
);
10687 wxClientDC
dc( m_rowLabelWin
);
10688 GetTextBoxSize( dc
, lines
, &w
, &h
);
10689 if ( h
< m_defaultRowHeight
)
10690 h
= m_defaultRowHeight
;
10691 SetRowSize(row
, h
);
10695 void wxGrid::AutoSizeColLabelSize( int col
)
10697 wxArrayString lines
;
10700 // Hide the edit control, so it
10701 // won't interfere with drag-shrinking.
10702 if ( IsCellEditControlShown() )
10704 HideCellEditControl();
10705 SaveEditControlValue();
10708 // autosize column width depending on label text
10709 StringToLines( GetColLabelValue( col
), lines
);
10710 wxClientDC
dc( m_colLabelWin
);
10711 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10712 GetTextBoxSize( dc
, lines
, &w
, &h
);
10714 GetTextBoxSize( dc
, lines
, &h
, &w
);
10715 if ( w
< m_defaultColWidth
)
10716 w
= m_defaultColWidth
;
10717 SetColSize(col
, w
);
10721 wxSize
wxGrid::DoGetBestSize() const
10723 wxGrid
*self
= (wxGrid
*)this; // const_cast
10725 // we do the same as in AutoSize() here with the exception that we don't
10726 // change the column/row sizes, only calculate them
10727 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10728 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10729 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10730 GetScrollY(size
.y
) * GetScrollLineY());
10732 // NOTE: This size should be cached, but first we need to add calls to
10733 // InvalidateBestSize everywhere that could change the results of this
10735 // CacheBestSize(size);
10737 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10738 + GetWindowBorderSize();
10746 wxPen
& wxGrid::GetDividerPen() const
10751 // ----------------------------------------------------------------------------
10752 // cell value accessor functions
10753 // ----------------------------------------------------------------------------
10755 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10759 m_table
->SetValue( row
, col
, s
);
10760 if ( !GetBatchCount() )
10763 wxRect
rect( CellToRect( row
, col
) );
10765 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10766 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10767 m_gridWin
->Refresh( false, &rect
);
10770 if ( m_currentCellCoords
.GetRow() == row
&&
10771 m_currentCellCoords
.GetCol() == col
&&
10772 IsCellEditControlShown())
10773 // Note: If we are using IsCellEditControlEnabled,
10774 // this interacts badly with calling SetCellValue from
10775 // an EVT_GRID_CELL_CHANGE handler.
10777 HideCellEditControl();
10778 ShowCellEditControl(); // will reread data from table
10783 // ----------------------------------------------------------------------------
10784 // block, row and column selection
10785 // ----------------------------------------------------------------------------
10787 void wxGrid::SelectRow( int row
, bool addToSelected
)
10789 if ( IsSelection() && !addToSelected
)
10793 m_selection
->SelectRow( row
, false, addToSelected
);
10796 void wxGrid::SelectCol( int col
, bool addToSelected
)
10798 if ( IsSelection() && !addToSelected
)
10802 m_selection
->SelectCol( col
, false, addToSelected
);
10805 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10806 bool addToSelected
)
10808 if ( IsSelection() && !addToSelected
)
10812 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10813 false, addToSelected
);
10816 void wxGrid::SelectAll()
10818 if ( m_numRows
> 0 && m_numCols
> 0 )
10821 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10825 // ----------------------------------------------------------------------------
10826 // cell, row and col deselection
10827 // ----------------------------------------------------------------------------
10829 void wxGrid::DeselectRow( int row
)
10831 if ( !m_selection
)
10834 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10836 if ( m_selection
->IsInSelection(row
, 0 ) )
10837 m_selection
->ToggleCellSelection(row
, 0);
10841 int nCols
= GetNumberCols();
10842 for ( int i
= 0; i
< nCols
; i
++ )
10844 if ( m_selection
->IsInSelection(row
, i
) )
10845 m_selection
->ToggleCellSelection(row
, i
);
10850 void wxGrid::DeselectCol( int col
)
10852 if ( !m_selection
)
10855 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10857 if ( m_selection
->IsInSelection(0, col
) )
10858 m_selection
->ToggleCellSelection(0, col
);
10862 int nRows
= GetNumberRows();
10863 for ( int i
= 0; i
< nRows
; i
++ )
10865 if ( m_selection
->IsInSelection(i
, col
) )
10866 m_selection
->ToggleCellSelection(i
, col
);
10871 void wxGrid::DeselectCell( int row
, int col
)
10873 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10874 m_selection
->ToggleCellSelection(row
, col
);
10877 bool wxGrid::IsSelection() const
10879 return ( m_selection
&& (m_selection
->IsSelection() ||
10880 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10881 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10884 bool wxGrid::IsInSelection( int row
, int col
) const
10886 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10887 ( row
>= m_selectingTopLeft
.GetRow() &&
10888 col
>= m_selectingTopLeft
.GetCol() &&
10889 row
<= m_selectingBottomRight
.GetRow() &&
10890 col
<= m_selectingBottomRight
.GetCol() )) );
10893 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10897 wxGridCellCoordsArray a
;
10901 return m_selection
->m_cellSelection
;
10904 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10908 wxGridCellCoordsArray a
;
10912 return m_selection
->m_blockSelectionTopLeft
;
10915 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10919 wxGridCellCoordsArray a
;
10923 return m_selection
->m_blockSelectionBottomRight
;
10926 wxArrayInt
wxGrid::GetSelectedRows() const
10934 return m_selection
->m_rowSelection
;
10937 wxArrayInt
wxGrid::GetSelectedCols() const
10945 return m_selection
->m_colSelection
;
10948 void wxGrid::ClearSelection()
10950 m_selectingTopLeft
=
10951 m_selectingBottomRight
=
10952 m_selectingKeyboard
= wxGridNoCellCoords
;
10954 m_selection
->ClearSelection();
10957 // This function returns the rectangle that encloses the given block
10958 // in device coords clipped to the client size of the grid window.
10960 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10961 const wxGridCellCoords
&bottomRight
) const
10963 wxRect
rect( wxGridNoCellRect
);
10966 cellRect
= CellToRect( topLeft
);
10967 if ( cellRect
!= wxGridNoCellRect
)
10973 rect
= wxRect(0, 0, 0, 0);
10976 cellRect
= CellToRect( bottomRight
);
10977 if ( cellRect
!= wxGridNoCellRect
)
10983 return wxGridNoCellRect
;
10987 int left
= rect
.GetLeft();
10988 int top
= rect
.GetTop();
10989 int right
= rect
.GetRight();
10990 int bottom
= rect
.GetBottom();
10992 int leftCol
= topLeft
.GetCol();
10993 int topRow
= topLeft
.GetRow();
10994 int rightCol
= bottomRight
.GetCol();
10995 int bottomRow
= bottomRight
.GetRow();
11003 leftCol
= rightCol
;
11013 topRow
= bottomRow
;
11017 for ( j
= topRow
; j
<= bottomRow
; j
++ )
11019 for ( i
= leftCol
; i
<= rightCol
; i
++ )
11021 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
11023 cellRect
= CellToRect( j
, i
);
11025 if (cellRect
.x
< left
)
11027 if (cellRect
.y
< top
)
11029 if (cellRect
.x
+ cellRect
.width
> right
)
11030 right
= cellRect
.x
+ cellRect
.width
;
11031 if (cellRect
.y
+ cellRect
.height
> bottom
)
11032 bottom
= cellRect
.y
+ cellRect
.height
;
11036 i
= rightCol
; // jump over inner cells.
11041 // convert to scrolled coords
11043 CalcScrolledPosition( left
, top
, &left
, &top
);
11044 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11047 m_gridWin
->GetClientSize( &cw
, &ch
);
11049 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11050 return wxRect(0,0,0,0);
11052 rect
.SetLeft( wxMax(0, left
) );
11053 rect
.SetTop( wxMax(0, top
) );
11054 rect
.SetRight( wxMin(cw
, right
) );
11055 rect
.SetBottom( wxMin(ch
, bottom
) );
11060 // ----------------------------------------------------------------------------
11061 // grid event classes
11062 // ----------------------------------------------------------------------------
11064 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11066 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11067 int row
, int col
, int x
, int y
, bool sel
,
11068 bool control
, bool shift
, bool alt
, bool meta
)
11069 : wxNotifyEvent( type
, id
)
11076 m_control
= control
;
11081 SetEventObject(obj
);
11085 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11087 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11088 int rowOrCol
, int x
, int y
,
11089 bool control
, bool shift
, bool alt
, bool meta
)
11090 : wxNotifyEvent( type
, id
)
11092 m_rowOrCol
= rowOrCol
;
11095 m_control
= control
;
11100 SetEventObject(obj
);
11104 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11106 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11107 const wxGridCellCoords
& topLeft
,
11108 const wxGridCellCoords
& bottomRight
,
11109 bool sel
, bool control
,
11110 bool shift
, bool alt
, bool meta
)
11111 : wxNotifyEvent( type
, id
)
11113 m_topLeft
= topLeft
;
11114 m_bottomRight
= bottomRight
;
11116 m_control
= control
;
11121 SetEventObject(obj
);
11125 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11127 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11128 wxObject
* obj
, int row
,
11129 int col
, wxControl
* ctrl
)
11130 : wxCommandEvent(type
, id
)
11132 SetEventObject(obj
);
11138 #endif // wxUSE_GRID