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_
)
76 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
77 : coords(other
.coords
),
83 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
85 coords
= other
.coords
;
86 if (attr
!= other
.attr
)
95 void ChangeAttr(wxGridCellAttr
* new_attr
)
99 // "Delete" (i.e. DecRef) the old attribute.
102 // Take ownership of the new attribute, i.e. no IncRef.
106 ~wxGridCellWithAttr()
111 wxGridCellCoords coords
;
112 wxGridCellAttr
*attr
;
115 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
116 class WXDLLIMPEXP_ADV
);
118 #include "wx/arrimpl.cpp"
120 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
121 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
128 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
129 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
130 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
131 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
132 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
133 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
134 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
135 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
136 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
137 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 // common base class for various grid subwindows
151 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
154 wxGridSubwindow() { m_owner
= NULL
; }
155 wxGridSubwindow(wxGrid
*owner
,
159 int additionalStyle
= 0,
160 const wxString
& name
= wxPanelNameStr
)
161 : wxWindow(owner
, id
, pos
, size
,
162 wxBORDER_NONE
| additionalStyle
,
168 virtual bool AcceptsFocus() const { return false; }
170 wxGrid
*GetOwner() { return m_owner
; }
173 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
177 DECLARE_EVENT_TABLE()
178 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
181 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
184 wxGridRowLabelWindow() { }
185 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
186 const wxPoint
&pos
, const wxSize
&size
);
189 void OnPaint( wxPaintEvent
& event
);
190 void OnMouseEvent( wxMouseEvent
& event
);
191 void OnMouseWheel( wxMouseEvent
& event
);
193 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
194 DECLARE_EVENT_TABLE()
195 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
199 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
202 wxGridColLabelWindow() { }
203 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
204 const wxPoint
&pos
, const wxSize
&size
);
207 void OnPaint( wxPaintEvent
& event
);
208 void OnMouseEvent( wxMouseEvent
& event
);
209 void OnMouseWheel( wxMouseEvent
& event
);
211 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
212 DECLARE_EVENT_TABLE()
213 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
217 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
220 wxGridCornerLabelWindow() { }
221 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
222 const wxPoint
&pos
, const wxSize
&size
);
225 void OnMouseEvent( wxMouseEvent
& event
);
226 void OnMouseWheel( wxMouseEvent
& event
);
227 void OnPaint( wxPaintEvent
& event
);
229 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
230 DECLARE_EVENT_TABLE()
231 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
234 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
239 m_rowLabelWin
= NULL
;
240 m_colLabelWin
= NULL
;
243 wxGridWindow( wxGrid
*parent
,
244 wxGridRowLabelWindow
*rowLblWin
,
245 wxGridColLabelWindow
*colLblWin
,
246 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
248 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
250 virtual bool AcceptsFocus() const { return true; }
253 wxGridRowLabelWindow
*m_rowLabelWin
;
254 wxGridColLabelWindow
*m_colLabelWin
;
256 void OnPaint( wxPaintEvent
&event
);
257 void OnMouseWheel( wxMouseEvent
& event
);
258 void OnMouseEvent( wxMouseEvent
& event
);
259 void OnKeyDown( wxKeyEvent
& );
260 void OnKeyUp( wxKeyEvent
& );
261 void OnChar( wxKeyEvent
& );
262 void OnEraseBackground( wxEraseEvent
& );
263 void OnFocus( wxFocusEvent
& );
265 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
266 DECLARE_EVENT_TABLE()
267 DECLARE_NO_COPY_CLASS(wxGridWindow
)
271 class wxGridCellEditorEvtHandler
: public wxEvtHandler
274 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
281 void OnKillFocus(wxFocusEvent
& event
);
282 void OnKeyDown(wxKeyEvent
& event
);
283 void OnChar(wxKeyEvent
& event
);
285 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
289 wxGridCellEditor
*m_editor
;
291 // Work around the fact that a focus kill event can be sent to
292 // a combobox within a set focus event.
295 DECLARE_EVENT_TABLE()
296 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
297 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
301 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
303 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
304 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
305 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
306 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
310 // ----------------------------------------------------------------------------
311 // the internal data representation used by wxGridCellAttrProvider
312 // ----------------------------------------------------------------------------
314 // this class stores attributes set for cells
315 class WXDLLIMPEXP_ADV wxGridCellAttrData
318 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
319 wxGridCellAttr
*GetAttr(int row
, int col
) const;
320 void UpdateAttrRows( size_t pos
, int numRows
);
321 void UpdateAttrCols( size_t pos
, int numCols
);
324 // searches for the attr for given cell, returns wxNOT_FOUND if not found
325 int FindIndex(int row
, int col
) const;
327 wxGridCellWithAttrArray m_attrs
;
330 // this class stores attributes set for rows or columns
331 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
334 // empty ctor to suppress warnings
335 wxGridRowOrColAttrData() {}
336 ~wxGridRowOrColAttrData();
338 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
339 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
340 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
343 wxArrayInt m_rowsOrCols
;
344 wxArrayAttrs m_attrs
;
347 // NB: this is just a wrapper around 3 objects: one which stores cell
348 // attributes, and 2 others for row/col ones
349 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
352 wxGridCellAttrData m_cellAttrs
;
353 wxGridRowOrColAttrData m_rowAttrs
,
358 // ----------------------------------------------------------------------------
359 // data structures used for the data type registry
360 // ----------------------------------------------------------------------------
362 struct wxGridDataTypeInfo
364 wxGridDataTypeInfo(const wxString
& typeName
,
365 wxGridCellRenderer
* renderer
,
366 wxGridCellEditor
* editor
)
367 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
370 ~wxGridDataTypeInfo()
372 wxSafeDecRef(m_renderer
);
373 wxSafeDecRef(m_editor
);
377 wxGridCellRenderer
* m_renderer
;
378 wxGridCellEditor
* m_editor
;
380 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
384 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
385 class WXDLLIMPEXP_ADV
);
388 class WXDLLIMPEXP_ADV wxGridTypeRegistry
391 wxGridTypeRegistry() {}
392 ~wxGridTypeRegistry();
394 void RegisterDataType(const wxString
& typeName
,
395 wxGridCellRenderer
* renderer
,
396 wxGridCellEditor
* editor
);
398 // find one of already registered data types
399 int FindRegisteredDataType(const wxString
& typeName
);
401 // try to FindRegisteredDataType(), if this fails and typeName is one of
402 // standard typenames, register it and return its index
403 int FindDataType(const wxString
& typeName
);
405 // try to FindDataType(), if it fails see if it is not one of already
406 // registered data types with some params in which case clone the
407 // registered data type and set params for it
408 int FindOrCloneDataType(const wxString
& typeName
);
410 wxGridCellRenderer
* GetRenderer(int index
);
411 wxGridCellEditor
* GetEditor(int index
);
414 wxGridDataTypeInfoArray m_typeinfo
;
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 //#define DEBUG_ATTR_CACHE
422 #ifdef DEBUG_ATTR_CACHE
423 static size_t gs_nAttrCacheHits
= 0;
424 static size_t gs_nAttrCacheMisses
= 0;
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
431 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
432 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
435 static const size_t GRID_SCROLL_LINE_X
= 15;
436 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
438 // the size of hash tables used a bit everywhere (the max number of elements
439 // in these hash tables is the number of rows/columns)
440 static const int GRID_HASH_SIZE
= 100;
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
449 // ensure that first is less or equal to second, swapping the values if
451 void EnsureFirstLessThanSecond(int& first
, int& second
)
453 if ( first
> second
)
454 wxSwap(first
, second
);
457 } // anonymous namespace
459 // ============================================================================
461 // ============================================================================
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 wxGridCellEditor::wxGridCellEditor()
473 wxGridCellEditor::~wxGridCellEditor()
478 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
479 wxWindowID
WXUNUSED(id
),
480 wxEvtHandler
* evtHandler
)
483 m_control
->PushEventHandler(evtHandler
);
486 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
487 wxGridCellAttr
*attr
)
489 // erase the background because we might not fill the cell
490 wxClientDC
dc(m_control
->GetParent());
491 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
493 gridWindow
->GetOwner()->PrepareDC(dc
);
495 dc
.SetPen(*wxTRANSPARENT_PEN
);
496 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
497 dc
.DrawRectangle(rectCell
);
499 // redraw the control we just painted over
500 m_control
->Refresh();
503 void wxGridCellEditor::Destroy()
507 m_control
->PopEventHandler( true /* delete it*/ );
509 m_control
->Destroy();
514 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
516 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
518 m_control
->Show(show
);
522 // set the colours/fonts if we have any
525 m_colFgOld
= m_control
->GetForegroundColour();
526 m_control
->SetForegroundColour(attr
->GetTextColour());
528 m_colBgOld
= m_control
->GetBackgroundColour();
529 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
531 // Workaround for GTK+1 font setting problem on some platforms
532 #if !defined(__WXGTK__) || defined(__WXGTK20__)
533 m_fontOld
= m_control
->GetFont();
534 m_control
->SetFont(attr
->GetFont());
537 // can't do anything more in the base class version, the other
538 // attributes may only be used by the derived classes
543 // restore the standard colours fonts
544 if ( m_colFgOld
.Ok() )
546 m_control
->SetForegroundColour(m_colFgOld
);
547 m_colFgOld
= wxNullColour
;
550 if ( m_colBgOld
.Ok() )
552 m_control
->SetBackgroundColour(m_colBgOld
);
553 m_colBgOld
= wxNullColour
;
556 // Workaround for GTK+1 font setting problem on some platforms
557 #if !defined(__WXGTK__) || defined(__WXGTK20__)
558 if ( m_fontOld
.Ok() )
560 m_control
->SetFont(m_fontOld
);
561 m_fontOld
= wxNullFont
;
567 void wxGridCellEditor::SetSize(const wxRect
& rect
)
569 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
571 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
574 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
579 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
581 bool ctrl
= event
.ControlDown();
582 bool alt
= event
.AltDown();
585 // On the Mac the Alt key is more like shift and is used for entry of
586 // valid characters, so check for Ctrl and Meta instead.
587 alt
= event
.MetaDown();
590 // Assume it's not a valid char if ctrl or alt is down, but if both are
591 // down then it may be because of an AltGr key combination, so let them
592 // through in that case.
593 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
600 // If it's a F-Key or other special key then it shouldn't start the
602 if (event
.GetKeyCode() >= WXK_START
)
606 // if the unicode key code is not really a unicode character (it may
607 // be a function key or etc., the platforms appear to always give us a
608 // small value in this case) then fallback to the ASCII key code but
609 // don't do anything for function keys or etc.
610 key
= event
.GetUnicodeKey();
613 key
= event
.GetKeyCode();
614 keyOk
= (key
<= 127);
617 key
= event
.GetKeyCode();
618 keyOk
= (key
<= 255);
624 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
629 void wxGridCellEditor::StartingClick()
635 // ----------------------------------------------------------------------------
636 // wxGridCellTextEditor
637 // ----------------------------------------------------------------------------
639 wxGridCellTextEditor::wxGridCellTextEditor()
644 void wxGridCellTextEditor::Create(wxWindow
* parent
,
646 wxEvtHandler
* evtHandler
)
648 DoCreate(parent
, id
, evtHandler
);
651 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
653 wxEvtHandler
* evtHandler
,
656 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
658 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
659 wxDefaultPosition
, wxDefaultSize
,
662 // set max length allowed in the textctrl, if the parameter was set
663 if ( m_maxChars
!= 0 )
665 Text()->SetMaxLength(m_maxChars
);
668 wxGridCellEditor::Create(parent
, id
, evtHandler
);
671 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
672 wxGridCellAttr
* WXUNUSED(attr
))
674 // as we fill the entire client area,
675 // don't do anything here to minimize flicker
678 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
680 wxRect
rect(rectOrig
);
682 // Make the edit control large enough to allow for internal margins
684 // TODO: remove this if the text ctrl sizing is improved esp. for unix
686 #if defined(__WXGTK__)
694 #elif defined(__WXMSW__)
708 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
709 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
711 #if defined(__WXMOTIF__)
716 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
717 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
718 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
719 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
722 wxGridCellEditor::SetSize(rect
);
725 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
727 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
729 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
731 DoBeginEdit(m_startValue
);
734 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
736 Text()->SetValue(startValue
);
737 Text()->SetInsertionPointEnd();
738 Text()->SetSelection(-1, -1);
742 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
744 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
746 bool changed
= false;
747 wxString value
= Text()->GetValue();
748 if (value
!= m_startValue
)
752 grid
->GetTable()->SetValue(row
, col
, value
);
754 m_startValue
= wxEmptyString
;
756 // No point in setting the text of the hidden control
757 //Text()->SetValue(m_startValue);
762 void wxGridCellTextEditor::Reset()
764 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
766 DoReset(m_startValue
);
769 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
771 Text()->SetValue(startValue
);
772 Text()->SetInsertionPointEnd();
775 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
777 return wxGridCellEditor::IsAcceptedKey(event
);
780 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
782 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
783 // longer an appropriate way to get the character into the text control.
784 // Do it ourselves instead. We know that if we get this far that we have
785 // a valid character, so not a whole lot of testing needs to be done.
787 wxTextCtrl
* tc
= Text();
792 ch
= event
.GetUnicodeKey();
794 ch
= (wxChar
)event
.GetKeyCode();
796 ch
= (wxChar
)event
.GetKeyCode();
802 // delete the character at the cursor
803 pos
= tc
->GetInsertionPoint();
804 if (pos
< tc
->GetLastPosition())
805 tc
->Remove(pos
, pos
+ 1);
809 // delete the character before the cursor
810 pos
= tc
->GetInsertionPoint();
812 tc
->Remove(pos
- 1, pos
);
821 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
822 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
824 #if defined(__WXMOTIF__) || defined(__WXGTK__)
825 // wxMotif needs a little extra help...
826 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
827 wxString
s( Text()->GetValue() );
828 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
830 Text()->SetInsertionPoint( pos
);
832 // the other ports can handle a Return key press
838 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
848 if ( params
.ToLong(&tmp
) )
850 m_maxChars
= (size_t)tmp
;
854 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
859 // return the value in the text control
860 wxString
wxGridCellTextEditor::GetValue() const
862 return Text()->GetValue();
865 // ----------------------------------------------------------------------------
866 // wxGridCellNumberEditor
867 // ----------------------------------------------------------------------------
869 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
875 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
877 wxEvtHandler
* evtHandler
)
882 // create a spin ctrl
883 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
884 wxDefaultPosition
, wxDefaultSize
,
888 wxGridCellEditor::Create(parent
, id
, evtHandler
);
893 // just a text control
894 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
897 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
902 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
904 // first get the value
905 wxGridTableBase
*table
= grid
->GetTable();
906 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
908 m_valueOld
= table
->GetValueAsLong(row
, col
);
913 wxString sValue
= table
->GetValue(row
, col
);
914 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
916 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
924 Spin()->SetValue((int)m_valueOld
);
930 DoBeginEdit(GetString());
934 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
943 value
= Spin()->GetValue();
944 if ( value
== m_valueOld
)
947 text
.Printf(wxT("%ld"), value
);
949 else // using unconstrained input
950 #endif // wxUSE_SPINCTRL
952 const wxString
textOld(grid
->GetCellValue(row
, col
));
953 text
= Text()->GetValue();
956 if ( textOld
.empty() )
959 else // non-empty text now (maybe 0)
961 if ( !text
.ToLong(&value
) )
964 // if value == m_valueOld == 0 but old text was "" and new one is
965 // "0" something still did change
966 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
971 wxGridTableBase
* const table
= grid
->GetTable();
972 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
973 table
->SetValueAsLong(row
, col
, value
);
975 table
->SetValue(row
, col
, text
);
980 void wxGridCellNumberEditor::Reset()
985 Spin()->SetValue((int)m_valueOld
);
990 DoReset(GetString());
994 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
996 if ( wxGridCellEditor::IsAcceptedKey(event
) )
998 int keycode
= event
.GetKeyCode();
999 if ( (keycode
< 128) &&
1000 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1009 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1011 int keycode
= event
.GetKeyCode();
1014 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1016 wxGridCellTextEditor::StartingKey(event
);
1018 // skip Skip() below
1025 if ( wxIsdigit(keycode
) )
1027 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1028 spin
->SetValue(keycode
- '0');
1029 spin
->SetSelection(1,1);
1038 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1049 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1053 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1057 // skip the error message below
1062 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1066 // return the value in the spin control if it is there (the text control otherwise)
1067 wxString
wxGridCellNumberEditor::GetValue() const
1074 long value
= Spin()->GetValue();
1075 s
.Printf(wxT("%ld"), value
);
1080 s
= Text()->GetValue();
1086 // ----------------------------------------------------------------------------
1087 // wxGridCellFloatEditor
1088 // ----------------------------------------------------------------------------
1090 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1093 m_precision
= precision
;
1096 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1098 wxEvtHandler
* evtHandler
)
1100 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1102 #if wxUSE_VALIDATORS
1103 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1107 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1109 // first get the value
1110 wxGridTableBase
* const table
= grid
->GetTable();
1111 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1113 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1119 const wxString value
= table
->GetValue(row
, col
);
1120 if ( !value
.empty() )
1122 if ( !value
.ToDouble(&m_valueOld
) )
1124 wxFAIL_MSG( _T("this cell doesn't have float value") );
1130 DoBeginEdit(GetString());
1133 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1135 const wxString
text(Text()->GetValue()),
1136 textOld(grid
->GetCellValue(row
, col
));
1139 if ( !text
.empty() )
1141 if ( !text
.ToDouble(&value
) )
1144 else // new value is empty string
1146 if ( textOld
.empty() )
1147 return false; // nothing changed
1152 // the test for empty strings ensures that we don't skip the value setting
1153 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1154 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1155 return false; // nothing changed
1157 wxGridTableBase
* const table
= grid
->GetTable();
1159 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1160 table
->SetValueAsDouble(row
, col
, value
);
1162 table
->SetValue(row
, col
, text
);
1167 void wxGridCellFloatEditor::Reset()
1169 DoReset(GetString());
1172 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1174 int keycode
= event
.GetKeyCode();
1176 tmpbuf
[0] = (char) keycode
;
1178 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1181 bool is_decimal_point
= ( strbuf
==
1182 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1184 bool is_decimal_point
= ( strbuf
== _T(".") );
1187 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1188 || is_decimal_point
)
1190 wxGridCellTextEditor::StartingKey(event
);
1192 // skip Skip() below
1199 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1210 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1214 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1216 m_precision
= (int)tmp
;
1218 // skip the error message below
1223 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1227 wxString
wxGridCellFloatEditor::GetString() const
1230 if ( m_precision
== -1 && m_width
!= -1)
1232 // default precision
1233 fmt
.Printf(_T("%%%d.f"), m_width
);
1235 else if ( m_precision
!= -1 && m_width
== -1)
1238 fmt
.Printf(_T("%%.%df"), m_precision
);
1240 else if ( m_precision
!= -1 && m_width
!= -1 )
1242 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1246 // default width/precision
1250 return wxString::Format(fmt
, m_valueOld
);
1253 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1255 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1257 const int keycode
= event
.GetKeyCode();
1258 if ( isascii(keycode
) )
1261 tmpbuf
[0] = (char) keycode
;
1263 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1266 const wxString decimalPoint
=
1267 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1269 const wxString
decimalPoint(_T('.'));
1272 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1273 if ( wxIsdigit(keycode
) ||
1274 tolower(keycode
) == 'e' ||
1275 keycode
== decimalPoint
||
1287 #endif // wxUSE_TEXTCTRL
1291 // ----------------------------------------------------------------------------
1292 // wxGridCellBoolEditor
1293 // ----------------------------------------------------------------------------
1295 // the default values for GetValue()
1296 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1298 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1300 wxEvtHandler
* evtHandler
)
1302 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1303 wxDefaultPosition
, wxDefaultSize
,
1306 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1309 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1311 bool resize
= false;
1312 wxSize size
= m_control
->GetSize();
1313 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1315 // check if the checkbox is not too big/small for this cell
1316 wxSize sizeBest
= m_control
->GetBestSize();
1317 if ( !(size
== sizeBest
) )
1319 // reset to default size if it had been made smaller
1325 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1327 // leave 1 pixel margin
1328 size
.x
= size
.y
= minSize
- 2;
1335 m_control
->SetSize(size
);
1338 // position it in the centre of the rectangle (TODO: support alignment?)
1340 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1341 // the checkbox without label still has some space to the right in wxGTK,
1342 // so shift it to the right
1344 #elif defined(__WXMSW__)
1345 // here too, but in other way
1350 int hAlign
= wxALIGN_CENTRE
;
1351 int vAlign
= wxALIGN_CENTRE
;
1353 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1356 if (hAlign
== wxALIGN_LEFT
)
1364 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1366 else if (hAlign
== wxALIGN_RIGHT
)
1368 x
= r
.x
+ r
.width
- size
.x
- 2;
1369 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1371 else if (hAlign
== wxALIGN_CENTRE
)
1373 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1374 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1377 m_control
->Move(x
, y
);
1380 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1382 m_control
->Show(show
);
1386 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1387 CBox()->SetBackgroundColour(colBg
);
1391 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1393 wxASSERT_MSG(m_control
,
1394 wxT("The wxGridCellEditor must be created first!"));
1396 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1398 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1402 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1404 if ( cellval
== ms_stringValues
[false] )
1405 m_startValue
= false;
1406 else if ( cellval
== ms_stringValues
[true] )
1407 m_startValue
= true;
1410 // do not try to be smart here and convert it to true or false
1411 // because we'll still overwrite it with something different and
1412 // this risks to be very surprising for the user code, let them
1414 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1418 CBox()->SetValue(m_startValue
);
1422 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1425 wxASSERT_MSG(m_control
,
1426 wxT("The wxGridCellEditor must be created first!"));
1428 bool changed
= false;
1429 bool value
= CBox()->GetValue();
1430 if ( value
!= m_startValue
)
1435 wxGridTableBase
* const table
= grid
->GetTable();
1436 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1437 table
->SetValueAsBool(row
, col
, value
);
1439 table
->SetValue(row
, col
, GetValue());
1445 void wxGridCellBoolEditor::Reset()
1447 wxASSERT_MSG(m_control
,
1448 wxT("The wxGridCellEditor must be created first!"));
1450 CBox()->SetValue(m_startValue
);
1453 void wxGridCellBoolEditor::StartingClick()
1455 CBox()->SetValue(!CBox()->GetValue());
1458 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1460 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1462 int keycode
= event
.GetKeyCode();
1475 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1477 int keycode
= event
.GetKeyCode();
1481 CBox()->SetValue(!CBox()->GetValue());
1485 CBox()->SetValue(true);
1489 CBox()->SetValue(false);
1494 wxString
wxGridCellBoolEditor::GetValue() const
1496 return ms_stringValues
[CBox()->GetValue()];
1500 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1501 const wxString
& valueFalse
)
1503 ms_stringValues
[false] = valueFalse
;
1504 ms_stringValues
[true] = valueTrue
;
1508 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1510 return value
== ms_stringValues
[true];
1513 #endif // wxUSE_CHECKBOX
1517 // ----------------------------------------------------------------------------
1518 // wxGridCellChoiceEditor
1519 // ----------------------------------------------------------------------------
1521 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1523 : m_choices(choices
),
1524 m_allowOthers(allowOthers
) { }
1526 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1527 const wxString choices
[],
1529 : m_allowOthers(allowOthers
)
1533 m_choices
.Alloc(count
);
1534 for ( size_t n
= 0; n
< count
; n
++ )
1536 m_choices
.Add(choices
[n
]);
1541 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1543 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1544 editor
->m_allowOthers
= m_allowOthers
;
1545 editor
->m_choices
= m_choices
;
1550 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1552 wxEvtHandler
* evtHandler
)
1554 int style
= wxTE_PROCESS_ENTER
|
1558 if ( !m_allowOthers
)
1559 style
|= wxCB_READONLY
;
1560 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1561 wxDefaultPosition
, wxDefaultSize
,
1565 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1568 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1569 wxGridCellAttr
* attr
)
1571 // as we fill the entire client area, don't do anything here to minimize
1574 // TODO: It doesn't actually fill the client area since the height of a
1575 // combo always defaults to the standard. Until someone has time to
1576 // figure out the right rectangle to paint, just do it the normal way.
1577 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1580 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1582 wxASSERT_MSG(m_control
,
1583 wxT("The wxGridCellEditor must be created first!"));
1585 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1587 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1589 // Don't immediately end if we get a kill focus event within BeginEdit
1591 evtHandler
->SetInSetFocus(true);
1593 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1595 Reset(); // this updates combo box to correspond to m_startValue
1597 Combo()->SetFocus();
1601 // When dropping down the menu, a kill focus event
1602 // happens after this point, so we can't reset the flag yet.
1603 #if !defined(__WXGTK20__)
1604 evtHandler
->SetInSetFocus(false);
1609 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1612 wxString value
= Combo()->GetValue();
1613 if ( value
== m_startValue
)
1616 grid
->GetTable()->SetValue(row
, col
, value
);
1621 void wxGridCellChoiceEditor::Reset()
1625 Combo()->SetValue(m_startValue
);
1626 Combo()->SetInsertionPointEnd();
1628 else // the combobox is read-only
1630 // find the right position, or default to the first if not found
1631 int pos
= Combo()->FindString(m_startValue
);
1632 if (pos
== wxNOT_FOUND
)
1634 Combo()->SetSelection(pos
);
1638 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1648 wxStringTokenizer
tk(params
, _T(','));
1649 while ( tk
.HasMoreTokens() )
1651 m_choices
.Add(tk
.GetNextToken());
1655 // return the value in the text control
1656 wxString
wxGridCellChoiceEditor::GetValue() const
1658 return Combo()->GetValue();
1661 #endif // wxUSE_COMBOBOX
1663 // ----------------------------------------------------------------------------
1664 // wxGridCellEditorEvtHandler
1665 // ----------------------------------------------------------------------------
1667 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1669 // Don't disable the cell if we're just starting to edit it
1674 m_grid
->DisableCellEditControl();
1679 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1681 switch ( event
.GetKeyCode() )
1685 m_grid
->DisableCellEditControl();
1689 m_grid
->GetEventHandler()->ProcessEvent( event
);
1693 case WXK_NUMPAD_ENTER
:
1694 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1695 m_editor
->HandleReturn(event
);
1704 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1706 int row
= m_grid
->GetGridCursorRow();
1707 int col
= m_grid
->GetGridCursorCol();
1708 wxRect rect
= m_grid
->CellToRect( row
, col
);
1710 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1712 // if cell width is smaller than grid client area, cell is wholly visible
1713 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1715 switch ( event
.GetKeyCode() )
1720 case WXK_NUMPAD_ENTER
:
1725 if ( wholeCellVisible
)
1727 // no special processing needed...
1732 // do special processing for partly visible cell...
1734 // get the widths of all cells previous to this one
1736 for ( int i
= 0; i
< col
; i
++ )
1738 colXPos
+= m_grid
->GetColSize(i
);
1741 int xUnit
= 1, yUnit
= 1;
1742 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1745 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1749 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1757 if ( wholeCellVisible
)
1759 // no special processing needed...
1764 // do special processing for partly visible cell...
1767 wxString value
= m_grid
->GetCellValue(row
, col
);
1768 if ( wxEmptyString
!= value
)
1770 // get width of cell CONTENTS (text)
1772 wxFont font
= m_grid
->GetCellFont(row
, col
);
1773 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1775 // try to RIGHT align the text by scrolling
1776 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1778 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1779 // otherwise the last part of the cell content might be hidden below the scroll bar
1780 // FIXME: maybe there is a more suitable correction?
1781 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1782 if ( textWidth
< 0 )
1788 // get the widths of all cells previous to this one
1790 for ( int i
= 0; i
< col
; i
++ )
1792 colXPos
+= m_grid
->GetColSize(i
);
1795 // and add the (modified) text width of the cell contents
1796 // as we'd like to see the last part of the cell contents
1797 colXPos
+= textWidth
;
1799 int xUnit
= 1, yUnit
= 1;
1800 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1801 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1812 // ----------------------------------------------------------------------------
1813 // wxGridCellWorker is an (almost) empty common base class for
1814 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1815 // ----------------------------------------------------------------------------
1817 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1822 wxGridCellWorker::~wxGridCellWorker()
1826 // ============================================================================
1828 // ============================================================================
1830 // ----------------------------------------------------------------------------
1831 // wxGridCellRenderer
1832 // ----------------------------------------------------------------------------
1834 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1835 wxGridCellAttr
& attr
,
1838 int WXUNUSED(row
), int WXUNUSED(col
),
1841 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
1844 if ( grid
.IsEnabled() )
1848 if ( grid
.HasFocus() )
1849 clr
= grid
.GetSelectionBackground();
1851 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1855 clr
= attr
.GetBackgroundColour();
1858 else // grey out fields if the grid is disabled
1860 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
1864 dc
.SetPen( *wxTRANSPARENT_PEN
);
1865 dc
.DrawRectangle(rect
);
1868 // ----------------------------------------------------------------------------
1869 // wxGridCellStringRenderer
1870 // ----------------------------------------------------------------------------
1872 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1873 const wxGridCellAttr
& attr
,
1877 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
1879 // TODO some special colours for attr.IsReadOnly() case?
1881 // different coloured text when the grid is disabled
1882 if ( grid
.IsEnabled() )
1887 if ( grid
.HasFocus() )
1888 clr
= grid
.GetSelectionBackground();
1890 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1891 dc
.SetTextBackground( clr
);
1892 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1896 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1897 dc
.SetTextForeground( attr
.GetTextColour() );
1902 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1903 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1906 dc
.SetFont( attr
.GetFont() );
1909 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1911 const wxString
& text
)
1913 wxCoord x
= 0, y
= 0, max_x
= 0;
1914 dc
.SetFont(attr
.GetFont());
1915 wxStringTokenizer
tk(text
, _T('\n'));
1916 while ( tk
.HasMoreTokens() )
1918 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1919 max_x
= wxMax(max_x
, x
);
1922 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1924 return wxSize(max_x
, y
);
1927 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1928 wxGridCellAttr
& attr
,
1932 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1935 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1936 wxGridCellAttr
& attr
,
1938 const wxRect
& rectCell
,
1942 wxRect rect
= rectCell
;
1945 // erase only this cells background, overflow cells should have been erased
1946 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1949 attr
.GetAlignment(&hAlign
, &vAlign
);
1951 int overflowCols
= 0;
1953 if (attr
.GetOverflow())
1955 int cols
= grid
.GetNumberCols();
1956 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1957 int cell_rows
, cell_cols
;
1958 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1959 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1961 int i
, c_cols
, c_rows
;
1962 for (i
= col
+cell_cols
; i
< cols
; i
++)
1964 bool is_empty
= true;
1965 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1967 // check w/ anchor cell for multicell block
1968 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1971 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1980 rect
.width
+= grid
.GetColSize(i
);
1988 if (rect
.width
>= best_width
)
1992 overflowCols
= i
- col
- cell_cols
+ 1;
1993 if (overflowCols
>= cols
)
1994 overflowCols
= cols
- 1;
1997 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1999 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
2001 clip
.x
+= rectCell
.width
;
2002 // draw each overflow cell individually
2003 int col_end
= col
+ cell_cols
+ overflowCols
;
2004 if (col_end
>= grid
.GetNumberCols())
2005 col_end
= grid
.GetNumberCols() - 1;
2006 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2008 clip
.width
= grid
.GetColSize(i
) - 1;
2009 dc
.DestroyClippingRegion();
2010 dc
.SetClippingRegion(clip
);
2012 SetTextColoursAndFont(grid
, attr
, dc
,
2013 grid
.IsInSelection(row
,i
));
2015 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2016 rect
, hAlign
, vAlign
);
2017 clip
.x
+= grid
.GetColSize(i
) - 1;
2023 dc
.DestroyClippingRegion();
2027 // now we only have to draw the text
2028 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2030 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2031 rect
, hAlign
, vAlign
);
2034 // ----------------------------------------------------------------------------
2035 // wxGridCellNumberRenderer
2036 // ----------------------------------------------------------------------------
2038 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2040 wxGridTableBase
*table
= grid
.GetTable();
2042 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2044 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2048 text
= table
->GetValue(row
, col
);
2054 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2055 wxGridCellAttr
& attr
,
2057 const wxRect
& rectCell
,
2061 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2063 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2065 // draw the text right aligned by default
2067 attr
.GetAlignment(&hAlign
, &vAlign
);
2068 hAlign
= wxALIGN_RIGHT
;
2070 wxRect rect
= rectCell
;
2073 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2076 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2077 wxGridCellAttr
& attr
,
2081 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2084 // ----------------------------------------------------------------------------
2085 // wxGridCellFloatRenderer
2086 // ----------------------------------------------------------------------------
2088 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2091 SetPrecision(precision
);
2094 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2096 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2097 renderer
->m_width
= m_width
;
2098 renderer
->m_precision
= m_precision
;
2099 renderer
->m_format
= m_format
;
2104 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2106 wxGridTableBase
*table
= grid
.GetTable();
2111 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2113 val
= table
->GetValueAsDouble(row
, col
);
2118 text
= table
->GetValue(row
, col
);
2119 hasDouble
= text
.ToDouble(&val
);
2126 if ( m_width
== -1 )
2128 if ( m_precision
== -1 )
2130 // default width/precision
2131 m_format
= _T("%f");
2135 m_format
.Printf(_T("%%.%df"), m_precision
);
2138 else if ( m_precision
== -1 )
2140 // default precision
2141 m_format
.Printf(_T("%%%d.f"), m_width
);
2145 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2149 text
.Printf(m_format
, val
);
2152 //else: text already contains the string
2157 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2158 wxGridCellAttr
& attr
,
2160 const wxRect
& rectCell
,
2164 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2166 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2168 // draw the text right aligned by default
2170 attr
.GetAlignment(&hAlign
, &vAlign
);
2171 hAlign
= wxALIGN_RIGHT
;
2173 wxRect rect
= rectCell
;
2176 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2179 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2180 wxGridCellAttr
& attr
,
2184 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2187 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2191 // reset to defaults
2197 wxString tmp
= params
.BeforeFirst(_T(','));
2201 if ( tmp
.ToLong(&width
) )
2203 SetWidth((int)width
);
2207 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2211 tmp
= params
.AfterFirst(_T(','));
2215 if ( tmp
.ToLong(&precision
) )
2217 SetPrecision((int)precision
);
2221 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2227 // ----------------------------------------------------------------------------
2228 // wxGridCellBoolRenderer
2229 // ----------------------------------------------------------------------------
2231 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2233 // FIXME these checkbox size calculations are really ugly...
2235 // between checkmark and box
2236 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2238 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2239 wxGridCellAttr
& WXUNUSED(attr
),
2244 // compute it only once (no locks for MT safeness in GUI thread...)
2245 if ( !ms_sizeCheckMark
.x
)
2247 // get checkbox size
2248 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2249 wxSize size
= checkbox
->GetBestSize();
2250 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2252 #if defined(__WXMOTIF__)
2253 checkSize
-= size
.y
/ 2;
2258 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2261 return ms_sizeCheckMark
;
2264 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2265 wxGridCellAttr
& attr
,
2271 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2273 // draw a check mark in the centre (ignoring alignment - TODO)
2274 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2276 // don't draw outside the cell
2277 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2278 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2280 // and even leave (at least) 1 pixel margin
2281 size
.x
= size
.y
= minSize
;
2284 // draw a border around checkmark
2286 attr
.GetAlignment(&hAlign
, &vAlign
);
2289 if (hAlign
== wxALIGN_CENTRE
)
2291 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2292 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2293 rectBorder
.width
= size
.x
;
2294 rectBorder
.height
= size
.y
;
2296 else if (hAlign
== wxALIGN_LEFT
)
2298 rectBorder
.x
= rect
.x
+ 2;
2299 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2300 rectBorder
.width
= size
.x
;
2301 rectBorder
.height
= size
.y
;
2303 else if (hAlign
== wxALIGN_RIGHT
)
2305 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2306 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2307 rectBorder
.width
= size
.x
;
2308 rectBorder
.height
= size
.y
;
2312 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2314 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2318 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2319 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2324 flags
|= wxCONTROL_CHECKED
;
2326 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2329 // ----------------------------------------------------------------------------
2331 // ----------------------------------------------------------------------------
2333 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2337 m_isReadOnly
= Unset
;
2342 m_attrkind
= wxGridCellAttr::Cell
;
2344 m_sizeRows
= m_sizeCols
= 1;
2345 m_overflow
= UnsetOverflow
;
2347 SetDefAttr(attrDefault
);
2350 wxGridCellAttr
*wxGridCellAttr::Clone() const
2352 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2354 if ( HasTextColour() )
2355 attr
->SetTextColour(GetTextColour());
2356 if ( HasBackgroundColour() )
2357 attr
->SetBackgroundColour(GetBackgroundColour());
2359 attr
->SetFont(GetFont());
2360 if ( HasAlignment() )
2361 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2363 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2367 attr
->SetRenderer(m_renderer
);
2368 m_renderer
->IncRef();
2372 attr
->SetEditor(m_editor
);
2377 attr
->SetReadOnly();
2379 attr
->SetOverflow( m_overflow
== Overflow
);
2380 attr
->SetKind( m_attrkind
);
2385 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2387 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2388 SetTextColour(mergefrom
->GetTextColour());
2389 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2390 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2391 if ( !HasFont() && mergefrom
->HasFont() )
2392 SetFont(mergefrom
->GetFont());
2393 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2396 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2397 SetAlignment(hAlign
, vAlign
);
2399 if ( !HasSize() && mergefrom
->HasSize() )
2400 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2402 // Directly access member functions as GetRender/Editor don't just return
2403 // m_renderer/m_editor
2405 // Maybe add support for merge of Render and Editor?
2406 if (!HasRenderer() && mergefrom
->HasRenderer() )
2408 m_renderer
= mergefrom
->m_renderer
;
2409 m_renderer
->IncRef();
2411 if ( !HasEditor() && mergefrom
->HasEditor() )
2413 m_editor
= mergefrom
->m_editor
;
2416 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2417 SetReadOnly(mergefrom
->IsReadOnly());
2419 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2420 SetOverflow(mergefrom
->GetOverflow());
2422 SetDefAttr(mergefrom
->m_defGridAttr
);
2425 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2427 // The size of a cell is normally 1,1
2429 // If this cell is larger (2,2) then this is the top left cell
2430 // the other cells that will be covered (lower right cells) must be
2431 // set to negative or zero values such that
2432 // row + num_rows of the covered cell points to the larger cell (this cell)
2433 // same goes for the col + num_cols.
2435 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2437 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2438 !((num_rows
<= 0) && (num_cols
> 0)) ||
2439 !((num_rows
== 0) && (num_cols
== 0))),
2440 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2442 m_sizeRows
= num_rows
;
2443 m_sizeCols
= num_cols
;
2446 const wxColour
& wxGridCellAttr::GetTextColour() const
2448 if (HasTextColour())
2452 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2454 return m_defGridAttr
->GetTextColour();
2458 wxFAIL_MSG(wxT("Missing default cell attribute"));
2459 return wxNullColour
;
2463 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2465 if (HasBackgroundColour())
2469 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2471 return m_defGridAttr
->GetBackgroundColour();
2475 wxFAIL_MSG(wxT("Missing default cell attribute"));
2476 return wxNullColour
;
2480 const wxFont
& wxGridCellAttr::GetFont() const
2486 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2488 return m_defGridAttr
->GetFont();
2492 wxFAIL_MSG(wxT("Missing default cell attribute"));
2497 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2506 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2508 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2512 wxFAIL_MSG(wxT("Missing default cell attribute"));
2516 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2519 *num_rows
= m_sizeRows
;
2521 *num_cols
= m_sizeCols
;
2524 // GetRenderer and GetEditor use a slightly different decision path about
2525 // which attribute to use. If a non-default attr object has one then it is
2526 // used, otherwise the default editor or renderer is fetched from the grid and
2527 // used. It should be the default for the data type of the cell. If it is
2528 // NULL (because the table has a type that the grid does not have in its
2529 // registry), then the grid's default editor or renderer is used.
2531 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2533 wxGridCellRenderer
*renderer
= NULL
;
2535 if ( m_renderer
&& this != m_defGridAttr
)
2537 // use the cells renderer if it has one
2538 renderer
= m_renderer
;
2541 else // no non-default cell renderer
2543 // get default renderer for the data type
2546 // GetDefaultRendererForCell() will do IncRef() for us
2547 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2550 if ( renderer
== NULL
)
2552 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2554 // if we still don't have one then use the grid default
2555 // (no need for IncRef() here neither)
2556 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2558 else // default grid attr
2560 // use m_renderer which we had decided not to use initially
2561 renderer
= m_renderer
;
2568 // we're supposed to always find something
2569 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2574 // same as above, except for s/renderer/editor/g
2575 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2577 wxGridCellEditor
*editor
= NULL
;
2579 if ( m_editor
&& this != m_defGridAttr
)
2581 // use the cells editor if it has one
2585 else // no non default cell editor
2587 // get default editor for the data type
2590 // GetDefaultEditorForCell() will do IncRef() for us
2591 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2594 if ( editor
== NULL
)
2596 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2598 // if we still don't have one then use the grid default
2599 // (no need for IncRef() here neither)
2600 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2602 else // default grid attr
2604 // use m_editor which we had decided not to use initially
2612 // we're supposed to always find something
2613 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2618 // ----------------------------------------------------------------------------
2619 // wxGridCellAttrData
2620 // ----------------------------------------------------------------------------
2622 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2624 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2625 // touch attribute's reference counting explicitly, since this
2626 // is managed by class wxGridCellWithAttr
2627 int n
= FindIndex(row
, col
);
2628 if ( n
== wxNOT_FOUND
)
2632 // add the attribute
2633 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2635 //else: nothing to do
2637 else // we already have an attribute for this cell
2641 // change the attribute
2642 m_attrs
[(size_t)n
].ChangeAttr(attr
);
2646 // remove this attribute
2647 m_attrs
.RemoveAt((size_t)n
);
2652 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2654 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2656 int n
= FindIndex(row
, col
);
2657 if ( n
!= wxNOT_FOUND
)
2659 attr
= m_attrs
[(size_t)n
].attr
;
2666 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2668 size_t count
= m_attrs
.GetCount();
2669 for ( size_t n
= 0; n
< count
; n
++ )
2671 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2672 wxCoord row
= coords
.GetRow();
2673 if ((size_t)row
>= pos
)
2677 // If rows inserted, include row counter where necessary
2678 coords
.SetRow(row
+ numRows
);
2680 else if (numRows
< 0)
2682 // If rows deleted ...
2683 if ((size_t)row
>= pos
- numRows
)
2685 // ...either decrement row counter (if row still exists)...
2686 coords
.SetRow(row
+ numRows
);
2690 // ...or remove the attribute
2691 m_attrs
.RemoveAt(n
);
2700 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2702 size_t count
= m_attrs
.GetCount();
2703 for ( size_t n
= 0; n
< count
; n
++ )
2705 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2706 wxCoord col
= coords
.GetCol();
2707 if ( (size_t)col
>= pos
)
2711 // If rows inserted, include row counter where necessary
2712 coords
.SetCol(col
+ numCols
);
2714 else if (numCols
< 0)
2716 // If rows deleted ...
2717 if ((size_t)col
>= pos
- numCols
)
2719 // ...either decrement row counter (if row still exists)...
2720 coords
.SetCol(col
+ numCols
);
2724 // ...or remove the attribute
2725 m_attrs
.RemoveAt(n
);
2734 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2736 size_t count
= m_attrs
.GetCount();
2737 for ( size_t n
= 0; n
< count
; n
++ )
2739 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2740 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2749 // ----------------------------------------------------------------------------
2750 // wxGridRowOrColAttrData
2751 // ----------------------------------------------------------------------------
2753 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2755 size_t count
= m_attrs
.GetCount();
2756 for ( size_t n
= 0; n
< count
; n
++ )
2758 m_attrs
[n
]->DecRef();
2762 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2764 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2766 int n
= m_rowsOrCols
.Index(rowOrCol
);
2767 if ( n
!= wxNOT_FOUND
)
2769 attr
= m_attrs
[(size_t)n
];
2776 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2778 int i
= m_rowsOrCols
.Index(rowOrCol
);
2779 if ( i
== wxNOT_FOUND
)
2783 // add the attribute - no need to do anything to reference count
2784 // since we take ownership of the attribute.
2785 m_rowsOrCols
.Add(rowOrCol
);
2788 // nothing to remove
2792 size_t n
= (size_t)i
;
2793 if ( m_attrs
[n
] == attr
)
2798 // change the attribute, handling reference count manually,
2799 // taking ownership of the new attribute.
2800 m_attrs
[n
]->DecRef();
2805 // remove this attribute, handling reference count manually
2806 m_attrs
[n
]->DecRef();
2807 m_rowsOrCols
.RemoveAt(n
);
2808 m_attrs
.RemoveAt(n
);
2813 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2815 size_t count
= m_attrs
.GetCount();
2816 for ( size_t n
= 0; n
< count
; n
++ )
2818 int & rowOrCol
= m_rowsOrCols
[n
];
2819 if ( (size_t)rowOrCol
>= pos
)
2821 if ( numRowsOrCols
> 0 )
2823 // If rows inserted, include row counter where necessary
2824 rowOrCol
+= numRowsOrCols
;
2826 else if ( numRowsOrCols
< 0)
2828 // If rows deleted, either decrement row counter (if row still exists)
2829 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2830 rowOrCol
+= numRowsOrCols
;
2833 m_rowsOrCols
.RemoveAt(n
);
2834 m_attrs
[n
]->DecRef();
2835 m_attrs
.RemoveAt(n
);
2844 // ----------------------------------------------------------------------------
2845 // wxGridCellAttrProvider
2846 // ----------------------------------------------------------------------------
2848 wxGridCellAttrProvider::wxGridCellAttrProvider()
2850 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2853 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2858 void wxGridCellAttrProvider::InitData()
2860 m_data
= new wxGridCellAttrProviderData
;
2863 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2864 wxGridCellAttr::wxAttrKind kind
) const
2866 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2871 case (wxGridCellAttr::Any
):
2872 // Get cached merge attributes.
2873 // Currently not used as no cache implemented as not mutable
2874 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2877 // Basically implement old version.
2878 // Also check merge cache, so we don't have to re-merge every time..
2879 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2880 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2881 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2883 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2885 // Two or more are non NULL
2886 attr
= new wxGridCellAttr
;
2887 attr
->SetKind(wxGridCellAttr::Merged
);
2889 // Order is important..
2892 attr
->MergeWith(attrcell
);
2897 attr
->MergeWith(attrcol
);
2902 attr
->MergeWith(attrrow
);
2906 // store merge attr if cache implemented
2908 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2912 // one or none is non null return it or null.
2931 case (wxGridCellAttr::Cell
):
2932 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2935 case (wxGridCellAttr::Col
):
2936 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2939 case (wxGridCellAttr::Row
):
2940 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2945 // (wxGridCellAttr::Default):
2946 // (wxGridCellAttr::Merged):
2954 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2960 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2963 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2968 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2971 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2976 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2979 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2983 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2985 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2989 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2993 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2995 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2999 // ----------------------------------------------------------------------------
3000 // wxGridTypeRegistry
3001 // ----------------------------------------------------------------------------
3003 wxGridTypeRegistry::~wxGridTypeRegistry()
3005 size_t count
= m_typeinfo
.GetCount();
3006 for ( size_t i
= 0; i
< count
; i
++ )
3007 delete m_typeinfo
[i
];
3010 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3011 wxGridCellRenderer
* renderer
,
3012 wxGridCellEditor
* editor
)
3014 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3016 // is it already registered?
3017 int loc
= FindRegisteredDataType(typeName
);
3018 if ( loc
!= wxNOT_FOUND
)
3020 delete m_typeinfo
[loc
];
3021 m_typeinfo
[loc
] = info
;
3025 m_typeinfo
.Add(info
);
3029 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3031 size_t count
= m_typeinfo
.GetCount();
3032 for ( size_t i
= 0; i
< count
; i
++ )
3034 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3043 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3045 int index
= FindRegisteredDataType(typeName
);
3046 if ( index
== wxNOT_FOUND
)
3048 // check whether this is one of the standard ones, in which case
3049 // register it "on the fly"
3051 if ( typeName
== wxGRID_VALUE_STRING
)
3053 RegisterDataType(wxGRID_VALUE_STRING
,
3054 new wxGridCellStringRenderer
,
3055 new wxGridCellTextEditor
);
3058 #endif // wxUSE_TEXTCTRL
3060 if ( typeName
== wxGRID_VALUE_BOOL
)
3062 RegisterDataType(wxGRID_VALUE_BOOL
,
3063 new wxGridCellBoolRenderer
,
3064 new wxGridCellBoolEditor
);
3067 #endif // wxUSE_CHECKBOX
3069 if ( typeName
== wxGRID_VALUE_NUMBER
)
3071 RegisterDataType(wxGRID_VALUE_NUMBER
,
3072 new wxGridCellNumberRenderer
,
3073 new wxGridCellNumberEditor
);
3075 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3077 RegisterDataType(wxGRID_VALUE_FLOAT
,
3078 new wxGridCellFloatRenderer
,
3079 new wxGridCellFloatEditor
);
3082 #endif // wxUSE_TEXTCTRL
3084 if ( typeName
== wxGRID_VALUE_CHOICE
)
3086 RegisterDataType(wxGRID_VALUE_CHOICE
,
3087 new wxGridCellStringRenderer
,
3088 new wxGridCellChoiceEditor
);
3091 #endif // wxUSE_COMBOBOX
3096 // we get here only if just added the entry for this type, so return
3098 index
= m_typeinfo
.GetCount() - 1;
3104 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3106 int index
= FindDataType(typeName
);
3107 if ( index
== wxNOT_FOUND
)
3109 // the first part of the typename is the "real" type, anything after ':'
3110 // are the parameters for the renderer
3111 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3112 if ( index
== wxNOT_FOUND
)
3117 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3118 wxGridCellRenderer
*rendererOld
= renderer
;
3119 renderer
= renderer
->Clone();
3120 rendererOld
->DecRef();
3122 wxGridCellEditor
*editor
= GetEditor(index
);
3123 wxGridCellEditor
*editorOld
= editor
;
3124 editor
= editor
->Clone();
3125 editorOld
->DecRef();
3127 // do it even if there are no parameters to reset them to defaults
3128 wxString params
= typeName
.AfterFirst(_T(':'));
3129 renderer
->SetParameters(params
);
3130 editor
->SetParameters(params
);
3132 // register the new typename
3133 RegisterDataType(typeName
, renderer
, editor
);
3135 // we just registered it, it's the last one
3136 index
= m_typeinfo
.GetCount() - 1;
3142 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3144 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3151 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3153 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3160 // ----------------------------------------------------------------------------
3162 // ----------------------------------------------------------------------------
3164 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3166 wxGridTableBase::wxGridTableBase()
3168 m_view
= (wxGrid
*) NULL
;
3169 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3172 wxGridTableBase::~wxGridTableBase()
3174 delete m_attrProvider
;
3177 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3179 delete m_attrProvider
;
3180 m_attrProvider
= attrProvider
;
3183 bool wxGridTableBase::CanHaveAttributes()
3185 if ( ! GetAttrProvider() )
3187 // use the default attr provider by default
3188 SetAttrProvider(new wxGridCellAttrProvider
);
3194 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3196 if ( m_attrProvider
)
3197 return m_attrProvider
->GetAttr(row
, col
, kind
);
3199 return (wxGridCellAttr
*)NULL
;
3202 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3204 if ( m_attrProvider
)
3207 attr
->SetKind(wxGridCellAttr::Cell
);
3208 m_attrProvider
->SetAttr(attr
, row
, col
);
3212 // as we take ownership of the pointer and don't store it, we must
3218 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3220 if ( m_attrProvider
)
3222 attr
->SetKind(wxGridCellAttr::Row
);
3223 m_attrProvider
->SetRowAttr(attr
, row
);
3227 // as we take ownership of the pointer and don't store it, we must
3233 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3235 if ( m_attrProvider
)
3237 attr
->SetKind(wxGridCellAttr::Col
);
3238 m_attrProvider
->SetColAttr(attr
, col
);
3242 // as we take ownership of the pointer and don't store it, we must
3248 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3249 size_t WXUNUSED(numRows
) )
3251 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3256 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3258 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3263 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3264 size_t WXUNUSED(numRows
) )
3266 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3271 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3272 size_t WXUNUSED(numCols
) )
3274 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3279 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3281 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3286 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3287 size_t WXUNUSED(numCols
) )
3289 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3294 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3298 // RD: Starting the rows at zero confuses users,
3299 // no matter how much it makes sense to us geeks.
3305 wxString
wxGridTableBase::GetColLabelValue( int col
)
3307 // default col labels are:
3308 // cols 0 to 25 : A-Z
3309 // cols 26 to 675 : AA-ZZ
3314 for ( n
= 1; ; n
++ )
3316 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3322 // reverse the string...
3324 for ( i
= 0; i
< n
; i
++ )
3332 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3334 return wxGRID_VALUE_STRING
;
3337 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3338 const wxString
& typeName
)
3340 return typeName
== wxGRID_VALUE_STRING
;
3343 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3345 return CanGetValueAs(row
, col
, typeName
);
3348 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3353 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3358 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3363 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3364 long WXUNUSED(value
) )
3368 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3369 double WXUNUSED(value
) )
3373 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3374 bool WXUNUSED(value
) )
3378 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3379 const wxString
& WXUNUSED(typeName
) )
3384 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3385 const wxString
& WXUNUSED(typeName
),
3386 void* WXUNUSED(value
) )
3390 //////////////////////////////////////////////////////////////////////
3392 // Message class for the grid table to send requests and notifications
3396 wxGridTableMessage::wxGridTableMessage()
3398 m_table
= (wxGridTableBase
*) NULL
;
3404 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3405 int commandInt1
, int commandInt2
)
3409 m_comInt1
= commandInt1
;
3410 m_comInt2
= commandInt2
;
3413 //////////////////////////////////////////////////////////////////////
3415 // A basic grid table for string data. An object of this class will
3416 // created by wxGrid if you don't specify an alternative table class.
3419 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3421 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3423 wxGridStringTable::wxGridStringTable()
3428 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3431 m_data
.Alloc( numRows
);
3434 sa
.Alloc( numCols
);
3435 sa
.Add( wxEmptyString
, numCols
);
3437 m_data
.Add( sa
, numRows
);
3440 wxGridStringTable::~wxGridStringTable()
3444 int wxGridStringTable::GetNumberRows()
3446 return m_data
.GetCount();
3449 int wxGridStringTable::GetNumberCols()
3451 if ( m_data
.GetCount() > 0 )
3452 return m_data
[0].GetCount();
3457 wxString
wxGridStringTable::GetValue( int row
, int col
)
3459 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3461 _T("invalid row or column index in wxGridStringTable") );
3463 return m_data
[row
][col
];
3466 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3468 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3469 _T("invalid row or column index in wxGridStringTable") );
3471 m_data
[row
][col
] = value
;
3474 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3476 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3478 _T("invalid row or column index in wxGridStringTable") );
3480 return (m_data
[row
][col
] == wxEmptyString
);
3483 void wxGridStringTable::Clear()
3486 int numRows
, numCols
;
3488 numRows
= m_data
.GetCount();
3491 numCols
= m_data
[0].GetCount();
3493 for ( row
= 0; row
< numRows
; row
++ )
3495 for ( col
= 0; col
< numCols
; col
++ )
3497 m_data
[row
][col
] = wxEmptyString
;
3503 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3505 size_t curNumRows
= m_data
.GetCount();
3506 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3507 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3509 if ( pos
>= curNumRows
)
3511 return AppendRows( numRows
);
3515 sa
.Alloc( curNumCols
);
3516 sa
.Add( wxEmptyString
, curNumCols
);
3517 m_data
.Insert( sa
, pos
, numRows
);
3521 wxGridTableMessage
msg( this,
3522 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3526 GetView()->ProcessTableMessage( msg
);
3532 bool wxGridStringTable::AppendRows( size_t numRows
)
3534 size_t curNumRows
= m_data
.GetCount();
3535 size_t curNumCols
= ( curNumRows
> 0
3536 ? m_data
[0].GetCount()
3537 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3540 if ( curNumCols
> 0 )
3542 sa
.Alloc( curNumCols
);
3543 sa
.Add( wxEmptyString
, curNumCols
);
3546 m_data
.Add( sa
, numRows
);
3550 wxGridTableMessage
msg( this,
3551 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3554 GetView()->ProcessTableMessage( msg
);
3560 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3562 size_t curNumRows
= m_data
.GetCount();
3564 if ( pos
>= curNumRows
)
3566 wxFAIL_MSG( wxString::Format
3568 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3570 (unsigned long)numRows
,
3571 (unsigned long)curNumRows
3577 if ( numRows
> curNumRows
- pos
)
3579 numRows
= curNumRows
- pos
;
3582 if ( numRows
>= curNumRows
)
3588 m_data
.RemoveAt( pos
, numRows
);
3593 wxGridTableMessage
msg( this,
3594 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3598 GetView()->ProcessTableMessage( msg
);
3604 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3608 size_t curNumRows
= m_data
.GetCount();
3609 size_t curNumCols
= ( curNumRows
> 0
3610 ? m_data
[0].GetCount()
3611 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3613 if ( pos
>= curNumCols
)
3615 return AppendCols( numCols
);
3618 if ( !m_colLabels
.IsEmpty() )
3620 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3623 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3624 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3627 for ( row
= 0; row
< curNumRows
; row
++ )
3629 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3631 m_data
[row
].Insert( wxEmptyString
, col
);
3637 wxGridTableMessage
msg( this,
3638 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3642 GetView()->ProcessTableMessage( msg
);
3648 bool wxGridStringTable::AppendCols( size_t numCols
)
3652 size_t curNumRows
= m_data
.GetCount();
3654 for ( row
= 0; row
< curNumRows
; row
++ )
3656 m_data
[row
].Add( wxEmptyString
, numCols
);
3661 wxGridTableMessage
msg( this,
3662 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3665 GetView()->ProcessTableMessage( msg
);
3671 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3675 size_t curNumRows
= m_data
.GetCount();
3676 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3677 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3679 if ( pos
>= curNumCols
)
3681 wxFAIL_MSG( wxString::Format
3683 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3685 (unsigned long)numCols
,
3686 (unsigned long)curNumCols
3693 colID
= GetView()->GetColAt( pos
);
3697 if ( numCols
> curNumCols
- colID
)
3699 numCols
= curNumCols
- colID
;
3702 if ( !m_colLabels
.IsEmpty() )
3704 // m_colLabels stores just as many elements as it needs, e.g. if only
3705 // the label of the first column had been set it would have only one
3706 // element and not numCols, so account for it
3707 int nToRm
= m_colLabels
.size() - colID
;
3709 m_colLabels
.RemoveAt( colID
, nToRm
);
3712 for ( row
= 0; row
< curNumRows
; row
++ )
3714 if ( numCols
>= curNumCols
)
3716 m_data
[row
].Clear();
3720 m_data
[row
].RemoveAt( colID
, numCols
);
3726 wxGridTableMessage
msg( this,
3727 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3731 GetView()->ProcessTableMessage( msg
);
3737 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3739 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3741 // using default label
3743 return wxGridTableBase::GetRowLabelValue( row
);
3747 return m_rowLabels
[row
];
3751 wxString
wxGridStringTable::GetColLabelValue( int col
)
3753 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3755 // using default label
3757 return wxGridTableBase::GetColLabelValue( col
);
3761 return m_colLabels
[col
];
3765 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3767 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3769 int n
= m_rowLabels
.GetCount();
3772 for ( i
= n
; i
<= row
; i
++ )
3774 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3778 m_rowLabels
[row
] = value
;
3781 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3783 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3785 int n
= m_colLabels
.GetCount();
3788 for ( i
= n
; i
<= col
; i
++ )
3790 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3794 m_colLabels
[col
] = value
;
3798 //////////////////////////////////////////////////////////////////////
3799 //////////////////////////////////////////////////////////////////////
3801 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3802 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3805 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3807 m_owner
->CancelMouseCapture();
3810 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3812 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3813 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3814 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3815 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3818 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3820 const wxPoint
&pos
, const wxSize
&size
)
3821 : wxGridSubwindow(parent
, id
, pos
, size
)
3826 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3830 // NO - don't do this because it will set both the x and y origin
3831 // coords to match the parent scrolled window and we just want to
3832 // set the y coord - MB
3834 // m_owner->PrepareDC( dc );
3837 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3838 wxPoint pt
= dc
.GetDeviceOrigin();
3839 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3841 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3842 m_owner
->DrawRowLabels( dc
, rows
);
3845 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3847 m_owner
->ProcessRowLabelMouseEvent( event
);
3850 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3852 m_owner
->GetEventHandler()->ProcessEvent( event
);
3855 //////////////////////////////////////////////////////////////////////
3857 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3859 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3860 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3861 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3862 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3865 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3867 const wxPoint
&pos
, const wxSize
&size
)
3868 : wxGridSubwindow(parent
, id
, pos
, size
)
3873 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3877 // NO - don't do this because it will set both the x and y origin
3878 // coords to match the parent scrolled window and we just want to
3879 // set the x coord - MB
3881 // m_owner->PrepareDC( dc );
3884 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3885 wxPoint pt
= dc
.GetDeviceOrigin();
3886 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3887 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3889 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3891 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3892 m_owner
->DrawColLabels( dc
, cols
);
3895 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3897 m_owner
->ProcessColLabelMouseEvent( event
);
3900 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3902 m_owner
->GetEventHandler()->ProcessEvent( event
);
3905 //////////////////////////////////////////////////////////////////////
3907 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3909 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3910 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3911 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3912 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3915 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3918 const wxSize
& size
)
3919 : wxGridSubwindow(parent
, id
, pos
, size
)
3924 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3928 m_owner
->DrawCornerLabel(dc
);
3931 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3933 m_owner
->ProcessCornerLabelMouseEvent( event
);
3936 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3938 m_owner
->GetEventHandler()->ProcessEvent(event
);
3941 //////////////////////////////////////////////////////////////////////
3943 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3945 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3946 EVT_PAINT( wxGridWindow::OnPaint
)
3947 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3948 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3949 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3950 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3951 EVT_CHAR( wxGridWindow::OnChar
)
3952 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3953 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3954 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3957 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3958 wxGridRowLabelWindow
*rowLblWin
,
3959 wxGridColLabelWindow
*colLblWin
,
3962 const wxSize
&size
)
3963 : wxGridSubwindow(parent
, id
, pos
, size
,
3964 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3965 wxT("grid window") )
3968 m_rowLabelWin
= rowLblWin
;
3969 m_colLabelWin
= colLblWin
;
3972 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3974 wxPaintDC
dc( this );
3975 m_owner
->PrepareDC( dc
);
3976 wxRegion reg
= GetUpdateRegion();
3977 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3978 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3980 m_owner
->DrawAllGridLines( dc
, reg
);
3982 m_owner
->DrawGridSpace( dc
);
3983 m_owner
->DrawHighlight( dc
, dirtyCells
);
3986 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3988 wxWindow::ScrollWindow( dx
, dy
, rect
);
3989 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3990 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3993 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3995 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3998 m_owner
->ProcessGridCellMouseEvent( event
);
4001 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4003 m_owner
->GetEventHandler()->ProcessEvent( event
);
4006 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4007 // cursor must be in the cell edit control to get key events
4009 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4011 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4015 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4017 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4021 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4023 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4027 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4031 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4033 // and if we have any selection, it has to be repainted, because it
4034 // uses different colour when the grid is not focused:
4035 if ( m_owner
->IsSelection() )
4041 // NB: Note that this code is in "else" branch only because the other
4042 // branch refreshes everything and so there's no point in calling
4043 // Refresh() again, *not* because it should only be done if
4044 // !IsSelection(). If the above code is ever optimized to refresh
4045 // only selected area, this needs to be moved out of the "else"
4046 // branch so that it's always executed.
4048 // current cell cursor {dis,re}appears on focus change:
4049 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4050 m_owner
->GetGridCursorCol());
4051 const wxRect cursor
=
4052 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4053 Refresh(true, &cursor
);
4056 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4060 //////////////////////////////////////////////////////////////////////
4062 // Internal Helper function for computing row or column from some
4063 // (unscrolled) coordinate value, using either
4064 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4065 // of m_rowBottoms/m_ColRights to speed up the search!
4067 // Internal helper macros for simpler use of that function
4069 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4070 const wxArrayInt
& BorderArray
, int nMax
,
4073 #define internalXToCol(x) XToCol(x, true)
4074 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4075 m_minAcceptableRowHeight, \
4076 m_rowBottoms, m_numRows, true)
4078 /////////////////////////////////////////////////////////////////////
4080 #if wxUSE_EXTENDED_RTTI
4081 WX_DEFINE_FLAGS( wxGridStyle
)
4083 wxBEGIN_FLAGS( wxGridStyle
)
4084 // new style border flags, we put them first to
4085 // use them for streaming out
4086 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4087 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4088 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4089 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4090 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4091 wxFLAGS_MEMBER(wxBORDER_NONE
)
4093 // old style border flags
4094 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4095 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4096 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4097 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4098 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4099 wxFLAGS_MEMBER(wxBORDER
)
4101 // standard window styles
4102 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4103 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4104 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4105 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4106 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4107 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4108 wxFLAGS_MEMBER(wxVSCROLL
)
4109 wxFLAGS_MEMBER(wxHSCROLL
)
4111 wxEND_FLAGS( wxGridStyle
)
4113 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4115 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4116 wxHIDE_PROPERTY( Children
)
4117 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4118 wxEND_PROPERTIES_TABLE()
4120 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4121 wxEND_HANDLERS_TABLE()
4123 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4126 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4129 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4132 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4133 EVT_PAINT( wxGrid::OnPaint
)
4134 EVT_SIZE( wxGrid::OnSize
)
4135 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4136 EVT_KEY_UP( wxGrid::OnKeyUp
)
4137 EVT_CHAR ( wxGrid::OnChar
)
4138 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4146 wxGrid::wxGrid( wxWindow
*parent
,
4151 const wxString
& name
)
4154 Create(parent
, id
, pos
, size
, style
, name
);
4157 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4158 const wxPoint
& pos
, const wxSize
& size
,
4159 long style
, const wxString
& name
)
4161 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4162 style
| wxWANTS_CHARS
, name
))
4165 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4166 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4169 SetInitialSize(size
);
4170 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4178 // Must do this or ~wxScrollHelper will pop the wrong event handler
4179 SetTargetWindow(this);
4181 wxSafeDecRef(m_defaultCellAttr
);
4183 #ifdef DEBUG_ATTR_CACHE
4184 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4185 wxPrintf(_T("wxGrid attribute cache statistics: "
4186 "total: %u, hits: %u (%u%%)\n"),
4187 total
, gs_nAttrCacheHits
,
4188 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4191 // if we own the table, just delete it, otherwise at least don't leave it
4192 // with dangling view pointer
4195 else if ( m_table
&& m_table
->GetView() == this )
4196 m_table
->SetView(NULL
);
4198 delete m_typeRegistry
;
4203 // ----- internal init and update functions
4206 // NOTE: If using the default visual attributes works everywhere then this can
4207 // be removed as well as the #else cases below.
4208 #define _USE_VISATTR 0
4210 void wxGrid::Create()
4212 // create the type registry
4213 m_typeRegistry
= new wxGridTypeRegistry
;
4215 m_cellEditCtrlEnabled
= false;
4217 m_defaultCellAttr
= new wxGridCellAttr();
4219 // Set default cell attributes
4220 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4221 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4222 m_defaultCellAttr
->SetFont(GetFont());
4223 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4224 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4225 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4228 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4229 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4231 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4232 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4235 m_defaultCellAttr
->SetTextColour(
4236 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4237 m_defaultCellAttr
->SetBackgroundColour(
4238 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4243 m_currentCellCoords
= wxGridNoCellCoords
;
4245 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4246 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4248 // subwindow components that make up the wxGrid
4249 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4254 m_colLabelWin
= new wxGridColLabelWindow( this,
4259 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4264 m_gridWin
= new wxGridWindow( this,
4271 SetTargetWindow( m_gridWin
);
4274 wxColour gfg
= gva
.colFg
;
4275 wxColour gbg
= gva
.colBg
;
4276 wxColour lfg
= lva
.colFg
;
4277 wxColour lbg
= lva
.colBg
;
4279 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4280 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4281 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4282 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4285 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4286 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4287 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4288 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4289 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4290 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4292 m_gridWin
->SetOwnForegroundColour(gfg
);
4293 m_gridWin
->SetOwnBackgroundColour(gbg
);
4298 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4299 wxGridSelectionModes selmode
)
4301 wxCHECK_MSG( !m_created
,
4303 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4305 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4308 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4310 wxCHECK_RET( m_created
,
4311 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4313 m_selection
->SetSelectionMode( selmode
);
4316 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4318 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4319 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4321 return m_selection
->GetSelectionMode();
4325 wxGrid::SetTable(wxGridTableBase
*table
,
4327 wxGrid::wxGridSelectionModes selmode
)
4329 bool checkSelection
= false;
4332 // stop all processing
4337 m_table
->SetView(0);
4349 checkSelection
= true;
4351 // kill row and column size arrays
4352 m_colWidths
.Empty();
4353 m_colRights
.Empty();
4354 m_rowHeights
.Empty();
4355 m_rowBottoms
.Empty();
4360 m_numRows
= table
->GetNumberRows();
4361 m_numCols
= table
->GetNumberCols();
4364 m_table
->SetView( this );
4365 m_ownTable
= takeOwnership
;
4366 m_selection
= new wxGridSelection( this, selmode
);
4369 // If the newly set table is smaller than the
4370 // original one current cell and selection regions
4371 // might be invalid,
4372 m_selectingKeyboard
= wxGridNoCellCoords
;
4373 m_currentCellCoords
=
4374 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4375 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4376 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4377 m_selectingTopLeft
.GetCol() >= m_numCols
)
4379 m_selectingTopLeft
= wxGridNoCellCoords
;
4380 m_selectingBottomRight
= wxGridNoCellCoords
;
4383 m_selectingBottomRight
=
4384 wxGridCellCoords(wxMin(m_numRows
,
4385 m_selectingBottomRight
.GetRow()),
4387 m_selectingBottomRight
.GetCol()));
4397 void wxGrid::InitVars()
4401 m_cornerLabelWin
= NULL
;
4402 m_rowLabelWin
= NULL
;
4403 m_colLabelWin
= NULL
;
4410 m_defaultCellAttr
= NULL
;
4411 m_typeRegistry
= NULL
;
4412 m_winCapture
= NULL
;
4417 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4418 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4420 if ( m_rowLabelWin
)
4422 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4426 m_labelBackgroundColour
= *wxWHITE
;
4429 m_labelTextColour
= *wxBLACK
;
4432 m_attrCache
.row
= -1;
4433 m_attrCache
.col
= -1;
4434 m_attrCache
.attr
= NULL
;
4436 m_labelFont
= GetFont();
4437 m_labelFont
.SetWeight( wxBOLD
);
4439 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4440 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4442 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4443 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4444 m_colLabelTextOrientation
= wxHORIZONTAL
;
4446 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4447 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4449 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4450 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4452 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4453 m_defaultRowHeight
+= 8;
4455 m_defaultRowHeight
+= 4;
4458 m_gridLineColour
= wxColour( 192,192,192 );
4459 m_gridLinesEnabled
= true;
4460 m_cellHighlightColour
= *wxBLACK
;
4461 m_cellHighlightPenWidth
= 2;
4462 m_cellHighlightROPenWidth
= 1;
4464 m_canDragColMove
= false;
4466 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4467 m_winCapture
= (wxWindow
*)NULL
;
4468 m_canDragRowSize
= true;
4469 m_canDragColSize
= true;
4470 m_canDragGridSize
= true;
4471 m_canDragCell
= false;
4473 m_dragRowOrCol
= -1;
4474 m_isDragging
= false;
4475 m_startDragPos
= wxDefaultPosition
;
4476 m_nativeColumnLabels
= false;
4478 m_waitForSlowClick
= false;
4480 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4481 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4483 m_currentCellCoords
= wxGridNoCellCoords
;
4487 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4488 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4490 m_editable
= true; // default for whole grid
4492 m_inOnKeyDown
= false;
4498 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4499 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4502 // ----------------------------------------------------------------------------
4503 // the idea is to call these functions only when necessary because they create
4504 // quite big arrays which eat memory mostly unnecessary - in particular, if
4505 // default widths/heights are used for all rows/columns, we may not use these
4508 // with some extra code, it should be possible to only store the widths/heights
4509 // different from default ones (resulting in space savings for huge grids) but
4510 // this is not done currently
4511 // ----------------------------------------------------------------------------
4513 void wxGrid::InitRowHeights()
4515 m_rowHeights
.Empty();
4516 m_rowBottoms
.Empty();
4518 m_rowHeights
.Alloc( m_numRows
);
4519 m_rowBottoms
.Alloc( m_numRows
);
4521 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4524 for ( int i
= 0; i
< m_numRows
; i
++ )
4526 rowBottom
+= m_defaultRowHeight
;
4527 m_rowBottoms
.Add( rowBottom
);
4531 void wxGrid::InitColWidths()
4533 m_colWidths
.Empty();
4534 m_colRights
.Empty();
4536 m_colWidths
.Alloc( m_numCols
);
4537 m_colRights
.Alloc( m_numCols
);
4539 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4542 for ( int i
= 0; i
< m_numCols
; i
++ )
4544 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4545 m_colRights
.Add( colRight
);
4549 int wxGrid::GetColWidth(int col
) const
4551 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4554 int wxGrid::GetColLeft(int col
) const
4556 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4557 : m_colRights
[col
] - m_colWidths
[col
];
4560 int wxGrid::GetColRight(int col
) const
4562 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4566 int wxGrid::GetRowHeight(int row
) const
4568 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4571 int wxGrid::GetRowTop(int row
) const
4573 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4574 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4577 int wxGrid::GetRowBottom(int row
) const
4579 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4580 : m_rowBottoms
[row
];
4583 void wxGrid::CalcDimensions()
4585 // compute the size of the scrollable area
4586 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4587 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4592 // take into account editor if shown
4593 if ( IsCellEditControlShown() )
4596 int r
= m_currentCellCoords
.GetRow();
4597 int c
= m_currentCellCoords
.GetCol();
4598 int x
= GetColLeft(c
);
4599 int y
= GetRowTop(r
);
4601 // how big is the editor
4602 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4603 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4604 editor
->GetControl()->GetSize(&w2
, &h2
);
4615 // preserve (more or less) the previous position
4617 GetViewStart( &x
, &y
);
4619 // ensure the position is valid for the new scroll ranges
4621 x
= wxMax( w
- 1, 0 );
4623 y
= wxMax( h
- 1, 0 );
4625 // update the virtual size and refresh the scrollbars to reflect it
4626 m_gridWin
->SetVirtualSize(w
, h
);
4630 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4631 // still must reposition the children
4635 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4637 wxSize
sizeGridWin(size
);
4638 sizeGridWin
.x
-= m_rowLabelWidth
;
4639 sizeGridWin
.y
-= m_colLabelHeight
;
4644 void wxGrid::CalcWindowSizes()
4646 // escape if the window is has not been fully created yet
4648 if ( m_cornerLabelWin
== NULL
)
4652 GetClientSize( &cw
, &ch
);
4654 // the grid may be too small to have enough space for the labels yet, don't
4655 // size the windows to negative sizes in this case
4656 int gw
= cw
- m_rowLabelWidth
;
4657 int gh
= ch
- m_colLabelHeight
;
4663 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4664 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4666 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4667 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4669 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4670 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4672 if ( m_gridWin
&& m_gridWin
->IsShown() )
4673 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4676 // this is called when the grid table sends a message
4677 // to indicate that it has been redimensioned
4679 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4682 bool result
= false;
4684 // Clear the attribute cache as the attribute might refer to a different
4685 // cell than stored in the cache after adding/removing rows/columns.
4688 // By the same reasoning, the editor should be dismissed if columns are
4689 // added or removed. And for consistency, it should IMHO always be
4690 // removed, not only if the cell "underneath" it actually changes.
4691 // For now, I intentionally do not save the editor's content as the
4692 // cell it might want to save that stuff to might no longer exist.
4693 HideCellEditControl();
4696 // if we were using the default widths/heights so far, we must change them
4698 if ( m_colWidths
.IsEmpty() )
4703 if ( m_rowHeights
.IsEmpty() )
4709 switch ( msg
.GetId() )
4711 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4713 size_t pos
= msg
.GetCommandInt();
4714 int numRows
= msg
.GetCommandInt2();
4716 m_numRows
+= numRows
;
4718 if ( !m_rowHeights
.IsEmpty() )
4720 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4721 m_rowBottoms
.Insert( 0, pos
, numRows
);
4725 bottom
= m_rowBottoms
[pos
- 1];
4727 for ( i
= pos
; i
< m_numRows
; i
++ )
4729 bottom
+= m_rowHeights
[i
];
4730 m_rowBottoms
[i
] = bottom
;
4734 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4736 // if we have just inserted cols into an empty grid the current
4737 // cell will be undefined...
4739 SetCurrentCell( 0, 0 );
4743 m_selection
->UpdateRows( pos
, numRows
);
4744 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4746 attrProvider
->UpdateAttrRows( pos
, numRows
);
4748 if ( !GetBatchCount() )
4751 m_rowLabelWin
->Refresh();
4757 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4759 int numRows
= msg
.GetCommandInt();
4760 int oldNumRows
= m_numRows
;
4761 m_numRows
+= numRows
;
4763 if ( !m_rowHeights
.IsEmpty() )
4765 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4766 m_rowBottoms
.Add( 0, numRows
);
4769 if ( oldNumRows
> 0 )
4770 bottom
= m_rowBottoms
[oldNumRows
- 1];
4772 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4774 bottom
+= m_rowHeights
[i
];
4775 m_rowBottoms
[i
] = bottom
;
4779 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4781 // if we have just inserted cols into an empty grid the current
4782 // cell will be undefined...
4784 SetCurrentCell( 0, 0 );
4787 if ( !GetBatchCount() )
4790 m_rowLabelWin
->Refresh();
4796 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4798 size_t pos
= msg
.GetCommandInt();
4799 int numRows
= msg
.GetCommandInt2();
4800 m_numRows
-= numRows
;
4802 if ( !m_rowHeights
.IsEmpty() )
4804 m_rowHeights
.RemoveAt( pos
, numRows
);
4805 m_rowBottoms
.RemoveAt( pos
, numRows
);
4808 for ( i
= 0; i
< m_numRows
; i
++ )
4810 h
+= m_rowHeights
[i
];
4811 m_rowBottoms
[i
] = h
;
4817 m_currentCellCoords
= wxGridNoCellCoords
;
4821 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4822 m_currentCellCoords
.Set( 0, 0 );
4826 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4827 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4830 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4832 // ifdef'd out following patch from Paul Gammans
4834 // No need to touch column attributes, unless we
4835 // removed _all_ rows, in this case, we remove
4836 // all column attributes.
4837 // I hate to do this here, but the
4838 // needed data is not available inside UpdateAttrRows.
4839 if ( !GetNumberRows() )
4840 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4844 if ( !GetBatchCount() )
4847 m_rowLabelWin
->Refresh();
4853 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4855 size_t pos
= msg
.GetCommandInt();
4856 int numCols
= msg
.GetCommandInt2();
4857 m_numCols
+= numCols
;
4859 if ( !m_colAt
.IsEmpty() )
4861 //Shift the column IDs
4863 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4865 if ( m_colAt
[i
] >= (int)pos
)
4866 m_colAt
[i
] += numCols
;
4869 m_colAt
.Insert( pos
, pos
, numCols
);
4871 //Set the new columns' positions
4872 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4878 if ( !m_colWidths
.IsEmpty() )
4880 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4881 m_colRights
.Insert( 0, pos
, numCols
);
4885 right
= m_colRights
[GetColAt( pos
- 1 )];
4888 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4890 i
= GetColAt( colPos
);
4892 right
+= m_colWidths
[i
];
4893 m_colRights
[i
] = right
;
4897 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4899 // if we have just inserted cols into an empty grid the current
4900 // cell will be undefined...
4902 SetCurrentCell( 0, 0 );
4906 m_selection
->UpdateCols( pos
, numCols
);
4907 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4909 attrProvider
->UpdateAttrCols( pos
, numCols
);
4910 if ( !GetBatchCount() )
4913 m_colLabelWin
->Refresh();
4919 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4921 int numCols
= msg
.GetCommandInt();
4922 int oldNumCols
= m_numCols
;
4923 m_numCols
+= numCols
;
4925 if ( !m_colAt
.IsEmpty() )
4927 m_colAt
.Add( 0, numCols
);
4929 //Set the new columns' positions
4931 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4937 if ( !m_colWidths
.IsEmpty() )
4939 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4940 m_colRights
.Add( 0, numCols
);
4943 if ( oldNumCols
> 0 )
4944 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4947 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4949 i
= GetColAt( colPos
);
4951 right
+= m_colWidths
[i
];
4952 m_colRights
[i
] = right
;
4956 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4958 // if we have just inserted cols into an empty grid the current
4959 // cell will be undefined...
4961 SetCurrentCell( 0, 0 );
4963 if ( !GetBatchCount() )
4966 m_colLabelWin
->Refresh();
4972 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4974 size_t pos
= msg
.GetCommandInt();
4975 int numCols
= msg
.GetCommandInt2();
4976 m_numCols
-= numCols
;
4978 if ( !m_colAt
.IsEmpty() )
4980 int colID
= GetColAt( pos
);
4982 m_colAt
.RemoveAt( pos
, numCols
);
4984 //Shift the column IDs
4986 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4988 if ( m_colAt
[colPos
] > colID
)
4989 m_colAt
[colPos
] -= numCols
;
4993 if ( !m_colWidths
.IsEmpty() )
4995 m_colWidths
.RemoveAt( pos
, numCols
);
4996 m_colRights
.RemoveAt( pos
, numCols
);
5000 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5002 i
= GetColAt( colPos
);
5004 w
+= m_colWidths
[i
];
5011 m_currentCellCoords
= wxGridNoCellCoords
;
5015 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5016 m_currentCellCoords
.Set( 0, 0 );
5020 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5021 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5024 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5026 // ifdef'd out following patch from Paul Gammans
5028 // No need to touch row attributes, unless we
5029 // removed _all_ columns, in this case, we remove
5030 // all row attributes.
5031 // I hate to do this here, but the
5032 // needed data is not available inside UpdateAttrCols.
5033 if ( !GetNumberCols() )
5034 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5038 if ( !GetBatchCount() )
5041 m_colLabelWin
->Refresh();
5048 if (result
&& !GetBatchCount() )
5049 m_gridWin
->Refresh();
5054 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5056 wxRegionIterator
iter( reg
);
5059 wxArrayInt rowlabels
;
5066 // TODO: remove this when we can...
5067 // There is a bug in wxMotif that gives garbage update
5068 // rectangles if you jump-scroll a long way by clicking the
5069 // scrollbar with middle button. This is a work-around
5071 #if defined(__WXMOTIF__)
5073 m_gridWin
->GetClientSize( &cw
, &ch
);
5074 if ( r
.GetTop() > ch
)
5076 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5079 // logical bounds of update region
5082 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5083 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5085 // find the row labels within these bounds
5088 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5090 if ( GetRowBottom(row
) < top
)
5093 if ( GetRowTop(row
) > bottom
)
5096 rowlabels
.Add( row
);
5105 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5107 wxRegionIterator
iter( reg
);
5110 wxArrayInt colLabels
;
5117 // TODO: remove this when we can...
5118 // There is a bug in wxMotif that gives garbage update
5119 // rectangles if you jump-scroll a long way by clicking the
5120 // scrollbar with middle button. This is a work-around
5122 #if defined(__WXMOTIF__)
5124 m_gridWin
->GetClientSize( &cw
, &ch
);
5125 if ( r
.GetLeft() > cw
)
5127 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5130 // logical bounds of update region
5133 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5134 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5136 // find the cells within these bounds
5140 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5142 col
= GetColAt( colPos
);
5144 if ( GetColRight(col
) < left
)
5147 if ( GetColLeft(col
) > right
)
5150 colLabels
.Add( col
);
5159 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5161 wxRegionIterator
iter( reg
);
5164 wxGridCellCoordsArray cellsExposed
;
5166 int left
, top
, right
, bottom
;
5171 // TODO: remove this when we can...
5172 // There is a bug in wxMotif that gives garbage update
5173 // rectangles if you jump-scroll a long way by clicking the
5174 // scrollbar with middle button. This is a work-around
5176 #if defined(__WXMOTIF__)
5178 m_gridWin
->GetClientSize( &cw
, &ch
);
5179 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5180 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5181 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5182 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5185 // logical bounds of update region
5187 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5188 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5190 // find the cells within these bounds
5193 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5195 if ( GetRowBottom(row
) <= top
)
5198 if ( GetRowTop(row
) > bottom
)
5202 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5204 col
= GetColAt( colPos
);
5206 if ( GetColRight(col
) <= left
)
5209 if ( GetColLeft(col
) > right
)
5212 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5219 return cellsExposed
;
5223 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5226 wxPoint
pos( event
.GetPosition() );
5227 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5229 if ( event
.Dragging() )
5233 m_isDragging
= true;
5234 m_rowLabelWin
->CaptureMouse();
5237 if ( event
.LeftIsDown() )
5239 switch ( m_cursorMode
)
5241 case WXGRID_CURSOR_RESIZE_ROW
:
5243 int cw
, ch
, left
, dummy
;
5244 m_gridWin
->GetClientSize( &cw
, &ch
);
5245 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5247 wxClientDC
dc( m_gridWin
);
5250 GetRowTop(m_dragRowOrCol
) +
5251 GetRowMinimalHeight(m_dragRowOrCol
) );
5252 dc
.SetLogicalFunction(wxINVERT
);
5253 if ( m_dragLastPos
>= 0 )
5255 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5257 dc
.DrawLine( left
, y
, left
+cw
, y
);
5262 case WXGRID_CURSOR_SELECT_ROW
:
5264 if ( (row
= YToRow( y
)) >= 0 )
5268 m_selection
->SelectRow( row
,
5269 event
.ControlDown(),
5278 // default label to suppress warnings about "enumeration value
5279 // 'xxx' not handled in switch
5287 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5292 if (m_rowLabelWin
->HasCapture())
5293 m_rowLabelWin
->ReleaseMouse();
5294 m_isDragging
= false;
5297 // ------------ Entering or leaving the window
5299 if ( event
.Entering() || event
.Leaving() )
5301 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5304 // ------------ Left button pressed
5306 else if ( event
.LeftDown() )
5308 // don't send a label click event for a hit on the
5309 // edge of the row label - this is probably the user
5310 // wanting to resize the row
5312 if ( YToEdgeOfRow(y
) < 0 )
5316 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5318 if ( !event
.ShiftDown() && !event
.CmdDown() )
5322 if ( event
.ShiftDown() )
5324 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5327 GetNumberCols() - 1,
5328 event
.ControlDown(),
5335 m_selection
->SelectRow( row
,
5336 event
.ControlDown(),
5343 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5348 // starting to drag-resize a row
5349 if ( CanDragRowSize() )
5350 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5354 // ------------ Left double click
5356 else if (event
.LeftDClick() )
5358 row
= YToEdgeOfRow(y
);
5363 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5365 // no default action at the moment
5370 // adjust row height depending on label text
5371 AutoSizeRowLabelSize( row
);
5373 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5378 // ------------ Left button released
5380 else if ( event
.LeftUp() )
5382 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5384 DoEndDragResizeRow();
5386 // Note: we are ending the event *after* doing
5387 // default processing in this case
5389 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5392 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5396 // ------------ Right button down
5398 else if ( event
.RightDown() )
5402 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5404 // no default action at the moment
5408 // ------------ Right double click
5410 else if ( event
.RightDClick() )
5414 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5416 // no default action at the moment
5420 // ------------ No buttons down and mouse moving
5422 else if ( event
.Moving() )
5424 m_dragRowOrCol
= YToEdgeOfRow( y
);
5425 if ( m_dragRowOrCol
>= 0 )
5427 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5429 // don't capture the mouse yet
5430 if ( CanDragRowSize() )
5431 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5434 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5436 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5441 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5444 wxPoint
pos( event
.GetPosition() );
5445 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5447 if ( event
.Dragging() )
5451 m_isDragging
= true;
5452 m_colLabelWin
->CaptureMouse();
5454 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5455 m_dragRowOrCol
= XToCol( x
);
5458 if ( event
.LeftIsDown() )
5460 switch ( m_cursorMode
)
5462 case WXGRID_CURSOR_RESIZE_COL
:
5464 int cw
, ch
, dummy
, top
;
5465 m_gridWin
->GetClientSize( &cw
, &ch
);
5466 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5468 wxClientDC
dc( m_gridWin
);
5471 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5472 GetColMinimalWidth(m_dragRowOrCol
));
5473 dc
.SetLogicalFunction(wxINVERT
);
5474 if ( m_dragLastPos
>= 0 )
5476 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5478 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5483 case WXGRID_CURSOR_SELECT_COL
:
5485 if ( (col
= XToCol( x
)) >= 0 )
5489 m_selection
->SelectCol( col
,
5490 event
.ControlDown(),
5499 case WXGRID_CURSOR_MOVE_COL
:
5502 m_moveToCol
= GetColAt( 0 );
5504 m_moveToCol
= XToCol( x
);
5508 if ( m_moveToCol
< 0 )
5509 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5510 else if ( x
>= (GetColLeft( m_moveToCol
) + (GetColWidth(m_moveToCol
) / 2)) )
5512 m_moveToCol
= GetColAt( GetColPos( m_moveToCol
) + 1 );
5513 if ( m_moveToCol
< 0 )
5514 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5516 markerX
= GetColLeft( m_moveToCol
);
5519 markerX
= GetColLeft( m_moveToCol
);
5521 if ( markerX
!= m_dragLastPos
)
5523 wxClientDC
dc( m_colLabelWin
);
5527 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5531 //Clean up the last indicator
5532 if ( m_dragLastPos
>= 0 )
5534 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5536 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5537 dc
.SetPen(wxNullPen
);
5539 if ( XToCol( m_dragLastPos
) != -1 )
5540 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5543 const wxColour
*color
;
5544 //Moving to the same place? Don't draw a marker
5545 if ( (m_moveToCol
== m_dragRowOrCol
)
5546 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5547 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5548 color
= wxLIGHT_GREY
;
5553 wxPen
pen( *color
, 2 );
5556 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5558 dc
.SetPen(wxNullPen
);
5560 m_dragLastPos
= markerX
- 1;
5565 // default label to suppress warnings about "enumeration value
5566 // 'xxx' not handled in switch
5574 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5579 if (m_colLabelWin
->HasCapture())
5580 m_colLabelWin
->ReleaseMouse();
5581 m_isDragging
= false;
5584 // ------------ Entering or leaving the window
5586 if ( event
.Entering() || event
.Leaving() )
5588 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5591 // ------------ Left button pressed
5593 else if ( event
.LeftDown() )
5595 // don't send a label click event for a hit on the
5596 // edge of the col label - this is probably the user
5597 // wanting to resize the col
5599 if ( XToEdgeOfCol(x
) < 0 )
5603 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5605 if ( m_canDragColMove
)
5607 //Show button as pressed
5608 wxClientDC
dc( m_colLabelWin
);
5609 int colLeft
= GetColLeft( col
);
5610 int colRight
= GetColRight( col
) - 1;
5611 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5612 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5613 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5615 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5619 if ( !event
.ShiftDown() && !event
.CmdDown() )
5623 if ( event
.ShiftDown() )
5625 m_selection
->SelectBlock( 0,
5626 m_currentCellCoords
.GetCol(),
5627 GetNumberRows() - 1, col
,
5628 event
.ControlDown(),
5635 m_selection
->SelectCol( col
,
5636 event
.ControlDown(),
5643 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5649 // starting to drag-resize a col
5651 if ( CanDragColSize() )
5652 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5656 // ------------ Left double click
5658 if ( event
.LeftDClick() )
5660 col
= XToEdgeOfCol(x
);
5665 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5667 // no default action at the moment
5672 // adjust column width depending on label text
5673 AutoSizeColLabelSize( col
);
5675 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5680 // ------------ Left button released
5682 else if ( event
.LeftUp() )
5684 switch ( m_cursorMode
)
5686 case WXGRID_CURSOR_RESIZE_COL
:
5687 DoEndDragResizeCol();
5689 // Note: we are ending the event *after* doing
5690 // default processing in this case
5692 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5695 case WXGRID_CURSOR_MOVE_COL
:
5698 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5701 case WXGRID_CURSOR_SELECT_COL
:
5702 case WXGRID_CURSOR_SELECT_CELL
:
5703 case WXGRID_CURSOR_RESIZE_ROW
:
5704 case WXGRID_CURSOR_SELECT_ROW
:
5705 // nothing to do (?)
5709 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5713 // ------------ Right button down
5715 else if ( event
.RightDown() )
5719 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5721 // no default action at the moment
5725 // ------------ Right double click
5727 else if ( event
.RightDClick() )
5731 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5733 // no default action at the moment
5737 // ------------ No buttons down and mouse moving
5739 else if ( event
.Moving() )
5741 m_dragRowOrCol
= XToEdgeOfCol( x
);
5742 if ( m_dragRowOrCol
>= 0 )
5744 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5746 // don't capture the cursor yet
5747 if ( CanDragColSize() )
5748 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5751 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5753 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5758 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5760 if ( event
.LeftDown() )
5762 // indicate corner label by having both row and
5765 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5770 else if ( event
.LeftDClick() )
5772 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5774 else if ( event
.RightDown() )
5776 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5778 // no default action at the moment
5781 else if ( event
.RightDClick() )
5783 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5785 // no default action at the moment
5790 void wxGrid::CancelMouseCapture()
5792 // cancel operation currently in progress, whatever it is
5795 m_isDragging
= false;
5796 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5797 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5798 m_winCapture
= NULL
;
5800 // remove traces of whatever we drew on screen
5805 void wxGrid::ChangeCursorMode(CursorMode mode
,
5810 static const wxChar
*cursorModes
[] =
5820 wxLogTrace(_T("grid"),
5821 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5822 win
== m_colLabelWin
? _T("colLabelWin")
5823 : win
? _T("rowLabelWin")
5825 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5828 if ( mode
== m_cursorMode
&&
5829 win
== m_winCapture
&&
5830 captureMouse
== (m_winCapture
!= NULL
))
5835 // by default use the grid itself
5841 if (m_winCapture
->HasCapture())
5842 m_winCapture
->ReleaseMouse();
5843 m_winCapture
= (wxWindow
*)NULL
;
5846 m_cursorMode
= mode
;
5848 switch ( m_cursorMode
)
5850 case WXGRID_CURSOR_RESIZE_ROW
:
5851 win
->SetCursor( m_rowResizeCursor
);
5854 case WXGRID_CURSOR_RESIZE_COL
:
5855 win
->SetCursor( m_colResizeCursor
);
5858 case WXGRID_CURSOR_MOVE_COL
:
5859 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5863 win
->SetCursor( *wxSTANDARD_CURSOR
);
5867 // we need to capture mouse when resizing
5868 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5869 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5871 if ( captureMouse
&& resize
)
5873 win
->CaptureMouse();
5878 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5881 wxPoint
pos( event
.GetPosition() );
5882 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5884 wxGridCellCoords coords
;
5885 XYToCell( x
, y
, coords
);
5887 int cell_rows
, cell_cols
;
5888 bool isFirstDrag
= !m_isDragging
;
5889 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5890 if ((cell_rows
< 0) || (cell_cols
< 0))
5892 coords
.SetRow(coords
.GetRow() + cell_rows
);
5893 coords
.SetCol(coords
.GetCol() + cell_cols
);
5896 if ( event
.Dragging() )
5898 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5900 // Don't start doing anything until the mouse has been dragged at
5901 // least 3 pixels in any direction...
5904 if (m_startDragPos
== wxDefaultPosition
)
5906 m_startDragPos
= pos
;
5909 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5913 m_isDragging
= true;
5914 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5916 // Hide the edit control, so it
5917 // won't interfere with drag-shrinking.
5918 if ( IsCellEditControlShown() )
5920 HideCellEditControl();
5921 SaveEditControlValue();
5924 if ( coords
!= wxGridNoCellCoords
)
5926 if ( event
.CmdDown() )
5928 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5929 m_selectingKeyboard
= coords
;
5930 HighlightBlock( m_selectingKeyboard
, coords
);
5932 else if ( CanDragCell() )
5936 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5937 m_selectingKeyboard
= coords
;
5939 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5948 if ( !IsSelection() )
5950 HighlightBlock( coords
, coords
);
5954 HighlightBlock( m_currentCellCoords
, coords
);
5958 if (! IsVisible(coords
))
5960 MakeCellVisible(coords
);
5961 // TODO: need to introduce a delay or something here. The
5962 // scrolling is way too fast, at least under MSW and GTK.
5965 // Have we captured the mouse yet?
5968 m_winCapture
= m_gridWin
;
5969 m_winCapture
->CaptureMouse();
5974 else if ( event
.LeftIsDown() &&
5975 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5977 int cw
, ch
, left
, dummy
;
5978 m_gridWin
->GetClientSize( &cw
, &ch
);
5979 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5981 wxClientDC
dc( m_gridWin
);
5983 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5984 GetRowMinimalHeight(m_dragRowOrCol
) );
5985 dc
.SetLogicalFunction(wxINVERT
);
5986 if ( m_dragLastPos
>= 0 )
5988 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5990 dc
.DrawLine( left
, y
, left
+cw
, y
);
5993 else if ( event
.LeftIsDown() &&
5994 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5996 int cw
, ch
, dummy
, top
;
5997 m_gridWin
->GetClientSize( &cw
, &ch
);
5998 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6000 wxClientDC
dc( m_gridWin
);
6002 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6003 GetColMinimalWidth(m_dragRowOrCol
) );
6004 dc
.SetLogicalFunction(wxINVERT
);
6005 if ( m_dragLastPos
>= 0 )
6007 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6009 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6016 m_isDragging
= false;
6017 m_startDragPos
= wxDefaultPosition
;
6019 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6020 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6023 if ( event
.Entering() || event
.Leaving() )
6025 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6026 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6031 // ------------ Left button pressed
6033 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6035 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6040 if ( !event
.CmdDown() )
6042 if ( event
.ShiftDown() )
6046 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6047 m_currentCellCoords
.GetCol(),
6050 event
.ControlDown(),
6056 else if ( XToEdgeOfCol(x
) < 0 &&
6057 YToEdgeOfRow(y
) < 0 )
6059 DisableCellEditControl();
6060 MakeCellVisible( coords
);
6062 if ( event
.CmdDown() )
6066 m_selection
->ToggleCellSelection( coords
.GetRow(),
6068 event
.ControlDown(),
6073 m_selectingTopLeft
= wxGridNoCellCoords
;
6074 m_selectingBottomRight
= wxGridNoCellCoords
;
6075 m_selectingKeyboard
= coords
;
6079 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6080 coords
!= wxGridNoCellCoords
;
6081 SetCurrentCell( coords
);
6087 // ------------ Left double click
6089 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6091 DisableCellEditControl();
6093 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6095 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6100 // we want double click to select a cell and start editing
6101 // (i.e. to behave in same way as sequence of two slow clicks):
6102 m_waitForSlowClick
= true;
6107 // ------------ Left button released
6109 else if ( event
.LeftUp() )
6111 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6115 if (m_winCapture
->HasCapture())
6116 m_winCapture
->ReleaseMouse();
6117 m_winCapture
= NULL
;
6120 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6123 EnableCellEditControl();
6125 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6126 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6127 editor
->StartingClick();
6131 m_waitForSlowClick
= false;
6133 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6134 m_selectingBottomRight
!= wxGridNoCellCoords
)
6138 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6139 m_selectingTopLeft
.GetCol(),
6140 m_selectingBottomRight
.GetRow(),
6141 m_selectingBottomRight
.GetCol(),
6142 event
.ControlDown(),
6148 m_selectingTopLeft
= wxGridNoCellCoords
;
6149 m_selectingBottomRight
= wxGridNoCellCoords
;
6151 // Show the edit control, if it has been hidden for
6153 ShowCellEditControl();
6156 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6158 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6159 DoEndDragResizeRow();
6161 // Note: we are ending the event *after* doing
6162 // default processing in this case
6164 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6166 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6168 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6169 DoEndDragResizeCol();
6171 // Note: we are ending the event *after* doing
6172 // default processing in this case
6174 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6180 // ------------ Right button down
6182 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6184 DisableCellEditControl();
6185 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6190 // no default action at the moment
6194 // ------------ Right double click
6196 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6198 DisableCellEditControl();
6199 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6204 // no default action at the moment
6208 // ------------ Moving and no button action
6210 else if ( event
.Moving() && !event
.IsButton() )
6212 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6214 // out of grid cell area
6215 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6219 int dragRow
= YToEdgeOfRow( y
);
6220 int dragCol
= XToEdgeOfCol( x
);
6222 // Dragging on the corner of a cell to resize in both
6223 // directions is not implemented yet...
6225 if ( dragRow
>= 0 && dragCol
>= 0 )
6227 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6233 m_dragRowOrCol
= dragRow
;
6235 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6237 if ( CanDragRowSize() && CanDragGridSize() )
6238 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6241 else if ( dragCol
>= 0 )
6243 m_dragRowOrCol
= dragCol
;
6245 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6247 if ( CanDragColSize() && CanDragGridSize() )
6248 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6251 else // Neither on a row or col edge
6253 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6255 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6261 void wxGrid::DoEndDragResizeRow()
6263 if ( m_dragLastPos
>= 0 )
6265 // erase the last line and resize the row
6267 int cw
, ch
, left
, dummy
;
6268 m_gridWin
->GetClientSize( &cw
, &ch
);
6269 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6271 wxClientDC
dc( m_gridWin
);
6273 dc
.SetLogicalFunction( wxINVERT
);
6274 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6275 HideCellEditControl();
6276 SaveEditControlValue();
6278 int rowTop
= GetRowTop(m_dragRowOrCol
);
6279 SetRowSize( m_dragRowOrCol
,
6280 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6282 if ( !GetBatchCount() )
6284 // Only needed to get the correct rect.y:
6285 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6287 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6288 rect
.width
= m_rowLabelWidth
;
6289 rect
.height
= ch
- rect
.y
;
6290 m_rowLabelWin
->Refresh( true, &rect
);
6293 // if there is a multicell block, paint all of it
6296 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6297 int leftCol
= XToCol(left
);
6298 int rightCol
= internalXToCol(left
+ cw
);
6301 for (i
=leftCol
; i
<rightCol
; i
++)
6303 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6304 if (cell_rows
< subtract_rows
)
6305 subtract_rows
= cell_rows
;
6307 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6308 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6309 rect
.height
= ch
- rect
.y
;
6312 m_gridWin
->Refresh( false, &rect
);
6315 ShowCellEditControl();
6320 void wxGrid::DoEndDragResizeCol()
6322 if ( m_dragLastPos
>= 0 )
6324 // erase the last line and resize the col
6326 int cw
, ch
, dummy
, top
;
6327 m_gridWin
->GetClientSize( &cw
, &ch
);
6328 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6330 wxClientDC
dc( m_gridWin
);
6332 dc
.SetLogicalFunction( wxINVERT
);
6333 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6334 HideCellEditControl();
6335 SaveEditControlValue();
6337 int colLeft
= GetColLeft(m_dragRowOrCol
);
6338 SetColSize( m_dragRowOrCol
,
6339 wxMax( m_dragLastPos
- colLeft
,
6340 GetColMinimalWidth(m_dragRowOrCol
) ) );
6342 if ( !GetBatchCount() )
6344 // Only needed to get the correct rect.x:
6345 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6347 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6348 rect
.width
= cw
- rect
.x
;
6349 rect
.height
= m_colLabelHeight
;
6350 m_colLabelWin
->Refresh( true, &rect
);
6353 // if there is a multicell block, paint all of it
6356 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6357 int topRow
= YToRow(top
);
6358 int bottomRow
= internalYToRow(top
+ cw
);
6361 for (i
=topRow
; i
<bottomRow
; i
++)
6363 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6364 if (cell_cols
< subtract_cols
)
6365 subtract_cols
= cell_cols
;
6368 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6369 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6370 rect
.width
= cw
- rect
.x
;
6374 m_gridWin
->Refresh( false, &rect
);
6377 ShowCellEditControl();
6381 void wxGrid::DoEndDragMoveCol()
6383 //The user clicked on the column but didn't actually drag
6384 if ( m_dragLastPos
< 0 )
6386 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6391 if ( m_moveToCol
== -1 )
6392 newPos
= m_numCols
- 1;
6395 newPos
= GetColPos( m_moveToCol
);
6396 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6400 SetColPos( m_dragRowOrCol
, newPos
);
6403 void wxGrid::SetColPos( int colID
, int newPos
)
6405 if ( m_colAt
.IsEmpty() )
6407 m_colAt
.Alloc( m_numCols
);
6410 for ( i
= 0; i
< m_numCols
; i
++ )
6416 int oldPos
= GetColPos( colID
);
6418 //Reshuffle the m_colAt array
6419 if ( newPos
> oldPos
)
6422 for ( i
= oldPos
; i
< newPos
; i
++ )
6424 m_colAt
[i
] = m_colAt
[i
+1];
6430 for ( i
= oldPos
; i
> newPos
; i
-- )
6432 m_colAt
[i
] = m_colAt
[i
-1];
6436 m_colAt
[newPos
] = colID
;
6438 //Recalculate the column rights
6439 if ( !m_colWidths
.IsEmpty() )
6443 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6445 int colID
= GetColAt( colPos
);
6447 colRight
+= m_colWidths
[colID
];
6448 m_colRights
[colID
] = colRight
;
6452 m_colLabelWin
->Refresh();
6453 m_gridWin
->Refresh();
6458 void wxGrid::EnableDragColMove( bool enable
)
6460 if ( m_canDragColMove
== enable
)
6463 m_canDragColMove
= enable
;
6465 if ( !m_canDragColMove
)
6469 //Recalculate the column rights
6470 if ( !m_colWidths
.IsEmpty() )
6474 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6476 colRight
+= m_colWidths
[colPos
];
6477 m_colRights
[colPos
] = colRight
;
6481 m_colLabelWin
->Refresh();
6482 m_gridWin
->Refresh();
6488 // ------ interaction with data model
6490 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6492 switch ( msg
.GetId() )
6494 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6495 return GetModelValues();
6497 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6498 return SetModelValues();
6500 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6501 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6502 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6503 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6504 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6505 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6506 return Redimension( msg
);
6513 // The behaviour of this function depends on the grid table class
6514 // Clear() function. For the default wxGridStringTable class the
6515 // behavious is to replace all cell contents with wxEmptyString but
6516 // not to change the number of rows or cols.
6518 void wxGrid::ClearGrid()
6522 if (IsCellEditControlEnabled())
6523 DisableCellEditControl();
6526 if (!GetBatchCount())
6527 m_gridWin
->Refresh();
6531 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6535 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6541 if (IsCellEditControlEnabled())
6542 DisableCellEditControl();
6544 bool done
= m_table
->InsertRows( pos
, numRows
);
6547 // the table will have sent the results of the insert row
6548 // operation to this view object as a grid table message
6554 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6558 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6564 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6567 // the table will have sent the results of the append row
6568 // operation to this view object as a grid table message
6574 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6578 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6584 if (IsCellEditControlEnabled())
6585 DisableCellEditControl();
6587 bool done
= m_table
->DeleteRows( pos
, numRows
);
6589 // the table will have sent the results of the delete row
6590 // operation to this view object as a grid table message
6596 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6600 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6606 if (IsCellEditControlEnabled())
6607 DisableCellEditControl();
6609 bool done
= m_table
->InsertCols( pos
, numCols
);
6611 // the table will have sent the results of the insert col
6612 // operation to this view object as a grid table message
6618 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6622 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6628 bool done
= m_table
->AppendCols( numCols
);
6630 // the table will have sent the results of the append col
6631 // operation to this view object as a grid table message
6637 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6641 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6647 if (IsCellEditControlEnabled())
6648 DisableCellEditControl();
6650 bool done
= m_table
->DeleteCols( pos
, numCols
);
6652 // the table will have sent the results of the delete col
6653 // operation to this view object as a grid table message
6660 // ----- event handlers
6663 // Generate a grid event based on a mouse event and
6664 // return the result of ProcessEvent()
6666 int wxGrid::SendEvent( const wxEventType type
,
6668 wxMouseEvent
& mouseEv
)
6670 bool claimed
, vetoed
;
6672 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6674 int rowOrCol
= (row
== -1 ? col
: row
);
6676 wxGridSizeEvent
gridEvt( GetId(),
6680 mouseEv
.GetX() + GetRowLabelSize(),
6681 mouseEv
.GetY() + GetColLabelSize(),
6682 mouseEv
.ControlDown(),
6683 mouseEv
.ShiftDown(),
6685 mouseEv
.MetaDown() );
6687 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6688 vetoed
= !gridEvt
.IsAllowed();
6690 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6692 // Right now, it should _never_ end up here!
6693 wxGridRangeSelectEvent
gridEvt( GetId(),
6697 m_selectingBottomRight
,
6699 mouseEv
.ControlDown(),
6700 mouseEv
.ShiftDown(),
6702 mouseEv
.MetaDown() );
6704 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6705 vetoed
= !gridEvt
.IsAllowed();
6707 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6708 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6709 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6710 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6712 wxPoint pos
= mouseEv
.GetPosition();
6714 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6715 pos
.y
+= GetColLabelSize();
6716 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6717 pos
.x
+= GetRowLabelSize();
6719 wxGridEvent
gridEvt( GetId(),
6726 mouseEv
.ControlDown(),
6727 mouseEv
.ShiftDown(),
6729 mouseEv
.MetaDown() );
6730 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6731 vetoed
= !gridEvt
.IsAllowed();
6735 wxGridEvent
gridEvt( GetId(),
6739 mouseEv
.GetX() + GetRowLabelSize(),
6740 mouseEv
.GetY() + GetColLabelSize(),
6742 mouseEv
.ControlDown(),
6743 mouseEv
.ShiftDown(),
6745 mouseEv
.MetaDown() );
6746 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6747 vetoed
= !gridEvt
.IsAllowed();
6750 // A Veto'd event may not be `claimed' so test this first
6754 return claimed
? 1 : 0;
6757 // Generate a grid event of specified type and return the result
6758 // of ProcessEvent().
6760 int wxGrid::SendEvent( const wxEventType type
,
6763 bool claimed
, vetoed
;
6765 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6767 int rowOrCol
= (row
== -1 ? col
: row
);
6769 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6771 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6772 vetoed
= !gridEvt
.IsAllowed();
6776 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6778 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6779 vetoed
= !gridEvt
.IsAllowed();
6782 // A Veto'd event may not be `claimed' so test this first
6786 return claimed
? 1 : 0;
6789 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6791 // needed to prevent zillions of paint events on MSW
6795 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6797 // Don't do anything if between Begin/EndBatch...
6798 // EndBatch() will do all this on the last nested one anyway.
6799 if ( m_created
&& !GetBatchCount() )
6801 // Refresh to get correct scrolled position:
6802 wxScrolledWindow::Refresh(eraseb
, rect
);
6806 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6807 int width_label
, width_cell
, height_label
, height_cell
;
6810 // Copy rectangle can get scroll offsets..
6811 rect_x
= rect
->GetX();
6812 rect_y
= rect
->GetY();
6813 rectWidth
= rect
->GetWidth();
6814 rectHeight
= rect
->GetHeight();
6816 width_label
= m_rowLabelWidth
- rect_x
;
6817 if (width_label
> rectWidth
)
6818 width_label
= rectWidth
;
6820 height_label
= m_colLabelHeight
- rect_y
;
6821 if (height_label
> rectHeight
)
6822 height_label
= rectHeight
;
6824 if (rect_x
> m_rowLabelWidth
)
6826 x
= rect_x
- m_rowLabelWidth
;
6827 width_cell
= rectWidth
;
6832 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6835 if (rect_y
> m_colLabelHeight
)
6837 y
= rect_y
- m_colLabelHeight
;
6838 height_cell
= rectHeight
;
6843 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6846 // Paint corner label part intersecting rect.
6847 if ( width_label
> 0 && height_label
> 0 )
6849 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6850 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6853 // Paint col labels part intersecting rect.
6854 if ( width_cell
> 0 && height_label
> 0 )
6856 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6857 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6860 // Paint row labels part intersecting rect.
6861 if ( width_label
> 0 && height_cell
> 0 )
6863 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6864 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6867 // Paint cell area part intersecting rect.
6868 if ( width_cell
> 0 && height_cell
> 0 )
6870 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6871 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6876 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6877 m_colLabelWin
->Refresh(eraseb
, NULL
);
6878 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6879 m_gridWin
->Refresh(eraseb
, NULL
);
6884 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6886 if (m_targetWindow
!= this) // check whether initialisation has been done
6888 // reposition our children windows
6893 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6895 if ( m_inOnKeyDown
)
6897 // shouldn't be here - we are going round in circles...
6899 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6902 m_inOnKeyDown
= true;
6904 // propagate the event up and see if it gets processed
6905 wxWindow
*parent
= GetParent();
6906 wxKeyEvent
keyEvt( event
);
6907 keyEvt
.SetEventObject( parent
);
6909 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6911 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6913 if (event
.GetKeyCode() == WXK_RIGHT
)
6914 event
.m_keyCode
= WXK_LEFT
;
6915 else if (event
.GetKeyCode() == WXK_LEFT
)
6916 event
.m_keyCode
= WXK_RIGHT
;
6919 // try local handlers
6920 switch ( event
.GetKeyCode() )
6923 if ( event
.ControlDown() )
6924 MoveCursorUpBlock( event
.ShiftDown() );
6926 MoveCursorUp( event
.ShiftDown() );
6930 if ( event
.ControlDown() )
6931 MoveCursorDownBlock( event
.ShiftDown() );
6933 MoveCursorDown( event
.ShiftDown() );
6937 if ( event
.ControlDown() )
6938 MoveCursorLeftBlock( event
.ShiftDown() );
6940 MoveCursorLeft( event
.ShiftDown() );
6944 if ( event
.ControlDown() )
6945 MoveCursorRightBlock( event
.ShiftDown() );
6947 MoveCursorRight( event
.ShiftDown() );
6951 case WXK_NUMPAD_ENTER
:
6952 if ( event
.ControlDown() )
6954 event
.Skip(); // to let the edit control have the return
6958 if ( GetGridCursorRow() < GetNumberRows()-1 )
6960 MoveCursorDown( event
.ShiftDown() );
6964 // at the bottom of a column
6965 DisableCellEditControl();
6975 if (event
.ShiftDown())
6977 if ( GetGridCursorCol() > 0 )
6979 MoveCursorLeft( false );
6984 DisableCellEditControl();
6989 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6991 MoveCursorRight( false );
6996 DisableCellEditControl();
7002 if ( event
.ControlDown() )
7004 MakeCellVisible( 0, 0 );
7005 SetCurrentCell( 0, 0 );
7014 if ( event
.ControlDown() )
7016 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7017 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7034 // Ctrl-Space selects the current column, Shift-Space -- the
7035 // current row and Ctrl-Shift-Space -- everything
7036 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7039 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7043 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7046 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7047 m_selection
->SelectBlock(0, 0,
7048 m_numRows
- 1, m_numCols
- 1);
7052 if ( !IsEditable() )
7054 MoveCursorRight(false);
7057 //else: fall through
7070 m_inOnKeyDown
= false;
7073 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7075 // try local handlers
7077 if ( event
.GetKeyCode() == WXK_SHIFT
)
7079 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7080 m_selectingBottomRight
!= wxGridNoCellCoords
)
7084 m_selection
->SelectBlock(
7085 m_selectingTopLeft
.GetRow(),
7086 m_selectingTopLeft
.GetCol(),
7087 m_selectingBottomRight
.GetRow(),
7088 m_selectingBottomRight
.GetCol(),
7089 event
.ControlDown(),
7096 m_selectingTopLeft
= wxGridNoCellCoords
;
7097 m_selectingBottomRight
= wxGridNoCellCoords
;
7098 m_selectingKeyboard
= wxGridNoCellCoords
;
7102 void wxGrid::OnChar( wxKeyEvent
& event
)
7104 // is it possible to edit the current cell at all?
7105 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7107 // yes, now check whether the cells editor accepts the key
7108 int row
= m_currentCellCoords
.GetRow();
7109 int col
= m_currentCellCoords
.GetCol();
7110 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7111 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7113 // <F2> is special and will always start editing, for
7114 // other keys - ask the editor itself
7115 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7116 || editor
->IsAcceptedKey(event
) )
7118 // ensure cell is visble
7119 MakeCellVisible(row
, col
);
7120 EnableCellEditControl();
7122 // a problem can arise if the cell is not completely
7123 // visible (even after calling MakeCellVisible the
7124 // control is not created and calling StartingKey will
7126 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7127 editor
->StartingKey(event
);
7143 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7147 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7149 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7151 // the event has been intercepted - do nothing
7155 #if !defined(__WXMAC__)
7156 wxClientDC
dc( m_gridWin
);
7160 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7162 DisableCellEditControl();
7164 if ( IsVisible( m_currentCellCoords
, false ) )
7167 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7168 if ( !m_gridLinesEnabled
)
7176 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7178 // Otherwise refresh redraws the highlight!
7179 m_currentCellCoords
= coords
;
7181 #if defined(__WXMAC__)
7182 m_gridWin
->Refresh(true /*, & r */);
7184 DrawGridCellArea( dc
, cells
);
7185 DrawAllGridLines( dc
, r
);
7190 m_currentCellCoords
= coords
;
7192 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7193 #if !defined(__WXMAC__)
7194 DrawCellHighlight( dc
, attr
);
7199 void wxGrid::HighlightBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7201 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7205 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7208 rightCol
= GetNumberCols() - 1;
7210 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7213 bottomRow
= GetNumberRows() - 1;
7217 EnsureFirstLessThanSecond(topRow
, bottomRow
);
7218 EnsureFirstLessThanSecond(leftCol
, rightCol
);
7220 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7221 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7223 // First the case that we selected a completely new area
7224 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7225 m_selectingBottomRight
== wxGridNoCellCoords
)
7228 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7229 wxGridCellCoords ( bottomRow
, rightCol
) );
7230 m_gridWin
->Refresh( false, &rect
);
7233 // Now handle changing an existing selection area.
7234 else if ( m_selectingTopLeft
!= updateTopLeft
||
7235 m_selectingBottomRight
!= updateBottomRight
)
7237 // Compute two optimal update rectangles:
7238 // Either one rectangle is a real subset of the
7239 // other, or they are (almost) disjoint!
7241 bool need_refresh
[4];
7245 need_refresh
[3] = false;
7248 // Store intermediate values
7249 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7250 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7251 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7252 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7254 // Determine the outer/inner coordinates.
7255 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
7256 EnsureFirstLessThanSecond(oldTop
, topRow
);
7257 EnsureFirstLessThanSecond(rightCol
, oldRight
);
7258 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
7260 // Now, either the stuff marked old is the outer
7261 // rectangle or we don't have a situation where one
7262 // is contained in the other.
7264 if ( oldLeft
< leftCol
)
7266 // Refresh the newly selected or deselected
7267 // area to the left of the old or new selection.
7268 need_refresh
[0] = true;
7269 rect
[0] = BlockToDeviceRect(
7270 wxGridCellCoords( oldTop
, oldLeft
),
7271 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7274 if ( oldTop
< topRow
)
7276 // Refresh the newly selected or deselected
7277 // area above the old or new selection.
7278 need_refresh
[1] = true;
7279 rect
[1] = BlockToDeviceRect(
7280 wxGridCellCoords( oldTop
, leftCol
),
7281 wxGridCellCoords( topRow
- 1, rightCol
) );
7284 if ( oldRight
> rightCol
)
7286 // Refresh the newly selected or deselected
7287 // area to the right of the old or new selection.
7288 need_refresh
[2] = true;
7289 rect
[2] = BlockToDeviceRect(
7290 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7291 wxGridCellCoords( oldBottom
, oldRight
) );
7294 if ( oldBottom
> bottomRow
)
7296 // Refresh the newly selected or deselected
7297 // area below the old or new selection.
7298 need_refresh
[3] = true;
7299 rect
[3] = BlockToDeviceRect(
7300 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7301 wxGridCellCoords( oldBottom
, rightCol
) );
7304 // various Refresh() calls
7305 for (i
= 0; i
< 4; i
++ )
7306 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7307 m_gridWin
->Refresh( false, &(rect
[i
]) );
7311 m_selectingTopLeft
= updateTopLeft
;
7312 m_selectingBottomRight
= updateBottomRight
;
7316 // ------ functions to get/send data (see also public functions)
7319 bool wxGrid::GetModelValues()
7321 // Hide the editor, so it won't hide a changed value.
7322 HideCellEditControl();
7326 // all we need to do is repaint the grid
7328 m_gridWin
->Refresh();
7335 bool wxGrid::SetModelValues()
7339 // Disable the editor, so it won't hide a changed value.
7340 // Do we also want to save the current value of the editor first?
7342 DisableCellEditControl();
7346 for ( row
= 0; row
< m_numRows
; row
++ )
7348 for ( col
= 0; col
< m_numCols
; col
++ )
7350 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7360 // Note - this function only draws cells that are in the list of
7361 // exposed cells (usually set from the update region by
7362 // CalcExposedCells)
7364 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7366 if ( !m_numRows
|| !m_numCols
)
7369 int i
, numCells
= cells
.GetCount();
7370 int row
, col
, cell_rows
, cell_cols
;
7371 wxGridCellCoordsArray redrawCells
;
7373 for ( i
= numCells
- 1; i
>= 0; i
-- )
7375 row
= cells
[i
].GetRow();
7376 col
= cells
[i
].GetCol();
7377 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7379 // If this cell is part of a multicell block, find owner for repaint
7380 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7382 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7383 bool marked
= false;
7384 for ( int j
= 0; j
< numCells
; j
++ )
7386 if ( cell
== cells
[j
] )
7395 int count
= redrawCells
.GetCount();
7396 for (int j
= 0; j
< count
; j
++)
7398 if ( cell
== redrawCells
[j
] )
7406 redrawCells
.Add( cell
);
7409 // don't bother drawing this cell
7413 // If this cell is empty, find cell to left that might want to overflow
7414 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7416 for ( int l
= 0; l
< cell_rows
; l
++ )
7418 // find a cell in this row to leave already marked for repaint
7420 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7421 if ((redrawCells
[k
].GetCol() < left
) &&
7422 (redrawCells
[k
].GetRow() == row
))
7424 left
= redrawCells
[k
].GetCol();
7428 left
= 0; // oh well
7430 for (int j
= col
- 1; j
>= left
; j
--)
7432 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7434 if (GetCellOverflow(row
+ l
, j
))
7436 wxGridCellCoords
cell(row
+ l
, j
);
7437 bool marked
= false;
7439 for (int k
= 0; k
< numCells
; k
++)
7441 if ( cell
== cells
[k
] )
7450 int count
= redrawCells
.GetCount();
7451 for (int k
= 0; k
< count
; k
++)
7453 if ( cell
== redrawCells
[k
] )
7460 redrawCells
.Add( cell
);
7469 DrawCell( dc
, cells
[i
] );
7472 numCells
= redrawCells
.GetCount();
7474 for ( i
= numCells
- 1; i
>= 0; i
-- )
7476 DrawCell( dc
, redrawCells
[i
] );
7480 void wxGrid::DrawGridSpace( wxDC
& dc
)
7483 m_gridWin
->GetClientSize( &cw
, &ch
);
7486 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7488 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7489 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7491 if ( right
> rightCol
|| bottom
> bottomRow
)
7494 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7496 dc
.SetBrush(GetDefaultCellBackgroundColour());
7497 dc
.SetPen( *wxTRANSPARENT_PEN
);
7499 if ( right
> rightCol
)
7501 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7504 if ( bottom
> bottomRow
)
7506 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7511 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7513 int row
= coords
.GetRow();
7514 int col
= coords
.GetCol();
7516 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7519 // we draw the cell border ourselves
7520 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7522 bool isCurrent
= coords
== m_currentCellCoords
;
7524 wxRect rect
= CellToRect( row
, col
);
7526 // if the editor is shown, we should use it and not the renderer
7527 // Note: However, only if it is really _shown_, i.e. not hidden!
7528 if ( isCurrent
&& IsCellEditControlShown() )
7530 // NB: this "#if..." is temporary and fixes a problem where the
7531 // edit control is erased by this code after being rendered.
7532 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7533 // implicitly, causing this out-of order render.
7534 #if !defined(__WXMAC__)
7535 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7536 editor
->PaintBackground(rect
, attr
);
7542 // but all the rest is drawn by the cell renderer and hence may be customized
7543 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7544 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7551 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7553 // don't show highlight when the grid doesn't have focus
7557 int row
= m_currentCellCoords
.GetRow();
7558 int col
= m_currentCellCoords
.GetCol();
7560 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7563 wxRect rect
= CellToRect(row
, col
);
7565 // hmmm... what could we do here to show that the cell is disabled?
7566 // for now, I just draw a thinner border than for the other ones, but
7567 // it doesn't look really good
7569 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7573 // The center of the drawn line is where the position/width/height of
7574 // the rectangle is actually at (on wxMSW at least), so the
7575 // size of the rectangle is reduced to compensate for the thickness of
7576 // the line. If this is too strange on non-wxMSW platforms then
7577 // please #ifdef this appropriately.
7578 rect
.x
+= penWidth
/ 2;
7579 rect
.y
+= penWidth
/ 2;
7580 rect
.width
-= penWidth
- 1;
7581 rect
.height
-= penWidth
- 1;
7583 // Now draw the rectangle
7584 // use the cellHighlightColour if the cell is inside a selection, this
7585 // will ensure the cell is always visible.
7586 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7587 : m_cellHighlightColour
,
7589 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7590 dc
.DrawRectangle(rect
);
7594 // VZ: my experiments with 3D borders...
7596 // how to properly set colours for arbitrary bg?
7597 wxCoord x1
= rect
.x
,
7599 x2
= rect
.x
+ rect
.width
- 1,
7600 y2
= rect
.y
+ rect
.height
- 1;
7602 dc
.SetPen(*wxWHITE_PEN
);
7603 dc
.DrawLine(x1
, y1
, x2
, y1
);
7604 dc
.DrawLine(x1
, y1
, x1
, y2
);
7606 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7607 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7609 dc
.SetPen(*wxBLACK_PEN
);
7610 dc
.DrawLine(x1
, y2
, x2
, y2
);
7611 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7615 wxPen
wxGrid::GetDefaultGridLinePen()
7617 return wxPen(GetGridLineColour());
7620 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7622 return GetDefaultGridLinePen();
7625 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7627 return GetDefaultGridLinePen();
7630 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7632 int row
= coords
.GetRow();
7633 int col
= coords
.GetCol();
7634 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7638 wxRect rect
= CellToRect( row
, col
);
7640 // right hand border
7641 dc
.SetPen( GetColGridLinePen(col
) );
7642 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7643 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7646 dc
.SetPen( GetRowGridLinePen(row
) );
7647 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7648 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7651 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7653 // This if block was previously in wxGrid::OnPaint but that doesn't
7654 // seem to get called under wxGTK - MB
7656 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7657 m_numRows
&& m_numCols
)
7659 m_currentCellCoords
.Set(0, 0);
7662 if ( IsCellEditControlShown() )
7664 // don't show highlight when the edit control is shown
7668 // if the active cell was repainted, repaint its highlight too because it
7669 // might have been damaged by the grid lines
7670 size_t count
= cells
.GetCount();
7671 for ( size_t n
= 0; n
< count
; n
++ )
7673 wxGridCellCoords cell
= cells
[n
];
7675 // If we are using attributes, then we may have just exposed another
7676 // cell in a partially-visible merged cluster of cells. If the "anchor"
7677 // (upper left) cell of this merged cluster is the cell indicated by
7678 // m_currentCellCoords, then we need to refresh the cell highlight even
7679 // though the "anchor" itself is not part of our update segment.
7680 if ( CanHaveAttributes() )
7684 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
7687 cell
.SetRow(cell
.GetRow() + rows
);
7690 cell
.SetCol(cell
.GetCol() + cols
);
7693 if ( cell
== m_currentCellCoords
)
7695 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7696 DrawCellHighlight(dc
, attr
);
7704 // This is used to redraw all grid lines e.g. when the grid line colour
7707 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7709 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7712 int top
, bottom
, left
, right
;
7715 m_gridWin
->GetClientSize(&cw
, &ch
);
7716 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7717 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7719 // avoid drawing grid lines past the last row and col
7721 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7722 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7724 // no gridlines inside multicells, clip them out
7725 int leftCol
= GetColPos( internalXToCol(left
) );
7726 int topRow
= internalYToRow(top
);
7727 int rightCol
= GetColPos( internalXToCol(right
) );
7728 int bottomRow
= internalYToRow(bottom
);
7730 wxRegion
clippedcells(0, 0, cw
, ch
);
7732 int cell_rows
, cell_cols
;
7735 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
7737 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
7739 int i
= GetColAt( colPos
);
7741 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7742 if ((cell_rows
> 1) || (cell_cols
> 1))
7744 rect
= CellToRect(j
,i
);
7745 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7746 clippedcells
.Subtract(rect
);
7748 else if ((cell_rows
< 0) || (cell_cols
< 0))
7750 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7751 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7752 clippedcells
.Subtract(rect
);
7757 dc
.SetDeviceClippingRegion( clippedcells
);
7760 // horizontal grid lines
7761 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7763 int bot
= GetRowBottom(i
) - 1;
7770 dc
.SetPen( GetRowGridLinePen(i
) );
7771 dc
.DrawLine( left
, bot
, right
, bot
);
7775 // vertical grid lines
7776 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7778 int i
= GetColAt( colPos
);
7780 int colRight
= GetColRight(i
);
7782 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7786 if ( colRight
> right
)
7789 if ( colRight
>= left
)
7791 dc
.SetPen( GetColGridLinePen(i
) );
7792 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7796 dc
.DestroyClippingRegion();
7799 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7804 const size_t numLabels
= rows
.GetCount();
7805 for ( size_t i
= 0; i
< numLabels
; i
++ )
7807 DrawRowLabel( dc
, rows
[i
] );
7811 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7813 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7818 int rowTop
= GetRowTop(row
),
7819 rowBottom
= GetRowBottom(row
) - 1;
7821 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7822 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7823 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7824 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7826 dc
.SetPen( *wxWHITE_PEN
);
7827 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7828 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7830 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7831 dc
.SetTextForeground( GetLabelTextColour() );
7832 dc
.SetFont( GetLabelFont() );
7835 GetRowLabelAlignment( &hAlign
, &vAlign
);
7838 rect
.SetY( GetRowTop(row
) + 2 );
7839 rect
.SetWidth( m_rowLabelWidth
- 4 );
7840 rect
.SetHeight( GetRowHeight(row
) - 4 );
7841 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7844 void wxGrid::SetUseNativeColLabels( bool native
)
7846 m_nativeColumnLabels
= native
;
7849 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7850 SetColLabelSize( height
);
7853 m_colLabelWin
->Refresh();
7854 m_cornerLabelWin
->Refresh();
7857 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7862 const size_t numLabels
= cols
.GetCount();
7863 for ( size_t i
= 0; i
< numLabels
; i
++ )
7865 DrawColLabel( dc
, cols
[i
] );
7869 void wxGrid::DrawCornerLabel(wxDC
& dc
)
7871 if ( m_nativeColumnLabels
)
7873 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
7876 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
7880 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7881 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
7882 m_rowLabelWidth
- 1, 0 );
7883 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
7884 0, m_colLabelHeight
- 1 );
7885 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
7886 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
7888 dc
.SetPen( *wxWHITE_PEN
);
7889 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
7890 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
7894 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
7896 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7899 int colLeft
= GetColLeft(col
);
7901 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
7903 if ( m_nativeColumnLabels
)
7905 wxRendererNative::Get().DrawHeaderButton(m_colLabelWin
, dc
, rect
, 0);
7909 int colRight
= GetColRight(col
) - 1;
7911 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7912 dc
.DrawLine( colRight
, 0,
7913 colRight
, m_colLabelHeight
- 1 );
7914 dc
.DrawLine( colLeft
, 0,
7916 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7917 colRight
+ 1, m_colLabelHeight
- 1 );
7919 dc
.SetPen( *wxWHITE_PEN
);
7920 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7921 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7924 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7925 dc
.SetTextForeground( GetLabelTextColour() );
7926 dc
.SetFont( GetLabelFont() );
7929 GetColLabelAlignment( &hAlign
, &vAlign
);
7930 const int orient
= GetColLabelTextOrientation();
7933 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
7936 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
7937 // we just have to add textOrientation support
7938 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7939 const wxString
& value
,
7943 int textOrientation
)
7945 wxArrayString lines
;
7947 StringToLines( value
, lines
);
7949 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
7952 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7953 const wxArrayString
& lines
,
7957 int textOrientation
)
7959 if ( lines
.empty() )
7962 wxDCClipper
clip(dc
, rect
);
7967 if ( textOrientation
== wxHORIZONTAL
)
7968 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7970 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7974 switch ( vertAlign
)
7976 case wxALIGN_BOTTOM
:
7977 if ( textOrientation
== wxHORIZONTAL
)
7978 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7980 x
= rect
.x
+ rect
.width
- textWidth
;
7983 case wxALIGN_CENTRE
:
7984 if ( textOrientation
== wxHORIZONTAL
)
7985 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
7987 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
7992 if ( textOrientation
== wxHORIZONTAL
)
7999 // Align each line of a multi-line label
8000 size_t nLines
= lines
.GetCount();
8001 for ( size_t l
= 0; l
< nLines
; l
++ )
8003 const wxString
& line
= lines
[l
];
8007 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8011 wxCoord lineWidth
= 0,
8013 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8015 switch ( horizAlign
)
8018 if ( textOrientation
== wxHORIZONTAL
)
8019 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8021 y
= rect
.y
+ lineWidth
+ 1;
8024 case wxALIGN_CENTRE
:
8025 if ( textOrientation
== wxHORIZONTAL
)
8026 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8028 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8033 if ( textOrientation
== wxHORIZONTAL
)
8036 y
= rect
.y
+ rect
.height
- 1;
8040 if ( textOrientation
== wxHORIZONTAL
)
8042 dc
.DrawText( line
, x
, y
);
8047 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8053 // Split multi-line text up into an array of strings.
8054 // Any existing contents of the string array are preserved.
8056 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8057 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8061 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8062 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8064 while ( startPos
< (int)tVal
.length() )
8066 pos
= tVal
.Mid(startPos
).Find( eol
);
8071 else if ( pos
== 0 )
8073 lines
.Add( wxEmptyString
);
8077 lines
.Add( tVal
.Mid(startPos
, pos
) );
8080 startPos
+= pos
+ 1;
8083 if ( startPos
< (int)tVal
.length() )
8085 lines
.Add( tVal
.Mid( startPos
) );
8089 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8090 const wxArrayString
& lines
,
8091 long *width
, long *height
) const
8095 wxCoord lineW
= 0, lineH
= 0;
8098 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8100 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8101 w
= wxMax( w
, lineW
);
8110 // ------ Batch processing.
8112 void wxGrid::EndBatch()
8114 if ( m_batchCount
> 0 )
8117 if ( !m_batchCount
)
8120 m_rowLabelWin
->Refresh();
8121 m_colLabelWin
->Refresh();
8122 m_cornerLabelWin
->Refresh();
8123 m_gridWin
->Refresh();
8128 // Use this, rather than wxWindow::Refresh(), to force an immediate
8129 // repainting of the grid. Has no effect if you are already inside a
8130 // BeginBatch / EndBatch block.
8132 void wxGrid::ForceRefresh()
8138 bool wxGrid::Enable(bool enable
)
8140 if ( !wxScrolledWindow::Enable(enable
) )
8143 // redraw in the new state
8144 m_gridWin
->Refresh();
8150 // ------ Edit control functions
8153 void wxGrid::EnableEditing( bool edit
)
8155 // TODO: improve this ?
8157 if ( edit
!= m_editable
)
8160 EnableCellEditControl(edit
);
8165 void wxGrid::EnableCellEditControl( bool enable
)
8170 if ( enable
!= m_cellEditCtrlEnabled
)
8174 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8177 // this should be checked by the caller!
8178 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8180 // do it before ShowCellEditControl()
8181 m_cellEditCtrlEnabled
= enable
;
8183 ShowCellEditControl();
8187 //FIXME:add veto support
8188 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8190 HideCellEditControl();
8191 SaveEditControlValue();
8193 // do it after HideCellEditControl()
8194 m_cellEditCtrlEnabled
= enable
;
8199 bool wxGrid::IsCurrentCellReadOnly() const
8202 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8203 bool readonly
= attr
->IsReadOnly();
8209 bool wxGrid::CanEnableCellControl() const
8211 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8212 !IsCurrentCellReadOnly();
8215 bool wxGrid::IsCellEditControlEnabled() const
8217 // the cell edit control might be disable for all cells or just for the
8218 // current one if it's read only
8219 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8222 bool wxGrid::IsCellEditControlShown() const
8224 bool isShown
= false;
8226 if ( m_cellEditCtrlEnabled
)
8228 int row
= m_currentCellCoords
.GetRow();
8229 int col
= m_currentCellCoords
.GetCol();
8230 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8231 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8236 if ( editor
->IsCreated() )
8238 isShown
= editor
->GetControl()->IsShown();
8248 void wxGrid::ShowCellEditControl()
8250 if ( IsCellEditControlEnabled() )
8252 if ( !IsVisible( m_currentCellCoords
, false ) )
8254 m_cellEditCtrlEnabled
= false;
8259 wxRect rect
= CellToRect( m_currentCellCoords
);
8260 int row
= m_currentCellCoords
.GetRow();
8261 int col
= m_currentCellCoords
.GetCol();
8263 // if this is part of a multicell, find owner (topleft)
8264 int cell_rows
, cell_cols
;
8265 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8266 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8270 m_currentCellCoords
.SetRow( row
);
8271 m_currentCellCoords
.SetCol( col
);
8274 // erase the highlight and the cell contents because the editor
8275 // might not cover the entire cell
8276 wxClientDC
dc( m_gridWin
);
8278 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8279 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8280 dc
.SetPen(*wxTRANSPARENT_PEN
);
8281 dc
.DrawRectangle(rect
);
8283 // convert to scrolled coords
8284 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8290 // cell is shifted by one pixel
8291 // However, don't allow x or y to become negative
8292 // since the SetSize() method interprets that as
8299 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8300 if ( !editor
->IsCreated() )
8302 editor
->Create(m_gridWin
, wxID_ANY
,
8303 new wxGridCellEditorEvtHandler(this, editor
));
8305 wxGridEditorCreatedEvent
evt(GetId(),
8306 wxEVT_GRID_EDITOR_CREATED
,
8310 editor
->GetControl());
8311 GetEventHandler()->ProcessEvent(evt
);
8314 // resize editor to overflow into righthand cells if allowed
8315 int maxWidth
= rect
.width
;
8316 wxString value
= GetCellValue(row
, col
);
8317 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8320 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8321 if (maxWidth
< rect
.width
)
8322 maxWidth
= rect
.width
;
8325 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8326 if (rect
.x
+ maxWidth
> client_right
)
8327 maxWidth
= client_right
- rect
.x
;
8329 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8331 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8332 // may have changed earlier
8333 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8336 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8338 // looks weird going over a multicell
8339 if (m_table
->IsEmptyCell( row
, i
) &&
8340 (rect
.width
< maxWidth
) && (c_rows
== 1))
8342 rect
.width
+= GetColWidth( i
);
8348 if (rect
.GetRight() > client_right
)
8349 rect
.SetRight( client_right
- 1 );
8352 editor
->SetCellAttr( attr
);
8353 editor
->SetSize( rect
);
8355 editor
->GetControl()->Move(
8356 editor
->GetControl()->GetPosition().x
+ nXMove
,
8357 editor
->GetControl()->GetPosition().y
);
8358 editor
->Show( true, attr
);
8360 // recalc dimensions in case we need to
8361 // expand the scrolled window to account for editor
8364 editor
->BeginEdit(row
, col
, this);
8365 editor
->SetCellAttr(NULL
);
8373 void wxGrid::HideCellEditControl()
8375 if ( IsCellEditControlEnabled() )
8377 int row
= m_currentCellCoords
.GetRow();
8378 int col
= m_currentCellCoords
.GetCol();
8380 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8381 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8382 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8383 editor
->Show( false );
8387 // return the focus to the grid itself if the editor had it
8389 // note that we must not do this unconditionally to avoid stealing
8390 // focus from the window which just received it if we are hiding the
8391 // editor precisely because we lost focus
8392 if ( editorHadFocus
)
8393 m_gridWin
->SetFocus();
8395 // refresh whole row to the right
8396 wxRect
rect( CellToRect(row
, col
) );
8397 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8398 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8401 // ensure that the pixels under the focus ring get refreshed as well
8402 rect
.Inflate(10, 10);
8405 m_gridWin
->Refresh( false, &rect
);
8409 void wxGrid::SaveEditControlValue()
8411 if ( IsCellEditControlEnabled() )
8413 int row
= m_currentCellCoords
.GetRow();
8414 int col
= m_currentCellCoords
.GetCol();
8416 wxString oldval
= GetCellValue(row
, col
);
8418 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8419 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8420 bool changed
= editor
->EndEdit(row
, col
, this);
8427 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8428 m_currentCellCoords
.GetRow(),
8429 m_currentCellCoords
.GetCol() ) < 0 )
8431 // Event has been vetoed, set the data back.
8432 SetCellValue(row
, col
, oldval
);
8439 // ------ Grid location functions
8440 // Note that all of these functions work with the logical coordinates of
8441 // grid cells and labels so you will need to convert from device
8442 // coordinates for mouse events etc.
8445 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8447 int row
= YToRow(y
);
8448 int col
= XToCol(x
);
8450 if ( row
== -1 || col
== -1 )
8452 coords
= wxGridNoCellCoords
;
8456 coords
.Set( row
, col
);
8460 // Internal Helper function for computing row or column from some
8461 // (unscrolled) coordinate value, using either
8462 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8463 // of m_rowBottoms/m_ColRights to speed up the search!
8465 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8466 const wxArrayInt
& BorderArray
, int nMax
,
8470 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8475 size_t i_max
= coord
/ defaultDist
,
8478 if (BorderArray
.IsEmpty())
8480 if ((int) i_max
< nMax
)
8482 return clipToMinMax
? nMax
- 1 : -1;
8485 if ( i_max
>= BorderArray
.GetCount())
8487 i_max
= BorderArray
.GetCount() - 1;
8491 if ( coord
>= BorderArray
[i_max
])
8495 i_max
= coord
/ minDist
;
8497 i_max
= BorderArray
.GetCount() - 1;
8500 if ( i_max
>= BorderArray
.GetCount())
8501 i_max
= BorderArray
.GetCount() - 1;
8504 if ( coord
>= BorderArray
[i_max
])
8505 return clipToMinMax
? (int)i_max
: -1;
8506 if ( coord
< BorderArray
[0] )
8509 while ( i_max
- i_min
> 0 )
8511 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8512 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8513 if (coord
>= BorderArray
[ i_max
- 1])
8517 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8518 if (coord
< BorderArray
[median
])
8527 int wxGrid::YToRow( int y
) const
8529 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8530 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8533 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8536 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8538 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8540 int maxPos
= x
/ m_defaultColWidth
;
8543 if (m_colRights
.IsEmpty())
8545 if(maxPos
< m_numCols
)
8546 return GetColAt( maxPos
);
8547 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8550 if ( maxPos
>= m_numCols
)
8551 maxPos
= m_numCols
- 1;
8554 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8557 if (m_minAcceptableColWidth
)
8558 maxPos
= x
/ m_minAcceptableColWidth
;
8560 maxPos
= m_numCols
- 1;
8562 if ( maxPos
>= m_numCols
)
8563 maxPos
= m_numCols
- 1;
8566 //X is beyond the last column
8567 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8568 return clipToMinMax
? GetColAt( maxPos
) : -1;
8570 //X is before the first column
8571 if ( x
< m_colRights
[GetColAt( 0 )] )
8572 return GetColAt( 0 );
8574 //Perform a binary search
8575 while ( maxPos
- minPos
> 0 )
8577 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8578 0, _T("wxGrid: internal error in XToCol"));
8580 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8581 return GetColAt( maxPos
);
8584 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8585 if (x
< m_colRights
[GetColAt( median
)])
8590 return GetColAt( maxPos
);
8593 // return the row number that that the y coord is near
8594 // the edge of, or -1 if not near an edge.
8595 // coords can only possibly be near an edge if
8596 // (a) the row/column is large enough to still allow for an "inner" area
8597 // that is _not_ nead the edge (i.e., if the height/width is smaller
8598 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8601 // (b) resizing rows/columns (the thing for which edge detection is
8602 // relevant at all) is enabled.
8604 int wxGrid::YToEdgeOfRow( int y
) const
8607 i
= internalYToRow(y
);
8609 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8611 // We know that we are in row i, test whether we are
8612 // close enough to lower or upper border, respectively.
8613 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8615 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8622 // return the col number that that the x coord is near the edge of, or
8623 // -1 if not near an edge
8624 // See comment at YToEdgeOfRow for conditions on edge detection.
8626 int wxGrid::XToEdgeOfCol( int x
) const
8629 i
= internalXToCol(x
);
8631 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8633 // We know that we are in column i; test whether we are
8634 // close enough to right or left border, respectively.
8635 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8637 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8644 wxRect
wxGrid::CellToRect( int row
, int col
) const
8646 wxRect
rect( -1, -1, -1, -1 );
8648 if ( row
>= 0 && row
< m_numRows
&&
8649 col
>= 0 && col
< m_numCols
)
8651 int i
, cell_rows
, cell_cols
;
8652 rect
.width
= rect
.height
= 0;
8653 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8654 // if negative then find multicell owner
8659 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8661 rect
.x
= GetColLeft(col
);
8662 rect
.y
= GetRowTop(row
);
8663 for (i
=col
; i
< col
+ cell_cols
; i
++)
8664 rect
.width
+= GetColWidth(i
);
8665 for (i
=row
; i
< row
+ cell_rows
; i
++)
8666 rect
.height
+= GetRowHeight(i
);
8669 // if grid lines are enabled, then the area of the cell is a bit smaller
8670 if (m_gridLinesEnabled
)
8679 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8681 // get the cell rectangle in logical coords
8683 wxRect
r( CellToRect( row
, col
) );
8685 // convert to device coords
8687 int left
, top
, right
, bottom
;
8688 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8689 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8691 // check against the client area of the grid window
8693 m_gridWin
->GetClientSize( &cw
, &ch
);
8695 if ( wholeCellVisible
)
8697 // is the cell wholly visible ?
8698 return ( left
>= 0 && right
<= cw
&&
8699 top
>= 0 && bottom
<= ch
);
8703 // is the cell partly visible ?
8705 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8706 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8710 // make the specified cell location visible by doing a minimal amount
8713 void wxGrid::MakeCellVisible( int row
, int col
)
8716 int xpos
= -1, ypos
= -1;
8718 if ( row
>= 0 && row
< m_numRows
&&
8719 col
>= 0 && col
< m_numCols
)
8721 // get the cell rectangle in logical coords
8722 wxRect
r( CellToRect( row
, col
) );
8724 // convert to device coords
8725 int left
, top
, right
, bottom
;
8726 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8727 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8730 m_gridWin
->GetClientSize( &cw
, &ch
);
8736 else if ( bottom
> ch
)
8738 int h
= r
.GetHeight();
8740 for ( i
= row
- 1; i
>= 0; i
-- )
8742 int rowHeight
= GetRowHeight(i
);
8743 if ( h
+ rowHeight
> ch
)
8750 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8751 // have rounding errors (this is important, because if we do,
8752 // we might not scroll at all and some cells won't be redrawn)
8754 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8755 // so just add a full scroll unit...
8756 ypos
+= m_scrollLineY
;
8759 // special handling for wide cells - show always left part of the cell!
8760 // Otherwise, e.g. when stepping from row to row, it would jump between
8761 // left and right part of the cell on every step!
8763 if ( left
< 0 || (right
- left
) >= cw
)
8767 else if ( right
> cw
)
8769 // position the view so that the cell is on the right
8771 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8772 xpos
= x0
+ (right
- cw
);
8774 // see comment for ypos above
8775 xpos
+= m_scrollLineX
;
8778 if ( xpos
!= -1 || ypos
!= -1 )
8781 xpos
/= m_scrollLineX
;
8783 ypos
/= m_scrollLineY
;
8784 Scroll( xpos
, ypos
);
8791 // ------ Grid cursor movement functions
8794 bool wxGrid::MoveCursorUp( bool expandSelection
)
8796 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8797 m_currentCellCoords
.GetRow() >= 0 )
8799 if ( expandSelection
)
8801 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8802 m_selectingKeyboard
= m_currentCellCoords
;
8803 if ( m_selectingKeyboard
.GetRow() > 0 )
8805 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8806 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8807 m_selectingKeyboard
.GetCol() );
8808 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8811 else if ( m_currentCellCoords
.GetRow() > 0 )
8813 int row
= m_currentCellCoords
.GetRow() - 1;
8814 int col
= m_currentCellCoords
.GetCol();
8816 MakeCellVisible( row
, col
);
8817 SetCurrentCell( row
, col
);
8828 bool wxGrid::MoveCursorDown( bool expandSelection
)
8830 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8831 m_currentCellCoords
.GetRow() < m_numRows
)
8833 if ( expandSelection
)
8835 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8836 m_selectingKeyboard
= m_currentCellCoords
;
8837 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8839 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8840 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8841 m_selectingKeyboard
.GetCol() );
8842 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8845 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8847 int row
= m_currentCellCoords
.GetRow() + 1;
8848 int col
= m_currentCellCoords
.GetCol();
8850 MakeCellVisible( row
, col
);
8851 SetCurrentCell( row
, col
);
8862 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8864 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8865 m_currentCellCoords
.GetCol() >= 0 )
8867 if ( expandSelection
)
8869 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8870 m_selectingKeyboard
= m_currentCellCoords
;
8871 if ( m_selectingKeyboard
.GetCol() > 0 )
8873 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8874 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8875 m_selectingKeyboard
.GetCol() );
8876 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8879 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8881 int row
= m_currentCellCoords
.GetRow();
8882 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8885 MakeCellVisible( row
, col
);
8886 SetCurrentCell( row
, col
);
8897 bool wxGrid::MoveCursorRight( bool expandSelection
)
8899 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8900 m_currentCellCoords
.GetCol() < m_numCols
)
8902 if ( expandSelection
)
8904 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8905 m_selectingKeyboard
= m_currentCellCoords
;
8906 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8908 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8909 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8910 m_selectingKeyboard
.GetCol() );
8911 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8914 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8916 int row
= m_currentCellCoords
.GetRow();
8917 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8920 MakeCellVisible( row
, col
);
8921 SetCurrentCell( row
, col
);
8932 bool wxGrid::MovePageUp()
8934 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8937 int row
= m_currentCellCoords
.GetRow();
8941 m_gridWin
->GetClientSize( &cw
, &ch
);
8943 int y
= GetRowTop(row
);
8944 int newRow
= internalYToRow( y
- ch
+ 1 );
8946 if ( newRow
== row
)
8948 // row > 0, so newRow can never be less than 0 here.
8952 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8953 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8961 bool wxGrid::MovePageDown()
8963 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8966 int row
= m_currentCellCoords
.GetRow();
8967 if ( (row
+ 1) < m_numRows
)
8970 m_gridWin
->GetClientSize( &cw
, &ch
);
8972 int y
= GetRowTop(row
);
8973 int newRow
= internalYToRow( y
+ ch
);
8974 if ( newRow
== row
)
8976 // row < m_numRows, so newRow can't overflow here.
8980 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8981 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8989 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8992 m_currentCellCoords
!= wxGridNoCellCoords
&&
8993 m_currentCellCoords
.GetRow() > 0 )
8995 int row
= m_currentCellCoords
.GetRow();
8996 int col
= m_currentCellCoords
.GetCol();
8998 if ( m_table
->IsEmptyCell(row
, col
) )
9000 // starting in an empty cell: find the next block of
9006 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9010 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9012 // starting at the top of a block: find the next block
9018 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9024 // starting within a block: find the top of the block
9029 if ( m_table
->IsEmptyCell(row
, col
) )
9037 MakeCellVisible( row
, col
);
9038 if ( expandSelection
)
9040 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9041 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9046 SetCurrentCell( row
, col
);
9055 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9058 m_currentCellCoords
!= wxGridNoCellCoords
&&
9059 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9061 int row
= m_currentCellCoords
.GetRow();
9062 int col
= m_currentCellCoords
.GetCol();
9064 if ( m_table
->IsEmptyCell(row
, col
) )
9066 // starting in an empty cell: find the next block of
9069 while ( row
< m_numRows
- 1 )
9072 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9076 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9078 // starting at the bottom of a block: find the next block
9081 while ( row
< m_numRows
- 1 )
9084 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9090 // starting within a block: find the bottom of the block
9092 while ( row
< m_numRows
- 1 )
9095 if ( m_table
->IsEmptyCell(row
, col
) )
9103 MakeCellVisible( row
, col
);
9104 if ( expandSelection
)
9106 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9107 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9112 SetCurrentCell( row
, col
);
9121 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9124 m_currentCellCoords
!= wxGridNoCellCoords
&&
9125 m_currentCellCoords
.GetCol() > 0 )
9127 int row
= m_currentCellCoords
.GetRow();
9128 int col
= m_currentCellCoords
.GetCol();
9130 if ( m_table
->IsEmptyCell(row
, col
) )
9132 // starting in an empty cell: find the next block of
9138 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9142 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9144 // starting at the left of a block: find the next block
9150 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9156 // starting within a block: find the left of the block
9161 if ( m_table
->IsEmptyCell(row
, col
) )
9169 MakeCellVisible( row
, col
);
9170 if ( expandSelection
)
9172 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9173 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9178 SetCurrentCell( row
, col
);
9187 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9190 m_currentCellCoords
!= wxGridNoCellCoords
&&
9191 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9193 int row
= m_currentCellCoords
.GetRow();
9194 int col
= m_currentCellCoords
.GetCol();
9196 if ( m_table
->IsEmptyCell(row
, col
) )
9198 // starting in an empty cell: find the next block of
9201 while ( col
< m_numCols
- 1 )
9204 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9208 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9210 // starting at the right of a block: find the next block
9213 while ( col
< m_numCols
- 1 )
9216 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9222 // starting within a block: find the right of the block
9224 while ( col
< m_numCols
- 1 )
9227 if ( m_table
->IsEmptyCell(row
, col
) )
9235 MakeCellVisible( row
, col
);
9236 if ( expandSelection
)
9238 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9239 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9244 SetCurrentCell( row
, col
);
9254 // ------ Label values and formatting
9257 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9260 *horiz
= m_rowLabelHorizAlign
;
9262 *vert
= m_rowLabelVertAlign
;
9265 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9268 *horiz
= m_colLabelHorizAlign
;
9270 *vert
= m_colLabelVertAlign
;
9273 int wxGrid::GetColLabelTextOrientation() const
9275 return m_colLabelTextOrientation
;
9278 wxString
wxGrid::GetRowLabelValue( int row
) const
9282 return m_table
->GetRowLabelValue( row
);
9292 wxString
wxGrid::GetColLabelValue( int col
) const
9296 return m_table
->GetColLabelValue( col
);
9306 void wxGrid::SetRowLabelSize( int width
)
9308 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9310 if ( width
== wxGRID_AUTOSIZE
)
9312 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9315 if ( width
!= m_rowLabelWidth
)
9319 m_rowLabelWin
->Show( false );
9320 m_cornerLabelWin
->Show( false );
9322 else if ( m_rowLabelWidth
== 0 )
9324 m_rowLabelWin
->Show( true );
9325 if ( m_colLabelHeight
> 0 )
9326 m_cornerLabelWin
->Show( true );
9329 m_rowLabelWidth
= width
;
9331 wxScrolledWindow::Refresh( true );
9335 void wxGrid::SetColLabelSize( int height
)
9337 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9339 if ( height
== wxGRID_AUTOSIZE
)
9341 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9344 if ( height
!= m_colLabelHeight
)
9348 m_colLabelWin
->Show( false );
9349 m_cornerLabelWin
->Show( false );
9351 else if ( m_colLabelHeight
== 0 )
9353 m_colLabelWin
->Show( true );
9354 if ( m_rowLabelWidth
> 0 )
9355 m_cornerLabelWin
->Show( true );
9358 m_colLabelHeight
= height
;
9360 wxScrolledWindow::Refresh( true );
9364 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9366 if ( m_labelBackgroundColour
!= colour
)
9368 m_labelBackgroundColour
= colour
;
9369 m_rowLabelWin
->SetBackgroundColour( colour
);
9370 m_colLabelWin
->SetBackgroundColour( colour
);
9371 m_cornerLabelWin
->SetBackgroundColour( colour
);
9373 if ( !GetBatchCount() )
9375 m_rowLabelWin
->Refresh();
9376 m_colLabelWin
->Refresh();
9377 m_cornerLabelWin
->Refresh();
9382 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9384 if ( m_labelTextColour
!= colour
)
9386 m_labelTextColour
= colour
;
9387 if ( !GetBatchCount() )
9389 m_rowLabelWin
->Refresh();
9390 m_colLabelWin
->Refresh();
9395 void wxGrid::SetLabelFont( const wxFont
& font
)
9398 if ( !GetBatchCount() )
9400 m_rowLabelWin
->Refresh();
9401 m_colLabelWin
->Refresh();
9405 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9407 // allow old (incorrect) defs to be used
9410 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9411 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9412 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9417 case wxTOP
: vert
= wxALIGN_TOP
; break;
9418 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9419 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9422 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9424 m_rowLabelHorizAlign
= horiz
;
9427 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9429 m_rowLabelVertAlign
= vert
;
9432 if ( !GetBatchCount() )
9434 m_rowLabelWin
->Refresh();
9438 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9440 // allow old (incorrect) defs to be used
9443 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9444 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9445 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9450 case wxTOP
: vert
= wxALIGN_TOP
; break;
9451 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9452 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9455 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9457 m_colLabelHorizAlign
= horiz
;
9460 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9462 m_colLabelVertAlign
= vert
;
9465 if ( !GetBatchCount() )
9467 m_colLabelWin
->Refresh();
9471 // Note: under MSW, the default column label font must be changed because it
9472 // does not support vertical printing
9474 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9475 // pGrid->SetLabelFont(font);
9476 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9478 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9480 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9481 m_colLabelTextOrientation
= textOrientation
;
9483 if ( !GetBatchCount() )
9484 m_colLabelWin
->Refresh();
9487 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9491 m_table
->SetRowLabelValue( row
, s
);
9492 if ( !GetBatchCount() )
9494 wxRect rect
= CellToRect( row
, 0 );
9495 if ( rect
.height
> 0 )
9497 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9499 rect
.width
= m_rowLabelWidth
;
9500 m_rowLabelWin
->Refresh( true, &rect
);
9506 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9510 m_table
->SetColLabelValue( col
, s
);
9511 if ( !GetBatchCount() )
9513 wxRect rect
= CellToRect( 0, col
);
9514 if ( rect
.width
> 0 )
9516 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9518 rect
.height
= m_colLabelHeight
;
9519 m_colLabelWin
->Refresh( true, &rect
);
9525 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9527 if ( m_gridLineColour
!= colour
)
9529 m_gridLineColour
= colour
;
9531 wxClientDC
dc( m_gridWin
);
9533 DrawAllGridLines( dc
, wxRegion() );
9537 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9539 if ( m_cellHighlightColour
!= colour
)
9541 m_cellHighlightColour
= colour
;
9543 wxClientDC
dc( m_gridWin
);
9545 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9546 DrawCellHighlight(dc
, attr
);
9551 void wxGrid::SetCellHighlightPenWidth(int width
)
9553 if (m_cellHighlightPenWidth
!= width
)
9555 m_cellHighlightPenWidth
= width
;
9557 // Just redrawing the cell highlight is not enough since that won't
9558 // make any visible change if the the thickness is getting smaller.
9559 int row
= m_currentCellCoords
.GetRow();
9560 int col
= m_currentCellCoords
.GetCol();
9561 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9564 wxRect rect
= CellToRect(row
, col
);
9565 m_gridWin
->Refresh(true, &rect
);
9569 void wxGrid::SetCellHighlightROPenWidth(int width
)
9571 if (m_cellHighlightROPenWidth
!= width
)
9573 m_cellHighlightROPenWidth
= width
;
9575 // Just redrawing the cell highlight is not enough since that won't
9576 // make any visible change if the the thickness is getting smaller.
9577 int row
= m_currentCellCoords
.GetRow();
9578 int col
= m_currentCellCoords
.GetCol();
9579 if ( row
== -1 || col
== -1 ||
9580 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9583 wxRect rect
= CellToRect(row
, col
);
9584 m_gridWin
->Refresh(true, &rect
);
9588 void wxGrid::EnableGridLines( bool enable
)
9590 if ( enable
!= m_gridLinesEnabled
)
9592 m_gridLinesEnabled
= enable
;
9594 if ( !GetBatchCount() )
9598 wxClientDC
dc( m_gridWin
);
9600 DrawAllGridLines( dc
, wxRegion() );
9604 m_gridWin
->Refresh();
9610 int wxGrid::GetDefaultRowSize() const
9612 return m_defaultRowHeight
;
9615 int wxGrid::GetRowSize( int row
) const
9617 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9619 return GetRowHeight(row
);
9622 int wxGrid::GetDefaultColSize() const
9624 return m_defaultColWidth
;
9627 int wxGrid::GetColSize( int col
) const
9629 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9631 return GetColWidth(col
);
9634 // ============================================================================
9635 // access to the grid attributes: each of them has a default value in the grid
9636 // itself and may be overidden on a per-cell basis
9637 // ============================================================================
9639 // ----------------------------------------------------------------------------
9640 // setting default attributes
9641 // ----------------------------------------------------------------------------
9643 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9645 m_defaultCellAttr
->SetBackgroundColour(col
);
9647 m_gridWin
->SetBackgroundColour(col
);
9651 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9653 m_defaultCellAttr
->SetTextColour(col
);
9656 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9658 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9661 void wxGrid::SetDefaultCellOverflow( bool allow
)
9663 m_defaultCellAttr
->SetOverflow(allow
);
9666 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9668 m_defaultCellAttr
->SetFont(font
);
9671 // For editors and renderers the type registry takes precedence over the
9672 // default attr, so we need to register the new editor/renderer for the string
9673 // data type in order to make setting a default editor/renderer appear to
9676 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9678 RegisterDataType(wxGRID_VALUE_STRING
,
9680 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9683 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9685 RegisterDataType(wxGRID_VALUE_STRING
,
9686 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9690 // ----------------------------------------------------------------------------
9691 // access to the default attributes
9692 // ----------------------------------------------------------------------------
9694 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9696 return m_defaultCellAttr
->GetBackgroundColour();
9699 wxColour
wxGrid::GetDefaultCellTextColour() const
9701 return m_defaultCellAttr
->GetTextColour();
9704 wxFont
wxGrid::GetDefaultCellFont() const
9706 return m_defaultCellAttr
->GetFont();
9709 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9711 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9714 bool wxGrid::GetDefaultCellOverflow() const
9716 return m_defaultCellAttr
->GetOverflow();
9719 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9721 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9724 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9726 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9729 // ----------------------------------------------------------------------------
9730 // access to cell attributes
9731 // ----------------------------------------------------------------------------
9733 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9735 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9736 wxColour colour
= attr
->GetBackgroundColour();
9742 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9744 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9745 wxColour colour
= attr
->GetTextColour();
9751 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9753 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9754 wxFont font
= attr
->GetFont();
9760 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9762 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9763 attr
->GetAlignment(horiz
, vert
);
9767 bool wxGrid::GetCellOverflow( int row
, int col
) const
9769 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9770 bool allow
= attr
->GetOverflow();
9776 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9778 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9779 attr
->GetSize( num_rows
, num_cols
);
9783 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9785 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9786 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9792 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9794 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9795 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9801 bool wxGrid::IsReadOnly(int row
, int col
) const
9803 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9804 bool isReadOnly
= attr
->IsReadOnly();
9810 // ----------------------------------------------------------------------------
9811 // attribute support: cache, automatic provider creation, ...
9812 // ----------------------------------------------------------------------------
9814 bool wxGrid::CanHaveAttributes() const
9821 return m_table
->CanHaveAttributes();
9824 void wxGrid::ClearAttrCache()
9826 if ( m_attrCache
.row
!= -1 )
9828 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9829 m_attrCache
.attr
= NULL
;
9830 m_attrCache
.row
= -1;
9831 // wxSafeDecRec(...) might cause event processing that accesses
9832 // the cached attribute, if one exists (e.g. by deleting the
9833 // editor stored within the attribute). Therefore it is important
9834 // to invalidate the cache before calling wxSafeDecRef!
9835 wxSafeDecRef(oldAttr
);
9839 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9843 wxGrid
*self
= (wxGrid
*)this; // const_cast
9845 self
->ClearAttrCache();
9846 self
->m_attrCache
.row
= row
;
9847 self
->m_attrCache
.col
= col
;
9848 self
->m_attrCache
.attr
= attr
;
9853 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9855 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9857 *attr
= m_attrCache
.attr
;
9858 wxSafeIncRef(m_attrCache
.attr
);
9860 #ifdef DEBUG_ATTR_CACHE
9861 gs_nAttrCacheHits
++;
9868 #ifdef DEBUG_ATTR_CACHE
9869 gs_nAttrCacheMisses
++;
9876 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9878 wxGridCellAttr
*attr
= NULL
;
9879 // Additional test to avoid looking at the cache e.g. for
9880 // wxNoCellCoords, as this will confuse memory management.
9883 if ( !LookupAttr(row
, col
, &attr
) )
9885 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9886 : (wxGridCellAttr
*)NULL
;
9887 CacheAttr(row
, col
, attr
);
9893 attr
->SetDefAttr(m_defaultCellAttr
);
9897 attr
= m_defaultCellAttr
;
9904 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9906 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9907 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9909 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9910 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9912 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9915 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9917 // artificially inc the ref count to match DecRef() in caller
9919 m_table
->SetAttr(attr
, row
, col
);
9925 // ----------------------------------------------------------------------------
9926 // setting column attributes (wrappers around SetColAttr)
9927 // ----------------------------------------------------------------------------
9929 void wxGrid::SetColFormatBool(int col
)
9931 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9934 void wxGrid::SetColFormatNumber(int col
)
9936 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9939 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9941 wxString typeName
= wxGRID_VALUE_FLOAT
;
9942 if ( (width
!= -1) || (precision
!= -1) )
9944 typeName
<< _T(':') << width
<< _T(',') << precision
;
9947 SetColFormatCustom(col
, typeName
);
9950 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9952 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9954 attr
= new wxGridCellAttr
;
9955 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9956 attr
->SetRenderer(renderer
);
9957 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
9958 attr
->SetEditor(editor
);
9960 SetColAttr(col
, attr
);
9964 // ----------------------------------------------------------------------------
9965 // setting cell attributes: this is forwarded to the table
9966 // ----------------------------------------------------------------------------
9968 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9970 if ( CanHaveAttributes() )
9972 m_table
->SetAttr(attr
, row
, col
);
9981 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9983 if ( CanHaveAttributes() )
9985 m_table
->SetRowAttr(attr
, row
);
9994 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9996 if ( CanHaveAttributes() )
9998 m_table
->SetColAttr(attr
, col
);
10003 wxSafeDecRef(attr
);
10007 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10009 if ( CanHaveAttributes() )
10011 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10012 attr
->SetBackgroundColour(colour
);
10017 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10019 if ( CanHaveAttributes() )
10021 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10022 attr
->SetTextColour(colour
);
10027 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10029 if ( CanHaveAttributes() )
10031 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10032 attr
->SetFont(font
);
10037 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10039 if ( CanHaveAttributes() )
10041 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10042 attr
->SetAlignment(horiz
, vert
);
10047 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10049 if ( CanHaveAttributes() )
10051 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10052 attr
->SetOverflow(allow
);
10057 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10059 if ( CanHaveAttributes() )
10061 int cell_rows
, cell_cols
;
10063 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10064 attr
->GetSize(&cell_rows
, &cell_cols
);
10065 attr
->SetSize(num_rows
, num_cols
);
10068 // Cannot set the size of a cell to 0 or negative values
10069 // While it is perfectly legal to do that, this function cannot
10070 // handle all the possibilies, do it by hand by getting the CellAttr.
10071 // You can only set the size of a cell to 1,1 or greater with this fn
10072 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10073 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10074 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10075 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10077 // if this was already a multicell then "turn off" the other cells first
10078 if ((cell_rows
> 1) || (cell_cols
> 1))
10081 for (j
=row
; j
< row
+ cell_rows
; j
++)
10083 for (i
=col
; i
< col
+ cell_cols
; i
++)
10085 if ((i
!= col
) || (j
!= row
))
10087 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10088 attr_stub
->SetSize( 1, 1 );
10089 attr_stub
->DecRef();
10095 // mark the cells that will be covered by this cell to
10096 // negative or zero values to point back at this cell
10097 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10100 for (j
=row
; j
< row
+ num_rows
; j
++)
10102 for (i
=col
; i
< col
+ num_cols
; i
++)
10104 if ((i
!= col
) || (j
!= row
))
10106 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10107 attr_stub
->SetSize( row
- j
, col
- i
);
10108 attr_stub
->DecRef();
10116 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10118 if ( CanHaveAttributes() )
10120 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10121 attr
->SetRenderer(renderer
);
10126 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10128 if ( CanHaveAttributes() )
10130 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10131 attr
->SetEditor(editor
);
10136 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10138 if ( CanHaveAttributes() )
10140 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10141 attr
->SetReadOnly(isReadOnly
);
10146 // ----------------------------------------------------------------------------
10147 // Data type registration
10148 // ----------------------------------------------------------------------------
10150 void wxGrid::RegisterDataType(const wxString
& typeName
,
10151 wxGridCellRenderer
* renderer
,
10152 wxGridCellEditor
* editor
)
10154 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10158 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10160 wxString typeName
= m_table
->GetTypeName(row
, col
);
10161 return GetDefaultEditorForType(typeName
);
10164 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10166 wxString typeName
= m_table
->GetTypeName(row
, col
);
10167 return GetDefaultRendererForType(typeName
);
10170 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10172 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10173 if ( index
== wxNOT_FOUND
)
10175 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10180 return m_typeRegistry
->GetEditor(index
);
10183 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10185 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10186 if ( index
== wxNOT_FOUND
)
10188 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10193 return m_typeRegistry
->GetRenderer(index
);
10196 // ----------------------------------------------------------------------------
10198 // ----------------------------------------------------------------------------
10200 void wxGrid::EnableDragRowSize( bool enable
)
10202 m_canDragRowSize
= enable
;
10205 void wxGrid::EnableDragColSize( bool enable
)
10207 m_canDragColSize
= enable
;
10210 void wxGrid::EnableDragGridSize( bool enable
)
10212 m_canDragGridSize
= enable
;
10215 void wxGrid::EnableDragCell( bool enable
)
10217 m_canDragCell
= enable
;
10220 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10222 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10224 if ( resizeExistingRows
)
10226 // since we are resizing all rows to the default row size,
10227 // we can simply clear the row heights and row bottoms
10228 // arrays (which also allows us to take advantage of
10229 // some speed optimisations)
10230 m_rowHeights
.Empty();
10231 m_rowBottoms
.Empty();
10232 if ( !GetBatchCount() )
10237 void wxGrid::SetRowSize( int row
, int height
)
10239 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10241 // if < 0 then calculate new height from label
10245 wxArrayString lines
;
10246 wxClientDC
dc(m_rowLabelWin
);
10247 dc
.SetFont(GetLabelFont());
10248 StringToLines(GetRowLabelValue( row
), lines
);
10249 GetTextBoxSize( dc
, lines
, &w
, &h
);
10250 //check that it is not less than the minimal height
10251 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10254 // See comment in SetColSize
10255 if ( height
< GetRowMinimalAcceptableHeight())
10258 if ( m_rowHeights
.IsEmpty() )
10260 // need to really create the array
10264 int h
= wxMax( 0, height
);
10265 int diff
= h
- m_rowHeights
[row
];
10267 m_rowHeights
[row
] = h
;
10268 for ( int i
= row
; i
< m_numRows
; i
++ )
10270 m_rowBottoms
[i
] += diff
;
10273 if ( !GetBatchCount() )
10277 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10279 // we dont allow zero default column width
10280 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10282 if ( resizeExistingCols
)
10284 // since we are resizing all columns to the default column size,
10285 // we can simply clear the col widths and col rights
10286 // arrays (which also allows us to take advantage of
10287 // some speed optimisations)
10288 m_colWidths
.Empty();
10289 m_colRights
.Empty();
10290 if ( !GetBatchCount() )
10295 void wxGrid::SetColSize( int col
, int width
)
10297 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10299 // if < 0 then calculate new width from label
10303 wxArrayString lines
;
10304 wxClientDC
dc(m_colLabelWin
);
10305 dc
.SetFont(GetLabelFont());
10306 StringToLines(GetColLabelValue(col
), lines
);
10307 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10308 GetTextBoxSize( dc
, lines
, &w
, &h
);
10310 GetTextBoxSize( dc
, lines
, &h
, &w
);
10312 //check that it is not less than the minimal width
10313 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10316 // should we check that it's bigger than GetColMinimalWidth(col) here?
10318 // No, because it is reasonable to assume the library user know's
10319 // what he is doing. However we should test against the weaker
10320 // constraint of minimalAcceptableWidth, as this breaks rendering
10322 // This test then fixes sf.net bug #645734
10324 if ( width
< GetColMinimalAcceptableWidth() )
10327 if ( m_colWidths
.IsEmpty() )
10329 // need to really create the array
10333 int w
= wxMax( 0, width
);
10334 int diff
= w
- m_colWidths
[col
];
10335 m_colWidths
[col
] = w
;
10337 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10339 m_colRights
[GetColAt(colPos
)] += diff
;
10342 if ( !GetBatchCount() )
10346 void wxGrid::SetColMinimalWidth( int col
, int width
)
10348 if (width
> GetColMinimalAcceptableWidth())
10350 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10351 m_colMinWidths
[key
] = width
;
10355 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10357 if (width
> GetRowMinimalAcceptableHeight())
10359 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10360 m_rowMinHeights
[key
] = width
;
10364 int wxGrid::GetColMinimalWidth(int col
) const
10366 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10367 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10369 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10372 int wxGrid::GetRowMinimalHeight(int row
) const
10374 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10375 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10377 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10380 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10382 // We do allow a width of 0 since this gives us
10383 // an easy way to temporarily hiding columns.
10385 m_minAcceptableColWidth
= width
;
10388 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10390 // We do allow a height of 0 since this gives us
10391 // an easy way to temporarily hiding rows.
10393 m_minAcceptableRowHeight
= height
;
10396 int wxGrid::GetColMinimalAcceptableWidth() const
10398 return m_minAcceptableColWidth
;
10401 int wxGrid::GetRowMinimalAcceptableHeight() const
10403 return m_minAcceptableRowHeight
;
10406 // ----------------------------------------------------------------------------
10408 // ----------------------------------------------------------------------------
10411 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10413 const bool column
= direction
== wxGRID_COLUMN
;
10415 wxClientDC
dc(m_gridWin
);
10417 // cancel editing of cell
10418 HideCellEditControl();
10419 SaveEditControlValue();
10421 // init both of them to avoid compiler warnings, even if we only need one
10429 wxCoord extent
, extentMax
= 0;
10430 int max
= column
? m_numRows
: m_numCols
;
10431 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10438 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10439 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10442 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10443 extent
= column
? size
.x
: size
.y
;
10444 if ( extent
> extentMax
)
10445 extentMax
= extent
;
10447 renderer
->DecRef();
10453 // now also compare with the column label extent
10455 dc
.SetFont( GetLabelFont() );
10459 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10460 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10464 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10466 extent
= column
? w
: h
;
10467 if ( extent
> extentMax
)
10468 extentMax
= extent
;
10472 // empty column - give default extent (notice that if extentMax is less
10473 // than default extent but != 0, it's OK)
10474 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10479 // leave some space around text
10487 // Ensure automatic width is not less than minimal width. See the
10488 // comment in SetColSize() for explanation of why this isn't done
10489 // in SetColSize().
10491 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10493 SetColSize( col
, extentMax
);
10494 if ( !GetBatchCount() )
10497 m_gridWin
->GetClientSize( &cw
, &ch
);
10498 wxRect
rect ( CellToRect( 0, col
) );
10500 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10501 rect
.width
= cw
- rect
.x
;
10502 rect
.height
= m_colLabelHeight
;
10503 m_colLabelWin
->Refresh( true, &rect
);
10508 // Ensure automatic width is not less than minimal height. See the
10509 // comment in SetColSize() for explanation of why this isn't done
10510 // in SetRowSize().
10512 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10514 SetRowSize(row
, extentMax
);
10515 if ( !GetBatchCount() )
10518 m_gridWin
->GetClientSize( &cw
, &ch
);
10519 wxRect
rect( CellToRect( row
, 0 ) );
10521 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10522 rect
.width
= m_rowLabelWidth
;
10523 rect
.height
= ch
- rect
.y
;
10524 m_rowLabelWin
->Refresh( true, &rect
);
10531 SetColMinimalWidth(col
, extentMax
);
10533 SetRowMinimalHeight(row
, extentMax
);
10537 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10539 // calculate size for the rows or columns?
10540 const bool calcRows
= direction
== wxGRID_ROW
;
10542 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10543 : GetGridColLabelWindow());
10544 dc
.SetFont(GetLabelFont());
10546 // which dimension should we take into account for calculations?
10548 // for columns, the text can be only horizontal so it's easy but for rows
10549 // we also have to take into account the text orientation
10551 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10553 wxArrayString lines
;
10554 wxCoord extentMax
= 0;
10556 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10557 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10561 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10562 : GetColLabelValue(rowOrCol
);
10563 StringToLines(label
, lines
);
10566 GetTextBoxSize(dc
, lines
, &w
, &h
);
10568 const wxCoord extent
= useWidth
? w
: h
;
10569 if ( extent
> extentMax
)
10570 extentMax
= extent
;
10575 // empty column - give default extent (notice that if extentMax is less
10576 // than default extent but != 0, it's OK)
10577 extentMax
= calcRows
? GetDefaultRowLabelSize()
10578 : GetDefaultColLabelSize();
10581 // leave some space around text (taken from AutoSizeColOrRow)
10590 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10592 int width
= m_rowLabelWidth
;
10594 wxGridUpdateLocker locker
;
10596 locker
.Create(this);
10598 for ( int col
= 0; col
< m_numCols
; col
++ )
10601 AutoSizeColumn(col
, setAsMin
);
10603 width
+= GetColWidth(col
);
10609 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10611 int height
= m_colLabelHeight
;
10613 wxGridUpdateLocker locker
;
10615 locker
.Create(this);
10617 for ( int row
= 0; row
< m_numRows
; row
++ )
10620 AutoSizeRow(row
, setAsMin
);
10622 height
+= GetRowHeight(row
);
10628 void wxGrid::AutoSize()
10630 wxGridUpdateLocker
locker(this);
10632 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10633 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10635 // we know that we're not going to have scrollbars so disable them now to
10636 // avoid trouble in SetClientSize() which can otherwise set the correct
10637 // client size but also leave space for (not needed any more) scrollbars
10638 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10640 // restore the scroll rate parameters overwritten by SetScrollbars()
10641 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10643 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10646 void wxGrid::AutoSizeRowLabelSize( int row
)
10648 // Hide the edit control, so it
10649 // won't interfere with drag-shrinking.
10650 if ( IsCellEditControlShown() )
10652 HideCellEditControl();
10653 SaveEditControlValue();
10656 // autosize row height depending on label text
10657 SetRowSize(row
, -1);
10661 void wxGrid::AutoSizeColLabelSize( int col
)
10663 // Hide the edit control, so it
10664 // won't interfere with drag-shrinking.
10665 if ( IsCellEditControlShown() )
10667 HideCellEditControl();
10668 SaveEditControlValue();
10671 // autosize column width depending on label text
10672 SetColSize(col
, -1);
10676 wxSize
wxGrid::DoGetBestSize() const
10678 wxGrid
*self
= (wxGrid
*)this; // const_cast
10680 // we do the same as in AutoSize() here with the exception that we don't
10681 // change the column/row sizes, only calculate them
10682 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10683 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10685 // NOTE: This size should be cached, but first we need to add calls to
10686 // InvalidateBestSize everywhere that could change the results of this
10688 // CacheBestSize(size);
10690 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10691 + GetWindowBorderSize();
10699 wxPen
& wxGrid::GetDividerPen() const
10704 // ----------------------------------------------------------------------------
10705 // cell value accessor functions
10706 // ----------------------------------------------------------------------------
10708 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10712 m_table
->SetValue( row
, col
, s
);
10713 if ( !GetBatchCount() )
10716 wxRect
rect( CellToRect( row
, col
) );
10718 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10719 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10720 m_gridWin
->Refresh( false, &rect
);
10723 if ( m_currentCellCoords
.GetRow() == row
&&
10724 m_currentCellCoords
.GetCol() == col
&&
10725 IsCellEditControlShown())
10726 // Note: If we are using IsCellEditControlEnabled,
10727 // this interacts badly with calling SetCellValue from
10728 // an EVT_GRID_CELL_CHANGE handler.
10730 HideCellEditControl();
10731 ShowCellEditControl(); // will reread data from table
10736 // ----------------------------------------------------------------------------
10737 // block, row and column selection
10738 // ----------------------------------------------------------------------------
10740 void wxGrid::SelectRow( int row
, bool addToSelected
)
10742 if ( IsSelection() && !addToSelected
)
10746 m_selection
->SelectRow( row
, false, addToSelected
);
10749 void wxGrid::SelectCol( int col
, bool addToSelected
)
10751 if ( IsSelection() && !addToSelected
)
10755 m_selection
->SelectCol( col
, false, addToSelected
);
10758 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10759 bool addToSelected
)
10761 if ( IsSelection() && !addToSelected
)
10765 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10766 false, addToSelected
);
10769 void wxGrid::SelectAll()
10771 if ( m_numRows
> 0 && m_numCols
> 0 )
10774 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10778 // ----------------------------------------------------------------------------
10779 // cell, row and col deselection
10780 // ----------------------------------------------------------------------------
10782 void wxGrid::DeselectRow( int row
)
10784 if ( !m_selection
)
10787 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10789 if ( m_selection
->IsInSelection(row
, 0 ) )
10790 m_selection
->ToggleCellSelection(row
, 0);
10794 int nCols
= GetNumberCols();
10795 for ( int i
= 0; i
< nCols
; i
++ )
10797 if ( m_selection
->IsInSelection(row
, i
) )
10798 m_selection
->ToggleCellSelection(row
, i
);
10803 void wxGrid::DeselectCol( int col
)
10805 if ( !m_selection
)
10808 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10810 if ( m_selection
->IsInSelection(0, col
) )
10811 m_selection
->ToggleCellSelection(0, col
);
10815 int nRows
= GetNumberRows();
10816 for ( int i
= 0; i
< nRows
; i
++ )
10818 if ( m_selection
->IsInSelection(i
, col
) )
10819 m_selection
->ToggleCellSelection(i
, col
);
10824 void wxGrid::DeselectCell( int row
, int col
)
10826 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10827 m_selection
->ToggleCellSelection(row
, col
);
10830 bool wxGrid::IsSelection() const
10832 return ( m_selection
&& (m_selection
->IsSelection() ||
10833 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10834 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10837 bool wxGrid::IsInSelection( int row
, int col
) const
10839 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10840 ( row
>= m_selectingTopLeft
.GetRow() &&
10841 col
>= m_selectingTopLeft
.GetCol() &&
10842 row
<= m_selectingBottomRight
.GetRow() &&
10843 col
<= m_selectingBottomRight
.GetCol() )) );
10846 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10850 wxGridCellCoordsArray a
;
10854 return m_selection
->m_cellSelection
;
10857 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10861 wxGridCellCoordsArray a
;
10865 return m_selection
->m_blockSelectionTopLeft
;
10868 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10872 wxGridCellCoordsArray a
;
10876 return m_selection
->m_blockSelectionBottomRight
;
10879 wxArrayInt
wxGrid::GetSelectedRows() const
10887 return m_selection
->m_rowSelection
;
10890 wxArrayInt
wxGrid::GetSelectedCols() const
10898 return m_selection
->m_colSelection
;
10901 void wxGrid::ClearSelection()
10903 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
10904 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
10905 m_selectingTopLeft
=
10906 m_selectingBottomRight
=
10907 m_selectingKeyboard
= wxGridNoCellCoords
;
10908 Refresh( false, &r1
);
10909 Refresh( false, &r2
);
10911 m_selection
->ClearSelection();
10914 // This function returns the rectangle that encloses the given block
10915 // in device coords clipped to the client size of the grid window.
10917 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
10918 const wxGridCellCoords
& bottomRight
) const
10921 wxRect tempCellRect
= CellToRect(topLeft
);
10922 if ( tempCellRect
!= wxGridNoCellRect
)
10924 resultRect
= tempCellRect
;
10928 resultRect
= wxRect(0, 0, 0, 0);
10931 tempCellRect
= CellToRect(bottomRight
);
10932 if ( tempCellRect
!= wxGridNoCellRect
)
10934 resultRect
+= tempCellRect
;
10938 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
10939 return wxGridNoCellRect
;
10942 // Ensure that left/right and top/bottom pairs are in order.
10943 int left
= resultRect
.GetLeft();
10944 int top
= resultRect
.GetTop();
10945 int right
= resultRect
.GetRight();
10946 int bottom
= resultRect
.GetBottom();
10948 int leftCol
= topLeft
.GetCol();
10949 int topRow
= topLeft
.GetRow();
10950 int rightCol
= bottomRight
.GetCol();
10951 int bottomRow
= bottomRight
.GetRow();
10960 leftCol
= rightCol
;
10971 topRow
= bottomRow
;
10975 // The following loop is ONLY necessary to detect and handle merged cells.
10977 m_gridWin
->GetClientSize( &cw
, &ch
);
10979 // Get the origin coordinates: notice that they will be negative if the
10980 // grid is scrolled downwards/to the right.
10981 int gridOriginX
= 0;
10982 int gridOriginY
= 0;
10983 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
10985 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
10986 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
10988 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
10989 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
10991 // Bound our loop so that we only examine the portion of the selected block
10992 // that is shown on screen. Therefore, we compare the Top-Left block values
10993 // to the Top-Left screen values, and the Bottom-Right block values to the
10994 // Bottom-Right screen values, choosing appropriately.
10995 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
10996 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
10997 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
10998 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11000 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11002 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11004 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11005 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11007 tempCellRect
= CellToRect( j
, i
);
11009 if (tempCellRect
.x
< left
)
11010 left
= tempCellRect
.x
;
11011 if (tempCellRect
.y
< top
)
11012 top
= tempCellRect
.y
;
11013 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11014 right
= tempCellRect
.x
+ tempCellRect
.width
;
11015 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11016 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11020 i
= visibleRightCol
; // jump over inner cells.
11025 // Convert to scrolled coords
11026 CalcScrolledPosition( left
, top
, &left
, &top
);
11027 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11029 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11030 return wxRect(0,0,0,0);
11032 resultRect
.SetLeft( wxMax(0, left
) );
11033 resultRect
.SetTop( wxMax(0, top
) );
11034 resultRect
.SetRight( wxMin(cw
, right
) );
11035 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11040 // ----------------------------------------------------------------------------
11042 // ----------------------------------------------------------------------------
11044 #if wxUSE_DRAG_AND_DROP
11046 // this allow setting drop target directly on wxGrid
11047 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11049 GetGridWindow()->SetDropTarget(dropTarget
);
11052 #endif // wxUSE_DRAG_AND_DROP
11054 // ----------------------------------------------------------------------------
11055 // grid event classes
11056 // ----------------------------------------------------------------------------
11058 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11060 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11061 int row
, int col
, int x
, int y
, bool sel
,
11062 bool control
, bool shift
, bool alt
, bool meta
)
11063 : wxNotifyEvent( type
, id
)
11070 m_control
= control
;
11075 SetEventObject(obj
);
11079 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11081 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11082 int rowOrCol
, int x
, int y
,
11083 bool control
, bool shift
, bool alt
, bool meta
)
11084 : wxNotifyEvent( type
, id
)
11086 m_rowOrCol
= rowOrCol
;
11089 m_control
= control
;
11094 SetEventObject(obj
);
11098 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11100 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11101 const wxGridCellCoords
& topLeft
,
11102 const wxGridCellCoords
& bottomRight
,
11103 bool sel
, bool control
,
11104 bool shift
, bool alt
, bool meta
)
11105 : wxNotifyEvent( type
, id
)
11107 m_topLeft
= topLeft
;
11108 m_bottomRight
= bottomRight
;
11110 m_control
= control
;
11115 SetEventObject(obj
);
11119 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11121 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11122 wxObject
* obj
, int row
,
11123 int col
, wxControl
* ctrl
)
11124 : wxCommandEvent(type
, id
)
11126 SetEventObject(obj
);
11132 #endif // wxUSE_GRID